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