blob: a19d5db933055664249c44be7d3370fbdc77a307 [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 Hofmeyr87e45502017-06-20 00:17:59 +0200126/*! Install top node of command vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127void install_node(struct cmd_node *node, int (*func) (struct vty *))
128{
129 vector_set_index(cmdvec, node->node, node);
130 node->func = func;
131 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
132}
133
134/* Compare two command's string. Used in sort_node (). */
135static int cmp_node(const void *p, const void *q)
136{
137 struct cmd_element *a = *(struct cmd_element **)p;
138 struct cmd_element *b = *(struct cmd_element **)q;
139
140 return strcmp(a->string, b->string);
141}
142
143static int cmp_desc(const void *p, const void *q)
144{
145 struct desc *a = *(struct desc **)p;
146 struct desc *b = *(struct desc **)q;
147
148 return strcmp(a->cmd, b->cmd);
149}
150
Jacob Erlbeck2442e092013-09-06 16:51:58 +0200151static int is_config_child(struct vty *vty)
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800152{
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800153 if (vty->node <= CONFIG_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800154 return 0;
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800155 else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800156 return 1;
157 else if (host.app_info->is_config_node)
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800158 return host.app_info->is_config_node(vty, vty->node);
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800159 else
160 return vty->node > CONFIG_NODE;
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800161}
162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/*! Sort each node's command element according to command string. */
Harald Welte95b2b472011-07-16 11:58:09 +0200164void sort_node(void)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200165{
166 unsigned int i, j;
167 struct cmd_node *cnode;
168 vector descvec;
169 struct cmd_element *cmd_element;
170
171 for (i = 0; i < vector_active(cmdvec); i++)
172 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
173 vector cmd_vector = cnode->cmd_vector;
174 qsort(cmd_vector->index, vector_active(cmd_vector),
175 sizeof(void *), cmp_node);
176
177 for (j = 0; j < vector_active(cmd_vector); j++)
178 if ((cmd_element =
179 vector_slot(cmd_vector, j)) != NULL
180 && vector_active(cmd_element->strvec)) {
181 descvec =
182 vector_slot(cmd_element->strvec,
183 vector_active
184 (cmd_element->strvec) -
185 1);
186 qsort(descvec->index,
187 vector_active(descvec),
188 sizeof(void *), cmp_desc);
189 }
190 }
191}
192
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200193/*! Break up string in command tokens. Return leading indents.
194 * \param[in] string String to split.
195 * \param[out] indent If not NULL, return a talloc_strdup of indent characters.
196 * \param[out] strvec_p Returns vector of split tokens, must not be NULL.
197 * \returns CMD_SUCCESS or CMD_ERR_INVALID_INDENT
198 *
199 * If \a indent is passed non-NULL, only simple space ' ' indents are allowed,
200 * so that \a indent can simply return the count of leading spaces.
201 * Otherwise any isspace() characters are allowed for indenting (backwards compat).
202 */
203int cmd_make_strvec2(const char *string, char **indent, vector *strvec_p)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200204{
205 const char *cp, *start;
206 char *token;
207 int strlen;
208 vector strvec;
209
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200210 *strvec_p = NULL;
211 if (indent)
212 *indent = 0;
213
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200214 if (string == NULL)
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200215 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200216
217 cp = string;
218
219 /* Skip white spaces. */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200220 while (isspace((int)*cp) && *cp != '\0') {
221 /* if we're counting indents, we need to be strict about them */
222 if (indent && (*cp != ' ') && (*cp != '\t')) {
223 /* Ignore blank lines, they appear as leading whitespace with line breaks. */
224 if (*cp == '\n' || *cp == '\r') {
225 cp++;
226 string = cp;
227 continue;
228 }
229 return CMD_ERR_INVALID_INDENT;
230 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200231 cp++;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200232 }
233
234 if (indent)
235 *indent = talloc_strndup(tall_vty_cmd_ctx, string, cp - string);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200236
237 /* Return if there is only white spaces */
238 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200239 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200240
241 if (*cp == '!' || *cp == '#')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200242 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200243
244 /* Prepare return vector. */
245 strvec = vector_init(VECTOR_MIN_SIZE);
246
247 /* Copy each command piece and set into vector. */
248 while (1) {
249 start = cp;
250 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
251 *cp != '\0')
252 cp++;
253 strlen = cp - start;
254 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
255 memcpy(token, start, strlen);
256 *(token + strlen) = '\0';
257 vector_set(strvec, token);
258
259 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
260 *cp != '\0')
261 cp++;
262
263 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200264 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200265 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200266
267 *strvec_p = strvec;
268 return CMD_SUCCESS;
269}
270
271/*! Breaking up string into each command piece. I assume given
272 character is separated by a space character. Return value is a
273 vector which includes char ** data element. */
274vector cmd_make_strvec(const char *string)
275{
276 vector strvec;
277 cmd_make_strvec2(string, NULL, &strvec);
278 return strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! Free allocated string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200282void cmd_free_strvec(vector v)
283{
284 unsigned int i;
285 char *cp;
286
287 if (!v)
288 return;
289
290 for (i = 0; i < vector_active(v); i++)
291 if ((cp = vector_slot(v, i)) != NULL)
292 talloc_free(cp);
293
294 vector_free(v);
295}
296
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200297/*! Fetch next description. Used in \ref cmd_make_descvec(). */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200298static char *cmd_desc_str(const char **string)
299{
300 const char *cp, *start;
301 char *token;
302 int strlen;
303
304 cp = *string;
305
306 if (cp == NULL)
307 return NULL;
308
309 /* Skip white spaces. */
310 while (isspace((int)*cp) && *cp != '\0')
311 cp++;
312
313 /* Return if there is only white spaces */
314 if (*cp == '\0')
315 return NULL;
316
317 start = cp;
318
319 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
320 cp++;
321
322 strlen = cp - start;
323 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
324 memcpy(token, start, strlen);
325 *(token + strlen) = '\0';
326
327 *string = cp;
328
329 return token;
330}
331
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200332/*! New string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200333static vector cmd_make_descvec(const char *string, const char *descstr)
334{
335 int multiple = 0;
336 const char *sp;
337 char *token;
338 int len;
339 const char *cp;
340 const char *dp;
341 vector allvec;
342 vector strvec = NULL;
343 struct desc *desc;
344
345 cp = string;
346 dp = descstr;
347
348 if (cp == NULL)
349 return NULL;
350
351 allvec = vector_init(VECTOR_MIN_SIZE);
352
353 while (1) {
354 while (isspace((int)*cp) && *cp != '\0')
355 cp++;
356
357 if (*cp == '(') {
358 multiple = 1;
359 cp++;
360 }
361 if (*cp == ')') {
362 multiple = 0;
363 cp++;
364 }
365 if (*cp == '|') {
Harald Weltea99d45a2015-11-12 13:48:23 +0100366 OSMO_ASSERT(multiple);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200367 cp++;
368 }
369
370 while (isspace((int)*cp) && *cp != '\0')
371 cp++;
372
373 if (*cp == '(') {
374 multiple = 1;
375 cp++;
376 }
377
378 if (*cp == '\0')
379 return allvec;
380
381 sp = cp;
382
383 while (!
384 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
385 || *cp == ')' || *cp == '|') && *cp != '\0')
386 cp++;
387
388 len = cp - sp;
389
390 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
391 memcpy(token, sp, len);
392 *(token + len) = '\0';
393
394 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
395 desc->cmd = token;
396 desc->str = cmd_desc_str(&dp);
397
398 if (multiple) {
399 if (multiple == 1) {
400 strvec = vector_init(VECTOR_MIN_SIZE);
401 vector_set(allvec, strvec);
402 }
403 multiple++;
404 } else {
405 strvec = vector_init(VECTOR_MIN_SIZE);
406 vector_set(allvec, strvec);
407 }
408 vector_set(strvec, desc);
409 }
410}
411
412/* Count mandantory string vector size. This is to determine inputed
413 command has enough command length. */
414static int cmd_cmdsize(vector strvec)
415{
416 unsigned int i;
417 int size = 0;
418 vector descvec;
419 struct desc *desc;
420
421 for (i = 0; i < vector_active(strvec); i++)
422 if ((descvec = vector_slot(strvec, i)) != NULL) {
423 if ((vector_active(descvec)) == 1
424 && (desc = vector_slot(descvec, 0)) != NULL) {
425 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
426 return size;
427 else
428 size++;
429 } else
430 size++;
431 }
432 return size;
433}
434
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200435/*! Return prompt character of specified node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200436const char *cmd_prompt(enum node_type node)
437{
438 struct cmd_node *cnode;
439
440 cnode = vector_slot(cmdvec, node);
441 return cnode->prompt;
442}
443
Alexander Couzensad580ba2016-05-16 16:01:45 +0200444/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200445 * escape all special asciidoc symbols
Alexander Couzensad580ba2016-05-16 16:01:45 +0200446 * \param unsafe string
447 * \return a new talloc char *
448 */
449char *osmo_asciidoc_escape(const char *inp)
450{
451 int _strlen;
452 char *out, *out_ptr;
453 int len = 0, i, j;
454
455 if (!inp)
456 return NULL;
457 _strlen = strlen(inp);
458
459 for (i = 0; i < _strlen; ++i) {
460 switch (inp[i]) {
461 case '|':
462 len += 2;
463 break;
464 default:
465 len += 1;
466 break;
467 }
468 }
469
470 out = talloc_size(NULL, len + 1);
471 if (!out)
472 return NULL;
473
474 out_ptr = out;
475
476#define ADD(out, str) \
477 for (j = 0; j < strlen(str); ++j) \
478 *(out++) = str[j];
479
480 for (i = 0; i < _strlen; ++i) {
481 switch (inp[i]) {
482 case '|':
483 ADD(out_ptr, "\\|");
484 break;
485 default:
486 *(out_ptr++) = inp[i];
487 break;
488 }
489 }
490
491#undef ADD
492
493 out_ptr[0] = '\0';
494 return out;
495}
496
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100497static char *xml_escape(const char *inp)
498{
499 int _strlen;
500 char *out, *out_ptr;
501 int len = 0, i, j;
502
503 if (!inp)
504 return NULL;
505 _strlen = strlen(inp);
506
507 for (i = 0; i < _strlen; ++i) {
508 switch (inp[i]) {
509 case '"':
510 len += 6;
511 break;
512 case '\'':
513 len += 6;
514 break;
515 case '<':
516 len += 4;
517 break;
518 case '>':
519 len += 4;
520 break;
521 case '&':
522 len += 5;
523 break;
524 default:
525 len += 1;
526 break;
527 }
528 }
529
530 out = talloc_size(NULL, len + 1);
531 if (!out)
532 return NULL;
533
534 out_ptr = out;
535
536#define ADD(out, str) \
537 for (j = 0; j < strlen(str); ++j) \
538 *(out++) = str[j];
539
540 for (i = 0; i < _strlen; ++i) {
541 switch (inp[i]) {
542 case '"':
543 ADD(out_ptr, "&quot;");
544 break;
545 case '\'':
546 ADD(out_ptr, "&apos;");
547 break;
548 case '<':
549 ADD(out_ptr, "&lt;");
550 break;
551 case '>':
552 ADD(out_ptr, "&gt;");
553 break;
554 case '&':
555 ADD(out_ptr, "&amp;");
556 break;
557 default:
558 *(out_ptr++) = inp[i];
559 break;
560 }
561 }
562
563#undef ADD
564
565 out_ptr[0] = '\0';
566 return out;
567}
568
569/*
570 * Write one cmd_element as XML to the given VTY.
571 */
572static int vty_dump_element(struct cmd_element *cmd, struct vty *vty)
573{
574 char *xml_string = xml_escape(cmd->string);
575
576 vty_out(vty, " <command id='%s'>%s", xml_string, VTY_NEWLINE);
577 vty_out(vty, " <params>%s", VTY_NEWLINE);
578
579 int j;
580 for (j = 0; j < vector_count(cmd->strvec); ++j) {
581 vector descvec = vector_slot(cmd->strvec, j);
582 int i;
583 for (i = 0; i < vector_active(descvec); ++i) {
584 char *xml_param, *xml_doc;
585 struct desc *desc = vector_slot(descvec, i);
586 if (desc == NULL)
587 continue;
588
589 xml_param = xml_escape(desc->cmd);
590 xml_doc = xml_escape(desc->str);
591 vty_out(vty, " <param name='%s' doc='%s' />%s",
592 xml_param, xml_doc, VTY_NEWLINE);
593 talloc_free(xml_param);
594 talloc_free(xml_doc);
595 }
596 }
597
598 vty_out(vty, " </params>%s", VTY_NEWLINE);
599 vty_out(vty, " </command>%s", VTY_NEWLINE);
600
601 talloc_free(xml_string);
602 return 0;
603}
604
605/*
606 * Dump all nodes and commands associated with a given node as XML to the VTY.
607 */
608static int vty_dump_nodes(struct vty *vty)
609{
610 int i, j;
611
612 vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
613
614 for (i = 0; i < vector_active(cmdvec); ++i) {
615 struct cmd_node *cnode;
616 cnode = vector_slot(cmdvec, i);
617 if (!cnode)
618 continue;
619
620 vty_out(vty, " <node id='%d'>%s", i, VTY_NEWLINE);
621
622 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
623 struct cmd_element *elem;
624 elem = vector_slot(cnode->cmd_vector, j);
625 vty_dump_element(elem, vty);
626 }
627
628 vty_out(vty, " </node>%s", VTY_NEWLINE);
629 }
630
631 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
632
633 return 0;
634}
635
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200636/* Check if a command with given string exists at given node */
Harald Welteaddeaa32017-01-07 12:52:00 +0100637static int check_element_exists(struct cmd_node *cnode, const char *cmdstring)
638{
639 int i;
640
641 for (i = 0; i < vector_active(cnode->cmd_vector); ++i) {
642 struct cmd_element *elem;
643 elem = vector_slot(cnode->cmd_vector, i);
644 if (!elem->string)
645 continue;
646 if (!strcmp(elem->string, cmdstring))
647 return 1;
648 }
649 return 0;
650}
651
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200652/*! Install a command into a node
Harald Welte7acb30c2011-08-17 17:13:48 +0200653 * \param[in] ntype Node Type
654 * \param[cmd] element to be installed
655 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000656void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200657{
658 struct cmd_node *cnode;
659
660 cnode = vector_slot(cmdvec, ntype);
661
Harald Weltea99d45a2015-11-12 13:48:23 +0100662 OSMO_ASSERT(cnode);
Harald Welteaddeaa32017-01-07 12:52:00 +0100663 /* ensure no _identical_ command has been registered at this
664 * node so far */
665 OSMO_ASSERT(!check_element_exists(cnode, cmd->string));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200666
667 vector_set(cnode->cmd_vector, cmd);
668
669 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
670 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
671}
672
673/* Install a command into VIEW and ENABLE node */
674void install_element_ve(struct cmd_element *cmd)
675{
676 install_element(VIEW_NODE, cmd);
677 install_element(ENABLE_NODE, cmd);
678}
679
680#ifdef VTY_CRYPT_PW
681static unsigned char itoa64[] =
682 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
683
684static void to64(char *s, long v, int n)
685{
686 while (--n >= 0) {
687 *s++ = itoa64[v & 0x3f];
688 v >>= 6;
689 }
690}
691
692static char *zencrypt(const char *passwd)
693{
694 char salt[6];
695 struct timeval tv;
696 char *crypt(const char *, const char *);
697
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200698 osmo_gettimeofday(&tv, 0);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200699
700 to64(&salt[0], random(), 3);
701 to64(&salt[3], tv.tv_usec, 3);
702 salt[5] = '\0';
703
704 return crypt(passwd, salt);
705}
706#endif
707
708/* This function write configuration of this host. */
709static int config_write_host(struct vty *vty)
710{
711 if (host.name)
712 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
713
714 if (host.encrypt) {
715 if (host.password_encrypt)
716 vty_out(vty, "password 8 %s%s", host.password_encrypt,
717 VTY_NEWLINE);
718 if (host.enable_encrypt)
719 vty_out(vty, "enable password 8 %s%s",
720 host.enable_encrypt, VTY_NEWLINE);
721 } else {
722 if (host.password)
723 vty_out(vty, "password %s%s", host.password,
724 VTY_NEWLINE);
725 if (host.enable)
726 vty_out(vty, "enable password %s%s", host.enable,
727 VTY_NEWLINE);
728 }
729
730 if (host.advanced)
731 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
732
733 if (host.encrypt)
734 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
735
736 if (host.lines >= 0)
737 vty_out(vty, "service terminal-length %d%s", host.lines,
738 VTY_NEWLINE);
739
740 if (host.motdfile)
741 vty_out(vty, "banner motd file %s%s", host.motdfile,
742 VTY_NEWLINE);
743 else if (!host.motd)
744 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
745
746 return 1;
747}
748
749/* Utility function for getting command vector. */
750static vector cmd_node_vector(vector v, enum node_type ntype)
751{
752 struct cmd_node *cnode = vector_slot(v, ntype);
753 return cnode->cmd_vector;
754}
755
756/* Completion match types. */
757enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100758 no_match = 0,
759 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200760 extend_match,
761 ipv4_prefix_match,
762 ipv4_match,
763 ipv6_prefix_match,
764 ipv6_match,
765 range_match,
766 vararg_match,
767 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100768 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200769};
770
771static enum match_type cmd_ipv4_match(const char *str)
772{
773 const char *sp;
774 int dots = 0, nums = 0;
775 char buf[4];
776
777 if (str == NULL)
778 return partly_match;
779
780 for (;;) {
781 memset(buf, 0, sizeof(buf));
782 sp = str;
783 while (*str != '\0') {
784 if (*str == '.') {
785 if (dots >= 3)
786 return no_match;
787
788 if (*(str + 1) == '.')
789 return no_match;
790
791 if (*(str + 1) == '\0')
792 return partly_match;
793
794 dots++;
795 break;
796 }
797 if (!isdigit((int)*str))
798 return no_match;
799
800 str++;
801 }
802
803 if (str - sp > 3)
804 return no_match;
805
806 strncpy(buf, sp, str - sp);
807 if (atoi(buf) > 255)
808 return no_match;
809
810 nums++;
811
812 if (*str == '\0')
813 break;
814
815 str++;
816 }
817
818 if (nums < 4)
819 return partly_match;
820
821 return exact_match;
822}
823
824static enum match_type cmd_ipv4_prefix_match(const char *str)
825{
826 const char *sp;
827 int dots = 0;
828 char buf[4];
829
830 if (str == NULL)
831 return partly_match;
832
833 for (;;) {
834 memset(buf, 0, sizeof(buf));
835 sp = str;
836 while (*str != '\0' && *str != '/') {
837 if (*str == '.') {
838 if (dots == 3)
839 return no_match;
840
841 if (*(str + 1) == '.' || *(str + 1) == '/')
842 return no_match;
843
844 if (*(str + 1) == '\0')
845 return partly_match;
846
847 dots++;
848 break;
849 }
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 if (dots == 3) {
865 if (*str == '/') {
866 if (*(str + 1) == '\0')
867 return partly_match;
868
869 str++;
870 break;
871 } else if (*str == '\0')
872 return partly_match;
873 }
874
875 if (*str == '\0')
876 return partly_match;
877
878 str++;
879 }
880
881 sp = str;
882 while (*str != '\0') {
883 if (!isdigit((int)*str))
884 return no_match;
885
886 str++;
887 }
888
889 if (atoi(sp) > 32)
890 return no_match;
891
892 return exact_match;
893}
894
895#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
896#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
897#define STATE_START 1
898#define STATE_COLON 2
899#define STATE_DOUBLE 3
900#define STATE_ADDR 4
901#define STATE_DOT 5
902#define STATE_SLASH 6
903#define STATE_MASK 7
904
905#ifdef HAVE_IPV6
906
907static enum match_type cmd_ipv6_match(const char *str)
908{
909 int state = STATE_START;
910 int colons = 0, nums = 0, double_colon = 0;
911 const char *sp = NULL;
912 struct sockaddr_in6 sin6_dummy;
913 int ret;
914
915 if (str == NULL)
916 return partly_match;
917
918 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
919 return no_match;
920
921 /* use inet_pton that has a better support,
922 * for example inet_pton can support the automatic addresses:
923 * ::1.2.3.4
924 */
925 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
926
927 if (ret == 1)
928 return exact_match;
929
930 while (*str != '\0') {
931 switch (state) {
932 case STATE_START:
933 if (*str == ':') {
934 if (*(str + 1) != ':' && *(str + 1) != '\0')
935 return no_match;
936 colons--;
937 state = STATE_COLON;
938 } else {
939 sp = str;
940 state = STATE_ADDR;
941 }
942
943 continue;
944 case STATE_COLON:
945 colons++;
946 if (*(str + 1) == ':')
947 state = STATE_DOUBLE;
948 else {
949 sp = str + 1;
950 state = STATE_ADDR;
951 }
952 break;
953 case STATE_DOUBLE:
954 if (double_colon)
955 return no_match;
956
957 if (*(str + 1) == ':')
958 return no_match;
959 else {
960 if (*(str + 1) != '\0')
961 colons++;
962 sp = str + 1;
963 state = STATE_ADDR;
964 }
965
966 double_colon++;
967 nums++;
968 break;
969 case STATE_ADDR:
970 if (*(str + 1) == ':' || *(str + 1) == '\0') {
971 if (str - sp > 3)
972 return no_match;
973
974 nums++;
975 state = STATE_COLON;
976 }
977 if (*(str + 1) == '.')
978 state = STATE_DOT;
979 break;
980 case STATE_DOT:
981 state = STATE_ADDR;
982 break;
983 default:
984 break;
985 }
986
987 if (nums > 8)
988 return no_match;
989
990 if (colons > 7)
991 return no_match;
992
993 str++;
994 }
995
996#if 0
997 if (nums < 11)
998 return partly_match;
999#endif /* 0 */
1000
1001 return exact_match;
1002}
1003
1004static enum match_type cmd_ipv6_prefix_match(const char *str)
1005{
1006 int state = STATE_START;
1007 int colons = 0, nums = 0, double_colon = 0;
1008 int mask;
1009 const char *sp = NULL;
1010 char *endptr = NULL;
1011
1012 if (str == NULL)
1013 return partly_match;
1014
1015 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
1016 return no_match;
1017
1018 while (*str != '\0' && state != STATE_MASK) {
1019 switch (state) {
1020 case STATE_START:
1021 if (*str == ':') {
1022 if (*(str + 1) != ':' && *(str + 1) != '\0')
1023 return no_match;
1024 colons--;
1025 state = STATE_COLON;
1026 } else {
1027 sp = str;
1028 state = STATE_ADDR;
1029 }
1030
1031 continue;
1032 case STATE_COLON:
1033 colons++;
1034 if (*(str + 1) == '/')
1035 return no_match;
1036 else if (*(str + 1) == ':')
1037 state = STATE_DOUBLE;
1038 else {
1039 sp = str + 1;
1040 state = STATE_ADDR;
1041 }
1042 break;
1043 case STATE_DOUBLE:
1044 if (double_colon)
1045 return no_match;
1046
1047 if (*(str + 1) == ':')
1048 return no_match;
1049 else {
1050 if (*(str + 1) != '\0' && *(str + 1) != '/')
1051 colons++;
1052 sp = str + 1;
1053
1054 if (*(str + 1) == '/')
1055 state = STATE_SLASH;
1056 else
1057 state = STATE_ADDR;
1058 }
1059
1060 double_colon++;
1061 nums += 1;
1062 break;
1063 case STATE_ADDR:
1064 if (*(str + 1) == ':' || *(str + 1) == '.'
1065 || *(str + 1) == '\0' || *(str + 1) == '/') {
1066 if (str - sp > 3)
1067 return no_match;
1068
1069 for (; sp <= str; sp++)
1070 if (*sp == '/')
1071 return no_match;
1072
1073 nums++;
1074
1075 if (*(str + 1) == ':')
1076 state = STATE_COLON;
1077 else if (*(str + 1) == '.')
1078 state = STATE_DOT;
1079 else if (*(str + 1) == '/')
1080 state = STATE_SLASH;
1081 }
1082 break;
1083 case STATE_DOT:
1084 state = STATE_ADDR;
1085 break;
1086 case STATE_SLASH:
1087 if (*(str + 1) == '\0')
1088 return partly_match;
1089
1090 state = STATE_MASK;
1091 break;
1092 default:
1093 break;
1094 }
1095
1096 if (nums > 11)
1097 return no_match;
1098
1099 if (colons > 7)
1100 return no_match;
1101
1102 str++;
1103 }
1104
1105 if (state < STATE_MASK)
1106 return partly_match;
1107
1108 mask = strtol(str, &endptr, 10);
1109 if (*endptr != '\0')
1110 return no_match;
1111
1112 if (mask < 0 || mask > 128)
1113 return no_match;
1114
1115/* I don't know why mask < 13 makes command match partly.
1116 Forgive me to make this comments. I Want to set static default route
1117 because of lack of function to originate default in ospf6d; sorry
1118 yasu
1119 if (mask < 13)
1120 return partly_match;
1121*/
1122
1123 return exact_match;
1124}
1125
1126#endif /* HAVE_IPV6 */
1127
1128#define DECIMAL_STRLEN_MAX 10
1129
1130static int cmd_range_match(const char *range, const char *str)
1131{
1132 char *p;
1133 char buf[DECIMAL_STRLEN_MAX + 1];
1134 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001135
1136 if (str == NULL)
1137 return 1;
1138
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001139 if (range[1] == '-') {
1140 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001141
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001142 val = strtol(str, &endptr, 10);
1143 if (*endptr != '\0')
1144 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001145
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001146 range += 2;
1147 p = strchr(range, '-');
1148 if (p == NULL)
1149 return 0;
1150 if (p - range > DECIMAL_STRLEN_MAX)
1151 return 0;
1152 strncpy(buf, range, p - range);
1153 buf[p - range] = '\0';
1154 min = -strtol(buf, &endptr, 10);
1155 if (*endptr != '\0')
1156 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001157
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001158 range = p + 1;
1159 p = strchr(range, '>');
1160 if (p == NULL)
1161 return 0;
1162 if (p - range > DECIMAL_STRLEN_MAX)
1163 return 0;
1164 strncpy(buf, range, p - range);
1165 buf[p - range] = '\0';
1166 max = strtol(buf, &endptr, 10);
1167 if (*endptr != '\0')
1168 return 0;
1169
1170 if (val < min || val > max)
1171 return 0;
1172 } else {
1173 unsigned long min, max, val;
1174
1175 val = strtoul(str, &endptr, 10);
1176 if (*endptr != '\0')
1177 return 0;
1178
1179 range++;
1180 p = strchr(range, '-');
1181 if (p == NULL)
1182 return 0;
1183 if (p - range > DECIMAL_STRLEN_MAX)
1184 return 0;
1185 strncpy(buf, range, p - range);
1186 buf[p - range] = '\0';
1187 min = strtoul(buf, &endptr, 10);
1188 if (*endptr != '\0')
1189 return 0;
1190
1191 range = p + 1;
1192 p = strchr(range, '>');
1193 if (p == NULL)
1194 return 0;
1195 if (p - range > DECIMAL_STRLEN_MAX)
1196 return 0;
1197 strncpy(buf, range, p - range);
1198 buf[p - range] = '\0';
1199 max = strtoul(buf, &endptr, 10);
1200 if (*endptr != '\0')
1201 return 0;
1202
1203 if (val < min || val > max)
1204 return 0;
1205 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001206
1207 return 1;
1208}
1209
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001210/* helper to retrieve the 'real' argument string from an optional argument */
1211static char *
1212cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001213{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001214 /* we've got "[blah]". We want to strip off the []s and redo the
1215 * match check for "blah"
1216 */
1217 size_t len = strlen(str);
1218 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001219
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001220 if (len < 3)
1221 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001222
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001223 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1224 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001225
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001226 memcpy(tmp, (str + 1), len - 2);
1227 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001228
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001229 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001230}
1231
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001232static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001233cmd_match(const char *str, const char *command,
1234 enum match_type min, bool recur)
1235{
1236
1237 if (recur && CMD_OPTION(str))
1238 {
1239 enum match_type ret;
1240 char *tmp = cmd_deopt(str);
1241
1242 /* this would be a bug in a command, however handle it gracefully
1243 * as it we only discover it if a user tries to run it
1244 */
1245 if (tmp == NULL)
1246 return no_match;
1247
1248 ret = cmd_match(tmp, command, min, false);
1249
1250 talloc_free(tmp);
1251
1252 return ret;
1253 }
1254 else if (CMD_VARARG(str))
1255 return vararg_match;
1256 else if (CMD_RANGE(str))
1257 {
1258 if (cmd_range_match(str, command))
1259 return range_match;
1260 }
1261#ifdef HAVE_IPV6
1262 else if (CMD_IPV6(str))
1263 {
1264 if (cmd_ipv6_match(command) >= min)
1265 return ipv6_match;
1266 }
1267 else if (CMD_IPV6_PREFIX(str))
1268 {
1269 if (cmd_ipv6_prefix_match(command) >= min)
1270 return ipv6_prefix_match;
1271 }
1272#endif /* HAVE_IPV6 */
1273 else if (CMD_IPV4(str))
1274 {
1275 if (cmd_ipv4_match(command) >= min)
1276 return ipv4_match;
1277 }
1278 else if (CMD_IPV4_PREFIX(str))
1279 {
1280 if (cmd_ipv4_prefix_match(command) >= min)
1281 return ipv4_prefix_match;
1282 }
1283 else if (CMD_VARIABLE(str))
1284 return extend_match;
1285 else if (strncmp(command, str, strlen(command)) == 0)
1286 {
1287 if (strcmp(command, str) == 0)
1288 return exact_match;
1289 else if (partly_match >= min)
1290 return partly_match;
1291 }
1292
1293 return no_match;
1294}
1295
1296/* Filter vector at the specified index and by the given command string, to
1297 * the desired matching level (thus allowing part matches), and return match
1298 * type flag.
1299 */
1300static enum match_type
1301cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001302{
1303 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001304 struct cmd_element *cmd_element;
1305 enum match_type match_type;
1306 vector descvec;
1307 struct desc *desc;
1308
1309 match_type = no_match;
1310
1311 /* If command and cmd_element string does not match set NULL to vector */
1312 for (i = 0; i < vector_active(v); i++)
1313 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001314 if (index >= vector_active(cmd_element->strvec))
1315 vector_slot(v, i) = NULL;
1316 else {
1317 unsigned int j;
1318 int matched = 0;
1319
1320 descvec =
1321 vector_slot(cmd_element->strvec, index);
1322
1323 for (j = 0; j < vector_active(descvec); j++)
1324 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001325 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001326
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001327 ret = cmd_match (desc->cmd, command, level, true);
1328
1329 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001330 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001331
1332 if (match_type < ret)
1333 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001334 }
1335 if (!matched)
1336 vector_slot(v, i) = NULL;
1337 }
1338 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001339
1340 if (match_type == no_match)
1341 return no_match;
1342
1343 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1344 * go again and filter out commands whose argument (at this index) is
1345 * 'weaker'. E.g., if we have 2 commands:
1346 *
1347 * foo bar <1-255>
1348 * foo bar BLAH
1349 *
1350 * and the command string is 'foo bar 10', then we will get here with with
1351 * 'range_match' being the strongest match. However, if 'BLAH' came
1352 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1353 *
1354 * If we don't do a 2nd pass and filter it out, the higher-layers will
1355 * consider this to be ambiguous.
1356 */
1357 for (i = 0; i < vector_active(v); i++)
1358 if ((cmd_element = vector_slot(v, i)) != NULL) {
1359 if (index >= vector_active(cmd_element->strvec))
1360 vector_slot(v, i) = NULL;
1361 else {
1362 unsigned int j;
1363 int matched = 0;
1364
1365 descvec =
1366 vector_slot(cmd_element->strvec, index);
1367
1368 for (j = 0; j < vector_active(descvec); j++)
1369 if ((desc = vector_slot(descvec, j))) {
1370 enum match_type ret;
1371
1372 ret = cmd_match(desc->cmd, command, any_match, true);
1373
1374 if (ret >= match_type)
1375 matched++;
1376 }
1377 if (!matched)
1378 vector_slot(v, i) = NULL;
1379 }
1380 }
1381
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001382 return match_type;
1383}
1384
1385/* Check ambiguous match */
1386static int
1387is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1388{
1389 unsigned int i;
1390 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001391 struct cmd_element *cmd_element;
1392 const char *matched = NULL;
1393 vector descvec;
1394 struct desc *desc;
1395
1396 for (i = 0; i < vector_active(v); i++)
1397 if ((cmd_element = vector_slot(v, i)) != NULL) {
1398 int match = 0;
1399
1400 descvec = vector_slot(cmd_element->strvec, index);
1401
1402 for (j = 0; j < vector_active(descvec); j++)
1403 if ((desc = vector_slot(descvec, j))) {
1404 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001405 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001406
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001407 if (CMD_OPTION(str))
1408 if ((str = cmd_deopt(str)) == NULL)
1409 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001410
1411 switch (type) {
1412 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001413 if (!(CMD_VARIABLE (str))
1414 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001415 match++;
1416 break;
1417 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001418 if (!(CMD_VARIABLE (str))
1419 && strncmp(command, str, strlen (command)) == 0)
1420 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001421 if (matched
1422 && strcmp(matched,
1423 str) != 0)
1424 return 1; /* There is ambiguous match. */
1425 else
1426 matched = str;
1427 match++;
1428 }
1429 break;
1430 case range_match:
1431 if (cmd_range_match
1432 (str, command)) {
1433 if (matched
1434 && strcmp(matched,
1435 str) != 0)
1436 return 1;
1437 else
1438 matched = str;
1439 match++;
1440 }
1441 break;
1442#ifdef HAVE_IPV6
1443 case ipv6_match:
1444 if (CMD_IPV6(str))
1445 match++;
1446 break;
1447 case ipv6_prefix_match:
1448 if ((ret =
1449 cmd_ipv6_prefix_match
1450 (command)) != no_match) {
1451 if (ret == partly_match)
1452 return 2; /* There is incomplete match. */
1453
1454 match++;
1455 }
1456 break;
1457#endif /* HAVE_IPV6 */
1458 case ipv4_match:
1459 if (CMD_IPV4(str))
1460 match++;
1461 break;
1462 case ipv4_prefix_match:
1463 if ((ret =
1464 cmd_ipv4_prefix_match
1465 (command)) != no_match) {
1466 if (ret == partly_match)
1467 return 2; /* There is incomplete match. */
1468
1469 match++;
1470 }
1471 break;
1472 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001473 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001474 match++;
1475 break;
1476 case no_match:
1477 default:
1478 break;
1479 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001480
1481 if (CMD_OPTION(desc->cmd))
1482 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001483 }
1484 if (!match)
1485 vector_slot(v, i) = NULL;
1486 }
1487 return 0;
1488}
1489
1490/* If src matches dst return dst string, otherwise return NULL */
1491static const char *cmd_entry_function(const char *src, const char *dst)
1492{
1493 /* Skip variable arguments. */
1494 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1495 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1496 return NULL;
1497
1498 /* In case of 'command \t', given src is NULL string. */
1499 if (src == NULL)
1500 return dst;
1501
1502 /* Matched with input string. */
1503 if (strncmp(src, dst, strlen(src)) == 0)
1504 return dst;
1505
1506 return NULL;
1507}
1508
1509/* If src matches dst return dst string, otherwise return NULL */
1510/* This version will return the dst string always if it is
1511 CMD_VARIABLE for '?' key processing */
1512static const char *cmd_entry_function_desc(const char *src, const char *dst)
1513{
1514 if (CMD_VARARG(dst))
1515 return dst;
1516
1517 if (CMD_RANGE(dst)) {
1518 if (cmd_range_match(dst, src))
1519 return dst;
1520 else
1521 return NULL;
1522 }
1523#ifdef HAVE_IPV6
1524 if (CMD_IPV6(dst)) {
1525 if (cmd_ipv6_match(src))
1526 return dst;
1527 else
1528 return NULL;
1529 }
1530
1531 if (CMD_IPV6_PREFIX(dst)) {
1532 if (cmd_ipv6_prefix_match(src))
1533 return dst;
1534 else
1535 return NULL;
1536 }
1537#endif /* HAVE_IPV6 */
1538
1539 if (CMD_IPV4(dst)) {
1540 if (cmd_ipv4_match(src))
1541 return dst;
1542 else
1543 return NULL;
1544 }
1545
1546 if (CMD_IPV4_PREFIX(dst)) {
1547 if (cmd_ipv4_prefix_match(src))
1548 return dst;
1549 else
1550 return NULL;
1551 }
1552
1553 /* Optional or variable commands always match on '?' */
1554 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1555 return dst;
1556
1557 /* In case of 'command \t', given src is NULL string. */
1558 if (src == NULL)
1559 return dst;
1560
1561 if (strncmp(src, dst, strlen(src)) == 0)
1562 return dst;
1563 else
1564 return NULL;
1565}
1566
1567/* Check same string element existence. If it isn't there return
1568 1. */
1569static int cmd_unique_string(vector v, const char *str)
1570{
1571 unsigned int i;
1572 char *match;
1573
1574 for (i = 0; i < vector_active(v); i++)
1575 if ((match = vector_slot(v, i)) != NULL)
1576 if (strcmp(match, str) == 0)
1577 return 0;
1578 return 1;
1579}
1580
1581/* Compare string to description vector. If there is same string
1582 return 1 else return 0. */
1583static int desc_unique_string(vector v, const char *str)
1584{
1585 unsigned int i;
1586 struct desc *desc;
1587
1588 for (i = 0; i < vector_active(v); i++)
1589 if ((desc = vector_slot(v, i)) != NULL)
1590 if (strcmp(desc->cmd, str) == 0)
1591 return 1;
1592 return 0;
1593}
1594
1595static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1596{
1597 if (first_word != NULL &&
1598 node != AUTH_NODE &&
1599 node != VIEW_NODE &&
1600 node != AUTH_ENABLE_NODE &&
1601 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1602 return 1;
1603 return 0;
1604}
1605
1606/* '?' describe command support. */
1607static vector
1608cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1609{
1610 unsigned int i;
1611 vector cmd_vector;
1612#define INIT_MATCHVEC_SIZE 10
1613 vector matchvec;
1614 struct cmd_element *cmd_element;
1615 unsigned int index;
1616 int ret;
1617 enum match_type match;
1618 char *command;
1619 static struct desc desc_cr = { "<cr>", "" };
1620
1621 /* Set index. */
1622 if (vector_active(vline) == 0) {
1623 *status = CMD_ERR_NO_MATCH;
1624 return NULL;
1625 } else
1626 index = vector_active(vline) - 1;
1627
1628 /* Make copy vector of current node's command vector. */
1629 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1630
1631 /* Prepare match vector */
1632 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1633
1634 /* Filter commands. */
1635 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001636 for (i = 0; i < index; i++) {
1637 command = vector_slot(vline, i);
1638 if (!command)
1639 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001640
Harald Welte80d30fe2013-02-12 11:08:57 +01001641 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001642
Harald Welte80d30fe2013-02-12 11:08:57 +01001643 if (match == vararg_match) {
1644 struct cmd_element *cmd_element;
1645 vector descvec;
1646 unsigned int j, k;
1647
1648 for (j = 0; j < vector_active(cmd_vector); j++)
1649 if ((cmd_element =
1650 vector_slot(cmd_vector, j)) != NULL
1651 &&
1652 (vector_active(cmd_element->strvec))) {
1653 descvec =
1654 vector_slot(cmd_element->
1655 strvec,
1656 vector_active
1657 (cmd_element->
1658 strvec) - 1);
1659 for (k = 0;
1660 k < vector_active(descvec);
1661 k++) {
1662 struct desc *desc =
1663 vector_slot(descvec,
1664 k);
1665 vector_set(matchvec,
1666 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001667 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001668 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001669
Harald Welte80d30fe2013-02-12 11:08:57 +01001670 vector_set(matchvec, &desc_cr);
1671 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001672
Harald Welte80d30fe2013-02-12 11:08:57 +01001673 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001674 }
1675
Harald Welte80d30fe2013-02-12 11:08:57 +01001676 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1677 match)) == 1) {
1678 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001679 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001680 *status = CMD_ERR_AMBIGUOUS;
1681 return NULL;
1682 } else if (ret == 2) {
1683 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001684 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001685 *status = CMD_ERR_NO_MATCH;
1686 return NULL;
1687 }
1688 }
1689
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001690 /* Prepare match vector */
1691 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1692
1693 /* Make sure that cmd_vector is filtered based on current word */
1694 command = vector_slot(vline, index);
1695 if (command)
Vadim Yanitskiy49a0dec2017-06-12 03:49:38 +07001696 cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001697
1698 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001699 for (i = 0; i < vector_active(cmd_vector); i++) {
1700 const char *string = NULL;
1701 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001702
Harald Welte80d30fe2013-02-12 11:08:57 +01001703 cmd_element = vector_slot(cmd_vector, i);
1704 if (!cmd_element)
1705 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001706
Harald Welted17aa592013-02-12 11:11:34 +01001707 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1708 continue;
1709
Harald Welte80d30fe2013-02-12 11:08:57 +01001710 strvec = cmd_element->strvec;
1711
1712 /* if command is NULL, index may be equal to vector_active */
1713 if (command && index >= vector_active(strvec))
1714 vector_slot(cmd_vector, i) = NULL;
1715 else {
1716 /* Check if command is completed. */
1717 if (command == NULL
1718 && index == vector_active(strvec)) {
1719 string = "<cr>";
1720 if (!desc_unique_string(matchvec, string))
1721 vector_set(matchvec, &desc_cr);
1722 } else {
1723 unsigned int j;
1724 vector descvec = vector_slot(strvec, index);
1725 struct desc *desc;
1726
1727 for (j = 0; j < vector_active(descvec); j++) {
1728 desc = vector_slot(descvec, j);
1729 if (!desc)
1730 continue;
1731 string = cmd_entry_function_desc
1732 (command, desc->cmd);
1733 if (!string)
1734 continue;
1735 /* Uniqueness check */
1736 if (!desc_unique_string(matchvec, string))
1737 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001738 }
1739 }
1740 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001741 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001742 vector_free(cmd_vector);
1743
1744 if (vector_slot(matchvec, 0) == NULL) {
1745 vector_free(matchvec);
1746 *status = CMD_ERR_NO_MATCH;
1747 } else
1748 *status = CMD_SUCCESS;
1749
1750 return matchvec;
1751}
1752
1753vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1754{
1755 vector ret;
1756
1757 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1758 enum node_type onode;
1759 vector shifted_vline;
1760 unsigned int index;
1761
1762 onode = vty->node;
1763 vty->node = ENABLE_NODE;
1764 /* We can try it on enable node, cos' the vty is authenticated */
1765
1766 shifted_vline = vector_init(vector_count(vline));
1767 /* use memcpy? */
1768 for (index = 1; index < vector_active(vline); index++) {
1769 vector_set_index(shifted_vline, index - 1,
1770 vector_lookup(vline, index));
1771 }
1772
1773 ret = cmd_describe_command_real(shifted_vline, vty, status);
1774
1775 vector_free(shifted_vline);
1776 vty->node = onode;
1777 return ret;
1778 }
1779
1780 return cmd_describe_command_real(vline, vty, status);
1781}
1782
1783/* Check LCD of matched command. */
1784static int cmd_lcd(char **matched)
1785{
1786 int i;
1787 int j;
1788 int lcd = -1;
1789 char *s1, *s2;
1790 char c1, c2;
1791
1792 if (matched[0] == NULL || matched[1] == NULL)
1793 return 0;
1794
1795 for (i = 1; matched[i] != NULL; i++) {
1796 s1 = matched[i - 1];
1797 s2 = matched[i];
1798
1799 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1800 if (c1 != c2)
1801 break;
1802
1803 if (lcd < 0)
1804 lcd = j;
1805 else {
1806 if (lcd > j)
1807 lcd = j;
1808 }
1809 }
1810 return lcd;
1811}
1812
1813/* Command line completion support. */
1814static char **cmd_complete_command_real(vector vline, struct vty *vty,
1815 int *status)
1816{
1817 unsigned int i;
1818 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1819#define INIT_MATCHVEC_SIZE 10
1820 vector matchvec;
1821 struct cmd_element *cmd_element;
1822 unsigned int index;
1823 char **match_str;
1824 struct desc *desc;
1825 vector descvec;
1826 char *command;
1827 int lcd;
1828
1829 if (vector_active(vline) == 0) {
1830 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001831 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001832 return NULL;
1833 } else
1834 index = vector_active(vline) - 1;
1835
1836 /* First, filter by preceeding command string */
1837 for (i = 0; i < index; i++)
1838 if ((command = vector_slot(vline, i))) {
1839 enum match_type match;
1840 int ret;
1841
1842 /* First try completion match, if there is exactly match return 1 */
1843 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001844 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001845
1846 /* If there is exact match then filter ambiguous match else check
1847 ambiguousness. */
1848 if ((ret =
1849 is_cmd_ambiguous(command, cmd_vector, i,
1850 match)) == 1) {
1851 vector_free(cmd_vector);
1852 *status = CMD_ERR_AMBIGUOUS;
1853 return NULL;
1854 }
1855 /*
1856 else if (ret == 2)
1857 {
1858 vector_free (cmd_vector);
1859 *status = CMD_ERR_NO_MATCH;
1860 return NULL;
1861 }
1862 */
1863 }
1864
1865 /* Prepare match vector. */
1866 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1867
1868 /* Now we got into completion */
1869 for (i = 0; i < vector_active(cmd_vector); i++)
1870 if ((cmd_element = vector_slot(cmd_vector, i))) {
1871 const char *string;
1872 vector strvec = cmd_element->strvec;
1873
1874 /* Check field length */
1875 if (index >= vector_active(strvec))
1876 vector_slot(cmd_vector, i) = NULL;
1877 else {
1878 unsigned int j;
1879
1880 descvec = vector_slot(strvec, index);
1881 for (j = 0; j < vector_active(descvec); j++)
1882 if ((desc = vector_slot(descvec, j))) {
1883 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1884 if (cmd_unique_string (matchvec, string))
1885 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1886 }
1887 }
1888 }
1889
1890 /* We don't need cmd_vector any more. */
1891 vector_free(cmd_vector);
1892
1893 /* No matched command */
1894 if (vector_slot(matchvec, 0) == NULL) {
1895 vector_free(matchvec);
1896
1897 /* In case of 'command \t' pattern. Do you need '?' command at
1898 the end of the line. */
1899 if (vector_slot(vline, index) == '\0')
1900 *status = CMD_ERR_NOTHING_TODO;
1901 else
1902 *status = CMD_ERR_NO_MATCH;
1903 return NULL;
1904 }
1905
1906 /* Only one matched */
1907 if (vector_slot(matchvec, 1) == NULL) {
1908 match_str = (char **)matchvec->index;
1909 vector_only_wrapper_free(matchvec);
1910 *status = CMD_COMPLETE_FULL_MATCH;
1911 return match_str;
1912 }
1913 /* Make it sure last element is NULL. */
1914 vector_set(matchvec, NULL);
1915
1916 /* Check LCD of matched strings. */
1917 if (vector_slot(vline, index) != NULL) {
1918 lcd = cmd_lcd((char **)matchvec->index);
1919
1920 if (lcd) {
1921 int len = strlen(vector_slot(vline, index));
1922
1923 if (len < lcd) {
1924 char *lcdstr;
1925
1926 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1927 "complete-lcdstr");
1928 memcpy(lcdstr, matchvec->index[0], lcd);
1929 lcdstr[lcd] = '\0';
1930
1931 /* match_str = (char **) &lcdstr; */
1932
1933 /* Free matchvec. */
1934 for (i = 0; i < vector_active(matchvec); i++) {
1935 if (vector_slot(matchvec, i))
1936 talloc_free(vector_slot(matchvec, i));
1937 }
1938 vector_free(matchvec);
1939
1940 /* Make new matchvec. */
1941 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1942 vector_set(matchvec, lcdstr);
1943 match_str = (char **)matchvec->index;
1944 vector_only_wrapper_free(matchvec);
1945
1946 *status = CMD_COMPLETE_MATCH;
1947 return match_str;
1948 }
1949 }
1950 }
1951
1952 match_str = (char **)matchvec->index;
1953 vector_only_wrapper_free(matchvec);
1954 *status = CMD_COMPLETE_LIST_MATCH;
1955 return match_str;
1956}
1957
1958char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1959{
1960 char **ret;
1961
1962 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1963 enum node_type onode;
1964 vector shifted_vline;
1965 unsigned int index;
1966
1967 onode = vty->node;
1968 vty->node = ENABLE_NODE;
1969 /* We can try it on enable node, cos' the vty is authenticated */
1970
1971 shifted_vline = vector_init(vector_count(vline));
1972 /* use memcpy? */
1973 for (index = 1; index < vector_active(vline); index++) {
1974 vector_set_index(shifted_vline, index - 1,
1975 vector_lookup(vline, index));
1976 }
1977
1978 ret = cmd_complete_command_real(shifted_vline, vty, status);
1979
1980 vector_free(shifted_vline);
1981 vty->node = onode;
1982 return ret;
1983 }
1984
1985 return cmd_complete_command_real(vline, vty, status);
1986}
1987
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02001988static struct vty_parent_node *vty_parent(struct vty *vty)
1989{
1990 return llist_first_entry_or_null(&vty->parent_nodes,
1991 struct vty_parent_node,
1992 entry);
1993}
1994
1995static bool vty_pop_parent(struct vty *vty)
1996{
1997 struct vty_parent_node *parent = vty_parent(vty);
1998 if (!parent)
1999 return false;
2000 llist_del(&parent->entry);
2001 vty->node = parent->node;
2002 vty->priv = parent->priv;
2003 if (vty->indent)
2004 talloc_free(vty->indent);
2005 vty->indent = parent->indent;
2006 talloc_free(parent);
2007 return true;
2008}
2009
2010static void vty_clear_parents(struct vty *vty)
2011{
2012 while (vty_pop_parent(vty));
2013}
2014
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002015/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002016/*
2017 * This function MUST eventually converge on a node when called repeatedly,
2018 * there must not be any cycles.
2019 * All 'config' nodes shall converge on CONFIG_NODE.
2020 * All other 'enable' nodes shall converge on ENABLE_NODE.
2021 * All 'view' only nodes shall converge on VIEW_NODE.
2022 * All other nodes shall converge on themselves or it must be ensured,
2023 * that the user's rights are not extended anyhow by calling this function.
2024 *
2025 * Note that these requirements also apply to all functions that are used
2026 * as go_parent_cb.
2027 * Note also that this function relies on the is_config_child callback to
2028 * recognize non-config nodes if go_parent_cb is not set.
2029 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00002030int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002031{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002032 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002033 case AUTH_NODE:
2034 case VIEW_NODE:
2035 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002036 case CONFIG_NODE:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002037 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002038 break;
2039
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002040 case AUTH_ENABLE_NODE:
2041 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002042 vty_clear_parents(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002043 break;
2044
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002045 case CFG_LOG_NODE:
2046 case VTY_NODE:
2047 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002048 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002049 break;
2050
2051 default:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002052 if (host.app_info->go_parent_cb) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002053 host.app_info->go_parent_cb(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002054 vty_pop_parent(vty);
2055 }
2056 else if (is_config_child(vty)) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002057 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002058 vty_clear_parents(vty);
2059 }
2060 else {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002061 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002062 vty_clear_parents(vty);
2063 }
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002064 break;
2065 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002066
2067 return vty->node;
2068}
2069
2070/* Execute command by argument vline vector. */
2071static int
2072cmd_execute_command_real(vector vline, struct vty *vty,
2073 struct cmd_element **cmd)
2074{
2075 unsigned int i;
2076 unsigned int index;
2077 vector cmd_vector;
2078 struct cmd_element *cmd_element;
2079 struct cmd_element *matched_element;
2080 unsigned int matched_count, incomplete_count;
2081 int argc;
2082 const char *argv[CMD_ARGC_MAX];
2083 enum match_type match = 0;
2084 int varflag;
2085 char *command;
2086
2087 /* Make copy of command elements. */
2088 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2089
2090 for (index = 0; index < vector_active(vline); index++)
2091 if ((command = vector_slot(vline, index))) {
2092 int ret;
2093
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002094 match = cmd_filter(command, cmd_vector, index,
2095 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002096
2097 if (match == vararg_match)
2098 break;
2099
2100 ret =
2101 is_cmd_ambiguous(command, cmd_vector, index, match);
2102
2103 if (ret == 1) {
2104 vector_free(cmd_vector);
2105 return CMD_ERR_AMBIGUOUS;
2106 } else if (ret == 2) {
2107 vector_free(cmd_vector);
2108 return CMD_ERR_NO_MATCH;
2109 }
2110 }
2111
2112 /* Check matched count. */
2113 matched_element = NULL;
2114 matched_count = 0;
2115 incomplete_count = 0;
2116
2117 for (i = 0; i < vector_active(cmd_vector); i++)
2118 if ((cmd_element = vector_slot(cmd_vector, i))) {
2119 if (match == vararg_match
2120 || index >= cmd_element->cmdsize) {
2121 matched_element = cmd_element;
2122#if 0
2123 printf("DEBUG: %s\n", cmd_element->string);
2124#endif
2125 matched_count++;
2126 } else {
2127 incomplete_count++;
2128 }
2129 }
2130
2131 /* Finish of using cmd_vector. */
2132 vector_free(cmd_vector);
2133
2134 /* To execute command, matched_count must be 1. */
2135 if (matched_count == 0) {
2136 if (incomplete_count)
2137 return CMD_ERR_INCOMPLETE;
2138 else
2139 return CMD_ERR_NO_MATCH;
2140 }
2141
2142 if (matched_count > 1)
2143 return CMD_ERR_AMBIGUOUS;
2144
2145 /* Argument treatment */
2146 varflag = 0;
2147 argc = 0;
2148
2149 for (i = 0; i < vector_active(vline); i++) {
2150 if (varflag)
2151 argv[argc++] = vector_slot(vline, i);
2152 else {
2153 vector descvec =
2154 vector_slot(matched_element->strvec, i);
2155
2156 if (vector_active(descvec) == 1) {
2157 struct desc *desc = vector_slot(descvec, 0);
2158
2159 if (CMD_VARARG(desc->cmd))
2160 varflag = 1;
2161
2162 if (varflag || CMD_VARIABLE(desc->cmd)
2163 || CMD_OPTION(desc->cmd))
2164 argv[argc++] = vector_slot(vline, i);
2165 } else
2166 argv[argc++] = vector_slot(vline, i);
2167 }
2168
2169 if (argc >= CMD_ARGC_MAX)
2170 return CMD_ERR_EXEED_ARGC_MAX;
2171 }
2172
2173 /* For vtysh execution. */
2174 if (cmd)
2175 *cmd = matched_element;
2176
2177 if (matched_element->daemon)
2178 return CMD_SUCCESS_DAEMON;
2179
2180 /* Execute matched command. */
2181 return (*matched_element->func) (matched_element, vty, argc, argv);
2182}
2183
2184int
2185cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2186 int vtysh)
2187{
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002188 int ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002189 enum node_type onode;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002190
2191 onode = vty->node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002192
2193 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2194 vector shifted_vline;
2195 unsigned int index;
2196
2197 vty->node = ENABLE_NODE;
2198 /* We can try it on enable node, cos' the vty is authenticated */
2199
2200 shifted_vline = vector_init(vector_count(vline));
2201 /* use memcpy? */
2202 for (index = 1; index < vector_active(vline); index++) {
2203 vector_set_index(shifted_vline, index - 1,
2204 vector_lookup(vline, index));
2205 }
2206
2207 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2208
2209 vector_free(shifted_vline);
2210 vty->node = onode;
2211 return ret;
2212 }
2213
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002214 return cmd_execute_command_real(vline, vty, cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002215}
2216
2217/* Execute command by argument readline. */
2218int
2219cmd_execute_command_strict(vector vline, struct vty *vty,
2220 struct cmd_element **cmd)
2221{
2222 unsigned int i;
2223 unsigned int index;
2224 vector cmd_vector;
2225 struct cmd_element *cmd_element;
2226 struct cmd_element *matched_element;
2227 unsigned int matched_count, incomplete_count;
2228 int argc;
2229 const char *argv[CMD_ARGC_MAX];
2230 int varflag;
2231 enum match_type match = 0;
2232 char *command;
2233
2234 /* Make copy of command element */
2235 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2236
2237 for (index = 0; index < vector_active(vline); index++)
2238 if ((command = vector_slot(vline, index))) {
2239 int ret;
2240
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002241 match = cmd_filter(vector_slot(vline, index),
2242 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002243
2244 /* If command meets '.VARARG' then finish matching. */
2245 if (match == vararg_match)
2246 break;
2247
2248 ret =
2249 is_cmd_ambiguous(command, cmd_vector, index, match);
2250 if (ret == 1) {
2251 vector_free(cmd_vector);
2252 return CMD_ERR_AMBIGUOUS;
2253 }
2254 if (ret == 2) {
2255 vector_free(cmd_vector);
2256 return CMD_ERR_NO_MATCH;
2257 }
2258 }
2259
2260 /* Check matched count. */
2261 matched_element = NULL;
2262 matched_count = 0;
2263 incomplete_count = 0;
2264 for (i = 0; i < vector_active(cmd_vector); i++)
2265 if (vector_slot(cmd_vector, i) != NULL) {
2266 cmd_element = vector_slot(cmd_vector, i);
2267
2268 if (match == vararg_match
2269 || index >= cmd_element->cmdsize) {
2270 matched_element = cmd_element;
2271 matched_count++;
2272 } else
2273 incomplete_count++;
2274 }
2275
2276 /* Finish of using cmd_vector. */
2277 vector_free(cmd_vector);
2278
2279 /* To execute command, matched_count must be 1. */
2280 if (matched_count == 0) {
2281 if (incomplete_count)
2282 return CMD_ERR_INCOMPLETE;
2283 else
2284 return CMD_ERR_NO_MATCH;
2285 }
2286
2287 if (matched_count > 1)
2288 return CMD_ERR_AMBIGUOUS;
2289
2290 /* Argument treatment */
2291 varflag = 0;
2292 argc = 0;
2293
2294 for (i = 0; i < vector_active(vline); i++) {
2295 if (varflag)
2296 argv[argc++] = vector_slot(vline, i);
2297 else {
2298 vector descvec =
2299 vector_slot(matched_element->strvec, i);
2300
2301 if (vector_active(descvec) == 1) {
2302 struct desc *desc = vector_slot(descvec, 0);
2303
2304 if (CMD_VARARG(desc->cmd))
2305 varflag = 1;
2306
2307 if (varflag || CMD_VARIABLE(desc->cmd)
2308 || CMD_OPTION(desc->cmd))
2309 argv[argc++] = vector_slot(vline, i);
2310 } else
2311 argv[argc++] = vector_slot(vline, i);
2312 }
2313
2314 if (argc >= CMD_ARGC_MAX)
2315 return CMD_ERR_EXEED_ARGC_MAX;
2316 }
2317
2318 /* For vtysh execution. */
2319 if (cmd)
2320 *cmd = matched_element;
2321
2322 if (matched_element->daemon)
2323 return CMD_SUCCESS_DAEMON;
2324
2325 /* Now execute matched command */
2326 return (*matched_element->func) (matched_element, vty, argc, argv);
2327}
2328
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002329static inline size_t len(const char *str)
2330{
2331 return str? strlen(str) : 0;
2332}
2333
Neels Hofmeyr00b5ed32017-09-20 00:46:03 +02002334/*! Make sure the common length of strings a and b is identical, then compare their lengths. I.e., if a
2335 * is longer than b, a must start with exactly b, and vice versa.
2336 * \returns EINVAL on mismatch, -1 for a < b, 0 for a == b, 1 for a > b.
2337 */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002338static int indent_cmp(const char *a, const char *b)
2339{
2340 size_t al, bl;
2341 al = len(a);
2342 bl = len(b);
2343 if (al > bl) {
2344 if (bl && strncmp(a, b, bl) != 0)
2345 return EINVAL;
2346 return 1;
2347 }
2348 /* al <= bl */
2349 if (al && strncmp(a, b, al) != 0)
2350 return EINVAL;
2351 return (al < bl)? -1 : 0;
2352}
2353
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002354/* Configration make from file. */
2355int config_from_file(struct vty *vty, FILE * fp)
2356{
2357 int ret;
2358 vector vline;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002359 char *indent;
2360 int cmp;
2361 struct vty_parent_node this_node;
2362 struct vty_parent_node *parent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002363
2364 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002365 indent = NULL;
2366 vline = NULL;
2367 ret = cmd_make_strvec2(vty->buf, &indent, &vline);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002368
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002369 if (ret != CMD_SUCCESS)
2370 goto return_invalid_indent;
2371
2372 /* In case of comment or empty line */
2373 if (vline == NULL) {
2374 if (indent) {
2375 talloc_free(indent);
2376 indent = NULL;
2377 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002378 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002379 }
2380
Neels Hofmeyr43063632017-09-19 23:54:01 +02002381 /* We have a nonempty line. */
2382 if (!vty->indent) {
2383 /* We have just entered a node and expecting the first child to come up; but we
2384 * may also skip right back to a parent or ancestor level. */
2385 parent = vty_parent(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002386
Neels Hofmeyr43063632017-09-19 23:54:01 +02002387 /* If there is no parent, record any indentation we encounter. */
2388 cmp = parent ? indent_cmp(indent, parent->indent) : 1;
2389
2390 if (cmp == EINVAL)
2391 goto return_invalid_indent;
2392
2393 if (cmp <= 0) {
2394 /* We have gone right back to the parent level or higher, we are skipping
2395 * this child node level entirely. Pop the parent to go back to a node
2396 * that was actually there (to reinstate vty->indent) and re-use below
2397 * go-parent while-loop to find an accurate match of indent in the node
2398 * ancestry. */
2399 vty_go_parent(vty);
2400 } else {
2401 /* The indent is deeper than the just entered parent, record the new
2402 * indentation characters. */
2403 vty->indent = talloc_strdup(vty, indent);
2404 /* This *is* the new indentation. */
2405 cmp = 0;
2406 }
2407 } else {
2408 /* There is a known indentation for this node level, validate and detect node
2409 * exits. */
2410 cmp = indent_cmp(indent, vty->indent);
2411 if (cmp == EINVAL)
2412 goto return_invalid_indent;
2413 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002414
2415 /* Less indent: go up the parent nodes to find matching amount of less indent. When this
2416 * loop exits, we want to have found an exact match, i.e. cmp == 0. */
2417 while (cmp < 0) {
2418 vty_go_parent(vty);
2419 cmp = indent_cmp(indent, vty->indent);
2420 if (cmp == EINVAL)
2421 goto return_invalid_indent;
2422 }
2423
2424 /* More indent without having entered a child node level? Either the parent node's indent
2425 * wasn't hit exactly (e.g. there's a space more than the parent level had further above)
2426 * or the indentation increased even though the vty command didn't enter a child. */
2427 if (cmp > 0)
2428 goto return_invalid_indent;
2429
2430 /* Remember the current node before the command possibly changes it. */
2431 this_node = (struct vty_parent_node){
2432 .node = vty->node,
2433 .priv = vty->priv,
2434 .indent = vty->indent,
2435 };
2436
2437 parent = vty_parent(vty);
2438 ret = cmd_execute_command_strict(vline, vty, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002439 cmd_free_strvec(vline);
2440
2441 if (ret != CMD_SUCCESS && ret != CMD_WARNING
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002442 && ret != CMD_ERR_NOTHING_TODO) {
2443 if (indent) {
2444 talloc_free(indent);
2445 indent = NULL;
2446 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002447 return ret;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002448 }
2449
2450 /* If we have stepped down into a child node, push a parent frame.
2451 * The causality is such: we don't expect every single node entry implementation to push
2452 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2453 * a parent node. Hence if the node changed without the parent node changing, we must
2454 * have stepped into a child node (and now expect a deeper indent). */
2455 if (vty->node != this_node.node && parent == vty_parent(vty)) {
2456 /* Push the parent node. */
2457 parent = talloc_zero(vty, struct vty_parent_node);
2458 *parent = this_node;
2459 llist_add(&parent->entry, &vty->parent_nodes);
2460
2461 /* The current talloc'ed vty->indent string will now be owned by this parent
2462 * struct. Indicate that we don't know what deeper indent characters the user
2463 * will choose. */
2464 vty->indent = NULL;
2465 }
2466
2467 if (indent) {
2468 talloc_free(indent);
2469 indent = NULL;
2470 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002471 }
2472 return CMD_SUCCESS;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002473
2474return_invalid_indent:
2475 if (vline)
2476 cmd_free_strvec(vline);
2477 if (indent) {
2478 talloc_free(indent);
2479 indent = NULL;
2480 }
2481 return CMD_ERR_INVALID_INDENT;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002482}
2483
2484/* Configration from terminal */
2485DEFUN(config_terminal,
2486 config_terminal_cmd,
2487 "configure terminal",
2488 "Configuration from vty interface\n" "Configuration terminal\n")
2489{
2490 if (vty_config_lock(vty))
2491 vty->node = CONFIG_NODE;
2492 else {
2493 vty_out(vty, "VTY configuration is locked by other VTY%s",
2494 VTY_NEWLINE);
2495 return CMD_WARNING;
2496 }
2497 return CMD_SUCCESS;
2498}
2499
2500/* Enable command */
2501DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2502{
2503 /* If enable password is NULL, change to ENABLE_NODE */
2504 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2505 vty->type == VTY_SHELL_SERV)
2506 vty->node = ENABLE_NODE;
2507 else
2508 vty->node = AUTH_ENABLE_NODE;
2509
2510 return CMD_SUCCESS;
2511}
2512
2513/* Disable command */
2514DEFUN(disable,
2515 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2516{
2517 if (vty->node == ENABLE_NODE)
2518 vty->node = VIEW_NODE;
2519 return CMD_SUCCESS;
2520}
2521
2522/* Down vty node level. */
2523gDEFUN(config_exit,
2524 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2525{
2526 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002527 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002528 case VIEW_NODE:
2529 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002530 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002531 break;
2532 case CONFIG_NODE:
2533 vty->node = ENABLE_NODE;
2534 vty_config_unlock(vty);
2535 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002536 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002537 if (vty->node > CONFIG_NODE)
2538 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002539 break;
2540 }
2541 return CMD_SUCCESS;
2542}
2543
2544/* End of configuration. */
2545 gDEFUN(config_end,
2546 config_end_cmd, "end", "End current mode and change to enable mode.")
2547{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002548 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002549 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002550
2551 /* Repeatedly call go_parent until a top node is reached. */
2552 while (vty->node > CONFIG_NODE) {
2553 if (vty->node == last_node) {
2554 /* Ensure termination, this shouldn't happen. */
2555 break;
2556 }
2557 last_node = vty->node;
2558 vty_go_parent(vty);
2559 }
2560
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002561 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002562 if (vty->node > ENABLE_NODE)
2563 vty->node = ENABLE_NODE;
2564 vty->index = NULL;
2565 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002566 }
2567 return CMD_SUCCESS;
2568}
2569
2570/* Show version. */
2571DEFUN(show_version,
2572 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2573{
Harald Welte237f6242010-05-25 23:00:45 +02002574 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2575 host.app_info->version,
2576 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2577 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002578
2579 return CMD_SUCCESS;
2580}
2581
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002582DEFUN(show_online_help,
2583 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2584{
2585 vty_dump_nodes(vty);
2586 return CMD_SUCCESS;
2587}
2588
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002589/* Help display function for all node. */
2590gDEFUN(config_help,
2591 config_help_cmd, "help", "Description of the interactive help system\n")
2592{
2593 vty_out(vty,
2594 "This VTY provides advanced help features. When you need help,%s\
2595anytime at the command line please press '?'.%s\
2596%s\
2597If nothing matches, the help list will be empty and you must backup%s\
2598 until entering a '?' shows the available options.%s\
2599Two styles of help are provided:%s\
26001. Full help is available when you are ready to enter a%s\
2601command argument (e.g. 'show ?') and describes each possible%s\
2602argument.%s\
26032. Partial help is provided when an abbreviated argument is entered%s\
2604 and you want to know what arguments match the input%s\
2605 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2606 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2607 return CMD_SUCCESS;
2608}
2609
2610/* Help display function for all node. */
2611gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2612{
2613 unsigned int i;
2614 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2615 struct cmd_element *cmd;
2616
2617 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2618 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2619 && !(cmd->attr == CMD_ATTR_DEPRECATED
2620 || cmd->attr == CMD_ATTR_HIDDEN))
2621 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2622 return CMD_SUCCESS;
2623}
2624
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002625static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002626{
2627 unsigned int i;
2628 int fd;
2629 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002630 char *config_file_tmp = NULL;
2631 char *config_file_sav = NULL;
2632 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002633 struct stat st;
2634
2635 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002636
2637 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002638 config_file_sav =
2639 _talloc_zero(tall_vty_cmd_ctx,
2640 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2641 "config_file_sav");
2642 strcpy(config_file_sav, config_file);
2643 strcat(config_file_sav, CONF_BACKUP_EXT);
2644
2645 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2646 "config_file_tmp");
2647 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2648
2649 /* Open file to configuration write. */
2650 fd = mkstemp(config_file_tmp);
2651 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002652 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002653 talloc_free(config_file_tmp);
2654 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002655 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002656 }
2657
2658 /* Make vty for configuration file. */
2659 file_vty = vty_new();
2660 file_vty->fd = fd;
2661 file_vty->type = VTY_FILE;
2662
2663 /* Config file header print. */
2664 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002665 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002666 //vty_time_print (file_vty, 1);
2667 vty_out(file_vty, "!\n");
2668
2669 for (i = 0; i < vector_active(cmdvec); i++)
2670 if ((node = vector_slot(cmdvec, i)) && node->func) {
2671 if ((*node->func) (file_vty))
2672 vty_out(file_vty, "!\n");
2673 }
2674 vty_close(file_vty);
2675
2676 if (unlink(config_file_sav) != 0)
2677 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002678 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002679 talloc_free(config_file_sav);
2680 talloc_free(config_file_tmp);
2681 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002682 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002683 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002684
2685 /* Only link the .sav file if the original file exists */
2686 if (stat(config_file, &st) == 0) {
2687 if (link(config_file, config_file_sav) != 0) {
2688 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2689 talloc_free(config_file_sav);
2690 talloc_free(config_file_tmp);
2691 unlink(config_file_tmp);
2692 return -3;
2693 }
2694 sync();
2695 if (unlink(config_file) != 0) {
2696 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2697 talloc_free(config_file_sav);
2698 talloc_free(config_file_tmp);
2699 unlink(config_file_tmp);
2700 return -4;
2701 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002702 }
2703 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002704 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002705 talloc_free(config_file_sav);
2706 talloc_free(config_file_tmp);
2707 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002708 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002709 }
2710 unlink(config_file_tmp);
2711 sync();
2712
2713 talloc_free(config_file_sav);
2714 talloc_free(config_file_tmp);
2715
2716 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002717 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2718 return -6;
2719 }
2720
2721 return 0;
2722}
2723
2724
2725/* Write current configuration into file. */
2726DEFUN(config_write_file,
2727 config_write_file_cmd,
2728 "write file",
2729 "Write running configuration to memory, network, or terminal\n"
2730 "Write to configuration file\n")
2731{
2732 char *failed_file;
2733 int rc;
2734
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002735 if (host.app_info->config_is_consistent) {
2736 rc = host.app_info->config_is_consistent(vty);
2737 if (!rc) {
2738 vty_out(vty, "Configuration is not consistent%s",
2739 VTY_NEWLINE);
2740 return CMD_WARNING;
2741 }
2742 }
2743
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002744 if (host.config == NULL) {
2745 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2746 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002747 return CMD_WARNING;
2748 }
2749
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002750 rc = write_config_file(host.config, &failed_file);
2751 switch (rc) {
2752 case -1:
2753 vty_out(vty, "Can't open configuration file %s.%s",
2754 failed_file, VTY_NEWLINE);
2755 rc = CMD_WARNING;
2756 break;
2757 case -2:
2758 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2759 failed_file, VTY_NEWLINE);
2760 rc = CMD_WARNING;
2761 break;
2762 case -3:
2763 vty_out(vty, "Can't backup old configuration file %s.%s",
2764 failed_file, VTY_NEWLINE);
2765 rc = CMD_WARNING;
2766 break;
2767 case -4:
2768 vty_out(vty, "Can't unlink configuration file %s.%s",
2769 failed_file, VTY_NEWLINE);
2770 rc = CMD_WARNING;
2771 break;
2772 case -5:
2773 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2774 VTY_NEWLINE);
2775 rc = CMD_WARNING;
2776 break;
2777 case -6:
2778 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2779 failed_file, strerror(errno), errno, VTY_NEWLINE);
2780 rc = CMD_WARNING;
2781 break;
2782 default:
2783 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2784 rc = CMD_SUCCESS;
2785 break;
2786 }
2787
2788 talloc_free(failed_file);
2789 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002790}
2791
2792ALIAS(config_write_file,
2793 config_write_cmd,
2794 "write", "Write running configuration to memory, network, or terminal\n")
2795
2796 ALIAS(config_write_file,
2797 config_write_memory_cmd,
2798 "write memory",
2799 "Write running configuration to memory, network, or terminal\n"
2800 "Write configuration to the file (same as write file)\n")
2801
2802 ALIAS(config_write_file,
2803 copy_runningconfig_startupconfig_cmd,
2804 "copy running-config startup-config",
2805 "Copy configuration\n"
2806 "Copy running config to... \n"
2807 "Copy running config to startup config (same as write file)\n")
2808
2809/* Write current configuration into the terminal. */
2810 DEFUN(config_write_terminal,
2811 config_write_terminal_cmd,
2812 "write terminal",
2813 "Write running configuration to memory, network, or terminal\n"
2814 "Write to terminal\n")
2815{
2816 unsigned int i;
2817 struct cmd_node *node;
2818
2819 if (vty->type == VTY_SHELL_SERV) {
2820 for (i = 0; i < vector_active(cmdvec); i++)
2821 if ((node = vector_slot(cmdvec, i)) && node->func
2822 && node->vtysh) {
2823 if ((*node->func) (vty))
2824 vty_out(vty, "!%s", VTY_NEWLINE);
2825 }
2826 } else {
2827 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2828 VTY_NEWLINE);
2829 vty_out(vty, "!%s", VTY_NEWLINE);
2830
2831 for (i = 0; i < vector_active(cmdvec); i++)
2832 if ((node = vector_slot(cmdvec, i)) && node->func) {
2833 if ((*node->func) (vty))
2834 vty_out(vty, "!%s", VTY_NEWLINE);
2835 }
2836 vty_out(vty, "end%s", VTY_NEWLINE);
2837 }
2838 return CMD_SUCCESS;
2839}
2840
2841/* Write current configuration into the terminal. */
2842ALIAS(config_write_terminal,
2843 show_running_config_cmd,
2844 "show running-config", SHOW_STR "running configuration\n")
2845
2846/* Write startup configuration into the terminal. */
2847 DEFUN(show_startup_config,
2848 show_startup_config_cmd,
2849 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2850{
2851 char buf[BUFSIZ];
2852 FILE *confp;
2853
2854 confp = fopen(host.config, "r");
2855 if (confp == NULL) {
2856 vty_out(vty, "Can't open configuration file [%s]%s",
2857 host.config, VTY_NEWLINE);
2858 return CMD_WARNING;
2859 }
2860
2861 while (fgets(buf, BUFSIZ, confp)) {
2862 char *cp = buf;
2863
2864 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2865 cp++;
2866 *cp = '\0';
2867
2868 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2869 }
2870
2871 fclose(confp);
2872
2873 return CMD_SUCCESS;
2874}
2875
2876/* Hostname configuration */
2877DEFUN(config_hostname,
2878 hostname_cmd,
2879 "hostname WORD",
2880 "Set system's network name\n" "This system's network name\n")
2881{
2882 if (!isalpha((int)*argv[0])) {
2883 vty_out(vty, "Please specify string starting with alphabet%s",
2884 VTY_NEWLINE);
2885 return CMD_WARNING;
2886 }
2887
2888 if (host.name)
2889 talloc_free(host.name);
2890
2891 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2892 return CMD_SUCCESS;
2893}
2894
2895DEFUN(config_no_hostname,
2896 no_hostname_cmd,
2897 "no hostname [HOSTNAME]",
2898 NO_STR "Reset system's network name\n" "Host name of this router\n")
2899{
2900 if (host.name)
2901 talloc_free(host.name);
2902 host.name = NULL;
2903 return CMD_SUCCESS;
2904}
2905
2906/* VTY interface password set. */
2907DEFUN(config_password, password_cmd,
2908 "password (8|) WORD",
2909 "Assign the terminal connection password\n"
2910 "Specifies a HIDDEN password will follow\n"
2911 "dummy string \n" "The HIDDEN line password string\n")
2912{
2913 /* Argument check. */
2914 if (argc == 0) {
2915 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2916 return CMD_WARNING;
2917 }
2918
2919 if (argc == 2) {
2920 if (*argv[0] == '8') {
2921 if (host.password)
2922 talloc_free(host.password);
2923 host.password = NULL;
2924 if (host.password_encrypt)
2925 talloc_free(host.password_encrypt);
2926 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2927 return CMD_SUCCESS;
2928 } else {
2929 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2930 return CMD_WARNING;
2931 }
2932 }
2933
2934 if (!isalnum((int)*argv[0])) {
2935 vty_out(vty,
2936 "Please specify string starting with alphanumeric%s",
2937 VTY_NEWLINE);
2938 return CMD_WARNING;
2939 }
2940
2941 if (host.password)
2942 talloc_free(host.password);
2943 host.password = NULL;
2944
2945#ifdef VTY_CRYPT_PW
2946 if (host.encrypt) {
2947 if (host.password_encrypt)
2948 talloc_free(host.password_encrypt);
2949 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2950 } else
2951#endif
2952 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2953
2954 return CMD_SUCCESS;
2955}
2956
2957ALIAS(config_password, password_text_cmd,
2958 "password LINE",
2959 "Assign the terminal connection password\n"
2960 "The UNENCRYPTED (cleartext) line password\n")
2961
2962/* VTY enable password set. */
2963 DEFUN(config_enable_password, enable_password_cmd,
2964 "enable password (8|) WORD",
2965 "Modify enable password parameters\n"
2966 "Assign the privileged level password\n"
2967 "Specifies a HIDDEN password will follow\n"
2968 "dummy string \n" "The HIDDEN 'enable' password string\n")
2969{
2970 /* Argument check. */
2971 if (argc == 0) {
2972 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2973 return CMD_WARNING;
2974 }
2975
2976 /* Crypt type is specified. */
2977 if (argc == 2) {
2978 if (*argv[0] == '8') {
2979 if (host.enable)
2980 talloc_free(host.enable);
2981 host.enable = NULL;
2982
2983 if (host.enable_encrypt)
2984 talloc_free(host.enable_encrypt);
2985 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2986
2987 return CMD_SUCCESS;
2988 } else {
2989 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2990 return CMD_WARNING;
2991 }
2992 }
2993
2994 if (!isalnum((int)*argv[0])) {
2995 vty_out(vty,
2996 "Please specify string starting with alphanumeric%s",
2997 VTY_NEWLINE);
2998 return CMD_WARNING;
2999 }
3000
3001 if (host.enable)
3002 talloc_free(host.enable);
3003 host.enable = NULL;
3004
3005 /* Plain password input. */
3006#ifdef VTY_CRYPT_PW
3007 if (host.encrypt) {
3008 if (host.enable_encrypt)
3009 talloc_free(host.enable_encrypt);
3010 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3011 } else
3012#endif
3013 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3014
3015 return CMD_SUCCESS;
3016}
3017
3018ALIAS(config_enable_password,
3019 enable_password_text_cmd,
3020 "enable password LINE",
3021 "Modify enable password parameters\n"
3022 "Assign the privileged level password\n"
3023 "The UNENCRYPTED (cleartext) 'enable' password\n")
3024
3025/* VTY enable password delete. */
3026 DEFUN(no_config_enable_password, no_enable_password_cmd,
3027 "no enable password",
3028 NO_STR
3029 "Modify enable password parameters\n"
3030 "Assign the privileged level password\n")
3031{
3032 if (host.enable)
3033 talloc_free(host.enable);
3034 host.enable = NULL;
3035
3036 if (host.enable_encrypt)
3037 talloc_free(host.enable_encrypt);
3038 host.enable_encrypt = NULL;
3039
3040 return CMD_SUCCESS;
3041}
3042
3043#ifdef VTY_CRYPT_PW
3044DEFUN(service_password_encrypt,
3045 service_password_encrypt_cmd,
3046 "service password-encryption",
3047 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3048{
3049 if (host.encrypt)
3050 return CMD_SUCCESS;
3051
3052 host.encrypt = 1;
3053
3054 if (host.password) {
3055 if (host.password_encrypt)
3056 talloc_free(host.password_encrypt);
3057 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
3058 }
3059 if (host.enable) {
3060 if (host.enable_encrypt)
3061 talloc_free(host.enable_encrypt);
3062 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
3063 }
3064
3065 return CMD_SUCCESS;
3066}
3067
3068DEFUN(no_service_password_encrypt,
3069 no_service_password_encrypt_cmd,
3070 "no service password-encryption",
3071 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3072{
3073 if (!host.encrypt)
3074 return CMD_SUCCESS;
3075
3076 host.encrypt = 0;
3077
3078 if (host.password_encrypt)
3079 talloc_free(host.password_encrypt);
3080 host.password_encrypt = NULL;
3081
3082 if (host.enable_encrypt)
3083 talloc_free(host.enable_encrypt);
3084 host.enable_encrypt = NULL;
3085
3086 return CMD_SUCCESS;
3087}
3088#endif
3089
3090DEFUN(config_terminal_length, config_terminal_length_cmd,
3091 "terminal length <0-512>",
3092 "Set terminal line parameters\n"
3093 "Set number of lines on a screen\n"
3094 "Number of lines on screen (0 for no pausing)\n")
3095{
3096 int lines;
3097 char *endptr = NULL;
3098
3099 lines = strtol(argv[0], &endptr, 10);
3100 if (lines < 0 || lines > 512 || *endptr != '\0') {
3101 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3102 return CMD_WARNING;
3103 }
3104 vty->lines = lines;
3105
3106 return CMD_SUCCESS;
3107}
3108
3109DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
3110 "terminal no length",
3111 "Set terminal line parameters\n"
3112 NO_STR "Set number of lines on a screen\n")
3113{
3114 vty->lines = -1;
3115 return CMD_SUCCESS;
3116}
3117
3118DEFUN(service_terminal_length, service_terminal_length_cmd,
3119 "service terminal-length <0-512>",
3120 "Set up miscellaneous service\n"
3121 "System wide terminal length configuration\n"
3122 "Number of lines of VTY (0 means no line control)\n")
3123{
3124 int lines;
3125 char *endptr = NULL;
3126
3127 lines = strtol(argv[0], &endptr, 10);
3128 if (lines < 0 || lines > 512 || *endptr != '\0') {
3129 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3130 return CMD_WARNING;
3131 }
3132 host.lines = lines;
3133
3134 return CMD_SUCCESS;
3135}
3136
3137DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
3138 "no service terminal-length [<0-512>]",
3139 NO_STR
3140 "Set up miscellaneous service\n"
3141 "System wide terminal length configuration\n"
3142 "Number of lines of VTY (0 means no line control)\n")
3143{
3144 host.lines = -1;
3145 return CMD_SUCCESS;
3146}
3147
3148DEFUN_HIDDEN(do_echo,
3149 echo_cmd,
3150 "echo .MESSAGE",
3151 "Echo a message back to the vty\n" "The message to echo\n")
3152{
3153 char *message;
3154
3155 vty_out(vty, "%s%s",
3156 ((message =
3157 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
3158 if (message)
3159 talloc_free(message);
3160 return CMD_SUCCESS;
3161}
3162
3163#if 0
3164DEFUN(config_logmsg,
3165 config_logmsg_cmd,
3166 "logmsg " LOG_LEVELS " .MESSAGE",
3167 "Send a message to enabled logging destinations\n"
3168 LOG_LEVEL_DESC "The message to send\n")
3169{
3170 int level;
3171 char *message;
3172
3173 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3174 return CMD_ERR_NO_MATCH;
3175
3176 zlog(NULL, level,
3177 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3178 if (message)
3179 talloc_free(message);
3180 return CMD_SUCCESS;
3181}
3182
3183DEFUN(show_logging,
3184 show_logging_cmd,
3185 "show logging", SHOW_STR "Show current logging configuration\n")
3186{
3187 struct zlog *zl = zlog_default;
3188
3189 vty_out(vty, "Syslog logging: ");
3190 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3191 vty_out(vty, "disabled");
3192 else
3193 vty_out(vty, "level %s, facility %s, ident %s",
3194 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3195 facility_name(zl->facility), zl->ident);
3196 vty_out(vty, "%s", VTY_NEWLINE);
3197
3198 vty_out(vty, "Stdout logging: ");
3199 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3200 vty_out(vty, "disabled");
3201 else
3202 vty_out(vty, "level %s",
3203 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3204 vty_out(vty, "%s", VTY_NEWLINE);
3205
3206 vty_out(vty, "Monitor logging: ");
3207 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3208 vty_out(vty, "disabled");
3209 else
3210 vty_out(vty, "level %s",
3211 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3212 vty_out(vty, "%s", VTY_NEWLINE);
3213
3214 vty_out(vty, "File logging: ");
3215 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3216 vty_out(vty, "disabled");
3217 else
3218 vty_out(vty, "level %s, filename %s",
3219 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3220 zl->filename);
3221 vty_out(vty, "%s", VTY_NEWLINE);
3222
3223 vty_out(vty, "Protocol name: %s%s",
3224 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3225 vty_out(vty, "Record priority: %s%s",
3226 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3227
3228 return CMD_SUCCESS;
3229}
3230
3231DEFUN(config_log_stdout,
3232 config_log_stdout_cmd,
3233 "log stdout", "Logging control\n" "Set stdout logging level\n")
3234{
3235 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3236 return CMD_SUCCESS;
3237}
3238
3239DEFUN(config_log_stdout_level,
3240 config_log_stdout_level_cmd,
3241 "log stdout " LOG_LEVELS,
3242 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3243{
3244 int level;
3245
3246 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3247 return CMD_ERR_NO_MATCH;
3248 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3249 return CMD_SUCCESS;
3250}
3251
3252DEFUN(no_config_log_stdout,
3253 no_config_log_stdout_cmd,
3254 "no log stdout [LEVEL]",
3255 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3256{
3257 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3258 return CMD_SUCCESS;
3259}
3260
3261DEFUN(config_log_monitor,
3262 config_log_monitor_cmd,
3263 "log monitor",
3264 "Logging control\n" "Set terminal line (monitor) logging level\n")
3265{
3266 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3267 return CMD_SUCCESS;
3268}
3269
3270DEFUN(config_log_monitor_level,
3271 config_log_monitor_level_cmd,
3272 "log monitor " LOG_LEVELS,
3273 "Logging control\n"
3274 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3275{
3276 int level;
3277
3278 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3279 return CMD_ERR_NO_MATCH;
3280 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3281 return CMD_SUCCESS;
3282}
3283
3284DEFUN(no_config_log_monitor,
3285 no_config_log_monitor_cmd,
3286 "no log monitor [LEVEL]",
3287 NO_STR
3288 "Logging control\n"
3289 "Disable terminal line (monitor) logging\n" "Logging level\n")
3290{
3291 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3292 return CMD_SUCCESS;
3293}
3294
3295static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3296{
3297 int ret;
3298 char *p = NULL;
3299 const char *fullpath;
3300
3301 /* Path detection. */
3302 if (!IS_DIRECTORY_SEP(*fname)) {
3303 char cwd[MAXPATHLEN + 1];
3304 cwd[MAXPATHLEN] = '\0';
3305
3306 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3307 zlog_err("config_log_file: Unable to alloc mem!");
3308 return CMD_WARNING;
3309 }
3310
3311 if ((p = _talloc_zero(tall_vcmd_ctx,
3312 strlen(cwd) + strlen(fname) + 2),
3313 "set_log_file")
3314 == NULL) {
3315 zlog_err("config_log_file: Unable to alloc mem!");
3316 return CMD_WARNING;
3317 }
3318 sprintf(p, "%s/%s", cwd, fname);
3319 fullpath = p;
3320 } else
3321 fullpath = fname;
3322
3323 ret = zlog_set_file(NULL, fullpath, loglevel);
3324
3325 if (p)
3326 talloc_free(p);
3327
3328 if (!ret) {
3329 vty_out(vty, "can't open logfile %s\n", fname);
3330 return CMD_WARNING;
3331 }
3332
3333 if (host.logfile)
3334 talloc_free(host.logfile);
3335
3336 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3337
3338 return CMD_SUCCESS;
3339}
3340
3341DEFUN(config_log_file,
3342 config_log_file_cmd,
3343 "log file FILENAME",
3344 "Logging control\n" "Logging to file\n" "Logging filename\n")
3345{
3346 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3347}
3348
3349DEFUN(config_log_file_level,
3350 config_log_file_level_cmd,
3351 "log file FILENAME " LOG_LEVELS,
3352 "Logging control\n"
3353 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3354{
3355 int level;
3356
3357 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3358 return CMD_ERR_NO_MATCH;
3359 return set_log_file(vty, argv[0], level);
3360}
3361
3362DEFUN(no_config_log_file,
3363 no_config_log_file_cmd,
3364 "no log file [FILENAME]",
3365 NO_STR
3366 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3367{
3368 zlog_reset_file(NULL);
3369
3370 if (host.logfile)
3371 talloc_free(host.logfile);
3372
3373 host.logfile = NULL;
3374
3375 return CMD_SUCCESS;
3376}
3377
3378ALIAS(no_config_log_file,
3379 no_config_log_file_level_cmd,
3380 "no log file FILENAME LEVEL",
3381 NO_STR
3382 "Logging control\n"
3383 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3384
3385 DEFUN(config_log_syslog,
3386 config_log_syslog_cmd,
3387 "log syslog", "Logging control\n" "Set syslog logging level\n")
3388{
3389 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3390 return CMD_SUCCESS;
3391}
3392
3393DEFUN(config_log_syslog_level,
3394 config_log_syslog_level_cmd,
3395 "log syslog " LOG_LEVELS,
3396 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3397{
3398 int level;
3399
3400 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3401 return CMD_ERR_NO_MATCH;
3402 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3403 return CMD_SUCCESS;
3404}
3405
3406DEFUN_DEPRECATED(config_log_syslog_facility,
3407 config_log_syslog_facility_cmd,
3408 "log syslog facility " LOG_FACILITIES,
3409 "Logging control\n"
3410 "Logging goes to syslog\n"
3411 "(Deprecated) Facility parameter for syslog messages\n"
3412 LOG_FACILITY_DESC)
3413{
3414 int facility;
3415
3416 if ((facility = facility_match(argv[0])) < 0)
3417 return CMD_ERR_NO_MATCH;
3418
3419 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3420 zlog_default->facility = facility;
3421 return CMD_SUCCESS;
3422}
3423
3424DEFUN(no_config_log_syslog,
3425 no_config_log_syslog_cmd,
3426 "no log syslog [LEVEL]",
3427 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3428{
3429 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3430 return CMD_SUCCESS;
3431}
3432
3433ALIAS(no_config_log_syslog,
3434 no_config_log_syslog_facility_cmd,
3435 "no log syslog facility " LOG_FACILITIES,
3436 NO_STR
3437 "Logging control\n"
3438 "Logging goes to syslog\n"
3439 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3440
3441 DEFUN(config_log_facility,
3442 config_log_facility_cmd,
3443 "log facility " LOG_FACILITIES,
3444 "Logging control\n"
3445 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3446{
3447 int facility;
3448
3449 if ((facility = facility_match(argv[0])) < 0)
3450 return CMD_ERR_NO_MATCH;
3451 zlog_default->facility = facility;
3452 return CMD_SUCCESS;
3453}
3454
3455DEFUN(no_config_log_facility,
3456 no_config_log_facility_cmd,
3457 "no log facility [FACILITY]",
3458 NO_STR
3459 "Logging control\n"
3460 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3461{
3462 zlog_default->facility = LOG_DAEMON;
3463 return CMD_SUCCESS;
3464}
3465
3466DEFUN_DEPRECATED(config_log_trap,
3467 config_log_trap_cmd,
3468 "log trap " LOG_LEVELS,
3469 "Logging control\n"
3470 "(Deprecated) Set logging level and default for all destinations\n"
3471 LOG_LEVEL_DESC)
3472{
3473 int new_level;
3474 int i;
3475
3476 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3477 return CMD_ERR_NO_MATCH;
3478
3479 zlog_default->default_lvl = new_level;
3480 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3481 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3482 zlog_default->maxlvl[i] = new_level;
3483 return CMD_SUCCESS;
3484}
3485
3486DEFUN_DEPRECATED(no_config_log_trap,
3487 no_config_log_trap_cmd,
3488 "no log trap [LEVEL]",
3489 NO_STR
3490 "Logging control\n"
3491 "Permit all logging information\n" "Logging level\n")
3492{
3493 zlog_default->default_lvl = LOG_DEBUG;
3494 return CMD_SUCCESS;
3495}
3496
3497DEFUN(config_log_record_priority,
3498 config_log_record_priority_cmd,
3499 "log record-priority",
3500 "Logging control\n"
3501 "Log the priority of the message within the message\n")
3502{
3503 zlog_default->record_priority = 1;
3504 return CMD_SUCCESS;
3505}
3506
3507DEFUN(no_config_log_record_priority,
3508 no_config_log_record_priority_cmd,
3509 "no log record-priority",
3510 NO_STR
3511 "Logging control\n"
3512 "Do not log the priority of the message within the message\n")
3513{
3514 zlog_default->record_priority = 0;
3515 return CMD_SUCCESS;
3516}
3517#endif
3518
3519DEFUN(banner_motd_file,
3520 banner_motd_file_cmd,
3521 "banner motd file [FILE]",
3522 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3523{
3524 if (host.motdfile)
3525 talloc_free(host.motdfile);
3526 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3527
3528 return CMD_SUCCESS;
3529}
3530
3531DEFUN(banner_motd_default,
3532 banner_motd_default_cmd,
3533 "banner motd default",
3534 "Set banner string\n" "Strings for motd\n" "Default string\n")
3535{
3536 host.motd = default_motd;
3537 return CMD_SUCCESS;
3538}
3539
3540DEFUN(no_banner_motd,
3541 no_banner_motd_cmd,
3542 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3543{
3544 host.motd = NULL;
3545 if (host.motdfile)
3546 talloc_free(host.motdfile);
3547 host.motdfile = NULL;
3548 return CMD_SUCCESS;
3549}
3550
3551/* Set config filename. Called from vty.c */
3552void host_config_set(const char *filename)
3553{
3554 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3555}
3556
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003557void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003558{
3559 install_element(node, &config_help_cmd);
3560 install_element(node, &config_list_cmd);
3561
3562 install_element(node, &config_write_terminal_cmd);
3563 install_element(node, &config_write_file_cmd);
3564 install_element(node, &config_write_memory_cmd);
3565 install_element(node, &config_write_cmd);
3566 install_element(node, &show_running_config_cmd);
3567}
3568
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003569void vty_install_default(int node)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003570{
3571 install_default(node);
3572
3573 install_element(node, &config_exit_cmd);
3574
3575 if (node >= CONFIG_NODE) {
3576 /* It's not a top node. */
3577 install_element(node, &config_end_cmd);
3578 }
3579}
3580
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003581/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003582 * Write the current running config to a given file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003583 * \param[in] vty the vty of the code
3584 * \param[in] filename where to store the file
3585 * \return 0 in case of success.
3586 *
3587 * If the filename already exists create a filename.sav
3588 * version with the current code.
3589 *
3590 */
3591int osmo_vty_write_config_file(const char *filename)
3592{
3593 char *failed_file;
3594 int rc;
3595
3596 rc = write_config_file(filename, &failed_file);
3597 talloc_free(failed_file);
3598 return rc;
3599}
3600
3601/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003602 * Save the current state to the config file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003603 * \return 0 in case of success.
3604 *
3605 * If the filename already exists create a filename.sav
3606 * version with the current code.
3607 *
3608 */
3609int osmo_vty_save_config_file(void)
3610{
3611 char *failed_file;
3612 int rc;
3613
3614 if (host.config == NULL)
3615 return -7;
3616
3617 rc = write_config_file(host.config, &failed_file);
3618 talloc_free(failed_file);
3619 return rc;
3620}
3621
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003622/* Initialize command interface. Install basic nodes and commands. */
3623void cmd_init(int terminal)
3624{
3625 /* Allocate initial top vector of commands. */
3626 cmdvec = vector_init(VECTOR_MIN_SIZE);
3627
3628 /* Default host value settings. */
3629 host.name = NULL;
3630 host.password = NULL;
3631 host.enable = NULL;
3632 host.logfile = NULL;
3633 host.config = NULL;
3634 host.lines = -1;
3635 host.motd = default_motd;
3636 host.motdfile = NULL;
3637
3638 /* Install top nodes. */
3639 install_node(&view_node, NULL);
3640 install_node(&enable_node, NULL);
3641 install_node(&auth_node, NULL);
3642 install_node(&auth_enable_node, NULL);
3643 install_node(&config_node, config_write_host);
3644
3645 /* Each node's basic commands. */
3646 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003647 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003648 if (terminal) {
3649 install_element(VIEW_NODE, &config_list_cmd);
3650 install_element(VIEW_NODE, &config_exit_cmd);
3651 install_element(VIEW_NODE, &config_help_cmd);
3652 install_element(VIEW_NODE, &config_enable_cmd);
3653 install_element(VIEW_NODE, &config_terminal_length_cmd);
3654 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3655 install_element(VIEW_NODE, &echo_cmd);
3656 }
3657
3658 if (terminal) {
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003659 vty_install_default(ENABLE_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003660 install_element(ENABLE_NODE, &config_disable_cmd);
3661 install_element(ENABLE_NODE, &config_terminal_cmd);
3662 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3663 }
3664 install_element (ENABLE_NODE, &show_startup_config_cmd);
3665 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003666 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003667
3668 if (terminal) {
3669 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3670 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3671 install_element(ENABLE_NODE, &echo_cmd);
3672
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003673 vty_install_default(CONFIG_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003674 }
3675
3676 install_element(CONFIG_NODE, &hostname_cmd);
3677 install_element(CONFIG_NODE, &no_hostname_cmd);
3678
3679 if (terminal) {
3680 install_element(CONFIG_NODE, &password_cmd);
3681 install_element(CONFIG_NODE, &password_text_cmd);
3682 install_element(CONFIG_NODE, &enable_password_cmd);
3683 install_element(CONFIG_NODE, &enable_password_text_cmd);
3684 install_element(CONFIG_NODE, &no_enable_password_cmd);
3685
3686#ifdef VTY_CRYPT_PW
3687 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3688 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3689#endif
3690 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3691 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3692 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3693 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3694 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3695
3696 }
3697 srand(time(NULL));
3698}
Harald Welte7acb30c2011-08-17 17:13:48 +02003699
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003700/*! @} */