blob: 21b26b4eb15a7d87b196856b0fef5a6797b5d0e8 [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
6
7This file is part of GNU Zebra.
8
9GNU Zebra is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published
11by the Free Software Foundation; either version 2, or (at your
12option) any later version.
13
14GNU Zebra is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU Zebra; see the file COPYING. If not, write to the
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010021Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22Boston, MA 02110-1301, USA. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020023
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Sylvain Munaut4d8eea42012-12-28 11:58:23 +010027#include <stdbool.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020028#include <syslog.h>
29#include <errno.h>
30#define _XOPEN_SOURCE
31#include <unistd.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020032#include <ctype.h>
33#include <time.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36
37#include <osmocom/vty/vector.h>
38#include <osmocom/vty/vty.h>
39#include <osmocom/vty/command.h>
40
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010041#include <osmocom/core/talloc.h>
Harald Weltea99d45a2015-11-12 13:48:23 +010042#include <osmocom/core/utils.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020043
Harald Weltee881b1b2011-08-17 18:52:30 +020044/*! \addtogroup command
Harald Welte96e2a002017-06-12 21:44:18 +020045 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 * VTY command handling
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020047 *
48 * \file command.c */
Harald Welte7acb30c2011-08-17 17:13:48 +020049
Harald Welte3fb0b6f2010-05-19 19:02:52 +020050#define CONFIGFILE_MASK 022
51
52void *tall_vty_cmd_ctx;
53
54/* Command vector which includes some level of command lists. Normally
55 each daemon maintains each own cmdvec. */
56vector cmdvec;
57
58/* Host information structure. */
59struct host host;
60
61/* Standard command node structures. */
62struct cmd_node auth_node = {
63 AUTH_NODE,
64 "Password: ",
65};
66
67struct cmd_node view_node = {
68 VIEW_NODE,
69 "%s> ",
70};
71
72struct cmd_node auth_enable_node = {
73 AUTH_ENABLE_NODE,
74 "Password: ",
75};
76
77struct cmd_node enable_node = {
78 ENABLE_NODE,
79 "%s# ",
80};
81
82struct cmd_node config_node = {
83 CONFIG_NODE,
84 "%s(config)# ",
85 1
86};
87
88/* Default motd string. */
89const char *default_motd = "";
90
Neels Hofmeyr87e45502017-06-20 00:17:59 +020091/*! print the version (and optionally copyright) information
Harald Welte7acb30c2011-08-17 17:13:48 +020092 *
93 * This is called from main when a daemon is invoked with -v or --version. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020094void print_version(int print_copyright)
95{
Harald Welte237f6242010-05-25 23:00:45 +020096 printf("%s version %s\n", host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097 if (print_copyright)
Harald Welte237f6242010-05-25 23:00:45 +020098 printf("\n%s\n", host.app_info->copyright);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020099}
100
101/* Utility function to concatenate argv argument into a single string
102 with inserting ' ' character between each argument. */
103char *argv_concat(const char **argv, int argc, int shift)
104{
105 int i;
106 size_t len;
107 char *str;
108 char *p;
109
110 len = 0;
111 for (i = shift; i < argc; i++)
112 len += strlen(argv[i]) + 1;
113 if (!len)
114 return NULL;
115 p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat");
116 for (i = shift; i < argc; i++) {
117 size_t arglen;
118 memcpy(p, argv[i], (arglen = strlen(argv[i])));
119 p += arglen;
120 *p++ = ' ';
121 }
122 *(p - 1) = '\0';
123 return str;
124}
125
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200126/* Strip all characters from a string (prompt) except for alnum, '-' and '_'.
127 * For example used to derive a node->name from node->prompt if the user didn't provide a name;
128 * in turn, this name us used for XML IDs in 'show online-help'. */
129static const char *node_name_from_prompt(const char *prompt, char *name_buf, size_t name_buf_size)
130{
131 const char *pos;
132 int dest = 0;
133
134 if (!prompt || !*prompt)
135 return "";
136
137 for (pos = prompt; *pos && dest < (name_buf_size-1); pos++) {
138 if (pos[0] == '%' && pos[1]) {
139 /* skip "%s"; loop pos++ does the second one. */
140 pos++;
141 continue;
142 }
143 if (!(isalnum(pos[0]) || pos[0] == '-' || pos[0] == '_'))
144 continue;
145 name_buf[dest] = pos[0];
146 dest++;
147 }
148 name_buf[dest] = '\0';
149 return name_buf;
150}
151
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +0200152static void install_basic_node_commands(int node);
153
154/*! Install top node of command vector, without adding basic node commands. */
155static void install_node_bare(struct cmd_node *node, int (*func) (struct vty *))
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200156{
157 vector_set_index(cmdvec, node->node, node);
158 node->func = func;
159 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200160 if (!*node->name)
161 node_name_from_prompt(node->prompt, node->name, sizeof(node->name));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162}
163
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +0200164/*! Install top node of command vector. */
165void install_node(struct cmd_node *node, int (*func) (struct vty *))
166{
167 install_node_bare(node, func);
168 install_basic_node_commands(node->node);
169}
170
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200171/* Compare two command's string. Used in sort_node (). */
172static int cmp_node(const void *p, const void *q)
173{
174 struct cmd_element *a = *(struct cmd_element **)p;
175 struct cmd_element *b = *(struct cmd_element **)q;
176
177 return strcmp(a->string, b->string);
178}
179
180static int cmp_desc(const void *p, const void *q)
181{
182 struct desc *a = *(struct desc **)p;
183 struct desc *b = *(struct desc **)q;
184
185 return strcmp(a->cmd, b->cmd);
186}
187
Jacob Erlbeck2442e092013-09-06 16:51:58 +0200188static int is_config_child(struct vty *vty)
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800189{
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800190 if (vty->node <= CONFIG_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800191 return 0;
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800192 else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800193 return 1;
194 else if (host.app_info->is_config_node)
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800195 return host.app_info->is_config_node(vty, vty->node);
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800196 else
197 return vty->node > CONFIG_NODE;
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800198}
199
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200200/*! Sort each node's command element according to command string. */
Harald Welte95b2b472011-07-16 11:58:09 +0200201void sort_node(void)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200202{
203 unsigned int i, j;
204 struct cmd_node *cnode;
205 vector descvec;
206 struct cmd_element *cmd_element;
207
208 for (i = 0; i < vector_active(cmdvec); i++)
209 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
210 vector cmd_vector = cnode->cmd_vector;
211 qsort(cmd_vector->index, vector_active(cmd_vector),
212 sizeof(void *), cmp_node);
213
214 for (j = 0; j < vector_active(cmd_vector); j++)
215 if ((cmd_element =
216 vector_slot(cmd_vector, j)) != NULL
217 && vector_active(cmd_element->strvec)) {
218 descvec =
219 vector_slot(cmd_element->strvec,
220 vector_active
221 (cmd_element->strvec) -
222 1);
223 qsort(descvec->index,
224 vector_active(descvec),
225 sizeof(void *), cmp_desc);
226 }
227 }
228}
229
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200230/*! Break up string in command tokens. Return leading indents.
231 * \param[in] string String to split.
232 * \param[out] indent If not NULL, return a talloc_strdup of indent characters.
233 * \param[out] strvec_p Returns vector of split tokens, must not be NULL.
234 * \returns CMD_SUCCESS or CMD_ERR_INVALID_INDENT
235 *
236 * If \a indent is passed non-NULL, only simple space ' ' indents are allowed,
237 * so that \a indent can simply return the count of leading spaces.
238 * Otherwise any isspace() characters are allowed for indenting (backwards compat).
239 */
240int cmd_make_strvec2(const char *string, char **indent, vector *strvec_p)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200241{
242 const char *cp, *start;
243 char *token;
244 int strlen;
245 vector strvec;
246
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200247 *strvec_p = NULL;
248 if (indent)
249 *indent = 0;
250
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200251 if (string == NULL)
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200252 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200253
254 cp = string;
255
256 /* Skip white spaces. */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200257 while (isspace((int)*cp) && *cp != '\0') {
258 /* if we're counting indents, we need to be strict about them */
259 if (indent && (*cp != ' ') && (*cp != '\t')) {
260 /* Ignore blank lines, they appear as leading whitespace with line breaks. */
261 if (*cp == '\n' || *cp == '\r') {
262 cp++;
263 string = cp;
264 continue;
265 }
266 return CMD_ERR_INVALID_INDENT;
267 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200268 cp++;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200269 }
270
271 if (indent)
272 *indent = talloc_strndup(tall_vty_cmd_ctx, string, cp - string);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200273
274 /* Return if there is only white spaces */
275 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200276 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200277
278 if (*cp == '!' || *cp == '#')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200279 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200280
281 /* Prepare return vector. */
282 strvec = vector_init(VECTOR_MIN_SIZE);
283
284 /* Copy each command piece and set into vector. */
285 while (1) {
286 start = cp;
287 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
288 *cp != '\0')
289 cp++;
290 strlen = cp - start;
291 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
292 memcpy(token, start, strlen);
293 *(token + strlen) = '\0';
294 vector_set(strvec, token);
295
296 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
297 *cp != '\0')
298 cp++;
299
300 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200301 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200302 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200303
304 *strvec_p = strvec;
305 return CMD_SUCCESS;
306}
307
308/*! Breaking up string into each command piece. I assume given
309 character is separated by a space character. Return value is a
310 vector which includes char ** data element. */
311vector cmd_make_strvec(const char *string)
312{
313 vector strvec;
314 cmd_make_strvec2(string, NULL, &strvec);
315 return strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200316}
317
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200318/*! Free allocated string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200319void cmd_free_strvec(vector v)
320{
321 unsigned int i;
322 char *cp;
323
324 if (!v)
325 return;
326
327 for (i = 0; i < vector_active(v); i++)
328 if ((cp = vector_slot(v, i)) != NULL)
329 talloc_free(cp);
330
331 vector_free(v);
332}
333
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200334/*! Fetch next description. Used in \ref cmd_make_descvec(). */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200335static char *cmd_desc_str(const char **string)
336{
337 const char *cp, *start;
338 char *token;
339 int strlen;
340
341 cp = *string;
342
343 if (cp == NULL)
344 return NULL;
345
346 /* Skip white spaces. */
347 while (isspace((int)*cp) && *cp != '\0')
348 cp++;
349
350 /* Return if there is only white spaces */
351 if (*cp == '\0')
352 return NULL;
353
354 start = cp;
355
356 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
357 cp++;
358
359 strlen = cp - start;
360 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
361 memcpy(token, start, strlen);
362 *(token + strlen) = '\0';
363
364 *string = cp;
365
366 return token;
367}
368
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200369/*! New string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200370static vector cmd_make_descvec(const char *string, const char *descstr)
371{
372 int multiple = 0;
373 const char *sp;
374 char *token;
375 int len;
376 const char *cp;
377 const char *dp;
378 vector allvec;
379 vector strvec = NULL;
380 struct desc *desc;
381
382 cp = string;
383 dp = descstr;
384
385 if (cp == NULL)
386 return NULL;
387
388 allvec = vector_init(VECTOR_MIN_SIZE);
389
390 while (1) {
391 while (isspace((int)*cp) && *cp != '\0')
392 cp++;
393
394 if (*cp == '(') {
395 multiple = 1;
396 cp++;
397 }
398 if (*cp == ')') {
399 multiple = 0;
400 cp++;
401 }
402 if (*cp == '|') {
Harald Weltea99d45a2015-11-12 13:48:23 +0100403 OSMO_ASSERT(multiple);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200404 cp++;
405 }
406
407 while (isspace((int)*cp) && *cp != '\0')
408 cp++;
409
410 if (*cp == '(') {
411 multiple = 1;
412 cp++;
413 }
414
415 if (*cp == '\0')
416 return allvec;
417
418 sp = cp;
419
420 while (!
421 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
422 || *cp == ')' || *cp == '|') && *cp != '\0')
423 cp++;
424
425 len = cp - sp;
426
427 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
428 memcpy(token, sp, len);
429 *(token + len) = '\0';
430
431 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
432 desc->cmd = token;
433 desc->str = cmd_desc_str(&dp);
434
435 if (multiple) {
436 if (multiple == 1) {
437 strvec = vector_init(VECTOR_MIN_SIZE);
438 vector_set(allvec, strvec);
439 }
440 multiple++;
441 } else {
442 strvec = vector_init(VECTOR_MIN_SIZE);
443 vector_set(allvec, strvec);
444 }
445 vector_set(strvec, desc);
446 }
447}
448
449/* Count mandantory string vector size. This is to determine inputed
450 command has enough command length. */
451static int cmd_cmdsize(vector strvec)
452{
453 unsigned int i;
454 int size = 0;
455 vector descvec;
456 struct desc *desc;
457
458 for (i = 0; i < vector_active(strvec); i++)
459 if ((descvec = vector_slot(strvec, i)) != NULL) {
460 if ((vector_active(descvec)) == 1
461 && (desc = vector_slot(descvec, 0)) != NULL) {
462 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
463 return size;
464 else
465 size++;
466 } else
467 size++;
468 }
469 return size;
470}
471
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200472/*! Return prompt character of specified node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200473const char *cmd_prompt(enum node_type node)
474{
475 struct cmd_node *cnode;
476
477 cnode = vector_slot(cmdvec, node);
478 return cnode->prompt;
479}
480
Alexander Couzensad580ba2016-05-16 16:01:45 +0200481/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200482 * escape all special asciidoc symbols
Alexander Couzensad580ba2016-05-16 16:01:45 +0200483 * \param unsafe string
484 * \return a new talloc char *
485 */
486char *osmo_asciidoc_escape(const char *inp)
487{
488 int _strlen;
489 char *out, *out_ptr;
490 int len = 0, i, j;
491
492 if (!inp)
493 return NULL;
494 _strlen = strlen(inp);
495
496 for (i = 0; i < _strlen; ++i) {
497 switch (inp[i]) {
498 case '|':
499 len += 2;
500 break;
501 default:
502 len += 1;
503 break;
504 }
505 }
506
507 out = talloc_size(NULL, len + 1);
508 if (!out)
509 return NULL;
510
511 out_ptr = out;
512
513#define ADD(out, str) \
514 for (j = 0; j < strlen(str); ++j) \
515 *(out++) = str[j];
516
517 for (i = 0; i < _strlen; ++i) {
518 switch (inp[i]) {
519 case '|':
520 ADD(out_ptr, "\\|");
521 break;
522 default:
523 *(out_ptr++) = inp[i];
524 break;
525 }
526 }
527
528#undef ADD
529
530 out_ptr[0] = '\0';
531 return out;
532}
533
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100534static char *xml_escape(const char *inp)
535{
536 int _strlen;
537 char *out, *out_ptr;
538 int len = 0, i, j;
539
540 if (!inp)
541 return NULL;
542 _strlen = strlen(inp);
543
544 for (i = 0; i < _strlen; ++i) {
545 switch (inp[i]) {
546 case '"':
547 len += 6;
548 break;
549 case '\'':
550 len += 6;
551 break;
552 case '<':
553 len += 4;
554 break;
555 case '>':
556 len += 4;
557 break;
558 case '&':
559 len += 5;
560 break;
561 default:
562 len += 1;
563 break;
564 }
565 }
566
567 out = talloc_size(NULL, len + 1);
568 if (!out)
569 return NULL;
570
571 out_ptr = out;
572
573#define ADD(out, str) \
574 for (j = 0; j < strlen(str); ++j) \
575 *(out++) = str[j];
576
577 for (i = 0; i < _strlen; ++i) {
578 switch (inp[i]) {
579 case '"':
580 ADD(out_ptr, "&quot;");
581 break;
582 case '\'':
583 ADD(out_ptr, "&apos;");
584 break;
585 case '<':
586 ADD(out_ptr, "&lt;");
587 break;
588 case '>':
589 ADD(out_ptr, "&gt;");
590 break;
591 case '&':
592 ADD(out_ptr, "&amp;");
593 break;
594 default:
595 *(out_ptr++) = inp[i];
596 break;
597 }
598 }
599
600#undef ADD
601
602 out_ptr[0] = '\0';
603 return out;
604}
605
606/*
607 * Write one cmd_element as XML to the given VTY.
608 */
609static int vty_dump_element(struct cmd_element *cmd, struct vty *vty)
610{
611 char *xml_string = xml_escape(cmd->string);
612
613 vty_out(vty, " <command id='%s'>%s", xml_string, VTY_NEWLINE);
614 vty_out(vty, " <params>%s", VTY_NEWLINE);
615
616 int j;
617 for (j = 0; j < vector_count(cmd->strvec); ++j) {
618 vector descvec = vector_slot(cmd->strvec, j);
619 int i;
620 for (i = 0; i < vector_active(descvec); ++i) {
621 char *xml_param, *xml_doc;
622 struct desc *desc = vector_slot(descvec, i);
623 if (desc == NULL)
624 continue;
625
626 xml_param = xml_escape(desc->cmd);
627 xml_doc = xml_escape(desc->str);
628 vty_out(vty, " <param name='%s' doc='%s' />%s",
629 xml_param, xml_doc, VTY_NEWLINE);
630 talloc_free(xml_param);
631 talloc_free(xml_doc);
632 }
633 }
634
635 vty_out(vty, " </params>%s", VTY_NEWLINE);
636 vty_out(vty, " </command>%s", VTY_NEWLINE);
637
638 talloc_free(xml_string);
639 return 0;
640}
641
642/*
643 * Dump all nodes and commands associated with a given node as XML to the VTY.
644 */
645static int vty_dump_nodes(struct vty *vty)
646{
647 int i, j;
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200648 int same_name_count;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100649
650 vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
651
652 for (i = 0; i < vector_active(cmdvec); ++i) {
653 struct cmd_node *cnode;
654 cnode = vector_slot(cmdvec, i);
655 if (!cnode)
656 continue;
657
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200658 /* De-dup node IDs: how many times has this same name been used before? Count the first
659 * occurence as _1 and omit that first suffix, so that the first occurence is called
660 * 'name', the second becomes 'name_2', then 'name_3', ... */
661 same_name_count = 1;
662 for (j = 0; j < i; ++j) {
663 struct cmd_node *cnode2;
664 cnode2 = vector_slot(cmdvec, j);
665 if (!cnode2)
666 continue;
667 if (strcmp(cnode->name, cnode2->name) == 0)
668 same_name_count ++;
669 }
670
671 vty_out(vty, " <node id='%s", cnode->name);
672 if (same_name_count > 1 || !*cnode->name)
673 vty_out(vty, "_%d", same_name_count);
674 vty_out(vty, "'>%s", VTY_NEWLINE);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100675
676 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
677 struct cmd_element *elem;
678 elem = vector_slot(cnode->cmd_vector, j);
679 vty_dump_element(elem, vty);
680 }
681
682 vty_out(vty, " </node>%s", VTY_NEWLINE);
683 }
684
685 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
686
687 return 0;
688}
689
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200690/* Check if a command with given string exists at given node */
Harald Welteaddeaa32017-01-07 12:52:00 +0100691static int check_element_exists(struct cmd_node *cnode, const char *cmdstring)
692{
693 int i;
694
695 for (i = 0; i < vector_active(cnode->cmd_vector); ++i) {
696 struct cmd_element *elem;
697 elem = vector_slot(cnode->cmd_vector, i);
698 if (!elem->string)
699 continue;
700 if (!strcmp(elem->string, cmdstring))
701 return 1;
702 }
703 return 0;
704}
705
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200706/*! Install a command into a node
Harald Welte7acb30c2011-08-17 17:13:48 +0200707 * \param[in] ntype Node Type
708 * \param[cmd] element to be installed
709 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000710void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200711{
712 struct cmd_node *cnode;
713
714 cnode = vector_slot(cmdvec, ntype);
715
Harald Weltea99d45a2015-11-12 13:48:23 +0100716 OSMO_ASSERT(cnode);
Harald Welteaddeaa32017-01-07 12:52:00 +0100717 /* ensure no _identical_ command has been registered at this
718 * node so far */
719 OSMO_ASSERT(!check_element_exists(cnode, cmd->string));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200720
721 vector_set(cnode->cmd_vector, cmd);
722
723 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
724 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
725}
726
727/* Install a command into VIEW and ENABLE node */
728void install_element_ve(struct cmd_element *cmd)
729{
730 install_element(VIEW_NODE, cmd);
731 install_element(ENABLE_NODE, cmd);
732}
733
734#ifdef VTY_CRYPT_PW
735static unsigned char itoa64[] =
736 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
737
738static void to64(char *s, long v, int n)
739{
740 while (--n >= 0) {
741 *s++ = itoa64[v & 0x3f];
742 v >>= 6;
743 }
744}
745
746static char *zencrypt(const char *passwd)
747{
748 char salt[6];
749 struct timeval tv;
750 char *crypt(const char *, const char *);
751
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200752 osmo_gettimeofday(&tv, 0);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200753
754 to64(&salt[0], random(), 3);
755 to64(&salt[3], tv.tv_usec, 3);
756 salt[5] = '\0';
757
758 return crypt(passwd, salt);
759}
760#endif
761
762/* This function write configuration of this host. */
763static int config_write_host(struct vty *vty)
764{
765 if (host.name)
766 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
767
768 if (host.encrypt) {
769 if (host.password_encrypt)
770 vty_out(vty, "password 8 %s%s", host.password_encrypt,
771 VTY_NEWLINE);
772 if (host.enable_encrypt)
773 vty_out(vty, "enable password 8 %s%s",
774 host.enable_encrypt, VTY_NEWLINE);
775 } else {
776 if (host.password)
777 vty_out(vty, "password %s%s", host.password,
778 VTY_NEWLINE);
779 if (host.enable)
780 vty_out(vty, "enable password %s%s", host.enable,
781 VTY_NEWLINE);
782 }
783
784 if (host.advanced)
785 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
786
787 if (host.encrypt)
788 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
789
790 if (host.lines >= 0)
791 vty_out(vty, "service terminal-length %d%s", host.lines,
792 VTY_NEWLINE);
793
794 if (host.motdfile)
795 vty_out(vty, "banner motd file %s%s", host.motdfile,
796 VTY_NEWLINE);
797 else if (!host.motd)
798 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
799
800 return 1;
801}
802
803/* Utility function for getting command vector. */
804static vector cmd_node_vector(vector v, enum node_type ntype)
805{
806 struct cmd_node *cnode = vector_slot(v, ntype);
807 return cnode->cmd_vector;
808}
809
810/* Completion match types. */
811enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100812 no_match = 0,
813 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200814 extend_match,
815 ipv4_prefix_match,
816 ipv4_match,
817 ipv6_prefix_match,
818 ipv6_match,
819 range_match,
820 vararg_match,
821 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100822 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200823};
824
825static enum match_type cmd_ipv4_match(const char *str)
826{
827 const char *sp;
828 int dots = 0, nums = 0;
829 char buf[4];
830
831 if (str == NULL)
832 return partly_match;
833
834 for (;;) {
835 memset(buf, 0, sizeof(buf));
836 sp = str;
837 while (*str != '\0') {
838 if (*str == '.') {
839 if (dots >= 3)
840 return no_match;
841
842 if (*(str + 1) == '.')
843 return no_match;
844
845 if (*(str + 1) == '\0')
846 return partly_match;
847
848 dots++;
849 break;
850 }
851 if (!isdigit((int)*str))
852 return no_match;
853
854 str++;
855 }
856
857 if (str - sp > 3)
858 return no_match;
859
860 strncpy(buf, sp, str - sp);
861 if (atoi(buf) > 255)
862 return no_match;
863
864 nums++;
865
866 if (*str == '\0')
867 break;
868
869 str++;
870 }
871
872 if (nums < 4)
873 return partly_match;
874
875 return exact_match;
876}
877
878static enum match_type cmd_ipv4_prefix_match(const char *str)
879{
880 const char *sp;
881 int dots = 0;
882 char buf[4];
883
884 if (str == NULL)
885 return partly_match;
886
887 for (;;) {
888 memset(buf, 0, sizeof(buf));
889 sp = str;
890 while (*str != '\0' && *str != '/') {
891 if (*str == '.') {
892 if (dots == 3)
893 return no_match;
894
895 if (*(str + 1) == '.' || *(str + 1) == '/')
896 return no_match;
897
898 if (*(str + 1) == '\0')
899 return partly_match;
900
901 dots++;
902 break;
903 }
904
905 if (!isdigit((int)*str))
906 return no_match;
907
908 str++;
909 }
910
911 if (str - sp > 3)
912 return no_match;
913
914 strncpy(buf, sp, str - sp);
915 if (atoi(buf) > 255)
916 return no_match;
917
918 if (dots == 3) {
919 if (*str == '/') {
920 if (*(str + 1) == '\0')
921 return partly_match;
922
923 str++;
924 break;
925 } else if (*str == '\0')
926 return partly_match;
927 }
928
929 if (*str == '\0')
930 return partly_match;
931
932 str++;
933 }
934
935 sp = str;
936 while (*str != '\0') {
937 if (!isdigit((int)*str))
938 return no_match;
939
940 str++;
941 }
942
943 if (atoi(sp) > 32)
944 return no_match;
945
946 return exact_match;
947}
948
949#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
950#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
951#define STATE_START 1
952#define STATE_COLON 2
953#define STATE_DOUBLE 3
954#define STATE_ADDR 4
955#define STATE_DOT 5
956#define STATE_SLASH 6
957#define STATE_MASK 7
958
959#ifdef HAVE_IPV6
960
961static enum match_type cmd_ipv6_match(const char *str)
962{
963 int state = STATE_START;
964 int colons = 0, nums = 0, double_colon = 0;
965 const char *sp = NULL;
966 struct sockaddr_in6 sin6_dummy;
967 int ret;
968
969 if (str == NULL)
970 return partly_match;
971
972 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
973 return no_match;
974
975 /* use inet_pton that has a better support,
976 * for example inet_pton can support the automatic addresses:
977 * ::1.2.3.4
978 */
979 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
980
981 if (ret == 1)
982 return exact_match;
983
984 while (*str != '\0') {
985 switch (state) {
986 case STATE_START:
987 if (*str == ':') {
988 if (*(str + 1) != ':' && *(str + 1) != '\0')
989 return no_match;
990 colons--;
991 state = STATE_COLON;
992 } else {
993 sp = str;
994 state = STATE_ADDR;
995 }
996
997 continue;
998 case STATE_COLON:
999 colons++;
1000 if (*(str + 1) == ':')
1001 state = STATE_DOUBLE;
1002 else {
1003 sp = str + 1;
1004 state = STATE_ADDR;
1005 }
1006 break;
1007 case STATE_DOUBLE:
1008 if (double_colon)
1009 return no_match;
1010
1011 if (*(str + 1) == ':')
1012 return no_match;
1013 else {
1014 if (*(str + 1) != '\0')
1015 colons++;
1016 sp = str + 1;
1017 state = STATE_ADDR;
1018 }
1019
1020 double_colon++;
1021 nums++;
1022 break;
1023 case STATE_ADDR:
1024 if (*(str + 1) == ':' || *(str + 1) == '\0') {
1025 if (str - sp > 3)
1026 return no_match;
1027
1028 nums++;
1029 state = STATE_COLON;
1030 }
1031 if (*(str + 1) == '.')
1032 state = STATE_DOT;
1033 break;
1034 case STATE_DOT:
1035 state = STATE_ADDR;
1036 break;
1037 default:
1038 break;
1039 }
1040
1041 if (nums > 8)
1042 return no_match;
1043
1044 if (colons > 7)
1045 return no_match;
1046
1047 str++;
1048 }
1049
1050#if 0
1051 if (nums < 11)
1052 return partly_match;
1053#endif /* 0 */
1054
1055 return exact_match;
1056}
1057
1058static enum match_type cmd_ipv6_prefix_match(const char *str)
1059{
1060 int state = STATE_START;
1061 int colons = 0, nums = 0, double_colon = 0;
1062 int mask;
1063 const char *sp = NULL;
1064 char *endptr = NULL;
1065
1066 if (str == NULL)
1067 return partly_match;
1068
1069 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
1070 return no_match;
1071
1072 while (*str != '\0' && state != STATE_MASK) {
1073 switch (state) {
1074 case STATE_START:
1075 if (*str == ':') {
1076 if (*(str + 1) != ':' && *(str + 1) != '\0')
1077 return no_match;
1078 colons--;
1079 state = STATE_COLON;
1080 } else {
1081 sp = str;
1082 state = STATE_ADDR;
1083 }
1084
1085 continue;
1086 case STATE_COLON:
1087 colons++;
1088 if (*(str + 1) == '/')
1089 return no_match;
1090 else if (*(str + 1) == ':')
1091 state = STATE_DOUBLE;
1092 else {
1093 sp = str + 1;
1094 state = STATE_ADDR;
1095 }
1096 break;
1097 case STATE_DOUBLE:
1098 if (double_colon)
1099 return no_match;
1100
1101 if (*(str + 1) == ':')
1102 return no_match;
1103 else {
1104 if (*(str + 1) != '\0' && *(str + 1) != '/')
1105 colons++;
1106 sp = str + 1;
1107
1108 if (*(str + 1) == '/')
1109 state = STATE_SLASH;
1110 else
1111 state = STATE_ADDR;
1112 }
1113
1114 double_colon++;
1115 nums += 1;
1116 break;
1117 case STATE_ADDR:
1118 if (*(str + 1) == ':' || *(str + 1) == '.'
1119 || *(str + 1) == '\0' || *(str + 1) == '/') {
1120 if (str - sp > 3)
1121 return no_match;
1122
1123 for (; sp <= str; sp++)
1124 if (*sp == '/')
1125 return no_match;
1126
1127 nums++;
1128
1129 if (*(str + 1) == ':')
1130 state = STATE_COLON;
1131 else if (*(str + 1) == '.')
1132 state = STATE_DOT;
1133 else if (*(str + 1) == '/')
1134 state = STATE_SLASH;
1135 }
1136 break;
1137 case STATE_DOT:
1138 state = STATE_ADDR;
1139 break;
1140 case STATE_SLASH:
1141 if (*(str + 1) == '\0')
1142 return partly_match;
1143
1144 state = STATE_MASK;
1145 break;
1146 default:
1147 break;
1148 }
1149
1150 if (nums > 11)
1151 return no_match;
1152
1153 if (colons > 7)
1154 return no_match;
1155
1156 str++;
1157 }
1158
1159 if (state < STATE_MASK)
1160 return partly_match;
1161
1162 mask = strtol(str, &endptr, 10);
1163 if (*endptr != '\0')
1164 return no_match;
1165
1166 if (mask < 0 || mask > 128)
1167 return no_match;
1168
1169/* I don't know why mask < 13 makes command match partly.
1170 Forgive me to make this comments. I Want to set static default route
1171 because of lack of function to originate default in ospf6d; sorry
1172 yasu
1173 if (mask < 13)
1174 return partly_match;
1175*/
1176
1177 return exact_match;
1178}
1179
1180#endif /* HAVE_IPV6 */
1181
1182#define DECIMAL_STRLEN_MAX 10
1183
1184static int cmd_range_match(const char *range, const char *str)
1185{
1186 char *p;
1187 char buf[DECIMAL_STRLEN_MAX + 1];
1188 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001189
1190 if (str == NULL)
1191 return 1;
1192
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001193 if (range[1] == '-') {
1194 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001195
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001196 val = strtol(str, &endptr, 10);
1197 if (*endptr != '\0')
1198 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001199
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001200 range += 2;
1201 p = strchr(range, '-');
1202 if (p == NULL)
1203 return 0;
1204 if (p - range > DECIMAL_STRLEN_MAX)
1205 return 0;
1206 strncpy(buf, range, p - range);
1207 buf[p - range] = '\0';
1208 min = -strtol(buf, &endptr, 10);
1209 if (*endptr != '\0')
1210 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001211
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001212 range = p + 1;
1213 p = strchr(range, '>');
1214 if (p == NULL)
1215 return 0;
1216 if (p - range > DECIMAL_STRLEN_MAX)
1217 return 0;
1218 strncpy(buf, range, p - range);
1219 buf[p - range] = '\0';
1220 max = strtol(buf, &endptr, 10);
1221 if (*endptr != '\0')
1222 return 0;
1223
1224 if (val < min || val > max)
1225 return 0;
1226 } else {
1227 unsigned long min, max, val;
1228
1229 val = strtoul(str, &endptr, 10);
1230 if (*endptr != '\0')
1231 return 0;
1232
1233 range++;
1234 p = strchr(range, '-');
1235 if (p == NULL)
1236 return 0;
1237 if (p - range > DECIMAL_STRLEN_MAX)
1238 return 0;
1239 strncpy(buf, range, p - range);
1240 buf[p - range] = '\0';
1241 min = strtoul(buf, &endptr, 10);
1242 if (*endptr != '\0')
1243 return 0;
1244
1245 range = p + 1;
1246 p = strchr(range, '>');
1247 if (p == NULL)
1248 return 0;
1249 if (p - range > DECIMAL_STRLEN_MAX)
1250 return 0;
1251 strncpy(buf, range, p - range);
1252 buf[p - range] = '\0';
1253 max = strtoul(buf, &endptr, 10);
1254 if (*endptr != '\0')
1255 return 0;
1256
1257 if (val < min || val > max)
1258 return 0;
1259 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001260
1261 return 1;
1262}
1263
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001264/* helper to retrieve the 'real' argument string from an optional argument */
1265static char *
1266cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001267{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001268 /* we've got "[blah]". We want to strip off the []s and redo the
1269 * match check for "blah"
1270 */
1271 size_t len = strlen(str);
1272 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001273
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001274 if (len < 3)
1275 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001276
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001277 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1278 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001279
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001280 memcpy(tmp, (str + 1), len - 2);
1281 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001282
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001283 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001284}
1285
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001286static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001287cmd_match(const char *str, const char *command,
1288 enum match_type min, bool recur)
1289{
1290
1291 if (recur && CMD_OPTION(str))
1292 {
1293 enum match_type ret;
1294 char *tmp = cmd_deopt(str);
1295
1296 /* this would be a bug in a command, however handle it gracefully
1297 * as it we only discover it if a user tries to run it
1298 */
1299 if (tmp == NULL)
1300 return no_match;
1301
1302 ret = cmd_match(tmp, command, min, false);
1303
1304 talloc_free(tmp);
1305
1306 return ret;
1307 }
1308 else if (CMD_VARARG(str))
1309 return vararg_match;
1310 else if (CMD_RANGE(str))
1311 {
1312 if (cmd_range_match(str, command))
1313 return range_match;
1314 }
1315#ifdef HAVE_IPV6
1316 else if (CMD_IPV6(str))
1317 {
1318 if (cmd_ipv6_match(command) >= min)
1319 return ipv6_match;
1320 }
1321 else if (CMD_IPV6_PREFIX(str))
1322 {
1323 if (cmd_ipv6_prefix_match(command) >= min)
1324 return ipv6_prefix_match;
1325 }
1326#endif /* HAVE_IPV6 */
1327 else if (CMD_IPV4(str))
1328 {
1329 if (cmd_ipv4_match(command) >= min)
1330 return ipv4_match;
1331 }
1332 else if (CMD_IPV4_PREFIX(str))
1333 {
1334 if (cmd_ipv4_prefix_match(command) >= min)
1335 return ipv4_prefix_match;
1336 }
1337 else if (CMD_VARIABLE(str))
1338 return extend_match;
1339 else if (strncmp(command, str, strlen(command)) == 0)
1340 {
1341 if (strcmp(command, str) == 0)
1342 return exact_match;
1343 else if (partly_match >= min)
1344 return partly_match;
1345 }
1346
1347 return no_match;
1348}
1349
1350/* Filter vector at the specified index and by the given command string, to
1351 * the desired matching level (thus allowing part matches), and return match
1352 * type flag.
1353 */
1354static enum match_type
1355cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001356{
1357 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001358 struct cmd_element *cmd_element;
1359 enum match_type match_type;
1360 vector descvec;
1361 struct desc *desc;
1362
1363 match_type = no_match;
1364
1365 /* If command and cmd_element string does not match set NULL to vector */
1366 for (i = 0; i < vector_active(v); i++)
1367 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001368 if (index >= vector_active(cmd_element->strvec))
1369 vector_slot(v, i) = NULL;
1370 else {
1371 unsigned int j;
1372 int matched = 0;
1373
1374 descvec =
1375 vector_slot(cmd_element->strvec, index);
1376
1377 for (j = 0; j < vector_active(descvec); j++)
1378 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001379 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001380
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001381 ret = cmd_match (desc->cmd, command, level, true);
1382
1383 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001384 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001385
1386 if (match_type < ret)
1387 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001388 }
1389 if (!matched)
1390 vector_slot(v, i) = NULL;
1391 }
1392 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001393
1394 if (match_type == no_match)
1395 return no_match;
1396
1397 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1398 * go again and filter out commands whose argument (at this index) is
1399 * 'weaker'. E.g., if we have 2 commands:
1400 *
1401 * foo bar <1-255>
1402 * foo bar BLAH
1403 *
1404 * and the command string is 'foo bar 10', then we will get here with with
1405 * 'range_match' being the strongest match. However, if 'BLAH' came
1406 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1407 *
1408 * If we don't do a 2nd pass and filter it out, the higher-layers will
1409 * consider this to be ambiguous.
1410 */
1411 for (i = 0; i < vector_active(v); i++)
1412 if ((cmd_element = vector_slot(v, i)) != NULL) {
1413 if (index >= vector_active(cmd_element->strvec))
1414 vector_slot(v, i) = NULL;
1415 else {
1416 unsigned int j;
1417 int matched = 0;
1418
1419 descvec =
1420 vector_slot(cmd_element->strvec, index);
1421
1422 for (j = 0; j < vector_active(descvec); j++)
1423 if ((desc = vector_slot(descvec, j))) {
1424 enum match_type ret;
1425
1426 ret = cmd_match(desc->cmd, command, any_match, true);
1427
1428 if (ret >= match_type)
1429 matched++;
1430 }
1431 if (!matched)
1432 vector_slot(v, i) = NULL;
1433 }
1434 }
1435
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001436 return match_type;
1437}
1438
1439/* Check ambiguous match */
1440static int
1441is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1442{
1443 unsigned int i;
1444 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001445 struct cmd_element *cmd_element;
1446 const char *matched = NULL;
1447 vector descvec;
1448 struct desc *desc;
1449
1450 for (i = 0; i < vector_active(v); i++)
1451 if ((cmd_element = vector_slot(v, i)) != NULL) {
1452 int match = 0;
1453
1454 descvec = vector_slot(cmd_element->strvec, index);
1455
1456 for (j = 0; j < vector_active(descvec); j++)
1457 if ((desc = vector_slot(descvec, j))) {
1458 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001459 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001460
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001461 if (CMD_OPTION(str))
1462 if ((str = cmd_deopt(str)) == NULL)
1463 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001464
1465 switch (type) {
1466 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001467 if (!(CMD_VARIABLE (str))
1468 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001469 match++;
1470 break;
1471 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001472 if (!(CMD_VARIABLE (str))
1473 && strncmp(command, str, strlen (command)) == 0)
1474 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001475 if (matched
1476 && strcmp(matched,
1477 str) != 0)
1478 return 1; /* There is ambiguous match. */
1479 else
1480 matched = str;
1481 match++;
1482 }
1483 break;
1484 case range_match:
1485 if (cmd_range_match
1486 (str, command)) {
1487 if (matched
1488 && strcmp(matched,
1489 str) != 0)
1490 return 1;
1491 else
1492 matched = str;
1493 match++;
1494 }
1495 break;
1496#ifdef HAVE_IPV6
1497 case ipv6_match:
1498 if (CMD_IPV6(str))
1499 match++;
1500 break;
1501 case ipv6_prefix_match:
1502 if ((ret =
1503 cmd_ipv6_prefix_match
1504 (command)) != no_match) {
1505 if (ret == partly_match)
1506 return 2; /* There is incomplete match. */
1507
1508 match++;
1509 }
1510 break;
1511#endif /* HAVE_IPV6 */
1512 case ipv4_match:
1513 if (CMD_IPV4(str))
1514 match++;
1515 break;
1516 case ipv4_prefix_match:
1517 if ((ret =
1518 cmd_ipv4_prefix_match
1519 (command)) != no_match) {
1520 if (ret == partly_match)
1521 return 2; /* There is incomplete match. */
1522
1523 match++;
1524 }
1525 break;
1526 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001527 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001528 match++;
1529 break;
1530 case no_match:
1531 default:
1532 break;
1533 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001534
1535 if (CMD_OPTION(desc->cmd))
1536 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001537 }
1538 if (!match)
1539 vector_slot(v, i) = NULL;
1540 }
1541 return 0;
1542}
1543
1544/* If src matches dst return dst string, otherwise return NULL */
1545static const char *cmd_entry_function(const char *src, const char *dst)
1546{
1547 /* Skip variable arguments. */
1548 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1549 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1550 return NULL;
1551
1552 /* In case of 'command \t', given src is NULL string. */
1553 if (src == NULL)
1554 return dst;
1555
1556 /* Matched with input string. */
1557 if (strncmp(src, dst, strlen(src)) == 0)
1558 return dst;
1559
1560 return NULL;
1561}
1562
1563/* If src matches dst return dst string, otherwise return NULL */
1564/* This version will return the dst string always if it is
1565 CMD_VARIABLE for '?' key processing */
1566static const char *cmd_entry_function_desc(const char *src, const char *dst)
1567{
1568 if (CMD_VARARG(dst))
1569 return dst;
1570
1571 if (CMD_RANGE(dst)) {
1572 if (cmd_range_match(dst, src))
1573 return dst;
1574 else
1575 return NULL;
1576 }
1577#ifdef HAVE_IPV6
1578 if (CMD_IPV6(dst)) {
1579 if (cmd_ipv6_match(src))
1580 return dst;
1581 else
1582 return NULL;
1583 }
1584
1585 if (CMD_IPV6_PREFIX(dst)) {
1586 if (cmd_ipv6_prefix_match(src))
1587 return dst;
1588 else
1589 return NULL;
1590 }
1591#endif /* HAVE_IPV6 */
1592
1593 if (CMD_IPV4(dst)) {
1594 if (cmd_ipv4_match(src))
1595 return dst;
1596 else
1597 return NULL;
1598 }
1599
1600 if (CMD_IPV4_PREFIX(dst)) {
1601 if (cmd_ipv4_prefix_match(src))
1602 return dst;
1603 else
1604 return NULL;
1605 }
1606
1607 /* Optional or variable commands always match on '?' */
1608 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1609 return dst;
1610
1611 /* In case of 'command \t', given src is NULL string. */
1612 if (src == NULL)
1613 return dst;
1614
1615 if (strncmp(src, dst, strlen(src)) == 0)
1616 return dst;
1617 else
1618 return NULL;
1619}
1620
1621/* Check same string element existence. If it isn't there return
1622 1. */
1623static int cmd_unique_string(vector v, const char *str)
1624{
1625 unsigned int i;
1626 char *match;
1627
1628 for (i = 0; i < vector_active(v); i++)
1629 if ((match = vector_slot(v, i)) != NULL)
1630 if (strcmp(match, str) == 0)
1631 return 0;
1632 return 1;
1633}
1634
1635/* Compare string to description vector. If there is same string
1636 return 1 else return 0. */
1637static int desc_unique_string(vector v, const char *str)
1638{
1639 unsigned int i;
1640 struct desc *desc;
1641
1642 for (i = 0; i < vector_active(v); i++)
1643 if ((desc = vector_slot(v, i)) != NULL)
1644 if (strcmp(desc->cmd, str) == 0)
1645 return 1;
1646 return 0;
1647}
1648
1649static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1650{
1651 if (first_word != NULL &&
1652 node != AUTH_NODE &&
1653 node != VIEW_NODE &&
1654 node != AUTH_ENABLE_NODE &&
1655 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1656 return 1;
1657 return 0;
1658}
1659
1660/* '?' describe command support. */
1661static vector
1662cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1663{
1664 unsigned int i;
1665 vector cmd_vector;
1666#define INIT_MATCHVEC_SIZE 10
1667 vector matchvec;
1668 struct cmd_element *cmd_element;
1669 unsigned int index;
1670 int ret;
1671 enum match_type match;
1672 char *command;
1673 static struct desc desc_cr = { "<cr>", "" };
1674
1675 /* Set index. */
1676 if (vector_active(vline) == 0) {
1677 *status = CMD_ERR_NO_MATCH;
1678 return NULL;
1679 } else
1680 index = vector_active(vline) - 1;
1681
1682 /* Make copy vector of current node's command vector. */
1683 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1684
1685 /* Prepare match vector */
1686 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1687
1688 /* Filter commands. */
1689 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001690 for (i = 0; i < index; i++) {
1691 command = vector_slot(vline, i);
1692 if (!command)
1693 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001694
Harald Welte80d30fe2013-02-12 11:08:57 +01001695 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001696
Harald Welte80d30fe2013-02-12 11:08:57 +01001697 if (match == vararg_match) {
1698 struct cmd_element *cmd_element;
1699 vector descvec;
1700 unsigned int j, k;
1701
1702 for (j = 0; j < vector_active(cmd_vector); j++)
1703 if ((cmd_element =
1704 vector_slot(cmd_vector, j)) != NULL
1705 &&
1706 (vector_active(cmd_element->strvec))) {
1707 descvec =
1708 vector_slot(cmd_element->
1709 strvec,
1710 vector_active
1711 (cmd_element->
1712 strvec) - 1);
1713 for (k = 0;
1714 k < vector_active(descvec);
1715 k++) {
1716 struct desc *desc =
1717 vector_slot(descvec,
1718 k);
1719 vector_set(matchvec,
1720 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001721 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001722 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001723
Harald Welte80d30fe2013-02-12 11:08:57 +01001724 vector_set(matchvec, &desc_cr);
1725 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001726
Harald Welte80d30fe2013-02-12 11:08:57 +01001727 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001728 }
1729
Harald Welte80d30fe2013-02-12 11:08:57 +01001730 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1731 match)) == 1) {
1732 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001733 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001734 *status = CMD_ERR_AMBIGUOUS;
1735 return NULL;
1736 } else if (ret == 2) {
1737 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001738 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001739 *status = CMD_ERR_NO_MATCH;
1740 return NULL;
1741 }
1742 }
1743
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001744 /* Prepare match vector */
1745 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1746
1747 /* Make sure that cmd_vector is filtered based on current word */
1748 command = vector_slot(vline, index);
1749 if (command)
Vadim Yanitskiy49a0dec2017-06-12 03:49:38 +07001750 cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001751
1752 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001753 for (i = 0; i < vector_active(cmd_vector); i++) {
1754 const char *string = NULL;
1755 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001756
Harald Welte80d30fe2013-02-12 11:08:57 +01001757 cmd_element = vector_slot(cmd_vector, i);
1758 if (!cmd_element)
1759 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001760
Harald Welted17aa592013-02-12 11:11:34 +01001761 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1762 continue;
1763
Harald Welte80d30fe2013-02-12 11:08:57 +01001764 strvec = cmd_element->strvec;
1765
1766 /* if command is NULL, index may be equal to vector_active */
1767 if (command && index >= vector_active(strvec))
1768 vector_slot(cmd_vector, i) = NULL;
1769 else {
1770 /* Check if command is completed. */
1771 if (command == NULL
1772 && index == vector_active(strvec)) {
1773 string = "<cr>";
1774 if (!desc_unique_string(matchvec, string))
1775 vector_set(matchvec, &desc_cr);
1776 } else {
1777 unsigned int j;
1778 vector descvec = vector_slot(strvec, index);
1779 struct desc *desc;
1780
1781 for (j = 0; j < vector_active(descvec); j++) {
1782 desc = vector_slot(descvec, j);
1783 if (!desc)
1784 continue;
1785 string = cmd_entry_function_desc
1786 (command, desc->cmd);
1787 if (!string)
1788 continue;
1789 /* Uniqueness check */
1790 if (!desc_unique_string(matchvec, string))
1791 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001792 }
1793 }
1794 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001795 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001796 vector_free(cmd_vector);
1797
1798 if (vector_slot(matchvec, 0) == NULL) {
1799 vector_free(matchvec);
1800 *status = CMD_ERR_NO_MATCH;
1801 } else
1802 *status = CMD_SUCCESS;
1803
1804 return matchvec;
1805}
1806
1807vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1808{
1809 vector ret;
1810
1811 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1812 enum node_type onode;
1813 vector shifted_vline;
1814 unsigned int index;
1815
1816 onode = vty->node;
1817 vty->node = ENABLE_NODE;
1818 /* We can try it on enable node, cos' the vty is authenticated */
1819
1820 shifted_vline = vector_init(vector_count(vline));
1821 /* use memcpy? */
1822 for (index = 1; index < vector_active(vline); index++) {
1823 vector_set_index(shifted_vline, index - 1,
1824 vector_lookup(vline, index));
1825 }
1826
1827 ret = cmd_describe_command_real(shifted_vline, vty, status);
1828
1829 vector_free(shifted_vline);
1830 vty->node = onode;
1831 return ret;
1832 }
1833
1834 return cmd_describe_command_real(vline, vty, status);
1835}
1836
1837/* Check LCD of matched command. */
1838static int cmd_lcd(char **matched)
1839{
1840 int i;
1841 int j;
1842 int lcd = -1;
1843 char *s1, *s2;
1844 char c1, c2;
1845
1846 if (matched[0] == NULL || matched[1] == NULL)
1847 return 0;
1848
1849 for (i = 1; matched[i] != NULL; i++) {
1850 s1 = matched[i - 1];
1851 s2 = matched[i];
1852
1853 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1854 if (c1 != c2)
1855 break;
1856
1857 if (lcd < 0)
1858 lcd = j;
1859 else {
1860 if (lcd > j)
1861 lcd = j;
1862 }
1863 }
1864 return lcd;
1865}
1866
1867/* Command line completion support. */
1868static char **cmd_complete_command_real(vector vline, struct vty *vty,
1869 int *status)
1870{
1871 unsigned int i;
1872 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1873#define INIT_MATCHVEC_SIZE 10
1874 vector matchvec;
1875 struct cmd_element *cmd_element;
1876 unsigned int index;
1877 char **match_str;
1878 struct desc *desc;
1879 vector descvec;
1880 char *command;
1881 int lcd;
1882
1883 if (vector_active(vline) == 0) {
1884 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001885 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001886 return NULL;
1887 } else
1888 index = vector_active(vline) - 1;
1889
1890 /* First, filter by preceeding command string */
1891 for (i = 0; i < index; i++)
1892 if ((command = vector_slot(vline, i))) {
1893 enum match_type match;
1894 int ret;
1895
1896 /* First try completion match, if there is exactly match return 1 */
1897 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001898 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001899
1900 /* If there is exact match then filter ambiguous match else check
1901 ambiguousness. */
1902 if ((ret =
1903 is_cmd_ambiguous(command, cmd_vector, i,
1904 match)) == 1) {
1905 vector_free(cmd_vector);
1906 *status = CMD_ERR_AMBIGUOUS;
1907 return NULL;
1908 }
1909 /*
1910 else if (ret == 2)
1911 {
1912 vector_free (cmd_vector);
1913 *status = CMD_ERR_NO_MATCH;
1914 return NULL;
1915 }
1916 */
1917 }
1918
1919 /* Prepare match vector. */
1920 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1921
1922 /* Now we got into completion */
1923 for (i = 0; i < vector_active(cmd_vector); i++)
1924 if ((cmd_element = vector_slot(cmd_vector, i))) {
1925 const char *string;
1926 vector strvec = cmd_element->strvec;
1927
1928 /* Check field length */
1929 if (index >= vector_active(strvec))
1930 vector_slot(cmd_vector, i) = NULL;
1931 else {
1932 unsigned int j;
1933
1934 descvec = vector_slot(strvec, index);
1935 for (j = 0; j < vector_active(descvec); j++)
1936 if ((desc = vector_slot(descvec, j))) {
1937 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1938 if (cmd_unique_string (matchvec, string))
1939 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1940 }
1941 }
1942 }
1943
1944 /* We don't need cmd_vector any more. */
1945 vector_free(cmd_vector);
1946
1947 /* No matched command */
1948 if (vector_slot(matchvec, 0) == NULL) {
1949 vector_free(matchvec);
1950
1951 /* In case of 'command \t' pattern. Do you need '?' command at
1952 the end of the line. */
1953 if (vector_slot(vline, index) == '\0')
1954 *status = CMD_ERR_NOTHING_TODO;
1955 else
1956 *status = CMD_ERR_NO_MATCH;
1957 return NULL;
1958 }
1959
1960 /* Only one matched */
1961 if (vector_slot(matchvec, 1) == NULL) {
1962 match_str = (char **)matchvec->index;
1963 vector_only_wrapper_free(matchvec);
1964 *status = CMD_COMPLETE_FULL_MATCH;
1965 return match_str;
1966 }
1967 /* Make it sure last element is NULL. */
1968 vector_set(matchvec, NULL);
1969
1970 /* Check LCD of matched strings. */
1971 if (vector_slot(vline, index) != NULL) {
1972 lcd = cmd_lcd((char **)matchvec->index);
1973
1974 if (lcd) {
1975 int len = strlen(vector_slot(vline, index));
1976
1977 if (len < lcd) {
1978 char *lcdstr;
1979
1980 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1981 "complete-lcdstr");
1982 memcpy(lcdstr, matchvec->index[0], lcd);
1983 lcdstr[lcd] = '\0';
1984
1985 /* match_str = (char **) &lcdstr; */
1986
1987 /* Free matchvec. */
1988 for (i = 0; i < vector_active(matchvec); i++) {
1989 if (vector_slot(matchvec, i))
1990 talloc_free(vector_slot(matchvec, i));
1991 }
1992 vector_free(matchvec);
1993
1994 /* Make new matchvec. */
1995 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1996 vector_set(matchvec, lcdstr);
1997 match_str = (char **)matchvec->index;
1998 vector_only_wrapper_free(matchvec);
1999
2000 *status = CMD_COMPLETE_MATCH;
2001 return match_str;
2002 }
2003 }
2004 }
2005
2006 match_str = (char **)matchvec->index;
2007 vector_only_wrapper_free(matchvec);
2008 *status = CMD_COMPLETE_LIST_MATCH;
2009 return match_str;
2010}
2011
2012char **cmd_complete_command(vector vline, struct vty *vty, int *status)
2013{
2014 char **ret;
2015
2016 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2017 enum node_type onode;
2018 vector shifted_vline;
2019 unsigned int index;
2020
2021 onode = vty->node;
2022 vty->node = ENABLE_NODE;
2023 /* We can try it on enable node, cos' the vty is authenticated */
2024
2025 shifted_vline = vector_init(vector_count(vline));
2026 /* use memcpy? */
2027 for (index = 1; index < vector_active(vline); index++) {
2028 vector_set_index(shifted_vline, index - 1,
2029 vector_lookup(vline, index));
2030 }
2031
2032 ret = cmd_complete_command_real(shifted_vline, vty, status);
2033
2034 vector_free(shifted_vline);
2035 vty->node = onode;
2036 return ret;
2037 }
2038
2039 return cmd_complete_command_real(vline, vty, status);
2040}
2041
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002042static struct vty_parent_node *vty_parent(struct vty *vty)
2043{
2044 return llist_first_entry_or_null(&vty->parent_nodes,
2045 struct vty_parent_node,
2046 entry);
2047}
2048
2049static bool vty_pop_parent(struct vty *vty)
2050{
2051 struct vty_parent_node *parent = vty_parent(vty);
2052 if (!parent)
2053 return false;
2054 llist_del(&parent->entry);
2055 vty->node = parent->node;
2056 vty->priv = parent->priv;
2057 if (vty->indent)
2058 talloc_free(vty->indent);
2059 vty->indent = parent->indent;
2060 talloc_free(parent);
2061 return true;
2062}
2063
2064static void vty_clear_parents(struct vty *vty)
2065{
2066 while (vty_pop_parent(vty));
2067}
2068
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002069/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002070/*
2071 * This function MUST eventually converge on a node when called repeatedly,
2072 * there must not be any cycles.
2073 * All 'config' nodes shall converge on CONFIG_NODE.
2074 * All other 'enable' nodes shall converge on ENABLE_NODE.
2075 * All 'view' only nodes shall converge on VIEW_NODE.
2076 * All other nodes shall converge on themselves or it must be ensured,
2077 * that the user's rights are not extended anyhow by calling this function.
2078 *
2079 * Note that these requirements also apply to all functions that are used
2080 * as go_parent_cb.
2081 * Note also that this function relies on the is_config_child callback to
2082 * recognize non-config nodes if go_parent_cb is not set.
2083 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00002084int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002085{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002086 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002087 case AUTH_NODE:
2088 case VIEW_NODE:
2089 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002090 case CONFIG_NODE:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002091 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002092 break;
2093
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002094 case AUTH_ENABLE_NODE:
2095 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002096 vty_clear_parents(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002097 break;
2098
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002099 case CFG_LOG_NODE:
2100 case VTY_NODE:
2101 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002102 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002103 break;
2104
2105 default:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002106 if (host.app_info->go_parent_cb) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002107 host.app_info->go_parent_cb(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002108 vty_pop_parent(vty);
2109 }
2110 else if (is_config_child(vty)) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002111 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002112 vty_clear_parents(vty);
2113 }
2114 else {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002115 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002116 vty_clear_parents(vty);
2117 }
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002118 break;
2119 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002120
2121 return vty->node;
2122}
2123
2124/* Execute command by argument vline vector. */
2125static int
2126cmd_execute_command_real(vector vline, struct vty *vty,
2127 struct cmd_element **cmd)
2128{
2129 unsigned int i;
2130 unsigned int index;
2131 vector cmd_vector;
2132 struct cmd_element *cmd_element;
2133 struct cmd_element *matched_element;
2134 unsigned int matched_count, incomplete_count;
2135 int argc;
2136 const char *argv[CMD_ARGC_MAX];
2137 enum match_type match = 0;
2138 int varflag;
2139 char *command;
2140
2141 /* Make copy of command elements. */
2142 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2143
2144 for (index = 0; index < vector_active(vline); index++)
2145 if ((command = vector_slot(vline, index))) {
2146 int ret;
2147
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002148 match = cmd_filter(command, cmd_vector, index,
2149 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002150
2151 if (match == vararg_match)
2152 break;
2153
2154 ret =
2155 is_cmd_ambiguous(command, cmd_vector, index, match);
2156
2157 if (ret == 1) {
2158 vector_free(cmd_vector);
2159 return CMD_ERR_AMBIGUOUS;
2160 } else if (ret == 2) {
2161 vector_free(cmd_vector);
2162 return CMD_ERR_NO_MATCH;
2163 }
2164 }
2165
2166 /* Check matched count. */
2167 matched_element = NULL;
2168 matched_count = 0;
2169 incomplete_count = 0;
2170
2171 for (i = 0; i < vector_active(cmd_vector); i++)
2172 if ((cmd_element = vector_slot(cmd_vector, i))) {
2173 if (match == vararg_match
2174 || index >= cmd_element->cmdsize) {
2175 matched_element = cmd_element;
2176#if 0
2177 printf("DEBUG: %s\n", cmd_element->string);
2178#endif
2179 matched_count++;
2180 } else {
2181 incomplete_count++;
2182 }
2183 }
2184
2185 /* Finish of using cmd_vector. */
2186 vector_free(cmd_vector);
2187
2188 /* To execute command, matched_count must be 1. */
2189 if (matched_count == 0) {
2190 if (incomplete_count)
2191 return CMD_ERR_INCOMPLETE;
2192 else
2193 return CMD_ERR_NO_MATCH;
2194 }
2195
2196 if (matched_count > 1)
2197 return CMD_ERR_AMBIGUOUS;
2198
2199 /* Argument treatment */
2200 varflag = 0;
2201 argc = 0;
2202
2203 for (i = 0; i < vector_active(vline); i++) {
2204 if (varflag)
2205 argv[argc++] = vector_slot(vline, i);
2206 else {
2207 vector descvec =
2208 vector_slot(matched_element->strvec, i);
2209
2210 if (vector_active(descvec) == 1) {
2211 struct desc *desc = vector_slot(descvec, 0);
2212
2213 if (CMD_VARARG(desc->cmd))
2214 varflag = 1;
2215
2216 if (varflag || CMD_VARIABLE(desc->cmd)
2217 || CMD_OPTION(desc->cmd))
2218 argv[argc++] = vector_slot(vline, i);
2219 } else
2220 argv[argc++] = vector_slot(vline, i);
2221 }
2222
2223 if (argc >= CMD_ARGC_MAX)
2224 return CMD_ERR_EXEED_ARGC_MAX;
2225 }
2226
2227 /* For vtysh execution. */
2228 if (cmd)
2229 *cmd = matched_element;
2230
2231 if (matched_element->daemon)
2232 return CMD_SUCCESS_DAEMON;
2233
2234 /* Execute matched command. */
2235 return (*matched_element->func) (matched_element, vty, argc, argv);
2236}
2237
2238int
2239cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2240 int vtysh)
2241{
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002242 int ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002243 enum node_type onode;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002244
2245 onode = vty->node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002246
2247 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2248 vector shifted_vline;
2249 unsigned int index;
2250
2251 vty->node = ENABLE_NODE;
2252 /* We can try it on enable node, cos' the vty is authenticated */
2253
2254 shifted_vline = vector_init(vector_count(vline));
2255 /* use memcpy? */
2256 for (index = 1; index < vector_active(vline); index++) {
2257 vector_set_index(shifted_vline, index - 1,
2258 vector_lookup(vline, index));
2259 }
2260
2261 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2262
2263 vector_free(shifted_vline);
2264 vty->node = onode;
2265 return ret;
2266 }
2267
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002268 return cmd_execute_command_real(vline, vty, cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002269}
2270
2271/* Execute command by argument readline. */
2272int
2273cmd_execute_command_strict(vector vline, struct vty *vty,
2274 struct cmd_element **cmd)
2275{
2276 unsigned int i;
2277 unsigned int index;
2278 vector cmd_vector;
2279 struct cmd_element *cmd_element;
2280 struct cmd_element *matched_element;
2281 unsigned int matched_count, incomplete_count;
2282 int argc;
2283 const char *argv[CMD_ARGC_MAX];
2284 int varflag;
2285 enum match_type match = 0;
2286 char *command;
2287
2288 /* Make copy of command element */
2289 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2290
2291 for (index = 0; index < vector_active(vline); index++)
2292 if ((command = vector_slot(vline, index))) {
2293 int ret;
2294
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002295 match = cmd_filter(vector_slot(vline, index),
2296 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002297
2298 /* If command meets '.VARARG' then finish matching. */
2299 if (match == vararg_match)
2300 break;
2301
2302 ret =
2303 is_cmd_ambiguous(command, cmd_vector, index, match);
2304 if (ret == 1) {
2305 vector_free(cmd_vector);
2306 return CMD_ERR_AMBIGUOUS;
2307 }
2308 if (ret == 2) {
2309 vector_free(cmd_vector);
2310 return CMD_ERR_NO_MATCH;
2311 }
2312 }
2313
2314 /* Check matched count. */
2315 matched_element = NULL;
2316 matched_count = 0;
2317 incomplete_count = 0;
2318 for (i = 0; i < vector_active(cmd_vector); i++)
2319 if (vector_slot(cmd_vector, i) != NULL) {
2320 cmd_element = vector_slot(cmd_vector, i);
2321
2322 if (match == vararg_match
2323 || index >= cmd_element->cmdsize) {
2324 matched_element = cmd_element;
2325 matched_count++;
2326 } else
2327 incomplete_count++;
2328 }
2329
2330 /* Finish of using cmd_vector. */
2331 vector_free(cmd_vector);
2332
2333 /* To execute command, matched_count must be 1. */
2334 if (matched_count == 0) {
2335 if (incomplete_count)
2336 return CMD_ERR_INCOMPLETE;
2337 else
2338 return CMD_ERR_NO_MATCH;
2339 }
2340
2341 if (matched_count > 1)
2342 return CMD_ERR_AMBIGUOUS;
2343
2344 /* Argument treatment */
2345 varflag = 0;
2346 argc = 0;
2347
2348 for (i = 0; i < vector_active(vline); i++) {
2349 if (varflag)
2350 argv[argc++] = vector_slot(vline, i);
2351 else {
2352 vector descvec =
2353 vector_slot(matched_element->strvec, i);
2354
2355 if (vector_active(descvec) == 1) {
2356 struct desc *desc = vector_slot(descvec, 0);
2357
2358 if (CMD_VARARG(desc->cmd))
2359 varflag = 1;
2360
2361 if (varflag || CMD_VARIABLE(desc->cmd)
2362 || CMD_OPTION(desc->cmd))
2363 argv[argc++] = vector_slot(vline, i);
2364 } else
2365 argv[argc++] = vector_slot(vline, i);
2366 }
2367
2368 if (argc >= CMD_ARGC_MAX)
2369 return CMD_ERR_EXEED_ARGC_MAX;
2370 }
2371
2372 /* For vtysh execution. */
2373 if (cmd)
2374 *cmd = matched_element;
2375
2376 if (matched_element->daemon)
2377 return CMD_SUCCESS_DAEMON;
2378
2379 /* Now execute matched command */
2380 return (*matched_element->func) (matched_element, vty, argc, argv);
2381}
2382
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002383static inline size_t len(const char *str)
2384{
2385 return str? strlen(str) : 0;
2386}
2387
Neels Hofmeyr00b5ed32017-09-20 00:46:03 +02002388/*! Make sure the common length of strings a and b is identical, then compare their lengths. I.e., if a
2389 * is longer than b, a must start with exactly b, and vice versa.
2390 * \returns EINVAL on mismatch, -1 for a < b, 0 for a == b, 1 for a > b.
2391 */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002392static int indent_cmp(const char *a, const char *b)
2393{
2394 size_t al, bl;
2395 al = len(a);
2396 bl = len(b);
2397 if (al > bl) {
2398 if (bl && strncmp(a, b, bl) != 0)
2399 return EINVAL;
2400 return 1;
2401 }
2402 /* al <= bl */
2403 if (al && strncmp(a, b, al) != 0)
2404 return EINVAL;
2405 return (al < bl)? -1 : 0;
2406}
2407
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002408/* Configration make from file. */
2409int config_from_file(struct vty *vty, FILE * fp)
2410{
2411 int ret;
2412 vector vline;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002413 char *indent;
2414 int cmp;
2415 struct vty_parent_node this_node;
2416 struct vty_parent_node *parent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002417
2418 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002419 indent = NULL;
2420 vline = NULL;
2421 ret = cmd_make_strvec2(vty->buf, &indent, &vline);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002422
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002423 if (ret != CMD_SUCCESS)
2424 goto return_invalid_indent;
2425
2426 /* In case of comment or empty line */
2427 if (vline == NULL) {
2428 if (indent) {
2429 talloc_free(indent);
2430 indent = NULL;
2431 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002432 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002433 }
2434
Neels Hofmeyr43063632017-09-19 23:54:01 +02002435 /* We have a nonempty line. */
2436 if (!vty->indent) {
2437 /* We have just entered a node and expecting the first child to come up; but we
2438 * may also skip right back to a parent or ancestor level. */
2439 parent = vty_parent(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002440
Neels Hofmeyr43063632017-09-19 23:54:01 +02002441 /* If there is no parent, record any indentation we encounter. */
2442 cmp = parent ? indent_cmp(indent, parent->indent) : 1;
2443
2444 if (cmp == EINVAL)
2445 goto return_invalid_indent;
2446
2447 if (cmp <= 0) {
2448 /* We have gone right back to the parent level or higher, we are skipping
2449 * this child node level entirely. Pop the parent to go back to a node
2450 * that was actually there (to reinstate vty->indent) and re-use below
2451 * go-parent while-loop to find an accurate match of indent in the node
2452 * ancestry. */
2453 vty_go_parent(vty);
2454 } else {
2455 /* The indent is deeper than the just entered parent, record the new
2456 * indentation characters. */
2457 vty->indent = talloc_strdup(vty, indent);
2458 /* This *is* the new indentation. */
2459 cmp = 0;
2460 }
2461 } else {
2462 /* There is a known indentation for this node level, validate and detect node
2463 * exits. */
2464 cmp = indent_cmp(indent, vty->indent);
2465 if (cmp == EINVAL)
2466 goto return_invalid_indent;
2467 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002468
2469 /* Less indent: go up the parent nodes to find matching amount of less indent. When this
2470 * loop exits, we want to have found an exact match, i.e. cmp == 0. */
2471 while (cmp < 0) {
2472 vty_go_parent(vty);
2473 cmp = indent_cmp(indent, vty->indent);
2474 if (cmp == EINVAL)
2475 goto return_invalid_indent;
2476 }
2477
2478 /* More indent without having entered a child node level? Either the parent node's indent
2479 * wasn't hit exactly (e.g. there's a space more than the parent level had further above)
2480 * or the indentation increased even though the vty command didn't enter a child. */
2481 if (cmp > 0)
2482 goto return_invalid_indent;
2483
2484 /* Remember the current node before the command possibly changes it. */
2485 this_node = (struct vty_parent_node){
2486 .node = vty->node,
2487 .priv = vty->priv,
2488 .indent = vty->indent,
2489 };
2490
2491 parent = vty_parent(vty);
2492 ret = cmd_execute_command_strict(vline, vty, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002493 cmd_free_strvec(vline);
2494
2495 if (ret != CMD_SUCCESS && ret != CMD_WARNING
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002496 && ret != CMD_ERR_NOTHING_TODO) {
2497 if (indent) {
2498 talloc_free(indent);
2499 indent = NULL;
2500 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002501 return ret;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002502 }
2503
2504 /* If we have stepped down into a child node, push a parent frame.
2505 * The causality is such: we don't expect every single node entry implementation to push
2506 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2507 * a parent node. Hence if the node changed without the parent node changing, we must
2508 * have stepped into a child node (and now expect a deeper indent). */
2509 if (vty->node != this_node.node && parent == vty_parent(vty)) {
2510 /* Push the parent node. */
2511 parent = talloc_zero(vty, struct vty_parent_node);
2512 *parent = this_node;
2513 llist_add(&parent->entry, &vty->parent_nodes);
2514
2515 /* The current talloc'ed vty->indent string will now be owned by this parent
2516 * struct. Indicate that we don't know what deeper indent characters the user
2517 * will choose. */
2518 vty->indent = NULL;
2519 }
2520
2521 if (indent) {
2522 talloc_free(indent);
2523 indent = NULL;
2524 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002525 }
2526 return CMD_SUCCESS;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002527
2528return_invalid_indent:
2529 if (vline)
2530 cmd_free_strvec(vline);
2531 if (indent) {
2532 talloc_free(indent);
2533 indent = NULL;
2534 }
2535 return CMD_ERR_INVALID_INDENT;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002536}
2537
2538/* Configration from terminal */
2539DEFUN(config_terminal,
2540 config_terminal_cmd,
2541 "configure terminal",
2542 "Configuration from vty interface\n" "Configuration terminal\n")
2543{
2544 if (vty_config_lock(vty))
2545 vty->node = CONFIG_NODE;
2546 else {
2547 vty_out(vty, "VTY configuration is locked by other VTY%s",
2548 VTY_NEWLINE);
2549 return CMD_WARNING;
2550 }
2551 return CMD_SUCCESS;
2552}
2553
2554/* Enable command */
2555DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2556{
2557 /* If enable password is NULL, change to ENABLE_NODE */
2558 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2559 vty->type == VTY_SHELL_SERV)
2560 vty->node = ENABLE_NODE;
2561 else
2562 vty->node = AUTH_ENABLE_NODE;
2563
2564 return CMD_SUCCESS;
2565}
2566
2567/* Disable command */
2568DEFUN(disable,
2569 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2570{
2571 if (vty->node == ENABLE_NODE)
2572 vty->node = VIEW_NODE;
2573 return CMD_SUCCESS;
2574}
2575
2576/* Down vty node level. */
2577gDEFUN(config_exit,
2578 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2579{
2580 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002581 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002582 case VIEW_NODE:
2583 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002584 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002585 break;
2586 case CONFIG_NODE:
2587 vty->node = ENABLE_NODE;
2588 vty_config_unlock(vty);
2589 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002590 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002591 if (vty->node > CONFIG_NODE)
2592 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002593 break;
2594 }
2595 return CMD_SUCCESS;
2596}
2597
2598/* End of configuration. */
2599 gDEFUN(config_end,
2600 config_end_cmd, "end", "End current mode and change to enable mode.")
2601{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002602 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002603 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002604
2605 /* Repeatedly call go_parent until a top node is reached. */
2606 while (vty->node > CONFIG_NODE) {
2607 if (vty->node == last_node) {
2608 /* Ensure termination, this shouldn't happen. */
2609 break;
2610 }
2611 last_node = vty->node;
2612 vty_go_parent(vty);
2613 }
2614
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002615 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002616 if (vty->node > ENABLE_NODE)
2617 vty->node = ENABLE_NODE;
2618 vty->index = NULL;
2619 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002620 }
2621 return CMD_SUCCESS;
2622}
2623
2624/* Show version. */
2625DEFUN(show_version,
2626 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2627{
Harald Welte237f6242010-05-25 23:00:45 +02002628 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2629 host.app_info->version,
2630 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2631 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002632
2633 return CMD_SUCCESS;
2634}
2635
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002636DEFUN(show_online_help,
2637 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2638{
2639 vty_dump_nodes(vty);
2640 return CMD_SUCCESS;
2641}
2642
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002643/* Help display function for all node. */
2644gDEFUN(config_help,
2645 config_help_cmd, "help", "Description of the interactive help system\n")
2646{
2647 vty_out(vty,
2648 "This VTY provides advanced help features. When you need help,%s\
2649anytime at the command line please press '?'.%s\
2650%s\
2651If nothing matches, the help list will be empty and you must backup%s\
2652 until entering a '?' shows the available options.%s\
2653Two styles of help are provided:%s\
26541. Full help is available when you are ready to enter a%s\
2655command argument (e.g. 'show ?') and describes each possible%s\
2656argument.%s\
26572. Partial help is provided when an abbreviated argument is entered%s\
2658 and you want to know what arguments match the input%s\
2659 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2660 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2661 return CMD_SUCCESS;
2662}
2663
2664/* Help display function for all node. */
2665gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2666{
2667 unsigned int i;
2668 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2669 struct cmd_element *cmd;
2670
2671 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2672 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2673 && !(cmd->attr == CMD_ATTR_DEPRECATED
2674 || cmd->attr == CMD_ATTR_HIDDEN))
2675 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2676 return CMD_SUCCESS;
2677}
2678
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002679static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002680{
2681 unsigned int i;
2682 int fd;
2683 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002684 char *config_file_tmp = NULL;
2685 char *config_file_sav = NULL;
2686 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002687 struct stat st;
2688
2689 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002690
2691 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002692 config_file_sav =
2693 _talloc_zero(tall_vty_cmd_ctx,
2694 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2695 "config_file_sav");
2696 strcpy(config_file_sav, config_file);
2697 strcat(config_file_sav, CONF_BACKUP_EXT);
2698
2699 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2700 "config_file_tmp");
2701 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2702
2703 /* Open file to configuration write. */
2704 fd = mkstemp(config_file_tmp);
2705 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002706 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002707 talloc_free(config_file_tmp);
2708 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002709 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002710 }
2711
2712 /* Make vty for configuration file. */
2713 file_vty = vty_new();
2714 file_vty->fd = fd;
2715 file_vty->type = VTY_FILE;
2716
2717 /* Config file header print. */
2718 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002719 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002720 //vty_time_print (file_vty, 1);
2721 vty_out(file_vty, "!\n");
2722
2723 for (i = 0; i < vector_active(cmdvec); i++)
2724 if ((node = vector_slot(cmdvec, i)) && node->func) {
2725 if ((*node->func) (file_vty))
2726 vty_out(file_vty, "!\n");
2727 }
2728 vty_close(file_vty);
2729
2730 if (unlink(config_file_sav) != 0)
2731 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002732 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002733 talloc_free(config_file_sav);
2734 talloc_free(config_file_tmp);
2735 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002736 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002737 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002738
2739 /* Only link the .sav file if the original file exists */
2740 if (stat(config_file, &st) == 0) {
2741 if (link(config_file, config_file_sav) != 0) {
2742 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2743 talloc_free(config_file_sav);
2744 talloc_free(config_file_tmp);
2745 unlink(config_file_tmp);
2746 return -3;
2747 }
2748 sync();
2749 if (unlink(config_file) != 0) {
2750 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2751 talloc_free(config_file_sav);
2752 talloc_free(config_file_tmp);
2753 unlink(config_file_tmp);
2754 return -4;
2755 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002756 }
2757 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002758 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002759 talloc_free(config_file_sav);
2760 talloc_free(config_file_tmp);
2761 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002762 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002763 }
2764 unlink(config_file_tmp);
2765 sync();
2766
2767 talloc_free(config_file_sav);
2768 talloc_free(config_file_tmp);
2769
2770 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002771 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2772 return -6;
2773 }
2774
2775 return 0;
2776}
2777
2778
2779/* Write current configuration into file. */
2780DEFUN(config_write_file,
2781 config_write_file_cmd,
2782 "write file",
2783 "Write running configuration to memory, network, or terminal\n"
2784 "Write to configuration file\n")
2785{
2786 char *failed_file;
2787 int rc;
2788
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002789 if (host.app_info->config_is_consistent) {
2790 rc = host.app_info->config_is_consistent(vty);
2791 if (!rc) {
2792 vty_out(vty, "Configuration is not consistent%s",
2793 VTY_NEWLINE);
2794 return CMD_WARNING;
2795 }
2796 }
2797
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002798 if (host.config == NULL) {
2799 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2800 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002801 return CMD_WARNING;
2802 }
2803
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002804 rc = write_config_file(host.config, &failed_file);
2805 switch (rc) {
2806 case -1:
2807 vty_out(vty, "Can't open configuration file %s.%s",
2808 failed_file, VTY_NEWLINE);
2809 rc = CMD_WARNING;
2810 break;
2811 case -2:
2812 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2813 failed_file, VTY_NEWLINE);
2814 rc = CMD_WARNING;
2815 break;
2816 case -3:
2817 vty_out(vty, "Can't backup old configuration file %s.%s",
2818 failed_file, VTY_NEWLINE);
2819 rc = CMD_WARNING;
2820 break;
2821 case -4:
2822 vty_out(vty, "Can't unlink configuration file %s.%s",
2823 failed_file, VTY_NEWLINE);
2824 rc = CMD_WARNING;
2825 break;
2826 case -5:
2827 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2828 VTY_NEWLINE);
2829 rc = CMD_WARNING;
2830 break;
2831 case -6:
2832 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2833 failed_file, strerror(errno), errno, VTY_NEWLINE);
2834 rc = CMD_WARNING;
2835 break;
2836 default:
2837 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2838 rc = CMD_SUCCESS;
2839 break;
2840 }
2841
2842 talloc_free(failed_file);
2843 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002844}
2845
2846ALIAS(config_write_file,
2847 config_write_cmd,
2848 "write", "Write running configuration to memory, network, or terminal\n")
2849
2850 ALIAS(config_write_file,
2851 config_write_memory_cmd,
2852 "write memory",
2853 "Write running configuration to memory, network, or terminal\n"
2854 "Write configuration to the file (same as write file)\n")
2855
2856 ALIAS(config_write_file,
2857 copy_runningconfig_startupconfig_cmd,
2858 "copy running-config startup-config",
2859 "Copy configuration\n"
2860 "Copy running config to... \n"
2861 "Copy running config to startup config (same as write file)\n")
2862
2863/* Write current configuration into the terminal. */
2864 DEFUN(config_write_terminal,
2865 config_write_terminal_cmd,
2866 "write terminal",
2867 "Write running configuration to memory, network, or terminal\n"
2868 "Write to terminal\n")
2869{
2870 unsigned int i;
2871 struct cmd_node *node;
2872
2873 if (vty->type == VTY_SHELL_SERV) {
2874 for (i = 0; i < vector_active(cmdvec); i++)
2875 if ((node = vector_slot(cmdvec, i)) && node->func
2876 && node->vtysh) {
2877 if ((*node->func) (vty))
2878 vty_out(vty, "!%s", VTY_NEWLINE);
2879 }
2880 } else {
2881 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2882 VTY_NEWLINE);
2883 vty_out(vty, "!%s", VTY_NEWLINE);
2884
2885 for (i = 0; i < vector_active(cmdvec); i++)
2886 if ((node = vector_slot(cmdvec, i)) && node->func) {
2887 if ((*node->func) (vty))
2888 vty_out(vty, "!%s", VTY_NEWLINE);
2889 }
2890 vty_out(vty, "end%s", VTY_NEWLINE);
2891 }
2892 return CMD_SUCCESS;
2893}
2894
2895/* Write current configuration into the terminal. */
2896ALIAS(config_write_terminal,
2897 show_running_config_cmd,
2898 "show running-config", SHOW_STR "running configuration\n")
2899
2900/* Write startup configuration into the terminal. */
2901 DEFUN(show_startup_config,
2902 show_startup_config_cmd,
2903 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2904{
2905 char buf[BUFSIZ];
2906 FILE *confp;
2907
2908 confp = fopen(host.config, "r");
2909 if (confp == NULL) {
2910 vty_out(vty, "Can't open configuration file [%s]%s",
2911 host.config, VTY_NEWLINE);
2912 return CMD_WARNING;
2913 }
2914
2915 while (fgets(buf, BUFSIZ, confp)) {
2916 char *cp = buf;
2917
2918 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2919 cp++;
2920 *cp = '\0';
2921
2922 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2923 }
2924
2925 fclose(confp);
2926
2927 return CMD_SUCCESS;
2928}
2929
2930/* Hostname configuration */
2931DEFUN(config_hostname,
2932 hostname_cmd,
2933 "hostname WORD",
2934 "Set system's network name\n" "This system's network name\n")
2935{
2936 if (!isalpha((int)*argv[0])) {
2937 vty_out(vty, "Please specify string starting with alphabet%s",
2938 VTY_NEWLINE);
2939 return CMD_WARNING;
2940 }
2941
2942 if (host.name)
2943 talloc_free(host.name);
2944
2945 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2946 return CMD_SUCCESS;
2947}
2948
2949DEFUN(config_no_hostname,
2950 no_hostname_cmd,
2951 "no hostname [HOSTNAME]",
2952 NO_STR "Reset system's network name\n" "Host name of this router\n")
2953{
2954 if (host.name)
2955 talloc_free(host.name);
2956 host.name = NULL;
2957 return CMD_SUCCESS;
2958}
2959
2960/* VTY interface password set. */
2961DEFUN(config_password, password_cmd,
2962 "password (8|) WORD",
2963 "Assign the terminal connection password\n"
2964 "Specifies a HIDDEN password will follow\n"
2965 "dummy string \n" "The HIDDEN line password string\n")
2966{
2967 /* Argument check. */
2968 if (argc == 0) {
2969 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2970 return CMD_WARNING;
2971 }
2972
2973 if (argc == 2) {
2974 if (*argv[0] == '8') {
2975 if (host.password)
2976 talloc_free(host.password);
2977 host.password = NULL;
2978 if (host.password_encrypt)
2979 talloc_free(host.password_encrypt);
2980 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2981 return CMD_SUCCESS;
2982 } else {
2983 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2984 return CMD_WARNING;
2985 }
2986 }
2987
2988 if (!isalnum((int)*argv[0])) {
2989 vty_out(vty,
2990 "Please specify string starting with alphanumeric%s",
2991 VTY_NEWLINE);
2992 return CMD_WARNING;
2993 }
2994
2995 if (host.password)
2996 talloc_free(host.password);
2997 host.password = NULL;
2998
2999#ifdef VTY_CRYPT_PW
3000 if (host.encrypt) {
3001 if (host.password_encrypt)
3002 talloc_free(host.password_encrypt);
3003 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3004 } else
3005#endif
3006 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3007
3008 return CMD_SUCCESS;
3009}
3010
3011ALIAS(config_password, password_text_cmd,
3012 "password LINE",
3013 "Assign the terminal connection password\n"
3014 "The UNENCRYPTED (cleartext) line password\n")
3015
3016/* VTY enable password set. */
3017 DEFUN(config_enable_password, enable_password_cmd,
3018 "enable password (8|) WORD",
3019 "Modify enable password parameters\n"
3020 "Assign the privileged level password\n"
3021 "Specifies a HIDDEN password will follow\n"
3022 "dummy string \n" "The HIDDEN 'enable' password string\n")
3023{
3024 /* Argument check. */
3025 if (argc == 0) {
3026 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3027 return CMD_WARNING;
3028 }
3029
3030 /* Crypt type is specified. */
3031 if (argc == 2) {
3032 if (*argv[0] == '8') {
3033 if (host.enable)
3034 talloc_free(host.enable);
3035 host.enable = NULL;
3036
3037 if (host.enable_encrypt)
3038 talloc_free(host.enable_encrypt);
3039 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3040
3041 return CMD_SUCCESS;
3042 } else {
3043 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3044 return CMD_WARNING;
3045 }
3046 }
3047
3048 if (!isalnum((int)*argv[0])) {
3049 vty_out(vty,
3050 "Please specify string starting with alphanumeric%s",
3051 VTY_NEWLINE);
3052 return CMD_WARNING;
3053 }
3054
3055 if (host.enable)
3056 talloc_free(host.enable);
3057 host.enable = NULL;
3058
3059 /* Plain password input. */
3060#ifdef VTY_CRYPT_PW
3061 if (host.encrypt) {
3062 if (host.enable_encrypt)
3063 talloc_free(host.enable_encrypt);
3064 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3065 } else
3066#endif
3067 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3068
3069 return CMD_SUCCESS;
3070}
3071
3072ALIAS(config_enable_password,
3073 enable_password_text_cmd,
3074 "enable password LINE",
3075 "Modify enable password parameters\n"
3076 "Assign the privileged level password\n"
3077 "The UNENCRYPTED (cleartext) 'enable' password\n")
3078
3079/* VTY enable password delete. */
3080 DEFUN(no_config_enable_password, no_enable_password_cmd,
3081 "no enable password",
3082 NO_STR
3083 "Modify enable password parameters\n"
3084 "Assign the privileged level password\n")
3085{
3086 if (host.enable)
3087 talloc_free(host.enable);
3088 host.enable = NULL;
3089
3090 if (host.enable_encrypt)
3091 talloc_free(host.enable_encrypt);
3092 host.enable_encrypt = NULL;
3093
3094 return CMD_SUCCESS;
3095}
3096
3097#ifdef VTY_CRYPT_PW
3098DEFUN(service_password_encrypt,
3099 service_password_encrypt_cmd,
3100 "service password-encryption",
3101 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3102{
3103 if (host.encrypt)
3104 return CMD_SUCCESS;
3105
3106 host.encrypt = 1;
3107
3108 if (host.password) {
3109 if (host.password_encrypt)
3110 talloc_free(host.password_encrypt);
3111 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
3112 }
3113 if (host.enable) {
3114 if (host.enable_encrypt)
3115 talloc_free(host.enable_encrypt);
3116 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
3117 }
3118
3119 return CMD_SUCCESS;
3120}
3121
3122DEFUN(no_service_password_encrypt,
3123 no_service_password_encrypt_cmd,
3124 "no service password-encryption",
3125 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3126{
3127 if (!host.encrypt)
3128 return CMD_SUCCESS;
3129
3130 host.encrypt = 0;
3131
3132 if (host.password_encrypt)
3133 talloc_free(host.password_encrypt);
3134 host.password_encrypt = NULL;
3135
3136 if (host.enable_encrypt)
3137 talloc_free(host.enable_encrypt);
3138 host.enable_encrypt = NULL;
3139
3140 return CMD_SUCCESS;
3141}
3142#endif
3143
3144DEFUN(config_terminal_length, config_terminal_length_cmd,
3145 "terminal length <0-512>",
3146 "Set terminal line parameters\n"
3147 "Set number of lines on a screen\n"
3148 "Number of lines on screen (0 for no pausing)\n")
3149{
3150 int lines;
3151 char *endptr = NULL;
3152
3153 lines = strtol(argv[0], &endptr, 10);
3154 if (lines < 0 || lines > 512 || *endptr != '\0') {
3155 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3156 return CMD_WARNING;
3157 }
3158 vty->lines = lines;
3159
3160 return CMD_SUCCESS;
3161}
3162
3163DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
3164 "terminal no length",
3165 "Set terminal line parameters\n"
3166 NO_STR "Set number of lines on a screen\n")
3167{
3168 vty->lines = -1;
3169 return CMD_SUCCESS;
3170}
3171
3172DEFUN(service_terminal_length, service_terminal_length_cmd,
3173 "service terminal-length <0-512>",
3174 "Set up miscellaneous service\n"
3175 "System wide terminal length configuration\n"
3176 "Number of lines of VTY (0 means no line control)\n")
3177{
3178 int lines;
3179 char *endptr = NULL;
3180
3181 lines = strtol(argv[0], &endptr, 10);
3182 if (lines < 0 || lines > 512 || *endptr != '\0') {
3183 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3184 return CMD_WARNING;
3185 }
3186 host.lines = lines;
3187
3188 return CMD_SUCCESS;
3189}
3190
3191DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
3192 "no service terminal-length [<0-512>]",
3193 NO_STR
3194 "Set up miscellaneous service\n"
3195 "System wide terminal length configuration\n"
3196 "Number of lines of VTY (0 means no line control)\n")
3197{
3198 host.lines = -1;
3199 return CMD_SUCCESS;
3200}
3201
3202DEFUN_HIDDEN(do_echo,
3203 echo_cmd,
3204 "echo .MESSAGE",
3205 "Echo a message back to the vty\n" "The message to echo\n")
3206{
3207 char *message;
3208
3209 vty_out(vty, "%s%s",
3210 ((message =
3211 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
3212 if (message)
3213 talloc_free(message);
3214 return CMD_SUCCESS;
3215}
3216
3217#if 0
3218DEFUN(config_logmsg,
3219 config_logmsg_cmd,
3220 "logmsg " LOG_LEVELS " .MESSAGE",
3221 "Send a message to enabled logging destinations\n"
3222 LOG_LEVEL_DESC "The message to send\n")
3223{
3224 int level;
3225 char *message;
3226
3227 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3228 return CMD_ERR_NO_MATCH;
3229
3230 zlog(NULL, level,
3231 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3232 if (message)
3233 talloc_free(message);
3234 return CMD_SUCCESS;
3235}
3236
3237DEFUN(show_logging,
3238 show_logging_cmd,
3239 "show logging", SHOW_STR "Show current logging configuration\n")
3240{
3241 struct zlog *zl = zlog_default;
3242
3243 vty_out(vty, "Syslog logging: ");
3244 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3245 vty_out(vty, "disabled");
3246 else
3247 vty_out(vty, "level %s, facility %s, ident %s",
3248 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3249 facility_name(zl->facility), zl->ident);
3250 vty_out(vty, "%s", VTY_NEWLINE);
3251
3252 vty_out(vty, "Stdout logging: ");
3253 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3254 vty_out(vty, "disabled");
3255 else
3256 vty_out(vty, "level %s",
3257 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3258 vty_out(vty, "%s", VTY_NEWLINE);
3259
3260 vty_out(vty, "Monitor logging: ");
3261 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3262 vty_out(vty, "disabled");
3263 else
3264 vty_out(vty, "level %s",
3265 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3266 vty_out(vty, "%s", VTY_NEWLINE);
3267
3268 vty_out(vty, "File logging: ");
3269 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3270 vty_out(vty, "disabled");
3271 else
3272 vty_out(vty, "level %s, filename %s",
3273 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3274 zl->filename);
3275 vty_out(vty, "%s", VTY_NEWLINE);
3276
3277 vty_out(vty, "Protocol name: %s%s",
3278 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3279 vty_out(vty, "Record priority: %s%s",
3280 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3281
3282 return CMD_SUCCESS;
3283}
3284
3285DEFUN(config_log_stdout,
3286 config_log_stdout_cmd,
3287 "log stdout", "Logging control\n" "Set stdout logging level\n")
3288{
3289 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3290 return CMD_SUCCESS;
3291}
3292
3293DEFUN(config_log_stdout_level,
3294 config_log_stdout_level_cmd,
3295 "log stdout " LOG_LEVELS,
3296 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3297{
3298 int level;
3299
3300 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3301 return CMD_ERR_NO_MATCH;
3302 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3303 return CMD_SUCCESS;
3304}
3305
3306DEFUN(no_config_log_stdout,
3307 no_config_log_stdout_cmd,
3308 "no log stdout [LEVEL]",
3309 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3310{
3311 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3312 return CMD_SUCCESS;
3313}
3314
3315DEFUN(config_log_monitor,
3316 config_log_monitor_cmd,
3317 "log monitor",
3318 "Logging control\n" "Set terminal line (monitor) logging level\n")
3319{
3320 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3321 return CMD_SUCCESS;
3322}
3323
3324DEFUN(config_log_monitor_level,
3325 config_log_monitor_level_cmd,
3326 "log monitor " LOG_LEVELS,
3327 "Logging control\n"
3328 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3329{
3330 int level;
3331
3332 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3333 return CMD_ERR_NO_MATCH;
3334 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3335 return CMD_SUCCESS;
3336}
3337
3338DEFUN(no_config_log_monitor,
3339 no_config_log_monitor_cmd,
3340 "no log monitor [LEVEL]",
3341 NO_STR
3342 "Logging control\n"
3343 "Disable terminal line (monitor) logging\n" "Logging level\n")
3344{
3345 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3346 return CMD_SUCCESS;
3347}
3348
3349static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3350{
3351 int ret;
3352 char *p = NULL;
3353 const char *fullpath;
3354
3355 /* Path detection. */
3356 if (!IS_DIRECTORY_SEP(*fname)) {
3357 char cwd[MAXPATHLEN + 1];
3358 cwd[MAXPATHLEN] = '\0';
3359
3360 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3361 zlog_err("config_log_file: Unable to alloc mem!");
3362 return CMD_WARNING;
3363 }
3364
3365 if ((p = _talloc_zero(tall_vcmd_ctx,
3366 strlen(cwd) + strlen(fname) + 2),
3367 "set_log_file")
3368 == NULL) {
3369 zlog_err("config_log_file: Unable to alloc mem!");
3370 return CMD_WARNING;
3371 }
3372 sprintf(p, "%s/%s", cwd, fname);
3373 fullpath = p;
3374 } else
3375 fullpath = fname;
3376
3377 ret = zlog_set_file(NULL, fullpath, loglevel);
3378
3379 if (p)
3380 talloc_free(p);
3381
3382 if (!ret) {
3383 vty_out(vty, "can't open logfile %s\n", fname);
3384 return CMD_WARNING;
3385 }
3386
3387 if (host.logfile)
3388 talloc_free(host.logfile);
3389
3390 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3391
3392 return CMD_SUCCESS;
3393}
3394
3395DEFUN(config_log_file,
3396 config_log_file_cmd,
3397 "log file FILENAME",
3398 "Logging control\n" "Logging to file\n" "Logging filename\n")
3399{
3400 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3401}
3402
3403DEFUN(config_log_file_level,
3404 config_log_file_level_cmd,
3405 "log file FILENAME " LOG_LEVELS,
3406 "Logging control\n"
3407 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3408{
3409 int level;
3410
3411 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3412 return CMD_ERR_NO_MATCH;
3413 return set_log_file(vty, argv[0], level);
3414}
3415
3416DEFUN(no_config_log_file,
3417 no_config_log_file_cmd,
3418 "no log file [FILENAME]",
3419 NO_STR
3420 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3421{
3422 zlog_reset_file(NULL);
3423
3424 if (host.logfile)
3425 talloc_free(host.logfile);
3426
3427 host.logfile = NULL;
3428
3429 return CMD_SUCCESS;
3430}
3431
3432ALIAS(no_config_log_file,
3433 no_config_log_file_level_cmd,
3434 "no log file FILENAME LEVEL",
3435 NO_STR
3436 "Logging control\n"
3437 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3438
3439 DEFUN(config_log_syslog,
3440 config_log_syslog_cmd,
3441 "log syslog", "Logging control\n" "Set syslog logging level\n")
3442{
3443 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3444 return CMD_SUCCESS;
3445}
3446
3447DEFUN(config_log_syslog_level,
3448 config_log_syslog_level_cmd,
3449 "log syslog " LOG_LEVELS,
3450 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3451{
3452 int level;
3453
3454 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3455 return CMD_ERR_NO_MATCH;
3456 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3457 return CMD_SUCCESS;
3458}
3459
3460DEFUN_DEPRECATED(config_log_syslog_facility,
3461 config_log_syslog_facility_cmd,
3462 "log syslog facility " LOG_FACILITIES,
3463 "Logging control\n"
3464 "Logging goes to syslog\n"
3465 "(Deprecated) Facility parameter for syslog messages\n"
3466 LOG_FACILITY_DESC)
3467{
3468 int facility;
3469
3470 if ((facility = facility_match(argv[0])) < 0)
3471 return CMD_ERR_NO_MATCH;
3472
3473 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3474 zlog_default->facility = facility;
3475 return CMD_SUCCESS;
3476}
3477
3478DEFUN(no_config_log_syslog,
3479 no_config_log_syslog_cmd,
3480 "no log syslog [LEVEL]",
3481 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3482{
3483 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3484 return CMD_SUCCESS;
3485}
3486
3487ALIAS(no_config_log_syslog,
3488 no_config_log_syslog_facility_cmd,
3489 "no log syslog facility " LOG_FACILITIES,
3490 NO_STR
3491 "Logging control\n"
3492 "Logging goes to syslog\n"
3493 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3494
3495 DEFUN(config_log_facility,
3496 config_log_facility_cmd,
3497 "log facility " LOG_FACILITIES,
3498 "Logging control\n"
3499 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3500{
3501 int facility;
3502
3503 if ((facility = facility_match(argv[0])) < 0)
3504 return CMD_ERR_NO_MATCH;
3505 zlog_default->facility = facility;
3506 return CMD_SUCCESS;
3507}
3508
3509DEFUN(no_config_log_facility,
3510 no_config_log_facility_cmd,
3511 "no log facility [FACILITY]",
3512 NO_STR
3513 "Logging control\n"
3514 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3515{
3516 zlog_default->facility = LOG_DAEMON;
3517 return CMD_SUCCESS;
3518}
3519
3520DEFUN_DEPRECATED(config_log_trap,
3521 config_log_trap_cmd,
3522 "log trap " LOG_LEVELS,
3523 "Logging control\n"
3524 "(Deprecated) Set logging level and default for all destinations\n"
3525 LOG_LEVEL_DESC)
3526{
3527 int new_level;
3528 int i;
3529
3530 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3531 return CMD_ERR_NO_MATCH;
3532
3533 zlog_default->default_lvl = new_level;
3534 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3535 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3536 zlog_default->maxlvl[i] = new_level;
3537 return CMD_SUCCESS;
3538}
3539
3540DEFUN_DEPRECATED(no_config_log_trap,
3541 no_config_log_trap_cmd,
3542 "no log trap [LEVEL]",
3543 NO_STR
3544 "Logging control\n"
3545 "Permit all logging information\n" "Logging level\n")
3546{
3547 zlog_default->default_lvl = LOG_DEBUG;
3548 return CMD_SUCCESS;
3549}
3550
3551DEFUN(config_log_record_priority,
3552 config_log_record_priority_cmd,
3553 "log record-priority",
3554 "Logging control\n"
3555 "Log the priority of the message within the message\n")
3556{
3557 zlog_default->record_priority = 1;
3558 return CMD_SUCCESS;
3559}
3560
3561DEFUN(no_config_log_record_priority,
3562 no_config_log_record_priority_cmd,
3563 "no log record-priority",
3564 NO_STR
3565 "Logging control\n"
3566 "Do not log the priority of the message within the message\n")
3567{
3568 zlog_default->record_priority = 0;
3569 return CMD_SUCCESS;
3570}
3571#endif
3572
3573DEFUN(banner_motd_file,
3574 banner_motd_file_cmd,
3575 "banner motd file [FILE]",
3576 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3577{
3578 if (host.motdfile)
3579 talloc_free(host.motdfile);
3580 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3581
3582 return CMD_SUCCESS;
3583}
3584
3585DEFUN(banner_motd_default,
3586 banner_motd_default_cmd,
3587 "banner motd default",
3588 "Set banner string\n" "Strings for motd\n" "Default string\n")
3589{
3590 host.motd = default_motd;
3591 return CMD_SUCCESS;
3592}
3593
3594DEFUN(no_banner_motd,
3595 no_banner_motd_cmd,
3596 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3597{
3598 host.motd = NULL;
3599 if (host.motdfile)
3600 talloc_free(host.motdfile);
3601 host.motdfile = NULL;
3602 return CMD_SUCCESS;
3603}
3604
3605/* Set config filename. Called from vty.c */
3606void host_config_set(const char *filename)
3607{
3608 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3609}
3610
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003611/*! Deprecated, now happens implicitly when calling install_node().
3612 * Users of the API may still attempt to call this function, hence
3613 * leave it here as a no-op. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003614void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003615{
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003616}
3617
3618/*! Deprecated, now happens implicitly when calling install_node().
3619 * Users of the API may still attempt to call this function, hence
3620 * leave it here as a no-op. */
3621void vty_install_default(int node)
3622{
3623}
3624
3625/*! Install common commands like 'exit' and 'list'. */
3626static void install_basic_node_commands(int node)
3627{
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003628 install_element(node, &config_help_cmd);
3629 install_element(node, &config_list_cmd);
3630
3631 install_element(node, &config_write_terminal_cmd);
3632 install_element(node, &config_write_file_cmd);
3633 install_element(node, &config_write_memory_cmd);
3634 install_element(node, &config_write_cmd);
3635 install_element(node, &show_running_config_cmd);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003636
3637 install_element(node, &config_exit_cmd);
3638
3639 if (node >= CONFIG_NODE) {
3640 /* It's not a top node. */
3641 install_element(node, &config_end_cmd);
3642 }
3643}
3644
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003645/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003646 * Write the current running config to a given file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003647 * \param[in] vty the vty of the code
3648 * \param[in] filename where to store the file
3649 * \return 0 in case of success.
3650 *
3651 * If the filename already exists create a filename.sav
3652 * version with the current code.
3653 *
3654 */
3655int osmo_vty_write_config_file(const char *filename)
3656{
3657 char *failed_file;
3658 int rc;
3659
3660 rc = write_config_file(filename, &failed_file);
3661 talloc_free(failed_file);
3662 return rc;
3663}
3664
3665/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003666 * Save the current state to the config file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003667 * \return 0 in case of success.
3668 *
3669 * If the filename already exists create a filename.sav
3670 * version with the current code.
3671 *
3672 */
3673int osmo_vty_save_config_file(void)
3674{
3675 char *failed_file;
3676 int rc;
3677
3678 if (host.config == NULL)
3679 return -7;
3680
3681 rc = write_config_file(host.config, &failed_file);
3682 talloc_free(failed_file);
3683 return rc;
3684}
3685
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003686/* Initialize command interface. Install basic nodes and commands. */
3687void cmd_init(int terminal)
3688{
3689 /* Allocate initial top vector of commands. */
3690 cmdvec = vector_init(VECTOR_MIN_SIZE);
3691
3692 /* Default host value settings. */
3693 host.name = NULL;
3694 host.password = NULL;
3695 host.enable = NULL;
3696 host.logfile = NULL;
3697 host.config = NULL;
3698 host.lines = -1;
3699 host.motd = default_motd;
3700 host.motdfile = NULL;
3701
3702 /* Install top nodes. */
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003703 install_node_bare(&view_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003704 install_node(&enable_node, NULL);
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003705 install_node_bare(&auth_node, NULL);
3706 install_node_bare(&auth_enable_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003707 install_node(&config_node, config_write_host);
3708
3709 /* Each node's basic commands. */
3710 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003711 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003712 if (terminal) {
3713 install_element(VIEW_NODE, &config_list_cmd);
3714 install_element(VIEW_NODE, &config_exit_cmd);
3715 install_element(VIEW_NODE, &config_help_cmd);
3716 install_element(VIEW_NODE, &config_enable_cmd);
3717 install_element(VIEW_NODE, &config_terminal_length_cmd);
3718 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3719 install_element(VIEW_NODE, &echo_cmd);
3720 }
3721
3722 if (terminal) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003723 install_element(ENABLE_NODE, &config_disable_cmd);
3724 install_element(ENABLE_NODE, &config_terminal_cmd);
3725 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3726 }
3727 install_element (ENABLE_NODE, &show_startup_config_cmd);
3728 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003729 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003730
3731 if (terminal) {
3732 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3733 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3734 install_element(ENABLE_NODE, &echo_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003735 }
3736
3737 install_element(CONFIG_NODE, &hostname_cmd);
3738 install_element(CONFIG_NODE, &no_hostname_cmd);
3739
3740 if (terminal) {
3741 install_element(CONFIG_NODE, &password_cmd);
3742 install_element(CONFIG_NODE, &password_text_cmd);
3743 install_element(CONFIG_NODE, &enable_password_cmd);
3744 install_element(CONFIG_NODE, &enable_password_text_cmd);
3745 install_element(CONFIG_NODE, &no_enable_password_cmd);
3746
3747#ifdef VTY_CRYPT_PW
3748 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3749 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3750#endif
3751 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3752 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3753 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3754 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3755 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3756
3757 }
3758 srand(time(NULL));
3759}
Harald Welte7acb30c2011-08-17 17:13:48 +02003760
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003761/*! @} */