blob: 587bd6263ea3a9f66eb521de66c3a62857e0e376 [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
Alexander Couzensad580ba2016-05-16 16:01:45 +0200405/*!
406 * \brief escape all special asciidoc symbols
407 * \param unsafe string
408 * \return a new talloc char *
409 */
410char *osmo_asciidoc_escape(const char *inp)
411{
412 int _strlen;
413 char *out, *out_ptr;
414 int len = 0, i, j;
415
416 if (!inp)
417 return NULL;
418 _strlen = strlen(inp);
419
420 for (i = 0; i < _strlen; ++i) {
421 switch (inp[i]) {
422 case '|':
423 len += 2;
424 break;
425 default:
426 len += 1;
427 break;
428 }
429 }
430
431 out = talloc_size(NULL, len + 1);
432 if (!out)
433 return NULL;
434
435 out_ptr = out;
436
437#define ADD(out, str) \
438 for (j = 0; j < strlen(str); ++j) \
439 *(out++) = str[j];
440
441 for (i = 0; i < _strlen; ++i) {
442 switch (inp[i]) {
443 case '|':
444 ADD(out_ptr, "\\|");
445 break;
446 default:
447 *(out_ptr++) = inp[i];
448 break;
449 }
450 }
451
452#undef ADD
453
454 out_ptr[0] = '\0';
455 return out;
456}
457
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100458static char *xml_escape(const char *inp)
459{
460 int _strlen;
461 char *out, *out_ptr;
462 int len = 0, i, j;
463
464 if (!inp)
465 return NULL;
466 _strlen = strlen(inp);
467
468 for (i = 0; i < _strlen; ++i) {
469 switch (inp[i]) {
470 case '"':
471 len += 6;
472 break;
473 case '\'':
474 len += 6;
475 break;
476 case '<':
477 len += 4;
478 break;
479 case '>':
480 len += 4;
481 break;
482 case '&':
483 len += 5;
484 break;
485 default:
486 len += 1;
487 break;
488 }
489 }
490
491 out = talloc_size(NULL, len + 1);
492 if (!out)
493 return NULL;
494
495 out_ptr = out;
496
497#define ADD(out, str) \
498 for (j = 0; j < strlen(str); ++j) \
499 *(out++) = str[j];
500
501 for (i = 0; i < _strlen; ++i) {
502 switch (inp[i]) {
503 case '"':
504 ADD(out_ptr, "&quot;");
505 break;
506 case '\'':
507 ADD(out_ptr, "&apos;");
508 break;
509 case '<':
510 ADD(out_ptr, "&lt;");
511 break;
512 case '>':
513 ADD(out_ptr, "&gt;");
514 break;
515 case '&':
516 ADD(out_ptr, "&amp;");
517 break;
518 default:
519 *(out_ptr++) = inp[i];
520 break;
521 }
522 }
523
524#undef ADD
525
526 out_ptr[0] = '\0';
527 return out;
528}
529
530/*
531 * Write one cmd_element as XML to the given VTY.
532 */
533static int vty_dump_element(struct cmd_element *cmd, struct vty *vty)
534{
535 char *xml_string = xml_escape(cmd->string);
536
537 vty_out(vty, " <command id='%s'>%s", xml_string, VTY_NEWLINE);
538 vty_out(vty, " <params>%s", VTY_NEWLINE);
539
540 int j;
541 for (j = 0; j < vector_count(cmd->strvec); ++j) {
542 vector descvec = vector_slot(cmd->strvec, j);
543 int i;
544 for (i = 0; i < vector_active(descvec); ++i) {
545 char *xml_param, *xml_doc;
546 struct desc *desc = vector_slot(descvec, i);
547 if (desc == NULL)
548 continue;
549
550 xml_param = xml_escape(desc->cmd);
551 xml_doc = xml_escape(desc->str);
552 vty_out(vty, " <param name='%s' doc='%s' />%s",
553 xml_param, xml_doc, VTY_NEWLINE);
554 talloc_free(xml_param);
555 talloc_free(xml_doc);
556 }
557 }
558
559 vty_out(vty, " </params>%s", VTY_NEWLINE);
560 vty_out(vty, " </command>%s", VTY_NEWLINE);
561
562 talloc_free(xml_string);
563 return 0;
564}
565
566/*
567 * Dump all nodes and commands associated with a given node as XML to the VTY.
568 */
569static int vty_dump_nodes(struct vty *vty)
570{
571 int i, j;
572
573 vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
574
575 for (i = 0; i < vector_active(cmdvec); ++i) {
576 struct cmd_node *cnode;
577 cnode = vector_slot(cmdvec, i);
578 if (!cnode)
579 continue;
580
581 vty_out(vty, " <node id='%d'>%s", i, VTY_NEWLINE);
582
583 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
584 struct cmd_element *elem;
585 elem = vector_slot(cnode->cmd_vector, j);
586 vty_dump_element(elem, vty);
587 }
588
589 vty_out(vty, " </node>%s", VTY_NEWLINE);
590 }
591
592 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
593
594 return 0;
595}
596
Harald Welteaddeaa32017-01-07 12:52:00 +0100597/* \brief Check if a command with given string exists at given node */
598static int check_element_exists(struct cmd_node *cnode, const char *cmdstring)
599{
600 int i;
601
602 for (i = 0; i < vector_active(cnode->cmd_vector); ++i) {
603 struct cmd_element *elem;
604 elem = vector_slot(cnode->cmd_vector, i);
605 if (!elem->string)
606 continue;
607 if (!strcmp(elem->string, cmdstring))
608 return 1;
609 }
610 return 0;
611}
612
Harald Welte7acb30c2011-08-17 17:13:48 +0200613/*! \brief Install a command into a node
614 * \param[in] ntype Node Type
615 * \param[cmd] element to be installed
616 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000617void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200618{
619 struct cmd_node *cnode;
620
621 cnode = vector_slot(cmdvec, ntype);
622
Harald Weltea99d45a2015-11-12 13:48:23 +0100623 OSMO_ASSERT(cnode);
Harald Welteaddeaa32017-01-07 12:52:00 +0100624 /* ensure no _identical_ command has been registered at this
625 * node so far */
626 OSMO_ASSERT(!check_element_exists(cnode, cmd->string));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200627
628 vector_set(cnode->cmd_vector, cmd);
629
630 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
631 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
632}
633
634/* Install a command into VIEW and ENABLE node */
635void install_element_ve(struct cmd_element *cmd)
636{
637 install_element(VIEW_NODE, cmd);
638 install_element(ENABLE_NODE, cmd);
639}
640
641#ifdef VTY_CRYPT_PW
642static unsigned char itoa64[] =
643 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
644
645static void to64(char *s, long v, int n)
646{
647 while (--n >= 0) {
648 *s++ = itoa64[v & 0x3f];
649 v >>= 6;
650 }
651}
652
653static char *zencrypt(const char *passwd)
654{
655 char salt[6];
656 struct timeval tv;
657 char *crypt(const char *, const char *);
658
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200659 osmo_gettimeofday(&tv, 0);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200660
661 to64(&salt[0], random(), 3);
662 to64(&salt[3], tv.tv_usec, 3);
663 salt[5] = '\0';
664
665 return crypt(passwd, salt);
666}
667#endif
668
669/* This function write configuration of this host. */
670static int config_write_host(struct vty *vty)
671{
672 if (host.name)
673 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
674
675 if (host.encrypt) {
676 if (host.password_encrypt)
677 vty_out(vty, "password 8 %s%s", host.password_encrypt,
678 VTY_NEWLINE);
679 if (host.enable_encrypt)
680 vty_out(vty, "enable password 8 %s%s",
681 host.enable_encrypt, VTY_NEWLINE);
682 } else {
683 if (host.password)
684 vty_out(vty, "password %s%s", host.password,
685 VTY_NEWLINE);
686 if (host.enable)
687 vty_out(vty, "enable password %s%s", host.enable,
688 VTY_NEWLINE);
689 }
690
691 if (host.advanced)
692 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
693
694 if (host.encrypt)
695 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
696
697 if (host.lines >= 0)
698 vty_out(vty, "service terminal-length %d%s", host.lines,
699 VTY_NEWLINE);
700
701 if (host.motdfile)
702 vty_out(vty, "banner motd file %s%s", host.motdfile,
703 VTY_NEWLINE);
704 else if (!host.motd)
705 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
706
707 return 1;
708}
709
710/* Utility function for getting command vector. */
711static vector cmd_node_vector(vector v, enum node_type ntype)
712{
713 struct cmd_node *cnode = vector_slot(v, ntype);
714 return cnode->cmd_vector;
715}
716
717/* Completion match types. */
718enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100719 no_match = 0,
720 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200721 extend_match,
722 ipv4_prefix_match,
723 ipv4_match,
724 ipv6_prefix_match,
725 ipv6_match,
726 range_match,
727 vararg_match,
728 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100729 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200730};
731
732static enum match_type cmd_ipv4_match(const char *str)
733{
734 const char *sp;
735 int dots = 0, nums = 0;
736 char buf[4];
737
738 if (str == NULL)
739 return partly_match;
740
741 for (;;) {
742 memset(buf, 0, sizeof(buf));
743 sp = str;
744 while (*str != '\0') {
745 if (*str == '.') {
746 if (dots >= 3)
747 return no_match;
748
749 if (*(str + 1) == '.')
750 return no_match;
751
752 if (*(str + 1) == '\0')
753 return partly_match;
754
755 dots++;
756 break;
757 }
758 if (!isdigit((int)*str))
759 return no_match;
760
761 str++;
762 }
763
764 if (str - sp > 3)
765 return no_match;
766
767 strncpy(buf, sp, str - sp);
768 if (atoi(buf) > 255)
769 return no_match;
770
771 nums++;
772
773 if (*str == '\0')
774 break;
775
776 str++;
777 }
778
779 if (nums < 4)
780 return partly_match;
781
782 return exact_match;
783}
784
785static enum match_type cmd_ipv4_prefix_match(const char *str)
786{
787 const char *sp;
788 int dots = 0;
789 char buf[4];
790
791 if (str == NULL)
792 return partly_match;
793
794 for (;;) {
795 memset(buf, 0, sizeof(buf));
796 sp = str;
797 while (*str != '\0' && *str != '/') {
798 if (*str == '.') {
799 if (dots == 3)
800 return no_match;
801
802 if (*(str + 1) == '.' || *(str + 1) == '/')
803 return no_match;
804
805 if (*(str + 1) == '\0')
806 return partly_match;
807
808 dots++;
809 break;
810 }
811
812 if (!isdigit((int)*str))
813 return no_match;
814
815 str++;
816 }
817
818 if (str - sp > 3)
819 return no_match;
820
821 strncpy(buf, sp, str - sp);
822 if (atoi(buf) > 255)
823 return no_match;
824
825 if (dots == 3) {
826 if (*str == '/') {
827 if (*(str + 1) == '\0')
828 return partly_match;
829
830 str++;
831 break;
832 } else if (*str == '\0')
833 return partly_match;
834 }
835
836 if (*str == '\0')
837 return partly_match;
838
839 str++;
840 }
841
842 sp = str;
843 while (*str != '\0') {
844 if (!isdigit((int)*str))
845 return no_match;
846
847 str++;
848 }
849
850 if (atoi(sp) > 32)
851 return no_match;
852
853 return exact_match;
854}
855
856#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
857#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
858#define STATE_START 1
859#define STATE_COLON 2
860#define STATE_DOUBLE 3
861#define STATE_ADDR 4
862#define STATE_DOT 5
863#define STATE_SLASH 6
864#define STATE_MASK 7
865
866#ifdef HAVE_IPV6
867
868static enum match_type cmd_ipv6_match(const char *str)
869{
870 int state = STATE_START;
871 int colons = 0, nums = 0, double_colon = 0;
872 const char *sp = NULL;
873 struct sockaddr_in6 sin6_dummy;
874 int ret;
875
876 if (str == NULL)
877 return partly_match;
878
879 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
880 return no_match;
881
882 /* use inet_pton that has a better support,
883 * for example inet_pton can support the automatic addresses:
884 * ::1.2.3.4
885 */
886 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
887
888 if (ret == 1)
889 return exact_match;
890
891 while (*str != '\0') {
892 switch (state) {
893 case STATE_START:
894 if (*str == ':') {
895 if (*(str + 1) != ':' && *(str + 1) != '\0')
896 return no_match;
897 colons--;
898 state = STATE_COLON;
899 } else {
900 sp = str;
901 state = STATE_ADDR;
902 }
903
904 continue;
905 case STATE_COLON:
906 colons++;
907 if (*(str + 1) == ':')
908 state = STATE_DOUBLE;
909 else {
910 sp = str + 1;
911 state = STATE_ADDR;
912 }
913 break;
914 case STATE_DOUBLE:
915 if (double_colon)
916 return no_match;
917
918 if (*(str + 1) == ':')
919 return no_match;
920 else {
921 if (*(str + 1) != '\0')
922 colons++;
923 sp = str + 1;
924 state = STATE_ADDR;
925 }
926
927 double_colon++;
928 nums++;
929 break;
930 case STATE_ADDR:
931 if (*(str + 1) == ':' || *(str + 1) == '\0') {
932 if (str - sp > 3)
933 return no_match;
934
935 nums++;
936 state = STATE_COLON;
937 }
938 if (*(str + 1) == '.')
939 state = STATE_DOT;
940 break;
941 case STATE_DOT:
942 state = STATE_ADDR;
943 break;
944 default:
945 break;
946 }
947
948 if (nums > 8)
949 return no_match;
950
951 if (colons > 7)
952 return no_match;
953
954 str++;
955 }
956
957#if 0
958 if (nums < 11)
959 return partly_match;
960#endif /* 0 */
961
962 return exact_match;
963}
964
965static enum match_type cmd_ipv6_prefix_match(const char *str)
966{
967 int state = STATE_START;
968 int colons = 0, nums = 0, double_colon = 0;
969 int mask;
970 const char *sp = NULL;
971 char *endptr = NULL;
972
973 if (str == NULL)
974 return partly_match;
975
976 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
977 return no_match;
978
979 while (*str != '\0' && state != STATE_MASK) {
980 switch (state) {
981 case STATE_START:
982 if (*str == ':') {
983 if (*(str + 1) != ':' && *(str + 1) != '\0')
984 return no_match;
985 colons--;
986 state = STATE_COLON;
987 } else {
988 sp = str;
989 state = STATE_ADDR;
990 }
991
992 continue;
993 case STATE_COLON:
994 colons++;
995 if (*(str + 1) == '/')
996 return no_match;
997 else if (*(str + 1) == ':')
998 state = STATE_DOUBLE;
999 else {
1000 sp = str + 1;
1001 state = STATE_ADDR;
1002 }
1003 break;
1004 case STATE_DOUBLE:
1005 if (double_colon)
1006 return no_match;
1007
1008 if (*(str + 1) == ':')
1009 return no_match;
1010 else {
1011 if (*(str + 1) != '\0' && *(str + 1) != '/')
1012 colons++;
1013 sp = str + 1;
1014
1015 if (*(str + 1) == '/')
1016 state = STATE_SLASH;
1017 else
1018 state = STATE_ADDR;
1019 }
1020
1021 double_colon++;
1022 nums += 1;
1023 break;
1024 case STATE_ADDR:
1025 if (*(str + 1) == ':' || *(str + 1) == '.'
1026 || *(str + 1) == '\0' || *(str + 1) == '/') {
1027 if (str - sp > 3)
1028 return no_match;
1029
1030 for (; sp <= str; sp++)
1031 if (*sp == '/')
1032 return no_match;
1033
1034 nums++;
1035
1036 if (*(str + 1) == ':')
1037 state = STATE_COLON;
1038 else if (*(str + 1) == '.')
1039 state = STATE_DOT;
1040 else if (*(str + 1) == '/')
1041 state = STATE_SLASH;
1042 }
1043 break;
1044 case STATE_DOT:
1045 state = STATE_ADDR;
1046 break;
1047 case STATE_SLASH:
1048 if (*(str + 1) == '\0')
1049 return partly_match;
1050
1051 state = STATE_MASK;
1052 break;
1053 default:
1054 break;
1055 }
1056
1057 if (nums > 11)
1058 return no_match;
1059
1060 if (colons > 7)
1061 return no_match;
1062
1063 str++;
1064 }
1065
1066 if (state < STATE_MASK)
1067 return partly_match;
1068
1069 mask = strtol(str, &endptr, 10);
1070 if (*endptr != '\0')
1071 return no_match;
1072
1073 if (mask < 0 || mask > 128)
1074 return no_match;
1075
1076/* I don't know why mask < 13 makes command match partly.
1077 Forgive me to make this comments. I Want to set static default route
1078 because of lack of function to originate default in ospf6d; sorry
1079 yasu
1080 if (mask < 13)
1081 return partly_match;
1082*/
1083
1084 return exact_match;
1085}
1086
1087#endif /* HAVE_IPV6 */
1088
1089#define DECIMAL_STRLEN_MAX 10
1090
1091static int cmd_range_match(const char *range, const char *str)
1092{
1093 char *p;
1094 char buf[DECIMAL_STRLEN_MAX + 1];
1095 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001096
1097 if (str == NULL)
1098 return 1;
1099
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001100 if (range[1] == '-') {
1101 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001102
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001103 val = strtol(str, &endptr, 10);
1104 if (*endptr != '\0')
1105 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001106
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001107 range += 2;
1108 p = strchr(range, '-');
1109 if (p == NULL)
1110 return 0;
1111 if (p - range > DECIMAL_STRLEN_MAX)
1112 return 0;
1113 strncpy(buf, range, p - range);
1114 buf[p - range] = '\0';
1115 min = -strtol(buf, &endptr, 10);
1116 if (*endptr != '\0')
1117 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001118
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001119 range = p + 1;
1120 p = strchr(range, '>');
1121 if (p == NULL)
1122 return 0;
1123 if (p - range > DECIMAL_STRLEN_MAX)
1124 return 0;
1125 strncpy(buf, range, p - range);
1126 buf[p - range] = '\0';
1127 max = strtol(buf, &endptr, 10);
1128 if (*endptr != '\0')
1129 return 0;
1130
1131 if (val < min || val > max)
1132 return 0;
1133 } else {
1134 unsigned long min, max, val;
1135
1136 val = strtoul(str, &endptr, 10);
1137 if (*endptr != '\0')
1138 return 0;
1139
1140 range++;
1141 p = strchr(range, '-');
1142 if (p == NULL)
1143 return 0;
1144 if (p - range > DECIMAL_STRLEN_MAX)
1145 return 0;
1146 strncpy(buf, range, p - range);
1147 buf[p - range] = '\0';
1148 min = strtoul(buf, &endptr, 10);
1149 if (*endptr != '\0')
1150 return 0;
1151
1152 range = p + 1;
1153 p = strchr(range, '>');
1154 if (p == NULL)
1155 return 0;
1156 if (p - range > DECIMAL_STRLEN_MAX)
1157 return 0;
1158 strncpy(buf, range, p - range);
1159 buf[p - range] = '\0';
1160 max = strtoul(buf, &endptr, 10);
1161 if (*endptr != '\0')
1162 return 0;
1163
1164 if (val < min || val > max)
1165 return 0;
1166 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001167
1168 return 1;
1169}
1170
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001171/* helper to retrieve the 'real' argument string from an optional argument */
1172static char *
1173cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001174{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001175 /* we've got "[blah]". We want to strip off the []s and redo the
1176 * match check for "blah"
1177 */
1178 size_t len = strlen(str);
1179 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001180
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001181 if (len < 3)
1182 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001183
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001184 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1185 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001186
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001187 memcpy(tmp, (str + 1), len - 2);
1188 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001189
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001190 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001191}
1192
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001193static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001194cmd_match(const char *str, const char *command,
1195 enum match_type min, bool recur)
1196{
1197
1198 if (recur && CMD_OPTION(str))
1199 {
1200 enum match_type ret;
1201 char *tmp = cmd_deopt(str);
1202
1203 /* this would be a bug in a command, however handle it gracefully
1204 * as it we only discover it if a user tries to run it
1205 */
1206 if (tmp == NULL)
1207 return no_match;
1208
1209 ret = cmd_match(tmp, command, min, false);
1210
1211 talloc_free(tmp);
1212
1213 return ret;
1214 }
1215 else if (CMD_VARARG(str))
1216 return vararg_match;
1217 else if (CMD_RANGE(str))
1218 {
1219 if (cmd_range_match(str, command))
1220 return range_match;
1221 }
1222#ifdef HAVE_IPV6
1223 else if (CMD_IPV6(str))
1224 {
1225 if (cmd_ipv6_match(command) >= min)
1226 return ipv6_match;
1227 }
1228 else if (CMD_IPV6_PREFIX(str))
1229 {
1230 if (cmd_ipv6_prefix_match(command) >= min)
1231 return ipv6_prefix_match;
1232 }
1233#endif /* HAVE_IPV6 */
1234 else if (CMD_IPV4(str))
1235 {
1236 if (cmd_ipv4_match(command) >= min)
1237 return ipv4_match;
1238 }
1239 else if (CMD_IPV4_PREFIX(str))
1240 {
1241 if (cmd_ipv4_prefix_match(command) >= min)
1242 return ipv4_prefix_match;
1243 }
1244 else if (CMD_VARIABLE(str))
1245 return extend_match;
1246 else if (strncmp(command, str, strlen(command)) == 0)
1247 {
1248 if (strcmp(command, str) == 0)
1249 return exact_match;
1250 else if (partly_match >= min)
1251 return partly_match;
1252 }
1253
1254 return no_match;
1255}
1256
1257/* Filter vector at the specified index and by the given command string, to
1258 * the desired matching level (thus allowing part matches), and return match
1259 * type flag.
1260 */
1261static enum match_type
1262cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001263{
1264 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001265 struct cmd_element *cmd_element;
1266 enum match_type match_type;
1267 vector descvec;
1268 struct desc *desc;
1269
1270 match_type = no_match;
1271
1272 /* If command and cmd_element string does not match set NULL to vector */
1273 for (i = 0; i < vector_active(v); i++)
1274 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001275 if (index >= vector_active(cmd_element->strvec))
1276 vector_slot(v, i) = NULL;
1277 else {
1278 unsigned int j;
1279 int matched = 0;
1280
1281 descvec =
1282 vector_slot(cmd_element->strvec, index);
1283
1284 for (j = 0; j < vector_active(descvec); j++)
1285 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001286 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001287
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001288 ret = cmd_match (desc->cmd, command, level, true);
1289
1290 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001291 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001292
1293 if (match_type < ret)
1294 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001295 }
1296 if (!matched)
1297 vector_slot(v, i) = NULL;
1298 }
1299 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001300
1301 if (match_type == no_match)
1302 return no_match;
1303
1304 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1305 * go again and filter out commands whose argument (at this index) is
1306 * 'weaker'. E.g., if we have 2 commands:
1307 *
1308 * foo bar <1-255>
1309 * foo bar BLAH
1310 *
1311 * and the command string is 'foo bar 10', then we will get here with with
1312 * 'range_match' being the strongest match. However, if 'BLAH' came
1313 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1314 *
1315 * If we don't do a 2nd pass and filter it out, the higher-layers will
1316 * consider this to be ambiguous.
1317 */
1318 for (i = 0; i < vector_active(v); i++)
1319 if ((cmd_element = vector_slot(v, i)) != NULL) {
1320 if (index >= vector_active(cmd_element->strvec))
1321 vector_slot(v, i) = NULL;
1322 else {
1323 unsigned int j;
1324 int matched = 0;
1325
1326 descvec =
1327 vector_slot(cmd_element->strvec, index);
1328
1329 for (j = 0; j < vector_active(descvec); j++)
1330 if ((desc = vector_slot(descvec, j))) {
1331 enum match_type ret;
1332
1333 ret = cmd_match(desc->cmd, command, any_match, true);
1334
1335 if (ret >= match_type)
1336 matched++;
1337 }
1338 if (!matched)
1339 vector_slot(v, i) = NULL;
1340 }
1341 }
1342
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001343 return match_type;
1344}
1345
1346/* Check ambiguous match */
1347static int
1348is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1349{
1350 unsigned int i;
1351 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001352 struct cmd_element *cmd_element;
1353 const char *matched = NULL;
1354 vector descvec;
1355 struct desc *desc;
1356
1357 for (i = 0; i < vector_active(v); i++)
1358 if ((cmd_element = vector_slot(v, i)) != NULL) {
1359 int match = 0;
1360
1361 descvec = vector_slot(cmd_element->strvec, index);
1362
1363 for (j = 0; j < vector_active(descvec); j++)
1364 if ((desc = vector_slot(descvec, j))) {
1365 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001366 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001367
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001368 if (CMD_OPTION(str))
1369 if ((str = cmd_deopt(str)) == NULL)
1370 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001371
1372 switch (type) {
1373 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001374 if (!(CMD_VARIABLE (str))
1375 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001376 match++;
1377 break;
1378 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001379 if (!(CMD_VARIABLE (str))
1380 && strncmp(command, str, strlen (command)) == 0)
1381 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001382 if (matched
1383 && strcmp(matched,
1384 str) != 0)
1385 return 1; /* There is ambiguous match. */
1386 else
1387 matched = str;
1388 match++;
1389 }
1390 break;
1391 case range_match:
1392 if (cmd_range_match
1393 (str, command)) {
1394 if (matched
1395 && strcmp(matched,
1396 str) != 0)
1397 return 1;
1398 else
1399 matched = str;
1400 match++;
1401 }
1402 break;
1403#ifdef HAVE_IPV6
1404 case ipv6_match:
1405 if (CMD_IPV6(str))
1406 match++;
1407 break;
1408 case ipv6_prefix_match:
1409 if ((ret =
1410 cmd_ipv6_prefix_match
1411 (command)) != no_match) {
1412 if (ret == partly_match)
1413 return 2; /* There is incomplete match. */
1414
1415 match++;
1416 }
1417 break;
1418#endif /* HAVE_IPV6 */
1419 case ipv4_match:
1420 if (CMD_IPV4(str))
1421 match++;
1422 break;
1423 case ipv4_prefix_match:
1424 if ((ret =
1425 cmd_ipv4_prefix_match
1426 (command)) != no_match) {
1427 if (ret == partly_match)
1428 return 2; /* There is incomplete match. */
1429
1430 match++;
1431 }
1432 break;
1433 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001434 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001435 match++;
1436 break;
1437 case no_match:
1438 default:
1439 break;
1440 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001441
1442 if (CMD_OPTION(desc->cmd))
1443 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001444 }
1445 if (!match)
1446 vector_slot(v, i) = NULL;
1447 }
1448 return 0;
1449}
1450
1451/* If src matches dst return dst string, otherwise return NULL */
1452static const char *cmd_entry_function(const char *src, const char *dst)
1453{
1454 /* Skip variable arguments. */
1455 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1456 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1457 return NULL;
1458
1459 /* In case of 'command \t', given src is NULL string. */
1460 if (src == NULL)
1461 return dst;
1462
1463 /* Matched with input string. */
1464 if (strncmp(src, dst, strlen(src)) == 0)
1465 return dst;
1466
1467 return NULL;
1468}
1469
1470/* If src matches dst return dst string, otherwise return NULL */
1471/* This version will return the dst string always if it is
1472 CMD_VARIABLE for '?' key processing */
1473static const char *cmd_entry_function_desc(const char *src, const char *dst)
1474{
1475 if (CMD_VARARG(dst))
1476 return dst;
1477
1478 if (CMD_RANGE(dst)) {
1479 if (cmd_range_match(dst, src))
1480 return dst;
1481 else
1482 return NULL;
1483 }
1484#ifdef HAVE_IPV6
1485 if (CMD_IPV6(dst)) {
1486 if (cmd_ipv6_match(src))
1487 return dst;
1488 else
1489 return NULL;
1490 }
1491
1492 if (CMD_IPV6_PREFIX(dst)) {
1493 if (cmd_ipv6_prefix_match(src))
1494 return dst;
1495 else
1496 return NULL;
1497 }
1498#endif /* HAVE_IPV6 */
1499
1500 if (CMD_IPV4(dst)) {
1501 if (cmd_ipv4_match(src))
1502 return dst;
1503 else
1504 return NULL;
1505 }
1506
1507 if (CMD_IPV4_PREFIX(dst)) {
1508 if (cmd_ipv4_prefix_match(src))
1509 return dst;
1510 else
1511 return NULL;
1512 }
1513
1514 /* Optional or variable commands always match on '?' */
1515 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1516 return dst;
1517
1518 /* In case of 'command \t', given src is NULL string. */
1519 if (src == NULL)
1520 return dst;
1521
1522 if (strncmp(src, dst, strlen(src)) == 0)
1523 return dst;
1524 else
1525 return NULL;
1526}
1527
1528/* Check same string element existence. If it isn't there return
1529 1. */
1530static int cmd_unique_string(vector v, const char *str)
1531{
1532 unsigned int i;
1533 char *match;
1534
1535 for (i = 0; i < vector_active(v); i++)
1536 if ((match = vector_slot(v, i)) != NULL)
1537 if (strcmp(match, str) == 0)
1538 return 0;
1539 return 1;
1540}
1541
1542/* Compare string to description vector. If there is same string
1543 return 1 else return 0. */
1544static int desc_unique_string(vector v, const char *str)
1545{
1546 unsigned int i;
1547 struct desc *desc;
1548
1549 for (i = 0; i < vector_active(v); i++)
1550 if ((desc = vector_slot(v, i)) != NULL)
1551 if (strcmp(desc->cmd, str) == 0)
1552 return 1;
1553 return 0;
1554}
1555
1556static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1557{
1558 if (first_word != NULL &&
1559 node != AUTH_NODE &&
1560 node != VIEW_NODE &&
1561 node != AUTH_ENABLE_NODE &&
1562 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1563 return 1;
1564 return 0;
1565}
1566
1567/* '?' describe command support. */
1568static vector
1569cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1570{
1571 unsigned int i;
1572 vector cmd_vector;
1573#define INIT_MATCHVEC_SIZE 10
1574 vector matchvec;
1575 struct cmd_element *cmd_element;
1576 unsigned int index;
1577 int ret;
1578 enum match_type match;
1579 char *command;
1580 static struct desc desc_cr = { "<cr>", "" };
1581
1582 /* Set index. */
1583 if (vector_active(vline) == 0) {
1584 *status = CMD_ERR_NO_MATCH;
1585 return NULL;
1586 } else
1587 index = vector_active(vline) - 1;
1588
1589 /* Make copy vector of current node's command vector. */
1590 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1591
1592 /* Prepare match vector */
1593 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1594
1595 /* Filter commands. */
1596 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001597 for (i = 0; i < index; i++) {
1598 command = vector_slot(vline, i);
1599 if (!command)
1600 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001601
Harald Welte80d30fe2013-02-12 11:08:57 +01001602 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001603
Harald Welte80d30fe2013-02-12 11:08:57 +01001604 if (match == vararg_match) {
1605 struct cmd_element *cmd_element;
1606 vector descvec;
1607 unsigned int j, k;
1608
1609 for (j = 0; j < vector_active(cmd_vector); j++)
1610 if ((cmd_element =
1611 vector_slot(cmd_vector, j)) != NULL
1612 &&
1613 (vector_active(cmd_element->strvec))) {
1614 descvec =
1615 vector_slot(cmd_element->
1616 strvec,
1617 vector_active
1618 (cmd_element->
1619 strvec) - 1);
1620 for (k = 0;
1621 k < vector_active(descvec);
1622 k++) {
1623 struct desc *desc =
1624 vector_slot(descvec,
1625 k);
1626 vector_set(matchvec,
1627 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001628 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001629 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001630
Harald Welte80d30fe2013-02-12 11:08:57 +01001631 vector_set(matchvec, &desc_cr);
1632 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001633
Harald Welte80d30fe2013-02-12 11:08:57 +01001634 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001635 }
1636
Harald Welte80d30fe2013-02-12 11:08:57 +01001637 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1638 match)) == 1) {
1639 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001640 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001641 *status = CMD_ERR_AMBIGUOUS;
1642 return NULL;
1643 } else if (ret == 2) {
1644 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001645 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001646 *status = CMD_ERR_NO_MATCH;
1647 return NULL;
1648 }
1649 }
1650
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001651 /* Prepare match vector */
1652 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1653
1654 /* Make sure that cmd_vector is filtered based on current word */
1655 command = vector_slot(vline, index);
1656 if (command)
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001657 match = cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001658
1659 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001660 for (i = 0; i < vector_active(cmd_vector); i++) {
1661 const char *string = NULL;
1662 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001663
Harald Welte80d30fe2013-02-12 11:08:57 +01001664 cmd_element = vector_slot(cmd_vector, i);
1665 if (!cmd_element)
1666 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001667
Harald Welted17aa592013-02-12 11:11:34 +01001668 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1669 continue;
1670
Harald Welte80d30fe2013-02-12 11:08:57 +01001671 strvec = cmd_element->strvec;
1672
1673 /* if command is NULL, index may be equal to vector_active */
1674 if (command && index >= vector_active(strvec))
1675 vector_slot(cmd_vector, i) = NULL;
1676 else {
1677 /* Check if command is completed. */
1678 if (command == NULL
1679 && index == vector_active(strvec)) {
1680 string = "<cr>";
1681 if (!desc_unique_string(matchvec, string))
1682 vector_set(matchvec, &desc_cr);
1683 } else {
1684 unsigned int j;
1685 vector descvec = vector_slot(strvec, index);
1686 struct desc *desc;
1687
1688 for (j = 0; j < vector_active(descvec); j++) {
1689 desc = vector_slot(descvec, j);
1690 if (!desc)
1691 continue;
1692 string = cmd_entry_function_desc
1693 (command, desc->cmd);
1694 if (!string)
1695 continue;
1696 /* Uniqueness check */
1697 if (!desc_unique_string(matchvec, string))
1698 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001699 }
1700 }
1701 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001702 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001703 vector_free(cmd_vector);
1704
1705 if (vector_slot(matchvec, 0) == NULL) {
1706 vector_free(matchvec);
1707 *status = CMD_ERR_NO_MATCH;
1708 } else
1709 *status = CMD_SUCCESS;
1710
1711 return matchvec;
1712}
1713
1714vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1715{
1716 vector ret;
1717
1718 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1719 enum node_type onode;
1720 vector shifted_vline;
1721 unsigned int index;
1722
1723 onode = vty->node;
1724 vty->node = ENABLE_NODE;
1725 /* We can try it on enable node, cos' the vty is authenticated */
1726
1727 shifted_vline = vector_init(vector_count(vline));
1728 /* use memcpy? */
1729 for (index = 1; index < vector_active(vline); index++) {
1730 vector_set_index(shifted_vline, index - 1,
1731 vector_lookup(vline, index));
1732 }
1733
1734 ret = cmd_describe_command_real(shifted_vline, vty, status);
1735
1736 vector_free(shifted_vline);
1737 vty->node = onode;
1738 return ret;
1739 }
1740
1741 return cmd_describe_command_real(vline, vty, status);
1742}
1743
1744/* Check LCD of matched command. */
1745static int cmd_lcd(char **matched)
1746{
1747 int i;
1748 int j;
1749 int lcd = -1;
1750 char *s1, *s2;
1751 char c1, c2;
1752
1753 if (matched[0] == NULL || matched[1] == NULL)
1754 return 0;
1755
1756 for (i = 1; matched[i] != NULL; i++) {
1757 s1 = matched[i - 1];
1758 s2 = matched[i];
1759
1760 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1761 if (c1 != c2)
1762 break;
1763
1764 if (lcd < 0)
1765 lcd = j;
1766 else {
1767 if (lcd > j)
1768 lcd = j;
1769 }
1770 }
1771 return lcd;
1772}
1773
1774/* Command line completion support. */
1775static char **cmd_complete_command_real(vector vline, struct vty *vty,
1776 int *status)
1777{
1778 unsigned int i;
1779 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1780#define INIT_MATCHVEC_SIZE 10
1781 vector matchvec;
1782 struct cmd_element *cmd_element;
1783 unsigned int index;
1784 char **match_str;
1785 struct desc *desc;
1786 vector descvec;
1787 char *command;
1788 int lcd;
1789
1790 if (vector_active(vline) == 0) {
1791 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001792 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001793 return NULL;
1794 } else
1795 index = vector_active(vline) - 1;
1796
1797 /* First, filter by preceeding command string */
1798 for (i = 0; i < index; i++)
1799 if ((command = vector_slot(vline, i))) {
1800 enum match_type match;
1801 int ret;
1802
1803 /* First try completion match, if there is exactly match return 1 */
1804 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001805 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001806
1807 /* If there is exact match then filter ambiguous match else check
1808 ambiguousness. */
1809 if ((ret =
1810 is_cmd_ambiguous(command, cmd_vector, i,
1811 match)) == 1) {
1812 vector_free(cmd_vector);
1813 *status = CMD_ERR_AMBIGUOUS;
1814 return NULL;
1815 }
1816 /*
1817 else if (ret == 2)
1818 {
1819 vector_free (cmd_vector);
1820 *status = CMD_ERR_NO_MATCH;
1821 return NULL;
1822 }
1823 */
1824 }
1825
1826 /* Prepare match vector. */
1827 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1828
1829 /* Now we got into completion */
1830 for (i = 0; i < vector_active(cmd_vector); i++)
1831 if ((cmd_element = vector_slot(cmd_vector, i))) {
1832 const char *string;
1833 vector strvec = cmd_element->strvec;
1834
1835 /* Check field length */
1836 if (index >= vector_active(strvec))
1837 vector_slot(cmd_vector, i) = NULL;
1838 else {
1839 unsigned int j;
1840
1841 descvec = vector_slot(strvec, index);
1842 for (j = 0; j < vector_active(descvec); j++)
1843 if ((desc = vector_slot(descvec, j))) {
1844 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1845 if (cmd_unique_string (matchvec, string))
1846 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1847 }
1848 }
1849 }
1850
1851 /* We don't need cmd_vector any more. */
1852 vector_free(cmd_vector);
1853
1854 /* No matched command */
1855 if (vector_slot(matchvec, 0) == NULL) {
1856 vector_free(matchvec);
1857
1858 /* In case of 'command \t' pattern. Do you need '?' command at
1859 the end of the line. */
1860 if (vector_slot(vline, index) == '\0')
1861 *status = CMD_ERR_NOTHING_TODO;
1862 else
1863 *status = CMD_ERR_NO_MATCH;
1864 return NULL;
1865 }
1866
1867 /* Only one matched */
1868 if (vector_slot(matchvec, 1) == NULL) {
1869 match_str = (char **)matchvec->index;
1870 vector_only_wrapper_free(matchvec);
1871 *status = CMD_COMPLETE_FULL_MATCH;
1872 return match_str;
1873 }
1874 /* Make it sure last element is NULL. */
1875 vector_set(matchvec, NULL);
1876
1877 /* Check LCD of matched strings. */
1878 if (vector_slot(vline, index) != NULL) {
1879 lcd = cmd_lcd((char **)matchvec->index);
1880
1881 if (lcd) {
1882 int len = strlen(vector_slot(vline, index));
1883
1884 if (len < lcd) {
1885 char *lcdstr;
1886
1887 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1888 "complete-lcdstr");
1889 memcpy(lcdstr, matchvec->index[0], lcd);
1890 lcdstr[lcd] = '\0';
1891
1892 /* match_str = (char **) &lcdstr; */
1893
1894 /* Free matchvec. */
1895 for (i = 0; i < vector_active(matchvec); i++) {
1896 if (vector_slot(matchvec, i))
1897 talloc_free(vector_slot(matchvec, i));
1898 }
1899 vector_free(matchvec);
1900
1901 /* Make new matchvec. */
1902 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1903 vector_set(matchvec, lcdstr);
1904 match_str = (char **)matchvec->index;
1905 vector_only_wrapper_free(matchvec);
1906
1907 *status = CMD_COMPLETE_MATCH;
1908 return match_str;
1909 }
1910 }
1911 }
1912
1913 match_str = (char **)matchvec->index;
1914 vector_only_wrapper_free(matchvec);
1915 *status = CMD_COMPLETE_LIST_MATCH;
1916 return match_str;
1917}
1918
1919char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1920{
1921 char **ret;
1922
1923 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1924 enum node_type onode;
1925 vector shifted_vline;
1926 unsigned int index;
1927
1928 onode = vty->node;
1929 vty->node = ENABLE_NODE;
1930 /* We can try it on enable node, cos' the vty is authenticated */
1931
1932 shifted_vline = vector_init(vector_count(vline));
1933 /* use memcpy? */
1934 for (index = 1; index < vector_active(vline); index++) {
1935 vector_set_index(shifted_vline, index - 1,
1936 vector_lookup(vline, index));
1937 }
1938
1939 ret = cmd_complete_command_real(shifted_vline, vty, status);
1940
1941 vector_free(shifted_vline);
1942 vty->node = onode;
1943 return ret;
1944 }
1945
1946 return cmd_complete_command_real(vline, vty, status);
1947}
1948
1949/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001950/*
1951 * This function MUST eventually converge on a node when called repeatedly,
1952 * there must not be any cycles.
1953 * All 'config' nodes shall converge on CONFIG_NODE.
1954 * All other 'enable' nodes shall converge on ENABLE_NODE.
1955 * All 'view' only nodes shall converge on VIEW_NODE.
1956 * All other nodes shall converge on themselves or it must be ensured,
1957 * that the user's rights are not extended anyhow by calling this function.
1958 *
1959 * Note that these requirements also apply to all functions that are used
1960 * as go_parent_cb.
1961 * Note also that this function relies on the is_config_child callback to
1962 * recognize non-config nodes if go_parent_cb is not set.
1963 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00001964int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001965{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001966 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001967 case AUTH_NODE:
1968 case VIEW_NODE:
1969 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001970 case CONFIG_NODE:
1971 break;
1972
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001973 case AUTH_ENABLE_NODE:
1974 vty->node = VIEW_NODE;
1975 break;
1976
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001977 case CFG_LOG_NODE:
1978 case VTY_NODE:
1979 vty->node = CONFIG_NODE;
1980 break;
1981
1982 default:
1983 if (host.app_info->go_parent_cb)
1984 host.app_info->go_parent_cb(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001985 else if (is_config_child(vty))
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001986 vty->node = CONFIG_NODE;
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001987 else
1988 vty->node = VIEW_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001989 break;
1990 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001991
1992 return vty->node;
1993}
1994
1995/* Execute command by argument vline vector. */
1996static int
1997cmd_execute_command_real(vector vline, struct vty *vty,
1998 struct cmd_element **cmd)
1999{
2000 unsigned int i;
2001 unsigned int index;
2002 vector cmd_vector;
2003 struct cmd_element *cmd_element;
2004 struct cmd_element *matched_element;
2005 unsigned int matched_count, incomplete_count;
2006 int argc;
2007 const char *argv[CMD_ARGC_MAX];
2008 enum match_type match = 0;
2009 int varflag;
2010 char *command;
2011
2012 /* Make copy of command elements. */
2013 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2014
2015 for (index = 0; index < vector_active(vline); index++)
2016 if ((command = vector_slot(vline, index))) {
2017 int ret;
2018
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002019 match = cmd_filter(command, cmd_vector, index,
2020 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002021
2022 if (match == vararg_match)
2023 break;
2024
2025 ret =
2026 is_cmd_ambiguous(command, cmd_vector, index, match);
2027
2028 if (ret == 1) {
2029 vector_free(cmd_vector);
2030 return CMD_ERR_AMBIGUOUS;
2031 } else if (ret == 2) {
2032 vector_free(cmd_vector);
2033 return CMD_ERR_NO_MATCH;
2034 }
2035 }
2036
2037 /* Check matched count. */
2038 matched_element = NULL;
2039 matched_count = 0;
2040 incomplete_count = 0;
2041
2042 for (i = 0; i < vector_active(cmd_vector); i++)
2043 if ((cmd_element = vector_slot(cmd_vector, i))) {
2044 if (match == vararg_match
2045 || index >= cmd_element->cmdsize) {
2046 matched_element = cmd_element;
2047#if 0
2048 printf("DEBUG: %s\n", cmd_element->string);
2049#endif
2050 matched_count++;
2051 } else {
2052 incomplete_count++;
2053 }
2054 }
2055
2056 /* Finish of using cmd_vector. */
2057 vector_free(cmd_vector);
2058
2059 /* To execute command, matched_count must be 1. */
2060 if (matched_count == 0) {
2061 if (incomplete_count)
2062 return CMD_ERR_INCOMPLETE;
2063 else
2064 return CMD_ERR_NO_MATCH;
2065 }
2066
2067 if (matched_count > 1)
2068 return CMD_ERR_AMBIGUOUS;
2069
2070 /* Argument treatment */
2071 varflag = 0;
2072 argc = 0;
2073
2074 for (i = 0; i < vector_active(vline); i++) {
2075 if (varflag)
2076 argv[argc++] = vector_slot(vline, i);
2077 else {
2078 vector descvec =
2079 vector_slot(matched_element->strvec, i);
2080
2081 if (vector_active(descvec) == 1) {
2082 struct desc *desc = vector_slot(descvec, 0);
2083
2084 if (CMD_VARARG(desc->cmd))
2085 varflag = 1;
2086
2087 if (varflag || CMD_VARIABLE(desc->cmd)
2088 || CMD_OPTION(desc->cmd))
2089 argv[argc++] = vector_slot(vline, i);
2090 } else
2091 argv[argc++] = vector_slot(vline, i);
2092 }
2093
2094 if (argc >= CMD_ARGC_MAX)
2095 return CMD_ERR_EXEED_ARGC_MAX;
2096 }
2097
2098 /* For vtysh execution. */
2099 if (cmd)
2100 *cmd = matched_element;
2101
2102 if (matched_element->daemon)
2103 return CMD_SUCCESS_DAEMON;
2104
2105 /* Execute matched command. */
2106 return (*matched_element->func) (matched_element, vty, argc, argv);
2107}
2108
2109int
2110cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2111 int vtysh)
2112{
2113 int ret, saved_ret, tried = 0;
2114 enum node_type onode;
2115 void *oindex;
2116
2117 onode = vty->node;
2118 oindex = vty->index;
2119
2120 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2121 vector shifted_vline;
2122 unsigned int index;
2123
2124 vty->node = ENABLE_NODE;
2125 /* We can try it on enable node, cos' the vty is authenticated */
2126
2127 shifted_vline = vector_init(vector_count(vline));
2128 /* use memcpy? */
2129 for (index = 1; index < vector_active(vline); index++) {
2130 vector_set_index(shifted_vline, index - 1,
2131 vector_lookup(vline, index));
2132 }
2133
2134 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2135
2136 vector_free(shifted_vline);
2137 vty->node = onode;
2138 return ret;
2139 }
2140
2141 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2142
2143 if (vtysh)
2144 return saved_ret;
2145
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002146 /* Go to parent for config nodes to attempt to find the right command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002147 while (ret != CMD_SUCCESS && ret != CMD_WARNING
Jacob Erlbeck2442e092013-09-06 16:51:58 +02002148 && is_config_child(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002149 vty_go_parent(vty);
2150 ret = cmd_execute_command_real(vline, vty, cmd);
2151 tried = 1;
2152 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2153 /* succesfull command, leave the node as is */
2154 return ret;
2155 }
2156 }
2157 /* no command succeeded, reset the vty to the original node and
2158 return the error for this node */
2159 if (tried) {
2160 vty->node = onode;
2161 vty->index = oindex;
2162 }
2163 return saved_ret;
2164}
2165
2166/* Execute command by argument readline. */
2167int
2168cmd_execute_command_strict(vector vline, struct vty *vty,
2169 struct cmd_element **cmd)
2170{
2171 unsigned int i;
2172 unsigned int index;
2173 vector cmd_vector;
2174 struct cmd_element *cmd_element;
2175 struct cmd_element *matched_element;
2176 unsigned int matched_count, incomplete_count;
2177 int argc;
2178 const char *argv[CMD_ARGC_MAX];
2179 int varflag;
2180 enum match_type match = 0;
2181 char *command;
2182
2183 /* Make copy of command element */
2184 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2185
2186 for (index = 0; index < vector_active(vline); index++)
2187 if ((command = vector_slot(vline, index))) {
2188 int ret;
2189
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002190 match = cmd_filter(vector_slot(vline, index),
2191 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002192
2193 /* If command meets '.VARARG' then finish matching. */
2194 if (match == vararg_match)
2195 break;
2196
2197 ret =
2198 is_cmd_ambiguous(command, cmd_vector, index, match);
2199 if (ret == 1) {
2200 vector_free(cmd_vector);
2201 return CMD_ERR_AMBIGUOUS;
2202 }
2203 if (ret == 2) {
2204 vector_free(cmd_vector);
2205 return CMD_ERR_NO_MATCH;
2206 }
2207 }
2208
2209 /* Check matched count. */
2210 matched_element = NULL;
2211 matched_count = 0;
2212 incomplete_count = 0;
2213 for (i = 0; i < vector_active(cmd_vector); i++)
2214 if (vector_slot(cmd_vector, i) != NULL) {
2215 cmd_element = vector_slot(cmd_vector, i);
2216
2217 if (match == vararg_match
2218 || index >= cmd_element->cmdsize) {
2219 matched_element = cmd_element;
2220 matched_count++;
2221 } else
2222 incomplete_count++;
2223 }
2224
2225 /* Finish of using cmd_vector. */
2226 vector_free(cmd_vector);
2227
2228 /* To execute command, matched_count must be 1. */
2229 if (matched_count == 0) {
2230 if (incomplete_count)
2231 return CMD_ERR_INCOMPLETE;
2232 else
2233 return CMD_ERR_NO_MATCH;
2234 }
2235
2236 if (matched_count > 1)
2237 return CMD_ERR_AMBIGUOUS;
2238
2239 /* Argument treatment */
2240 varflag = 0;
2241 argc = 0;
2242
2243 for (i = 0; i < vector_active(vline); i++) {
2244 if (varflag)
2245 argv[argc++] = vector_slot(vline, i);
2246 else {
2247 vector descvec =
2248 vector_slot(matched_element->strvec, i);
2249
2250 if (vector_active(descvec) == 1) {
2251 struct desc *desc = vector_slot(descvec, 0);
2252
2253 if (CMD_VARARG(desc->cmd))
2254 varflag = 1;
2255
2256 if (varflag || CMD_VARIABLE(desc->cmd)
2257 || CMD_OPTION(desc->cmd))
2258 argv[argc++] = vector_slot(vline, i);
2259 } else
2260 argv[argc++] = vector_slot(vline, i);
2261 }
2262
2263 if (argc >= CMD_ARGC_MAX)
2264 return CMD_ERR_EXEED_ARGC_MAX;
2265 }
2266
2267 /* For vtysh execution. */
2268 if (cmd)
2269 *cmd = matched_element;
2270
2271 if (matched_element->daemon)
2272 return CMD_SUCCESS_DAEMON;
2273
2274 /* Now execute matched command */
2275 return (*matched_element->func) (matched_element, vty, argc, argv);
2276}
2277
2278/* Configration make from file. */
2279int config_from_file(struct vty *vty, FILE * fp)
2280{
2281 int ret;
2282 vector vline;
2283
2284 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2285 vline = cmd_make_strvec(vty->buf);
2286
2287 /* In case of comment line */
2288 if (vline == NULL)
2289 continue;
2290 /* Execute configuration command : this is strict match */
2291 ret = cmd_execute_command_strict(vline, vty, NULL);
2292
2293 /* Try again with setting node to CONFIG_NODE */
2294 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2295 && ret != CMD_ERR_NOTHING_TODO
Jacob Erlbeck2442e092013-09-06 16:51:58 +02002296 && is_config_child(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002297 vty_go_parent(vty);
2298 ret = cmd_execute_command_strict(vline, vty, NULL);
2299 }
2300
2301 cmd_free_strvec(vline);
2302
2303 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2304 && ret != CMD_ERR_NOTHING_TODO)
2305 return ret;
2306 }
2307 return CMD_SUCCESS;
2308}
2309
2310/* Configration from terminal */
2311DEFUN(config_terminal,
2312 config_terminal_cmd,
2313 "configure terminal",
2314 "Configuration from vty interface\n" "Configuration terminal\n")
2315{
2316 if (vty_config_lock(vty))
2317 vty->node = CONFIG_NODE;
2318 else {
2319 vty_out(vty, "VTY configuration is locked by other VTY%s",
2320 VTY_NEWLINE);
2321 return CMD_WARNING;
2322 }
2323 return CMD_SUCCESS;
2324}
2325
2326/* Enable command */
2327DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2328{
2329 /* If enable password is NULL, change to ENABLE_NODE */
2330 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2331 vty->type == VTY_SHELL_SERV)
2332 vty->node = ENABLE_NODE;
2333 else
2334 vty->node = AUTH_ENABLE_NODE;
2335
2336 return CMD_SUCCESS;
2337}
2338
2339/* Disable command */
2340DEFUN(disable,
2341 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2342{
2343 if (vty->node == ENABLE_NODE)
2344 vty->node = VIEW_NODE;
2345 return CMD_SUCCESS;
2346}
2347
2348/* Down vty node level. */
2349gDEFUN(config_exit,
2350 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2351{
2352 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002353 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002354 case VIEW_NODE:
2355 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002356 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002357 break;
2358 case CONFIG_NODE:
2359 vty->node = ENABLE_NODE;
2360 vty_config_unlock(vty);
2361 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002362 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002363 if (vty->node > CONFIG_NODE)
2364 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002365 break;
2366 }
2367 return CMD_SUCCESS;
2368}
2369
2370/* End of configuration. */
2371 gDEFUN(config_end,
2372 config_end_cmd, "end", "End current mode and change to enable mode.")
2373{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002374 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002375 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002376
2377 /* Repeatedly call go_parent until a top node is reached. */
2378 while (vty->node > CONFIG_NODE) {
2379 if (vty->node == last_node) {
2380 /* Ensure termination, this shouldn't happen. */
2381 break;
2382 }
2383 last_node = vty->node;
2384 vty_go_parent(vty);
2385 }
2386
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002387 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002388 if (vty->node > ENABLE_NODE)
2389 vty->node = ENABLE_NODE;
2390 vty->index = NULL;
2391 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002392 }
2393 return CMD_SUCCESS;
2394}
2395
2396/* Show version. */
2397DEFUN(show_version,
2398 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2399{
Harald Welte237f6242010-05-25 23:00:45 +02002400 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2401 host.app_info->version,
2402 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2403 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002404
2405 return CMD_SUCCESS;
2406}
2407
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002408DEFUN(show_online_help,
2409 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2410{
2411 vty_dump_nodes(vty);
2412 return CMD_SUCCESS;
2413}
2414
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002415/* Help display function for all node. */
2416gDEFUN(config_help,
2417 config_help_cmd, "help", "Description of the interactive help system\n")
2418{
2419 vty_out(vty,
2420 "This VTY provides advanced help features. When you need help,%s\
2421anytime at the command line please press '?'.%s\
2422%s\
2423If nothing matches, the help list will be empty and you must backup%s\
2424 until entering a '?' shows the available options.%s\
2425Two styles of help are provided:%s\
24261. Full help is available when you are ready to enter a%s\
2427command argument (e.g. 'show ?') and describes each possible%s\
2428argument.%s\
24292. Partial help is provided when an abbreviated argument is entered%s\
2430 and you want to know what arguments match the input%s\
2431 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2432 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2433 return CMD_SUCCESS;
2434}
2435
2436/* Help display function for all node. */
2437gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2438{
2439 unsigned int i;
2440 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2441 struct cmd_element *cmd;
2442
2443 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2444 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2445 && !(cmd->attr == CMD_ATTR_DEPRECATED
2446 || cmd->attr == CMD_ATTR_HIDDEN))
2447 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2448 return CMD_SUCCESS;
2449}
2450
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002451static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002452{
2453 unsigned int i;
2454 int fd;
2455 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002456 char *config_file_tmp = NULL;
2457 char *config_file_sav = NULL;
2458 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002459 struct stat st;
2460
2461 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002462
2463 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002464 config_file_sav =
2465 _talloc_zero(tall_vty_cmd_ctx,
2466 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2467 "config_file_sav");
2468 strcpy(config_file_sav, config_file);
2469 strcat(config_file_sav, CONF_BACKUP_EXT);
2470
2471 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2472 "config_file_tmp");
2473 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2474
2475 /* Open file to configuration write. */
2476 fd = mkstemp(config_file_tmp);
2477 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002478 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002479 talloc_free(config_file_tmp);
2480 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002481 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002482 }
2483
2484 /* Make vty for configuration file. */
2485 file_vty = vty_new();
2486 file_vty->fd = fd;
2487 file_vty->type = VTY_FILE;
2488
2489 /* Config file header print. */
2490 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002491 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002492 //vty_time_print (file_vty, 1);
2493 vty_out(file_vty, "!\n");
2494
2495 for (i = 0; i < vector_active(cmdvec); i++)
2496 if ((node = vector_slot(cmdvec, i)) && node->func) {
2497 if ((*node->func) (file_vty))
2498 vty_out(file_vty, "!\n");
2499 }
2500 vty_close(file_vty);
2501
2502 if (unlink(config_file_sav) != 0)
2503 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002504 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002505 talloc_free(config_file_sav);
2506 talloc_free(config_file_tmp);
2507 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002508 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002509 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002510
2511 /* Only link the .sav file if the original file exists */
2512 if (stat(config_file, &st) == 0) {
2513 if (link(config_file, config_file_sav) != 0) {
2514 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2515 talloc_free(config_file_sav);
2516 talloc_free(config_file_tmp);
2517 unlink(config_file_tmp);
2518 return -3;
2519 }
2520 sync();
2521 if (unlink(config_file) != 0) {
2522 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2523 talloc_free(config_file_sav);
2524 talloc_free(config_file_tmp);
2525 unlink(config_file_tmp);
2526 return -4;
2527 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002528 }
2529 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002530 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002531 talloc_free(config_file_sav);
2532 talloc_free(config_file_tmp);
2533 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002534 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002535 }
2536 unlink(config_file_tmp);
2537 sync();
2538
2539 talloc_free(config_file_sav);
2540 talloc_free(config_file_tmp);
2541
2542 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002543 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2544 return -6;
2545 }
2546
2547 return 0;
2548}
2549
2550
2551/* Write current configuration into file. */
2552DEFUN(config_write_file,
2553 config_write_file_cmd,
2554 "write file",
2555 "Write running configuration to memory, network, or terminal\n"
2556 "Write to configuration file\n")
2557{
2558 char *failed_file;
2559 int rc;
2560
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002561 if (host.app_info->config_is_consistent) {
2562 rc = host.app_info->config_is_consistent(vty);
2563 if (!rc) {
2564 vty_out(vty, "Configuration is not consistent%s",
2565 VTY_NEWLINE);
2566 return CMD_WARNING;
2567 }
2568 }
2569
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002570 if (host.config == NULL) {
2571 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2572 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002573 return CMD_WARNING;
2574 }
2575
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002576 rc = write_config_file(host.config, &failed_file);
2577 switch (rc) {
2578 case -1:
2579 vty_out(vty, "Can't open configuration file %s.%s",
2580 failed_file, VTY_NEWLINE);
2581 rc = CMD_WARNING;
2582 break;
2583 case -2:
2584 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2585 failed_file, VTY_NEWLINE);
2586 rc = CMD_WARNING;
2587 break;
2588 case -3:
2589 vty_out(vty, "Can't backup old configuration file %s.%s",
2590 failed_file, VTY_NEWLINE);
2591 rc = CMD_WARNING;
2592 break;
2593 case -4:
2594 vty_out(vty, "Can't unlink configuration file %s.%s",
2595 failed_file, VTY_NEWLINE);
2596 rc = CMD_WARNING;
2597 break;
2598 case -5:
2599 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2600 VTY_NEWLINE);
2601 rc = CMD_WARNING;
2602 break;
2603 case -6:
2604 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2605 failed_file, strerror(errno), errno, VTY_NEWLINE);
2606 rc = CMD_WARNING;
2607 break;
2608 default:
2609 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2610 rc = CMD_SUCCESS;
2611 break;
2612 }
2613
2614 talloc_free(failed_file);
2615 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002616}
2617
2618ALIAS(config_write_file,
2619 config_write_cmd,
2620 "write", "Write running configuration to memory, network, or terminal\n")
2621
2622 ALIAS(config_write_file,
2623 config_write_memory_cmd,
2624 "write memory",
2625 "Write running configuration to memory, network, or terminal\n"
2626 "Write configuration to the file (same as write file)\n")
2627
2628 ALIAS(config_write_file,
2629 copy_runningconfig_startupconfig_cmd,
2630 "copy running-config startup-config",
2631 "Copy configuration\n"
2632 "Copy running config to... \n"
2633 "Copy running config to startup config (same as write file)\n")
2634
2635/* Write current configuration into the terminal. */
2636 DEFUN(config_write_terminal,
2637 config_write_terminal_cmd,
2638 "write terminal",
2639 "Write running configuration to memory, network, or terminal\n"
2640 "Write to terminal\n")
2641{
2642 unsigned int i;
2643 struct cmd_node *node;
2644
2645 if (vty->type == VTY_SHELL_SERV) {
2646 for (i = 0; i < vector_active(cmdvec); i++)
2647 if ((node = vector_slot(cmdvec, i)) && node->func
2648 && node->vtysh) {
2649 if ((*node->func) (vty))
2650 vty_out(vty, "!%s", VTY_NEWLINE);
2651 }
2652 } else {
2653 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2654 VTY_NEWLINE);
2655 vty_out(vty, "!%s", VTY_NEWLINE);
2656
2657 for (i = 0; i < vector_active(cmdvec); i++)
2658 if ((node = vector_slot(cmdvec, i)) && node->func) {
2659 if ((*node->func) (vty))
2660 vty_out(vty, "!%s", VTY_NEWLINE);
2661 }
2662 vty_out(vty, "end%s", VTY_NEWLINE);
2663 }
2664 return CMD_SUCCESS;
2665}
2666
2667/* Write current configuration into the terminal. */
2668ALIAS(config_write_terminal,
2669 show_running_config_cmd,
2670 "show running-config", SHOW_STR "running configuration\n")
2671
2672/* Write startup configuration into the terminal. */
2673 DEFUN(show_startup_config,
2674 show_startup_config_cmd,
2675 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2676{
2677 char buf[BUFSIZ];
2678 FILE *confp;
2679
2680 confp = fopen(host.config, "r");
2681 if (confp == NULL) {
2682 vty_out(vty, "Can't open configuration file [%s]%s",
2683 host.config, VTY_NEWLINE);
2684 return CMD_WARNING;
2685 }
2686
2687 while (fgets(buf, BUFSIZ, confp)) {
2688 char *cp = buf;
2689
2690 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2691 cp++;
2692 *cp = '\0';
2693
2694 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2695 }
2696
2697 fclose(confp);
2698
2699 return CMD_SUCCESS;
2700}
2701
2702/* Hostname configuration */
2703DEFUN(config_hostname,
2704 hostname_cmd,
2705 "hostname WORD",
2706 "Set system's network name\n" "This system's network name\n")
2707{
2708 if (!isalpha((int)*argv[0])) {
2709 vty_out(vty, "Please specify string starting with alphabet%s",
2710 VTY_NEWLINE);
2711 return CMD_WARNING;
2712 }
2713
2714 if (host.name)
2715 talloc_free(host.name);
2716
2717 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2718 return CMD_SUCCESS;
2719}
2720
2721DEFUN(config_no_hostname,
2722 no_hostname_cmd,
2723 "no hostname [HOSTNAME]",
2724 NO_STR "Reset system's network name\n" "Host name of this router\n")
2725{
2726 if (host.name)
2727 talloc_free(host.name);
2728 host.name = NULL;
2729 return CMD_SUCCESS;
2730}
2731
2732/* VTY interface password set. */
2733DEFUN(config_password, password_cmd,
2734 "password (8|) WORD",
2735 "Assign the terminal connection password\n"
2736 "Specifies a HIDDEN password will follow\n"
2737 "dummy string \n" "The HIDDEN line password string\n")
2738{
2739 /* Argument check. */
2740 if (argc == 0) {
2741 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2742 return CMD_WARNING;
2743 }
2744
2745 if (argc == 2) {
2746 if (*argv[0] == '8') {
2747 if (host.password)
2748 talloc_free(host.password);
2749 host.password = NULL;
2750 if (host.password_encrypt)
2751 talloc_free(host.password_encrypt);
2752 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2753 return CMD_SUCCESS;
2754 } else {
2755 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2756 return CMD_WARNING;
2757 }
2758 }
2759
2760 if (!isalnum((int)*argv[0])) {
2761 vty_out(vty,
2762 "Please specify string starting with alphanumeric%s",
2763 VTY_NEWLINE);
2764 return CMD_WARNING;
2765 }
2766
2767 if (host.password)
2768 talloc_free(host.password);
2769 host.password = NULL;
2770
2771#ifdef VTY_CRYPT_PW
2772 if (host.encrypt) {
2773 if (host.password_encrypt)
2774 talloc_free(host.password_encrypt);
2775 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2776 } else
2777#endif
2778 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2779
2780 return CMD_SUCCESS;
2781}
2782
2783ALIAS(config_password, password_text_cmd,
2784 "password LINE",
2785 "Assign the terminal connection password\n"
2786 "The UNENCRYPTED (cleartext) line password\n")
2787
2788/* VTY enable password set. */
2789 DEFUN(config_enable_password, enable_password_cmd,
2790 "enable password (8|) WORD",
2791 "Modify enable password parameters\n"
2792 "Assign the privileged level password\n"
2793 "Specifies a HIDDEN password will follow\n"
2794 "dummy string \n" "The HIDDEN 'enable' password string\n")
2795{
2796 /* Argument check. */
2797 if (argc == 0) {
2798 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2799 return CMD_WARNING;
2800 }
2801
2802 /* Crypt type is specified. */
2803 if (argc == 2) {
2804 if (*argv[0] == '8') {
2805 if (host.enable)
2806 talloc_free(host.enable);
2807 host.enable = NULL;
2808
2809 if (host.enable_encrypt)
2810 talloc_free(host.enable_encrypt);
2811 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2812
2813 return CMD_SUCCESS;
2814 } else {
2815 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2816 return CMD_WARNING;
2817 }
2818 }
2819
2820 if (!isalnum((int)*argv[0])) {
2821 vty_out(vty,
2822 "Please specify string starting with alphanumeric%s",
2823 VTY_NEWLINE);
2824 return CMD_WARNING;
2825 }
2826
2827 if (host.enable)
2828 talloc_free(host.enable);
2829 host.enable = NULL;
2830
2831 /* Plain password input. */
2832#ifdef VTY_CRYPT_PW
2833 if (host.encrypt) {
2834 if (host.enable_encrypt)
2835 talloc_free(host.enable_encrypt);
2836 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2837 } else
2838#endif
2839 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2840
2841 return CMD_SUCCESS;
2842}
2843
2844ALIAS(config_enable_password,
2845 enable_password_text_cmd,
2846 "enable password LINE",
2847 "Modify enable password parameters\n"
2848 "Assign the privileged level password\n"
2849 "The UNENCRYPTED (cleartext) 'enable' password\n")
2850
2851/* VTY enable password delete. */
2852 DEFUN(no_config_enable_password, no_enable_password_cmd,
2853 "no enable password",
2854 NO_STR
2855 "Modify enable password parameters\n"
2856 "Assign the privileged level password\n")
2857{
2858 if (host.enable)
2859 talloc_free(host.enable);
2860 host.enable = NULL;
2861
2862 if (host.enable_encrypt)
2863 talloc_free(host.enable_encrypt);
2864 host.enable_encrypt = NULL;
2865
2866 return CMD_SUCCESS;
2867}
2868
2869#ifdef VTY_CRYPT_PW
2870DEFUN(service_password_encrypt,
2871 service_password_encrypt_cmd,
2872 "service password-encryption",
2873 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2874{
2875 if (host.encrypt)
2876 return CMD_SUCCESS;
2877
2878 host.encrypt = 1;
2879
2880 if (host.password) {
2881 if (host.password_encrypt)
2882 talloc_free(host.password_encrypt);
2883 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2884 }
2885 if (host.enable) {
2886 if (host.enable_encrypt)
2887 talloc_free(host.enable_encrypt);
2888 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2889 }
2890
2891 return CMD_SUCCESS;
2892}
2893
2894DEFUN(no_service_password_encrypt,
2895 no_service_password_encrypt_cmd,
2896 "no service password-encryption",
2897 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2898{
2899 if (!host.encrypt)
2900 return CMD_SUCCESS;
2901
2902 host.encrypt = 0;
2903
2904 if (host.password_encrypt)
2905 talloc_free(host.password_encrypt);
2906 host.password_encrypt = NULL;
2907
2908 if (host.enable_encrypt)
2909 talloc_free(host.enable_encrypt);
2910 host.enable_encrypt = NULL;
2911
2912 return CMD_SUCCESS;
2913}
2914#endif
2915
2916DEFUN(config_terminal_length, config_terminal_length_cmd,
2917 "terminal length <0-512>",
2918 "Set terminal line parameters\n"
2919 "Set number of lines on a screen\n"
2920 "Number of lines on screen (0 for no pausing)\n")
2921{
2922 int lines;
2923 char *endptr = NULL;
2924
2925 lines = strtol(argv[0], &endptr, 10);
2926 if (lines < 0 || lines > 512 || *endptr != '\0') {
2927 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2928 return CMD_WARNING;
2929 }
2930 vty->lines = lines;
2931
2932 return CMD_SUCCESS;
2933}
2934
2935DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2936 "terminal no length",
2937 "Set terminal line parameters\n"
2938 NO_STR "Set number of lines on a screen\n")
2939{
2940 vty->lines = -1;
2941 return CMD_SUCCESS;
2942}
2943
2944DEFUN(service_terminal_length, service_terminal_length_cmd,
2945 "service terminal-length <0-512>",
2946 "Set up miscellaneous service\n"
2947 "System wide terminal length configuration\n"
2948 "Number of lines of VTY (0 means no line control)\n")
2949{
2950 int lines;
2951 char *endptr = NULL;
2952
2953 lines = strtol(argv[0], &endptr, 10);
2954 if (lines < 0 || lines > 512 || *endptr != '\0') {
2955 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2956 return CMD_WARNING;
2957 }
2958 host.lines = lines;
2959
2960 return CMD_SUCCESS;
2961}
2962
2963DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2964 "no service terminal-length [<0-512>]",
2965 NO_STR
2966 "Set up miscellaneous service\n"
2967 "System wide terminal length configuration\n"
2968 "Number of lines of VTY (0 means no line control)\n")
2969{
2970 host.lines = -1;
2971 return CMD_SUCCESS;
2972}
2973
2974DEFUN_HIDDEN(do_echo,
2975 echo_cmd,
2976 "echo .MESSAGE",
2977 "Echo a message back to the vty\n" "The message to echo\n")
2978{
2979 char *message;
2980
2981 vty_out(vty, "%s%s",
2982 ((message =
2983 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2984 if (message)
2985 talloc_free(message);
2986 return CMD_SUCCESS;
2987}
2988
2989#if 0
2990DEFUN(config_logmsg,
2991 config_logmsg_cmd,
2992 "logmsg " LOG_LEVELS " .MESSAGE",
2993 "Send a message to enabled logging destinations\n"
2994 LOG_LEVEL_DESC "The message to send\n")
2995{
2996 int level;
2997 char *message;
2998
2999 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3000 return CMD_ERR_NO_MATCH;
3001
3002 zlog(NULL, level,
3003 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3004 if (message)
3005 talloc_free(message);
3006 return CMD_SUCCESS;
3007}
3008
3009DEFUN(show_logging,
3010 show_logging_cmd,
3011 "show logging", SHOW_STR "Show current logging configuration\n")
3012{
3013 struct zlog *zl = zlog_default;
3014
3015 vty_out(vty, "Syslog logging: ");
3016 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3017 vty_out(vty, "disabled");
3018 else
3019 vty_out(vty, "level %s, facility %s, ident %s",
3020 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3021 facility_name(zl->facility), zl->ident);
3022 vty_out(vty, "%s", VTY_NEWLINE);
3023
3024 vty_out(vty, "Stdout logging: ");
3025 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3026 vty_out(vty, "disabled");
3027 else
3028 vty_out(vty, "level %s",
3029 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3030 vty_out(vty, "%s", VTY_NEWLINE);
3031
3032 vty_out(vty, "Monitor logging: ");
3033 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3034 vty_out(vty, "disabled");
3035 else
3036 vty_out(vty, "level %s",
3037 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3038 vty_out(vty, "%s", VTY_NEWLINE);
3039
3040 vty_out(vty, "File logging: ");
3041 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3042 vty_out(vty, "disabled");
3043 else
3044 vty_out(vty, "level %s, filename %s",
3045 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3046 zl->filename);
3047 vty_out(vty, "%s", VTY_NEWLINE);
3048
3049 vty_out(vty, "Protocol name: %s%s",
3050 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3051 vty_out(vty, "Record priority: %s%s",
3052 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3053
3054 return CMD_SUCCESS;
3055}
3056
3057DEFUN(config_log_stdout,
3058 config_log_stdout_cmd,
3059 "log stdout", "Logging control\n" "Set stdout logging level\n")
3060{
3061 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3062 return CMD_SUCCESS;
3063}
3064
3065DEFUN(config_log_stdout_level,
3066 config_log_stdout_level_cmd,
3067 "log stdout " LOG_LEVELS,
3068 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3069{
3070 int level;
3071
3072 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3073 return CMD_ERR_NO_MATCH;
3074 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3075 return CMD_SUCCESS;
3076}
3077
3078DEFUN(no_config_log_stdout,
3079 no_config_log_stdout_cmd,
3080 "no log stdout [LEVEL]",
3081 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3082{
3083 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3084 return CMD_SUCCESS;
3085}
3086
3087DEFUN(config_log_monitor,
3088 config_log_monitor_cmd,
3089 "log monitor",
3090 "Logging control\n" "Set terminal line (monitor) logging level\n")
3091{
3092 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3093 return CMD_SUCCESS;
3094}
3095
3096DEFUN(config_log_monitor_level,
3097 config_log_monitor_level_cmd,
3098 "log monitor " LOG_LEVELS,
3099 "Logging control\n"
3100 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3101{
3102 int level;
3103
3104 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3105 return CMD_ERR_NO_MATCH;
3106 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3107 return CMD_SUCCESS;
3108}
3109
3110DEFUN(no_config_log_monitor,
3111 no_config_log_monitor_cmd,
3112 "no log monitor [LEVEL]",
3113 NO_STR
3114 "Logging control\n"
3115 "Disable terminal line (monitor) logging\n" "Logging level\n")
3116{
3117 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3118 return CMD_SUCCESS;
3119}
3120
3121static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3122{
3123 int ret;
3124 char *p = NULL;
3125 const char *fullpath;
3126
3127 /* Path detection. */
3128 if (!IS_DIRECTORY_SEP(*fname)) {
3129 char cwd[MAXPATHLEN + 1];
3130 cwd[MAXPATHLEN] = '\0';
3131
3132 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3133 zlog_err("config_log_file: Unable to alloc mem!");
3134 return CMD_WARNING;
3135 }
3136
3137 if ((p = _talloc_zero(tall_vcmd_ctx,
3138 strlen(cwd) + strlen(fname) + 2),
3139 "set_log_file")
3140 == NULL) {
3141 zlog_err("config_log_file: Unable to alloc mem!");
3142 return CMD_WARNING;
3143 }
3144 sprintf(p, "%s/%s", cwd, fname);
3145 fullpath = p;
3146 } else
3147 fullpath = fname;
3148
3149 ret = zlog_set_file(NULL, fullpath, loglevel);
3150
3151 if (p)
3152 talloc_free(p);
3153
3154 if (!ret) {
3155 vty_out(vty, "can't open logfile %s\n", fname);
3156 return CMD_WARNING;
3157 }
3158
3159 if (host.logfile)
3160 talloc_free(host.logfile);
3161
3162 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3163
3164 return CMD_SUCCESS;
3165}
3166
3167DEFUN(config_log_file,
3168 config_log_file_cmd,
3169 "log file FILENAME",
3170 "Logging control\n" "Logging to file\n" "Logging filename\n")
3171{
3172 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3173}
3174
3175DEFUN(config_log_file_level,
3176 config_log_file_level_cmd,
3177 "log file FILENAME " LOG_LEVELS,
3178 "Logging control\n"
3179 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3180{
3181 int level;
3182
3183 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3184 return CMD_ERR_NO_MATCH;
3185 return set_log_file(vty, argv[0], level);
3186}
3187
3188DEFUN(no_config_log_file,
3189 no_config_log_file_cmd,
3190 "no log file [FILENAME]",
3191 NO_STR
3192 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3193{
3194 zlog_reset_file(NULL);
3195
3196 if (host.logfile)
3197 talloc_free(host.logfile);
3198
3199 host.logfile = NULL;
3200
3201 return CMD_SUCCESS;
3202}
3203
3204ALIAS(no_config_log_file,
3205 no_config_log_file_level_cmd,
3206 "no log file FILENAME LEVEL",
3207 NO_STR
3208 "Logging control\n"
3209 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3210
3211 DEFUN(config_log_syslog,
3212 config_log_syslog_cmd,
3213 "log syslog", "Logging control\n" "Set syslog logging level\n")
3214{
3215 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3216 return CMD_SUCCESS;
3217}
3218
3219DEFUN(config_log_syslog_level,
3220 config_log_syslog_level_cmd,
3221 "log syslog " LOG_LEVELS,
3222 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3223{
3224 int level;
3225
3226 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3227 return CMD_ERR_NO_MATCH;
3228 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3229 return CMD_SUCCESS;
3230}
3231
3232DEFUN_DEPRECATED(config_log_syslog_facility,
3233 config_log_syslog_facility_cmd,
3234 "log syslog facility " LOG_FACILITIES,
3235 "Logging control\n"
3236 "Logging goes to syslog\n"
3237 "(Deprecated) Facility parameter for syslog messages\n"
3238 LOG_FACILITY_DESC)
3239{
3240 int facility;
3241
3242 if ((facility = facility_match(argv[0])) < 0)
3243 return CMD_ERR_NO_MATCH;
3244
3245 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3246 zlog_default->facility = facility;
3247 return CMD_SUCCESS;
3248}
3249
3250DEFUN(no_config_log_syslog,
3251 no_config_log_syslog_cmd,
3252 "no log syslog [LEVEL]",
3253 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3254{
3255 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3256 return CMD_SUCCESS;
3257}
3258
3259ALIAS(no_config_log_syslog,
3260 no_config_log_syslog_facility_cmd,
3261 "no log syslog facility " LOG_FACILITIES,
3262 NO_STR
3263 "Logging control\n"
3264 "Logging goes to syslog\n"
3265 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3266
3267 DEFUN(config_log_facility,
3268 config_log_facility_cmd,
3269 "log facility " LOG_FACILITIES,
3270 "Logging control\n"
3271 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3272{
3273 int facility;
3274
3275 if ((facility = facility_match(argv[0])) < 0)
3276 return CMD_ERR_NO_MATCH;
3277 zlog_default->facility = facility;
3278 return CMD_SUCCESS;
3279}
3280
3281DEFUN(no_config_log_facility,
3282 no_config_log_facility_cmd,
3283 "no log facility [FACILITY]",
3284 NO_STR
3285 "Logging control\n"
3286 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3287{
3288 zlog_default->facility = LOG_DAEMON;
3289 return CMD_SUCCESS;
3290}
3291
3292DEFUN_DEPRECATED(config_log_trap,
3293 config_log_trap_cmd,
3294 "log trap " LOG_LEVELS,
3295 "Logging control\n"
3296 "(Deprecated) Set logging level and default for all destinations\n"
3297 LOG_LEVEL_DESC)
3298{
3299 int new_level;
3300 int i;
3301
3302 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3303 return CMD_ERR_NO_MATCH;
3304
3305 zlog_default->default_lvl = new_level;
3306 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3307 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3308 zlog_default->maxlvl[i] = new_level;
3309 return CMD_SUCCESS;
3310}
3311
3312DEFUN_DEPRECATED(no_config_log_trap,
3313 no_config_log_trap_cmd,
3314 "no log trap [LEVEL]",
3315 NO_STR
3316 "Logging control\n"
3317 "Permit all logging information\n" "Logging level\n")
3318{
3319 zlog_default->default_lvl = LOG_DEBUG;
3320 return CMD_SUCCESS;
3321}
3322
3323DEFUN(config_log_record_priority,
3324 config_log_record_priority_cmd,
3325 "log record-priority",
3326 "Logging control\n"
3327 "Log the priority of the message within the message\n")
3328{
3329 zlog_default->record_priority = 1;
3330 return CMD_SUCCESS;
3331}
3332
3333DEFUN(no_config_log_record_priority,
3334 no_config_log_record_priority_cmd,
3335 "no log record-priority",
3336 NO_STR
3337 "Logging control\n"
3338 "Do not log the priority of the message within the message\n")
3339{
3340 zlog_default->record_priority = 0;
3341 return CMD_SUCCESS;
3342}
3343#endif
3344
3345DEFUN(banner_motd_file,
3346 banner_motd_file_cmd,
3347 "banner motd file [FILE]",
3348 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3349{
3350 if (host.motdfile)
3351 talloc_free(host.motdfile);
3352 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3353
3354 return CMD_SUCCESS;
3355}
3356
3357DEFUN(banner_motd_default,
3358 banner_motd_default_cmd,
3359 "banner motd default",
3360 "Set banner string\n" "Strings for motd\n" "Default string\n")
3361{
3362 host.motd = default_motd;
3363 return CMD_SUCCESS;
3364}
3365
3366DEFUN(no_banner_motd,
3367 no_banner_motd_cmd,
3368 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3369{
3370 host.motd = NULL;
3371 if (host.motdfile)
3372 talloc_free(host.motdfile);
3373 host.motdfile = NULL;
3374 return CMD_SUCCESS;
3375}
3376
3377/* Set config filename. Called from vty.c */
3378void host_config_set(const char *filename)
3379{
3380 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3381}
3382
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003383void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003384{
3385 install_element(node, &config_help_cmd);
3386 install_element(node, &config_list_cmd);
3387
3388 install_element(node, &config_write_terminal_cmd);
3389 install_element(node, &config_write_file_cmd);
3390 install_element(node, &config_write_memory_cmd);
3391 install_element(node, &config_write_cmd);
3392 install_element(node, &show_running_config_cmd);
3393}
3394
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003395void vty_install_default(int node)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003396{
3397 install_default(node);
3398
3399 install_element(node, &config_exit_cmd);
3400
3401 if (node >= CONFIG_NODE) {
3402 /* It's not a top node. */
3403 install_element(node, &config_end_cmd);
3404 }
3405}
3406
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003407/**
3408 * \brief Write the current running config to a given file
3409 * \param[in] vty the vty of the code
3410 * \param[in] filename where to store the file
3411 * \return 0 in case of success.
3412 *
3413 * If the filename already exists create a filename.sav
3414 * version with the current code.
3415 *
3416 */
3417int osmo_vty_write_config_file(const char *filename)
3418{
3419 char *failed_file;
3420 int rc;
3421
3422 rc = write_config_file(filename, &failed_file);
3423 talloc_free(failed_file);
3424 return rc;
3425}
3426
3427/**
3428 * \brief Save the current state to the config file
3429 * \return 0 in case of success.
3430 *
3431 * If the filename already exists create a filename.sav
3432 * version with the current code.
3433 *
3434 */
3435int osmo_vty_save_config_file(void)
3436{
3437 char *failed_file;
3438 int rc;
3439
3440 if (host.config == NULL)
3441 return -7;
3442
3443 rc = write_config_file(host.config, &failed_file);
3444 talloc_free(failed_file);
3445 return rc;
3446}
3447
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003448/* Initialize command interface. Install basic nodes and commands. */
3449void cmd_init(int terminal)
3450{
3451 /* Allocate initial top vector of commands. */
3452 cmdvec = vector_init(VECTOR_MIN_SIZE);
3453
3454 /* Default host value settings. */
3455 host.name = NULL;
3456 host.password = NULL;
3457 host.enable = NULL;
3458 host.logfile = NULL;
3459 host.config = NULL;
3460 host.lines = -1;
3461 host.motd = default_motd;
3462 host.motdfile = NULL;
3463
3464 /* Install top nodes. */
3465 install_node(&view_node, NULL);
3466 install_node(&enable_node, NULL);
3467 install_node(&auth_node, NULL);
3468 install_node(&auth_enable_node, NULL);
3469 install_node(&config_node, config_write_host);
3470
3471 /* Each node's basic commands. */
3472 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003473 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003474 if (terminal) {
3475 install_element(VIEW_NODE, &config_list_cmd);
3476 install_element(VIEW_NODE, &config_exit_cmd);
3477 install_element(VIEW_NODE, &config_help_cmd);
3478 install_element(VIEW_NODE, &config_enable_cmd);
3479 install_element(VIEW_NODE, &config_terminal_length_cmd);
3480 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3481 install_element(VIEW_NODE, &echo_cmd);
3482 }
3483
3484 if (terminal) {
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003485 vty_install_default(ENABLE_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003486 install_element(ENABLE_NODE, &config_disable_cmd);
3487 install_element(ENABLE_NODE, &config_terminal_cmd);
3488 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3489 }
3490 install_element (ENABLE_NODE, &show_startup_config_cmd);
3491 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003492 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003493
3494 if (terminal) {
3495 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3496 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3497 install_element(ENABLE_NODE, &echo_cmd);
3498
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003499 vty_install_default(CONFIG_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003500 }
3501
3502 install_element(CONFIG_NODE, &hostname_cmd);
3503 install_element(CONFIG_NODE, &no_hostname_cmd);
3504
3505 if (terminal) {
3506 install_element(CONFIG_NODE, &password_cmd);
3507 install_element(CONFIG_NODE, &password_text_cmd);
3508 install_element(CONFIG_NODE, &enable_password_cmd);
3509 install_element(CONFIG_NODE, &enable_password_text_cmd);
3510 install_element(CONFIG_NODE, &no_enable_password_cmd);
3511
3512#ifdef VTY_CRYPT_PW
3513 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3514 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3515#endif
3516 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3517 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3518 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3519 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3520 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3521
3522 }
3523 srand(time(NULL));
3524}
Harald Welte7acb30c2011-08-17 17:13:48 +02003525
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003526/*! @} */