blob: 6173292a03753a4cb964f80d3d4195a4bcae7d1a [file] [log] [blame]
Harald Welte955049f2009-03-10 12:16:51 +00001/*
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 "cardshell.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <syslog.h>
30#include <errno.h>
31#define _XOPEN_SOURCE
32#include <unistd.h>
33#include <assert.h>
34#include <ctype.h>
35#include <time.h>
36#include <sys/time.h>
Harald Welte4a3023d2009-08-06 18:50:10 +020037#include <sys/stat.h>
Harald Welte955049f2009-03-10 12:16:51 +000038
39//#include "memory.h"
40//#include "log.h"
41//#include <lib/version.h>
42//#include "thread.h"
43#include <vty/vector.h>
44#include <vty/vty.h>
45#include <vty/command.h>
46//#include "workqueue.h"
47
Harald Welte5258fc42009-03-28 19:07:53 +000048#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther430b59c2009-07-29 06:42:36 +020049#include <openbsc/gsm_subscriber.h>
Harald Welte2477d932009-08-07 00:32:41 +020050#include <openbsc/talloc.h>
51
Harald Welte955049f2009-03-10 12:16:51 +000052/* Command vector which includes some level of command lists. Normally
53 each daemon maintains each own cmdvec. */
54vector cmdvec;
55
56/* Host information structure. */
57struct host host;
58
59/* Standard command node structures. */
60struct cmd_node auth_node = {
61 AUTH_NODE,
62 "Password: ",
63};
64
65struct cmd_node view_node = {
66 VIEW_NODE,
67 "%s> ",
68};
69
70struct cmd_node auth_enable_node = {
71 AUTH_ENABLE_NODE,
72 "Password: ",
73};
74
75struct cmd_node enable_node = {
76 ENABLE_NODE,
77 "%s# ",
78};
79
80struct cmd_node config_node = {
81 CONFIG_NODE,
82 "%s(config)# ",
83 1
84};
85
86/* Default motd string. */
87const char *default_motd = "\r\n\
88Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\
89" QUAGGA_COPYRIGHT "\r\n\
90\r\n";
91
92#if 0
93static struct facility_map {
94 int facility;
95 const char *name;
96 size_t match;
97} syslog_facilities[] = {
98 {
99 LOG_KERN, "kern", 1}, {
100 LOG_USER, "user", 2}, {
101 LOG_MAIL, "mail", 1}, {
102 LOG_DAEMON, "daemon", 1}, {
103 LOG_AUTH, "auth", 1}, {
104 LOG_SYSLOG, "syslog", 1}, {
105 LOG_LPR, "lpr", 2}, {
106 LOG_NEWS, "news", 1}, {
107 LOG_UUCP, "uucp", 2}, {
108 LOG_CRON, "cron", 1},
109#ifdef LOG_FTP
110 {
111 LOG_FTP, "ftp", 1},
112#endif
113 {
114 LOG_LOCAL0, "local0", 6}, {
115 LOG_LOCAL1, "local1", 6}, {
116 LOG_LOCAL2, "local2", 6}, {
117 LOG_LOCAL3, "local3", 6}, {
118 LOG_LOCAL4, "local4", 6}, {
119 LOG_LOCAL5, "local5", 6}, {
120 LOG_LOCAL6, "local6", 6}, {
121 LOG_LOCAL7, "local7", 6}, {
1220, NULL, 0},};
123
124static const char *facility_name(int facility)
125{
126 struct facility_map *fm;
127
128 for (fm = syslog_facilities; fm->name; fm++)
129 if (fm->facility == facility)
130 return fm->name;
131 return "";
132}
133
134static int facility_match(const char *str)
135{
136 struct facility_map *fm;
137
138 for (fm = syslog_facilities; fm->name; fm++)
139 if (!strncmp(str, fm->name, fm->match))
140 return fm->facility;
141 return -1;
142}
143
144static int level_match(const char *s)
145{
146 int level;
147
148 for (level = 0; zlog_priority[level] != NULL; level++)
149 if (!strncmp(s, zlog_priority[level], 2))
150 return level;
151 return ZLOG_DISABLED;
152}
153#endif
154
155/* This is called from main when a daemon is invoked with -v or --version. */
156void print_version(const char *progname)
157{
158 printf("%s version %s\n", progname, QUAGGA_VERSION);
159 printf("%s\n", QUAGGA_COPYRIGHT);
160}
161
162/* Utility function to concatenate argv argument into a single string
163 with inserting ' ' character between each argument. */
164char *argv_concat(const char **argv, int argc, int shift)
165{
166 int i;
167 size_t len;
168 char *str;
169 char *p;
170
171 len = 0;
172 for (i = shift; i < argc; i++)
173 len += strlen(argv[i]) + 1;
174 if (!len)
175 return NULL;
Harald Welte0224e4d2009-08-07 13:25:41 +0200176 p = str = _talloc_zero(tall_vty_ctx, len, "arvg_concat");
Harald Welte955049f2009-03-10 12:16:51 +0000177 for (i = shift; i < argc; i++) {
178 size_t arglen;
179 memcpy(p, argv[i], (arglen = strlen(argv[i])));
180 p += arglen;
181 *p++ = ' ';
182 }
183 *(p - 1) = '\0';
184 return str;
185}
186
187/* Install top node of command vector. */
188void install_node(struct cmd_node *node, int (*func) (struct vty *))
189{
190 vector_set_index(cmdvec, node->node, node);
191 node->func = func;
192 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
193}
194
195/* Compare two command's string. Used in sort_node (). */
196static int cmp_node(const void *p, const void *q)
197{
198 struct cmd_element *a = *(struct cmd_element **)p;
199 struct cmd_element *b = *(struct cmd_element **)q;
200
201 return strcmp(a->string, b->string);
202}
203
204static int cmp_desc(const void *p, const void *q)
205{
206 struct desc *a = *(struct desc **)p;
207 struct desc *b = *(struct desc **)q;
208
209 return strcmp(a->cmd, b->cmd);
210}
211
212/* Sort each node's command element according to command string. */
213void sort_node()
214{
215 unsigned int i, j;
216 struct cmd_node *cnode;
217 vector descvec;
218 struct cmd_element *cmd_element;
219
220 for (i = 0; i < vector_active(cmdvec); i++)
221 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
222 vector cmd_vector = cnode->cmd_vector;
223 qsort(cmd_vector->index, vector_active(cmd_vector),
224 sizeof(void *), cmp_node);
225
226 for (j = 0; j < vector_active(cmd_vector); j++)
227 if ((cmd_element =
228 vector_slot(cmd_vector, j)) != NULL
229 && vector_active(cmd_element->strvec)) {
230 descvec =
231 vector_slot(cmd_element->strvec,
232 vector_active
233 (cmd_element->strvec) -
234 1);
235 qsort(descvec->index,
236 vector_active(descvec),
237 sizeof(void *), cmp_desc);
238 }
239 }
240}
241
242/* Breaking up string into each command piece. I assume given
243 character is separated by a space character. Return value is a
244 vector which includes char ** data element. */
245vector cmd_make_strvec(const char *string)
246{
247 const char *cp, *start;
248 char *token;
249 int strlen;
250 vector strvec;
251
252 if (string == NULL)
253 return NULL;
254
255 cp = string;
256
257 /* Skip white spaces. */
258 while (isspace((int)*cp) && *cp != '\0')
259 cp++;
260
261 /* Return if there is only white spaces */
262 if (*cp == '\0')
263 return NULL;
264
265 if (*cp == '!' || *cp == '#')
266 return NULL;
267
268 /* Prepare return vector. */
269 strvec = vector_init(VECTOR_MIN_SIZE);
270
271 /* Copy each command piece and set into vector. */
272 while (1) {
273 start = cp;
274 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
275 *cp != '\0')
276 cp++;
277 strlen = cp - start;
Harald Welte0224e4d2009-08-07 13:25:41 +0200278 token = _talloc_zero(tall_vty_ctx, strlen + 1, "make_strvec");
Harald Welte955049f2009-03-10 12:16:51 +0000279 memcpy(token, start, strlen);
280 *(token + strlen) = '\0';
281 vector_set(strvec, token);
282
283 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
284 *cp != '\0')
285 cp++;
286
287 if (*cp == '\0')
288 return strvec;
289 }
290}
291
292/* Free allocated string vector. */
293void cmd_free_strvec(vector v)
294{
295 unsigned int i;
296 char *cp;
297
298 if (!v)
299 return;
300
301 for (i = 0; i < vector_active(v); i++)
302 if ((cp = vector_slot(v, i)) != NULL)
Harald Welte2477d932009-08-07 00:32:41 +0200303 talloc_free(cp);
Harald Welte955049f2009-03-10 12:16:51 +0000304
305 vector_free(v);
306}
307
308/* Fetch next description. Used in cmd_make_descvec(). */
309static char *cmd_desc_str(const char **string)
310{
311 const char *cp, *start;
312 char *token;
313 int strlen;
314
315 cp = *string;
316
317 if (cp == NULL)
318 return NULL;
319
320 /* Skip white spaces. */
321 while (isspace((int)*cp) && *cp != '\0')
322 cp++;
323
324 /* Return if there is only white spaces */
325 if (*cp == '\0')
326 return NULL;
327
328 start = cp;
329
330 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
331 cp++;
332
333 strlen = cp - start;
Harald Welte0224e4d2009-08-07 13:25:41 +0200334 token = _talloc_zero(tall_vty_ctx, strlen + 1, "cmd_desc_str");
Harald Welte955049f2009-03-10 12:16:51 +0000335 memcpy(token, start, strlen);
336 *(token + strlen) = '\0';
337
338 *string = cp;
339
340 return token;
341}
342
343/* New string vector. */
344static vector cmd_make_descvec(const char *string, const char *descstr)
345{
346 int multiple = 0;
347 const char *sp;
348 char *token;
349 int len;
350 const char *cp;
351 const char *dp;
352 vector allvec;
353 vector strvec = NULL;
354 struct desc *desc;
355
356 cp = string;
357 dp = descstr;
358
359 if (cp == NULL)
360 return NULL;
361
362 allvec = vector_init(VECTOR_MIN_SIZE);
363
364 while (1) {
365 while (isspace((int)*cp) && *cp != '\0')
366 cp++;
367
368 if (*cp == '(') {
369 multiple = 1;
370 cp++;
371 }
372 if (*cp == ')') {
373 multiple = 0;
374 cp++;
375 }
376 if (*cp == '|') {
377 if (!multiple) {
378 fprintf(stderr, "Command parse error!: %s\n",
379 string);
380 exit(1);
381 }
382 cp++;
383 }
384
385 while (isspace((int)*cp) && *cp != '\0')
386 cp++;
387
388 if (*cp == '(') {
389 multiple = 1;
390 cp++;
391 }
392
393 if (*cp == '\0')
394 return allvec;
395
396 sp = cp;
397
398 while (!
399 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
400 || *cp == ')' || *cp == '|') && *cp != '\0')
401 cp++;
402
403 len = cp - sp;
404
Harald Welte0224e4d2009-08-07 13:25:41 +0200405 token = _talloc_zero(tall_vty_ctx, len + 1, "cmd_make_descvec");
Harald Welte955049f2009-03-10 12:16:51 +0000406 memcpy(token, sp, len);
407 *(token + len) = '\0';
408
Harald Welte0224e4d2009-08-07 13:25:41 +0200409 desc = talloc_zero(tall_vty_ctx, struct desc);
Harald Welte955049f2009-03-10 12:16:51 +0000410 desc->cmd = token;
411 desc->str = cmd_desc_str(&dp);
412
413 if (multiple) {
414 if (multiple == 1) {
415 strvec = vector_init(VECTOR_MIN_SIZE);
416 vector_set(allvec, strvec);
417 }
418 multiple++;
419 } else {
420 strvec = vector_init(VECTOR_MIN_SIZE);
421 vector_set(allvec, strvec);
422 }
423 vector_set(strvec, desc);
424 }
425}
426
427/* Count mandantory string vector size. This is to determine inputed
428 command has enough command length. */
429static int cmd_cmdsize(vector strvec)
430{
431 unsigned int i;
432 int size = 0;
433 vector descvec;
434 struct desc *desc;
435
436 for (i = 0; i < vector_active(strvec); i++)
437 if ((descvec = vector_slot(strvec, i)) != NULL) {
438 if ((vector_active(descvec)) == 1
439 && (desc = vector_slot(descvec, 0)) != NULL) {
440 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
441 return size;
442 else
443 size++;
444 } else
445 size++;
446 }
447 return size;
448}
449
450/* Return prompt character of specified node. */
451const char *cmd_prompt(enum node_type node)
452{
453 struct cmd_node *cnode;
454
455 cnode = vector_slot(cmdvec, node);
456 return cnode->prompt;
457}
458
459/* Install a command into a node. */
460void install_element(enum node_type ntype, struct cmd_element *cmd)
461{
462 struct cmd_node *cnode;
463
464 cnode = vector_slot(cmdvec, ntype);
465
466 if (cnode == NULL) {
467 fprintf(stderr,
468 "Command node %d doesn't exist, please check it\n",
469 ntype);
470 exit(1);
471 }
472
473 vector_set(cnode->cmd_vector, cmd);
474
475 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
476 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
477}
478
Harald Welteb1f21d42009-08-06 18:51:23 +0200479#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +0000480static unsigned char itoa64[] =
481 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
482
483static void to64(char *s, long v, int n)
484{
485 while (--n >= 0) {
486 *s++ = itoa64[v & 0x3f];
487 v >>= 6;
488 }
489}
490
491static char *zencrypt(const char *passwd)
492{
493 char salt[6];
494 struct timeval tv;
495 char *crypt(const char *, const char *);
496
497 gettimeofday(&tv, 0);
498
499 to64(&salt[0], random(), 3);
500 to64(&salt[3], tv.tv_usec, 3);
501 salt[5] = '\0';
502
503 return crypt(passwd, salt);
504}
Harald Welteb1f21d42009-08-06 18:51:23 +0200505#endif
Harald Welte955049f2009-03-10 12:16:51 +0000506
507/* This function write configuration of this host. */
508static int config_write_host(struct vty *vty)
509{
510#if 0
511 if (host.name)
512 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
513
514 if (host.encrypt) {
515 if (host.password_encrypt)
516 vty_out(vty, "password 8 %s%s", host.password_encrypt,
517 VTY_NEWLINE);
518 if (host.enable_encrypt)
519 vty_out(vty, "enable password 8 %s%s",
520 host.enable_encrypt, VTY_NEWLINE);
521 } else {
522 if (host.password)
523 vty_out(vty, "password %s%s", host.password,
524 VTY_NEWLINE);
525 if (host.enable)
526 vty_out(vty, "enable password %s%s", host.enable,
527 VTY_NEWLINE);
528 }
529
530 if (zlog_default->default_lvl != LOG_DEBUG) {
531 vty_out(vty, "! N.B. The 'log trap' command is deprecated.%s",
532 VTY_NEWLINE);
533 vty_out(vty, "log trap %s%s",
534 zlog_priority[zlog_default->default_lvl], VTY_NEWLINE);
535 }
536
537 if (host.logfile
538 && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED)) {
539 vty_out(vty, "log file %s", host.logfile);
540 if (zlog_default->maxlvl[ZLOG_DEST_FILE] !=
541 zlog_default->default_lvl)
542 vty_out(vty, " %s",
543 zlog_priority[zlog_default->
544 maxlvl[ZLOG_DEST_FILE]]);
545 vty_out(vty, "%s", VTY_NEWLINE);
546 }
547
548 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED) {
549 vty_out(vty, "log stdout");
550 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] !=
551 zlog_default->default_lvl)
552 vty_out(vty, " %s",
553 zlog_priority[zlog_default->
554 maxlvl[ZLOG_DEST_STDOUT]]);
555 vty_out(vty, "%s", VTY_NEWLINE);
556 }
557
558 if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
559 vty_out(vty, "no log monitor%s", VTY_NEWLINE);
560 else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] !=
561 zlog_default->default_lvl)
562 vty_out(vty, "log monitor %s%s",
563 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],
564 VTY_NEWLINE);
565
566 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED) {
567 vty_out(vty, "log syslog");
568 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] !=
569 zlog_default->default_lvl)
570 vty_out(vty, " %s",
571 zlog_priority[zlog_default->
572 maxlvl[ZLOG_DEST_SYSLOG]]);
573 vty_out(vty, "%s", VTY_NEWLINE);
574 }
575
576 if (zlog_default->facility != LOG_DAEMON)
577 vty_out(vty, "log facility %s%s",
578 facility_name(zlog_default->facility), VTY_NEWLINE);
579
580 if (zlog_default->record_priority == 1)
581 vty_out(vty, "log record-priority%s", VTY_NEWLINE);
582
583 if (host.advanced)
584 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
585
586 if (host.encrypt)
587 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
588
589 if (host.lines >= 0)
590 vty_out(vty, "service terminal-length %d%s", host.lines,
591 VTY_NEWLINE);
592
593 if (host.motdfile)
594 vty_out(vty, "banner motd file %s%s", host.motdfile,
595 VTY_NEWLINE);
596 else if (!host.motd)
597 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
598
599#endif
600 return 1;
601}
602
603/* Utility function for getting command vector. */
604static vector cmd_node_vector(vector v, enum node_type ntype)
605{
606 struct cmd_node *cnode = vector_slot(v, ntype);
607 return cnode->cmd_vector;
608}
609
610#if 0
611/* Filter command vector by symbol. This function is not actually used;
612 * should it be deleted? */
613static int cmd_filter_by_symbol(char *command, char *symbol)
614{
615 int i, lim;
616
617 if (strcmp(symbol, "IPV4_ADDRESS") == 0) {
618 i = 0;
619 lim = strlen(command);
620 while (i < lim) {
621 if (!
622 (isdigit((int)command[i]) || command[i] == '.'
623 || command[i] == '/'))
624 return 1;
625 i++;
626 }
627 return 0;
628 }
629 if (strcmp(symbol, "STRING") == 0) {
630 i = 0;
631 lim = strlen(command);
632 while (i < lim) {
633 if (!
634 (isalpha((int)command[i]) || command[i] == '_'
635 || command[i] == '-'))
636 return 1;
637 i++;
638 }
639 return 0;
640 }
641 if (strcmp(symbol, "IFNAME") == 0) {
642 i = 0;
643 lim = strlen(command);
644 while (i < lim) {
645 if (!isalnum((int)command[i]))
646 return 1;
647 i++;
648 }
649 return 0;
650 }
651 return 0;
652}
653#endif
654
655/* Completion match types. */
656enum match_type {
657 no_match,
658 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,
666 exact_match
667};
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;
1033 unsigned long min, max, val;
1034
1035 if (str == NULL)
1036 return 1;
1037
1038 val = strtoul(str, &endptr, 10);
1039 if (*endptr != '\0')
1040 return 0;
1041
1042 range++;
1043 p = strchr(range, '-');
1044 if (p == NULL)
1045 return 0;
1046 if (p - range > DECIMAL_STRLEN_MAX)
1047 return 0;
1048 strncpy(buf, range, p - range);
1049 buf[p - range] = '\0';
1050 min = strtoul(buf, &endptr, 10);
1051 if (*endptr != '\0')
1052 return 0;
1053
1054 range = p + 1;
1055 p = strchr(range, '>');
1056 if (p == NULL)
1057 return 0;
1058 if (p - range > DECIMAL_STRLEN_MAX)
1059 return 0;
1060 strncpy(buf, range, p - range);
1061 buf[p - range] = '\0';
1062 max = strtoul(buf, &endptr, 10);
1063 if (*endptr != '\0')
1064 return 0;
1065
1066 if (val < min || val > max)
1067 return 0;
1068
1069 return 1;
1070}
1071
1072/* Make completion match and return match type flag. */
1073static enum match_type
1074cmd_filter_by_completion(char *command, vector v, unsigned int index)
1075{
1076 unsigned int i;
1077 const char *str;
1078 struct cmd_element *cmd_element;
1079 enum match_type match_type;
1080 vector descvec;
1081 struct desc *desc;
1082
1083 match_type = no_match;
1084
1085 /* If command and cmd_element string does not match set NULL to vector */
1086 for (i = 0; i < vector_active(v); i++)
1087 if ((cmd_element = vector_slot(v, i)) != NULL) {
1088 if (index >= vector_active(cmd_element->strvec))
1089 vector_slot(v, i) = NULL;
1090 else {
1091 unsigned int j;
1092 int matched = 0;
1093
1094 descvec =
1095 vector_slot(cmd_element->strvec, index);
1096
1097 for (j = 0; j < vector_active(descvec); j++)
1098 if ((desc = vector_slot(descvec, j))) {
1099 str = desc->cmd;
1100
1101 if (CMD_VARARG(str)) {
1102 if (match_type <
1103 vararg_match)
1104 match_type =
1105 vararg_match;
1106 matched++;
1107 } else if (CMD_RANGE(str)) {
1108 if (cmd_range_match
1109 (str, command)) {
1110 if (match_type <
1111 range_match)
1112 match_type
1113 =
1114 range_match;
1115
1116 matched++;
1117 }
1118 }
1119#ifdef HAVE_IPV6
1120 else if (CMD_IPV6(str)) {
1121 if (cmd_ipv6_match
1122 (command)) {
1123 if (match_type <
1124 ipv6_match)
1125 match_type
1126 =
1127 ipv6_match;
1128
1129 matched++;
1130 }
1131 } else if (CMD_IPV6_PREFIX(str)) {
1132 if (cmd_ipv6_prefix_match(command)) {
1133 if (match_type <
1134 ipv6_prefix_match)
1135 match_type
1136 =
1137 ipv6_prefix_match;
1138
1139 matched++;
1140 }
1141 }
1142#endif /* HAVE_IPV6 */
1143 else if (CMD_IPV4(str)) {
1144 if (cmd_ipv4_match
1145 (command)) {
1146 if (match_type <
1147 ipv4_match)
1148 match_type
1149 =
1150 ipv4_match;
1151
1152 matched++;
1153 }
1154 } else if (CMD_IPV4_PREFIX(str)) {
1155 if (cmd_ipv4_prefix_match(command)) {
1156 if (match_type <
1157 ipv4_prefix_match)
1158 match_type
1159 =
1160 ipv4_prefix_match;
1161 matched++;
1162 }
1163 } else
1164 /* Check is this point's argument optional ? */
1165 if (CMD_OPTION(str)
1166 ||
1167 CMD_VARIABLE(str)) {
1168 if (match_type <
1169 extend_match)
1170 match_type =
1171 extend_match;
1172 matched++;
1173 } else
1174 if (strncmp
1175 (command, str,
1176 strlen(command)) ==
1177 0) {
1178 if (strcmp(command, str)
1179 == 0)
1180 match_type =
1181 exact_match;
1182 else {
1183 if (match_type <
1184 partly_match)
1185 match_type
1186 =
1187 partly_match;
1188 }
1189 matched++;
1190 }
1191 }
1192 if (!matched)
1193 vector_slot(v, i) = NULL;
1194 }
1195 }
1196 return match_type;
1197}
1198
1199/* Filter vector by command character with index. */
1200static enum match_type
1201cmd_filter_by_string(char *command, vector v, unsigned int index)
1202{
1203 unsigned int i;
1204 const char *str;
1205 struct cmd_element *cmd_element;
1206 enum match_type match_type;
1207 vector descvec;
1208 struct desc *desc;
1209
1210 match_type = no_match;
1211
1212 /* If command and cmd_element string does not match set NULL to vector */
1213 for (i = 0; i < vector_active(v); i++)
1214 if ((cmd_element = vector_slot(v, i)) != NULL) {
1215 /* If given index is bigger than max string vector of command,
1216 set NULL */
1217 if (index >= vector_active(cmd_element->strvec))
1218 vector_slot(v, i) = NULL;
1219 else {
1220 unsigned int j;
1221 int matched = 0;
1222
1223 descvec =
1224 vector_slot(cmd_element->strvec, index);
1225
1226 for (j = 0; j < vector_active(descvec); j++)
1227 if ((desc = vector_slot(descvec, j))) {
1228 str = desc->cmd;
1229
1230 if (CMD_VARARG(str)) {
1231 if (match_type <
1232 vararg_match)
1233 match_type =
1234 vararg_match;
1235 matched++;
1236 } else if (CMD_RANGE(str)) {
1237 if (cmd_range_match
1238 (str, command)) {
1239 if (match_type <
1240 range_match)
1241 match_type
1242 =
1243 range_match;
1244 matched++;
1245 }
1246 }
1247#ifdef HAVE_IPV6
1248 else if (CMD_IPV6(str)) {
1249 if (cmd_ipv6_match
1250 (command) ==
1251 exact_match) {
1252 if (match_type <
1253 ipv6_match)
1254 match_type
1255 =
1256 ipv6_match;
1257 matched++;
1258 }
1259 } else if (CMD_IPV6_PREFIX(str)) {
1260 if (cmd_ipv6_prefix_match(command) == exact_match) {
1261 if (match_type <
1262 ipv6_prefix_match)
1263 match_type
1264 =
1265 ipv6_prefix_match;
1266 matched++;
1267 }
1268 }
1269#endif /* HAVE_IPV6 */
1270 else if (CMD_IPV4(str)) {
1271 if (cmd_ipv4_match
1272 (command) ==
1273 exact_match) {
1274 if (match_type <
1275 ipv4_match)
1276 match_type
1277 =
1278 ipv4_match;
1279 matched++;
1280 }
1281 } else if (CMD_IPV4_PREFIX(str)) {
1282 if (cmd_ipv4_prefix_match(command) == exact_match) {
1283 if (match_type <
1284 ipv4_prefix_match)
1285 match_type
1286 =
1287 ipv4_prefix_match;
1288 matched++;
1289 }
1290 } else if (CMD_OPTION(str)
1291 || CMD_VARIABLE(str))
1292 {
1293 if (match_type <
1294 extend_match)
1295 match_type =
1296 extend_match;
1297 matched++;
1298 } else {
1299 if (strcmp(command, str)
1300 == 0) {
1301 match_type =
1302 exact_match;
1303 matched++;
1304 }
1305 }
1306 }
1307 if (!matched)
1308 vector_slot(v, i) = NULL;
1309 }
1310 }
1311 return match_type;
1312}
1313
1314/* Check ambiguous match */
1315static int
1316is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1317{
1318 unsigned int i;
1319 unsigned int j;
1320 const char *str = NULL;
1321 struct cmd_element *cmd_element;
1322 const char *matched = NULL;
1323 vector descvec;
1324 struct desc *desc;
1325
1326 for (i = 0; i < vector_active(v); i++)
1327 if ((cmd_element = vector_slot(v, i)) != NULL) {
1328 int match = 0;
1329
1330 descvec = vector_slot(cmd_element->strvec, index);
1331
1332 for (j = 0; j < vector_active(descvec); j++)
1333 if ((desc = vector_slot(descvec, j))) {
1334 enum match_type ret;
1335
1336 str = desc->cmd;
1337
1338 switch (type) {
1339 case exact_match:
1340 if (!
1341 (CMD_OPTION(str)
1342 || CMD_VARIABLE(str))
1343&& strcmp(command, str) == 0)
1344 match++;
1345 break;
1346 case partly_match:
1347 if (!
1348 (CMD_OPTION(str)
1349 || CMD_VARIABLE(str))
1350&& strncmp(command, str, strlen(command)) == 0) {
1351 if (matched
1352 && strcmp(matched,
1353 str) != 0)
1354 return 1; /* There is ambiguous match. */
1355 else
1356 matched = str;
1357 match++;
1358 }
1359 break;
1360 case range_match:
1361 if (cmd_range_match
1362 (str, command)) {
1363 if (matched
1364 && strcmp(matched,
1365 str) != 0)
1366 return 1;
1367 else
1368 matched = str;
1369 match++;
1370 }
1371 break;
1372#ifdef HAVE_IPV6
1373 case ipv6_match:
1374 if (CMD_IPV6(str))
1375 match++;
1376 break;
1377 case ipv6_prefix_match:
1378 if ((ret =
1379 cmd_ipv6_prefix_match
1380 (command)) != no_match) {
1381 if (ret == partly_match)
1382 return 2; /* There is incomplete match. */
1383
1384 match++;
1385 }
1386 break;
1387#endif /* HAVE_IPV6 */
1388 case ipv4_match:
1389 if (CMD_IPV4(str))
1390 match++;
1391 break;
1392 case ipv4_prefix_match:
1393 if ((ret =
1394 cmd_ipv4_prefix_match
1395 (command)) != no_match) {
1396 if (ret == partly_match)
1397 return 2; /* There is incomplete match. */
1398
1399 match++;
1400 }
1401 break;
1402 case extend_match:
1403 if (CMD_OPTION(str)
1404 || CMD_VARIABLE(str))
1405 match++;
1406 break;
1407 case no_match:
1408 default:
1409 break;
1410 }
1411 }
1412 if (!match)
1413 vector_slot(v, i) = NULL;
1414 }
1415 return 0;
1416}
1417
1418/* If src matches dst return dst string, otherwise return NULL */
1419static const char *cmd_entry_function(const char *src, const char *dst)
1420{
1421 /* Skip variable arguments. */
1422 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1423 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1424 return NULL;
1425
1426 /* In case of 'command \t', given src is NULL string. */
1427 if (src == NULL)
1428 return dst;
1429
1430 /* Matched with input string. */
1431 if (strncmp(src, dst, strlen(src)) == 0)
1432 return dst;
1433
1434 return NULL;
1435}
1436
1437/* If src matches dst return dst string, otherwise return NULL */
1438/* This version will return the dst string always if it is
1439 CMD_VARIABLE for '?' key processing */
1440static const char *cmd_entry_function_desc(const char *src, const char *dst)
1441{
1442 if (CMD_VARARG(dst))
1443 return dst;
1444
1445 if (CMD_RANGE(dst)) {
1446 if (cmd_range_match(dst, src))
1447 return dst;
1448 else
1449 return NULL;
1450 }
1451#ifdef HAVE_IPV6
1452 if (CMD_IPV6(dst)) {
1453 if (cmd_ipv6_match(src))
1454 return dst;
1455 else
1456 return NULL;
1457 }
1458
1459 if (CMD_IPV6_PREFIX(dst)) {
1460 if (cmd_ipv6_prefix_match(src))
1461 return dst;
1462 else
1463 return NULL;
1464 }
1465#endif /* HAVE_IPV6 */
1466
1467 if (CMD_IPV4(dst)) {
1468 if (cmd_ipv4_match(src))
1469 return dst;
1470 else
1471 return NULL;
1472 }
1473
1474 if (CMD_IPV4_PREFIX(dst)) {
1475 if (cmd_ipv4_prefix_match(src))
1476 return dst;
1477 else
1478 return NULL;
1479 }
1480
1481 /* Optional or variable commands always match on '?' */
1482 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1483 return dst;
1484
1485 /* In case of 'command \t', given src is NULL string. */
1486 if (src == NULL)
1487 return dst;
1488
1489 if (strncmp(src, dst, strlen(src)) == 0)
1490 return dst;
1491 else
1492 return NULL;
1493}
1494
1495/* Check same string element existence. If it isn't there return
1496 1. */
1497static int cmd_unique_string(vector v, const char *str)
1498{
1499 unsigned int i;
1500 char *match;
1501
1502 for (i = 0; i < vector_active(v); i++)
1503 if ((match = vector_slot(v, i)) != NULL)
1504 if (strcmp(match, str) == 0)
1505 return 0;
1506 return 1;
1507}
1508
1509/* Compare string to description vector. If there is same string
1510 return 1 else return 0. */
1511static int desc_unique_string(vector v, const char *str)
1512{
1513 unsigned int i;
1514 struct desc *desc;
1515
1516 for (i = 0; i < vector_active(v); i++)
1517 if ((desc = vector_slot(v, i)) != NULL)
1518 if (strcmp(desc->cmd, str) == 0)
1519 return 1;
1520 return 0;
1521}
1522
1523static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1524{
1525 if (first_word != NULL &&
1526 node != AUTH_NODE &&
1527 node != VIEW_NODE &&
1528 node != AUTH_ENABLE_NODE &&
1529 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1530 return 1;
1531 return 0;
1532}
1533
1534/* '?' describe command support. */
1535static vector
1536cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1537{
1538 unsigned int i;
1539 vector cmd_vector;
1540#define INIT_MATCHVEC_SIZE 10
1541 vector matchvec;
1542 struct cmd_element *cmd_element;
1543 unsigned int index;
1544 int ret;
1545 enum match_type match;
1546 char *command;
1547 static struct desc desc_cr = { "<cr>", "" };
1548
1549 /* Set index. */
1550 if (vector_active(vline) == 0) {
1551 *status = CMD_ERR_NO_MATCH;
1552 return NULL;
1553 } else
1554 index = vector_active(vline) - 1;
1555
1556 /* Make copy vector of current node's command vector. */
1557 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1558
1559 /* Prepare match vector */
1560 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1561
1562 /* Filter commands. */
1563 /* Only words precedes current word will be checked in this loop. */
1564 for (i = 0; i < index; i++)
1565 if ((command = vector_slot(vline, i))) {
1566 match =
1567 cmd_filter_by_completion(command, cmd_vector, i);
1568
1569 if (match == vararg_match) {
1570 struct cmd_element *cmd_element;
1571 vector descvec;
1572 unsigned int j, k;
1573
1574 for (j = 0; j < vector_active(cmd_vector); j++)
1575 if ((cmd_element =
1576 vector_slot(cmd_vector, j)) != NULL
1577 &&
1578 (vector_active
1579 (cmd_element->strvec))) {
1580 descvec =
1581 vector_slot(cmd_element->
1582 strvec,
1583 vector_active
1584 (cmd_element->
1585 strvec) - 1);
1586 for (k = 0;
1587 k < vector_active(descvec);
1588 k++) {
1589 struct desc *desc =
1590 vector_slot(descvec,
1591 k);
1592 vector_set(matchvec,
1593 desc);
1594 }
1595 }
1596
1597 vector_set(matchvec, &desc_cr);
1598 vector_free(cmd_vector);
1599
1600 return matchvec;
1601 }
1602
1603 if ((ret =
1604 is_cmd_ambiguous(command, cmd_vector, i,
1605 match)) == 1) {
1606 vector_free(cmd_vector);
1607 *status = CMD_ERR_AMBIGUOUS;
1608 return NULL;
1609 } else if (ret == 2) {
1610 vector_free(cmd_vector);
1611 *status = CMD_ERR_NO_MATCH;
1612 return NULL;
1613 }
1614 }
1615
1616 /* Prepare match vector */
1617 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1618
1619 /* Make sure that cmd_vector is filtered based on current word */
1620 command = vector_slot(vline, index);
1621 if (command)
1622 match = cmd_filter_by_completion(command, cmd_vector, index);
1623
1624 /* Make description vector. */
1625 for (i = 0; i < vector_active(cmd_vector); i++)
1626 if ((cmd_element = vector_slot(cmd_vector, i)) != NULL) {
1627 const char *string = NULL;
1628 vector strvec = cmd_element->strvec;
1629
1630 /* if command is NULL, index may be equal to vector_active */
1631 if (command && index >= vector_active(strvec))
1632 vector_slot(cmd_vector, i) = NULL;
1633 else {
1634 /* Check if command is completed. */
1635 if (command == NULL
1636 && index == vector_active(strvec)) {
1637 string = "<cr>";
1638 if (!desc_unique_string
1639 (matchvec, string))
1640 vector_set(matchvec, &desc_cr);
1641 } else {
1642 unsigned int j;
1643 vector descvec =
1644 vector_slot(strvec, index);
1645 struct desc *desc;
1646
1647 for (j = 0; j < vector_active(descvec);
1648 j++)
1649 if ((desc =
1650 vector_slot(descvec, j))) {
1651 string =
1652 cmd_entry_function_desc
1653 (command,
1654 desc->cmd);
1655 if (string) {
1656 /* Uniqueness check */
1657 if (!desc_unique_string(matchvec, string))
1658 vector_set
1659 (matchvec,
1660 desc);
1661 }
1662 }
1663 }
1664 }
1665 }
1666 vector_free(cmd_vector);
1667
1668 if (vector_slot(matchvec, 0) == NULL) {
1669 vector_free(matchvec);
1670 *status = CMD_ERR_NO_MATCH;
1671 } else
1672 *status = CMD_SUCCESS;
1673
1674 return matchvec;
1675}
1676
1677vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1678{
1679 vector ret;
1680
1681 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1682 enum node_type onode;
1683 vector shifted_vline;
1684 unsigned int index;
1685
1686 onode = vty->node;
1687 vty->node = ENABLE_NODE;
1688 /* We can try it on enable node, cos' the vty is authenticated */
1689
1690 shifted_vline = vector_init(vector_count(vline));
1691 /* use memcpy? */
1692 for (index = 1; index < vector_active(vline); index++) {
1693 vector_set_index(shifted_vline, index - 1,
1694 vector_lookup(vline, index));
1695 }
1696
1697 ret = cmd_describe_command_real(shifted_vline, vty, status);
1698
1699 vector_free(shifted_vline);
1700 vty->node = onode;
1701 return ret;
1702 }
1703
1704 return cmd_describe_command_real(vline, vty, status);
1705}
1706
1707/* Check LCD of matched command. */
1708static int cmd_lcd(char **matched)
1709{
1710 int i;
1711 int j;
1712 int lcd = -1;
1713 char *s1, *s2;
1714 char c1, c2;
1715
1716 if (matched[0] == NULL || matched[1] == NULL)
1717 return 0;
1718
1719 for (i = 1; matched[i] != NULL; i++) {
1720 s1 = matched[i - 1];
1721 s2 = matched[i];
1722
1723 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1724 if (c1 != c2)
1725 break;
1726
1727 if (lcd < 0)
1728 lcd = j;
1729 else {
1730 if (lcd > j)
1731 lcd = j;
1732 }
1733 }
1734 return lcd;
1735}
1736
1737/* Command line completion support. */
1738static char **cmd_complete_command_real(vector vline, struct vty *vty,
1739 int *status)
1740{
1741 unsigned int i;
1742 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1743#define INIT_MATCHVEC_SIZE 10
1744 vector matchvec;
1745 struct cmd_element *cmd_element;
1746 unsigned int index;
1747 char **match_str;
1748 struct desc *desc;
1749 vector descvec;
1750 char *command;
1751 int lcd;
1752
1753 if (vector_active(vline) == 0) {
1754 *status = CMD_ERR_NO_MATCH;
1755 return NULL;
1756 } else
1757 index = vector_active(vline) - 1;
1758
1759 /* First, filter by preceeding command string */
1760 for (i = 0; i < index; i++)
1761 if ((command = vector_slot(vline, i))) {
1762 enum match_type match;
1763 int ret;
1764
1765 /* First try completion match, if there is exactly match return 1 */
1766 match =
1767 cmd_filter_by_completion(command, cmd_vector, i);
1768
1769 /* If there is exact match then filter ambiguous match else check
1770 ambiguousness. */
1771 if ((ret =
1772 is_cmd_ambiguous(command, cmd_vector, i,
1773 match)) == 1) {
1774 vector_free(cmd_vector);
1775 *status = CMD_ERR_AMBIGUOUS;
1776 return NULL;
1777 }
1778 /*
1779 else if (ret == 2)
1780 {
1781 vector_free (cmd_vector);
1782 *status = CMD_ERR_NO_MATCH;
1783 return NULL;
1784 }
1785 */
1786 }
1787
1788 /* Prepare match vector. */
1789 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1790
1791 /* Now we got into completion */
1792 for (i = 0; i < vector_active(cmd_vector); i++)
1793 if ((cmd_element = vector_slot(cmd_vector, i))) {
1794 const char *string;
1795 vector strvec = cmd_element->strvec;
1796
1797 /* Check field length */
1798 if (index >= vector_active(strvec))
1799 vector_slot(cmd_vector, i) = NULL;
1800 else {
1801 unsigned int j;
1802
1803 descvec = vector_slot(strvec, index);
1804 for (j = 0; j < vector_active(descvec); j++)
1805 if ((desc = vector_slot(descvec, j))) {
Harald Weltefc1c3e52009-08-07 13:26:28 +02001806 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1807 if (cmd_unique_string (matchvec, string))
1808 vector_set (matchvec, talloc_strdup(tall_vty_ctx, string));
Harald Welte955049f2009-03-10 12:16:51 +00001809 }
1810 }
1811 }
1812
1813 /* We don't need cmd_vector any more. */
1814 vector_free(cmd_vector);
1815
1816 /* No matched command */
1817 if (vector_slot(matchvec, 0) == NULL) {
1818 vector_free(matchvec);
1819
1820 /* In case of 'command \t' pattern. Do you need '?' command at
1821 the end of the line. */
1822 if (vector_slot(vline, index) == '\0')
1823 *status = CMD_ERR_NOTHING_TODO;
1824 else
1825 *status = CMD_ERR_NO_MATCH;
1826 return NULL;
1827 }
1828
1829 /* Only one matched */
1830 if (vector_slot(matchvec, 1) == NULL) {
1831 match_str = (char **)matchvec->index;
1832 vector_only_wrapper_free(matchvec);
1833 *status = CMD_COMPLETE_FULL_MATCH;
1834 return match_str;
1835 }
1836 /* Make it sure last element is NULL. */
1837 vector_set(matchvec, NULL);
1838
1839 /* Check LCD of matched strings. */
1840 if (vector_slot(vline, index) != NULL) {
1841 lcd = cmd_lcd((char **)matchvec->index);
1842
1843 if (lcd) {
1844 int len = strlen(vector_slot(vline, index));
1845
1846 if (len < lcd) {
1847 char *lcdstr;
1848
Harald Welte0224e4d2009-08-07 13:25:41 +02001849 lcdstr = _talloc_zero(tall_vty_ctx, lcd + 1,
Harald Welte2477d932009-08-07 00:32:41 +02001850 "complete-lcdstr");
Harald Welte955049f2009-03-10 12:16:51 +00001851 memcpy(lcdstr, matchvec->index[0], lcd);
1852 lcdstr[lcd] = '\0';
1853
1854 /* match_str = (char **) &lcdstr; */
1855
1856 /* Free matchvec. */
1857 for (i = 0; i < vector_active(matchvec); i++) {
1858 if (vector_slot(matchvec, i))
Harald Welte2477d932009-08-07 00:32:41 +02001859 talloc_free(vector_slot(matchvec, i));
Harald Welte955049f2009-03-10 12:16:51 +00001860 }
1861 vector_free(matchvec);
1862
1863 /* Make new matchvec. */
1864 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1865 vector_set(matchvec, lcdstr);
1866 match_str = (char **)matchvec->index;
1867 vector_only_wrapper_free(matchvec);
1868
1869 *status = CMD_COMPLETE_MATCH;
1870 return match_str;
1871 }
1872 }
1873 }
1874
1875 match_str = (char **)matchvec->index;
1876 vector_only_wrapper_free(matchvec);
1877 *status = CMD_COMPLETE_LIST_MATCH;
1878 return match_str;
1879}
1880
1881char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1882{
1883 char **ret;
1884
1885 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1886 enum node_type onode;
1887 vector shifted_vline;
1888 unsigned int index;
1889
1890 onode = vty->node;
1891 vty->node = ENABLE_NODE;
1892 /* We can try it on enable node, cos' the vty is authenticated */
1893
1894 shifted_vline = vector_init(vector_count(vline));
1895 /* use memcpy? */
1896 for (index = 1; index < vector_active(vline); index++) {
1897 vector_set_index(shifted_vline, index - 1,
1898 vector_lookup(vline, index));
1899 }
1900
1901 ret = cmd_complete_command_real(shifted_vline, vty, status);
1902
1903 vector_free(shifted_vline);
1904 vty->node = onode;
1905 return ret;
1906 }
1907
1908 return cmd_complete_command_real(vline, vty, status);
1909}
1910
1911/* return parent node */
1912/* MUST eventually converge on CONFIG_NODE */
1913enum node_type node_parent(enum node_type node)
1914{
1915 enum node_type ret;
1916
1917 assert(node > CONFIG_NODE);
1918
1919 switch (node) {
1920 case BGP_VPNV4_NODE:
1921 case BGP_IPV4_NODE:
1922 case BGP_IPV4M_NODE:
1923 case BGP_IPV6_NODE:
1924 ret = BGP_NODE;
1925 break;
1926 case KEYCHAIN_KEY_NODE:
1927 ret = KEYCHAIN_NODE;
1928 break;
1929 default:
1930 ret = CONFIG_NODE;
1931 }
1932
1933 return ret;
1934}
1935
1936/* Execute command by argument vline vector. */
1937static int
1938cmd_execute_command_real(vector vline, struct vty *vty,
1939 struct cmd_element **cmd)
1940{
1941 unsigned int i;
1942 unsigned int index;
1943 vector cmd_vector;
1944 struct cmd_element *cmd_element;
1945 struct cmd_element *matched_element;
1946 unsigned int matched_count, incomplete_count;
1947 int argc;
1948 const char *argv[CMD_ARGC_MAX];
1949 enum match_type match = 0;
1950 int varflag;
1951 char *command;
1952
1953 /* Make copy of command elements. */
1954 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1955
1956 for (index = 0; index < vector_active(vline); index++)
1957 if ((command = vector_slot(vline, index))) {
1958 int ret;
1959
1960 match =
1961 cmd_filter_by_completion(command, cmd_vector,
1962 index);
1963
1964 if (match == vararg_match)
1965 break;
1966
1967 ret =
1968 is_cmd_ambiguous(command, cmd_vector, index, match);
1969
1970 if (ret == 1) {
1971 vector_free(cmd_vector);
1972 return CMD_ERR_AMBIGUOUS;
1973 } else if (ret == 2) {
1974 vector_free(cmd_vector);
1975 return CMD_ERR_NO_MATCH;
1976 }
1977 }
1978
1979 /* Check matched count. */
1980 matched_element = NULL;
1981 matched_count = 0;
1982 incomplete_count = 0;
1983
1984 for (i = 0; i < vector_active(cmd_vector); i++)
1985 if ((cmd_element = vector_slot(cmd_vector, i))) {
1986 if (match == vararg_match
1987 || index >= cmd_element->cmdsize) {
1988 matched_element = cmd_element;
1989#if 0
1990 printf("DEBUG: %s\n", cmd_element->string);
1991#endif
1992 matched_count++;
1993 } else {
1994 incomplete_count++;
1995 }
1996 }
1997
1998 /* Finish of using cmd_vector. */
1999 vector_free(cmd_vector);
2000
2001 /* To execute command, matched_count must be 1. */
2002 if (matched_count == 0) {
2003 if (incomplete_count)
2004 return CMD_ERR_INCOMPLETE;
2005 else
2006 return CMD_ERR_NO_MATCH;
2007 }
2008
2009 if (matched_count > 1)
2010 return CMD_ERR_AMBIGUOUS;
2011
2012 /* Argument treatment */
2013 varflag = 0;
2014 argc = 0;
2015
2016 for (i = 0; i < vector_active(vline); i++) {
2017 if (varflag)
2018 argv[argc++] = vector_slot(vline, i);
2019 else {
2020 vector descvec =
2021 vector_slot(matched_element->strvec, i);
2022
2023 if (vector_active(descvec) == 1) {
2024 struct desc *desc = vector_slot(descvec, 0);
2025
2026 if (CMD_VARARG(desc->cmd))
2027 varflag = 1;
2028
2029 if (varflag || CMD_VARIABLE(desc->cmd)
2030 || CMD_OPTION(desc->cmd))
2031 argv[argc++] = vector_slot(vline, i);
2032 } else
2033 argv[argc++] = vector_slot(vline, i);
2034 }
2035
2036 if (argc >= CMD_ARGC_MAX)
2037 return CMD_ERR_EXEED_ARGC_MAX;
2038 }
2039
2040 /* For vtysh execution. */
2041 if (cmd)
2042 *cmd = matched_element;
2043
2044 if (matched_element->daemon)
2045 return CMD_SUCCESS_DAEMON;
2046
2047 /* Execute matched command. */
2048 return (*matched_element->func) (matched_element, vty, argc, argv);
2049}
2050
2051int
2052cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2053 int vtysh)
2054{
2055 int ret, saved_ret, tried = 0;
2056 enum node_type onode, try_node;
2057
2058 onode = try_node = vty->node;
2059
2060 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2061 vector shifted_vline;
2062 unsigned int index;
2063
2064 vty->node = ENABLE_NODE;
2065 /* We can try it on enable node, cos' the vty is authenticated */
2066
2067 shifted_vline = vector_init(vector_count(vline));
2068 /* use memcpy? */
2069 for (index = 1; index < vector_active(vline); index++) {
2070 vector_set_index(shifted_vline, index - 1,
2071 vector_lookup(vline, index));
2072 }
2073
2074 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2075
2076 vector_free(shifted_vline);
2077 vty->node = onode;
2078 return ret;
2079 }
2080
2081 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2082
2083 if (vtysh)
2084 return saved_ret;
2085
2086 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
2087 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2088 && vty->node > CONFIG_NODE) {
2089 try_node = node_parent(try_node);
2090 vty->node = try_node;
2091 ret = cmd_execute_command_real(vline, vty, cmd);
2092 tried = 1;
2093 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2094 /* succesfull command, leave the node as is */
2095 return ret;
2096 }
2097 }
2098 /* no command succeeded, reset the vty to the original node and
2099 return the error for this node */
2100 if (tried)
2101 vty->node = onode;
2102 return saved_ret;
2103}
2104
2105/* Execute command by argument readline. */
2106int
2107cmd_execute_command_strict(vector vline, struct vty *vty,
2108 struct cmd_element **cmd)
2109{
2110 unsigned int i;
2111 unsigned int index;
2112 vector cmd_vector;
2113 struct cmd_element *cmd_element;
2114 struct cmd_element *matched_element;
2115 unsigned int matched_count, incomplete_count;
2116 int argc;
2117 const char *argv[CMD_ARGC_MAX];
2118 int varflag;
2119 enum match_type match = 0;
2120 char *command;
2121
2122 /* Make copy of command element */
2123 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2124
2125 for (index = 0; index < vector_active(vline); index++)
2126 if ((command = vector_slot(vline, index))) {
2127 int ret;
2128
2129 match = cmd_filter_by_string(vector_slot(vline, index),
2130 cmd_vector, index);
2131
2132 /* If command meets '.VARARG' then finish matching. */
2133 if (match == vararg_match)
2134 break;
2135
2136 ret =
2137 is_cmd_ambiguous(command, cmd_vector, index, match);
2138 if (ret == 1) {
2139 vector_free(cmd_vector);
2140 return CMD_ERR_AMBIGUOUS;
2141 }
2142 if (ret == 2) {
2143 vector_free(cmd_vector);
2144 return CMD_ERR_NO_MATCH;
2145 }
2146 }
2147
2148 /* Check matched count. */
2149 matched_element = NULL;
2150 matched_count = 0;
2151 incomplete_count = 0;
2152 for (i = 0; i < vector_active(cmd_vector); i++)
2153 if (vector_slot(cmd_vector, i) != NULL) {
2154 cmd_element = vector_slot(cmd_vector, i);
2155
2156 if (match == vararg_match
2157 || index >= cmd_element->cmdsize) {
2158 matched_element = cmd_element;
2159 matched_count++;
2160 } else
2161 incomplete_count++;
2162 }
2163
2164 /* Finish of using cmd_vector. */
2165 vector_free(cmd_vector);
2166
2167 /* To execute command, matched_count must be 1. */
2168 if (matched_count == 0) {
2169 if (incomplete_count)
2170 return CMD_ERR_INCOMPLETE;
2171 else
2172 return CMD_ERR_NO_MATCH;
2173 }
2174
2175 if (matched_count > 1)
2176 return CMD_ERR_AMBIGUOUS;
2177
2178 /* Argument treatment */
2179 varflag = 0;
2180 argc = 0;
2181
2182 for (i = 0; i < vector_active(vline); i++) {
2183 if (varflag)
2184 argv[argc++] = vector_slot(vline, i);
2185 else {
2186 vector descvec =
2187 vector_slot(matched_element->strvec, i);
2188
2189 if (vector_active(descvec) == 1) {
2190 struct desc *desc = vector_slot(descvec, 0);
2191
2192 if (CMD_VARARG(desc->cmd))
2193 varflag = 1;
2194
2195 if (varflag || CMD_VARIABLE(desc->cmd)
2196 || CMD_OPTION(desc->cmd))
2197 argv[argc++] = vector_slot(vline, i);
2198 } else
2199 argv[argc++] = vector_slot(vline, i);
2200 }
2201
2202 if (argc >= CMD_ARGC_MAX)
2203 return CMD_ERR_EXEED_ARGC_MAX;
2204 }
2205
2206 /* For vtysh execution. */
2207 if (cmd)
2208 *cmd = matched_element;
2209
2210 if (matched_element->daemon)
2211 return CMD_SUCCESS_DAEMON;
2212
2213 /* Now execute matched command */
2214 return (*matched_element->func) (matched_element, vty, argc, argv);
2215}
2216
Harald Welte955049f2009-03-10 12:16:51 +00002217/* Configration make from file. */
2218int config_from_file(struct vty *vty, FILE * fp)
2219{
2220 int ret;
2221 vector vline;
2222
2223 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2224 vline = cmd_make_strvec(vty->buf);
2225
2226 /* In case of comment line */
2227 if (vline == NULL)
2228 continue;
2229 /* Execute configuration command : this is strict match */
2230 ret = cmd_execute_command_strict(vline, vty, NULL);
2231
2232 /* Try again with setting node to CONFIG_NODE */
2233 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2234 && ret != CMD_ERR_NOTHING_TODO
2235 && vty->node != CONFIG_NODE) {
2236 vty->node = node_parent(vty->node);
2237 ret = cmd_execute_command_strict(vline, vty, NULL);
2238 }
2239
2240 cmd_free_strvec(vline);
2241
2242 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2243 && ret != CMD_ERR_NOTHING_TODO)
2244 return ret;
2245 }
2246 return CMD_SUCCESS;
2247}
Harald Welte955049f2009-03-10 12:16:51 +00002248
2249/* Configration from terminal */
2250DEFUN(config_terminal,
2251 config_terminal_cmd,
2252 "configure terminal",
2253 "Configuration from vty interface\n" "Configuration terminal\n")
2254{
2255 if (vty_config_lock(vty))
2256 vty->node = CONFIG_NODE;
2257 else {
2258 vty_out(vty, "VTY configuration is locked by other VTY%s",
2259 VTY_NEWLINE);
2260 return CMD_WARNING;
2261 }
2262 return CMD_SUCCESS;
2263}
2264
2265/* Enable command */
2266DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2267{
2268 /* If enable password is NULL, change to ENABLE_NODE */
2269 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2270 vty->type == VTY_SHELL_SERV)
2271 vty->node = ENABLE_NODE;
2272 else
2273 vty->node = AUTH_ENABLE_NODE;
2274
2275 return CMD_SUCCESS;
2276}
2277
2278/* Disable command */
2279DEFUN(disable,
2280 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2281{
2282 if (vty->node == ENABLE_NODE)
2283 vty->node = VIEW_NODE;
2284 return CMD_SUCCESS;
2285}
2286
2287/* Down vty node level. */
2288DEFUN(config_exit,
2289 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2290{
2291 switch (vty->node) {
2292 case BTS_NODE:
2293 vty->node = VIEW_NODE;
Harald Welte5258fc42009-03-28 19:07:53 +00002294 vty->index = NULL;
Harald Welte955049f2009-03-10 12:16:51 +00002295 break;
2296 case TRX_NODE:
2297 vty->node = BTS_NODE;
Harald Welte5258fc42009-03-28 19:07:53 +00002298 {
2299 /* set vty->index correctly ! */
2300 struct gsm_bts_trx *trx = vty->index;
2301 vty->index = trx->bts;
2302 }
Harald Welte955049f2009-03-10 12:16:51 +00002303 break;
2304 case TS_NODE:
2305 vty->node = TRX_NODE;
Harald Welte5258fc42009-03-28 19:07:53 +00002306 {
2307 /* set vty->index correctly ! */
2308 struct gsm_bts_trx_ts *ts = vty->index;
2309 vty->index = ts->trx;
2310 }
Harald Welte955049f2009-03-10 12:16:51 +00002311 break;
Harald Welte40f82892009-05-23 17:31:39 +00002312 case SUBSCR_NODE:
2313 vty->node = VIEW_NODE;
2314 subscr_put(vty->index);
2315 vty->index = NULL;
2316 break;
Harald Welte955049f2009-03-10 12:16:51 +00002317 case VIEW_NODE:
2318 case ENABLE_NODE:
2319 if (0) //vty_shell (vty))
2320 exit(0);
2321 else
2322 vty->status = VTY_CLOSE;
2323 break;
2324 case CONFIG_NODE:
2325 vty->node = ENABLE_NODE;
2326 vty_config_unlock(vty);
2327 break;
2328 case INTERFACE_NODE:
2329 case ZEBRA_NODE:
2330 case BGP_NODE:
2331 case RIP_NODE:
2332 case RIPNG_NODE:
2333 case OSPF_NODE:
2334 case OSPF6_NODE:
2335 case ISIS_NODE:
2336 case KEYCHAIN_NODE:
2337 case MASC_NODE:
2338 case RMAP_NODE:
2339 case VTY_NODE:
2340 vty->node = CONFIG_NODE;
2341 break;
2342 case BGP_VPNV4_NODE:
2343 case BGP_IPV4_NODE:
2344 case BGP_IPV4M_NODE:
2345 case BGP_IPV6_NODE:
2346 vty->node = BGP_NODE;
2347 break;
2348 case KEYCHAIN_KEY_NODE:
2349 vty->node = KEYCHAIN_NODE;
2350 break;
2351 default:
2352 break;
2353 }
2354 return CMD_SUCCESS;
2355}
2356
2357/* quit is alias of exit. */
2358ALIAS(config_exit,
2359 config_quit_cmd, "quit", "Exit current mode and down to previous mode\n")
2360
2361/* End of configuration. */
2362 DEFUN(config_end,
2363 config_end_cmd, "end", "End current mode and change to enable mode.")
2364{
2365 switch (vty->node) {
2366 case VIEW_NODE:
2367 case ENABLE_NODE:
2368 /* Nothing to do. */
2369 break;
2370 case CONFIG_NODE:
2371 case INTERFACE_NODE:
2372 case ZEBRA_NODE:
2373 case RIP_NODE:
2374 case RIPNG_NODE:
2375 case BGP_NODE:
2376 case BGP_VPNV4_NODE:
2377 case BGP_IPV4_NODE:
2378 case BGP_IPV4M_NODE:
2379 case BGP_IPV6_NODE:
2380 case RMAP_NODE:
2381 case OSPF_NODE:
2382 case OSPF6_NODE:
2383 case ISIS_NODE:
2384 case KEYCHAIN_NODE:
2385 case KEYCHAIN_KEY_NODE:
2386 case MASC_NODE:
2387 case VTY_NODE:
2388 vty_config_unlock(vty);
2389 vty->node = ENABLE_NODE;
2390 break;
2391 default:
2392 break;
2393 }
2394 return CMD_SUCCESS;
2395}
2396
2397/* Show version. */
2398DEFUN(show_version,
2399 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2400{
2401 vty_out(vty, "%s %s (%s).%s", QUAGGA_PROGNAME, QUAGGA_VERSION,
2402 host.name ? host.name : "", VTY_NEWLINE);
2403 vty_out(vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE);
2404
2405 return CMD_SUCCESS;
2406}
2407
2408/* Help display function for all node. */
2409DEFUN(config_help,
2410 config_help_cmd, "help", "Description of the interactive help system\n")
2411{
2412 vty_out(vty,
2413 "This VTY provides advanced help features. When you need help,%s\
2414anytime at the command line please press '?'.%s\
2415%s\
2416If nothing matches, the help list will be empty and you must backup%s\
2417 until entering a '?' shows the available options.%s\
2418Two styles of help are provided:%s\
24191. Full help is available when you are ready to enter a%s\
2420command argument (e.g. 'show ?') and describes each possible%s\
2421argument.%s\
24222. Partial help is provided when an abbreviated argument is entered%s\
2423 and you want to know what arguments match the input%s\
2424 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2425 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2426 return CMD_SUCCESS;
2427}
2428
2429/* Help display function for all node. */
2430DEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2431{
2432 unsigned int i;
2433 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2434 struct cmd_element *cmd;
2435
2436 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2437 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2438 && !(cmd->attr == CMD_ATTR_DEPRECATED
2439 || cmd->attr == CMD_ATTR_HIDDEN))
2440 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2441 return CMD_SUCCESS;
2442}
2443
Harald Welte955049f2009-03-10 12:16:51 +00002444/* Write current configuration into file. */
2445DEFUN(config_write_file,
2446 config_write_file_cmd,
2447 "write file",
2448 "Write running configuration to memory, network, or terminal\n"
2449 "Write to configuration file\n")
2450{
2451 unsigned int i;
2452 int fd;
2453 struct cmd_node *node;
2454 char *config_file;
2455 char *config_file_tmp = NULL;
2456 char *config_file_sav = NULL;
2457 struct vty *file_vty;
2458
2459 /* Check and see if we are operating under vtysh configuration */
2460 if (host.config == NULL) {
2461 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2462 VTY_NEWLINE);
2463 return CMD_WARNING;
2464 }
2465
2466 /* Get filename. */
2467 config_file = host.config;
2468
2469 config_file_sav =
Harald Welte0224e4d2009-08-07 13:25:41 +02002470 _talloc_zero(tall_vty_ctx,
Harald Welte2477d932009-08-07 00:32:41 +02002471 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2472 "config_file_sav");
Harald Welte955049f2009-03-10 12:16:51 +00002473 strcpy(config_file_sav, config_file);
2474 strcat(config_file_sav, CONF_BACKUP_EXT);
2475
Harald Welte0224e4d2009-08-07 13:25:41 +02002476 config_file_tmp = _talloc_zero(tall_vty_ctx, strlen(config_file) + 8,
Harald Welte2477d932009-08-07 00:32:41 +02002477 "config_file_tmp");
Harald Welte955049f2009-03-10 12:16:51 +00002478 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2479
2480 /* Open file to configuration write. */
2481 fd = mkstemp(config_file_tmp);
2482 if (fd < 0) {
2483 vty_out(vty, "Can't open configuration file %s.%s",
2484 config_file_tmp, VTY_NEWLINE);
Harald Welte2477d932009-08-07 00:32:41 +02002485 talloc_free(config_file_tmp);
2486 talloc_free(config_file_sav);
Harald Welte955049f2009-03-10 12:16:51 +00002487 return CMD_WARNING;
2488 }
2489
2490 /* Make vty for configuration file. */
2491 file_vty = vty_new();
2492 file_vty->fd = fd;
2493 file_vty->type = VTY_FILE;
2494
2495 /* Config file header print. */
Harald Welte2477d932009-08-07 00:32:41 +02002496 vty_out(file_vty, "!\n! OpenBSC configuration saved from vty\n! ");
Harald Welte955049f2009-03-10 12:16:51 +00002497 //vty_time_print (file_vty, 1);
2498 vty_out(file_vty, "!\n");
2499
2500 for (i = 0; i < vector_active(cmdvec); i++)
2501 if ((node = vector_slot(cmdvec, i)) && node->func) {
2502 if ((*node->func) (file_vty))
2503 vty_out(file_vty, "!\n");
2504 }
2505 vty_close(file_vty);
2506
2507 if (unlink(config_file_sav) != 0)
2508 if (errno != ENOENT) {
2509 vty_out(vty,
2510 "Can't unlink backup configuration file %s.%s",
2511 config_file_sav, VTY_NEWLINE);
Harald Welte2477d932009-08-07 00:32:41 +02002512 talloc_free(config_file_sav);
2513 talloc_free(config_file_tmp);
Harald Welte955049f2009-03-10 12:16:51 +00002514 unlink(config_file_tmp);
2515 return CMD_WARNING;
2516 }
2517 if (link(config_file, config_file_sav) != 0) {
2518 vty_out(vty, "Can't backup old configuration file %s.%s",
2519 config_file_sav, VTY_NEWLINE);
Harald Welte2477d932009-08-07 00:32:41 +02002520 talloc_free(config_file_sav);
2521 talloc_free(config_file_tmp);
Harald Welte955049f2009-03-10 12:16:51 +00002522 unlink(config_file_tmp);
2523 return CMD_WARNING;
2524 }
2525 sync();
2526 if (unlink(config_file) != 0) {
2527 vty_out(vty, "Can't unlink configuration file %s.%s",
2528 config_file, VTY_NEWLINE);
Harald Welte2477d932009-08-07 00:32:41 +02002529 talloc_free(config_file_sav);
2530 talloc_free(config_file_tmp);
Harald Welte955049f2009-03-10 12:16:51 +00002531 unlink(config_file_tmp);
2532 return CMD_WARNING;
2533 }
2534 if (link(config_file_tmp, config_file) != 0) {
2535 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2536 VTY_NEWLINE);
Harald Welte2477d932009-08-07 00:32:41 +02002537 talloc_free(config_file_sav);
2538 talloc_free(config_file_tmp);
Harald Welte955049f2009-03-10 12:16:51 +00002539 unlink(config_file_tmp);
2540 return CMD_WARNING;
2541 }
2542 unlink(config_file_tmp);
2543 sync();
2544
Harald Welte2477d932009-08-07 00:32:41 +02002545 talloc_free(config_file_sav);
2546 talloc_free(config_file_tmp);
Harald Welte955049f2009-03-10 12:16:51 +00002547
2548 if (chmod(config_file, CONFIGFILE_MASK) != 0) {
2549 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
Harald Welte4a3023d2009-08-06 18:50:10 +02002550 config_file, strerror(errno), errno, VTY_NEWLINE);
Harald Welte955049f2009-03-10 12:16:51 +00002551 return CMD_WARNING;
2552 }
2553
2554 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2555 return CMD_SUCCESS;
2556}
2557
2558ALIAS(config_write_file,
2559 config_write_cmd,
2560 "write", "Write running configuration to memory, network, or terminal\n")
2561
2562 ALIAS(config_write_file,
2563 config_write_memory_cmd,
2564 "write memory",
2565 "Write running configuration to memory, network, or terminal\n"
2566 "Write configuration to the file (same as write file)\n")
2567
2568 ALIAS(config_write_file,
2569 copy_runningconfig_startupconfig_cmd,
2570 "copy running-config startup-config",
2571 "Copy configuration\n"
2572 "Copy running config to... \n"
2573 "Copy running config to startup config (same as write file)\n")
2574
2575/* Write current configuration into the terminal. */
2576 DEFUN(config_write_terminal,
2577 config_write_terminal_cmd,
2578 "write terminal",
2579 "Write running configuration to memory, network, or terminal\n"
2580 "Write to terminal\n")
2581{
2582 unsigned int i;
2583 struct cmd_node *node;
2584
2585 if (vty->type == VTY_SHELL_SERV) {
2586 for (i = 0; i < vector_active(cmdvec); i++)
2587 if ((node = vector_slot(cmdvec, i)) && node->func
2588 && node->vtysh) {
2589 if ((*node->func) (vty))
2590 vty_out(vty, "!%s", VTY_NEWLINE);
2591 }
2592 } else {
2593 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2594 VTY_NEWLINE);
2595 vty_out(vty, "!%s", VTY_NEWLINE);
2596
2597 for (i = 0; i < vector_active(cmdvec); i++)
2598 if ((node = vector_slot(cmdvec, i)) && node->func) {
2599 if ((*node->func) (vty))
2600 vty_out(vty, "!%s", VTY_NEWLINE);
2601 }
2602 vty_out(vty, "end%s", VTY_NEWLINE);
2603 }
2604 return CMD_SUCCESS;
2605}
2606
2607/* Write current configuration into the terminal. */
2608ALIAS(config_write_terminal,
2609 show_running_config_cmd,
2610 "show running-config", SHOW_STR "running configuration\n")
2611
2612/* Write startup configuration into the terminal. */
2613 DEFUN(show_startup_config,
2614 show_startup_config_cmd,
2615 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2616{
2617 char buf[BUFSIZ];
2618 FILE *confp;
2619
2620 confp = fopen(host.config, "r");
2621 if (confp == NULL) {
2622 vty_out(vty, "Can't open configuration file [%s]%s",
2623 host.config, VTY_NEWLINE);
2624 return CMD_WARNING;
2625 }
2626
2627 while (fgets(buf, BUFSIZ, confp)) {
2628 char *cp = buf;
2629
2630 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2631 cp++;
2632 *cp = '\0';
2633
2634 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2635 }
2636
2637 fclose(confp);
2638
2639 return CMD_SUCCESS;
2640}
Harald Welte955049f2009-03-10 12:16:51 +00002641
2642/* Hostname configuration */
2643DEFUN(config_hostname,
2644 hostname_cmd,
2645 "hostname WORD",
2646 "Set system's network name\n" "This system's network name\n")
2647{
2648 if (!isalpha((int)*argv[0])) {
2649 vty_out(vty, "Please specify string starting with alphabet%s",
2650 VTY_NEWLINE);
2651 return CMD_WARNING;
2652 }
2653
2654 if (host.name)
Harald Welte2477d932009-08-07 00:32:41 +02002655 talloc_free(host.name);
Harald Welte955049f2009-03-10 12:16:51 +00002656
Harald Weltefc1c3e52009-08-07 13:26:28 +02002657 host.name = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00002658 return CMD_SUCCESS;
2659}
2660
2661DEFUN(config_no_hostname,
2662 no_hostname_cmd,
2663 "no hostname [HOSTNAME]",
2664 NO_STR "Reset system's network name\n" "Host name of this router\n")
2665{
2666 if (host.name)
Harald Welte2477d932009-08-07 00:32:41 +02002667 talloc_free(host.name);
Harald Welte955049f2009-03-10 12:16:51 +00002668 host.name = NULL;
2669 return CMD_SUCCESS;
2670}
2671
2672/* VTY interface password set. */
2673DEFUN(config_password, password_cmd,
2674 "password (8|) WORD",
2675 "Assign the terminal connection password\n"
2676 "Specifies a HIDDEN password will follow\n"
2677 "dummy string \n" "The HIDDEN line password string\n")
2678{
2679 /* Argument check. */
2680 if (argc == 0) {
2681 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2682 return CMD_WARNING;
2683 }
2684
2685 if (argc == 2) {
2686 if (*argv[0] == '8') {
2687 if (host.password)
Harald Welte2477d932009-08-07 00:32:41 +02002688 talloc_free(host.password);
Harald Welte955049f2009-03-10 12:16:51 +00002689 host.password = NULL;
2690 if (host.password_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002691 talloc_free(host.password_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002692 host.password_encrypt = talloc_strdup(tall_vty_ctx, argv[1]);
Harald Welte955049f2009-03-10 12:16:51 +00002693 return CMD_SUCCESS;
2694 } else {
2695 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2696 return CMD_WARNING;
2697 }
2698 }
2699
2700 if (!isalnum((int)*argv[0])) {
2701 vty_out(vty,
2702 "Please specify string starting with alphanumeric%s",
2703 VTY_NEWLINE);
2704 return CMD_WARNING;
2705 }
2706
2707 if (host.password)
Harald Welte2477d932009-08-07 00:32:41 +02002708 talloc_free(host.password);
Harald Welte955049f2009-03-10 12:16:51 +00002709 host.password = NULL;
2710
Harald Welted6cab812009-05-21 07:31:48 +00002711#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +00002712 if (host.encrypt) {
2713 if (host.password_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002714 talloc_free(host.password_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002715 host.password_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(argv[0]));
Harald Welte955049f2009-03-10 12:16:51 +00002716 } else
Harald Welted6cab812009-05-21 07:31:48 +00002717#endif
Harald Weltefc1c3e52009-08-07 13:26:28 +02002718 host.password = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00002719
2720 return CMD_SUCCESS;
2721}
2722
2723ALIAS(config_password, password_text_cmd,
2724 "password LINE",
2725 "Assign the terminal connection password\n"
2726 "The UNENCRYPTED (cleartext) line password\n")
2727
2728/* VTY enable password set. */
2729 DEFUN(config_enable_password, enable_password_cmd,
2730 "enable password (8|) WORD",
2731 "Modify enable password parameters\n"
2732 "Assign the privileged level password\n"
2733 "Specifies a HIDDEN password will follow\n"
2734 "dummy string \n" "The HIDDEN 'enable' password string\n")
2735{
2736 /* Argument check. */
2737 if (argc == 0) {
2738 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2739 return CMD_WARNING;
2740 }
2741
2742 /* Crypt type is specified. */
2743 if (argc == 2) {
2744 if (*argv[0] == '8') {
2745 if (host.enable)
Harald Welte2477d932009-08-07 00:32:41 +02002746 talloc_free(host.enable);
Harald Welte955049f2009-03-10 12:16:51 +00002747 host.enable = NULL;
2748
2749 if (host.enable_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002750 talloc_free(host.enable_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002751 host.enable_encrypt = talloc_strdup(tall_vty_ctx, argv[1]);
Harald Welte955049f2009-03-10 12:16:51 +00002752
2753 return CMD_SUCCESS;
2754 } else {
2755 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2756 return CMD_WARNING;
2757 }
2758 }
2759
2760 if (!isalnum((int)*argv[0])) {
2761 vty_out(vty,
2762 "Please specify string starting with alphanumeric%s",
2763 VTY_NEWLINE);
2764 return CMD_WARNING;
2765 }
2766
2767 if (host.enable)
Harald Welte2477d932009-08-07 00:32:41 +02002768 talloc_free(host.enable);
Harald Welte955049f2009-03-10 12:16:51 +00002769 host.enable = NULL;
2770
2771 /* Plain password input. */
Harald Welted6cab812009-05-21 07:31:48 +00002772#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +00002773 if (host.encrypt) {
2774 if (host.enable_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002775 talloc_free(host.enable_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002776 host.enable_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(argv[0]));
Harald Welte955049f2009-03-10 12:16:51 +00002777 } else
Harald Welted6cab812009-05-21 07:31:48 +00002778#endif
Harald Weltefc1c3e52009-08-07 13:26:28 +02002779 host.enable = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00002780
2781 return CMD_SUCCESS;
2782}
2783
2784ALIAS(config_enable_password,
2785 enable_password_text_cmd,
2786 "enable password LINE",
2787 "Modify enable password parameters\n"
2788 "Assign the privileged level password\n"
2789 "The UNENCRYPTED (cleartext) 'enable' password\n")
2790
2791/* VTY enable password delete. */
2792 DEFUN(no_config_enable_password, no_enable_password_cmd,
2793 "no enable password",
2794 NO_STR
2795 "Modify enable password parameters\n"
2796 "Assign the privileged level password\n")
2797{
2798 if (host.enable)
Harald Welte2477d932009-08-07 00:32:41 +02002799 talloc_free(host.enable);
Harald Welte955049f2009-03-10 12:16:51 +00002800 host.enable = NULL;
2801
2802 if (host.enable_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002803 talloc_free(host.enable_encrypt);
Harald Welte955049f2009-03-10 12:16:51 +00002804 host.enable_encrypt = NULL;
2805
2806 return CMD_SUCCESS;
2807}
2808
Harald Welted6cab812009-05-21 07:31:48 +00002809#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +00002810DEFUN(service_password_encrypt,
2811 service_password_encrypt_cmd,
2812 "service password-encryption",
2813 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2814{
2815 if (host.encrypt)
2816 return CMD_SUCCESS;
2817
2818 host.encrypt = 1;
2819
2820 if (host.password) {
2821 if (host.password_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002822 talloc_free(host.password_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002823 host.password_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(host.password));
Harald Welte955049f2009-03-10 12:16:51 +00002824 }
2825 if (host.enable) {
2826 if (host.enable_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002827 talloc_free(host.enable_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002828 host.enable_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(host.enable));
Harald Welte955049f2009-03-10 12:16:51 +00002829 }
2830
2831 return CMD_SUCCESS;
2832}
2833
2834DEFUN(no_service_password_encrypt,
2835 no_service_password_encrypt_cmd,
2836 "no service password-encryption",
2837 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2838{
2839 if (!host.encrypt)
2840 return CMD_SUCCESS;
2841
2842 host.encrypt = 0;
2843
2844 if (host.password_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002845 talloc_free(host.password_encrypt);
Harald Welte955049f2009-03-10 12:16:51 +00002846 host.password_encrypt = NULL;
2847
2848 if (host.enable_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002849 talloc_free(host.enable_encrypt);
Harald Welte955049f2009-03-10 12:16:51 +00002850 host.enable_encrypt = NULL;
2851
2852 return CMD_SUCCESS;
2853}
Harald Welted6cab812009-05-21 07:31:48 +00002854#endif
Harald Welte955049f2009-03-10 12:16:51 +00002855
2856DEFUN(config_terminal_length, config_terminal_length_cmd,
2857 "terminal length <0-512>",
2858 "Set terminal line parameters\n"
2859 "Set number of lines on a screen\n"
2860 "Number of lines on screen (0 for no pausing)\n")
2861{
2862 int lines;
2863 char *endptr = NULL;
2864
2865 lines = strtol(argv[0], &endptr, 10);
2866 if (lines < 0 || lines > 512 || *endptr != '\0') {
2867 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2868 return CMD_WARNING;
2869 }
2870 vty->lines = lines;
2871
2872 return CMD_SUCCESS;
2873}
2874
2875DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2876 "terminal no length",
2877 "Set terminal line parameters\n"
2878 NO_STR "Set number of lines on a screen\n")
2879{
2880 vty->lines = -1;
2881 return CMD_SUCCESS;
2882}
2883
2884DEFUN(service_terminal_length, service_terminal_length_cmd,
2885 "service terminal-length <0-512>",
2886 "Set up miscellaneous service\n"
2887 "System wide terminal length configuration\n"
2888 "Number of lines of VTY (0 means no line control)\n")
2889{
2890 int lines;
2891 char *endptr = NULL;
2892
2893 lines = strtol(argv[0], &endptr, 10);
2894 if (lines < 0 || lines > 512 || *endptr != '\0') {
2895 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2896 return CMD_WARNING;
2897 }
2898 host.lines = lines;
2899
2900 return CMD_SUCCESS;
2901}
2902
2903DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2904 "no service terminal-length [<0-512>]",
2905 NO_STR
2906 "Set up miscellaneous service\n"
2907 "System wide terminal length configuration\n"
2908 "Number of lines of VTY (0 means no line control)\n")
2909{
2910 host.lines = -1;
2911 return CMD_SUCCESS;
2912}
2913
2914DEFUN_HIDDEN(do_echo,
2915 echo_cmd,
2916 "echo .MESSAGE",
2917 "Echo a message back to the vty\n" "The message to echo\n")
2918{
2919 char *message;
2920
2921 vty_out(vty, "%s%s",
2922 ((message =
2923 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2924 if (message)
Harald Welte2477d932009-08-07 00:32:41 +02002925 talloc_free(message);
Harald Welte955049f2009-03-10 12:16:51 +00002926 return CMD_SUCCESS;
2927}
2928
2929#if 0
2930DEFUN(config_logmsg,
2931 config_logmsg_cmd,
2932 "logmsg " LOG_LEVELS " .MESSAGE",
2933 "Send a message to enabled logging destinations\n"
2934 LOG_LEVEL_DESC "The message to send\n")
2935{
2936 int level;
2937 char *message;
2938
2939 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2940 return CMD_ERR_NO_MATCH;
2941
2942 zlog(NULL, level,
2943 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2944 if (message)
Harald Welte2477d932009-08-07 00:32:41 +02002945 talloc_free(message);
Harald Welte955049f2009-03-10 12:16:51 +00002946 return CMD_SUCCESS;
2947}
2948
2949DEFUN(show_logging,
2950 show_logging_cmd,
2951 "show logging", SHOW_STR "Show current logging configuration\n")
2952{
2953 struct zlog *zl = zlog_default;
2954
2955 vty_out(vty, "Syslog logging: ");
2956 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2957 vty_out(vty, "disabled");
2958 else
2959 vty_out(vty, "level %s, facility %s, ident %s",
2960 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2961 facility_name(zl->facility), zl->ident);
2962 vty_out(vty, "%s", VTY_NEWLINE);
2963
2964 vty_out(vty, "Stdout logging: ");
2965 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2966 vty_out(vty, "disabled");
2967 else
2968 vty_out(vty, "level %s",
2969 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2970 vty_out(vty, "%s", VTY_NEWLINE);
2971
2972 vty_out(vty, "Monitor logging: ");
2973 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2974 vty_out(vty, "disabled");
2975 else
2976 vty_out(vty, "level %s",
2977 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2978 vty_out(vty, "%s", VTY_NEWLINE);
2979
2980 vty_out(vty, "File logging: ");
2981 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2982 vty_out(vty, "disabled");
2983 else
2984 vty_out(vty, "level %s, filename %s",
2985 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2986 zl->filename);
2987 vty_out(vty, "%s", VTY_NEWLINE);
2988
2989 vty_out(vty, "Protocol name: %s%s",
2990 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2991 vty_out(vty, "Record priority: %s%s",
2992 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2993
2994 return CMD_SUCCESS;
2995}
2996
2997DEFUN(config_log_stdout,
2998 config_log_stdout_cmd,
2999 "log stdout", "Logging control\n" "Set stdout logging level\n")
3000{
3001 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3002 return CMD_SUCCESS;
3003}
3004
3005DEFUN(config_log_stdout_level,
3006 config_log_stdout_level_cmd,
3007 "log stdout " LOG_LEVELS,
3008 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3009{
3010 int level;
3011
3012 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3013 return CMD_ERR_NO_MATCH;
3014 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3015 return CMD_SUCCESS;
3016}
3017
3018DEFUN(no_config_log_stdout,
3019 no_config_log_stdout_cmd,
3020 "no log stdout [LEVEL]",
3021 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3022{
3023 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3024 return CMD_SUCCESS;
3025}
3026
3027DEFUN(config_log_monitor,
3028 config_log_monitor_cmd,
3029 "log monitor",
3030 "Logging control\n" "Set terminal line (monitor) logging level\n")
3031{
3032 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3033 return CMD_SUCCESS;
3034}
3035
3036DEFUN(config_log_monitor_level,
3037 config_log_monitor_level_cmd,
3038 "log monitor " LOG_LEVELS,
3039 "Logging control\n"
3040 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3041{
3042 int level;
3043
3044 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3045 return CMD_ERR_NO_MATCH;
3046 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3047 return CMD_SUCCESS;
3048}
3049
3050DEFUN(no_config_log_monitor,
3051 no_config_log_monitor_cmd,
3052 "no log monitor [LEVEL]",
3053 NO_STR
3054 "Logging control\n"
3055 "Disable terminal line (monitor) logging\n" "Logging level\n")
3056{
3057 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3058 return CMD_SUCCESS;
3059}
3060
3061static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3062{
3063 int ret;
3064 char *p = NULL;
3065 const char *fullpath;
3066
3067 /* Path detection. */
3068 if (!IS_DIRECTORY_SEP(*fname)) {
3069 char cwd[MAXPATHLEN + 1];
3070 cwd[MAXPATHLEN] = '\0';
3071
3072 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3073 zlog_err("config_log_file: Unable to alloc mem!");
3074 return CMD_WARNING;
3075 }
3076
Harald Welte2477d932009-08-07 00:32:41 +02003077 if ((p = _talloc_zero(tall_vcmd_ctx,
3078 strlen(cwd) + strlen(fname) + 2),
3079 "set_log_file")
Harald Welte955049f2009-03-10 12:16:51 +00003080 == NULL) {
3081 zlog_err("config_log_file: Unable to alloc mem!");
3082 return CMD_WARNING;
3083 }
3084 sprintf(p, "%s/%s", cwd, fname);
3085 fullpath = p;
3086 } else
3087 fullpath = fname;
3088
3089 ret = zlog_set_file(NULL, fullpath, loglevel);
3090
3091 if (p)
Harald Welte2477d932009-08-07 00:32:41 +02003092 talloc_free(p);
Harald Welte955049f2009-03-10 12:16:51 +00003093
3094 if (!ret) {
3095 vty_out(vty, "can't open logfile %s\n", fname);
3096 return CMD_WARNING;
3097 }
3098
3099 if (host.logfile)
Harald Welte2477d932009-08-07 00:32:41 +02003100 talloc_free(host.logfile);
Harald Welte955049f2009-03-10 12:16:51 +00003101
Harald Weltefc1c3e52009-08-07 13:26:28 +02003102 host.logfile = talloc_strdup(tall_vty_ctx, fname);
Harald Welte955049f2009-03-10 12:16:51 +00003103
3104 return CMD_SUCCESS;
3105}
3106
3107DEFUN(config_log_file,
3108 config_log_file_cmd,
3109 "log file FILENAME",
3110 "Logging control\n" "Logging to file\n" "Logging filename\n")
3111{
3112 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3113}
3114
3115DEFUN(config_log_file_level,
3116 config_log_file_level_cmd,
3117 "log file FILENAME " LOG_LEVELS,
3118 "Logging control\n"
3119 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3120{
3121 int level;
3122
3123 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3124 return CMD_ERR_NO_MATCH;
3125 return set_log_file(vty, argv[0], level);
3126}
3127
3128DEFUN(no_config_log_file,
3129 no_config_log_file_cmd,
3130 "no log file [FILENAME]",
3131 NO_STR
3132 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3133{
3134 zlog_reset_file(NULL);
3135
3136 if (host.logfile)
Harald Welte2477d932009-08-07 00:32:41 +02003137 talloc_free(host.logfile);
Harald Welte955049f2009-03-10 12:16:51 +00003138
3139 host.logfile = NULL;
3140
3141 return CMD_SUCCESS;
3142}
3143
3144ALIAS(no_config_log_file,
3145 no_config_log_file_level_cmd,
3146 "no log file FILENAME LEVEL",
3147 NO_STR
3148 "Logging control\n"
3149 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3150
3151 DEFUN(config_log_syslog,
3152 config_log_syslog_cmd,
3153 "log syslog", "Logging control\n" "Set syslog logging level\n")
3154{
3155 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3156 return CMD_SUCCESS;
3157}
3158
3159DEFUN(config_log_syslog_level,
3160 config_log_syslog_level_cmd,
3161 "log syslog " LOG_LEVELS,
3162 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3163{
3164 int level;
3165
3166 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3167 return CMD_ERR_NO_MATCH;
3168 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3169 return CMD_SUCCESS;
3170}
3171
3172DEFUN_DEPRECATED(config_log_syslog_facility,
3173 config_log_syslog_facility_cmd,
3174 "log syslog facility " LOG_FACILITIES,
3175 "Logging control\n"
3176 "Logging goes to syslog\n"
3177 "(Deprecated) Facility parameter for syslog messages\n"
3178 LOG_FACILITY_DESC)
3179{
3180 int facility;
3181
3182 if ((facility = facility_match(argv[0])) < 0)
3183 return CMD_ERR_NO_MATCH;
3184
3185 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3186 zlog_default->facility = facility;
3187 return CMD_SUCCESS;
3188}
3189
3190DEFUN(no_config_log_syslog,
3191 no_config_log_syslog_cmd,
3192 "no log syslog [LEVEL]",
3193 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3194{
3195 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3196 return CMD_SUCCESS;
3197}
3198
3199ALIAS(no_config_log_syslog,
3200 no_config_log_syslog_facility_cmd,
3201 "no log syslog facility " LOG_FACILITIES,
3202 NO_STR
3203 "Logging control\n"
3204 "Logging goes to syslog\n"
3205 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3206
3207 DEFUN(config_log_facility,
3208 config_log_facility_cmd,
3209 "log facility " LOG_FACILITIES,
3210 "Logging control\n"
3211 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3212{
3213 int facility;
3214
3215 if ((facility = facility_match(argv[0])) < 0)
3216 return CMD_ERR_NO_MATCH;
3217 zlog_default->facility = facility;
3218 return CMD_SUCCESS;
3219}
3220
3221DEFUN(no_config_log_facility,
3222 no_config_log_facility_cmd,
3223 "no log facility [FACILITY]",
3224 NO_STR
3225 "Logging control\n"
3226 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3227{
3228 zlog_default->facility = LOG_DAEMON;
3229 return CMD_SUCCESS;
3230}
3231
3232DEFUN_DEPRECATED(config_log_trap,
3233 config_log_trap_cmd,
3234 "log trap " LOG_LEVELS,
3235 "Logging control\n"
3236 "(Deprecated) Set logging level and default for all destinations\n"
3237 LOG_LEVEL_DESC)
3238{
3239 int new_level;
3240 int i;
3241
3242 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3243 return CMD_ERR_NO_MATCH;
3244
3245 zlog_default->default_lvl = new_level;
3246 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3247 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3248 zlog_default->maxlvl[i] = new_level;
3249 return CMD_SUCCESS;
3250}
3251
3252DEFUN_DEPRECATED(no_config_log_trap,
3253 no_config_log_trap_cmd,
3254 "no log trap [LEVEL]",
3255 NO_STR
3256 "Logging control\n"
3257 "Permit all logging information\n" "Logging level\n")
3258{
3259 zlog_default->default_lvl = LOG_DEBUG;
3260 return CMD_SUCCESS;
3261}
3262
3263DEFUN(config_log_record_priority,
3264 config_log_record_priority_cmd,
3265 "log record-priority",
3266 "Logging control\n"
3267 "Log the priority of the message within the message\n")
3268{
3269 zlog_default->record_priority = 1;
3270 return CMD_SUCCESS;
3271}
3272
3273DEFUN(no_config_log_record_priority,
3274 no_config_log_record_priority_cmd,
3275 "no log record-priority",
3276 NO_STR
3277 "Logging control\n"
3278 "Do not log the priority of the message within the message\n")
3279{
3280 zlog_default->record_priority = 0;
3281 return CMD_SUCCESS;
3282}
3283#endif
3284
3285DEFUN(banner_motd_file,
3286 banner_motd_file_cmd,
3287 "banner motd file [FILE]",
3288 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3289{
3290 if (host.motdfile)
Harald Welte2477d932009-08-07 00:32:41 +02003291 talloc_free(host.motdfile);
Harald Weltefc1c3e52009-08-07 13:26:28 +02003292 host.motdfile = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00003293
3294 return CMD_SUCCESS;
3295}
3296
3297DEFUN(banner_motd_default,
3298 banner_motd_default_cmd,
3299 "banner motd default",
3300 "Set banner string\n" "Strings for motd\n" "Default string\n")
3301{
3302 host.motd = default_motd;
3303 return CMD_SUCCESS;
3304}
3305
3306DEFUN(no_banner_motd,
3307 no_banner_motd_cmd,
3308 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3309{
3310 host.motd = NULL;
3311 if (host.motdfile)
Harald Welte2477d932009-08-07 00:32:41 +02003312 talloc_free(host.motdfile);
Harald Welte955049f2009-03-10 12:16:51 +00003313 host.motdfile = NULL;
3314 return CMD_SUCCESS;
3315}
3316
3317/* Set config filename. Called from vty.c */
3318void host_config_set(char *filename)
3319{
Harald Weltefc1c3e52009-08-07 13:26:28 +02003320 host.config = talloc_strdup(tall_vty_ctx, filename);
Harald Welte955049f2009-03-10 12:16:51 +00003321}
3322
3323void install_default(enum node_type node)
3324{
3325 install_element(node, &config_exit_cmd);
3326 install_element(node, &config_quit_cmd);
3327 install_element(node, &config_end_cmd);
3328 install_element(node, &config_help_cmd);
3329 install_element(node, &config_list_cmd);
3330
Harald Welte955049f2009-03-10 12:16:51 +00003331 install_element(node, &config_write_terminal_cmd);
3332 install_element(node, &config_write_file_cmd);
3333 install_element(node, &config_write_memory_cmd);
3334 install_element(node, &config_write_cmd);
3335 install_element(node, &show_running_config_cmd);
Harald Welte955049f2009-03-10 12:16:51 +00003336}
3337
3338/* Initialize command interface. Install basic nodes and commands. */
3339void cmd_init(int terminal)
3340{
3341 /* Allocate initial top vector of commands. */
3342 cmdvec = vector_init(VECTOR_MIN_SIZE);
3343
3344 /* Default host value settings. */
3345 host.name = NULL;
3346 //host.password = NULL;
3347 host.password = "foo";
3348 host.enable = NULL;
3349 host.logfile = NULL;
3350 host.config = NULL;
3351 host.lines = -1;
3352 host.motd = default_motd;
3353 host.motdfile = NULL;
3354
3355 /* Install top nodes. */
3356 install_node(&view_node, NULL);
3357 install_node(&enable_node, NULL);
3358 install_node(&auth_node, NULL);
3359 install_node(&auth_enable_node, NULL);
3360 install_node(&config_node, config_write_host);
3361
3362 /* Each node's basic commands. */
3363 install_element(VIEW_NODE, &show_version_cmd);
3364 if (terminal) {
3365 install_element(VIEW_NODE, &config_list_cmd);
3366 install_element(VIEW_NODE, &config_exit_cmd);
3367 install_element(VIEW_NODE, &config_quit_cmd);
3368 install_element(VIEW_NODE, &config_help_cmd);
3369 install_element(VIEW_NODE, &config_enable_cmd);
3370 install_element(VIEW_NODE, &config_terminal_length_cmd);
3371 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3372 install_element(VIEW_NODE, &echo_cmd);
3373 }
3374
3375 if (terminal) {
3376 install_default(ENABLE_NODE);
3377 install_element(ENABLE_NODE, &config_disable_cmd);
3378 install_element(ENABLE_NODE, &config_terminal_cmd);
Harald Welte4a3023d2009-08-06 18:50:10 +02003379 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
Harald Welte955049f2009-03-10 12:16:51 +00003380 }
Harald Welte4a3023d2009-08-06 18:50:10 +02003381 install_element (ENABLE_NODE, &show_startup_config_cmd);
Harald Welte955049f2009-03-10 12:16:51 +00003382 install_element(ENABLE_NODE, &show_version_cmd);
3383
3384 if (terminal) {
3385 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3386 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3387 install_element(ENABLE_NODE, &echo_cmd);
3388
3389 install_default(CONFIG_NODE);
3390 }
3391
3392 install_element(CONFIG_NODE, &hostname_cmd);
3393 install_element(CONFIG_NODE, &no_hostname_cmd);
3394
3395 if (terminal) {
3396 install_element(CONFIG_NODE, &password_cmd);
3397 install_element(CONFIG_NODE, &password_text_cmd);
3398 install_element(CONFIG_NODE, &enable_password_cmd);
3399 install_element(CONFIG_NODE, &enable_password_text_cmd);
3400 install_element(CONFIG_NODE, &no_enable_password_cmd);
3401
Harald Welted6cab812009-05-21 07:31:48 +00003402#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +00003403 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3404 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
Harald Welted6cab812009-05-21 07:31:48 +00003405#endif
Harald Welte955049f2009-03-10 12:16:51 +00003406 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3407 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3408 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3409 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3410 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3411
3412 }
3413 srand(time(NULL));
3414}