blob: 1f1dddbe4fbe3ba1d153188fb8c513fac8fcd512 [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 Welte7acb30c2011-08-17 17:13:48 +020045 * @{
46 */
47/*! \file command.c */
48
Harald Welte3fb0b6f2010-05-19 19:02:52 +020049#define CONFIGFILE_MASK 022
50
51void *tall_vty_cmd_ctx;
52
53/* Command vector which includes some level of command lists. Normally
54 each daemon maintains each own cmdvec. */
55vector cmdvec;
56
57/* Host information structure. */
58struct host host;
59
60/* Standard command node structures. */
61struct cmd_node auth_node = {
62 AUTH_NODE,
63 "Password: ",
64};
65
66struct cmd_node view_node = {
67 VIEW_NODE,
68 "%s> ",
69};
70
71struct cmd_node auth_enable_node = {
72 AUTH_ENABLE_NODE,
73 "Password: ",
74};
75
76struct cmd_node enable_node = {
77 ENABLE_NODE,
78 "%s# ",
79};
80
81struct cmd_node config_node = {
82 CONFIG_NODE,
83 "%s(config)# ",
84 1
85};
86
87/* Default motd string. */
88const char *default_motd = "";
89
Harald Welte7acb30c2011-08-17 17:13:48 +020090/*! \brief print the version (and optionally copyright) information
91 *
92 * This is called from main when a daemon is invoked with -v or --version. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093void print_version(int print_copyright)
94{
Harald Welte237f6242010-05-25 23:00:45 +020095 printf("%s version %s\n", host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020096 if (print_copyright)
Harald Welte237f6242010-05-25 23:00:45 +020097 printf("\n%s\n", host.app_info->copyright);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020098}
99
100/* Utility function to concatenate argv argument into a single string
101 with inserting ' ' character between each argument. */
102char *argv_concat(const char **argv, int argc, int shift)
103{
104 int i;
105 size_t len;
106 char *str;
107 char *p;
108
109 len = 0;
110 for (i = shift; i < argc; i++)
111 len += strlen(argv[i]) + 1;
112 if (!len)
113 return NULL;
114 p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat");
115 for (i = shift; i < argc; i++) {
116 size_t arglen;
117 memcpy(p, argv[i], (arglen = strlen(argv[i])));
118 p += arglen;
119 *p++ = ' ';
120 }
121 *(p - 1) = '\0';
122 return str;
123}
124
Harald Welte7acb30c2011-08-17 17:13:48 +0200125/*! \brief Install top node of command vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126void install_node(struct cmd_node *node, int (*func) (struct vty *))
127{
128 vector_set_index(cmdvec, node->node, node);
129 node->func = func;
130 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
131}
132
133/* Compare two command's string. Used in sort_node (). */
134static int cmp_node(const void *p, const void *q)
135{
136 struct cmd_element *a = *(struct cmd_element **)p;
137 struct cmd_element *b = *(struct cmd_element **)q;
138
139 return strcmp(a->string, b->string);
140}
141
142static int cmp_desc(const void *p, const void *q)
143{
144 struct desc *a = *(struct desc **)p;
145 struct desc *b = *(struct desc **)q;
146
147 return strcmp(a->cmd, b->cmd);
148}
149
Jacob Erlbeck2442e092013-09-06 16:51:58 +0200150static int is_config_child(struct vty *vty)
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800151{
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800152 if (vty->node <= CONFIG_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800153 return 0;
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800154 else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800155 return 1;
156 else if (host.app_info->is_config_node)
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800157 return host.app_info->is_config_node(vty, vty->node);
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800158 else
159 return vty->node > CONFIG_NODE;
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800160}
161
Harald Welte7acb30c2011-08-17 17:13:48 +0200162/*! \brief Sort each node's command element according to command string. */
Harald Welte95b2b472011-07-16 11:58:09 +0200163void sort_node(void)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200164{
165 unsigned int i, j;
166 struct cmd_node *cnode;
167 vector descvec;
168 struct cmd_element *cmd_element;
169
170 for (i = 0; i < vector_active(cmdvec); i++)
171 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
172 vector cmd_vector = cnode->cmd_vector;
173 qsort(cmd_vector->index, vector_active(cmd_vector),
174 sizeof(void *), cmp_node);
175
176 for (j = 0; j < vector_active(cmd_vector); j++)
177 if ((cmd_element =
178 vector_slot(cmd_vector, j)) != NULL
179 && vector_active(cmd_element->strvec)) {
180 descvec =
181 vector_slot(cmd_element->strvec,
182 vector_active
183 (cmd_element->strvec) -
184 1);
185 qsort(descvec->index,
186 vector_active(descvec),
187 sizeof(void *), cmp_desc);
188 }
189 }
190}
191
Harald Welte7acb30c2011-08-17 17:13:48 +0200192/*! Breaking up string into each command piece. I assume given
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200193 character is separated by a space character. Return value is a
194 vector which includes char ** data element. */
195vector cmd_make_strvec(const char *string)
196{
197 const char *cp, *start;
198 char *token;
199 int strlen;
200 vector strvec;
201
202 if (string == NULL)
203 return NULL;
204
205 cp = string;
206
207 /* Skip white spaces. */
208 while (isspace((int)*cp) && *cp != '\0')
209 cp++;
210
211 /* Return if there is only white spaces */
212 if (*cp == '\0')
213 return NULL;
214
215 if (*cp == '!' || *cp == '#')
216 return NULL;
217
218 /* Prepare return vector. */
219 strvec = vector_init(VECTOR_MIN_SIZE);
220
221 /* Copy each command piece and set into vector. */
222 while (1) {
223 start = cp;
224 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
225 *cp != '\0')
226 cp++;
227 strlen = cp - start;
228 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
229 memcpy(token, start, strlen);
230 *(token + strlen) = '\0';
231 vector_set(strvec, token);
232
233 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
234 *cp != '\0')
235 cp++;
236
237 if (*cp == '\0')
238 return strvec;
239 }
240}
241
Harald Welte7acb30c2011-08-17 17:13:48 +0200242/*! \brief Free allocated string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200243void cmd_free_strvec(vector v)
244{
245 unsigned int i;
246 char *cp;
247
248 if (!v)
249 return;
250
251 for (i = 0; i < vector_active(v); i++)
252 if ((cp = vector_slot(v, i)) != NULL)
253 talloc_free(cp);
254
255 vector_free(v);
256}
257
Harald Welte7acb30c2011-08-17 17:13:48 +0200258/*! \brief Fetch next description. Used in \ref cmd_make_descvec(). */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200259static char *cmd_desc_str(const char **string)
260{
261 const char *cp, *start;
262 char *token;
263 int strlen;
264
265 cp = *string;
266
267 if (cp == NULL)
268 return NULL;
269
270 /* Skip white spaces. */
271 while (isspace((int)*cp) && *cp != '\0')
272 cp++;
273
274 /* Return if there is only white spaces */
275 if (*cp == '\0')
276 return NULL;
277
278 start = cp;
279
280 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
281 cp++;
282
283 strlen = cp - start;
284 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
285 memcpy(token, start, strlen);
286 *(token + strlen) = '\0';
287
288 *string = cp;
289
290 return token;
291}
292
Harald Welte7acb30c2011-08-17 17:13:48 +0200293/*! \brief New string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200294static vector cmd_make_descvec(const char *string, const char *descstr)
295{
296 int multiple = 0;
297 const char *sp;
298 char *token;
299 int len;
300 const char *cp;
301 const char *dp;
302 vector allvec;
303 vector strvec = NULL;
304 struct desc *desc;
305
306 cp = string;
307 dp = descstr;
308
309 if (cp == NULL)
310 return NULL;
311
312 allvec = vector_init(VECTOR_MIN_SIZE);
313
314 while (1) {
315 while (isspace((int)*cp) && *cp != '\0')
316 cp++;
317
318 if (*cp == '(') {
319 multiple = 1;
320 cp++;
321 }
322 if (*cp == ')') {
323 multiple = 0;
324 cp++;
325 }
326 if (*cp == '|') {
Harald Weltea99d45a2015-11-12 13:48:23 +0100327 OSMO_ASSERT(multiple);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200328 cp++;
329 }
330
331 while (isspace((int)*cp) && *cp != '\0')
332 cp++;
333
334 if (*cp == '(') {
335 multiple = 1;
336 cp++;
337 }
338
339 if (*cp == '\0')
340 return allvec;
341
342 sp = cp;
343
344 while (!
345 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
346 || *cp == ')' || *cp == '|') && *cp != '\0')
347 cp++;
348
349 len = cp - sp;
350
351 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
352 memcpy(token, sp, len);
353 *(token + len) = '\0';
354
355 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
356 desc->cmd = token;
357 desc->str = cmd_desc_str(&dp);
358
359 if (multiple) {
360 if (multiple == 1) {
361 strvec = vector_init(VECTOR_MIN_SIZE);
362 vector_set(allvec, strvec);
363 }
364 multiple++;
365 } else {
366 strvec = vector_init(VECTOR_MIN_SIZE);
367 vector_set(allvec, strvec);
368 }
369 vector_set(strvec, desc);
370 }
371}
372
373/* Count mandantory string vector size. This is to determine inputed
374 command has enough command length. */
375static int cmd_cmdsize(vector strvec)
376{
377 unsigned int i;
378 int size = 0;
379 vector descvec;
380 struct desc *desc;
381
382 for (i = 0; i < vector_active(strvec); i++)
383 if ((descvec = vector_slot(strvec, i)) != NULL) {
384 if ((vector_active(descvec)) == 1
385 && (desc = vector_slot(descvec, 0)) != NULL) {
386 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
387 return size;
388 else
389 size++;
390 } else
391 size++;
392 }
393 return size;
394}
395
Harald Welte7acb30c2011-08-17 17:13:48 +0200396/*! \brief Return prompt character of specified node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200397const char *cmd_prompt(enum node_type node)
398{
399 struct cmd_node *cnode;
400
401 cnode = vector_slot(cmdvec, node);
402 return cnode->prompt;
403}
404
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100405static char *xml_escape(const char *inp)
406{
407 int _strlen;
408 char *out, *out_ptr;
409 int len = 0, i, j;
410
411 if (!inp)
412 return NULL;
413 _strlen = strlen(inp);
414
415 for (i = 0; i < _strlen; ++i) {
416 switch (inp[i]) {
417 case '"':
418 len += 6;
419 break;
420 case '\'':
421 len += 6;
422 break;
423 case '<':
424 len += 4;
425 break;
426 case '>':
427 len += 4;
428 break;
429 case '&':
430 len += 5;
431 break;
432 default:
433 len += 1;
434 break;
435 }
436 }
437
438 out = talloc_size(NULL, len + 1);
439 if (!out)
440 return NULL;
441
442 out_ptr = out;
443
444#define ADD(out, str) \
445 for (j = 0; j < strlen(str); ++j) \
446 *(out++) = str[j];
447
448 for (i = 0; i < _strlen; ++i) {
449 switch (inp[i]) {
450 case '"':
451 ADD(out_ptr, "&quot;");
452 break;
453 case '\'':
454 ADD(out_ptr, "&apos;");
455 break;
456 case '<':
457 ADD(out_ptr, "&lt;");
458 break;
459 case '>':
460 ADD(out_ptr, "&gt;");
461 break;
462 case '&':
463 ADD(out_ptr, "&amp;");
464 break;
465 default:
466 *(out_ptr++) = inp[i];
467 break;
468 }
469 }
470
471#undef ADD
472
473 out_ptr[0] = '\0';
474 return out;
475}
476
477/*
478 * Write one cmd_element as XML to the given VTY.
479 */
480static int vty_dump_element(struct cmd_element *cmd, struct vty *vty)
481{
482 char *xml_string = xml_escape(cmd->string);
483
484 vty_out(vty, " <command id='%s'>%s", xml_string, VTY_NEWLINE);
485 vty_out(vty, " <params>%s", VTY_NEWLINE);
486
487 int j;
488 for (j = 0; j < vector_count(cmd->strvec); ++j) {
489 vector descvec = vector_slot(cmd->strvec, j);
490 int i;
491 for (i = 0; i < vector_active(descvec); ++i) {
492 char *xml_param, *xml_doc;
493 struct desc *desc = vector_slot(descvec, i);
494 if (desc == NULL)
495 continue;
496
497 xml_param = xml_escape(desc->cmd);
498 xml_doc = xml_escape(desc->str);
499 vty_out(vty, " <param name='%s' doc='%s' />%s",
500 xml_param, xml_doc, VTY_NEWLINE);
501 talloc_free(xml_param);
502 talloc_free(xml_doc);
503 }
504 }
505
506 vty_out(vty, " </params>%s", VTY_NEWLINE);
507 vty_out(vty, " </command>%s", VTY_NEWLINE);
508
509 talloc_free(xml_string);
510 return 0;
511}
512
513/*
514 * Dump all nodes and commands associated with a given node as XML to the VTY.
515 */
516static int vty_dump_nodes(struct vty *vty)
517{
518 int i, j;
519
520 vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
521
522 for (i = 0; i < vector_active(cmdvec); ++i) {
523 struct cmd_node *cnode;
524 cnode = vector_slot(cmdvec, i);
525 if (!cnode)
526 continue;
527
528 vty_out(vty, " <node id='%d'>%s", i, VTY_NEWLINE);
529
530 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
531 struct cmd_element *elem;
532 elem = vector_slot(cnode->cmd_vector, j);
533 vty_dump_element(elem, vty);
534 }
535
536 vty_out(vty, " </node>%s", VTY_NEWLINE);
537 }
538
539 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
540
541 return 0;
542}
543
Harald Welte7acb30c2011-08-17 17:13:48 +0200544/*! \brief Install a command into a node
545 * \param[in] ntype Node Type
546 * \param[cmd] element to be installed
547 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000548void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200549{
550 struct cmd_node *cnode;
551
552 cnode = vector_slot(cmdvec, ntype);
553
Harald Weltea99d45a2015-11-12 13:48:23 +0100554 OSMO_ASSERT(cnode);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200555
556 vector_set(cnode->cmd_vector, cmd);
557
558 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
559 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
560}
561
562/* Install a command into VIEW and ENABLE node */
563void install_element_ve(struct cmd_element *cmd)
564{
565 install_element(VIEW_NODE, cmd);
566 install_element(ENABLE_NODE, cmd);
567}
568
569#ifdef VTY_CRYPT_PW
570static unsigned char itoa64[] =
571 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
572
573static void to64(char *s, long v, int n)
574{
575 while (--n >= 0) {
576 *s++ = itoa64[v & 0x3f];
577 v >>= 6;
578 }
579}
580
581static char *zencrypt(const char *passwd)
582{
583 char salt[6];
584 struct timeval tv;
585 char *crypt(const char *, const char *);
586
587 gettimeofday(&tv, 0);
588
589 to64(&salt[0], random(), 3);
590 to64(&salt[3], tv.tv_usec, 3);
591 salt[5] = '\0';
592
593 return crypt(passwd, salt);
594}
595#endif
596
597/* This function write configuration of this host. */
598static int config_write_host(struct vty *vty)
599{
600 if (host.name)
601 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
602
603 if (host.encrypt) {
604 if (host.password_encrypt)
605 vty_out(vty, "password 8 %s%s", host.password_encrypt,
606 VTY_NEWLINE);
607 if (host.enable_encrypt)
608 vty_out(vty, "enable password 8 %s%s",
609 host.enable_encrypt, VTY_NEWLINE);
610 } else {
611 if (host.password)
612 vty_out(vty, "password %s%s", host.password,
613 VTY_NEWLINE);
614 if (host.enable)
615 vty_out(vty, "enable password %s%s", host.enable,
616 VTY_NEWLINE);
617 }
618
619 if (host.advanced)
620 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
621
622 if (host.encrypt)
623 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
624
625 if (host.lines >= 0)
626 vty_out(vty, "service terminal-length %d%s", host.lines,
627 VTY_NEWLINE);
628
629 if (host.motdfile)
630 vty_out(vty, "banner motd file %s%s", host.motdfile,
631 VTY_NEWLINE);
632 else if (!host.motd)
633 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
634
635 return 1;
636}
637
638/* Utility function for getting command vector. */
639static vector cmd_node_vector(vector v, enum node_type ntype)
640{
641 struct cmd_node *cnode = vector_slot(v, ntype);
642 return cnode->cmd_vector;
643}
644
645/* Completion match types. */
646enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100647 no_match = 0,
648 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200649 extend_match,
650 ipv4_prefix_match,
651 ipv4_match,
652 ipv6_prefix_match,
653 ipv6_match,
654 range_match,
655 vararg_match,
656 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100657 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200658};
659
660static enum match_type cmd_ipv4_match(const char *str)
661{
662 const char *sp;
663 int dots = 0, nums = 0;
664 char buf[4];
665
666 if (str == NULL)
667 return partly_match;
668
669 for (;;) {
670 memset(buf, 0, sizeof(buf));
671 sp = str;
672 while (*str != '\0') {
673 if (*str == '.') {
674 if (dots >= 3)
675 return no_match;
676
677 if (*(str + 1) == '.')
678 return no_match;
679
680 if (*(str + 1) == '\0')
681 return partly_match;
682
683 dots++;
684 break;
685 }
686 if (!isdigit((int)*str))
687 return no_match;
688
689 str++;
690 }
691
692 if (str - sp > 3)
693 return no_match;
694
695 strncpy(buf, sp, str - sp);
696 if (atoi(buf) > 255)
697 return no_match;
698
699 nums++;
700
701 if (*str == '\0')
702 break;
703
704 str++;
705 }
706
707 if (nums < 4)
708 return partly_match;
709
710 return exact_match;
711}
712
713static enum match_type cmd_ipv4_prefix_match(const char *str)
714{
715 const char *sp;
716 int dots = 0;
717 char buf[4];
718
719 if (str == NULL)
720 return partly_match;
721
722 for (;;) {
723 memset(buf, 0, sizeof(buf));
724 sp = str;
725 while (*str != '\0' && *str != '/') {
726 if (*str == '.') {
727 if (dots == 3)
728 return no_match;
729
730 if (*(str + 1) == '.' || *(str + 1) == '/')
731 return no_match;
732
733 if (*(str + 1) == '\0')
734 return partly_match;
735
736 dots++;
737 break;
738 }
739
740 if (!isdigit((int)*str))
741 return no_match;
742
743 str++;
744 }
745
746 if (str - sp > 3)
747 return no_match;
748
749 strncpy(buf, sp, str - sp);
750 if (atoi(buf) > 255)
751 return no_match;
752
753 if (dots == 3) {
754 if (*str == '/') {
755 if (*(str + 1) == '\0')
756 return partly_match;
757
758 str++;
759 break;
760 } else if (*str == '\0')
761 return partly_match;
762 }
763
764 if (*str == '\0')
765 return partly_match;
766
767 str++;
768 }
769
770 sp = str;
771 while (*str != '\0') {
772 if (!isdigit((int)*str))
773 return no_match;
774
775 str++;
776 }
777
778 if (atoi(sp) > 32)
779 return no_match;
780
781 return exact_match;
782}
783
784#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
785#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
786#define STATE_START 1
787#define STATE_COLON 2
788#define STATE_DOUBLE 3
789#define STATE_ADDR 4
790#define STATE_DOT 5
791#define STATE_SLASH 6
792#define STATE_MASK 7
793
794#ifdef HAVE_IPV6
795
796static enum match_type cmd_ipv6_match(const char *str)
797{
798 int state = STATE_START;
799 int colons = 0, nums = 0, double_colon = 0;
800 const char *sp = NULL;
801 struct sockaddr_in6 sin6_dummy;
802 int ret;
803
804 if (str == NULL)
805 return partly_match;
806
807 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
808 return no_match;
809
810 /* use inet_pton that has a better support,
811 * for example inet_pton can support the automatic addresses:
812 * ::1.2.3.4
813 */
814 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
815
816 if (ret == 1)
817 return exact_match;
818
819 while (*str != '\0') {
820 switch (state) {
821 case STATE_START:
822 if (*str == ':') {
823 if (*(str + 1) != ':' && *(str + 1) != '\0')
824 return no_match;
825 colons--;
826 state = STATE_COLON;
827 } else {
828 sp = str;
829 state = STATE_ADDR;
830 }
831
832 continue;
833 case STATE_COLON:
834 colons++;
835 if (*(str + 1) == ':')
836 state = STATE_DOUBLE;
837 else {
838 sp = str + 1;
839 state = STATE_ADDR;
840 }
841 break;
842 case STATE_DOUBLE:
843 if (double_colon)
844 return no_match;
845
846 if (*(str + 1) == ':')
847 return no_match;
848 else {
849 if (*(str + 1) != '\0')
850 colons++;
851 sp = str + 1;
852 state = STATE_ADDR;
853 }
854
855 double_colon++;
856 nums++;
857 break;
858 case STATE_ADDR:
859 if (*(str + 1) == ':' || *(str + 1) == '\0') {
860 if (str - sp > 3)
861 return no_match;
862
863 nums++;
864 state = STATE_COLON;
865 }
866 if (*(str + 1) == '.')
867 state = STATE_DOT;
868 break;
869 case STATE_DOT:
870 state = STATE_ADDR;
871 break;
872 default:
873 break;
874 }
875
876 if (nums > 8)
877 return no_match;
878
879 if (colons > 7)
880 return no_match;
881
882 str++;
883 }
884
885#if 0
886 if (nums < 11)
887 return partly_match;
888#endif /* 0 */
889
890 return exact_match;
891}
892
893static enum match_type cmd_ipv6_prefix_match(const char *str)
894{
895 int state = STATE_START;
896 int colons = 0, nums = 0, double_colon = 0;
897 int mask;
898 const char *sp = NULL;
899 char *endptr = NULL;
900
901 if (str == NULL)
902 return partly_match;
903
904 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
905 return no_match;
906
907 while (*str != '\0' && state != STATE_MASK) {
908 switch (state) {
909 case STATE_START:
910 if (*str == ':') {
911 if (*(str + 1) != ':' && *(str + 1) != '\0')
912 return no_match;
913 colons--;
914 state = STATE_COLON;
915 } else {
916 sp = str;
917 state = STATE_ADDR;
918 }
919
920 continue;
921 case STATE_COLON:
922 colons++;
923 if (*(str + 1) == '/')
924 return no_match;
925 else if (*(str + 1) == ':')
926 state = STATE_DOUBLE;
927 else {
928 sp = str + 1;
929 state = STATE_ADDR;
930 }
931 break;
932 case STATE_DOUBLE:
933 if (double_colon)
934 return no_match;
935
936 if (*(str + 1) == ':')
937 return no_match;
938 else {
939 if (*(str + 1) != '\0' && *(str + 1) != '/')
940 colons++;
941 sp = str + 1;
942
943 if (*(str + 1) == '/')
944 state = STATE_SLASH;
945 else
946 state = STATE_ADDR;
947 }
948
949 double_colon++;
950 nums += 1;
951 break;
952 case STATE_ADDR:
953 if (*(str + 1) == ':' || *(str + 1) == '.'
954 || *(str + 1) == '\0' || *(str + 1) == '/') {
955 if (str - sp > 3)
956 return no_match;
957
958 for (; sp <= str; sp++)
959 if (*sp == '/')
960 return no_match;
961
962 nums++;
963
964 if (*(str + 1) == ':')
965 state = STATE_COLON;
966 else if (*(str + 1) == '.')
967 state = STATE_DOT;
968 else if (*(str + 1) == '/')
969 state = STATE_SLASH;
970 }
971 break;
972 case STATE_DOT:
973 state = STATE_ADDR;
974 break;
975 case STATE_SLASH:
976 if (*(str + 1) == '\0')
977 return partly_match;
978
979 state = STATE_MASK;
980 break;
981 default:
982 break;
983 }
984
985 if (nums > 11)
986 return no_match;
987
988 if (colons > 7)
989 return no_match;
990
991 str++;
992 }
993
994 if (state < STATE_MASK)
995 return partly_match;
996
997 mask = strtol(str, &endptr, 10);
998 if (*endptr != '\0')
999 return no_match;
1000
1001 if (mask < 0 || mask > 128)
1002 return no_match;
1003
1004/* I don't know why mask < 13 makes command match partly.
1005 Forgive me to make this comments. I Want to set static default route
1006 because of lack of function to originate default in ospf6d; sorry
1007 yasu
1008 if (mask < 13)
1009 return partly_match;
1010*/
1011
1012 return exact_match;
1013}
1014
1015#endif /* HAVE_IPV6 */
1016
1017#define DECIMAL_STRLEN_MAX 10
1018
1019static int cmd_range_match(const char *range, const char *str)
1020{
1021 char *p;
1022 char buf[DECIMAL_STRLEN_MAX + 1];
1023 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001024
1025 if (str == NULL)
1026 return 1;
1027
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001028 if (range[1] == '-') {
1029 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001030
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001031 val = strtol(str, &endptr, 10);
1032 if (*endptr != '\0')
1033 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001034
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001035 range += 2;
1036 p = strchr(range, '-');
1037 if (p == NULL)
1038 return 0;
1039 if (p - range > DECIMAL_STRLEN_MAX)
1040 return 0;
1041 strncpy(buf, range, p - range);
1042 buf[p - range] = '\0';
1043 min = -strtol(buf, &endptr, 10);
1044 if (*endptr != '\0')
1045 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001046
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001047 range = p + 1;
1048 p = strchr(range, '>');
1049 if (p == NULL)
1050 return 0;
1051 if (p - range > DECIMAL_STRLEN_MAX)
1052 return 0;
1053 strncpy(buf, range, p - range);
1054 buf[p - range] = '\0';
1055 max = strtol(buf, &endptr, 10);
1056 if (*endptr != '\0')
1057 return 0;
1058
1059 if (val < min || val > max)
1060 return 0;
1061 } else {
1062 unsigned long min, max, val;
1063
1064 val = strtoul(str, &endptr, 10);
1065 if (*endptr != '\0')
1066 return 0;
1067
1068 range++;
1069 p = strchr(range, '-');
1070 if (p == NULL)
1071 return 0;
1072 if (p - range > DECIMAL_STRLEN_MAX)
1073 return 0;
1074 strncpy(buf, range, p - range);
1075 buf[p - range] = '\0';
1076 min = strtoul(buf, &endptr, 10);
1077 if (*endptr != '\0')
1078 return 0;
1079
1080 range = p + 1;
1081 p = strchr(range, '>');
1082 if (p == NULL)
1083 return 0;
1084 if (p - range > DECIMAL_STRLEN_MAX)
1085 return 0;
1086 strncpy(buf, range, p - range);
1087 buf[p - range] = '\0';
1088 max = strtoul(buf, &endptr, 10);
1089 if (*endptr != '\0')
1090 return 0;
1091
1092 if (val < min || val > max)
1093 return 0;
1094 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001095
1096 return 1;
1097}
1098
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001099/* helper to retrieve the 'real' argument string from an optional argument */
1100static char *
1101cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001102{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001103 /* we've got "[blah]". We want to strip off the []s and redo the
1104 * match check for "blah"
1105 */
1106 size_t len = strlen(str);
1107 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001108
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001109 if (len < 3)
1110 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001111
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001112 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1113 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001114
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001115 memcpy(tmp, (str + 1), len - 2);
1116 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001117
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001118 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001119}
1120
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001121static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001122cmd_match(const char *str, const char *command,
1123 enum match_type min, bool recur)
1124{
1125
1126 if (recur && CMD_OPTION(str))
1127 {
1128 enum match_type ret;
1129 char *tmp = cmd_deopt(str);
1130
1131 /* this would be a bug in a command, however handle it gracefully
1132 * as it we only discover it if a user tries to run it
1133 */
1134 if (tmp == NULL)
1135 return no_match;
1136
1137 ret = cmd_match(tmp, command, min, false);
1138
1139 talloc_free(tmp);
1140
1141 return ret;
1142 }
1143 else if (CMD_VARARG(str))
1144 return vararg_match;
1145 else if (CMD_RANGE(str))
1146 {
1147 if (cmd_range_match(str, command))
1148 return range_match;
1149 }
1150#ifdef HAVE_IPV6
1151 else if (CMD_IPV6(str))
1152 {
1153 if (cmd_ipv6_match(command) >= min)
1154 return ipv6_match;
1155 }
1156 else if (CMD_IPV6_PREFIX(str))
1157 {
1158 if (cmd_ipv6_prefix_match(command) >= min)
1159 return ipv6_prefix_match;
1160 }
1161#endif /* HAVE_IPV6 */
1162 else if (CMD_IPV4(str))
1163 {
1164 if (cmd_ipv4_match(command) >= min)
1165 return ipv4_match;
1166 }
1167 else if (CMD_IPV4_PREFIX(str))
1168 {
1169 if (cmd_ipv4_prefix_match(command) >= min)
1170 return ipv4_prefix_match;
1171 }
1172 else if (CMD_VARIABLE(str))
1173 return extend_match;
1174 else if (strncmp(command, str, strlen(command)) == 0)
1175 {
1176 if (strcmp(command, str) == 0)
1177 return exact_match;
1178 else if (partly_match >= min)
1179 return partly_match;
1180 }
1181
1182 return no_match;
1183}
1184
1185/* Filter vector at the specified index and by the given command string, to
1186 * the desired matching level (thus allowing part matches), and return match
1187 * type flag.
1188 */
1189static enum match_type
1190cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001191{
1192 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001193 struct cmd_element *cmd_element;
1194 enum match_type match_type;
1195 vector descvec;
1196 struct desc *desc;
1197
1198 match_type = no_match;
1199
1200 /* If command and cmd_element string does not match set NULL to vector */
1201 for (i = 0; i < vector_active(v); i++)
1202 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001203 if (index >= vector_active(cmd_element->strvec))
1204 vector_slot(v, i) = NULL;
1205 else {
1206 unsigned int j;
1207 int matched = 0;
1208
1209 descvec =
1210 vector_slot(cmd_element->strvec, index);
1211
1212 for (j = 0; j < vector_active(descvec); j++)
1213 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001214 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001215
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001216 ret = cmd_match (desc->cmd, command, level, true);
1217
1218 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001219 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001220
1221 if (match_type < ret)
1222 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001223 }
1224 if (!matched)
1225 vector_slot(v, i) = NULL;
1226 }
1227 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001228
1229 if (match_type == no_match)
1230 return no_match;
1231
1232 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1233 * go again and filter out commands whose argument (at this index) is
1234 * 'weaker'. E.g., if we have 2 commands:
1235 *
1236 * foo bar <1-255>
1237 * foo bar BLAH
1238 *
1239 * and the command string is 'foo bar 10', then we will get here with with
1240 * 'range_match' being the strongest match. However, if 'BLAH' came
1241 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1242 *
1243 * If we don't do a 2nd pass and filter it out, the higher-layers will
1244 * consider this to be ambiguous.
1245 */
1246 for (i = 0; i < vector_active(v); i++)
1247 if ((cmd_element = vector_slot(v, i)) != NULL) {
1248 if (index >= vector_active(cmd_element->strvec))
1249 vector_slot(v, i) = NULL;
1250 else {
1251 unsigned int j;
1252 int matched = 0;
1253
1254 descvec =
1255 vector_slot(cmd_element->strvec, index);
1256
1257 for (j = 0; j < vector_active(descvec); j++)
1258 if ((desc = vector_slot(descvec, j))) {
1259 enum match_type ret;
1260
1261 ret = cmd_match(desc->cmd, command, any_match, true);
1262
1263 if (ret >= match_type)
1264 matched++;
1265 }
1266 if (!matched)
1267 vector_slot(v, i) = NULL;
1268 }
1269 }
1270
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001271 return match_type;
1272}
1273
1274/* Check ambiguous match */
1275static int
1276is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1277{
1278 unsigned int i;
1279 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001280 struct cmd_element *cmd_element;
1281 const char *matched = NULL;
1282 vector descvec;
1283 struct desc *desc;
1284
1285 for (i = 0; i < vector_active(v); i++)
1286 if ((cmd_element = vector_slot(v, i)) != NULL) {
1287 int match = 0;
1288
1289 descvec = vector_slot(cmd_element->strvec, index);
1290
1291 for (j = 0; j < vector_active(descvec); j++)
1292 if ((desc = vector_slot(descvec, j))) {
1293 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001294 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001295
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001296 if (CMD_OPTION(str))
1297 if ((str = cmd_deopt(str)) == NULL)
1298 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001299
1300 switch (type) {
1301 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001302 if (!(CMD_VARIABLE (str))
1303 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001304 match++;
1305 break;
1306 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001307 if (!(CMD_VARIABLE (str))
1308 && strncmp(command, str, strlen (command)) == 0)
1309 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001310 if (matched
1311 && strcmp(matched,
1312 str) != 0)
1313 return 1; /* There is ambiguous match. */
1314 else
1315 matched = str;
1316 match++;
1317 }
1318 break;
1319 case range_match:
1320 if (cmd_range_match
1321 (str, command)) {
1322 if (matched
1323 && strcmp(matched,
1324 str) != 0)
1325 return 1;
1326 else
1327 matched = str;
1328 match++;
1329 }
1330 break;
1331#ifdef HAVE_IPV6
1332 case ipv6_match:
1333 if (CMD_IPV6(str))
1334 match++;
1335 break;
1336 case ipv6_prefix_match:
1337 if ((ret =
1338 cmd_ipv6_prefix_match
1339 (command)) != no_match) {
1340 if (ret == partly_match)
1341 return 2; /* There is incomplete match. */
1342
1343 match++;
1344 }
1345 break;
1346#endif /* HAVE_IPV6 */
1347 case ipv4_match:
1348 if (CMD_IPV4(str))
1349 match++;
1350 break;
1351 case ipv4_prefix_match:
1352 if ((ret =
1353 cmd_ipv4_prefix_match
1354 (command)) != no_match) {
1355 if (ret == partly_match)
1356 return 2; /* There is incomplete match. */
1357
1358 match++;
1359 }
1360 break;
1361 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001362 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001363 match++;
1364 break;
1365 case no_match:
1366 default:
1367 break;
1368 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001369
1370 if (CMD_OPTION(desc->cmd))
1371 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001372 }
1373 if (!match)
1374 vector_slot(v, i) = NULL;
1375 }
1376 return 0;
1377}
1378
1379/* If src matches dst return dst string, otherwise return NULL */
1380static const char *cmd_entry_function(const char *src, const char *dst)
1381{
1382 /* Skip variable arguments. */
1383 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1384 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1385 return NULL;
1386
1387 /* In case of 'command \t', given src is NULL string. */
1388 if (src == NULL)
1389 return dst;
1390
1391 /* Matched with input string. */
1392 if (strncmp(src, dst, strlen(src)) == 0)
1393 return dst;
1394
1395 return NULL;
1396}
1397
1398/* If src matches dst return dst string, otherwise return NULL */
1399/* This version will return the dst string always if it is
1400 CMD_VARIABLE for '?' key processing */
1401static const char *cmd_entry_function_desc(const char *src, const char *dst)
1402{
1403 if (CMD_VARARG(dst))
1404 return dst;
1405
1406 if (CMD_RANGE(dst)) {
1407 if (cmd_range_match(dst, src))
1408 return dst;
1409 else
1410 return NULL;
1411 }
1412#ifdef HAVE_IPV6
1413 if (CMD_IPV6(dst)) {
1414 if (cmd_ipv6_match(src))
1415 return dst;
1416 else
1417 return NULL;
1418 }
1419
1420 if (CMD_IPV6_PREFIX(dst)) {
1421 if (cmd_ipv6_prefix_match(src))
1422 return dst;
1423 else
1424 return NULL;
1425 }
1426#endif /* HAVE_IPV6 */
1427
1428 if (CMD_IPV4(dst)) {
1429 if (cmd_ipv4_match(src))
1430 return dst;
1431 else
1432 return NULL;
1433 }
1434
1435 if (CMD_IPV4_PREFIX(dst)) {
1436 if (cmd_ipv4_prefix_match(src))
1437 return dst;
1438 else
1439 return NULL;
1440 }
1441
1442 /* Optional or variable commands always match on '?' */
1443 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1444 return dst;
1445
1446 /* In case of 'command \t', given src is NULL string. */
1447 if (src == NULL)
1448 return dst;
1449
1450 if (strncmp(src, dst, strlen(src)) == 0)
1451 return dst;
1452 else
1453 return NULL;
1454}
1455
1456/* Check same string element existence. If it isn't there return
1457 1. */
1458static int cmd_unique_string(vector v, const char *str)
1459{
1460 unsigned int i;
1461 char *match;
1462
1463 for (i = 0; i < vector_active(v); i++)
1464 if ((match = vector_slot(v, i)) != NULL)
1465 if (strcmp(match, str) == 0)
1466 return 0;
1467 return 1;
1468}
1469
1470/* Compare string to description vector. If there is same string
1471 return 1 else return 0. */
1472static int desc_unique_string(vector v, const char *str)
1473{
1474 unsigned int i;
1475 struct desc *desc;
1476
1477 for (i = 0; i < vector_active(v); i++)
1478 if ((desc = vector_slot(v, i)) != NULL)
1479 if (strcmp(desc->cmd, str) == 0)
1480 return 1;
1481 return 0;
1482}
1483
1484static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1485{
1486 if (first_word != NULL &&
1487 node != AUTH_NODE &&
1488 node != VIEW_NODE &&
1489 node != AUTH_ENABLE_NODE &&
1490 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1491 return 1;
1492 return 0;
1493}
1494
1495/* '?' describe command support. */
1496static vector
1497cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1498{
1499 unsigned int i;
1500 vector cmd_vector;
1501#define INIT_MATCHVEC_SIZE 10
1502 vector matchvec;
1503 struct cmd_element *cmd_element;
1504 unsigned int index;
1505 int ret;
1506 enum match_type match;
1507 char *command;
1508 static struct desc desc_cr = { "<cr>", "" };
1509
1510 /* Set index. */
1511 if (vector_active(vline) == 0) {
1512 *status = CMD_ERR_NO_MATCH;
1513 return NULL;
1514 } else
1515 index = vector_active(vline) - 1;
1516
1517 /* Make copy vector of current node's command vector. */
1518 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1519
1520 /* Prepare match vector */
1521 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1522
1523 /* Filter commands. */
1524 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001525 for (i = 0; i < index; i++) {
1526 command = vector_slot(vline, i);
1527 if (!command)
1528 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001529
Harald Welte80d30fe2013-02-12 11:08:57 +01001530 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001531
Harald Welte80d30fe2013-02-12 11:08:57 +01001532 if (match == vararg_match) {
1533 struct cmd_element *cmd_element;
1534 vector descvec;
1535 unsigned int j, k;
1536
1537 for (j = 0; j < vector_active(cmd_vector); j++)
1538 if ((cmd_element =
1539 vector_slot(cmd_vector, j)) != NULL
1540 &&
1541 (vector_active(cmd_element->strvec))) {
1542 descvec =
1543 vector_slot(cmd_element->
1544 strvec,
1545 vector_active
1546 (cmd_element->
1547 strvec) - 1);
1548 for (k = 0;
1549 k < vector_active(descvec);
1550 k++) {
1551 struct desc *desc =
1552 vector_slot(descvec,
1553 k);
1554 vector_set(matchvec,
1555 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001556 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001557 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001558
Harald Welte80d30fe2013-02-12 11:08:57 +01001559 vector_set(matchvec, &desc_cr);
1560 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001561
Harald Welte80d30fe2013-02-12 11:08:57 +01001562 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001563 }
1564
Harald Welte80d30fe2013-02-12 11:08:57 +01001565 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1566 match)) == 1) {
1567 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001568 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001569 *status = CMD_ERR_AMBIGUOUS;
1570 return NULL;
1571 } else if (ret == 2) {
1572 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001573 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001574 *status = CMD_ERR_NO_MATCH;
1575 return NULL;
1576 }
1577 }
1578
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001579 /* Prepare match vector */
1580 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1581
1582 /* Make sure that cmd_vector is filtered based on current word */
1583 command = vector_slot(vline, index);
1584 if (command)
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001585 match = cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001586
1587 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001588 for (i = 0; i < vector_active(cmd_vector); i++) {
1589 const char *string = NULL;
1590 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001591
Harald Welte80d30fe2013-02-12 11:08:57 +01001592 cmd_element = vector_slot(cmd_vector, i);
1593 if (!cmd_element)
1594 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001595
Harald Welted17aa592013-02-12 11:11:34 +01001596 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1597 continue;
1598
Harald Welte80d30fe2013-02-12 11:08:57 +01001599 strvec = cmd_element->strvec;
1600
1601 /* if command is NULL, index may be equal to vector_active */
1602 if (command && index >= vector_active(strvec))
1603 vector_slot(cmd_vector, i) = NULL;
1604 else {
1605 /* Check if command is completed. */
1606 if (command == NULL
1607 && index == vector_active(strvec)) {
1608 string = "<cr>";
1609 if (!desc_unique_string(matchvec, string))
1610 vector_set(matchvec, &desc_cr);
1611 } else {
1612 unsigned int j;
1613 vector descvec = vector_slot(strvec, index);
1614 struct desc *desc;
1615
1616 for (j = 0; j < vector_active(descvec); j++) {
1617 desc = vector_slot(descvec, j);
1618 if (!desc)
1619 continue;
1620 string = cmd_entry_function_desc
1621 (command, desc->cmd);
1622 if (!string)
1623 continue;
1624 /* Uniqueness check */
1625 if (!desc_unique_string(matchvec, string))
1626 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001627 }
1628 }
1629 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001630 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001631 vector_free(cmd_vector);
1632
1633 if (vector_slot(matchvec, 0) == NULL) {
1634 vector_free(matchvec);
1635 *status = CMD_ERR_NO_MATCH;
1636 } else
1637 *status = CMD_SUCCESS;
1638
1639 return matchvec;
1640}
1641
1642vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1643{
1644 vector ret;
1645
1646 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1647 enum node_type onode;
1648 vector shifted_vline;
1649 unsigned int index;
1650
1651 onode = vty->node;
1652 vty->node = ENABLE_NODE;
1653 /* We can try it on enable node, cos' the vty is authenticated */
1654
1655 shifted_vline = vector_init(vector_count(vline));
1656 /* use memcpy? */
1657 for (index = 1; index < vector_active(vline); index++) {
1658 vector_set_index(shifted_vline, index - 1,
1659 vector_lookup(vline, index));
1660 }
1661
1662 ret = cmd_describe_command_real(shifted_vline, vty, status);
1663
1664 vector_free(shifted_vline);
1665 vty->node = onode;
1666 return ret;
1667 }
1668
1669 return cmd_describe_command_real(vline, vty, status);
1670}
1671
1672/* Check LCD of matched command. */
1673static int cmd_lcd(char **matched)
1674{
1675 int i;
1676 int j;
1677 int lcd = -1;
1678 char *s1, *s2;
1679 char c1, c2;
1680
1681 if (matched[0] == NULL || matched[1] == NULL)
1682 return 0;
1683
1684 for (i = 1; matched[i] != NULL; i++) {
1685 s1 = matched[i - 1];
1686 s2 = matched[i];
1687
1688 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1689 if (c1 != c2)
1690 break;
1691
1692 if (lcd < 0)
1693 lcd = j;
1694 else {
1695 if (lcd > j)
1696 lcd = j;
1697 }
1698 }
1699 return lcd;
1700}
1701
1702/* Command line completion support. */
1703static char **cmd_complete_command_real(vector vline, struct vty *vty,
1704 int *status)
1705{
1706 unsigned int i;
1707 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1708#define INIT_MATCHVEC_SIZE 10
1709 vector matchvec;
1710 struct cmd_element *cmd_element;
1711 unsigned int index;
1712 char **match_str;
1713 struct desc *desc;
1714 vector descvec;
1715 char *command;
1716 int lcd;
1717
1718 if (vector_active(vline) == 0) {
1719 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001720 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001721 return NULL;
1722 } else
1723 index = vector_active(vline) - 1;
1724
1725 /* First, filter by preceeding command string */
1726 for (i = 0; i < index; i++)
1727 if ((command = vector_slot(vline, i))) {
1728 enum match_type match;
1729 int ret;
1730
1731 /* First try completion match, if there is exactly match return 1 */
1732 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001733 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001734
1735 /* If there is exact match then filter ambiguous match else check
1736 ambiguousness. */
1737 if ((ret =
1738 is_cmd_ambiguous(command, cmd_vector, i,
1739 match)) == 1) {
1740 vector_free(cmd_vector);
1741 *status = CMD_ERR_AMBIGUOUS;
1742 return NULL;
1743 }
1744 /*
1745 else if (ret == 2)
1746 {
1747 vector_free (cmd_vector);
1748 *status = CMD_ERR_NO_MATCH;
1749 return NULL;
1750 }
1751 */
1752 }
1753
1754 /* Prepare match vector. */
1755 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1756
1757 /* Now we got into completion */
1758 for (i = 0; i < vector_active(cmd_vector); i++)
1759 if ((cmd_element = vector_slot(cmd_vector, i))) {
1760 const char *string;
1761 vector strvec = cmd_element->strvec;
1762
1763 /* Check field length */
1764 if (index >= vector_active(strvec))
1765 vector_slot(cmd_vector, i) = NULL;
1766 else {
1767 unsigned int j;
1768
1769 descvec = vector_slot(strvec, index);
1770 for (j = 0; j < vector_active(descvec); j++)
1771 if ((desc = vector_slot(descvec, j))) {
1772 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1773 if (cmd_unique_string (matchvec, string))
1774 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1775 }
1776 }
1777 }
1778
1779 /* We don't need cmd_vector any more. */
1780 vector_free(cmd_vector);
1781
1782 /* No matched command */
1783 if (vector_slot(matchvec, 0) == NULL) {
1784 vector_free(matchvec);
1785
1786 /* In case of 'command \t' pattern. Do you need '?' command at
1787 the end of the line. */
1788 if (vector_slot(vline, index) == '\0')
1789 *status = CMD_ERR_NOTHING_TODO;
1790 else
1791 *status = CMD_ERR_NO_MATCH;
1792 return NULL;
1793 }
1794
1795 /* Only one matched */
1796 if (vector_slot(matchvec, 1) == NULL) {
1797 match_str = (char **)matchvec->index;
1798 vector_only_wrapper_free(matchvec);
1799 *status = CMD_COMPLETE_FULL_MATCH;
1800 return match_str;
1801 }
1802 /* Make it sure last element is NULL. */
1803 vector_set(matchvec, NULL);
1804
1805 /* Check LCD of matched strings. */
1806 if (vector_slot(vline, index) != NULL) {
1807 lcd = cmd_lcd((char **)matchvec->index);
1808
1809 if (lcd) {
1810 int len = strlen(vector_slot(vline, index));
1811
1812 if (len < lcd) {
1813 char *lcdstr;
1814
1815 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1816 "complete-lcdstr");
1817 memcpy(lcdstr, matchvec->index[0], lcd);
1818 lcdstr[lcd] = '\0';
1819
1820 /* match_str = (char **) &lcdstr; */
1821
1822 /* Free matchvec. */
1823 for (i = 0; i < vector_active(matchvec); i++) {
1824 if (vector_slot(matchvec, i))
1825 talloc_free(vector_slot(matchvec, i));
1826 }
1827 vector_free(matchvec);
1828
1829 /* Make new matchvec. */
1830 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1831 vector_set(matchvec, lcdstr);
1832 match_str = (char **)matchvec->index;
1833 vector_only_wrapper_free(matchvec);
1834
1835 *status = CMD_COMPLETE_MATCH;
1836 return match_str;
1837 }
1838 }
1839 }
1840
1841 match_str = (char **)matchvec->index;
1842 vector_only_wrapper_free(matchvec);
1843 *status = CMD_COMPLETE_LIST_MATCH;
1844 return match_str;
1845}
1846
1847char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1848{
1849 char **ret;
1850
1851 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1852 enum node_type onode;
1853 vector shifted_vline;
1854 unsigned int index;
1855
1856 onode = vty->node;
1857 vty->node = ENABLE_NODE;
1858 /* We can try it on enable node, cos' the vty is authenticated */
1859
1860 shifted_vline = vector_init(vector_count(vline));
1861 /* use memcpy? */
1862 for (index = 1; index < vector_active(vline); index++) {
1863 vector_set_index(shifted_vline, index - 1,
1864 vector_lookup(vline, index));
1865 }
1866
1867 ret = cmd_complete_command_real(shifted_vline, vty, status);
1868
1869 vector_free(shifted_vline);
1870 vty->node = onode;
1871 return ret;
1872 }
1873
1874 return cmd_complete_command_real(vline, vty, status);
1875}
1876
1877/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001878/*
1879 * This function MUST eventually converge on a node when called repeatedly,
1880 * there must not be any cycles.
1881 * All 'config' nodes shall converge on CONFIG_NODE.
1882 * All other 'enable' nodes shall converge on ENABLE_NODE.
1883 * All 'view' only nodes shall converge on VIEW_NODE.
1884 * All other nodes shall converge on themselves or it must be ensured,
1885 * that the user's rights are not extended anyhow by calling this function.
1886 *
1887 * Note that these requirements also apply to all functions that are used
1888 * as go_parent_cb.
1889 * Note also that this function relies on the is_config_child callback to
1890 * recognize non-config nodes if go_parent_cb is not set.
1891 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00001892int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001893{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001894 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001895 case AUTH_NODE:
1896 case VIEW_NODE:
1897 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001898 case CONFIG_NODE:
1899 break;
1900
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001901 case AUTH_ENABLE_NODE:
1902 vty->node = VIEW_NODE;
1903 break;
1904
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001905 case CFG_LOG_NODE:
1906 case VTY_NODE:
1907 vty->node = CONFIG_NODE;
1908 break;
1909
1910 default:
1911 if (host.app_info->go_parent_cb)
1912 host.app_info->go_parent_cb(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001913 else if (is_config_child(vty))
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001914 vty->node = CONFIG_NODE;
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001915 else
1916 vty->node = VIEW_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001917 break;
1918 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001919
1920 return vty->node;
1921}
1922
1923/* Execute command by argument vline vector. */
1924static int
1925cmd_execute_command_real(vector vline, struct vty *vty,
1926 struct cmd_element **cmd)
1927{
1928 unsigned int i;
1929 unsigned int index;
1930 vector cmd_vector;
1931 struct cmd_element *cmd_element;
1932 struct cmd_element *matched_element;
1933 unsigned int matched_count, incomplete_count;
1934 int argc;
1935 const char *argv[CMD_ARGC_MAX];
1936 enum match_type match = 0;
1937 int varflag;
1938 char *command;
1939
1940 /* Make copy of command elements. */
1941 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1942
1943 for (index = 0; index < vector_active(vline); index++)
1944 if ((command = vector_slot(vline, index))) {
1945 int ret;
1946
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001947 match = cmd_filter(command, cmd_vector, index,
1948 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001949
1950 if (match == vararg_match)
1951 break;
1952
1953 ret =
1954 is_cmd_ambiguous(command, cmd_vector, index, match);
1955
1956 if (ret == 1) {
1957 vector_free(cmd_vector);
1958 return CMD_ERR_AMBIGUOUS;
1959 } else if (ret == 2) {
1960 vector_free(cmd_vector);
1961 return CMD_ERR_NO_MATCH;
1962 }
1963 }
1964
1965 /* Check matched count. */
1966 matched_element = NULL;
1967 matched_count = 0;
1968 incomplete_count = 0;
1969
1970 for (i = 0; i < vector_active(cmd_vector); i++)
1971 if ((cmd_element = vector_slot(cmd_vector, i))) {
1972 if (match == vararg_match
1973 || index >= cmd_element->cmdsize) {
1974 matched_element = cmd_element;
1975#if 0
1976 printf("DEBUG: %s\n", cmd_element->string);
1977#endif
1978 matched_count++;
1979 } else {
1980 incomplete_count++;
1981 }
1982 }
1983
1984 /* Finish of using cmd_vector. */
1985 vector_free(cmd_vector);
1986
1987 /* To execute command, matched_count must be 1. */
1988 if (matched_count == 0) {
1989 if (incomplete_count)
1990 return CMD_ERR_INCOMPLETE;
1991 else
1992 return CMD_ERR_NO_MATCH;
1993 }
1994
1995 if (matched_count > 1)
1996 return CMD_ERR_AMBIGUOUS;
1997
1998 /* Argument treatment */
1999 varflag = 0;
2000 argc = 0;
2001
2002 for (i = 0; i < vector_active(vline); i++) {
2003 if (varflag)
2004 argv[argc++] = vector_slot(vline, i);
2005 else {
2006 vector descvec =
2007 vector_slot(matched_element->strvec, i);
2008
2009 if (vector_active(descvec) == 1) {
2010 struct desc *desc = vector_slot(descvec, 0);
2011
2012 if (CMD_VARARG(desc->cmd))
2013 varflag = 1;
2014
2015 if (varflag || CMD_VARIABLE(desc->cmd)
2016 || CMD_OPTION(desc->cmd))
2017 argv[argc++] = vector_slot(vline, i);
2018 } else
2019 argv[argc++] = vector_slot(vline, i);
2020 }
2021
2022 if (argc >= CMD_ARGC_MAX)
2023 return CMD_ERR_EXEED_ARGC_MAX;
2024 }
2025
2026 /* For vtysh execution. */
2027 if (cmd)
2028 *cmd = matched_element;
2029
2030 if (matched_element->daemon)
2031 return CMD_SUCCESS_DAEMON;
2032
2033 /* Execute matched command. */
2034 return (*matched_element->func) (matched_element, vty, argc, argv);
2035}
2036
2037int
2038cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2039 int vtysh)
2040{
2041 int ret, saved_ret, tried = 0;
2042 enum node_type onode;
2043 void *oindex;
2044
2045 onode = vty->node;
2046 oindex = vty->index;
2047
2048 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2049 vector shifted_vline;
2050 unsigned int index;
2051
2052 vty->node = ENABLE_NODE;
2053 /* We can try it on enable node, cos' the vty is authenticated */
2054
2055 shifted_vline = vector_init(vector_count(vline));
2056 /* use memcpy? */
2057 for (index = 1; index < vector_active(vline); index++) {
2058 vector_set_index(shifted_vline, index - 1,
2059 vector_lookup(vline, index));
2060 }
2061
2062 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2063
2064 vector_free(shifted_vline);
2065 vty->node = onode;
2066 return ret;
2067 }
2068
2069 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2070
2071 if (vtysh)
2072 return saved_ret;
2073
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002074 /* Go to parent for config nodes to attempt to find the right command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002075 while (ret != CMD_SUCCESS && ret != CMD_WARNING
Jacob Erlbeck2442e092013-09-06 16:51:58 +02002076 && is_config_child(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002077 vty_go_parent(vty);
2078 ret = cmd_execute_command_real(vline, vty, cmd);
2079 tried = 1;
2080 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2081 /* succesfull command, leave the node as is */
2082 return ret;
2083 }
2084 }
2085 /* no command succeeded, reset the vty to the original node and
2086 return the error for this node */
2087 if (tried) {
2088 vty->node = onode;
2089 vty->index = oindex;
2090 }
2091 return saved_ret;
2092}
2093
2094/* Execute command by argument readline. */
2095int
2096cmd_execute_command_strict(vector vline, struct vty *vty,
2097 struct cmd_element **cmd)
2098{
2099 unsigned int i;
2100 unsigned int index;
2101 vector cmd_vector;
2102 struct cmd_element *cmd_element;
2103 struct cmd_element *matched_element;
2104 unsigned int matched_count, incomplete_count;
2105 int argc;
2106 const char *argv[CMD_ARGC_MAX];
2107 int varflag;
2108 enum match_type match = 0;
2109 char *command;
2110
2111 /* Make copy of command element */
2112 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2113
2114 for (index = 0; index < vector_active(vline); index++)
2115 if ((command = vector_slot(vline, index))) {
2116 int ret;
2117
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002118 match = cmd_filter(vector_slot(vline, index),
2119 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002120
2121 /* If command meets '.VARARG' then finish matching. */
2122 if (match == vararg_match)
2123 break;
2124
2125 ret =
2126 is_cmd_ambiguous(command, cmd_vector, index, match);
2127 if (ret == 1) {
2128 vector_free(cmd_vector);
2129 return CMD_ERR_AMBIGUOUS;
2130 }
2131 if (ret == 2) {
2132 vector_free(cmd_vector);
2133 return CMD_ERR_NO_MATCH;
2134 }
2135 }
2136
2137 /* Check matched count. */
2138 matched_element = NULL;
2139 matched_count = 0;
2140 incomplete_count = 0;
2141 for (i = 0; i < vector_active(cmd_vector); i++)
2142 if (vector_slot(cmd_vector, i) != NULL) {
2143 cmd_element = vector_slot(cmd_vector, i);
2144
2145 if (match == vararg_match
2146 || index >= cmd_element->cmdsize) {
2147 matched_element = cmd_element;
2148 matched_count++;
2149 } else
2150 incomplete_count++;
2151 }
2152
2153 /* Finish of using cmd_vector. */
2154 vector_free(cmd_vector);
2155
2156 /* To execute command, matched_count must be 1. */
2157 if (matched_count == 0) {
2158 if (incomplete_count)
2159 return CMD_ERR_INCOMPLETE;
2160 else
2161 return CMD_ERR_NO_MATCH;
2162 }
2163
2164 if (matched_count > 1)
2165 return CMD_ERR_AMBIGUOUS;
2166
2167 /* Argument treatment */
2168 varflag = 0;
2169 argc = 0;
2170
2171 for (i = 0; i < vector_active(vline); i++) {
2172 if (varflag)
2173 argv[argc++] = vector_slot(vline, i);
2174 else {
2175 vector descvec =
2176 vector_slot(matched_element->strvec, i);
2177
2178 if (vector_active(descvec) == 1) {
2179 struct desc *desc = vector_slot(descvec, 0);
2180
2181 if (CMD_VARARG(desc->cmd))
2182 varflag = 1;
2183
2184 if (varflag || CMD_VARIABLE(desc->cmd)
2185 || CMD_OPTION(desc->cmd))
2186 argv[argc++] = vector_slot(vline, i);
2187 } else
2188 argv[argc++] = vector_slot(vline, i);
2189 }
2190
2191 if (argc >= CMD_ARGC_MAX)
2192 return CMD_ERR_EXEED_ARGC_MAX;
2193 }
2194
2195 /* For vtysh execution. */
2196 if (cmd)
2197 *cmd = matched_element;
2198
2199 if (matched_element->daemon)
2200 return CMD_SUCCESS_DAEMON;
2201
2202 /* Now execute matched command */
2203 return (*matched_element->func) (matched_element, vty, argc, argv);
2204}
2205
2206/* Configration make from file. */
2207int config_from_file(struct vty *vty, FILE * fp)
2208{
2209 int ret;
2210 vector vline;
2211
2212 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2213 vline = cmd_make_strvec(vty->buf);
2214
2215 /* In case of comment line */
2216 if (vline == NULL)
2217 continue;
2218 /* Execute configuration command : this is strict match */
2219 ret = cmd_execute_command_strict(vline, vty, NULL);
2220
2221 /* Try again with setting node to CONFIG_NODE */
2222 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2223 && ret != CMD_ERR_NOTHING_TODO
Jacob Erlbeck2442e092013-09-06 16:51:58 +02002224 && is_config_child(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002225 vty_go_parent(vty);
2226 ret = cmd_execute_command_strict(vline, vty, NULL);
2227 }
2228
2229 cmd_free_strvec(vline);
2230
2231 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2232 && ret != CMD_ERR_NOTHING_TODO)
2233 return ret;
2234 }
2235 return CMD_SUCCESS;
2236}
2237
2238/* Configration from terminal */
2239DEFUN(config_terminal,
2240 config_terminal_cmd,
2241 "configure terminal",
2242 "Configuration from vty interface\n" "Configuration terminal\n")
2243{
2244 if (vty_config_lock(vty))
2245 vty->node = CONFIG_NODE;
2246 else {
2247 vty_out(vty, "VTY configuration is locked by other VTY%s",
2248 VTY_NEWLINE);
2249 return CMD_WARNING;
2250 }
2251 return CMD_SUCCESS;
2252}
2253
2254/* Enable command */
2255DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2256{
2257 /* If enable password is NULL, change to ENABLE_NODE */
2258 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2259 vty->type == VTY_SHELL_SERV)
2260 vty->node = ENABLE_NODE;
2261 else
2262 vty->node = AUTH_ENABLE_NODE;
2263
2264 return CMD_SUCCESS;
2265}
2266
2267/* Disable command */
2268DEFUN(disable,
2269 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2270{
2271 if (vty->node == ENABLE_NODE)
2272 vty->node = VIEW_NODE;
2273 return CMD_SUCCESS;
2274}
2275
2276/* Down vty node level. */
2277gDEFUN(config_exit,
2278 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2279{
2280 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002281 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002282 case VIEW_NODE:
2283 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002284 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002285 break;
2286 case CONFIG_NODE:
2287 vty->node = ENABLE_NODE;
2288 vty_config_unlock(vty);
2289 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002290 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002291 if (vty->node > CONFIG_NODE)
2292 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002293 break;
2294 }
2295 return CMD_SUCCESS;
2296}
2297
2298/* End of configuration. */
2299 gDEFUN(config_end,
2300 config_end_cmd, "end", "End current mode and change to enable mode.")
2301{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002302 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002303 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002304
2305 /* Repeatedly call go_parent until a top node is reached. */
2306 while (vty->node > CONFIG_NODE) {
2307 if (vty->node == last_node) {
2308 /* Ensure termination, this shouldn't happen. */
2309 break;
2310 }
2311 last_node = vty->node;
2312 vty_go_parent(vty);
2313 }
2314
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002315 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002316 if (vty->node > ENABLE_NODE)
2317 vty->node = ENABLE_NODE;
2318 vty->index = NULL;
2319 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002320 }
2321 return CMD_SUCCESS;
2322}
2323
2324/* Show version. */
2325DEFUN(show_version,
2326 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2327{
Harald Welte237f6242010-05-25 23:00:45 +02002328 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2329 host.app_info->version,
2330 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2331 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002332
2333 return CMD_SUCCESS;
2334}
2335
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002336DEFUN(show_online_help,
2337 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2338{
2339 vty_dump_nodes(vty);
2340 return CMD_SUCCESS;
2341}
2342
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002343/* Help display function for all node. */
2344gDEFUN(config_help,
2345 config_help_cmd, "help", "Description of the interactive help system\n")
2346{
2347 vty_out(vty,
2348 "This VTY provides advanced help features. When you need help,%s\
2349anytime at the command line please press '?'.%s\
2350%s\
2351If nothing matches, the help list will be empty and you must backup%s\
2352 until entering a '?' shows the available options.%s\
2353Two styles of help are provided:%s\
23541. Full help is available when you are ready to enter a%s\
2355command argument (e.g. 'show ?') and describes each possible%s\
2356argument.%s\
23572. Partial help is provided when an abbreviated argument is entered%s\
2358 and you want to know what arguments match the input%s\
2359 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2360 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2361 return CMD_SUCCESS;
2362}
2363
2364/* Help display function for all node. */
2365gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2366{
2367 unsigned int i;
2368 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2369 struct cmd_element *cmd;
2370
2371 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2372 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2373 && !(cmd->attr == CMD_ATTR_DEPRECATED
2374 || cmd->attr == CMD_ATTR_HIDDEN))
2375 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2376 return CMD_SUCCESS;
2377}
2378
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002379static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002380{
2381 unsigned int i;
2382 int fd;
2383 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002384 char *config_file_tmp = NULL;
2385 char *config_file_sav = NULL;
2386 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002387 struct stat st;
2388
2389 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002390
2391 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002392 config_file_sav =
2393 _talloc_zero(tall_vty_cmd_ctx,
2394 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2395 "config_file_sav");
2396 strcpy(config_file_sav, config_file);
2397 strcat(config_file_sav, CONF_BACKUP_EXT);
2398
2399 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2400 "config_file_tmp");
2401 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2402
2403 /* Open file to configuration write. */
2404 fd = mkstemp(config_file_tmp);
2405 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002406 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002407 talloc_free(config_file_tmp);
2408 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002409 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002410 }
2411
2412 /* Make vty for configuration file. */
2413 file_vty = vty_new();
2414 file_vty->fd = fd;
2415 file_vty->type = VTY_FILE;
2416
2417 /* Config file header print. */
2418 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002419 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002420 //vty_time_print (file_vty, 1);
2421 vty_out(file_vty, "!\n");
2422
2423 for (i = 0; i < vector_active(cmdvec); i++)
2424 if ((node = vector_slot(cmdvec, i)) && node->func) {
2425 if ((*node->func) (file_vty))
2426 vty_out(file_vty, "!\n");
2427 }
2428 vty_close(file_vty);
2429
2430 if (unlink(config_file_sav) != 0)
2431 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002432 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002433 talloc_free(config_file_sav);
2434 talloc_free(config_file_tmp);
2435 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002436 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002437 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002438
2439 /* Only link the .sav file if the original file exists */
2440 if (stat(config_file, &st) == 0) {
2441 if (link(config_file, config_file_sav) != 0) {
2442 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2443 talloc_free(config_file_sav);
2444 talloc_free(config_file_tmp);
2445 unlink(config_file_tmp);
2446 return -3;
2447 }
2448 sync();
2449 if (unlink(config_file) != 0) {
2450 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2451 talloc_free(config_file_sav);
2452 talloc_free(config_file_tmp);
2453 unlink(config_file_tmp);
2454 return -4;
2455 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002456 }
2457 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002458 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002459 talloc_free(config_file_sav);
2460 talloc_free(config_file_tmp);
2461 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002462 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002463 }
2464 unlink(config_file_tmp);
2465 sync();
2466
2467 talloc_free(config_file_sav);
2468 talloc_free(config_file_tmp);
2469
2470 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002471 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2472 return -6;
2473 }
2474
2475 return 0;
2476}
2477
2478
2479/* Write current configuration into file. */
2480DEFUN(config_write_file,
2481 config_write_file_cmd,
2482 "write file",
2483 "Write running configuration to memory, network, or terminal\n"
2484 "Write to configuration file\n")
2485{
2486 char *failed_file;
2487 int rc;
2488
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002489 if (host.app_info->config_is_consistent) {
2490 rc = host.app_info->config_is_consistent(vty);
2491 if (!rc) {
2492 vty_out(vty, "Configuration is not consistent%s",
2493 VTY_NEWLINE);
2494 return CMD_WARNING;
2495 }
2496 }
2497
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002498 if (host.config == NULL) {
2499 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2500 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002501 return CMD_WARNING;
2502 }
2503
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002504 rc = write_config_file(host.config, &failed_file);
2505 switch (rc) {
2506 case -1:
2507 vty_out(vty, "Can't open configuration file %s.%s",
2508 failed_file, VTY_NEWLINE);
2509 rc = CMD_WARNING;
2510 break;
2511 case -2:
2512 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2513 failed_file, VTY_NEWLINE);
2514 rc = CMD_WARNING;
2515 break;
2516 case -3:
2517 vty_out(vty, "Can't backup old configuration file %s.%s",
2518 failed_file, VTY_NEWLINE);
2519 rc = CMD_WARNING;
2520 break;
2521 case -4:
2522 vty_out(vty, "Can't unlink configuration file %s.%s",
2523 failed_file, VTY_NEWLINE);
2524 rc = CMD_WARNING;
2525 break;
2526 case -5:
2527 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2528 VTY_NEWLINE);
2529 rc = CMD_WARNING;
2530 break;
2531 case -6:
2532 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2533 failed_file, strerror(errno), errno, VTY_NEWLINE);
2534 rc = CMD_WARNING;
2535 break;
2536 default:
2537 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2538 rc = CMD_SUCCESS;
2539 break;
2540 }
2541
2542 talloc_free(failed_file);
2543 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002544}
2545
2546ALIAS(config_write_file,
2547 config_write_cmd,
2548 "write", "Write running configuration to memory, network, or terminal\n")
2549
2550 ALIAS(config_write_file,
2551 config_write_memory_cmd,
2552 "write memory",
2553 "Write running configuration to memory, network, or terminal\n"
2554 "Write configuration to the file (same as write file)\n")
2555
2556 ALIAS(config_write_file,
2557 copy_runningconfig_startupconfig_cmd,
2558 "copy running-config startup-config",
2559 "Copy configuration\n"
2560 "Copy running config to... \n"
2561 "Copy running config to startup config (same as write file)\n")
2562
2563/* Write current configuration into the terminal. */
2564 DEFUN(config_write_terminal,
2565 config_write_terminal_cmd,
2566 "write terminal",
2567 "Write running configuration to memory, network, or terminal\n"
2568 "Write to terminal\n")
2569{
2570 unsigned int i;
2571 struct cmd_node *node;
2572
2573 if (vty->type == VTY_SHELL_SERV) {
2574 for (i = 0; i < vector_active(cmdvec); i++)
2575 if ((node = vector_slot(cmdvec, i)) && node->func
2576 && node->vtysh) {
2577 if ((*node->func) (vty))
2578 vty_out(vty, "!%s", VTY_NEWLINE);
2579 }
2580 } else {
2581 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2582 VTY_NEWLINE);
2583 vty_out(vty, "!%s", VTY_NEWLINE);
2584
2585 for (i = 0; i < vector_active(cmdvec); i++)
2586 if ((node = vector_slot(cmdvec, i)) && node->func) {
2587 if ((*node->func) (vty))
2588 vty_out(vty, "!%s", VTY_NEWLINE);
2589 }
2590 vty_out(vty, "end%s", VTY_NEWLINE);
2591 }
2592 return CMD_SUCCESS;
2593}
2594
2595/* Write current configuration into the terminal. */
2596ALIAS(config_write_terminal,
2597 show_running_config_cmd,
2598 "show running-config", SHOW_STR "running configuration\n")
2599
2600/* Write startup configuration into the terminal. */
2601 DEFUN(show_startup_config,
2602 show_startup_config_cmd,
2603 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2604{
2605 char buf[BUFSIZ];
2606 FILE *confp;
2607
2608 confp = fopen(host.config, "r");
2609 if (confp == NULL) {
2610 vty_out(vty, "Can't open configuration file [%s]%s",
2611 host.config, VTY_NEWLINE);
2612 return CMD_WARNING;
2613 }
2614
2615 while (fgets(buf, BUFSIZ, confp)) {
2616 char *cp = buf;
2617
2618 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2619 cp++;
2620 *cp = '\0';
2621
2622 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2623 }
2624
2625 fclose(confp);
2626
2627 return CMD_SUCCESS;
2628}
2629
2630/* Hostname configuration */
2631DEFUN(config_hostname,
2632 hostname_cmd,
2633 "hostname WORD",
2634 "Set system's network name\n" "This system's network name\n")
2635{
2636 if (!isalpha((int)*argv[0])) {
2637 vty_out(vty, "Please specify string starting with alphabet%s",
2638 VTY_NEWLINE);
2639 return CMD_WARNING;
2640 }
2641
2642 if (host.name)
2643 talloc_free(host.name);
2644
2645 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2646 return CMD_SUCCESS;
2647}
2648
2649DEFUN(config_no_hostname,
2650 no_hostname_cmd,
2651 "no hostname [HOSTNAME]",
2652 NO_STR "Reset system's network name\n" "Host name of this router\n")
2653{
2654 if (host.name)
2655 talloc_free(host.name);
2656 host.name = NULL;
2657 return CMD_SUCCESS;
2658}
2659
2660/* VTY interface password set. */
2661DEFUN(config_password, password_cmd,
2662 "password (8|) WORD",
2663 "Assign the terminal connection password\n"
2664 "Specifies a HIDDEN password will follow\n"
2665 "dummy string \n" "The HIDDEN line password string\n")
2666{
2667 /* Argument check. */
2668 if (argc == 0) {
2669 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2670 return CMD_WARNING;
2671 }
2672
2673 if (argc == 2) {
2674 if (*argv[0] == '8') {
2675 if (host.password)
2676 talloc_free(host.password);
2677 host.password = NULL;
2678 if (host.password_encrypt)
2679 talloc_free(host.password_encrypt);
2680 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2681 return CMD_SUCCESS;
2682 } else {
2683 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2684 return CMD_WARNING;
2685 }
2686 }
2687
2688 if (!isalnum((int)*argv[0])) {
2689 vty_out(vty,
2690 "Please specify string starting with alphanumeric%s",
2691 VTY_NEWLINE);
2692 return CMD_WARNING;
2693 }
2694
2695 if (host.password)
2696 talloc_free(host.password);
2697 host.password = NULL;
2698
2699#ifdef VTY_CRYPT_PW
2700 if (host.encrypt) {
2701 if (host.password_encrypt)
2702 talloc_free(host.password_encrypt);
2703 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2704 } else
2705#endif
2706 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2707
2708 return CMD_SUCCESS;
2709}
2710
2711ALIAS(config_password, password_text_cmd,
2712 "password LINE",
2713 "Assign the terminal connection password\n"
2714 "The UNENCRYPTED (cleartext) line password\n")
2715
2716/* VTY enable password set. */
2717 DEFUN(config_enable_password, enable_password_cmd,
2718 "enable password (8|) WORD",
2719 "Modify enable password parameters\n"
2720 "Assign the privileged level password\n"
2721 "Specifies a HIDDEN password will follow\n"
2722 "dummy string \n" "The HIDDEN 'enable' password string\n")
2723{
2724 /* Argument check. */
2725 if (argc == 0) {
2726 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2727 return CMD_WARNING;
2728 }
2729
2730 /* Crypt type is specified. */
2731 if (argc == 2) {
2732 if (*argv[0] == '8') {
2733 if (host.enable)
2734 talloc_free(host.enable);
2735 host.enable = NULL;
2736
2737 if (host.enable_encrypt)
2738 talloc_free(host.enable_encrypt);
2739 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2740
2741 return CMD_SUCCESS;
2742 } else {
2743 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2744 return CMD_WARNING;
2745 }
2746 }
2747
2748 if (!isalnum((int)*argv[0])) {
2749 vty_out(vty,
2750 "Please specify string starting with alphanumeric%s",
2751 VTY_NEWLINE);
2752 return CMD_WARNING;
2753 }
2754
2755 if (host.enable)
2756 talloc_free(host.enable);
2757 host.enable = NULL;
2758
2759 /* Plain password input. */
2760#ifdef VTY_CRYPT_PW
2761 if (host.encrypt) {
2762 if (host.enable_encrypt)
2763 talloc_free(host.enable_encrypt);
2764 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2765 } else
2766#endif
2767 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2768
2769 return CMD_SUCCESS;
2770}
2771
2772ALIAS(config_enable_password,
2773 enable_password_text_cmd,
2774 "enable password LINE",
2775 "Modify enable password parameters\n"
2776 "Assign the privileged level password\n"
2777 "The UNENCRYPTED (cleartext) 'enable' password\n")
2778
2779/* VTY enable password delete. */
2780 DEFUN(no_config_enable_password, no_enable_password_cmd,
2781 "no enable password",
2782 NO_STR
2783 "Modify enable password parameters\n"
2784 "Assign the privileged level password\n")
2785{
2786 if (host.enable)
2787 talloc_free(host.enable);
2788 host.enable = NULL;
2789
2790 if (host.enable_encrypt)
2791 talloc_free(host.enable_encrypt);
2792 host.enable_encrypt = NULL;
2793
2794 return CMD_SUCCESS;
2795}
2796
2797#ifdef VTY_CRYPT_PW
2798DEFUN(service_password_encrypt,
2799 service_password_encrypt_cmd,
2800 "service password-encryption",
2801 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2802{
2803 if (host.encrypt)
2804 return CMD_SUCCESS;
2805
2806 host.encrypt = 1;
2807
2808 if (host.password) {
2809 if (host.password_encrypt)
2810 talloc_free(host.password_encrypt);
2811 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2812 }
2813 if (host.enable) {
2814 if (host.enable_encrypt)
2815 talloc_free(host.enable_encrypt);
2816 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2817 }
2818
2819 return CMD_SUCCESS;
2820}
2821
2822DEFUN(no_service_password_encrypt,
2823 no_service_password_encrypt_cmd,
2824 "no service password-encryption",
2825 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2826{
2827 if (!host.encrypt)
2828 return CMD_SUCCESS;
2829
2830 host.encrypt = 0;
2831
2832 if (host.password_encrypt)
2833 talloc_free(host.password_encrypt);
2834 host.password_encrypt = NULL;
2835
2836 if (host.enable_encrypt)
2837 talloc_free(host.enable_encrypt);
2838 host.enable_encrypt = NULL;
2839
2840 return CMD_SUCCESS;
2841}
2842#endif
2843
2844DEFUN(config_terminal_length, config_terminal_length_cmd,
2845 "terminal length <0-512>",
2846 "Set terminal line parameters\n"
2847 "Set number of lines on a screen\n"
2848 "Number of lines on screen (0 for no pausing)\n")
2849{
2850 int lines;
2851 char *endptr = NULL;
2852
2853 lines = strtol(argv[0], &endptr, 10);
2854 if (lines < 0 || lines > 512 || *endptr != '\0') {
2855 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2856 return CMD_WARNING;
2857 }
2858 vty->lines = lines;
2859
2860 return CMD_SUCCESS;
2861}
2862
2863DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2864 "terminal no length",
2865 "Set terminal line parameters\n"
2866 NO_STR "Set number of lines on a screen\n")
2867{
2868 vty->lines = -1;
2869 return CMD_SUCCESS;
2870}
2871
2872DEFUN(service_terminal_length, service_terminal_length_cmd,
2873 "service terminal-length <0-512>",
2874 "Set up miscellaneous service\n"
2875 "System wide terminal length configuration\n"
2876 "Number of lines of VTY (0 means no line control)\n")
2877{
2878 int lines;
2879 char *endptr = NULL;
2880
2881 lines = strtol(argv[0], &endptr, 10);
2882 if (lines < 0 || lines > 512 || *endptr != '\0') {
2883 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2884 return CMD_WARNING;
2885 }
2886 host.lines = lines;
2887
2888 return CMD_SUCCESS;
2889}
2890
2891DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2892 "no service terminal-length [<0-512>]",
2893 NO_STR
2894 "Set up miscellaneous service\n"
2895 "System wide terminal length configuration\n"
2896 "Number of lines of VTY (0 means no line control)\n")
2897{
2898 host.lines = -1;
2899 return CMD_SUCCESS;
2900}
2901
2902DEFUN_HIDDEN(do_echo,
2903 echo_cmd,
2904 "echo .MESSAGE",
2905 "Echo a message back to the vty\n" "The message to echo\n")
2906{
2907 char *message;
2908
2909 vty_out(vty, "%s%s",
2910 ((message =
2911 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2912 if (message)
2913 talloc_free(message);
2914 return CMD_SUCCESS;
2915}
2916
2917#if 0
2918DEFUN(config_logmsg,
2919 config_logmsg_cmd,
2920 "logmsg " LOG_LEVELS " .MESSAGE",
2921 "Send a message to enabled logging destinations\n"
2922 LOG_LEVEL_DESC "The message to send\n")
2923{
2924 int level;
2925 char *message;
2926
2927 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2928 return CMD_ERR_NO_MATCH;
2929
2930 zlog(NULL, level,
2931 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2932 if (message)
2933 talloc_free(message);
2934 return CMD_SUCCESS;
2935}
2936
2937DEFUN(show_logging,
2938 show_logging_cmd,
2939 "show logging", SHOW_STR "Show current logging configuration\n")
2940{
2941 struct zlog *zl = zlog_default;
2942
2943 vty_out(vty, "Syslog logging: ");
2944 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2945 vty_out(vty, "disabled");
2946 else
2947 vty_out(vty, "level %s, facility %s, ident %s",
2948 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2949 facility_name(zl->facility), zl->ident);
2950 vty_out(vty, "%s", VTY_NEWLINE);
2951
2952 vty_out(vty, "Stdout logging: ");
2953 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2954 vty_out(vty, "disabled");
2955 else
2956 vty_out(vty, "level %s",
2957 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2958 vty_out(vty, "%s", VTY_NEWLINE);
2959
2960 vty_out(vty, "Monitor logging: ");
2961 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2962 vty_out(vty, "disabled");
2963 else
2964 vty_out(vty, "level %s",
2965 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2966 vty_out(vty, "%s", VTY_NEWLINE);
2967
2968 vty_out(vty, "File logging: ");
2969 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2970 vty_out(vty, "disabled");
2971 else
2972 vty_out(vty, "level %s, filename %s",
2973 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2974 zl->filename);
2975 vty_out(vty, "%s", VTY_NEWLINE);
2976
2977 vty_out(vty, "Protocol name: %s%s",
2978 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2979 vty_out(vty, "Record priority: %s%s",
2980 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2981
2982 return CMD_SUCCESS;
2983}
2984
2985DEFUN(config_log_stdout,
2986 config_log_stdout_cmd,
2987 "log stdout", "Logging control\n" "Set stdout logging level\n")
2988{
2989 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2990 return CMD_SUCCESS;
2991}
2992
2993DEFUN(config_log_stdout_level,
2994 config_log_stdout_level_cmd,
2995 "log stdout " LOG_LEVELS,
2996 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2997{
2998 int level;
2999
3000 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3001 return CMD_ERR_NO_MATCH;
3002 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3003 return CMD_SUCCESS;
3004}
3005
3006DEFUN(no_config_log_stdout,
3007 no_config_log_stdout_cmd,
3008 "no log stdout [LEVEL]",
3009 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3010{
3011 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3012 return CMD_SUCCESS;
3013}
3014
3015DEFUN(config_log_monitor,
3016 config_log_monitor_cmd,
3017 "log monitor",
3018 "Logging control\n" "Set terminal line (monitor) logging level\n")
3019{
3020 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3021 return CMD_SUCCESS;
3022}
3023
3024DEFUN(config_log_monitor_level,
3025 config_log_monitor_level_cmd,
3026 "log monitor " LOG_LEVELS,
3027 "Logging control\n"
3028 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3029{
3030 int level;
3031
3032 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3033 return CMD_ERR_NO_MATCH;
3034 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3035 return CMD_SUCCESS;
3036}
3037
3038DEFUN(no_config_log_monitor,
3039 no_config_log_monitor_cmd,
3040 "no log monitor [LEVEL]",
3041 NO_STR
3042 "Logging control\n"
3043 "Disable terminal line (monitor) logging\n" "Logging level\n")
3044{
3045 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3046 return CMD_SUCCESS;
3047}
3048
3049static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3050{
3051 int ret;
3052 char *p = NULL;
3053 const char *fullpath;
3054
3055 /* Path detection. */
3056 if (!IS_DIRECTORY_SEP(*fname)) {
3057 char cwd[MAXPATHLEN + 1];
3058 cwd[MAXPATHLEN] = '\0';
3059
3060 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3061 zlog_err("config_log_file: Unable to alloc mem!");
3062 return CMD_WARNING;
3063 }
3064
3065 if ((p = _talloc_zero(tall_vcmd_ctx,
3066 strlen(cwd) + strlen(fname) + 2),
3067 "set_log_file")
3068 == NULL) {
3069 zlog_err("config_log_file: Unable to alloc mem!");
3070 return CMD_WARNING;
3071 }
3072 sprintf(p, "%s/%s", cwd, fname);
3073 fullpath = p;
3074 } else
3075 fullpath = fname;
3076
3077 ret = zlog_set_file(NULL, fullpath, loglevel);
3078
3079 if (p)
3080 talloc_free(p);
3081
3082 if (!ret) {
3083 vty_out(vty, "can't open logfile %s\n", fname);
3084 return CMD_WARNING;
3085 }
3086
3087 if (host.logfile)
3088 talloc_free(host.logfile);
3089
3090 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3091
3092 return CMD_SUCCESS;
3093}
3094
3095DEFUN(config_log_file,
3096 config_log_file_cmd,
3097 "log file FILENAME",
3098 "Logging control\n" "Logging to file\n" "Logging filename\n")
3099{
3100 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3101}
3102
3103DEFUN(config_log_file_level,
3104 config_log_file_level_cmd,
3105 "log file FILENAME " LOG_LEVELS,
3106 "Logging control\n"
3107 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3108{
3109 int level;
3110
3111 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3112 return CMD_ERR_NO_MATCH;
3113 return set_log_file(vty, argv[0], level);
3114}
3115
3116DEFUN(no_config_log_file,
3117 no_config_log_file_cmd,
3118 "no log file [FILENAME]",
3119 NO_STR
3120 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3121{
3122 zlog_reset_file(NULL);
3123
3124 if (host.logfile)
3125 talloc_free(host.logfile);
3126
3127 host.logfile = NULL;
3128
3129 return CMD_SUCCESS;
3130}
3131
3132ALIAS(no_config_log_file,
3133 no_config_log_file_level_cmd,
3134 "no log file FILENAME LEVEL",
3135 NO_STR
3136 "Logging control\n"
3137 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3138
3139 DEFUN(config_log_syslog,
3140 config_log_syslog_cmd,
3141 "log syslog", "Logging control\n" "Set syslog logging level\n")
3142{
3143 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3144 return CMD_SUCCESS;
3145}
3146
3147DEFUN(config_log_syslog_level,
3148 config_log_syslog_level_cmd,
3149 "log syslog " LOG_LEVELS,
3150 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3151{
3152 int level;
3153
3154 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3155 return CMD_ERR_NO_MATCH;
3156 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3157 return CMD_SUCCESS;
3158}
3159
3160DEFUN_DEPRECATED(config_log_syslog_facility,
3161 config_log_syslog_facility_cmd,
3162 "log syslog facility " LOG_FACILITIES,
3163 "Logging control\n"
3164 "Logging goes to syslog\n"
3165 "(Deprecated) Facility parameter for syslog messages\n"
3166 LOG_FACILITY_DESC)
3167{
3168 int facility;
3169
3170 if ((facility = facility_match(argv[0])) < 0)
3171 return CMD_ERR_NO_MATCH;
3172
3173 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3174 zlog_default->facility = facility;
3175 return CMD_SUCCESS;
3176}
3177
3178DEFUN(no_config_log_syslog,
3179 no_config_log_syslog_cmd,
3180 "no log syslog [LEVEL]",
3181 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3182{
3183 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3184 return CMD_SUCCESS;
3185}
3186
3187ALIAS(no_config_log_syslog,
3188 no_config_log_syslog_facility_cmd,
3189 "no log syslog facility " LOG_FACILITIES,
3190 NO_STR
3191 "Logging control\n"
3192 "Logging goes to syslog\n"
3193 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3194
3195 DEFUN(config_log_facility,
3196 config_log_facility_cmd,
3197 "log facility " LOG_FACILITIES,
3198 "Logging control\n"
3199 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3200{
3201 int facility;
3202
3203 if ((facility = facility_match(argv[0])) < 0)
3204 return CMD_ERR_NO_MATCH;
3205 zlog_default->facility = facility;
3206 return CMD_SUCCESS;
3207}
3208
3209DEFUN(no_config_log_facility,
3210 no_config_log_facility_cmd,
3211 "no log facility [FACILITY]",
3212 NO_STR
3213 "Logging control\n"
3214 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3215{
3216 zlog_default->facility = LOG_DAEMON;
3217 return CMD_SUCCESS;
3218}
3219
3220DEFUN_DEPRECATED(config_log_trap,
3221 config_log_trap_cmd,
3222 "log trap " LOG_LEVELS,
3223 "Logging control\n"
3224 "(Deprecated) Set logging level and default for all destinations\n"
3225 LOG_LEVEL_DESC)
3226{
3227 int new_level;
3228 int i;
3229
3230 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3231 return CMD_ERR_NO_MATCH;
3232
3233 zlog_default->default_lvl = new_level;
3234 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3235 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3236 zlog_default->maxlvl[i] = new_level;
3237 return CMD_SUCCESS;
3238}
3239
3240DEFUN_DEPRECATED(no_config_log_trap,
3241 no_config_log_trap_cmd,
3242 "no log trap [LEVEL]",
3243 NO_STR
3244 "Logging control\n"
3245 "Permit all logging information\n" "Logging level\n")
3246{
3247 zlog_default->default_lvl = LOG_DEBUG;
3248 return CMD_SUCCESS;
3249}
3250
3251DEFUN(config_log_record_priority,
3252 config_log_record_priority_cmd,
3253 "log record-priority",
3254 "Logging control\n"
3255 "Log the priority of the message within the message\n")
3256{
3257 zlog_default->record_priority = 1;
3258 return CMD_SUCCESS;
3259}
3260
3261DEFUN(no_config_log_record_priority,
3262 no_config_log_record_priority_cmd,
3263 "no log record-priority",
3264 NO_STR
3265 "Logging control\n"
3266 "Do not log the priority of the message within the message\n")
3267{
3268 zlog_default->record_priority = 0;
3269 return CMD_SUCCESS;
3270}
3271#endif
3272
3273DEFUN(banner_motd_file,
3274 banner_motd_file_cmd,
3275 "banner motd file [FILE]",
3276 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3277{
3278 if (host.motdfile)
3279 talloc_free(host.motdfile);
3280 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3281
3282 return CMD_SUCCESS;
3283}
3284
3285DEFUN(banner_motd_default,
3286 banner_motd_default_cmd,
3287 "banner motd default",
3288 "Set banner string\n" "Strings for motd\n" "Default string\n")
3289{
3290 host.motd = default_motd;
3291 return CMD_SUCCESS;
3292}
3293
3294DEFUN(no_banner_motd,
3295 no_banner_motd_cmd,
3296 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3297{
3298 host.motd = NULL;
3299 if (host.motdfile)
3300 talloc_free(host.motdfile);
3301 host.motdfile = NULL;
3302 return CMD_SUCCESS;
3303}
3304
3305/* Set config filename. Called from vty.c */
3306void host_config_set(const char *filename)
3307{
3308 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3309}
3310
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003311void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003312{
3313 install_element(node, &config_help_cmd);
3314 install_element(node, &config_list_cmd);
3315
3316 install_element(node, &config_write_terminal_cmd);
3317 install_element(node, &config_write_file_cmd);
3318 install_element(node, &config_write_memory_cmd);
3319 install_element(node, &config_write_cmd);
3320 install_element(node, &show_running_config_cmd);
3321}
3322
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003323void vty_install_default(int node)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003324{
3325 install_default(node);
3326
3327 install_element(node, &config_exit_cmd);
3328
3329 if (node >= CONFIG_NODE) {
3330 /* It's not a top node. */
3331 install_element(node, &config_end_cmd);
3332 }
3333}
3334
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003335/**
3336 * \brief Write the current running config to a given file
3337 * \param[in] vty the vty of the code
3338 * \param[in] filename where to store the file
3339 * \return 0 in case of success.
3340 *
3341 * If the filename already exists create a filename.sav
3342 * version with the current code.
3343 *
3344 */
3345int osmo_vty_write_config_file(const char *filename)
3346{
3347 char *failed_file;
3348 int rc;
3349
3350 rc = write_config_file(filename, &failed_file);
3351 talloc_free(failed_file);
3352 return rc;
3353}
3354
3355/**
3356 * \brief Save the current state to the config file
3357 * \return 0 in case of success.
3358 *
3359 * If the filename already exists create a filename.sav
3360 * version with the current code.
3361 *
3362 */
3363int osmo_vty_save_config_file(void)
3364{
3365 char *failed_file;
3366 int rc;
3367
3368 if (host.config == NULL)
3369 return -7;
3370
3371 rc = write_config_file(host.config, &failed_file);
3372 talloc_free(failed_file);
3373 return rc;
3374}
3375
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003376/* Initialize command interface. Install basic nodes and commands. */
3377void cmd_init(int terminal)
3378{
3379 /* Allocate initial top vector of commands. */
3380 cmdvec = vector_init(VECTOR_MIN_SIZE);
3381
3382 /* Default host value settings. */
3383 host.name = NULL;
3384 host.password = NULL;
3385 host.enable = NULL;
3386 host.logfile = NULL;
3387 host.config = NULL;
3388 host.lines = -1;
3389 host.motd = default_motd;
3390 host.motdfile = NULL;
3391
3392 /* Install top nodes. */
3393 install_node(&view_node, NULL);
3394 install_node(&enable_node, NULL);
3395 install_node(&auth_node, NULL);
3396 install_node(&auth_enable_node, NULL);
3397 install_node(&config_node, config_write_host);
3398
3399 /* Each node's basic commands. */
3400 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003401 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003402 if (terminal) {
3403 install_element(VIEW_NODE, &config_list_cmd);
3404 install_element(VIEW_NODE, &config_exit_cmd);
3405 install_element(VIEW_NODE, &config_help_cmd);
3406 install_element(VIEW_NODE, &config_enable_cmd);
3407 install_element(VIEW_NODE, &config_terminal_length_cmd);
3408 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3409 install_element(VIEW_NODE, &echo_cmd);
3410 }
3411
3412 if (terminal) {
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003413 vty_install_default(ENABLE_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003414 install_element(ENABLE_NODE, &config_disable_cmd);
3415 install_element(ENABLE_NODE, &config_terminal_cmd);
3416 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3417 }
3418 install_element (ENABLE_NODE, &show_startup_config_cmd);
3419 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003420 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003421
3422 if (terminal) {
3423 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3424 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3425 install_element(ENABLE_NODE, &echo_cmd);
3426
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003427 vty_install_default(CONFIG_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003428 }
3429
3430 install_element(CONFIG_NODE, &hostname_cmd);
3431 install_element(CONFIG_NODE, &no_hostname_cmd);
3432
3433 if (terminal) {
3434 install_element(CONFIG_NODE, &password_cmd);
3435 install_element(CONFIG_NODE, &password_text_cmd);
3436 install_element(CONFIG_NODE, &enable_password_cmd);
3437 install_element(CONFIG_NODE, &enable_password_text_cmd);
3438 install_element(CONFIG_NODE, &no_enable_password_cmd);
3439
3440#ifdef VTY_CRYPT_PW
3441 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3442 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3443#endif
3444 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3445 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3446 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3447 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3448 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3449
3450 }
3451 srand(time(NULL));
3452}
Harald Welte7acb30c2011-08-17 17:13:48 +02003453
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003454/*! @} */