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