blob: faa7c51c29672caee85275ca243e78548920221c [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
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800150static int is_config(struct vty *vty)
151{
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
Harald Welte237f6242010-05-25 23:00:45 +02001892 if (host.app_info->go_parent_cb)
1893 host.app_info->go_parent_cb(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001894 else
1895 vty->node = CONFIG_NODE;
1896
1897 return vty->node;
1898}
1899
1900/* Execute command by argument vline vector. */
1901static int
1902cmd_execute_command_real(vector vline, struct vty *vty,
1903 struct cmd_element **cmd)
1904{
1905 unsigned int i;
1906 unsigned int index;
1907 vector cmd_vector;
1908 struct cmd_element *cmd_element;
1909 struct cmd_element *matched_element;
1910 unsigned int matched_count, incomplete_count;
1911 int argc;
1912 const char *argv[CMD_ARGC_MAX];
1913 enum match_type match = 0;
1914 int varflag;
1915 char *command;
1916
1917 /* Make copy of command elements. */
1918 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1919
1920 for (index = 0; index < vector_active(vline); index++)
1921 if ((command = vector_slot(vline, index))) {
1922 int ret;
1923
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001924 match = cmd_filter(command, cmd_vector, index,
1925 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001926
1927 if (match == vararg_match)
1928 break;
1929
1930 ret =
1931 is_cmd_ambiguous(command, cmd_vector, index, match);
1932
1933 if (ret == 1) {
1934 vector_free(cmd_vector);
1935 return CMD_ERR_AMBIGUOUS;
1936 } else if (ret == 2) {
1937 vector_free(cmd_vector);
1938 return CMD_ERR_NO_MATCH;
1939 }
1940 }
1941
1942 /* Check matched count. */
1943 matched_element = NULL;
1944 matched_count = 0;
1945 incomplete_count = 0;
1946
1947 for (i = 0; i < vector_active(cmd_vector); i++)
1948 if ((cmd_element = vector_slot(cmd_vector, i))) {
1949 if (match == vararg_match
1950 || index >= cmd_element->cmdsize) {
1951 matched_element = cmd_element;
1952#if 0
1953 printf("DEBUG: %s\n", cmd_element->string);
1954#endif
1955 matched_count++;
1956 } else {
1957 incomplete_count++;
1958 }
1959 }
1960
1961 /* Finish of using cmd_vector. */
1962 vector_free(cmd_vector);
1963
1964 /* To execute command, matched_count must be 1. */
1965 if (matched_count == 0) {
1966 if (incomplete_count)
1967 return CMD_ERR_INCOMPLETE;
1968 else
1969 return CMD_ERR_NO_MATCH;
1970 }
1971
1972 if (matched_count > 1)
1973 return CMD_ERR_AMBIGUOUS;
1974
1975 /* Argument treatment */
1976 varflag = 0;
1977 argc = 0;
1978
1979 for (i = 0; i < vector_active(vline); i++) {
1980 if (varflag)
1981 argv[argc++] = vector_slot(vline, i);
1982 else {
1983 vector descvec =
1984 vector_slot(matched_element->strvec, i);
1985
1986 if (vector_active(descvec) == 1) {
1987 struct desc *desc = vector_slot(descvec, 0);
1988
1989 if (CMD_VARARG(desc->cmd))
1990 varflag = 1;
1991
1992 if (varflag || CMD_VARIABLE(desc->cmd)
1993 || CMD_OPTION(desc->cmd))
1994 argv[argc++] = vector_slot(vline, i);
1995 } else
1996 argv[argc++] = vector_slot(vline, i);
1997 }
1998
1999 if (argc >= CMD_ARGC_MAX)
2000 return CMD_ERR_EXEED_ARGC_MAX;
2001 }
2002
2003 /* For vtysh execution. */
2004 if (cmd)
2005 *cmd = matched_element;
2006
2007 if (matched_element->daemon)
2008 return CMD_SUCCESS_DAEMON;
2009
2010 /* Execute matched command. */
2011 return (*matched_element->func) (matched_element, vty, argc, argv);
2012}
2013
2014int
2015cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2016 int vtysh)
2017{
2018 int ret, saved_ret, tried = 0;
2019 enum node_type onode;
2020 void *oindex;
2021
2022 onode = vty->node;
2023 oindex = vty->index;
2024
2025 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2026 vector shifted_vline;
2027 unsigned int index;
2028
2029 vty->node = ENABLE_NODE;
2030 /* We can try it on enable node, cos' the vty is authenticated */
2031
2032 shifted_vline = vector_init(vector_count(vline));
2033 /* use memcpy? */
2034 for (index = 1; index < vector_active(vline); index++) {
2035 vector_set_index(shifted_vline, index - 1,
2036 vector_lookup(vline, index));
2037 }
2038
2039 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2040
2041 vector_free(shifted_vline);
2042 vty->node = onode;
2043 return ret;
2044 }
2045
2046 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2047
2048 if (vtysh)
2049 return saved_ret;
2050
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002051 /* Go to parent for config nodes to attempt to find the right command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002052 while (ret != CMD_SUCCESS && ret != CMD_WARNING
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002053 && is_config(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002054 vty_go_parent(vty);
2055 ret = cmd_execute_command_real(vline, vty, cmd);
2056 tried = 1;
2057 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2058 /* succesfull command, leave the node as is */
2059 return ret;
2060 }
2061 }
2062 /* no command succeeded, reset the vty to the original node and
2063 return the error for this node */
2064 if (tried) {
2065 vty->node = onode;
2066 vty->index = oindex;
2067 }
2068 return saved_ret;
2069}
2070
2071/* Execute command by argument readline. */
2072int
2073cmd_execute_command_strict(vector vline, struct vty *vty,
2074 struct cmd_element **cmd)
2075{
2076 unsigned int i;
2077 unsigned int index;
2078 vector cmd_vector;
2079 struct cmd_element *cmd_element;
2080 struct cmd_element *matched_element;
2081 unsigned int matched_count, incomplete_count;
2082 int argc;
2083 const char *argv[CMD_ARGC_MAX];
2084 int varflag;
2085 enum match_type match = 0;
2086 char *command;
2087
2088 /* Make copy of command element */
2089 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2090
2091 for (index = 0; index < vector_active(vline); index++)
2092 if ((command = vector_slot(vline, index))) {
2093 int ret;
2094
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002095 match = cmd_filter(vector_slot(vline, index),
2096 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002097
2098 /* If command meets '.VARARG' then finish matching. */
2099 if (match == vararg_match)
2100 break;
2101
2102 ret =
2103 is_cmd_ambiguous(command, cmd_vector, index, match);
2104 if (ret == 1) {
2105 vector_free(cmd_vector);
2106 return CMD_ERR_AMBIGUOUS;
2107 }
2108 if (ret == 2) {
2109 vector_free(cmd_vector);
2110 return CMD_ERR_NO_MATCH;
2111 }
2112 }
2113
2114 /* Check matched count. */
2115 matched_element = NULL;
2116 matched_count = 0;
2117 incomplete_count = 0;
2118 for (i = 0; i < vector_active(cmd_vector); i++)
2119 if (vector_slot(cmd_vector, i) != NULL) {
2120 cmd_element = vector_slot(cmd_vector, i);
2121
2122 if (match == vararg_match
2123 || index >= cmd_element->cmdsize) {
2124 matched_element = cmd_element;
2125 matched_count++;
2126 } else
2127 incomplete_count++;
2128 }
2129
2130 /* Finish of using cmd_vector. */
2131 vector_free(cmd_vector);
2132
2133 /* To execute command, matched_count must be 1. */
2134 if (matched_count == 0) {
2135 if (incomplete_count)
2136 return CMD_ERR_INCOMPLETE;
2137 else
2138 return CMD_ERR_NO_MATCH;
2139 }
2140
2141 if (matched_count > 1)
2142 return CMD_ERR_AMBIGUOUS;
2143
2144 /* Argument treatment */
2145 varflag = 0;
2146 argc = 0;
2147
2148 for (i = 0; i < vector_active(vline); i++) {
2149 if (varflag)
2150 argv[argc++] = vector_slot(vline, i);
2151 else {
2152 vector descvec =
2153 vector_slot(matched_element->strvec, i);
2154
2155 if (vector_active(descvec) == 1) {
2156 struct desc *desc = vector_slot(descvec, 0);
2157
2158 if (CMD_VARARG(desc->cmd))
2159 varflag = 1;
2160
2161 if (varflag || CMD_VARIABLE(desc->cmd)
2162 || CMD_OPTION(desc->cmd))
2163 argv[argc++] = vector_slot(vline, i);
2164 } else
2165 argv[argc++] = vector_slot(vline, i);
2166 }
2167
2168 if (argc >= CMD_ARGC_MAX)
2169 return CMD_ERR_EXEED_ARGC_MAX;
2170 }
2171
2172 /* For vtysh execution. */
2173 if (cmd)
2174 *cmd = matched_element;
2175
2176 if (matched_element->daemon)
2177 return CMD_SUCCESS_DAEMON;
2178
2179 /* Now execute matched command */
2180 return (*matched_element->func) (matched_element, vty, argc, argv);
2181}
2182
2183/* Configration make from file. */
2184int config_from_file(struct vty *vty, FILE * fp)
2185{
2186 int ret;
2187 vector vline;
2188
2189 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2190 vline = cmd_make_strvec(vty->buf);
2191
2192 /* In case of comment line */
2193 if (vline == NULL)
2194 continue;
2195 /* Execute configuration command : this is strict match */
2196 ret = cmd_execute_command_strict(vline, vty, NULL);
2197
2198 /* Try again with setting node to CONFIG_NODE */
2199 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2200 && ret != CMD_ERR_NOTHING_TODO
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002201 && vty->node != CONFIG_NODE && is_config(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002202 vty_go_parent(vty);
2203 ret = cmd_execute_command_strict(vline, vty, NULL);
2204 }
2205
2206 cmd_free_strvec(vline);
2207
2208 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2209 && ret != CMD_ERR_NOTHING_TODO)
2210 return ret;
2211 }
2212 return CMD_SUCCESS;
2213}
2214
2215/* Configration from terminal */
2216DEFUN(config_terminal,
2217 config_terminal_cmd,
2218 "configure terminal",
2219 "Configuration from vty interface\n" "Configuration terminal\n")
2220{
2221 if (vty_config_lock(vty))
2222 vty->node = CONFIG_NODE;
2223 else {
2224 vty_out(vty, "VTY configuration is locked by other VTY%s",
2225 VTY_NEWLINE);
2226 return CMD_WARNING;
2227 }
2228 return CMD_SUCCESS;
2229}
2230
2231/* Enable command */
2232DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2233{
2234 /* If enable password is NULL, change to ENABLE_NODE */
2235 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2236 vty->type == VTY_SHELL_SERV)
2237 vty->node = ENABLE_NODE;
2238 else
2239 vty->node = AUTH_ENABLE_NODE;
2240
2241 return CMD_SUCCESS;
2242}
2243
2244/* Disable command */
2245DEFUN(disable,
2246 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2247{
2248 if (vty->node == ENABLE_NODE)
2249 vty->node = VIEW_NODE;
2250 return CMD_SUCCESS;
2251}
2252
2253/* Down vty node level. */
2254gDEFUN(config_exit,
2255 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2256{
2257 switch (vty->node) {
2258 case VIEW_NODE:
2259 case ENABLE_NODE:
2260 if (0) //vty_shell (vty))
2261 exit(0);
2262 else
2263 vty->status = VTY_CLOSE;
2264 break;
2265 case CONFIG_NODE:
2266 vty->node = ENABLE_NODE;
2267 vty_config_unlock(vty);
2268 break;
2269 case VTY_NODE:
2270 vty->node = CONFIG_NODE;
2271 break;
Harald Welte28222962011-02-18 20:37:04 +01002272 case CFG_LOG_NODE:
2273 vty->node = CONFIG_NODE;
2274 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002275 default:
2276 break;
2277 }
2278 return CMD_SUCCESS;
2279}
2280
2281/* End of configuration. */
2282 gDEFUN(config_end,
2283 config_end_cmd, "end", "End current mode and change to enable mode.")
2284{
2285 switch (vty->node) {
2286 case VIEW_NODE:
2287 case ENABLE_NODE:
2288 /* Nothing to do. */
2289 break;
Harald Welte28222962011-02-18 20:37:04 +01002290 case CFG_LOG_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002291 case CONFIG_NODE:
2292 case VTY_NODE:
2293 vty_config_unlock(vty);
2294 vty->node = ENABLE_NODE;
2295 break;
2296 default:
2297 break;
2298 }
2299 return CMD_SUCCESS;
2300}
2301
2302/* Show version. */
2303DEFUN(show_version,
2304 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2305{
Harald Welte237f6242010-05-25 23:00:45 +02002306 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2307 host.app_info->version,
2308 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2309 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002310
2311 return CMD_SUCCESS;
2312}
2313
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002314DEFUN(show_online_help,
2315 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2316{
2317 vty_dump_nodes(vty);
2318 return CMD_SUCCESS;
2319}
2320
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002321/* Help display function for all node. */
2322gDEFUN(config_help,
2323 config_help_cmd, "help", "Description of the interactive help system\n")
2324{
2325 vty_out(vty,
2326 "This VTY provides advanced help features. When you need help,%s\
2327anytime at the command line please press '?'.%s\
2328%s\
2329If nothing matches, the help list will be empty and you must backup%s\
2330 until entering a '?' shows the available options.%s\
2331Two styles of help are provided:%s\
23321. Full help is available when you are ready to enter a%s\
2333command argument (e.g. 'show ?') and describes each possible%s\
2334argument.%s\
23352. Partial help is provided when an abbreviated argument is entered%s\
2336 and you want to know what arguments match the input%s\
2337 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2338 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2339 return CMD_SUCCESS;
2340}
2341
2342/* Help display function for all node. */
2343gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2344{
2345 unsigned int i;
2346 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2347 struct cmd_element *cmd;
2348
2349 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2350 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2351 && !(cmd->attr == CMD_ATTR_DEPRECATED
2352 || cmd->attr == CMD_ATTR_HIDDEN))
2353 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2354 return CMD_SUCCESS;
2355}
2356
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002357static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002358{
2359 unsigned int i;
2360 int fd;
2361 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002362 char *config_file_tmp = NULL;
2363 char *config_file_sav = NULL;
2364 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002365 struct stat st;
2366
2367 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002368
2369 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002370 config_file_sav =
2371 _talloc_zero(tall_vty_cmd_ctx,
2372 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2373 "config_file_sav");
2374 strcpy(config_file_sav, config_file);
2375 strcat(config_file_sav, CONF_BACKUP_EXT);
2376
2377 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2378 "config_file_tmp");
2379 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2380
2381 /* Open file to configuration write. */
2382 fd = mkstemp(config_file_tmp);
2383 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002384 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002385 talloc_free(config_file_tmp);
2386 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002387 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002388 }
2389
2390 /* Make vty for configuration file. */
2391 file_vty = vty_new();
2392 file_vty->fd = fd;
2393 file_vty->type = VTY_FILE;
2394
2395 /* Config file header print. */
2396 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002397 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002398 //vty_time_print (file_vty, 1);
2399 vty_out(file_vty, "!\n");
2400
2401 for (i = 0; i < vector_active(cmdvec); i++)
2402 if ((node = vector_slot(cmdvec, i)) && node->func) {
2403 if ((*node->func) (file_vty))
2404 vty_out(file_vty, "!\n");
2405 }
2406 vty_close(file_vty);
2407
2408 if (unlink(config_file_sav) != 0)
2409 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002410 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002411 talloc_free(config_file_sav);
2412 talloc_free(config_file_tmp);
2413 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002414 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002415 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002416
2417 /* Only link the .sav file if the original file exists */
2418 if (stat(config_file, &st) == 0) {
2419 if (link(config_file, config_file_sav) != 0) {
2420 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2421 talloc_free(config_file_sav);
2422 talloc_free(config_file_tmp);
2423 unlink(config_file_tmp);
2424 return -3;
2425 }
2426 sync();
2427 if (unlink(config_file) != 0) {
2428 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2429 talloc_free(config_file_sav);
2430 talloc_free(config_file_tmp);
2431 unlink(config_file_tmp);
2432 return -4;
2433 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002434 }
2435 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002436 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002437 talloc_free(config_file_sav);
2438 talloc_free(config_file_tmp);
2439 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002440 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002441 }
2442 unlink(config_file_tmp);
2443 sync();
2444
2445 talloc_free(config_file_sav);
2446 talloc_free(config_file_tmp);
2447
2448 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002449 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2450 return -6;
2451 }
2452
2453 return 0;
2454}
2455
2456
2457/* Write current configuration into file. */
2458DEFUN(config_write_file,
2459 config_write_file_cmd,
2460 "write file",
2461 "Write running configuration to memory, network, or terminal\n"
2462 "Write to configuration file\n")
2463{
2464 char *failed_file;
2465 int rc;
2466
2467 if (host.config == NULL) {
2468 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2469 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002470 return CMD_WARNING;
2471 }
2472
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002473 rc = write_config_file(host.config, &failed_file);
2474 switch (rc) {
2475 case -1:
2476 vty_out(vty, "Can't open configuration file %s.%s",
2477 failed_file, VTY_NEWLINE);
2478 rc = CMD_WARNING;
2479 break;
2480 case -2:
2481 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2482 failed_file, VTY_NEWLINE);
2483 rc = CMD_WARNING;
2484 break;
2485 case -3:
2486 vty_out(vty, "Can't backup old configuration file %s.%s",
2487 failed_file, VTY_NEWLINE);
2488 rc = CMD_WARNING;
2489 break;
2490 case -4:
2491 vty_out(vty, "Can't unlink configuration file %s.%s",
2492 failed_file, VTY_NEWLINE);
2493 rc = CMD_WARNING;
2494 break;
2495 case -5:
2496 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2497 VTY_NEWLINE);
2498 rc = CMD_WARNING;
2499 break;
2500 case -6:
2501 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2502 failed_file, strerror(errno), errno, VTY_NEWLINE);
2503 rc = CMD_WARNING;
2504 break;
2505 default:
2506 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2507 rc = CMD_SUCCESS;
2508 break;
2509 }
2510
2511 talloc_free(failed_file);
2512 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002513}
2514
2515ALIAS(config_write_file,
2516 config_write_cmd,
2517 "write", "Write running configuration to memory, network, or terminal\n")
2518
2519 ALIAS(config_write_file,
2520 config_write_memory_cmd,
2521 "write memory",
2522 "Write running configuration to memory, network, or terminal\n"
2523 "Write configuration to the file (same as write file)\n")
2524
2525 ALIAS(config_write_file,
2526 copy_runningconfig_startupconfig_cmd,
2527 "copy running-config startup-config",
2528 "Copy configuration\n"
2529 "Copy running config to... \n"
2530 "Copy running config to startup config (same as write file)\n")
2531
2532/* Write current configuration into the terminal. */
2533 DEFUN(config_write_terminal,
2534 config_write_terminal_cmd,
2535 "write terminal",
2536 "Write running configuration to memory, network, or terminal\n"
2537 "Write to terminal\n")
2538{
2539 unsigned int i;
2540 struct cmd_node *node;
2541
2542 if (vty->type == VTY_SHELL_SERV) {
2543 for (i = 0; i < vector_active(cmdvec); i++)
2544 if ((node = vector_slot(cmdvec, i)) && node->func
2545 && node->vtysh) {
2546 if ((*node->func) (vty))
2547 vty_out(vty, "!%s", VTY_NEWLINE);
2548 }
2549 } else {
2550 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2551 VTY_NEWLINE);
2552 vty_out(vty, "!%s", VTY_NEWLINE);
2553
2554 for (i = 0; i < vector_active(cmdvec); i++)
2555 if ((node = vector_slot(cmdvec, i)) && node->func) {
2556 if ((*node->func) (vty))
2557 vty_out(vty, "!%s", VTY_NEWLINE);
2558 }
2559 vty_out(vty, "end%s", VTY_NEWLINE);
2560 }
2561 return CMD_SUCCESS;
2562}
2563
2564/* Write current configuration into the terminal. */
2565ALIAS(config_write_terminal,
2566 show_running_config_cmd,
2567 "show running-config", SHOW_STR "running configuration\n")
2568
2569/* Write startup configuration into the terminal. */
2570 DEFUN(show_startup_config,
2571 show_startup_config_cmd,
2572 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2573{
2574 char buf[BUFSIZ];
2575 FILE *confp;
2576
2577 confp = fopen(host.config, "r");
2578 if (confp == NULL) {
2579 vty_out(vty, "Can't open configuration file [%s]%s",
2580 host.config, VTY_NEWLINE);
2581 return CMD_WARNING;
2582 }
2583
2584 while (fgets(buf, BUFSIZ, confp)) {
2585 char *cp = buf;
2586
2587 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2588 cp++;
2589 *cp = '\0';
2590
2591 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2592 }
2593
2594 fclose(confp);
2595
2596 return CMD_SUCCESS;
2597}
2598
2599/* Hostname configuration */
2600DEFUN(config_hostname,
2601 hostname_cmd,
2602 "hostname WORD",
2603 "Set system's network name\n" "This system's network name\n")
2604{
2605 if (!isalpha((int)*argv[0])) {
2606 vty_out(vty, "Please specify string starting with alphabet%s",
2607 VTY_NEWLINE);
2608 return CMD_WARNING;
2609 }
2610
2611 if (host.name)
2612 talloc_free(host.name);
2613
2614 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2615 return CMD_SUCCESS;
2616}
2617
2618DEFUN(config_no_hostname,
2619 no_hostname_cmd,
2620 "no hostname [HOSTNAME]",
2621 NO_STR "Reset system's network name\n" "Host name of this router\n")
2622{
2623 if (host.name)
2624 talloc_free(host.name);
2625 host.name = NULL;
2626 return CMD_SUCCESS;
2627}
2628
2629/* VTY interface password set. */
2630DEFUN(config_password, password_cmd,
2631 "password (8|) WORD",
2632 "Assign the terminal connection password\n"
2633 "Specifies a HIDDEN password will follow\n"
2634 "dummy string \n" "The HIDDEN line password string\n")
2635{
2636 /* Argument check. */
2637 if (argc == 0) {
2638 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2639 return CMD_WARNING;
2640 }
2641
2642 if (argc == 2) {
2643 if (*argv[0] == '8') {
2644 if (host.password)
2645 talloc_free(host.password);
2646 host.password = NULL;
2647 if (host.password_encrypt)
2648 talloc_free(host.password_encrypt);
2649 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2650 return CMD_SUCCESS;
2651 } else {
2652 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2653 return CMD_WARNING;
2654 }
2655 }
2656
2657 if (!isalnum((int)*argv[0])) {
2658 vty_out(vty,
2659 "Please specify string starting with alphanumeric%s",
2660 VTY_NEWLINE);
2661 return CMD_WARNING;
2662 }
2663
2664 if (host.password)
2665 talloc_free(host.password);
2666 host.password = NULL;
2667
2668#ifdef VTY_CRYPT_PW
2669 if (host.encrypt) {
2670 if (host.password_encrypt)
2671 talloc_free(host.password_encrypt);
2672 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2673 } else
2674#endif
2675 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2676
2677 return CMD_SUCCESS;
2678}
2679
2680ALIAS(config_password, password_text_cmd,
2681 "password LINE",
2682 "Assign the terminal connection password\n"
2683 "The UNENCRYPTED (cleartext) line password\n")
2684
2685/* VTY enable password set. */
2686 DEFUN(config_enable_password, enable_password_cmd,
2687 "enable password (8|) WORD",
2688 "Modify enable password parameters\n"
2689 "Assign the privileged level password\n"
2690 "Specifies a HIDDEN password will follow\n"
2691 "dummy string \n" "The HIDDEN 'enable' password string\n")
2692{
2693 /* Argument check. */
2694 if (argc == 0) {
2695 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2696 return CMD_WARNING;
2697 }
2698
2699 /* Crypt type is specified. */
2700 if (argc == 2) {
2701 if (*argv[0] == '8') {
2702 if (host.enable)
2703 talloc_free(host.enable);
2704 host.enable = NULL;
2705
2706 if (host.enable_encrypt)
2707 talloc_free(host.enable_encrypt);
2708 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2709
2710 return CMD_SUCCESS;
2711 } else {
2712 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2713 return CMD_WARNING;
2714 }
2715 }
2716
2717 if (!isalnum((int)*argv[0])) {
2718 vty_out(vty,
2719 "Please specify string starting with alphanumeric%s",
2720 VTY_NEWLINE);
2721 return CMD_WARNING;
2722 }
2723
2724 if (host.enable)
2725 talloc_free(host.enable);
2726 host.enable = NULL;
2727
2728 /* Plain password input. */
2729#ifdef VTY_CRYPT_PW
2730 if (host.encrypt) {
2731 if (host.enable_encrypt)
2732 talloc_free(host.enable_encrypt);
2733 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2734 } else
2735#endif
2736 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2737
2738 return CMD_SUCCESS;
2739}
2740
2741ALIAS(config_enable_password,
2742 enable_password_text_cmd,
2743 "enable password LINE",
2744 "Modify enable password parameters\n"
2745 "Assign the privileged level password\n"
2746 "The UNENCRYPTED (cleartext) 'enable' password\n")
2747
2748/* VTY enable password delete. */
2749 DEFUN(no_config_enable_password, no_enable_password_cmd,
2750 "no enable password",
2751 NO_STR
2752 "Modify enable password parameters\n"
2753 "Assign the privileged level password\n")
2754{
2755 if (host.enable)
2756 talloc_free(host.enable);
2757 host.enable = NULL;
2758
2759 if (host.enable_encrypt)
2760 talloc_free(host.enable_encrypt);
2761 host.enable_encrypt = NULL;
2762
2763 return CMD_SUCCESS;
2764}
2765
2766#ifdef VTY_CRYPT_PW
2767DEFUN(service_password_encrypt,
2768 service_password_encrypt_cmd,
2769 "service password-encryption",
2770 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2771{
2772 if (host.encrypt)
2773 return CMD_SUCCESS;
2774
2775 host.encrypt = 1;
2776
2777 if (host.password) {
2778 if (host.password_encrypt)
2779 talloc_free(host.password_encrypt);
2780 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2781 }
2782 if (host.enable) {
2783 if (host.enable_encrypt)
2784 talloc_free(host.enable_encrypt);
2785 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2786 }
2787
2788 return CMD_SUCCESS;
2789}
2790
2791DEFUN(no_service_password_encrypt,
2792 no_service_password_encrypt_cmd,
2793 "no service password-encryption",
2794 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2795{
2796 if (!host.encrypt)
2797 return CMD_SUCCESS;
2798
2799 host.encrypt = 0;
2800
2801 if (host.password_encrypt)
2802 talloc_free(host.password_encrypt);
2803 host.password_encrypt = NULL;
2804
2805 if (host.enable_encrypt)
2806 talloc_free(host.enable_encrypt);
2807 host.enable_encrypt = NULL;
2808
2809 return CMD_SUCCESS;
2810}
2811#endif
2812
2813DEFUN(config_terminal_length, config_terminal_length_cmd,
2814 "terminal length <0-512>",
2815 "Set terminal line parameters\n"
2816 "Set number of lines on a screen\n"
2817 "Number of lines on screen (0 for no pausing)\n")
2818{
2819 int lines;
2820 char *endptr = NULL;
2821
2822 lines = strtol(argv[0], &endptr, 10);
2823 if (lines < 0 || lines > 512 || *endptr != '\0') {
2824 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2825 return CMD_WARNING;
2826 }
2827 vty->lines = lines;
2828
2829 return CMD_SUCCESS;
2830}
2831
2832DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2833 "terminal no length",
2834 "Set terminal line parameters\n"
2835 NO_STR "Set number of lines on a screen\n")
2836{
2837 vty->lines = -1;
2838 return CMD_SUCCESS;
2839}
2840
2841DEFUN(service_terminal_length, service_terminal_length_cmd,
2842 "service terminal-length <0-512>",
2843 "Set up miscellaneous service\n"
2844 "System wide terminal length configuration\n"
2845 "Number of lines of VTY (0 means no line control)\n")
2846{
2847 int lines;
2848 char *endptr = NULL;
2849
2850 lines = strtol(argv[0], &endptr, 10);
2851 if (lines < 0 || lines > 512 || *endptr != '\0') {
2852 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2853 return CMD_WARNING;
2854 }
2855 host.lines = lines;
2856
2857 return CMD_SUCCESS;
2858}
2859
2860DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2861 "no service terminal-length [<0-512>]",
2862 NO_STR
2863 "Set up miscellaneous service\n"
2864 "System wide terminal length configuration\n"
2865 "Number of lines of VTY (0 means no line control)\n")
2866{
2867 host.lines = -1;
2868 return CMD_SUCCESS;
2869}
2870
2871DEFUN_HIDDEN(do_echo,
2872 echo_cmd,
2873 "echo .MESSAGE",
2874 "Echo a message back to the vty\n" "The message to echo\n")
2875{
2876 char *message;
2877
2878 vty_out(vty, "%s%s",
2879 ((message =
2880 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2881 if (message)
2882 talloc_free(message);
2883 return CMD_SUCCESS;
2884}
2885
2886#if 0
2887DEFUN(config_logmsg,
2888 config_logmsg_cmd,
2889 "logmsg " LOG_LEVELS " .MESSAGE",
2890 "Send a message to enabled logging destinations\n"
2891 LOG_LEVEL_DESC "The message to send\n")
2892{
2893 int level;
2894 char *message;
2895
2896 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2897 return CMD_ERR_NO_MATCH;
2898
2899 zlog(NULL, level,
2900 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2901 if (message)
2902 talloc_free(message);
2903 return CMD_SUCCESS;
2904}
2905
2906DEFUN(show_logging,
2907 show_logging_cmd,
2908 "show logging", SHOW_STR "Show current logging configuration\n")
2909{
2910 struct zlog *zl = zlog_default;
2911
2912 vty_out(vty, "Syslog logging: ");
2913 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2914 vty_out(vty, "disabled");
2915 else
2916 vty_out(vty, "level %s, facility %s, ident %s",
2917 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2918 facility_name(zl->facility), zl->ident);
2919 vty_out(vty, "%s", VTY_NEWLINE);
2920
2921 vty_out(vty, "Stdout logging: ");
2922 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2923 vty_out(vty, "disabled");
2924 else
2925 vty_out(vty, "level %s",
2926 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2927 vty_out(vty, "%s", VTY_NEWLINE);
2928
2929 vty_out(vty, "Monitor logging: ");
2930 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2931 vty_out(vty, "disabled");
2932 else
2933 vty_out(vty, "level %s",
2934 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2935 vty_out(vty, "%s", VTY_NEWLINE);
2936
2937 vty_out(vty, "File logging: ");
2938 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2939 vty_out(vty, "disabled");
2940 else
2941 vty_out(vty, "level %s, filename %s",
2942 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2943 zl->filename);
2944 vty_out(vty, "%s", VTY_NEWLINE);
2945
2946 vty_out(vty, "Protocol name: %s%s",
2947 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2948 vty_out(vty, "Record priority: %s%s",
2949 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2950
2951 return CMD_SUCCESS;
2952}
2953
2954DEFUN(config_log_stdout,
2955 config_log_stdout_cmd,
2956 "log stdout", "Logging control\n" "Set stdout logging level\n")
2957{
2958 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2959 return CMD_SUCCESS;
2960}
2961
2962DEFUN(config_log_stdout_level,
2963 config_log_stdout_level_cmd,
2964 "log stdout " LOG_LEVELS,
2965 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2966{
2967 int level;
2968
2969 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2970 return CMD_ERR_NO_MATCH;
2971 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
2972 return CMD_SUCCESS;
2973}
2974
2975DEFUN(no_config_log_stdout,
2976 no_config_log_stdout_cmd,
2977 "no log stdout [LEVEL]",
2978 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
2979{
2980 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2981 return CMD_SUCCESS;
2982}
2983
2984DEFUN(config_log_monitor,
2985 config_log_monitor_cmd,
2986 "log monitor",
2987 "Logging control\n" "Set terminal line (monitor) logging level\n")
2988{
2989 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2990 return CMD_SUCCESS;
2991}
2992
2993DEFUN(config_log_monitor_level,
2994 config_log_monitor_level_cmd,
2995 "log monitor " LOG_LEVELS,
2996 "Logging control\n"
2997 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2998{
2999 int level;
3000
3001 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3002 return CMD_ERR_NO_MATCH;
3003 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3004 return CMD_SUCCESS;
3005}
3006
3007DEFUN(no_config_log_monitor,
3008 no_config_log_monitor_cmd,
3009 "no log monitor [LEVEL]",
3010 NO_STR
3011 "Logging control\n"
3012 "Disable terminal line (monitor) logging\n" "Logging level\n")
3013{
3014 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3015 return CMD_SUCCESS;
3016}
3017
3018static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3019{
3020 int ret;
3021 char *p = NULL;
3022 const char *fullpath;
3023
3024 /* Path detection. */
3025 if (!IS_DIRECTORY_SEP(*fname)) {
3026 char cwd[MAXPATHLEN + 1];
3027 cwd[MAXPATHLEN] = '\0';
3028
3029 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3030 zlog_err("config_log_file: Unable to alloc mem!");
3031 return CMD_WARNING;
3032 }
3033
3034 if ((p = _talloc_zero(tall_vcmd_ctx,
3035 strlen(cwd) + strlen(fname) + 2),
3036 "set_log_file")
3037 == NULL) {
3038 zlog_err("config_log_file: Unable to alloc mem!");
3039 return CMD_WARNING;
3040 }
3041 sprintf(p, "%s/%s", cwd, fname);
3042 fullpath = p;
3043 } else
3044 fullpath = fname;
3045
3046 ret = zlog_set_file(NULL, fullpath, loglevel);
3047
3048 if (p)
3049 talloc_free(p);
3050
3051 if (!ret) {
3052 vty_out(vty, "can't open logfile %s\n", fname);
3053 return CMD_WARNING;
3054 }
3055
3056 if (host.logfile)
3057 talloc_free(host.logfile);
3058
3059 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3060
3061 return CMD_SUCCESS;
3062}
3063
3064DEFUN(config_log_file,
3065 config_log_file_cmd,
3066 "log file FILENAME",
3067 "Logging control\n" "Logging to file\n" "Logging filename\n")
3068{
3069 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3070}
3071
3072DEFUN(config_log_file_level,
3073 config_log_file_level_cmd,
3074 "log file FILENAME " LOG_LEVELS,
3075 "Logging control\n"
3076 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3077{
3078 int level;
3079
3080 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3081 return CMD_ERR_NO_MATCH;
3082 return set_log_file(vty, argv[0], level);
3083}
3084
3085DEFUN(no_config_log_file,
3086 no_config_log_file_cmd,
3087 "no log file [FILENAME]",
3088 NO_STR
3089 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3090{
3091 zlog_reset_file(NULL);
3092
3093 if (host.logfile)
3094 talloc_free(host.logfile);
3095
3096 host.logfile = NULL;
3097
3098 return CMD_SUCCESS;
3099}
3100
3101ALIAS(no_config_log_file,
3102 no_config_log_file_level_cmd,
3103 "no log file FILENAME LEVEL",
3104 NO_STR
3105 "Logging control\n"
3106 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3107
3108 DEFUN(config_log_syslog,
3109 config_log_syslog_cmd,
3110 "log syslog", "Logging control\n" "Set syslog logging level\n")
3111{
3112 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3113 return CMD_SUCCESS;
3114}
3115
3116DEFUN(config_log_syslog_level,
3117 config_log_syslog_level_cmd,
3118 "log syslog " LOG_LEVELS,
3119 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3120{
3121 int level;
3122
3123 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3124 return CMD_ERR_NO_MATCH;
3125 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3126 return CMD_SUCCESS;
3127}
3128
3129DEFUN_DEPRECATED(config_log_syslog_facility,
3130 config_log_syslog_facility_cmd,
3131 "log syslog facility " LOG_FACILITIES,
3132 "Logging control\n"
3133 "Logging goes to syslog\n"
3134 "(Deprecated) Facility parameter for syslog messages\n"
3135 LOG_FACILITY_DESC)
3136{
3137 int facility;
3138
3139 if ((facility = facility_match(argv[0])) < 0)
3140 return CMD_ERR_NO_MATCH;
3141
3142 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3143 zlog_default->facility = facility;
3144 return CMD_SUCCESS;
3145}
3146
3147DEFUN(no_config_log_syslog,
3148 no_config_log_syslog_cmd,
3149 "no log syslog [LEVEL]",
3150 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3151{
3152 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3153 return CMD_SUCCESS;
3154}
3155
3156ALIAS(no_config_log_syslog,
3157 no_config_log_syslog_facility_cmd,
3158 "no log syslog facility " LOG_FACILITIES,
3159 NO_STR
3160 "Logging control\n"
3161 "Logging goes to syslog\n"
3162 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3163
3164 DEFUN(config_log_facility,
3165 config_log_facility_cmd,
3166 "log facility " LOG_FACILITIES,
3167 "Logging control\n"
3168 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3169{
3170 int facility;
3171
3172 if ((facility = facility_match(argv[0])) < 0)
3173 return CMD_ERR_NO_MATCH;
3174 zlog_default->facility = facility;
3175 return CMD_SUCCESS;
3176}
3177
3178DEFUN(no_config_log_facility,
3179 no_config_log_facility_cmd,
3180 "no log facility [FACILITY]",
3181 NO_STR
3182 "Logging control\n"
3183 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3184{
3185 zlog_default->facility = LOG_DAEMON;
3186 return CMD_SUCCESS;
3187}
3188
3189DEFUN_DEPRECATED(config_log_trap,
3190 config_log_trap_cmd,
3191 "log trap " LOG_LEVELS,
3192 "Logging control\n"
3193 "(Deprecated) Set logging level and default for all destinations\n"
3194 LOG_LEVEL_DESC)
3195{
3196 int new_level;
3197 int i;
3198
3199 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3200 return CMD_ERR_NO_MATCH;
3201
3202 zlog_default->default_lvl = new_level;
3203 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3204 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3205 zlog_default->maxlvl[i] = new_level;
3206 return CMD_SUCCESS;
3207}
3208
3209DEFUN_DEPRECATED(no_config_log_trap,
3210 no_config_log_trap_cmd,
3211 "no log trap [LEVEL]",
3212 NO_STR
3213 "Logging control\n"
3214 "Permit all logging information\n" "Logging level\n")
3215{
3216 zlog_default->default_lvl = LOG_DEBUG;
3217 return CMD_SUCCESS;
3218}
3219
3220DEFUN(config_log_record_priority,
3221 config_log_record_priority_cmd,
3222 "log record-priority",
3223 "Logging control\n"
3224 "Log the priority of the message within the message\n")
3225{
3226 zlog_default->record_priority = 1;
3227 return CMD_SUCCESS;
3228}
3229
3230DEFUN(no_config_log_record_priority,
3231 no_config_log_record_priority_cmd,
3232 "no log record-priority",
3233 NO_STR
3234 "Logging control\n"
3235 "Do not log the priority of the message within the message\n")
3236{
3237 zlog_default->record_priority = 0;
3238 return CMD_SUCCESS;
3239}
3240#endif
3241
3242DEFUN(banner_motd_file,
3243 banner_motd_file_cmd,
3244 "banner motd file [FILE]",
3245 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3246{
3247 if (host.motdfile)
3248 talloc_free(host.motdfile);
3249 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3250
3251 return CMD_SUCCESS;
3252}
3253
3254DEFUN(banner_motd_default,
3255 banner_motd_default_cmd,
3256 "banner motd default",
3257 "Set banner string\n" "Strings for motd\n" "Default string\n")
3258{
3259 host.motd = default_motd;
3260 return CMD_SUCCESS;
3261}
3262
3263DEFUN(no_banner_motd,
3264 no_banner_motd_cmd,
3265 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3266{
3267 host.motd = NULL;
3268 if (host.motdfile)
3269 talloc_free(host.motdfile);
3270 host.motdfile = NULL;
3271 return CMD_SUCCESS;
3272}
3273
3274/* Set config filename. Called from vty.c */
3275void host_config_set(const char *filename)
3276{
3277 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3278}
3279
3280void install_default(enum node_type node)
3281{
3282 install_element(node, &config_help_cmd);
3283 install_element(node, &config_list_cmd);
3284
3285 install_element(node, &config_write_terminal_cmd);
3286 install_element(node, &config_write_file_cmd);
3287 install_element(node, &config_write_memory_cmd);
3288 install_element(node, &config_write_cmd);
3289 install_element(node, &show_running_config_cmd);
3290}
3291
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003292/**
3293 * \brief Write the current running config to a given file
3294 * \param[in] vty the vty of the code
3295 * \param[in] filename where to store the file
3296 * \return 0 in case of success.
3297 *
3298 * If the filename already exists create a filename.sav
3299 * version with the current code.
3300 *
3301 */
3302int osmo_vty_write_config_file(const char *filename)
3303{
3304 char *failed_file;
3305 int rc;
3306
3307 rc = write_config_file(filename, &failed_file);
3308 talloc_free(failed_file);
3309 return rc;
3310}
3311
3312/**
3313 * \brief Save the current state to the config file
3314 * \return 0 in case of success.
3315 *
3316 * If the filename already exists create a filename.sav
3317 * version with the current code.
3318 *
3319 */
3320int osmo_vty_save_config_file(void)
3321{
3322 char *failed_file;
3323 int rc;
3324
3325 if (host.config == NULL)
3326 return -7;
3327
3328 rc = write_config_file(host.config, &failed_file);
3329 talloc_free(failed_file);
3330 return rc;
3331}
3332
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003333/* Initialize command interface. Install basic nodes and commands. */
3334void cmd_init(int terminal)
3335{
3336 /* Allocate initial top vector of commands. */
3337 cmdvec = vector_init(VECTOR_MIN_SIZE);
3338
3339 /* Default host value settings. */
3340 host.name = NULL;
3341 host.password = NULL;
3342 host.enable = NULL;
3343 host.logfile = NULL;
3344 host.config = NULL;
3345 host.lines = -1;
3346 host.motd = default_motd;
3347 host.motdfile = NULL;
3348
3349 /* Install top nodes. */
3350 install_node(&view_node, NULL);
3351 install_node(&enable_node, NULL);
3352 install_node(&auth_node, NULL);
3353 install_node(&auth_enable_node, NULL);
3354 install_node(&config_node, config_write_host);
3355
3356 /* Each node's basic commands. */
3357 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003358 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003359 if (terminal) {
3360 install_element(VIEW_NODE, &config_list_cmd);
3361 install_element(VIEW_NODE, &config_exit_cmd);
3362 install_element(VIEW_NODE, &config_help_cmd);
3363 install_element(VIEW_NODE, &config_enable_cmd);
3364 install_element(VIEW_NODE, &config_terminal_length_cmd);
3365 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3366 install_element(VIEW_NODE, &echo_cmd);
3367 }
3368
3369 if (terminal) {
3370 install_element(ENABLE_NODE, &config_exit_cmd);
3371 install_default(ENABLE_NODE);
3372 install_element(ENABLE_NODE, &config_disable_cmd);
3373 install_element(ENABLE_NODE, &config_terminal_cmd);
3374 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3375 }
3376 install_element (ENABLE_NODE, &show_startup_config_cmd);
3377 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003378 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003379
3380 if (terminal) {
3381 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3382 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3383 install_element(ENABLE_NODE, &echo_cmd);
3384
3385 install_default(CONFIG_NODE);
3386 install_element(CONFIG_NODE, &config_exit_cmd);
3387 }
3388
3389 install_element(CONFIG_NODE, &hostname_cmd);
3390 install_element(CONFIG_NODE, &no_hostname_cmd);
3391
3392 if (terminal) {
3393 install_element(CONFIG_NODE, &password_cmd);
3394 install_element(CONFIG_NODE, &password_text_cmd);
3395 install_element(CONFIG_NODE, &enable_password_cmd);
3396 install_element(CONFIG_NODE, &enable_password_text_cmd);
3397 install_element(CONFIG_NODE, &no_enable_password_cmd);
3398
3399#ifdef VTY_CRYPT_PW
3400 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3401 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3402#endif
3403 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3404 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3405 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3406 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3407 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3408
3409 }
3410 srand(time(NULL));
3411}
Harald Welte7acb30c2011-08-17 17:13:48 +02003412
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003413/*! @} */