blob: ac731ee0683e45113df38f0338d740b099679955 [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);
1577 *status = CMD_ERR_AMBIGUOUS;
1578 return NULL;
1579 } else if (ret == 2) {
1580 vector_free(cmd_vector);
1581 *status = CMD_ERR_NO_MATCH;
1582 return NULL;
1583 }
1584 }
1585
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001586 /* Prepare match vector */
1587 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1588
1589 /* Make sure that cmd_vector is filtered based on current word */
1590 command = vector_slot(vline, index);
1591 if (command)
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001592 match = cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001593
1594 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001595 for (i = 0; i < vector_active(cmd_vector); i++) {
1596 const char *string = NULL;
1597 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001598
Harald Welte80d30fe2013-02-12 11:08:57 +01001599 cmd_element = vector_slot(cmd_vector, i);
1600 if (!cmd_element)
1601 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001602
Harald Welte80d30fe2013-02-12 11:08:57 +01001603 strvec = cmd_element->strvec;
1604
1605 /* if command is NULL, index may be equal to vector_active */
1606 if (command && index >= vector_active(strvec))
1607 vector_slot(cmd_vector, i) = NULL;
1608 else {
1609 /* Check if command is completed. */
1610 if (command == NULL
1611 && index == vector_active(strvec)) {
1612 string = "<cr>";
1613 if (!desc_unique_string(matchvec, string))
1614 vector_set(matchvec, &desc_cr);
1615 } else {
1616 unsigned int j;
1617 vector descvec = vector_slot(strvec, index);
1618 struct desc *desc;
1619
1620 for (j = 0; j < vector_active(descvec); j++) {
1621 desc = vector_slot(descvec, j);
1622 if (!desc)
1623 continue;
1624 string = cmd_entry_function_desc
1625 (command, desc->cmd);
1626 if (!string)
1627 continue;
1628 /* Uniqueness check */
1629 if (!desc_unique_string(matchvec, string))
1630 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001631 }
1632 }
1633 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001634 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001635 vector_free(cmd_vector);
1636
1637 if (vector_slot(matchvec, 0) == NULL) {
1638 vector_free(matchvec);
1639 *status = CMD_ERR_NO_MATCH;
1640 } else
1641 *status = CMD_SUCCESS;
1642
1643 return matchvec;
1644}
1645
1646vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1647{
1648 vector ret;
1649
1650 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1651 enum node_type onode;
1652 vector shifted_vline;
1653 unsigned int index;
1654
1655 onode = vty->node;
1656 vty->node = ENABLE_NODE;
1657 /* We can try it on enable node, cos' the vty is authenticated */
1658
1659 shifted_vline = vector_init(vector_count(vline));
1660 /* use memcpy? */
1661 for (index = 1; index < vector_active(vline); index++) {
1662 vector_set_index(shifted_vline, index - 1,
1663 vector_lookup(vline, index));
1664 }
1665
1666 ret = cmd_describe_command_real(shifted_vline, vty, status);
1667
1668 vector_free(shifted_vline);
1669 vty->node = onode;
1670 return ret;
1671 }
1672
1673 return cmd_describe_command_real(vline, vty, status);
1674}
1675
1676/* Check LCD of matched command. */
1677static int cmd_lcd(char **matched)
1678{
1679 int i;
1680 int j;
1681 int lcd = -1;
1682 char *s1, *s2;
1683 char c1, c2;
1684
1685 if (matched[0] == NULL || matched[1] == NULL)
1686 return 0;
1687
1688 for (i = 1; matched[i] != NULL; i++) {
1689 s1 = matched[i - 1];
1690 s2 = matched[i];
1691
1692 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1693 if (c1 != c2)
1694 break;
1695
1696 if (lcd < 0)
1697 lcd = j;
1698 else {
1699 if (lcd > j)
1700 lcd = j;
1701 }
1702 }
1703 return lcd;
1704}
1705
1706/* Command line completion support. */
1707static char **cmd_complete_command_real(vector vline, struct vty *vty,
1708 int *status)
1709{
1710 unsigned int i;
1711 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1712#define INIT_MATCHVEC_SIZE 10
1713 vector matchvec;
1714 struct cmd_element *cmd_element;
1715 unsigned int index;
1716 char **match_str;
1717 struct desc *desc;
1718 vector descvec;
1719 char *command;
1720 int lcd;
1721
1722 if (vector_active(vline) == 0) {
1723 *status = CMD_ERR_NO_MATCH;
1724 return NULL;
1725 } else
1726 index = vector_active(vline) - 1;
1727
1728 /* First, filter by preceeding command string */
1729 for (i = 0; i < index; i++)
1730 if ((command = vector_slot(vline, i))) {
1731 enum match_type match;
1732 int ret;
1733
1734 /* First try completion match, if there is exactly match return 1 */
1735 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001736 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001737
1738 /* If there is exact match then filter ambiguous match else check
1739 ambiguousness. */
1740 if ((ret =
1741 is_cmd_ambiguous(command, cmd_vector, i,
1742 match)) == 1) {
1743 vector_free(cmd_vector);
1744 *status = CMD_ERR_AMBIGUOUS;
1745 return NULL;
1746 }
1747 /*
1748 else if (ret == 2)
1749 {
1750 vector_free (cmd_vector);
1751 *status = CMD_ERR_NO_MATCH;
1752 return NULL;
1753 }
1754 */
1755 }
1756
1757 /* Prepare match vector. */
1758 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1759
1760 /* Now we got into completion */
1761 for (i = 0; i < vector_active(cmd_vector); i++)
1762 if ((cmd_element = vector_slot(cmd_vector, i))) {
1763 const char *string;
1764 vector strvec = cmd_element->strvec;
1765
1766 /* Check field length */
1767 if (index >= vector_active(strvec))
1768 vector_slot(cmd_vector, i) = NULL;
1769 else {
1770 unsigned int j;
1771
1772 descvec = vector_slot(strvec, index);
1773 for (j = 0; j < vector_active(descvec); j++)
1774 if ((desc = vector_slot(descvec, j))) {
1775 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1776 if (cmd_unique_string (matchvec, string))
1777 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1778 }
1779 }
1780 }
1781
1782 /* We don't need cmd_vector any more. */
1783 vector_free(cmd_vector);
1784
1785 /* No matched command */
1786 if (vector_slot(matchvec, 0) == NULL) {
1787 vector_free(matchvec);
1788
1789 /* In case of 'command \t' pattern. Do you need '?' command at
1790 the end of the line. */
1791 if (vector_slot(vline, index) == '\0')
1792 *status = CMD_ERR_NOTHING_TODO;
1793 else
1794 *status = CMD_ERR_NO_MATCH;
1795 return NULL;
1796 }
1797
1798 /* Only one matched */
1799 if (vector_slot(matchvec, 1) == NULL) {
1800 match_str = (char **)matchvec->index;
1801 vector_only_wrapper_free(matchvec);
1802 *status = CMD_COMPLETE_FULL_MATCH;
1803 return match_str;
1804 }
1805 /* Make it sure last element is NULL. */
1806 vector_set(matchvec, NULL);
1807
1808 /* Check LCD of matched strings. */
1809 if (vector_slot(vline, index) != NULL) {
1810 lcd = cmd_lcd((char **)matchvec->index);
1811
1812 if (lcd) {
1813 int len = strlen(vector_slot(vline, index));
1814
1815 if (len < lcd) {
1816 char *lcdstr;
1817
1818 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1819 "complete-lcdstr");
1820 memcpy(lcdstr, matchvec->index[0], lcd);
1821 lcdstr[lcd] = '\0';
1822
1823 /* match_str = (char **) &lcdstr; */
1824
1825 /* Free matchvec. */
1826 for (i = 0; i < vector_active(matchvec); i++) {
1827 if (vector_slot(matchvec, i))
1828 talloc_free(vector_slot(matchvec, i));
1829 }
1830 vector_free(matchvec);
1831
1832 /* Make new matchvec. */
1833 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1834 vector_set(matchvec, lcdstr);
1835 match_str = (char **)matchvec->index;
1836 vector_only_wrapper_free(matchvec);
1837
1838 *status = CMD_COMPLETE_MATCH;
1839 return match_str;
1840 }
1841 }
1842 }
1843
1844 match_str = (char **)matchvec->index;
1845 vector_only_wrapper_free(matchvec);
1846 *status = CMD_COMPLETE_LIST_MATCH;
1847 return match_str;
1848}
1849
1850char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1851{
1852 char **ret;
1853
1854 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1855 enum node_type onode;
1856 vector shifted_vline;
1857 unsigned int index;
1858
1859 onode = vty->node;
1860 vty->node = ENABLE_NODE;
1861 /* We can try it on enable node, cos' the vty is authenticated */
1862
1863 shifted_vline = vector_init(vector_count(vline));
1864 /* use memcpy? */
1865 for (index = 1; index < vector_active(vline); index++) {
1866 vector_set_index(shifted_vline, index - 1,
1867 vector_lookup(vline, index));
1868 }
1869
1870 ret = cmd_complete_command_real(shifted_vline, vty, status);
1871
1872 vector_free(shifted_vline);
1873 vty->node = onode;
1874 return ret;
1875 }
1876
1877 return cmd_complete_command_real(vline, vty, status);
1878}
1879
1880/* return parent node */
1881/* MUST eventually converge on CONFIG_NODE */
1882enum node_type vty_go_parent(struct vty *vty)
1883{
1884 assert(vty->node > CONFIG_NODE);
1885
Harald Welte237f6242010-05-25 23:00:45 +02001886 if (host.app_info->go_parent_cb)
1887 host.app_info->go_parent_cb(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001888 else
1889 vty->node = CONFIG_NODE;
1890
1891 return vty->node;
1892}
1893
1894/* Execute command by argument vline vector. */
1895static int
1896cmd_execute_command_real(vector vline, struct vty *vty,
1897 struct cmd_element **cmd)
1898{
1899 unsigned int i;
1900 unsigned int index;
1901 vector cmd_vector;
1902 struct cmd_element *cmd_element;
1903 struct cmd_element *matched_element;
1904 unsigned int matched_count, incomplete_count;
1905 int argc;
1906 const char *argv[CMD_ARGC_MAX];
1907 enum match_type match = 0;
1908 int varflag;
1909 char *command;
1910
1911 /* Make copy of command elements. */
1912 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1913
1914 for (index = 0; index < vector_active(vline); index++)
1915 if ((command = vector_slot(vline, index))) {
1916 int ret;
1917
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001918 match = cmd_filter(command, cmd_vector, index,
1919 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001920
1921 if (match == vararg_match)
1922 break;
1923
1924 ret =
1925 is_cmd_ambiguous(command, cmd_vector, index, match);
1926
1927 if (ret == 1) {
1928 vector_free(cmd_vector);
1929 return CMD_ERR_AMBIGUOUS;
1930 } else if (ret == 2) {
1931 vector_free(cmd_vector);
1932 return CMD_ERR_NO_MATCH;
1933 }
1934 }
1935
1936 /* Check matched count. */
1937 matched_element = NULL;
1938 matched_count = 0;
1939 incomplete_count = 0;
1940
1941 for (i = 0; i < vector_active(cmd_vector); i++)
1942 if ((cmd_element = vector_slot(cmd_vector, i))) {
1943 if (match == vararg_match
1944 || index >= cmd_element->cmdsize) {
1945 matched_element = cmd_element;
1946#if 0
1947 printf("DEBUG: %s\n", cmd_element->string);
1948#endif
1949 matched_count++;
1950 } else {
1951 incomplete_count++;
1952 }
1953 }
1954
1955 /* Finish of using cmd_vector. */
1956 vector_free(cmd_vector);
1957
1958 /* To execute command, matched_count must be 1. */
1959 if (matched_count == 0) {
1960 if (incomplete_count)
1961 return CMD_ERR_INCOMPLETE;
1962 else
1963 return CMD_ERR_NO_MATCH;
1964 }
1965
1966 if (matched_count > 1)
1967 return CMD_ERR_AMBIGUOUS;
1968
1969 /* Argument treatment */
1970 varflag = 0;
1971 argc = 0;
1972
1973 for (i = 0; i < vector_active(vline); i++) {
1974 if (varflag)
1975 argv[argc++] = vector_slot(vline, i);
1976 else {
1977 vector descvec =
1978 vector_slot(matched_element->strvec, i);
1979
1980 if (vector_active(descvec) == 1) {
1981 struct desc *desc = vector_slot(descvec, 0);
1982
1983 if (CMD_VARARG(desc->cmd))
1984 varflag = 1;
1985
1986 if (varflag || CMD_VARIABLE(desc->cmd)
1987 || CMD_OPTION(desc->cmd))
1988 argv[argc++] = vector_slot(vline, i);
1989 } else
1990 argv[argc++] = vector_slot(vline, i);
1991 }
1992
1993 if (argc >= CMD_ARGC_MAX)
1994 return CMD_ERR_EXEED_ARGC_MAX;
1995 }
1996
1997 /* For vtysh execution. */
1998 if (cmd)
1999 *cmd = matched_element;
2000
2001 if (matched_element->daemon)
2002 return CMD_SUCCESS_DAEMON;
2003
2004 /* Execute matched command. */
2005 return (*matched_element->func) (matched_element, vty, argc, argv);
2006}
2007
2008int
2009cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2010 int vtysh)
2011{
2012 int ret, saved_ret, tried = 0;
2013 enum node_type onode;
2014 void *oindex;
2015
2016 onode = vty->node;
2017 oindex = vty->index;
2018
2019 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2020 vector shifted_vline;
2021 unsigned int index;
2022
2023 vty->node = ENABLE_NODE;
2024 /* We can try it on enable node, cos' the vty is authenticated */
2025
2026 shifted_vline = vector_init(vector_count(vline));
2027 /* use memcpy? */
2028 for (index = 1; index < vector_active(vline); index++) {
2029 vector_set_index(shifted_vline, index - 1,
2030 vector_lookup(vline, index));
2031 }
2032
2033 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2034
2035 vector_free(shifted_vline);
2036 vty->node = onode;
2037 return ret;
2038 }
2039
2040 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2041
2042 if (vtysh)
2043 return saved_ret;
2044
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002045 /* Go to parent for config nodes to attempt to find the right command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002046 while (ret != CMD_SUCCESS && ret != CMD_WARNING
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002047 && is_config(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002048 vty_go_parent(vty);
2049 ret = cmd_execute_command_real(vline, vty, cmd);
2050 tried = 1;
2051 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2052 /* succesfull command, leave the node as is */
2053 return ret;
2054 }
2055 }
2056 /* no command succeeded, reset the vty to the original node and
2057 return the error for this node */
2058 if (tried) {
2059 vty->node = onode;
2060 vty->index = oindex;
2061 }
2062 return saved_ret;
2063}
2064
2065/* Execute command by argument readline. */
2066int
2067cmd_execute_command_strict(vector vline, struct vty *vty,
2068 struct cmd_element **cmd)
2069{
2070 unsigned int i;
2071 unsigned int index;
2072 vector cmd_vector;
2073 struct cmd_element *cmd_element;
2074 struct cmd_element *matched_element;
2075 unsigned int matched_count, incomplete_count;
2076 int argc;
2077 const char *argv[CMD_ARGC_MAX];
2078 int varflag;
2079 enum match_type match = 0;
2080 char *command;
2081
2082 /* Make copy of command element */
2083 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2084
2085 for (index = 0; index < vector_active(vline); index++)
2086 if ((command = vector_slot(vline, index))) {
2087 int ret;
2088
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002089 match = cmd_filter(vector_slot(vline, index),
2090 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002091
2092 /* If command meets '.VARARG' then finish matching. */
2093 if (match == vararg_match)
2094 break;
2095
2096 ret =
2097 is_cmd_ambiguous(command, cmd_vector, index, match);
2098 if (ret == 1) {
2099 vector_free(cmd_vector);
2100 return CMD_ERR_AMBIGUOUS;
2101 }
2102 if (ret == 2) {
2103 vector_free(cmd_vector);
2104 return CMD_ERR_NO_MATCH;
2105 }
2106 }
2107
2108 /* Check matched count. */
2109 matched_element = NULL;
2110 matched_count = 0;
2111 incomplete_count = 0;
2112 for (i = 0; i < vector_active(cmd_vector); i++)
2113 if (vector_slot(cmd_vector, i) != NULL) {
2114 cmd_element = vector_slot(cmd_vector, i);
2115
2116 if (match == vararg_match
2117 || index >= cmd_element->cmdsize) {
2118 matched_element = cmd_element;
2119 matched_count++;
2120 } else
2121 incomplete_count++;
2122 }
2123
2124 /* Finish of using cmd_vector. */
2125 vector_free(cmd_vector);
2126
2127 /* To execute command, matched_count must be 1. */
2128 if (matched_count == 0) {
2129 if (incomplete_count)
2130 return CMD_ERR_INCOMPLETE;
2131 else
2132 return CMD_ERR_NO_MATCH;
2133 }
2134
2135 if (matched_count > 1)
2136 return CMD_ERR_AMBIGUOUS;
2137
2138 /* Argument treatment */
2139 varflag = 0;
2140 argc = 0;
2141
2142 for (i = 0; i < vector_active(vline); i++) {
2143 if (varflag)
2144 argv[argc++] = vector_slot(vline, i);
2145 else {
2146 vector descvec =
2147 vector_slot(matched_element->strvec, i);
2148
2149 if (vector_active(descvec) == 1) {
2150 struct desc *desc = vector_slot(descvec, 0);
2151
2152 if (CMD_VARARG(desc->cmd))
2153 varflag = 1;
2154
2155 if (varflag || CMD_VARIABLE(desc->cmd)
2156 || CMD_OPTION(desc->cmd))
2157 argv[argc++] = vector_slot(vline, i);
2158 } else
2159 argv[argc++] = vector_slot(vline, i);
2160 }
2161
2162 if (argc >= CMD_ARGC_MAX)
2163 return CMD_ERR_EXEED_ARGC_MAX;
2164 }
2165
2166 /* For vtysh execution. */
2167 if (cmd)
2168 *cmd = matched_element;
2169
2170 if (matched_element->daemon)
2171 return CMD_SUCCESS_DAEMON;
2172
2173 /* Now execute matched command */
2174 return (*matched_element->func) (matched_element, vty, argc, argv);
2175}
2176
2177/* Configration make from file. */
2178int config_from_file(struct vty *vty, FILE * fp)
2179{
2180 int ret;
2181 vector vline;
2182
2183 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2184 vline = cmd_make_strvec(vty->buf);
2185
2186 /* In case of comment line */
2187 if (vline == NULL)
2188 continue;
2189 /* Execute configuration command : this is strict match */
2190 ret = cmd_execute_command_strict(vline, vty, NULL);
2191
2192 /* Try again with setting node to CONFIG_NODE */
2193 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2194 && ret != CMD_ERR_NOTHING_TODO
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002195 && vty->node != CONFIG_NODE && is_config(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002196 vty_go_parent(vty);
2197 ret = cmd_execute_command_strict(vline, vty, NULL);
2198 }
2199
2200 cmd_free_strvec(vline);
2201
2202 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2203 && ret != CMD_ERR_NOTHING_TODO)
2204 return ret;
2205 }
2206 return CMD_SUCCESS;
2207}
2208
2209/* Configration from terminal */
2210DEFUN(config_terminal,
2211 config_terminal_cmd,
2212 "configure terminal",
2213 "Configuration from vty interface\n" "Configuration terminal\n")
2214{
2215 if (vty_config_lock(vty))
2216 vty->node = CONFIG_NODE;
2217 else {
2218 vty_out(vty, "VTY configuration is locked by other VTY%s",
2219 VTY_NEWLINE);
2220 return CMD_WARNING;
2221 }
2222 return CMD_SUCCESS;
2223}
2224
2225/* Enable command */
2226DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2227{
2228 /* If enable password is NULL, change to ENABLE_NODE */
2229 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2230 vty->type == VTY_SHELL_SERV)
2231 vty->node = ENABLE_NODE;
2232 else
2233 vty->node = AUTH_ENABLE_NODE;
2234
2235 return CMD_SUCCESS;
2236}
2237
2238/* Disable command */
2239DEFUN(disable,
2240 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2241{
2242 if (vty->node == ENABLE_NODE)
2243 vty->node = VIEW_NODE;
2244 return CMD_SUCCESS;
2245}
2246
2247/* Down vty node level. */
2248gDEFUN(config_exit,
2249 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2250{
2251 switch (vty->node) {
2252 case VIEW_NODE:
2253 case ENABLE_NODE:
2254 if (0) //vty_shell (vty))
2255 exit(0);
2256 else
2257 vty->status = VTY_CLOSE;
2258 break;
2259 case CONFIG_NODE:
2260 vty->node = ENABLE_NODE;
2261 vty_config_unlock(vty);
2262 break;
2263 case VTY_NODE:
2264 vty->node = CONFIG_NODE;
2265 break;
Harald Welte28222962011-02-18 20:37:04 +01002266 case CFG_LOG_NODE:
2267 vty->node = CONFIG_NODE;
2268 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002269 default:
2270 break;
2271 }
2272 return CMD_SUCCESS;
2273}
2274
2275/* End of configuration. */
2276 gDEFUN(config_end,
2277 config_end_cmd, "end", "End current mode and change to enable mode.")
2278{
2279 switch (vty->node) {
2280 case VIEW_NODE:
2281 case ENABLE_NODE:
2282 /* Nothing to do. */
2283 break;
Harald Welte28222962011-02-18 20:37:04 +01002284 case CFG_LOG_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002285 case CONFIG_NODE:
2286 case VTY_NODE:
2287 vty_config_unlock(vty);
2288 vty->node = ENABLE_NODE;
2289 break;
2290 default:
2291 break;
2292 }
2293 return CMD_SUCCESS;
2294}
2295
2296/* Show version. */
2297DEFUN(show_version,
2298 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2299{
Harald Welte237f6242010-05-25 23:00:45 +02002300 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2301 host.app_info->version,
2302 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2303 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002304
2305 return CMD_SUCCESS;
2306}
2307
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002308DEFUN(show_online_help,
2309 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2310{
2311 vty_dump_nodes(vty);
2312 return CMD_SUCCESS;
2313}
2314
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002315/* Help display function for all node. */
2316gDEFUN(config_help,
2317 config_help_cmd, "help", "Description of the interactive help system\n")
2318{
2319 vty_out(vty,
2320 "This VTY provides advanced help features. When you need help,%s\
2321anytime at the command line please press '?'.%s\
2322%s\
2323If nothing matches, the help list will be empty and you must backup%s\
2324 until entering a '?' shows the available options.%s\
2325Two styles of help are provided:%s\
23261. Full help is available when you are ready to enter a%s\
2327command argument (e.g. 'show ?') and describes each possible%s\
2328argument.%s\
23292. Partial help is provided when an abbreviated argument is entered%s\
2330 and you want to know what arguments match the input%s\
2331 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2332 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2333 return CMD_SUCCESS;
2334}
2335
2336/* Help display function for all node. */
2337gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2338{
2339 unsigned int i;
2340 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2341 struct cmd_element *cmd;
2342
2343 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2344 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2345 && !(cmd->attr == CMD_ATTR_DEPRECATED
2346 || cmd->attr == CMD_ATTR_HIDDEN))
2347 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2348 return CMD_SUCCESS;
2349}
2350
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002351static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002352{
2353 unsigned int i;
2354 int fd;
2355 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002356 char *config_file_tmp = NULL;
2357 char *config_file_sav = NULL;
2358 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002359 struct stat st;
2360
2361 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002362
2363 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002364 config_file_sav =
2365 _talloc_zero(tall_vty_cmd_ctx,
2366 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2367 "config_file_sav");
2368 strcpy(config_file_sav, config_file);
2369 strcat(config_file_sav, CONF_BACKUP_EXT);
2370
2371 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2372 "config_file_tmp");
2373 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2374
2375 /* Open file to configuration write. */
2376 fd = mkstemp(config_file_tmp);
2377 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002378 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002379 talloc_free(config_file_tmp);
2380 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002381 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002382 }
2383
2384 /* Make vty for configuration file. */
2385 file_vty = vty_new();
2386 file_vty->fd = fd;
2387 file_vty->type = VTY_FILE;
2388
2389 /* Config file header print. */
2390 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002391 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002392 //vty_time_print (file_vty, 1);
2393 vty_out(file_vty, "!\n");
2394
2395 for (i = 0; i < vector_active(cmdvec); i++)
2396 if ((node = vector_slot(cmdvec, i)) && node->func) {
2397 if ((*node->func) (file_vty))
2398 vty_out(file_vty, "!\n");
2399 }
2400 vty_close(file_vty);
2401
2402 if (unlink(config_file_sav) != 0)
2403 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002404 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002405 talloc_free(config_file_sav);
2406 talloc_free(config_file_tmp);
2407 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002408 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002409 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002410
2411 /* Only link the .sav file if the original file exists */
2412 if (stat(config_file, &st) == 0) {
2413 if (link(config_file, config_file_sav) != 0) {
2414 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2415 talloc_free(config_file_sav);
2416 talloc_free(config_file_tmp);
2417 unlink(config_file_tmp);
2418 return -3;
2419 }
2420 sync();
2421 if (unlink(config_file) != 0) {
2422 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2423 talloc_free(config_file_sav);
2424 talloc_free(config_file_tmp);
2425 unlink(config_file_tmp);
2426 return -4;
2427 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002428 }
2429 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002430 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002431 talloc_free(config_file_sav);
2432 talloc_free(config_file_tmp);
2433 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002434 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002435 }
2436 unlink(config_file_tmp);
2437 sync();
2438
2439 talloc_free(config_file_sav);
2440 talloc_free(config_file_tmp);
2441
2442 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002443 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2444 return -6;
2445 }
2446
2447 return 0;
2448}
2449
2450
2451/* Write current configuration into file. */
2452DEFUN(config_write_file,
2453 config_write_file_cmd,
2454 "write file",
2455 "Write running configuration to memory, network, or terminal\n"
2456 "Write to configuration file\n")
2457{
2458 char *failed_file;
2459 int rc;
2460
2461 if (host.config == NULL) {
2462 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2463 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002464 return CMD_WARNING;
2465 }
2466
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002467 rc = write_config_file(host.config, &failed_file);
2468 switch (rc) {
2469 case -1:
2470 vty_out(vty, "Can't open configuration file %s.%s",
2471 failed_file, VTY_NEWLINE);
2472 rc = CMD_WARNING;
2473 break;
2474 case -2:
2475 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2476 failed_file, VTY_NEWLINE);
2477 rc = CMD_WARNING;
2478 break;
2479 case -3:
2480 vty_out(vty, "Can't backup old configuration file %s.%s",
2481 failed_file, VTY_NEWLINE);
2482 rc = CMD_WARNING;
2483 break;
2484 case -4:
2485 vty_out(vty, "Can't unlink configuration file %s.%s",
2486 failed_file, VTY_NEWLINE);
2487 rc = CMD_WARNING;
2488 break;
2489 case -5:
2490 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2491 VTY_NEWLINE);
2492 rc = CMD_WARNING;
2493 break;
2494 case -6:
2495 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2496 failed_file, strerror(errno), errno, VTY_NEWLINE);
2497 rc = CMD_WARNING;
2498 break;
2499 default:
2500 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2501 rc = CMD_SUCCESS;
2502 break;
2503 }
2504
2505 talloc_free(failed_file);
2506 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002507}
2508
2509ALIAS(config_write_file,
2510 config_write_cmd,
2511 "write", "Write running configuration to memory, network, or terminal\n")
2512
2513 ALIAS(config_write_file,
2514 config_write_memory_cmd,
2515 "write memory",
2516 "Write running configuration to memory, network, or terminal\n"
2517 "Write configuration to the file (same as write file)\n")
2518
2519 ALIAS(config_write_file,
2520 copy_runningconfig_startupconfig_cmd,
2521 "copy running-config startup-config",
2522 "Copy configuration\n"
2523 "Copy running config to... \n"
2524 "Copy running config to startup config (same as write file)\n")
2525
2526/* Write current configuration into the terminal. */
2527 DEFUN(config_write_terminal,
2528 config_write_terminal_cmd,
2529 "write terminal",
2530 "Write running configuration to memory, network, or terminal\n"
2531 "Write to terminal\n")
2532{
2533 unsigned int i;
2534 struct cmd_node *node;
2535
2536 if (vty->type == VTY_SHELL_SERV) {
2537 for (i = 0; i < vector_active(cmdvec); i++)
2538 if ((node = vector_slot(cmdvec, i)) && node->func
2539 && node->vtysh) {
2540 if ((*node->func) (vty))
2541 vty_out(vty, "!%s", VTY_NEWLINE);
2542 }
2543 } else {
2544 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2545 VTY_NEWLINE);
2546 vty_out(vty, "!%s", VTY_NEWLINE);
2547
2548 for (i = 0; i < vector_active(cmdvec); i++)
2549 if ((node = vector_slot(cmdvec, i)) && node->func) {
2550 if ((*node->func) (vty))
2551 vty_out(vty, "!%s", VTY_NEWLINE);
2552 }
2553 vty_out(vty, "end%s", VTY_NEWLINE);
2554 }
2555 return CMD_SUCCESS;
2556}
2557
2558/* Write current configuration into the terminal. */
2559ALIAS(config_write_terminal,
2560 show_running_config_cmd,
2561 "show running-config", SHOW_STR "running configuration\n")
2562
2563/* Write startup configuration into the terminal. */
2564 DEFUN(show_startup_config,
2565 show_startup_config_cmd,
2566 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2567{
2568 char buf[BUFSIZ];
2569 FILE *confp;
2570
2571 confp = fopen(host.config, "r");
2572 if (confp == NULL) {
2573 vty_out(vty, "Can't open configuration file [%s]%s",
2574 host.config, VTY_NEWLINE);
2575 return CMD_WARNING;
2576 }
2577
2578 while (fgets(buf, BUFSIZ, confp)) {
2579 char *cp = buf;
2580
2581 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2582 cp++;
2583 *cp = '\0';
2584
2585 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2586 }
2587
2588 fclose(confp);
2589
2590 return CMD_SUCCESS;
2591}
2592
2593/* Hostname configuration */
2594DEFUN(config_hostname,
2595 hostname_cmd,
2596 "hostname WORD",
2597 "Set system's network name\n" "This system's network name\n")
2598{
2599 if (!isalpha((int)*argv[0])) {
2600 vty_out(vty, "Please specify string starting with alphabet%s",
2601 VTY_NEWLINE);
2602 return CMD_WARNING;
2603 }
2604
2605 if (host.name)
2606 talloc_free(host.name);
2607
2608 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2609 return CMD_SUCCESS;
2610}
2611
2612DEFUN(config_no_hostname,
2613 no_hostname_cmd,
2614 "no hostname [HOSTNAME]",
2615 NO_STR "Reset system's network name\n" "Host name of this router\n")
2616{
2617 if (host.name)
2618 talloc_free(host.name);
2619 host.name = NULL;
2620 return CMD_SUCCESS;
2621}
2622
2623/* VTY interface password set. */
2624DEFUN(config_password, password_cmd,
2625 "password (8|) WORD",
2626 "Assign the terminal connection password\n"
2627 "Specifies a HIDDEN password will follow\n"
2628 "dummy string \n" "The HIDDEN line password string\n")
2629{
2630 /* Argument check. */
2631 if (argc == 0) {
2632 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2633 return CMD_WARNING;
2634 }
2635
2636 if (argc == 2) {
2637 if (*argv[0] == '8') {
2638 if (host.password)
2639 talloc_free(host.password);
2640 host.password = NULL;
2641 if (host.password_encrypt)
2642 talloc_free(host.password_encrypt);
2643 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2644 return CMD_SUCCESS;
2645 } else {
2646 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2647 return CMD_WARNING;
2648 }
2649 }
2650
2651 if (!isalnum((int)*argv[0])) {
2652 vty_out(vty,
2653 "Please specify string starting with alphanumeric%s",
2654 VTY_NEWLINE);
2655 return CMD_WARNING;
2656 }
2657
2658 if (host.password)
2659 talloc_free(host.password);
2660 host.password = NULL;
2661
2662#ifdef VTY_CRYPT_PW
2663 if (host.encrypt) {
2664 if (host.password_encrypt)
2665 talloc_free(host.password_encrypt);
2666 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2667 } else
2668#endif
2669 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2670
2671 return CMD_SUCCESS;
2672}
2673
2674ALIAS(config_password, password_text_cmd,
2675 "password LINE",
2676 "Assign the terminal connection password\n"
2677 "The UNENCRYPTED (cleartext) line password\n")
2678
2679/* VTY enable password set. */
2680 DEFUN(config_enable_password, enable_password_cmd,
2681 "enable password (8|) WORD",
2682 "Modify enable password parameters\n"
2683 "Assign the privileged level password\n"
2684 "Specifies a HIDDEN password will follow\n"
2685 "dummy string \n" "The HIDDEN 'enable' password string\n")
2686{
2687 /* Argument check. */
2688 if (argc == 0) {
2689 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2690 return CMD_WARNING;
2691 }
2692
2693 /* Crypt type is specified. */
2694 if (argc == 2) {
2695 if (*argv[0] == '8') {
2696 if (host.enable)
2697 talloc_free(host.enable);
2698 host.enable = NULL;
2699
2700 if (host.enable_encrypt)
2701 talloc_free(host.enable_encrypt);
2702 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2703
2704 return CMD_SUCCESS;
2705 } else {
2706 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2707 return CMD_WARNING;
2708 }
2709 }
2710
2711 if (!isalnum((int)*argv[0])) {
2712 vty_out(vty,
2713 "Please specify string starting with alphanumeric%s",
2714 VTY_NEWLINE);
2715 return CMD_WARNING;
2716 }
2717
2718 if (host.enable)
2719 talloc_free(host.enable);
2720 host.enable = NULL;
2721
2722 /* Plain password input. */
2723#ifdef VTY_CRYPT_PW
2724 if (host.encrypt) {
2725 if (host.enable_encrypt)
2726 talloc_free(host.enable_encrypt);
2727 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2728 } else
2729#endif
2730 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2731
2732 return CMD_SUCCESS;
2733}
2734
2735ALIAS(config_enable_password,
2736 enable_password_text_cmd,
2737 "enable password LINE",
2738 "Modify enable password parameters\n"
2739 "Assign the privileged level password\n"
2740 "The UNENCRYPTED (cleartext) 'enable' password\n")
2741
2742/* VTY enable password delete. */
2743 DEFUN(no_config_enable_password, no_enable_password_cmd,
2744 "no enable password",
2745 NO_STR
2746 "Modify enable password parameters\n"
2747 "Assign the privileged level password\n")
2748{
2749 if (host.enable)
2750 talloc_free(host.enable);
2751 host.enable = NULL;
2752
2753 if (host.enable_encrypt)
2754 talloc_free(host.enable_encrypt);
2755 host.enable_encrypt = NULL;
2756
2757 return CMD_SUCCESS;
2758}
2759
2760#ifdef VTY_CRYPT_PW
2761DEFUN(service_password_encrypt,
2762 service_password_encrypt_cmd,
2763 "service password-encryption",
2764 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2765{
2766 if (host.encrypt)
2767 return CMD_SUCCESS;
2768
2769 host.encrypt = 1;
2770
2771 if (host.password) {
2772 if (host.password_encrypt)
2773 talloc_free(host.password_encrypt);
2774 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2775 }
2776 if (host.enable) {
2777 if (host.enable_encrypt)
2778 talloc_free(host.enable_encrypt);
2779 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2780 }
2781
2782 return CMD_SUCCESS;
2783}
2784
2785DEFUN(no_service_password_encrypt,
2786 no_service_password_encrypt_cmd,
2787 "no service password-encryption",
2788 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2789{
2790 if (!host.encrypt)
2791 return CMD_SUCCESS;
2792
2793 host.encrypt = 0;
2794
2795 if (host.password_encrypt)
2796 talloc_free(host.password_encrypt);
2797 host.password_encrypt = NULL;
2798
2799 if (host.enable_encrypt)
2800 talloc_free(host.enable_encrypt);
2801 host.enable_encrypt = NULL;
2802
2803 return CMD_SUCCESS;
2804}
2805#endif
2806
2807DEFUN(config_terminal_length, config_terminal_length_cmd,
2808 "terminal length <0-512>",
2809 "Set terminal line parameters\n"
2810 "Set number of lines on a screen\n"
2811 "Number of lines on screen (0 for no pausing)\n")
2812{
2813 int lines;
2814 char *endptr = NULL;
2815
2816 lines = strtol(argv[0], &endptr, 10);
2817 if (lines < 0 || lines > 512 || *endptr != '\0') {
2818 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2819 return CMD_WARNING;
2820 }
2821 vty->lines = lines;
2822
2823 return CMD_SUCCESS;
2824}
2825
2826DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2827 "terminal no length",
2828 "Set terminal line parameters\n"
2829 NO_STR "Set number of lines on a screen\n")
2830{
2831 vty->lines = -1;
2832 return CMD_SUCCESS;
2833}
2834
2835DEFUN(service_terminal_length, service_terminal_length_cmd,
2836 "service terminal-length <0-512>",
2837 "Set up miscellaneous service\n"
2838 "System wide terminal length configuration\n"
2839 "Number of lines of VTY (0 means no line control)\n")
2840{
2841 int lines;
2842 char *endptr = NULL;
2843
2844 lines = strtol(argv[0], &endptr, 10);
2845 if (lines < 0 || lines > 512 || *endptr != '\0') {
2846 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2847 return CMD_WARNING;
2848 }
2849 host.lines = lines;
2850
2851 return CMD_SUCCESS;
2852}
2853
2854DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2855 "no service terminal-length [<0-512>]",
2856 NO_STR
2857 "Set up miscellaneous service\n"
2858 "System wide terminal length configuration\n"
2859 "Number of lines of VTY (0 means no line control)\n")
2860{
2861 host.lines = -1;
2862 return CMD_SUCCESS;
2863}
2864
2865DEFUN_HIDDEN(do_echo,
2866 echo_cmd,
2867 "echo .MESSAGE",
2868 "Echo a message back to the vty\n" "The message to echo\n")
2869{
2870 char *message;
2871
2872 vty_out(vty, "%s%s",
2873 ((message =
2874 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2875 if (message)
2876 talloc_free(message);
2877 return CMD_SUCCESS;
2878}
2879
2880#if 0
2881DEFUN(config_logmsg,
2882 config_logmsg_cmd,
2883 "logmsg " LOG_LEVELS " .MESSAGE",
2884 "Send a message to enabled logging destinations\n"
2885 LOG_LEVEL_DESC "The message to send\n")
2886{
2887 int level;
2888 char *message;
2889
2890 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2891 return CMD_ERR_NO_MATCH;
2892
2893 zlog(NULL, level,
2894 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2895 if (message)
2896 talloc_free(message);
2897 return CMD_SUCCESS;
2898}
2899
2900DEFUN(show_logging,
2901 show_logging_cmd,
2902 "show logging", SHOW_STR "Show current logging configuration\n")
2903{
2904 struct zlog *zl = zlog_default;
2905
2906 vty_out(vty, "Syslog logging: ");
2907 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2908 vty_out(vty, "disabled");
2909 else
2910 vty_out(vty, "level %s, facility %s, ident %s",
2911 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2912 facility_name(zl->facility), zl->ident);
2913 vty_out(vty, "%s", VTY_NEWLINE);
2914
2915 vty_out(vty, "Stdout logging: ");
2916 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2917 vty_out(vty, "disabled");
2918 else
2919 vty_out(vty, "level %s",
2920 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2921 vty_out(vty, "%s", VTY_NEWLINE);
2922
2923 vty_out(vty, "Monitor logging: ");
2924 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2925 vty_out(vty, "disabled");
2926 else
2927 vty_out(vty, "level %s",
2928 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2929 vty_out(vty, "%s", VTY_NEWLINE);
2930
2931 vty_out(vty, "File logging: ");
2932 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2933 vty_out(vty, "disabled");
2934 else
2935 vty_out(vty, "level %s, filename %s",
2936 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2937 zl->filename);
2938 vty_out(vty, "%s", VTY_NEWLINE);
2939
2940 vty_out(vty, "Protocol name: %s%s",
2941 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2942 vty_out(vty, "Record priority: %s%s",
2943 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2944
2945 return CMD_SUCCESS;
2946}
2947
2948DEFUN(config_log_stdout,
2949 config_log_stdout_cmd,
2950 "log stdout", "Logging control\n" "Set stdout logging level\n")
2951{
2952 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2953 return CMD_SUCCESS;
2954}
2955
2956DEFUN(config_log_stdout_level,
2957 config_log_stdout_level_cmd,
2958 "log stdout " LOG_LEVELS,
2959 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2960{
2961 int level;
2962
2963 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2964 return CMD_ERR_NO_MATCH;
2965 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
2966 return CMD_SUCCESS;
2967}
2968
2969DEFUN(no_config_log_stdout,
2970 no_config_log_stdout_cmd,
2971 "no log stdout [LEVEL]",
2972 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
2973{
2974 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2975 return CMD_SUCCESS;
2976}
2977
2978DEFUN(config_log_monitor,
2979 config_log_monitor_cmd,
2980 "log monitor",
2981 "Logging control\n" "Set terminal line (monitor) logging level\n")
2982{
2983 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2984 return CMD_SUCCESS;
2985}
2986
2987DEFUN(config_log_monitor_level,
2988 config_log_monitor_level_cmd,
2989 "log monitor " LOG_LEVELS,
2990 "Logging control\n"
2991 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2992{
2993 int level;
2994
2995 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2996 return CMD_ERR_NO_MATCH;
2997 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
2998 return CMD_SUCCESS;
2999}
3000
3001DEFUN(no_config_log_monitor,
3002 no_config_log_monitor_cmd,
3003 "no log monitor [LEVEL]",
3004 NO_STR
3005 "Logging control\n"
3006 "Disable terminal line (monitor) logging\n" "Logging level\n")
3007{
3008 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3009 return CMD_SUCCESS;
3010}
3011
3012static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3013{
3014 int ret;
3015 char *p = NULL;
3016 const char *fullpath;
3017
3018 /* Path detection. */
3019 if (!IS_DIRECTORY_SEP(*fname)) {
3020 char cwd[MAXPATHLEN + 1];
3021 cwd[MAXPATHLEN] = '\0';
3022
3023 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3024 zlog_err("config_log_file: Unable to alloc mem!");
3025 return CMD_WARNING;
3026 }
3027
3028 if ((p = _talloc_zero(tall_vcmd_ctx,
3029 strlen(cwd) + strlen(fname) + 2),
3030 "set_log_file")
3031 == NULL) {
3032 zlog_err("config_log_file: Unable to alloc mem!");
3033 return CMD_WARNING;
3034 }
3035 sprintf(p, "%s/%s", cwd, fname);
3036 fullpath = p;
3037 } else
3038 fullpath = fname;
3039
3040 ret = zlog_set_file(NULL, fullpath, loglevel);
3041
3042 if (p)
3043 talloc_free(p);
3044
3045 if (!ret) {
3046 vty_out(vty, "can't open logfile %s\n", fname);
3047 return CMD_WARNING;
3048 }
3049
3050 if (host.logfile)
3051 talloc_free(host.logfile);
3052
3053 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3054
3055 return CMD_SUCCESS;
3056}
3057
3058DEFUN(config_log_file,
3059 config_log_file_cmd,
3060 "log file FILENAME",
3061 "Logging control\n" "Logging to file\n" "Logging filename\n")
3062{
3063 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3064}
3065
3066DEFUN(config_log_file_level,
3067 config_log_file_level_cmd,
3068 "log file FILENAME " LOG_LEVELS,
3069 "Logging control\n"
3070 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3071{
3072 int level;
3073
3074 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3075 return CMD_ERR_NO_MATCH;
3076 return set_log_file(vty, argv[0], level);
3077}
3078
3079DEFUN(no_config_log_file,
3080 no_config_log_file_cmd,
3081 "no log file [FILENAME]",
3082 NO_STR
3083 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3084{
3085 zlog_reset_file(NULL);
3086
3087 if (host.logfile)
3088 talloc_free(host.logfile);
3089
3090 host.logfile = NULL;
3091
3092 return CMD_SUCCESS;
3093}
3094
3095ALIAS(no_config_log_file,
3096 no_config_log_file_level_cmd,
3097 "no log file FILENAME LEVEL",
3098 NO_STR
3099 "Logging control\n"
3100 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3101
3102 DEFUN(config_log_syslog,
3103 config_log_syslog_cmd,
3104 "log syslog", "Logging control\n" "Set syslog logging level\n")
3105{
3106 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3107 return CMD_SUCCESS;
3108}
3109
3110DEFUN(config_log_syslog_level,
3111 config_log_syslog_level_cmd,
3112 "log syslog " LOG_LEVELS,
3113 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3114{
3115 int level;
3116
3117 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3118 return CMD_ERR_NO_MATCH;
3119 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3120 return CMD_SUCCESS;
3121}
3122
3123DEFUN_DEPRECATED(config_log_syslog_facility,
3124 config_log_syslog_facility_cmd,
3125 "log syslog facility " LOG_FACILITIES,
3126 "Logging control\n"
3127 "Logging goes to syslog\n"
3128 "(Deprecated) Facility parameter for syslog messages\n"
3129 LOG_FACILITY_DESC)
3130{
3131 int facility;
3132
3133 if ((facility = facility_match(argv[0])) < 0)
3134 return CMD_ERR_NO_MATCH;
3135
3136 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3137 zlog_default->facility = facility;
3138 return CMD_SUCCESS;
3139}
3140
3141DEFUN(no_config_log_syslog,
3142 no_config_log_syslog_cmd,
3143 "no log syslog [LEVEL]",
3144 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3145{
3146 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3147 return CMD_SUCCESS;
3148}
3149
3150ALIAS(no_config_log_syslog,
3151 no_config_log_syslog_facility_cmd,
3152 "no log syslog facility " LOG_FACILITIES,
3153 NO_STR
3154 "Logging control\n"
3155 "Logging goes to syslog\n"
3156 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3157
3158 DEFUN(config_log_facility,
3159 config_log_facility_cmd,
3160 "log facility " LOG_FACILITIES,
3161 "Logging control\n"
3162 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3163{
3164 int facility;
3165
3166 if ((facility = facility_match(argv[0])) < 0)
3167 return CMD_ERR_NO_MATCH;
3168 zlog_default->facility = facility;
3169 return CMD_SUCCESS;
3170}
3171
3172DEFUN(no_config_log_facility,
3173 no_config_log_facility_cmd,
3174 "no log facility [FACILITY]",
3175 NO_STR
3176 "Logging control\n"
3177 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3178{
3179 zlog_default->facility = LOG_DAEMON;
3180 return CMD_SUCCESS;
3181}
3182
3183DEFUN_DEPRECATED(config_log_trap,
3184 config_log_trap_cmd,
3185 "log trap " LOG_LEVELS,
3186 "Logging control\n"
3187 "(Deprecated) Set logging level and default for all destinations\n"
3188 LOG_LEVEL_DESC)
3189{
3190 int new_level;
3191 int i;
3192
3193 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3194 return CMD_ERR_NO_MATCH;
3195
3196 zlog_default->default_lvl = new_level;
3197 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3198 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3199 zlog_default->maxlvl[i] = new_level;
3200 return CMD_SUCCESS;
3201}
3202
3203DEFUN_DEPRECATED(no_config_log_trap,
3204 no_config_log_trap_cmd,
3205 "no log trap [LEVEL]",
3206 NO_STR
3207 "Logging control\n"
3208 "Permit all logging information\n" "Logging level\n")
3209{
3210 zlog_default->default_lvl = LOG_DEBUG;
3211 return CMD_SUCCESS;
3212}
3213
3214DEFUN(config_log_record_priority,
3215 config_log_record_priority_cmd,
3216 "log record-priority",
3217 "Logging control\n"
3218 "Log the priority of the message within the message\n")
3219{
3220 zlog_default->record_priority = 1;
3221 return CMD_SUCCESS;
3222}
3223
3224DEFUN(no_config_log_record_priority,
3225 no_config_log_record_priority_cmd,
3226 "no log record-priority",
3227 NO_STR
3228 "Logging control\n"
3229 "Do not log the priority of the message within the message\n")
3230{
3231 zlog_default->record_priority = 0;
3232 return CMD_SUCCESS;
3233}
3234#endif
3235
3236DEFUN(banner_motd_file,
3237 banner_motd_file_cmd,
3238 "banner motd file [FILE]",
3239 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3240{
3241 if (host.motdfile)
3242 talloc_free(host.motdfile);
3243 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3244
3245 return CMD_SUCCESS;
3246}
3247
3248DEFUN(banner_motd_default,
3249 banner_motd_default_cmd,
3250 "banner motd default",
3251 "Set banner string\n" "Strings for motd\n" "Default string\n")
3252{
3253 host.motd = default_motd;
3254 return CMD_SUCCESS;
3255}
3256
3257DEFUN(no_banner_motd,
3258 no_banner_motd_cmd,
3259 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3260{
3261 host.motd = NULL;
3262 if (host.motdfile)
3263 talloc_free(host.motdfile);
3264 host.motdfile = NULL;
3265 return CMD_SUCCESS;
3266}
3267
3268/* Set config filename. Called from vty.c */
3269void host_config_set(const char *filename)
3270{
3271 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3272}
3273
3274void install_default(enum node_type node)
3275{
3276 install_element(node, &config_help_cmd);
3277 install_element(node, &config_list_cmd);
3278
3279 install_element(node, &config_write_terminal_cmd);
3280 install_element(node, &config_write_file_cmd);
3281 install_element(node, &config_write_memory_cmd);
3282 install_element(node, &config_write_cmd);
3283 install_element(node, &show_running_config_cmd);
3284}
3285
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003286/**
3287 * \brief Write the current running config to a given file
3288 * \param[in] vty the vty of the code
3289 * \param[in] filename where to store the file
3290 * \return 0 in case of success.
3291 *
3292 * If the filename already exists create a filename.sav
3293 * version with the current code.
3294 *
3295 */
3296int osmo_vty_write_config_file(const char *filename)
3297{
3298 char *failed_file;
3299 int rc;
3300
3301 rc = write_config_file(filename, &failed_file);
3302 talloc_free(failed_file);
3303 return rc;
3304}
3305
3306/**
3307 * \brief Save the current state to the config file
3308 * \return 0 in case of success.
3309 *
3310 * If the filename already exists create a filename.sav
3311 * version with the current code.
3312 *
3313 */
3314int osmo_vty_save_config_file(void)
3315{
3316 char *failed_file;
3317 int rc;
3318
3319 if (host.config == NULL)
3320 return -7;
3321
3322 rc = write_config_file(host.config, &failed_file);
3323 talloc_free(failed_file);
3324 return rc;
3325}
3326
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003327/* Initialize command interface. Install basic nodes and commands. */
3328void cmd_init(int terminal)
3329{
3330 /* Allocate initial top vector of commands. */
3331 cmdvec = vector_init(VECTOR_MIN_SIZE);
3332
3333 /* Default host value settings. */
3334 host.name = NULL;
3335 host.password = NULL;
3336 host.enable = NULL;
3337 host.logfile = NULL;
3338 host.config = NULL;
3339 host.lines = -1;
3340 host.motd = default_motd;
3341 host.motdfile = NULL;
3342
3343 /* Install top nodes. */
3344 install_node(&view_node, NULL);
3345 install_node(&enable_node, NULL);
3346 install_node(&auth_node, NULL);
3347 install_node(&auth_enable_node, NULL);
3348 install_node(&config_node, config_write_host);
3349
3350 /* Each node's basic commands. */
3351 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003352 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003353 if (terminal) {
3354 install_element(VIEW_NODE, &config_list_cmd);
3355 install_element(VIEW_NODE, &config_exit_cmd);
3356 install_element(VIEW_NODE, &config_help_cmd);
3357 install_element(VIEW_NODE, &config_enable_cmd);
3358 install_element(VIEW_NODE, &config_terminal_length_cmd);
3359 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3360 install_element(VIEW_NODE, &echo_cmd);
3361 }
3362
3363 if (terminal) {
3364 install_element(ENABLE_NODE, &config_exit_cmd);
3365 install_default(ENABLE_NODE);
3366 install_element(ENABLE_NODE, &config_disable_cmd);
3367 install_element(ENABLE_NODE, &config_terminal_cmd);
3368 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3369 }
3370 install_element (ENABLE_NODE, &show_startup_config_cmd);
3371 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003372 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003373
3374 if (terminal) {
3375 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3376 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3377 install_element(ENABLE_NODE, &echo_cmd);
3378
3379 install_default(CONFIG_NODE);
3380 install_element(CONFIG_NODE, &config_exit_cmd);
3381 }
3382
3383 install_element(CONFIG_NODE, &hostname_cmd);
3384 install_element(CONFIG_NODE, &no_hostname_cmd);
3385
3386 if (terminal) {
3387 install_element(CONFIG_NODE, &password_cmd);
3388 install_element(CONFIG_NODE, &password_text_cmd);
3389 install_element(CONFIG_NODE, &enable_password_cmd);
3390 install_element(CONFIG_NODE, &enable_password_text_cmd);
3391 install_element(CONFIG_NODE, &no_enable_password_cmd);
3392
3393#ifdef VTY_CRYPT_PW
3394 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3395 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3396#endif
3397 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3398 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3399 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3400 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3401 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3402
3403 }
3404 srand(time(NULL));
3405}
Harald Welte7acb30c2011-08-17 17:13:48 +02003406
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003407/*! @} */