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