blob: a65b4de5b4d4a02305503b6a82814bc86bf3b9e2 [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
2334static int indent_cmp(const char *a, const char *b)
2335{
2336 size_t al, bl;
2337 al = len(a);
2338 bl = len(b);
2339 if (al > bl) {
2340 if (bl && strncmp(a, b, bl) != 0)
2341 return EINVAL;
2342 return 1;
2343 }
2344 /* al <= bl */
2345 if (al && strncmp(a, b, al) != 0)
2346 return EINVAL;
2347 return (al < bl)? -1 : 0;
2348}
2349
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002350/* Configration make from file. */
2351int config_from_file(struct vty *vty, FILE * fp)
2352{
2353 int ret;
2354 vector vline;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002355 char *indent;
2356 int cmp;
2357 struct vty_parent_node this_node;
2358 struct vty_parent_node *parent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002359
2360 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002361 indent = NULL;
2362 vline = NULL;
2363 ret = cmd_make_strvec2(vty->buf, &indent, &vline);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002364
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002365 if (ret != CMD_SUCCESS)
2366 goto return_invalid_indent;
2367
2368 /* In case of comment or empty line */
2369 if (vline == NULL) {
2370 if (indent) {
2371 talloc_free(indent);
2372 indent = NULL;
2373 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002374 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002375 }
2376
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002377 /* We have a nonempty line. This might be the first on a deeper indenting level, so let's
2378 * remember this indent if we don't have one yet. */
2379 if (!vty->indent)
2380 vty->indent = talloc_strdup(vty, indent);
2381
2382 cmp = indent_cmp(indent, vty->indent);
2383 if (cmp == EINVAL)
2384 goto return_invalid_indent;
2385
2386 /* Less indent: go up the parent nodes to find matching amount of less indent. When this
2387 * loop exits, we want to have found an exact match, i.e. cmp == 0. */
2388 while (cmp < 0) {
2389 vty_go_parent(vty);
2390 cmp = indent_cmp(indent, vty->indent);
2391 if (cmp == EINVAL)
2392 goto return_invalid_indent;
2393 }
2394
2395 /* More indent without having entered a child node level? Either the parent node's indent
2396 * wasn't hit exactly (e.g. there's a space more than the parent level had further above)
2397 * or the indentation increased even though the vty command didn't enter a child. */
2398 if (cmp > 0)
2399 goto return_invalid_indent;
2400
2401 /* Remember the current node before the command possibly changes it. */
2402 this_node = (struct vty_parent_node){
2403 .node = vty->node,
2404 .priv = vty->priv,
2405 .indent = vty->indent,
2406 };
2407
2408 parent = vty_parent(vty);
2409 ret = cmd_execute_command_strict(vline, vty, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002410 cmd_free_strvec(vline);
2411
2412 if (ret != CMD_SUCCESS && ret != CMD_WARNING
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002413 && ret != CMD_ERR_NOTHING_TODO) {
2414 if (indent) {
2415 talloc_free(indent);
2416 indent = NULL;
2417 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002418 return ret;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002419 }
2420
2421 /* If we have stepped down into a child node, push a parent frame.
2422 * The causality is such: we don't expect every single node entry implementation to push
2423 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2424 * a parent node. Hence if the node changed without the parent node changing, we must
2425 * have stepped into a child node (and now expect a deeper indent). */
2426 if (vty->node != this_node.node && parent == vty_parent(vty)) {
2427 /* Push the parent node. */
2428 parent = talloc_zero(vty, struct vty_parent_node);
2429 *parent = this_node;
2430 llist_add(&parent->entry, &vty->parent_nodes);
2431
2432 /* The current talloc'ed vty->indent string will now be owned by this parent
2433 * struct. Indicate that we don't know what deeper indent characters the user
2434 * will choose. */
2435 vty->indent = NULL;
2436 }
2437
2438 if (indent) {
2439 talloc_free(indent);
2440 indent = NULL;
2441 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002442 }
2443 return CMD_SUCCESS;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002444
2445return_invalid_indent:
2446 if (vline)
2447 cmd_free_strvec(vline);
2448 if (indent) {
2449 talloc_free(indent);
2450 indent = NULL;
2451 }
2452 return CMD_ERR_INVALID_INDENT;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002453}
2454
2455/* Configration from terminal */
2456DEFUN(config_terminal,
2457 config_terminal_cmd,
2458 "configure terminal",
2459 "Configuration from vty interface\n" "Configuration terminal\n")
2460{
2461 if (vty_config_lock(vty))
2462 vty->node = CONFIG_NODE;
2463 else {
2464 vty_out(vty, "VTY configuration is locked by other VTY%s",
2465 VTY_NEWLINE);
2466 return CMD_WARNING;
2467 }
2468 return CMD_SUCCESS;
2469}
2470
2471/* Enable command */
2472DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2473{
2474 /* If enable password is NULL, change to ENABLE_NODE */
2475 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2476 vty->type == VTY_SHELL_SERV)
2477 vty->node = ENABLE_NODE;
2478 else
2479 vty->node = AUTH_ENABLE_NODE;
2480
2481 return CMD_SUCCESS;
2482}
2483
2484/* Disable command */
2485DEFUN(disable,
2486 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2487{
2488 if (vty->node == ENABLE_NODE)
2489 vty->node = VIEW_NODE;
2490 return CMD_SUCCESS;
2491}
2492
2493/* Down vty node level. */
2494gDEFUN(config_exit,
2495 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2496{
2497 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002498 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002499 case VIEW_NODE:
2500 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002501 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002502 break;
2503 case CONFIG_NODE:
2504 vty->node = ENABLE_NODE;
2505 vty_config_unlock(vty);
2506 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002507 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002508 if (vty->node > CONFIG_NODE)
2509 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002510 break;
2511 }
2512 return CMD_SUCCESS;
2513}
2514
2515/* End of configuration. */
2516 gDEFUN(config_end,
2517 config_end_cmd, "end", "End current mode and change to enable mode.")
2518{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002519 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002520 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002521
2522 /* Repeatedly call go_parent until a top node is reached. */
2523 while (vty->node > CONFIG_NODE) {
2524 if (vty->node == last_node) {
2525 /* Ensure termination, this shouldn't happen. */
2526 break;
2527 }
2528 last_node = vty->node;
2529 vty_go_parent(vty);
2530 }
2531
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002532 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002533 if (vty->node > ENABLE_NODE)
2534 vty->node = ENABLE_NODE;
2535 vty->index = NULL;
2536 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002537 }
2538 return CMD_SUCCESS;
2539}
2540
2541/* Show version. */
2542DEFUN(show_version,
2543 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2544{
Harald Welte237f6242010-05-25 23:00:45 +02002545 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2546 host.app_info->version,
2547 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2548 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002549
2550 return CMD_SUCCESS;
2551}
2552
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002553DEFUN(show_online_help,
2554 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2555{
2556 vty_dump_nodes(vty);
2557 return CMD_SUCCESS;
2558}
2559
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002560/* Help display function for all node. */
2561gDEFUN(config_help,
2562 config_help_cmd, "help", "Description of the interactive help system\n")
2563{
2564 vty_out(vty,
2565 "This VTY provides advanced help features. When you need help,%s\
2566anytime at the command line please press '?'.%s\
2567%s\
2568If nothing matches, the help list will be empty and you must backup%s\
2569 until entering a '?' shows the available options.%s\
2570Two styles of help are provided:%s\
25711. Full help is available when you are ready to enter a%s\
2572command argument (e.g. 'show ?') and describes each possible%s\
2573argument.%s\
25742. Partial help is provided when an abbreviated argument is entered%s\
2575 and you want to know what arguments match the input%s\
2576 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2577 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2578 return CMD_SUCCESS;
2579}
2580
2581/* Help display function for all node. */
2582gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2583{
2584 unsigned int i;
2585 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2586 struct cmd_element *cmd;
2587
2588 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2589 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2590 && !(cmd->attr == CMD_ATTR_DEPRECATED
2591 || cmd->attr == CMD_ATTR_HIDDEN))
2592 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2593 return CMD_SUCCESS;
2594}
2595
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002596static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002597{
2598 unsigned int i;
2599 int fd;
2600 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002601 char *config_file_tmp = NULL;
2602 char *config_file_sav = NULL;
2603 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002604 struct stat st;
2605
2606 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002607
2608 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002609 config_file_sav =
2610 _talloc_zero(tall_vty_cmd_ctx,
2611 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2612 "config_file_sav");
2613 strcpy(config_file_sav, config_file);
2614 strcat(config_file_sav, CONF_BACKUP_EXT);
2615
2616 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2617 "config_file_tmp");
2618 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2619
2620 /* Open file to configuration write. */
2621 fd = mkstemp(config_file_tmp);
2622 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002623 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002624 talloc_free(config_file_tmp);
2625 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002626 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002627 }
2628
2629 /* Make vty for configuration file. */
2630 file_vty = vty_new();
2631 file_vty->fd = fd;
2632 file_vty->type = VTY_FILE;
2633
2634 /* Config file header print. */
2635 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002636 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002637 //vty_time_print (file_vty, 1);
2638 vty_out(file_vty, "!\n");
2639
2640 for (i = 0; i < vector_active(cmdvec); i++)
2641 if ((node = vector_slot(cmdvec, i)) && node->func) {
2642 if ((*node->func) (file_vty))
2643 vty_out(file_vty, "!\n");
2644 }
2645 vty_close(file_vty);
2646
2647 if (unlink(config_file_sav) != 0)
2648 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002649 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002650 talloc_free(config_file_sav);
2651 talloc_free(config_file_tmp);
2652 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002653 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002654 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002655
2656 /* Only link the .sav file if the original file exists */
2657 if (stat(config_file, &st) == 0) {
2658 if (link(config_file, config_file_sav) != 0) {
2659 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2660 talloc_free(config_file_sav);
2661 talloc_free(config_file_tmp);
2662 unlink(config_file_tmp);
2663 return -3;
2664 }
2665 sync();
2666 if (unlink(config_file) != 0) {
2667 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2668 talloc_free(config_file_sav);
2669 talloc_free(config_file_tmp);
2670 unlink(config_file_tmp);
2671 return -4;
2672 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002673 }
2674 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002675 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002676 talloc_free(config_file_sav);
2677 talloc_free(config_file_tmp);
2678 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002679 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002680 }
2681 unlink(config_file_tmp);
2682 sync();
2683
2684 talloc_free(config_file_sav);
2685 talloc_free(config_file_tmp);
2686
2687 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002688 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2689 return -6;
2690 }
2691
2692 return 0;
2693}
2694
2695
2696/* Write current configuration into file. */
2697DEFUN(config_write_file,
2698 config_write_file_cmd,
2699 "write file",
2700 "Write running configuration to memory, network, or terminal\n"
2701 "Write to configuration file\n")
2702{
2703 char *failed_file;
2704 int rc;
2705
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002706 if (host.app_info->config_is_consistent) {
2707 rc = host.app_info->config_is_consistent(vty);
2708 if (!rc) {
2709 vty_out(vty, "Configuration is not consistent%s",
2710 VTY_NEWLINE);
2711 return CMD_WARNING;
2712 }
2713 }
2714
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002715 if (host.config == NULL) {
2716 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2717 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002718 return CMD_WARNING;
2719 }
2720
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002721 rc = write_config_file(host.config, &failed_file);
2722 switch (rc) {
2723 case -1:
2724 vty_out(vty, "Can't open configuration file %s.%s",
2725 failed_file, VTY_NEWLINE);
2726 rc = CMD_WARNING;
2727 break;
2728 case -2:
2729 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2730 failed_file, VTY_NEWLINE);
2731 rc = CMD_WARNING;
2732 break;
2733 case -3:
2734 vty_out(vty, "Can't backup old configuration file %s.%s",
2735 failed_file, VTY_NEWLINE);
2736 rc = CMD_WARNING;
2737 break;
2738 case -4:
2739 vty_out(vty, "Can't unlink configuration file %s.%s",
2740 failed_file, VTY_NEWLINE);
2741 rc = CMD_WARNING;
2742 break;
2743 case -5:
2744 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2745 VTY_NEWLINE);
2746 rc = CMD_WARNING;
2747 break;
2748 case -6:
2749 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2750 failed_file, strerror(errno), errno, VTY_NEWLINE);
2751 rc = CMD_WARNING;
2752 break;
2753 default:
2754 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2755 rc = CMD_SUCCESS;
2756 break;
2757 }
2758
2759 talloc_free(failed_file);
2760 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002761}
2762
2763ALIAS(config_write_file,
2764 config_write_cmd,
2765 "write", "Write running configuration to memory, network, or terminal\n")
2766
2767 ALIAS(config_write_file,
2768 config_write_memory_cmd,
2769 "write memory",
2770 "Write running configuration to memory, network, or terminal\n"
2771 "Write configuration to the file (same as write file)\n")
2772
2773 ALIAS(config_write_file,
2774 copy_runningconfig_startupconfig_cmd,
2775 "copy running-config startup-config",
2776 "Copy configuration\n"
2777 "Copy running config to... \n"
2778 "Copy running config to startup config (same as write file)\n")
2779
2780/* Write current configuration into the terminal. */
2781 DEFUN(config_write_terminal,
2782 config_write_terminal_cmd,
2783 "write terminal",
2784 "Write running configuration to memory, network, or terminal\n"
2785 "Write to terminal\n")
2786{
2787 unsigned int i;
2788 struct cmd_node *node;
2789
2790 if (vty->type == VTY_SHELL_SERV) {
2791 for (i = 0; i < vector_active(cmdvec); i++)
2792 if ((node = vector_slot(cmdvec, i)) && node->func
2793 && node->vtysh) {
2794 if ((*node->func) (vty))
2795 vty_out(vty, "!%s", VTY_NEWLINE);
2796 }
2797 } else {
2798 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2799 VTY_NEWLINE);
2800 vty_out(vty, "!%s", VTY_NEWLINE);
2801
2802 for (i = 0; i < vector_active(cmdvec); i++)
2803 if ((node = vector_slot(cmdvec, i)) && node->func) {
2804 if ((*node->func) (vty))
2805 vty_out(vty, "!%s", VTY_NEWLINE);
2806 }
2807 vty_out(vty, "end%s", VTY_NEWLINE);
2808 }
2809 return CMD_SUCCESS;
2810}
2811
2812/* Write current configuration into the terminal. */
2813ALIAS(config_write_terminal,
2814 show_running_config_cmd,
2815 "show running-config", SHOW_STR "running configuration\n")
2816
2817/* Write startup configuration into the terminal. */
2818 DEFUN(show_startup_config,
2819 show_startup_config_cmd,
2820 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2821{
2822 char buf[BUFSIZ];
2823 FILE *confp;
2824
2825 confp = fopen(host.config, "r");
2826 if (confp == NULL) {
2827 vty_out(vty, "Can't open configuration file [%s]%s",
2828 host.config, VTY_NEWLINE);
2829 return CMD_WARNING;
2830 }
2831
2832 while (fgets(buf, BUFSIZ, confp)) {
2833 char *cp = buf;
2834
2835 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2836 cp++;
2837 *cp = '\0';
2838
2839 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2840 }
2841
2842 fclose(confp);
2843
2844 return CMD_SUCCESS;
2845}
2846
2847/* Hostname configuration */
2848DEFUN(config_hostname,
2849 hostname_cmd,
2850 "hostname WORD",
2851 "Set system's network name\n" "This system's network name\n")
2852{
2853 if (!isalpha((int)*argv[0])) {
2854 vty_out(vty, "Please specify string starting with alphabet%s",
2855 VTY_NEWLINE);
2856 return CMD_WARNING;
2857 }
2858
2859 if (host.name)
2860 talloc_free(host.name);
2861
2862 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2863 return CMD_SUCCESS;
2864}
2865
2866DEFUN(config_no_hostname,
2867 no_hostname_cmd,
2868 "no hostname [HOSTNAME]",
2869 NO_STR "Reset system's network name\n" "Host name of this router\n")
2870{
2871 if (host.name)
2872 talloc_free(host.name);
2873 host.name = NULL;
2874 return CMD_SUCCESS;
2875}
2876
2877/* VTY interface password set. */
2878DEFUN(config_password, password_cmd,
2879 "password (8|) WORD",
2880 "Assign the terminal connection password\n"
2881 "Specifies a HIDDEN password will follow\n"
2882 "dummy string \n" "The HIDDEN line password string\n")
2883{
2884 /* Argument check. */
2885 if (argc == 0) {
2886 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2887 return CMD_WARNING;
2888 }
2889
2890 if (argc == 2) {
2891 if (*argv[0] == '8') {
2892 if (host.password)
2893 talloc_free(host.password);
2894 host.password = NULL;
2895 if (host.password_encrypt)
2896 talloc_free(host.password_encrypt);
2897 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2898 return CMD_SUCCESS;
2899 } else {
2900 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2901 return CMD_WARNING;
2902 }
2903 }
2904
2905 if (!isalnum((int)*argv[0])) {
2906 vty_out(vty,
2907 "Please specify string starting with alphanumeric%s",
2908 VTY_NEWLINE);
2909 return CMD_WARNING;
2910 }
2911
2912 if (host.password)
2913 talloc_free(host.password);
2914 host.password = NULL;
2915
2916#ifdef VTY_CRYPT_PW
2917 if (host.encrypt) {
2918 if (host.password_encrypt)
2919 talloc_free(host.password_encrypt);
2920 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2921 } else
2922#endif
2923 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2924
2925 return CMD_SUCCESS;
2926}
2927
2928ALIAS(config_password, password_text_cmd,
2929 "password LINE",
2930 "Assign the terminal connection password\n"
2931 "The UNENCRYPTED (cleartext) line password\n")
2932
2933/* VTY enable password set. */
2934 DEFUN(config_enable_password, enable_password_cmd,
2935 "enable password (8|) WORD",
2936 "Modify enable password parameters\n"
2937 "Assign the privileged level password\n"
2938 "Specifies a HIDDEN password will follow\n"
2939 "dummy string \n" "The HIDDEN 'enable' password string\n")
2940{
2941 /* Argument check. */
2942 if (argc == 0) {
2943 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2944 return CMD_WARNING;
2945 }
2946
2947 /* Crypt type is specified. */
2948 if (argc == 2) {
2949 if (*argv[0] == '8') {
2950 if (host.enable)
2951 talloc_free(host.enable);
2952 host.enable = NULL;
2953
2954 if (host.enable_encrypt)
2955 talloc_free(host.enable_encrypt);
2956 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2957
2958 return CMD_SUCCESS;
2959 } else {
2960 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2961 return CMD_WARNING;
2962 }
2963 }
2964
2965 if (!isalnum((int)*argv[0])) {
2966 vty_out(vty,
2967 "Please specify string starting with alphanumeric%s",
2968 VTY_NEWLINE);
2969 return CMD_WARNING;
2970 }
2971
2972 if (host.enable)
2973 talloc_free(host.enable);
2974 host.enable = NULL;
2975
2976 /* Plain password input. */
2977#ifdef VTY_CRYPT_PW
2978 if (host.encrypt) {
2979 if (host.enable_encrypt)
2980 talloc_free(host.enable_encrypt);
2981 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2982 } else
2983#endif
2984 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2985
2986 return CMD_SUCCESS;
2987}
2988
2989ALIAS(config_enable_password,
2990 enable_password_text_cmd,
2991 "enable password LINE",
2992 "Modify enable password parameters\n"
2993 "Assign the privileged level password\n"
2994 "The UNENCRYPTED (cleartext) 'enable' password\n")
2995
2996/* VTY enable password delete. */
2997 DEFUN(no_config_enable_password, no_enable_password_cmd,
2998 "no enable password",
2999 NO_STR
3000 "Modify enable password parameters\n"
3001 "Assign the privileged level password\n")
3002{
3003 if (host.enable)
3004 talloc_free(host.enable);
3005 host.enable = NULL;
3006
3007 if (host.enable_encrypt)
3008 talloc_free(host.enable_encrypt);
3009 host.enable_encrypt = NULL;
3010
3011 return CMD_SUCCESS;
3012}
3013
3014#ifdef VTY_CRYPT_PW
3015DEFUN(service_password_encrypt,
3016 service_password_encrypt_cmd,
3017 "service password-encryption",
3018 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3019{
3020 if (host.encrypt)
3021 return CMD_SUCCESS;
3022
3023 host.encrypt = 1;
3024
3025 if (host.password) {
3026 if (host.password_encrypt)
3027 talloc_free(host.password_encrypt);
3028 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
3029 }
3030 if (host.enable) {
3031 if (host.enable_encrypt)
3032 talloc_free(host.enable_encrypt);
3033 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
3034 }
3035
3036 return CMD_SUCCESS;
3037}
3038
3039DEFUN(no_service_password_encrypt,
3040 no_service_password_encrypt_cmd,
3041 "no service password-encryption",
3042 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3043{
3044 if (!host.encrypt)
3045 return CMD_SUCCESS;
3046
3047 host.encrypt = 0;
3048
3049 if (host.password_encrypt)
3050 talloc_free(host.password_encrypt);
3051 host.password_encrypt = NULL;
3052
3053 if (host.enable_encrypt)
3054 talloc_free(host.enable_encrypt);
3055 host.enable_encrypt = NULL;
3056
3057 return CMD_SUCCESS;
3058}
3059#endif
3060
3061DEFUN(config_terminal_length, config_terminal_length_cmd,
3062 "terminal length <0-512>",
3063 "Set terminal line parameters\n"
3064 "Set number of lines on a screen\n"
3065 "Number of lines on screen (0 for no pausing)\n")
3066{
3067 int lines;
3068 char *endptr = NULL;
3069
3070 lines = strtol(argv[0], &endptr, 10);
3071 if (lines < 0 || lines > 512 || *endptr != '\0') {
3072 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3073 return CMD_WARNING;
3074 }
3075 vty->lines = lines;
3076
3077 return CMD_SUCCESS;
3078}
3079
3080DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
3081 "terminal no length",
3082 "Set terminal line parameters\n"
3083 NO_STR "Set number of lines on a screen\n")
3084{
3085 vty->lines = -1;
3086 return CMD_SUCCESS;
3087}
3088
3089DEFUN(service_terminal_length, service_terminal_length_cmd,
3090 "service terminal-length <0-512>",
3091 "Set up miscellaneous service\n"
3092 "System wide terminal length configuration\n"
3093 "Number of lines of VTY (0 means no line control)\n")
3094{
3095 int lines;
3096 char *endptr = NULL;
3097
3098 lines = strtol(argv[0], &endptr, 10);
3099 if (lines < 0 || lines > 512 || *endptr != '\0') {
3100 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3101 return CMD_WARNING;
3102 }
3103 host.lines = lines;
3104
3105 return CMD_SUCCESS;
3106}
3107
3108DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
3109 "no service terminal-length [<0-512>]",
3110 NO_STR
3111 "Set up miscellaneous service\n"
3112 "System wide terminal length configuration\n"
3113 "Number of lines of VTY (0 means no line control)\n")
3114{
3115 host.lines = -1;
3116 return CMD_SUCCESS;
3117}
3118
3119DEFUN_HIDDEN(do_echo,
3120 echo_cmd,
3121 "echo .MESSAGE",
3122 "Echo a message back to the vty\n" "The message to echo\n")
3123{
3124 char *message;
3125
3126 vty_out(vty, "%s%s",
3127 ((message =
3128 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
3129 if (message)
3130 talloc_free(message);
3131 return CMD_SUCCESS;
3132}
3133
3134#if 0
3135DEFUN(config_logmsg,
3136 config_logmsg_cmd,
3137 "logmsg " LOG_LEVELS " .MESSAGE",
3138 "Send a message to enabled logging destinations\n"
3139 LOG_LEVEL_DESC "The message to send\n")
3140{
3141 int level;
3142 char *message;
3143
3144 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3145 return CMD_ERR_NO_MATCH;
3146
3147 zlog(NULL, level,
3148 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3149 if (message)
3150 talloc_free(message);
3151 return CMD_SUCCESS;
3152}
3153
3154DEFUN(show_logging,
3155 show_logging_cmd,
3156 "show logging", SHOW_STR "Show current logging configuration\n")
3157{
3158 struct zlog *zl = zlog_default;
3159
3160 vty_out(vty, "Syslog logging: ");
3161 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3162 vty_out(vty, "disabled");
3163 else
3164 vty_out(vty, "level %s, facility %s, ident %s",
3165 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3166 facility_name(zl->facility), zl->ident);
3167 vty_out(vty, "%s", VTY_NEWLINE);
3168
3169 vty_out(vty, "Stdout logging: ");
3170 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3171 vty_out(vty, "disabled");
3172 else
3173 vty_out(vty, "level %s",
3174 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3175 vty_out(vty, "%s", VTY_NEWLINE);
3176
3177 vty_out(vty, "Monitor logging: ");
3178 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3179 vty_out(vty, "disabled");
3180 else
3181 vty_out(vty, "level %s",
3182 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3183 vty_out(vty, "%s", VTY_NEWLINE);
3184
3185 vty_out(vty, "File logging: ");
3186 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3187 vty_out(vty, "disabled");
3188 else
3189 vty_out(vty, "level %s, filename %s",
3190 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3191 zl->filename);
3192 vty_out(vty, "%s", VTY_NEWLINE);
3193
3194 vty_out(vty, "Protocol name: %s%s",
3195 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3196 vty_out(vty, "Record priority: %s%s",
3197 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3198
3199 return CMD_SUCCESS;
3200}
3201
3202DEFUN(config_log_stdout,
3203 config_log_stdout_cmd,
3204 "log stdout", "Logging control\n" "Set stdout logging level\n")
3205{
3206 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3207 return CMD_SUCCESS;
3208}
3209
3210DEFUN(config_log_stdout_level,
3211 config_log_stdout_level_cmd,
3212 "log stdout " LOG_LEVELS,
3213 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3214{
3215 int level;
3216
3217 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3218 return CMD_ERR_NO_MATCH;
3219 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3220 return CMD_SUCCESS;
3221}
3222
3223DEFUN(no_config_log_stdout,
3224 no_config_log_stdout_cmd,
3225 "no log stdout [LEVEL]",
3226 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3227{
3228 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3229 return CMD_SUCCESS;
3230}
3231
3232DEFUN(config_log_monitor,
3233 config_log_monitor_cmd,
3234 "log monitor",
3235 "Logging control\n" "Set terminal line (monitor) logging level\n")
3236{
3237 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3238 return CMD_SUCCESS;
3239}
3240
3241DEFUN(config_log_monitor_level,
3242 config_log_monitor_level_cmd,
3243 "log monitor " LOG_LEVELS,
3244 "Logging control\n"
3245 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3246{
3247 int level;
3248
3249 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3250 return CMD_ERR_NO_MATCH;
3251 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3252 return CMD_SUCCESS;
3253}
3254
3255DEFUN(no_config_log_monitor,
3256 no_config_log_monitor_cmd,
3257 "no log monitor [LEVEL]",
3258 NO_STR
3259 "Logging control\n"
3260 "Disable terminal line (monitor) logging\n" "Logging level\n")
3261{
3262 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3263 return CMD_SUCCESS;
3264}
3265
3266static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3267{
3268 int ret;
3269 char *p = NULL;
3270 const char *fullpath;
3271
3272 /* Path detection. */
3273 if (!IS_DIRECTORY_SEP(*fname)) {
3274 char cwd[MAXPATHLEN + 1];
3275 cwd[MAXPATHLEN] = '\0';
3276
3277 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3278 zlog_err("config_log_file: Unable to alloc mem!");
3279 return CMD_WARNING;
3280 }
3281
3282 if ((p = _talloc_zero(tall_vcmd_ctx,
3283 strlen(cwd) + strlen(fname) + 2),
3284 "set_log_file")
3285 == NULL) {
3286 zlog_err("config_log_file: Unable to alloc mem!");
3287 return CMD_WARNING;
3288 }
3289 sprintf(p, "%s/%s", cwd, fname);
3290 fullpath = p;
3291 } else
3292 fullpath = fname;
3293
3294 ret = zlog_set_file(NULL, fullpath, loglevel);
3295
3296 if (p)
3297 talloc_free(p);
3298
3299 if (!ret) {
3300 vty_out(vty, "can't open logfile %s\n", fname);
3301 return CMD_WARNING;
3302 }
3303
3304 if (host.logfile)
3305 talloc_free(host.logfile);
3306
3307 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3308
3309 return CMD_SUCCESS;
3310}
3311
3312DEFUN(config_log_file,
3313 config_log_file_cmd,
3314 "log file FILENAME",
3315 "Logging control\n" "Logging to file\n" "Logging filename\n")
3316{
3317 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3318}
3319
3320DEFUN(config_log_file_level,
3321 config_log_file_level_cmd,
3322 "log file FILENAME " LOG_LEVELS,
3323 "Logging control\n"
3324 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3325{
3326 int level;
3327
3328 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3329 return CMD_ERR_NO_MATCH;
3330 return set_log_file(vty, argv[0], level);
3331}
3332
3333DEFUN(no_config_log_file,
3334 no_config_log_file_cmd,
3335 "no log file [FILENAME]",
3336 NO_STR
3337 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3338{
3339 zlog_reset_file(NULL);
3340
3341 if (host.logfile)
3342 talloc_free(host.logfile);
3343
3344 host.logfile = NULL;
3345
3346 return CMD_SUCCESS;
3347}
3348
3349ALIAS(no_config_log_file,
3350 no_config_log_file_level_cmd,
3351 "no log file FILENAME LEVEL",
3352 NO_STR
3353 "Logging control\n"
3354 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3355
3356 DEFUN(config_log_syslog,
3357 config_log_syslog_cmd,
3358 "log syslog", "Logging control\n" "Set syslog logging level\n")
3359{
3360 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3361 return CMD_SUCCESS;
3362}
3363
3364DEFUN(config_log_syslog_level,
3365 config_log_syslog_level_cmd,
3366 "log syslog " LOG_LEVELS,
3367 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3368{
3369 int level;
3370
3371 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3372 return CMD_ERR_NO_MATCH;
3373 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3374 return CMD_SUCCESS;
3375}
3376
3377DEFUN_DEPRECATED(config_log_syslog_facility,
3378 config_log_syslog_facility_cmd,
3379 "log syslog facility " LOG_FACILITIES,
3380 "Logging control\n"
3381 "Logging goes to syslog\n"
3382 "(Deprecated) Facility parameter for syslog messages\n"
3383 LOG_FACILITY_DESC)
3384{
3385 int facility;
3386
3387 if ((facility = facility_match(argv[0])) < 0)
3388 return CMD_ERR_NO_MATCH;
3389
3390 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3391 zlog_default->facility = facility;
3392 return CMD_SUCCESS;
3393}
3394
3395DEFUN(no_config_log_syslog,
3396 no_config_log_syslog_cmd,
3397 "no log syslog [LEVEL]",
3398 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3399{
3400 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3401 return CMD_SUCCESS;
3402}
3403
3404ALIAS(no_config_log_syslog,
3405 no_config_log_syslog_facility_cmd,
3406 "no log syslog facility " LOG_FACILITIES,
3407 NO_STR
3408 "Logging control\n"
3409 "Logging goes to syslog\n"
3410 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3411
3412 DEFUN(config_log_facility,
3413 config_log_facility_cmd,
3414 "log facility " LOG_FACILITIES,
3415 "Logging control\n"
3416 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3417{
3418 int facility;
3419
3420 if ((facility = facility_match(argv[0])) < 0)
3421 return CMD_ERR_NO_MATCH;
3422 zlog_default->facility = facility;
3423 return CMD_SUCCESS;
3424}
3425
3426DEFUN(no_config_log_facility,
3427 no_config_log_facility_cmd,
3428 "no log facility [FACILITY]",
3429 NO_STR
3430 "Logging control\n"
3431 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3432{
3433 zlog_default->facility = LOG_DAEMON;
3434 return CMD_SUCCESS;
3435}
3436
3437DEFUN_DEPRECATED(config_log_trap,
3438 config_log_trap_cmd,
3439 "log trap " LOG_LEVELS,
3440 "Logging control\n"
3441 "(Deprecated) Set logging level and default for all destinations\n"
3442 LOG_LEVEL_DESC)
3443{
3444 int new_level;
3445 int i;
3446
3447 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3448 return CMD_ERR_NO_MATCH;
3449
3450 zlog_default->default_lvl = new_level;
3451 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3452 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3453 zlog_default->maxlvl[i] = new_level;
3454 return CMD_SUCCESS;
3455}
3456
3457DEFUN_DEPRECATED(no_config_log_trap,
3458 no_config_log_trap_cmd,
3459 "no log trap [LEVEL]",
3460 NO_STR
3461 "Logging control\n"
3462 "Permit all logging information\n" "Logging level\n")
3463{
3464 zlog_default->default_lvl = LOG_DEBUG;
3465 return CMD_SUCCESS;
3466}
3467
3468DEFUN(config_log_record_priority,
3469 config_log_record_priority_cmd,
3470 "log record-priority",
3471 "Logging control\n"
3472 "Log the priority of the message within the message\n")
3473{
3474 zlog_default->record_priority = 1;
3475 return CMD_SUCCESS;
3476}
3477
3478DEFUN(no_config_log_record_priority,
3479 no_config_log_record_priority_cmd,
3480 "no log record-priority",
3481 NO_STR
3482 "Logging control\n"
3483 "Do not log the priority of the message within the message\n")
3484{
3485 zlog_default->record_priority = 0;
3486 return CMD_SUCCESS;
3487}
3488#endif
3489
3490DEFUN(banner_motd_file,
3491 banner_motd_file_cmd,
3492 "banner motd file [FILE]",
3493 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3494{
3495 if (host.motdfile)
3496 talloc_free(host.motdfile);
3497 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3498
3499 return CMD_SUCCESS;
3500}
3501
3502DEFUN(banner_motd_default,
3503 banner_motd_default_cmd,
3504 "banner motd default",
3505 "Set banner string\n" "Strings for motd\n" "Default string\n")
3506{
3507 host.motd = default_motd;
3508 return CMD_SUCCESS;
3509}
3510
3511DEFUN(no_banner_motd,
3512 no_banner_motd_cmd,
3513 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3514{
3515 host.motd = NULL;
3516 if (host.motdfile)
3517 talloc_free(host.motdfile);
3518 host.motdfile = NULL;
3519 return CMD_SUCCESS;
3520}
3521
3522/* Set config filename. Called from vty.c */
3523void host_config_set(const char *filename)
3524{
3525 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3526}
3527
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003528void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003529{
3530 install_element(node, &config_help_cmd);
3531 install_element(node, &config_list_cmd);
3532
3533 install_element(node, &config_write_terminal_cmd);
3534 install_element(node, &config_write_file_cmd);
3535 install_element(node, &config_write_memory_cmd);
3536 install_element(node, &config_write_cmd);
3537 install_element(node, &show_running_config_cmd);
3538}
3539
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003540void vty_install_default(int node)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003541{
3542 install_default(node);
3543
3544 install_element(node, &config_exit_cmd);
3545
3546 if (node >= CONFIG_NODE) {
3547 /* It's not a top node. */
3548 install_element(node, &config_end_cmd);
3549 }
3550}
3551
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003552/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003553 * Write the current running config to a given file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003554 * \param[in] vty the vty of the code
3555 * \param[in] filename where to store the file
3556 * \return 0 in case of success.
3557 *
3558 * If the filename already exists create a filename.sav
3559 * version with the current code.
3560 *
3561 */
3562int osmo_vty_write_config_file(const char *filename)
3563{
3564 char *failed_file;
3565 int rc;
3566
3567 rc = write_config_file(filename, &failed_file);
3568 talloc_free(failed_file);
3569 return rc;
3570}
3571
3572/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003573 * Save the current state to the config file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003574 * \return 0 in case of success.
3575 *
3576 * If the filename already exists create a filename.sav
3577 * version with the current code.
3578 *
3579 */
3580int osmo_vty_save_config_file(void)
3581{
3582 char *failed_file;
3583 int rc;
3584
3585 if (host.config == NULL)
3586 return -7;
3587
3588 rc = write_config_file(host.config, &failed_file);
3589 talloc_free(failed_file);
3590 return rc;
3591}
3592
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003593/* Initialize command interface. Install basic nodes and commands. */
3594void cmd_init(int terminal)
3595{
3596 /* Allocate initial top vector of commands. */
3597 cmdvec = vector_init(VECTOR_MIN_SIZE);
3598
3599 /* Default host value settings. */
3600 host.name = NULL;
3601 host.password = NULL;
3602 host.enable = NULL;
3603 host.logfile = NULL;
3604 host.config = NULL;
3605 host.lines = -1;
3606 host.motd = default_motd;
3607 host.motdfile = NULL;
3608
3609 /* Install top nodes. */
3610 install_node(&view_node, NULL);
3611 install_node(&enable_node, NULL);
3612 install_node(&auth_node, NULL);
3613 install_node(&auth_enable_node, NULL);
3614 install_node(&config_node, config_write_host);
3615
3616 /* Each node's basic commands. */
3617 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003618 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003619 if (terminal) {
3620 install_element(VIEW_NODE, &config_list_cmd);
3621 install_element(VIEW_NODE, &config_exit_cmd);
3622 install_element(VIEW_NODE, &config_help_cmd);
3623 install_element(VIEW_NODE, &config_enable_cmd);
3624 install_element(VIEW_NODE, &config_terminal_length_cmd);
3625 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3626 install_element(VIEW_NODE, &echo_cmd);
3627 }
3628
3629 if (terminal) {
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003630 vty_install_default(ENABLE_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003631 install_element(ENABLE_NODE, &config_disable_cmd);
3632 install_element(ENABLE_NODE, &config_terminal_cmd);
3633 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3634 }
3635 install_element (ENABLE_NODE, &show_startup_config_cmd);
3636 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003637 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003638
3639 if (terminal) {
3640 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3641 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3642 install_element(ENABLE_NODE, &echo_cmd);
3643
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003644 vty_install_default(CONFIG_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003645 }
3646
3647 install_element(CONFIG_NODE, &hostname_cmd);
3648 install_element(CONFIG_NODE, &no_hostname_cmd);
3649
3650 if (terminal) {
3651 install_element(CONFIG_NODE, &password_cmd);
3652 install_element(CONFIG_NODE, &password_text_cmd);
3653 install_element(CONFIG_NODE, &enable_password_cmd);
3654 install_element(CONFIG_NODE, &enable_password_text_cmd);
3655 install_element(CONFIG_NODE, &no_enable_password_cmd);
3656
3657#ifdef VTY_CRYPT_PW
3658 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3659 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3660#endif
3661 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3662 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3663 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3664 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3665 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3666
3667 }
3668 srand(time(NULL));
3669}
Harald Welte7acb30c2011-08-17 17:13:48 +02003670
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003671/*! @} */