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