blob: 44bdf26e4fac467c84aa873add50be56058ec96a [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/*
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 Weltec1a13942009-08-06 18:50:10 +020037#include <sys/stat.h>
Harald Welte59b04682009-06-10 05:40:52 +080038
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
48#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther31206802009-07-29 06:42:36 +020049#include <openbsc/gsm_subscriber.h>
Harald Welte59b04682009-06-10 05:40:52 +080050
51/* Command vector which includes some level of command lists. Normally
52 each daemon maintains each own cmdvec. */
53vector cmdvec;
54
55/* Host information structure. */
56struct host host;
57
58/* Standard command node structures. */
59struct cmd_node auth_node = {
60 AUTH_NODE,
61 "Password: ",
62};
63
64struct cmd_node view_node = {
65 VIEW_NODE,
66 "%s> ",
67};
68
69struct cmd_node auth_enable_node = {
70 AUTH_ENABLE_NODE,
71 "Password: ",
72};
73
74struct cmd_node enable_node = {
75 ENABLE_NODE,
76 "%s# ",
77};
78
79struct cmd_node config_node = {
80 CONFIG_NODE,
81 "%s(config)# ",
82 1
83};
84
85/* Default motd string. */
86const char *default_motd = "\r\n\
87Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\
88" QUAGGA_COPYRIGHT "\r\n\
89\r\n";
90
91#if 0
92static struct facility_map {
93 int facility;
94 const char *name;
95 size_t match;
96} syslog_facilities[] = {
97 {
98 LOG_KERN, "kern", 1}, {
99 LOG_USER, "user", 2}, {
100 LOG_MAIL, "mail", 1}, {
101 LOG_DAEMON, "daemon", 1}, {
102 LOG_AUTH, "auth", 1}, {
103 LOG_SYSLOG, "syslog", 1}, {
104 LOG_LPR, "lpr", 2}, {
105 LOG_NEWS, "news", 1}, {
106 LOG_UUCP, "uucp", 2}, {
107 LOG_CRON, "cron", 1},
108#ifdef LOG_FTP
109 {
110 LOG_FTP, "ftp", 1},
111#endif
112 {
113 LOG_LOCAL0, "local0", 6}, {
114 LOG_LOCAL1, "local1", 6}, {
115 LOG_LOCAL2, "local2", 6}, {
116 LOG_LOCAL3, "local3", 6}, {
117 LOG_LOCAL4, "local4", 6}, {
118 LOG_LOCAL5, "local5", 6}, {
119 LOG_LOCAL6, "local6", 6}, {
120 LOG_LOCAL7, "local7", 6}, {
1210, NULL, 0},};
122
123static const char *facility_name(int facility)
124{
125 struct facility_map *fm;
126
127 for (fm = syslog_facilities; fm->name; fm++)
128 if (fm->facility == facility)
129 return fm->name;
130 return "";
131}
132
133static int facility_match(const char *str)
134{
135 struct facility_map *fm;
136
137 for (fm = syslog_facilities; fm->name; fm++)
138 if (!strncmp(str, fm->name, fm->match))
139 return fm->facility;
140 return -1;
141}
142
143static int level_match(const char *s)
144{
145 int level;
146
147 for (level = 0; zlog_priority[level] != NULL; level++)
148 if (!strncmp(s, zlog_priority[level], 2))
149 return level;
150 return ZLOG_DISABLED;
151}
152#endif
153
154/* This is called from main when a daemon is invoked with -v or --version. */
155void print_version(const char *progname)
156{
157 printf("%s version %s\n", progname, QUAGGA_VERSION);
158 printf("%s\n", QUAGGA_COPYRIGHT);
159}
160
161/* Utility function to concatenate argv argument into a single string
162 with inserting ' ' character between each argument. */
163char *argv_concat(const char **argv, int argc, int shift)
164{
165 int i;
166 size_t len;
167 char *str;
168 char *p;
169
170 len = 0;
171 for (i = shift; i < argc; i++)
172 len += strlen(argv[i]) + 1;
173 if (!len)
174 return NULL;
175 p = str = malloc(len);
176 for (i = shift; i < argc; i++) {
177 size_t arglen;
178 memcpy(p, argv[i], (arglen = strlen(argv[i])));
179 p += arglen;
180 *p++ = ' ';
181 }
182 *(p - 1) = '\0';
183 return str;
184}
185
186/* Install top node of command vector. */
187void install_node(struct cmd_node *node, int (*func) (struct vty *))
188{
189 vector_set_index(cmdvec, node->node, node);
190 node->func = func;
191 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
192}
193
194/* Compare two command's string. Used in sort_node (). */
195static int cmp_node(const void *p, const void *q)
196{
197 struct cmd_element *a = *(struct cmd_element **)p;
198 struct cmd_element *b = *(struct cmd_element **)q;
199
200 return strcmp(a->string, b->string);
201}
202
203static int cmp_desc(const void *p, const void *q)
204{
205 struct desc *a = *(struct desc **)p;
206 struct desc *b = *(struct desc **)q;
207
208 return strcmp(a->cmd, b->cmd);
209}
210
211/* Sort each node's command element according to command string. */
212void sort_node()
213{
214 unsigned int i, j;
215 struct cmd_node *cnode;
216 vector descvec;
217 struct cmd_element *cmd_element;
218
219 for (i = 0; i < vector_active(cmdvec); i++)
220 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
221 vector cmd_vector = cnode->cmd_vector;
222 qsort(cmd_vector->index, vector_active(cmd_vector),
223 sizeof(void *), cmp_node);
224
225 for (j = 0; j < vector_active(cmd_vector); j++)
226 if ((cmd_element =
227 vector_slot(cmd_vector, j)) != NULL
228 && vector_active(cmd_element->strvec)) {
229 descvec =
230 vector_slot(cmd_element->strvec,
231 vector_active
232 (cmd_element->strvec) -
233 1);
234 qsort(descvec->index,
235 vector_active(descvec),
236 sizeof(void *), cmp_desc);
237 }
238 }
239}
240
241/* Breaking up string into each command piece. I assume given
242 character is separated by a space character. Return value is a
243 vector which includes char ** data element. */
244vector cmd_make_strvec(const char *string)
245{
246 const char *cp, *start;
247 char *token;
248 int strlen;
249 vector strvec;
250
251 if (string == NULL)
252 return NULL;
253
254 cp = string;
255
256 /* Skip white spaces. */
257 while (isspace((int)*cp) && *cp != '\0')
258 cp++;
259
260 /* Return if there is only white spaces */
261 if (*cp == '\0')
262 return NULL;
263
264 if (*cp == '!' || *cp == '#')
265 return NULL;
266
267 /* Prepare return vector. */
268 strvec = vector_init(VECTOR_MIN_SIZE);
269
270 /* Copy each command piece and set into vector. */
271 while (1) {
272 start = cp;
273 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
274 *cp != '\0')
275 cp++;
276 strlen = cp - start;
277 token = malloc(strlen + 1);
278 memcpy(token, start, strlen);
279 *(token + strlen) = '\0';
280 vector_set(strvec, token);
281
282 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
283 *cp != '\0')
284 cp++;
285
286 if (*cp == '\0')
287 return strvec;
288 }
289}
290
291/* Free allocated string vector. */
292void cmd_free_strvec(vector v)
293{
294 unsigned int i;
295 char *cp;
296
297 if (!v)
298 return;
299
300 for (i = 0; i < vector_active(v); i++)
301 if ((cp = vector_slot(v, i)) != NULL)
302 free(cp);
303
304 vector_free(v);
305}
306
307/* Fetch next description. Used in cmd_make_descvec(). */
308static char *cmd_desc_str(const char **string)
309{
310 const char *cp, *start;
311 char *token;
312 int strlen;
313
314 cp = *string;
315
316 if (cp == NULL)
317 return NULL;
318
319 /* Skip white spaces. */
320 while (isspace((int)*cp) && *cp != '\0')
321 cp++;
322
323 /* Return if there is only white spaces */
324 if (*cp == '\0')
325 return NULL;
326
327 start = cp;
328
329 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
330 cp++;
331
332 strlen = cp - start;
333 token = malloc(strlen + 1);
334 memcpy(token, start, strlen);
335 *(token + strlen) = '\0';
336
337 *string = cp;
338
339 return token;
340}
341
342/* New string vector. */
343static vector cmd_make_descvec(const char *string, const char *descstr)
344{
345 int multiple = 0;
346 const char *sp;
347 char *token;
348 int len;
349 const char *cp;
350 const char *dp;
351 vector allvec;
352 vector strvec = NULL;
353 struct desc *desc;
354
355 cp = string;
356 dp = descstr;
357
358 if (cp == NULL)
359 return NULL;
360
361 allvec = vector_init(VECTOR_MIN_SIZE);
362
363 while (1) {
364 while (isspace((int)*cp) && *cp != '\0')
365 cp++;
366
367 if (*cp == '(') {
368 multiple = 1;
369 cp++;
370 }
371 if (*cp == ')') {
372 multiple = 0;
373 cp++;
374 }
375 if (*cp == '|') {
376 if (!multiple) {
377 fprintf(stderr, "Command parse error!: %s\n",
378 string);
379 exit(1);
380 }
381 cp++;
382 }
383
384 while (isspace((int)*cp) && *cp != '\0')
385 cp++;
386
387 if (*cp == '(') {
388 multiple = 1;
389 cp++;
390 }
391
392 if (*cp == '\0')
393 return allvec;
394
395 sp = cp;
396
397 while (!
398 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
399 || *cp == ')' || *cp == '|') && *cp != '\0')
400 cp++;
401
402 len = cp - sp;
403
404 token = malloc(len + 1);
405 memcpy(token, sp, len);
406 *(token + len) = '\0';
407
408 desc = calloc(1, sizeof(struct desc));
409 desc->cmd = token;
410 desc->str = cmd_desc_str(&dp);
411
412 if (multiple) {
413 if (multiple == 1) {
414 strvec = vector_init(VECTOR_MIN_SIZE);
415 vector_set(allvec, strvec);
416 }
417 multiple++;
418 } else {
419 strvec = vector_init(VECTOR_MIN_SIZE);
420 vector_set(allvec, strvec);
421 }
422 vector_set(strvec, desc);
423 }
424}
425
426/* Count mandantory string vector size. This is to determine inputed
427 command has enough command length. */
428static int cmd_cmdsize(vector strvec)
429{
430 unsigned int i;
431 int size = 0;
432 vector descvec;
433 struct desc *desc;
434
435 for (i = 0; i < vector_active(strvec); i++)
436 if ((descvec = vector_slot(strvec, i)) != NULL) {
437 if ((vector_active(descvec)) == 1
438 && (desc = vector_slot(descvec, 0)) != NULL) {
439 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
440 return size;
441 else
442 size++;
443 } else
444 size++;
445 }
446 return size;
447}
448
449/* Return prompt character of specified node. */
450const char *cmd_prompt(enum node_type node)
451{
452 struct cmd_node *cnode;
453
454 cnode = vector_slot(cmdvec, node);
455 return cnode->prompt;
456}
457
458/* Install a command into a node. */
459void install_element(enum node_type ntype, struct cmd_element *cmd)
460{
461 struct cmd_node *cnode;
462
463 cnode = vector_slot(cmdvec, ntype);
464
465 if (cnode == NULL) {
466 fprintf(stderr,
467 "Command node %d doesn't exist, please check it\n",
468 ntype);
469 exit(1);
470 }
471
472 vector_set(cnode->cmd_vector, cmd);
473
474 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
475 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
476}
477
Harald Welteec6c5ca2009-08-06 18:51:23 +0200478#ifdef VTY_CRYPT_PW
Harald Welte59b04682009-06-10 05:40:52 +0800479static unsigned char itoa64[] =
480 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
481
482static void to64(char *s, long v, int n)
483{
484 while (--n >= 0) {
485 *s++ = itoa64[v & 0x3f];
486 v >>= 6;
487 }
488}
489
490static char *zencrypt(const char *passwd)
491{
492 char salt[6];
493 struct timeval tv;
494 char *crypt(const char *, const char *);
495
496 gettimeofday(&tv, 0);
497
498 to64(&salt[0], random(), 3);
499 to64(&salt[3], tv.tv_usec, 3);
500 salt[5] = '\0';
501
502 return crypt(passwd, salt);
503}
Harald Welteec6c5ca2009-08-06 18:51:23 +0200504#endif
Harald Welte59b04682009-06-10 05:40:52 +0800505
506/* This function write configuration of this host. */
507static int config_write_host(struct vty *vty)
508{
509#if 0
510 if (host.name)
511 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
512
513 if (host.encrypt) {
514 if (host.password_encrypt)
515 vty_out(vty, "password 8 %s%s", host.password_encrypt,
516 VTY_NEWLINE);
517 if (host.enable_encrypt)
518 vty_out(vty, "enable password 8 %s%s",
519 host.enable_encrypt, VTY_NEWLINE);
520 } else {
521 if (host.password)
522 vty_out(vty, "password %s%s", host.password,
523 VTY_NEWLINE);
524 if (host.enable)
525 vty_out(vty, "enable password %s%s", host.enable,
526 VTY_NEWLINE);
527 }
528
529 if (zlog_default->default_lvl != LOG_DEBUG) {
530 vty_out(vty, "! N.B. The 'log trap' command is deprecated.%s",
531 VTY_NEWLINE);
532 vty_out(vty, "log trap %s%s",
533 zlog_priority[zlog_default->default_lvl], VTY_NEWLINE);
534 }
535
536 if (host.logfile
537 && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED)) {
538 vty_out(vty, "log file %s", host.logfile);
539 if (zlog_default->maxlvl[ZLOG_DEST_FILE] !=
540 zlog_default->default_lvl)
541 vty_out(vty, " %s",
542 zlog_priority[zlog_default->
543 maxlvl[ZLOG_DEST_FILE]]);
544 vty_out(vty, "%s", VTY_NEWLINE);
545 }
546
547 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED) {
548 vty_out(vty, "log stdout");
549 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] !=
550 zlog_default->default_lvl)
551 vty_out(vty, " %s",
552 zlog_priority[zlog_default->
553 maxlvl[ZLOG_DEST_STDOUT]]);
554 vty_out(vty, "%s", VTY_NEWLINE);
555 }
556
557 if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
558 vty_out(vty, "no log monitor%s", VTY_NEWLINE);
559 else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] !=
560 zlog_default->default_lvl)
561 vty_out(vty, "log monitor %s%s",
562 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],
563 VTY_NEWLINE);
564
565 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED) {
566 vty_out(vty, "log syslog");
567 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] !=
568 zlog_default->default_lvl)
569 vty_out(vty, " %s",
570 zlog_priority[zlog_default->
571 maxlvl[ZLOG_DEST_SYSLOG]]);
572 vty_out(vty, "%s", VTY_NEWLINE);
573 }
574
575 if (zlog_default->facility != LOG_DAEMON)
576 vty_out(vty, "log facility %s%s",
577 facility_name(zlog_default->facility), VTY_NEWLINE);
578
579 if (zlog_default->record_priority == 1)
580 vty_out(vty, "log record-priority%s", VTY_NEWLINE);
581
582 if (host.advanced)
583 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
584
585 if (host.encrypt)
586 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
587
588 if (host.lines >= 0)
589 vty_out(vty, "service terminal-length %d%s", host.lines,
590 VTY_NEWLINE);
591
592 if (host.motdfile)
593 vty_out(vty, "banner motd file %s%s", host.motdfile,
594 VTY_NEWLINE);
595 else if (!host.motd)
596 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
597
598#endif
599 return 1;
600}
601
602/* Utility function for getting command vector. */
603static vector cmd_node_vector(vector v, enum node_type ntype)
604{
605 struct cmd_node *cnode = vector_slot(v, ntype);
606 return cnode->cmd_vector;
607}
608
609#if 0
610/* Filter command vector by symbol. This function is not actually used;
611 * should it be deleted? */
612static int cmd_filter_by_symbol(char *command, char *symbol)
613{
614 int i, lim;
615
616 if (strcmp(symbol, "IPV4_ADDRESS") == 0) {
617 i = 0;
618 lim = strlen(command);
619 while (i < lim) {
620 if (!
621 (isdigit((int)command[i]) || command[i] == '.'
622 || command[i] == '/'))
623 return 1;
624 i++;
625 }
626 return 0;
627 }
628 if (strcmp(symbol, "STRING") == 0) {
629 i = 0;
630 lim = strlen(command);
631 while (i < lim) {
632 if (!
633 (isalpha((int)command[i]) || command[i] == '_'
634 || command[i] == '-'))
635 return 1;
636 i++;
637 }
638 return 0;
639 }
640 if (strcmp(symbol, "IFNAME") == 0) {
641 i = 0;
642 lim = strlen(command);
643 while (i < lim) {
644 if (!isalnum((int)command[i]))
645 return 1;
646 i++;
647 }
648 return 0;
649 }
650 return 0;
651}
652#endif
653
654/* Completion match types. */
655enum match_type {
656 no_match,
657 extend_match,
658 ipv4_prefix_match,
659 ipv4_match,
660 ipv6_prefix_match,
661 ipv6_match,
662 range_match,
663 vararg_match,
664 partly_match,
665 exact_match
666};
667
668static enum match_type cmd_ipv4_match(const char *str)
669{
670 const char *sp;
671 int dots = 0, nums = 0;
672 char buf[4];
673
674 if (str == NULL)
675 return partly_match;
676
677 for (;;) {
678 memset(buf, 0, sizeof(buf));
679 sp = str;
680 while (*str != '\0') {
681 if (*str == '.') {
682 if (dots >= 3)
683 return no_match;
684
685 if (*(str + 1) == '.')
686 return no_match;
687
688 if (*(str + 1) == '\0')
689 return partly_match;
690
691 dots++;
692 break;
693 }
694 if (!isdigit((int)*str))
695 return no_match;
696
697 str++;
698 }
699
700 if (str - sp > 3)
701 return no_match;
702
703 strncpy(buf, sp, str - sp);
704 if (atoi(buf) > 255)
705 return no_match;
706
707 nums++;
708
709 if (*str == '\0')
710 break;
711
712 str++;
713 }
714
715 if (nums < 4)
716 return partly_match;
717
718 return exact_match;
719}
720
721static enum match_type cmd_ipv4_prefix_match(const char *str)
722{
723 const char *sp;
724 int dots = 0;
725 char buf[4];
726
727 if (str == NULL)
728 return partly_match;
729
730 for (;;) {
731 memset(buf, 0, sizeof(buf));
732 sp = str;
733 while (*str != '\0' && *str != '/') {
734 if (*str == '.') {
735 if (dots == 3)
736 return no_match;
737
738 if (*(str + 1) == '.' || *(str + 1) == '/')
739 return no_match;
740
741 if (*(str + 1) == '\0')
742 return partly_match;
743
744 dots++;
745 break;
746 }
747
748 if (!isdigit((int)*str))
749 return no_match;
750
751 str++;
752 }
753
754 if (str - sp > 3)
755 return no_match;
756
757 strncpy(buf, sp, str - sp);
758 if (atoi(buf) > 255)
759 return no_match;
760
761 if (dots == 3) {
762 if (*str == '/') {
763 if (*(str + 1) == '\0')
764 return partly_match;
765
766 str++;
767 break;
768 } else if (*str == '\0')
769 return partly_match;
770 }
771
772 if (*str == '\0')
773 return partly_match;
774
775 str++;
776 }
777
778 sp = str;
779 while (*str != '\0') {
780 if (!isdigit((int)*str))
781 return no_match;
782
783 str++;
784 }
785
786 if (atoi(sp) > 32)
787 return no_match;
788
789 return exact_match;
790}
791
792#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
793#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
794#define STATE_START 1
795#define STATE_COLON 2
796#define STATE_DOUBLE 3
797#define STATE_ADDR 4
798#define STATE_DOT 5
799#define STATE_SLASH 6
800#define STATE_MASK 7
801
802#ifdef HAVE_IPV6
803
804static enum match_type cmd_ipv6_match(const char *str)
805{
806 int state = STATE_START;
807 int colons = 0, nums = 0, double_colon = 0;
808 const char *sp = NULL;
809 struct sockaddr_in6 sin6_dummy;
810 int ret;
811
812 if (str == NULL)
813 return partly_match;
814
815 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
816 return no_match;
817
818 /* use inet_pton that has a better support,
819 * for example inet_pton can support the automatic addresses:
820 * ::1.2.3.4
821 */
822 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
823
824 if (ret == 1)
825 return exact_match;
826
827 while (*str != '\0') {
828 switch (state) {
829 case STATE_START:
830 if (*str == ':') {
831 if (*(str + 1) != ':' && *(str + 1) != '\0')
832 return no_match;
833 colons--;
834 state = STATE_COLON;
835 } else {
836 sp = str;
837 state = STATE_ADDR;
838 }
839
840 continue;
841 case STATE_COLON:
842 colons++;
843 if (*(str + 1) == ':')
844 state = STATE_DOUBLE;
845 else {
846 sp = str + 1;
847 state = STATE_ADDR;
848 }
849 break;
850 case STATE_DOUBLE:
851 if (double_colon)
852 return no_match;
853
854 if (*(str + 1) == ':')
855 return no_match;
856 else {
857 if (*(str + 1) != '\0')
858 colons++;
859 sp = str + 1;
860 state = STATE_ADDR;
861 }
862
863 double_colon++;
864 nums++;
865 break;
866 case STATE_ADDR:
867 if (*(str + 1) == ':' || *(str + 1) == '\0') {
868 if (str - sp > 3)
869 return no_match;
870
871 nums++;
872 state = STATE_COLON;
873 }
874 if (*(str + 1) == '.')
875 state = STATE_DOT;
876 break;
877 case STATE_DOT:
878 state = STATE_ADDR;
879 break;
880 default:
881 break;
882 }
883
884 if (nums > 8)
885 return no_match;
886
887 if (colons > 7)
888 return no_match;
889
890 str++;
891 }
892
893#if 0
894 if (nums < 11)
895 return partly_match;
896#endif /* 0 */
897
898 return exact_match;
899}
900
901static enum match_type cmd_ipv6_prefix_match(const char *str)
902{
903 int state = STATE_START;
904 int colons = 0, nums = 0, double_colon = 0;
905 int mask;
906 const char *sp = NULL;
907 char *endptr = NULL;
908
909 if (str == NULL)
910 return partly_match;
911
912 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
913 return no_match;
914
915 while (*str != '\0' && state != STATE_MASK) {
916 switch (state) {
917 case STATE_START:
918 if (*str == ':') {
919 if (*(str + 1) != ':' && *(str + 1) != '\0')
920 return no_match;
921 colons--;
922 state = STATE_COLON;
923 } else {
924 sp = str;
925 state = STATE_ADDR;
926 }
927
928 continue;
929 case STATE_COLON:
930 colons++;
931 if (*(str + 1) == '/')
932 return no_match;
933 else if (*(str + 1) == ':')
934 state = STATE_DOUBLE;
935 else {
936 sp = str + 1;
937 state = STATE_ADDR;
938 }
939 break;
940 case STATE_DOUBLE:
941 if (double_colon)
942 return no_match;
943
944 if (*(str + 1) == ':')
945 return no_match;
946 else {
947 if (*(str + 1) != '\0' && *(str + 1) != '/')
948 colons++;
949 sp = str + 1;
950
951 if (*(str + 1) == '/')
952 state = STATE_SLASH;
953 else
954 state = STATE_ADDR;
955 }
956
957 double_colon++;
958 nums += 1;
959 break;
960 case STATE_ADDR:
961 if (*(str + 1) == ':' || *(str + 1) == '.'
962 || *(str + 1) == '\0' || *(str + 1) == '/') {
963 if (str - sp > 3)
964 return no_match;
965
966 for (; sp <= str; sp++)
967 if (*sp == '/')
968 return no_match;
969
970 nums++;
971
972 if (*(str + 1) == ':')
973 state = STATE_COLON;
974 else if (*(str + 1) == '.')
975 state = STATE_DOT;
976 else if (*(str + 1) == '/')
977 state = STATE_SLASH;
978 }
979 break;
980 case STATE_DOT:
981 state = STATE_ADDR;
982 break;
983 case STATE_SLASH:
984 if (*(str + 1) == '\0')
985 return partly_match;
986
987 state = STATE_MASK;
988 break;
989 default:
990 break;
991 }
992
993 if (nums > 11)
994 return no_match;
995
996 if (colons > 7)
997 return no_match;
998
999 str++;
1000 }
1001
1002 if (state < STATE_MASK)
1003 return partly_match;
1004
1005 mask = strtol(str, &endptr, 10);
1006 if (*endptr != '\0')
1007 return no_match;
1008
1009 if (mask < 0 || mask > 128)
1010 return no_match;
1011
1012/* I don't know why mask < 13 makes command match partly.
1013 Forgive me to make this comments. I Want to set static default route
1014 because of lack of function to originate default in ospf6d; sorry
1015 yasu
1016 if (mask < 13)
1017 return partly_match;
1018*/
1019
1020 return exact_match;
1021}
1022
1023#endif /* HAVE_IPV6 */
1024
1025#define DECIMAL_STRLEN_MAX 10
1026
1027static int cmd_range_match(const char *range, const char *str)
1028{
1029 char *p;
1030 char buf[DECIMAL_STRLEN_MAX + 1];
1031 char *endptr = NULL;
1032 unsigned long min, max, val;
1033
1034 if (str == NULL)
1035 return 1;
1036
1037 val = strtoul(str, &endptr, 10);
1038 if (*endptr != '\0')
1039 return 0;
1040
1041 range++;
1042 p = strchr(range, '-');
1043 if (p == NULL)
1044 return 0;
1045 if (p - range > DECIMAL_STRLEN_MAX)
1046 return 0;
1047 strncpy(buf, range, p - range);
1048 buf[p - range] = '\0';
1049 min = strtoul(buf, &endptr, 10);
1050 if (*endptr != '\0')
1051 return 0;
1052
1053 range = p + 1;
1054 p = strchr(range, '>');
1055 if (p == NULL)
1056 return 0;
1057 if (p - range > DECIMAL_STRLEN_MAX)
1058 return 0;
1059 strncpy(buf, range, p - range);
1060 buf[p - range] = '\0';
1061 max = strtoul(buf, &endptr, 10);
1062 if (*endptr != '\0')
1063 return 0;
1064
1065 if (val < min || val > max)
1066 return 0;
1067
1068 return 1;
1069}
1070
1071/* Make completion match and return match type flag. */
1072static enum match_type
1073cmd_filter_by_completion(char *command, vector v, unsigned int index)
1074{
1075 unsigned int i;
1076 const char *str;
1077 struct cmd_element *cmd_element;
1078 enum match_type match_type;
1079 vector descvec;
1080 struct desc *desc;
1081
1082 match_type = no_match;
1083
1084 /* If command and cmd_element string does not match set NULL to vector */
1085 for (i = 0; i < vector_active(v); i++)
1086 if ((cmd_element = vector_slot(v, i)) != NULL) {
1087 if (index >= vector_active(cmd_element->strvec))
1088 vector_slot(v, i) = NULL;
1089 else {
1090 unsigned int j;
1091 int matched = 0;
1092
1093 descvec =
1094 vector_slot(cmd_element->strvec, index);
1095
1096 for (j = 0; j < vector_active(descvec); j++)
1097 if ((desc = vector_slot(descvec, j))) {
1098 str = desc->cmd;
1099
1100 if (CMD_VARARG(str)) {
1101 if (match_type <
1102 vararg_match)
1103 match_type =
1104 vararg_match;
1105 matched++;
1106 } else if (CMD_RANGE(str)) {
1107 if (cmd_range_match
1108 (str, command)) {
1109 if (match_type <
1110 range_match)
1111 match_type
1112 =
1113 range_match;
1114
1115 matched++;
1116 }
1117 }
1118#ifdef HAVE_IPV6
1119 else if (CMD_IPV6(str)) {
1120 if (cmd_ipv6_match
1121 (command)) {
1122 if (match_type <
1123 ipv6_match)
1124 match_type
1125 =
1126 ipv6_match;
1127
1128 matched++;
1129 }
1130 } else if (CMD_IPV6_PREFIX(str)) {
1131 if (cmd_ipv6_prefix_match(command)) {
1132 if (match_type <
1133 ipv6_prefix_match)
1134 match_type
1135 =
1136 ipv6_prefix_match;
1137
1138 matched++;
1139 }
1140 }
1141#endif /* HAVE_IPV6 */
1142 else if (CMD_IPV4(str)) {
1143 if (cmd_ipv4_match
1144 (command)) {
1145 if (match_type <
1146 ipv4_match)
1147 match_type
1148 =
1149 ipv4_match;
1150
1151 matched++;
1152 }
1153 } else if (CMD_IPV4_PREFIX(str)) {
1154 if (cmd_ipv4_prefix_match(command)) {
1155 if (match_type <
1156 ipv4_prefix_match)
1157 match_type
1158 =
1159 ipv4_prefix_match;
1160 matched++;
1161 }
1162 } else
1163 /* Check is this point's argument optional ? */
1164 if (CMD_OPTION(str)
1165 ||
1166 CMD_VARIABLE(str)) {
1167 if (match_type <
1168 extend_match)
1169 match_type =
1170 extend_match;
1171 matched++;
1172 } else
1173 if (strncmp
1174 (command, str,
1175 strlen(command)) ==
1176 0) {
1177 if (strcmp(command, str)
1178 == 0)
1179 match_type =
1180 exact_match;
1181 else {
1182 if (match_type <
1183 partly_match)
1184 match_type
1185 =
1186 partly_match;
1187 }
1188 matched++;
1189 }
1190 }
1191 if (!matched)
1192 vector_slot(v, i) = NULL;
1193 }
1194 }
1195 return match_type;
1196}
1197
1198/* Filter vector by command character with index. */
1199static enum match_type
1200cmd_filter_by_string(char *command, vector v, unsigned int index)
1201{
1202 unsigned int i;
1203 const char *str;
1204 struct cmd_element *cmd_element;
1205 enum match_type match_type;
1206 vector descvec;
1207 struct desc *desc;
1208
1209 match_type = no_match;
1210
1211 /* If command and cmd_element string does not match set NULL to vector */
1212 for (i = 0; i < vector_active(v); i++)
1213 if ((cmd_element = vector_slot(v, i)) != NULL) {
1214 /* If given index is bigger than max string vector of command,
1215 set NULL */
1216 if (index >= vector_active(cmd_element->strvec))
1217 vector_slot(v, i) = NULL;
1218 else {
1219 unsigned int j;
1220 int matched = 0;
1221
1222 descvec =
1223 vector_slot(cmd_element->strvec, index);
1224
1225 for (j = 0; j < vector_active(descvec); j++)
1226 if ((desc = vector_slot(descvec, j))) {
1227 str = desc->cmd;
1228
1229 if (CMD_VARARG(str)) {
1230 if (match_type <
1231 vararg_match)
1232 match_type =
1233 vararg_match;
1234 matched++;
1235 } else if (CMD_RANGE(str)) {
1236 if (cmd_range_match
1237 (str, command)) {
1238 if (match_type <
1239 range_match)
1240 match_type
1241 =
1242 range_match;
1243 matched++;
1244 }
1245 }
1246#ifdef HAVE_IPV6
1247 else if (CMD_IPV6(str)) {
1248 if (cmd_ipv6_match
1249 (command) ==
1250 exact_match) {
1251 if (match_type <
1252 ipv6_match)
1253 match_type
1254 =
1255 ipv6_match;
1256 matched++;
1257 }
1258 } else if (CMD_IPV6_PREFIX(str)) {
1259 if (cmd_ipv6_prefix_match(command) == exact_match) {
1260 if (match_type <
1261 ipv6_prefix_match)
1262 match_type
1263 =
1264 ipv6_prefix_match;
1265 matched++;
1266 }
1267 }
1268#endif /* HAVE_IPV6 */
1269 else if (CMD_IPV4(str)) {
1270 if (cmd_ipv4_match
1271 (command) ==
1272 exact_match) {
1273 if (match_type <
1274 ipv4_match)
1275 match_type
1276 =
1277 ipv4_match;
1278 matched++;
1279 }
1280 } else if (CMD_IPV4_PREFIX(str)) {
1281 if (cmd_ipv4_prefix_match(command) == exact_match) {
1282 if (match_type <
1283 ipv4_prefix_match)
1284 match_type
1285 =
1286 ipv4_prefix_match;
1287 matched++;
1288 }
1289 } else if (CMD_OPTION(str)
1290 || CMD_VARIABLE(str))
1291 {
1292 if (match_type <
1293 extend_match)
1294 match_type =
1295 extend_match;
1296 matched++;
1297 } else {
1298 if (strcmp(command, str)
1299 == 0) {
1300 match_type =
1301 exact_match;
1302 matched++;
1303 }
1304 }
1305 }
1306 if (!matched)
1307 vector_slot(v, i) = NULL;
1308 }
1309 }
1310 return match_type;
1311}
1312
1313/* Check ambiguous match */
1314static int
1315is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1316{
1317 unsigned int i;
1318 unsigned int j;
1319 const char *str = NULL;
1320 struct cmd_element *cmd_element;
1321 const char *matched = NULL;
1322 vector descvec;
1323 struct desc *desc;
1324
1325 for (i = 0; i < vector_active(v); i++)
1326 if ((cmd_element = vector_slot(v, i)) != NULL) {
1327 int match = 0;
1328
1329 descvec = vector_slot(cmd_element->strvec, index);
1330
1331 for (j = 0; j < vector_active(descvec); j++)
1332 if ((desc = vector_slot(descvec, j))) {
1333 enum match_type ret;
1334
1335 str = desc->cmd;
1336
1337 switch (type) {
1338 case exact_match:
1339 if (!
1340 (CMD_OPTION(str)
1341 || CMD_VARIABLE(str))
1342&& strcmp(command, str) == 0)
1343 match++;
1344 break;
1345 case partly_match:
1346 if (!
1347 (CMD_OPTION(str)
1348 || CMD_VARIABLE(str))
1349&& strncmp(command, str, strlen(command)) == 0) {
1350 if (matched
1351 && strcmp(matched,
1352 str) != 0)
1353 return 1; /* There is ambiguous match. */
1354 else
1355 matched = str;
1356 match++;
1357 }
1358 break;
1359 case range_match:
1360 if (cmd_range_match
1361 (str, command)) {
1362 if (matched
1363 && strcmp(matched,
1364 str) != 0)
1365 return 1;
1366 else
1367 matched = str;
1368 match++;
1369 }
1370 break;
1371#ifdef HAVE_IPV6
1372 case ipv6_match:
1373 if (CMD_IPV6(str))
1374 match++;
1375 break;
1376 case ipv6_prefix_match:
1377 if ((ret =
1378 cmd_ipv6_prefix_match
1379 (command)) != no_match) {
1380 if (ret == partly_match)
1381 return 2; /* There is incomplete match. */
1382
1383 match++;
1384 }
1385 break;
1386#endif /* HAVE_IPV6 */
1387 case ipv4_match:
1388 if (CMD_IPV4(str))
1389 match++;
1390 break;
1391 case ipv4_prefix_match:
1392 if ((ret =
1393 cmd_ipv4_prefix_match
1394 (command)) != no_match) {
1395 if (ret == partly_match)
1396 return 2; /* There is incomplete match. */
1397
1398 match++;
1399 }
1400 break;
1401 case extend_match:
1402 if (CMD_OPTION(str)
1403 || CMD_VARIABLE(str))
1404 match++;
1405 break;
1406 case no_match:
1407 default:
1408 break;
1409 }
1410 }
1411 if (!match)
1412 vector_slot(v, i) = NULL;
1413 }
1414 return 0;
1415}
1416
1417/* If src matches dst return dst string, otherwise return NULL */
1418static const char *cmd_entry_function(const char *src, const char *dst)
1419{
1420 /* Skip variable arguments. */
1421 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1422 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1423 return NULL;
1424
1425 /* In case of 'command \t', given src is NULL string. */
1426 if (src == NULL)
1427 return dst;
1428
1429 /* Matched with input string. */
1430 if (strncmp(src, dst, strlen(src)) == 0)
1431 return dst;
1432
1433 return NULL;
1434}
1435
1436/* If src matches dst return dst string, otherwise return NULL */
1437/* This version will return the dst string always if it is
1438 CMD_VARIABLE for '?' key processing */
1439static const char *cmd_entry_function_desc(const char *src, const char *dst)
1440{
1441 if (CMD_VARARG(dst))
1442 return dst;
1443
1444 if (CMD_RANGE(dst)) {
1445 if (cmd_range_match(dst, src))
1446 return dst;
1447 else
1448 return NULL;
1449 }
1450#ifdef HAVE_IPV6
1451 if (CMD_IPV6(dst)) {
1452 if (cmd_ipv6_match(src))
1453 return dst;
1454 else
1455 return NULL;
1456 }
1457
1458 if (CMD_IPV6_PREFIX(dst)) {
1459 if (cmd_ipv6_prefix_match(src))
1460 return dst;
1461 else
1462 return NULL;
1463 }
1464#endif /* HAVE_IPV6 */
1465
1466 if (CMD_IPV4(dst)) {
1467 if (cmd_ipv4_match(src))
1468 return dst;
1469 else
1470 return NULL;
1471 }
1472
1473 if (CMD_IPV4_PREFIX(dst)) {
1474 if (cmd_ipv4_prefix_match(src))
1475 return dst;
1476 else
1477 return NULL;
1478 }
1479
1480 /* Optional or variable commands always match on '?' */
1481 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1482 return dst;
1483
1484 /* In case of 'command \t', given src is NULL string. */
1485 if (src == NULL)
1486 return dst;
1487
1488 if (strncmp(src, dst, strlen(src)) == 0)
1489 return dst;
1490 else
1491 return NULL;
1492}
1493
1494/* Check same string element existence. If it isn't there return
1495 1. */
1496static int cmd_unique_string(vector v, const char *str)
1497{
1498 unsigned int i;
1499 char *match;
1500
1501 for (i = 0; i < vector_active(v); i++)
1502 if ((match = vector_slot(v, i)) != NULL)
1503 if (strcmp(match, str) == 0)
1504 return 0;
1505 return 1;
1506}
1507
1508/* Compare string to description vector. If there is same string
1509 return 1 else return 0. */
1510static int desc_unique_string(vector v, const char *str)
1511{
1512 unsigned int i;
1513 struct desc *desc;
1514
1515 for (i = 0; i < vector_active(v); i++)
1516 if ((desc = vector_slot(v, i)) != NULL)
1517 if (strcmp(desc->cmd, str) == 0)
1518 return 1;
1519 return 0;
1520}
1521
1522static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1523{
1524 if (first_word != NULL &&
1525 node != AUTH_NODE &&
1526 node != VIEW_NODE &&
1527 node != AUTH_ENABLE_NODE &&
1528 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1529 return 1;
1530 return 0;
1531}
1532
1533/* '?' describe command support. */
1534static vector
1535cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1536{
1537 unsigned int i;
1538 vector cmd_vector;
1539#define INIT_MATCHVEC_SIZE 10
1540 vector matchvec;
1541 struct cmd_element *cmd_element;
1542 unsigned int index;
1543 int ret;
1544 enum match_type match;
1545 char *command;
1546 static struct desc desc_cr = { "<cr>", "" };
1547
1548 /* Set index. */
1549 if (vector_active(vline) == 0) {
1550 *status = CMD_ERR_NO_MATCH;
1551 return NULL;
1552 } else
1553 index = vector_active(vline) - 1;
1554
1555 /* Make copy vector of current node's command vector. */
1556 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1557
1558 /* Prepare match vector */
1559 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1560
1561 /* Filter commands. */
1562 /* Only words precedes current word will be checked in this loop. */
1563 for (i = 0; i < index; i++)
1564 if ((command = vector_slot(vline, i))) {
1565 match =
1566 cmd_filter_by_completion(command, cmd_vector, i);
1567
1568 if (match == vararg_match) {
1569 struct cmd_element *cmd_element;
1570 vector descvec;
1571 unsigned int j, k;
1572
1573 for (j = 0; j < vector_active(cmd_vector); j++)
1574 if ((cmd_element =
1575 vector_slot(cmd_vector, j)) != NULL
1576 &&
1577 (vector_active
1578 (cmd_element->strvec))) {
1579 descvec =
1580 vector_slot(cmd_element->
1581 strvec,
1582 vector_active
1583 (cmd_element->
1584 strvec) - 1);
1585 for (k = 0;
1586 k < vector_active(descvec);
1587 k++) {
1588 struct desc *desc =
1589 vector_slot(descvec,
1590 k);
1591 vector_set(matchvec,
1592 desc);
1593 }
1594 }
1595
1596 vector_set(matchvec, &desc_cr);
1597 vector_free(cmd_vector);
1598
1599 return matchvec;
1600 }
1601
1602 if ((ret =
1603 is_cmd_ambiguous(command, cmd_vector, i,
1604 match)) == 1) {
1605 vector_free(cmd_vector);
1606 *status = CMD_ERR_AMBIGUOUS;
1607 return NULL;
1608 } else if (ret == 2) {
1609 vector_free(cmd_vector);
1610 *status = CMD_ERR_NO_MATCH;
1611 return NULL;
1612 }
1613 }
1614
1615 /* Prepare match vector */
1616 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1617
1618 /* Make sure that cmd_vector is filtered based on current word */
1619 command = vector_slot(vline, index);
1620 if (command)
1621 match = cmd_filter_by_completion(command, cmd_vector, index);
1622
1623 /* Make description vector. */
1624 for (i = 0; i < vector_active(cmd_vector); i++)
1625 if ((cmd_element = vector_slot(cmd_vector, i)) != NULL) {
1626 const char *string = NULL;
1627 vector strvec = cmd_element->strvec;
1628
1629 /* if command is NULL, index may be equal to vector_active */
1630 if (command && index >= vector_active(strvec))
1631 vector_slot(cmd_vector, i) = NULL;
1632 else {
1633 /* Check if command is completed. */
1634 if (command == NULL
1635 && index == vector_active(strvec)) {
1636 string = "<cr>";
1637 if (!desc_unique_string
1638 (matchvec, string))
1639 vector_set(matchvec, &desc_cr);
1640 } else {
1641 unsigned int j;
1642 vector descvec =
1643 vector_slot(strvec, index);
1644 struct desc *desc;
1645
1646 for (j = 0; j < vector_active(descvec);
1647 j++)
1648 if ((desc =
1649 vector_slot(descvec, j))) {
1650 string =
1651 cmd_entry_function_desc
1652 (command,
1653 desc->cmd);
1654 if (string) {
1655 /* Uniqueness check */
1656 if (!desc_unique_string(matchvec, string))
1657 vector_set
1658 (matchvec,
1659 desc);
1660 }
1661 }
1662 }
1663 }
1664 }
1665 vector_free(cmd_vector);
1666
1667 if (vector_slot(matchvec, 0) == NULL) {
1668 vector_free(matchvec);
1669 *status = CMD_ERR_NO_MATCH;
1670 } else
1671 *status = CMD_SUCCESS;
1672
1673 return matchvec;
1674}
1675
1676vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1677{
1678 vector ret;
1679
1680 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1681 enum node_type onode;
1682 vector shifted_vline;
1683 unsigned int index;
1684
1685 onode = vty->node;
1686 vty->node = ENABLE_NODE;
1687 /* We can try it on enable node, cos' the vty is authenticated */
1688
1689 shifted_vline = vector_init(vector_count(vline));
1690 /* use memcpy? */
1691 for (index = 1; index < vector_active(vline); index++) {
1692 vector_set_index(shifted_vline, index - 1,
1693 vector_lookup(vline, index));
1694 }
1695
1696 ret = cmd_describe_command_real(shifted_vline, vty, status);
1697
1698 vector_free(shifted_vline);
1699 vty->node = onode;
1700 return ret;
1701 }
1702
1703 return cmd_describe_command_real(vline, vty, status);
1704}
1705
1706/* Check LCD of matched command. */
1707static int cmd_lcd(char **matched)
1708{
1709 int i;
1710 int j;
1711 int lcd = -1;
1712 char *s1, *s2;
1713 char c1, c2;
1714
1715 if (matched[0] == NULL || matched[1] == NULL)
1716 return 0;
1717
1718 for (i = 1; matched[i] != NULL; i++) {
1719 s1 = matched[i - 1];
1720 s2 = matched[i];
1721
1722 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1723 if (c1 != c2)
1724 break;
1725
1726 if (lcd < 0)
1727 lcd = j;
1728 else {
1729 if (lcd > j)
1730 lcd = j;
1731 }
1732 }
1733 return lcd;
1734}
1735
1736/* Command line completion support. */
1737static char **cmd_complete_command_real(vector vline, struct vty *vty,
1738 int *status)
1739{
1740 unsigned int i;
1741 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1742#define INIT_MATCHVEC_SIZE 10
1743 vector matchvec;
1744 struct cmd_element *cmd_element;
1745 unsigned int index;
1746 char **match_str;
1747 struct desc *desc;
1748 vector descvec;
1749 char *command;
1750 int lcd;
1751
1752 if (vector_active(vline) == 0) {
1753 *status = CMD_ERR_NO_MATCH;
1754 return NULL;
1755 } else
1756 index = vector_active(vline) - 1;
1757
1758 /* First, filter by preceeding command string */
1759 for (i = 0; i < index; i++)
1760 if ((command = vector_slot(vline, i))) {
1761 enum match_type match;
1762 int ret;
1763
1764 /* First try completion match, if there is exactly match return 1 */
1765 match =
1766 cmd_filter_by_completion(command, cmd_vector, i);
1767
1768 /* If there is exact match then filter ambiguous match else check
1769 ambiguousness. */
1770 if ((ret =
1771 is_cmd_ambiguous(command, cmd_vector, i,
1772 match)) == 1) {
1773 vector_free(cmd_vector);
1774 *status = CMD_ERR_AMBIGUOUS;
1775 return NULL;
1776 }
1777 /*
1778 else if (ret == 2)
1779 {
1780 vector_free (cmd_vector);
1781 *status = CMD_ERR_NO_MATCH;
1782 return NULL;
1783 }
1784 */
1785 }
1786
1787 /* Prepare match vector. */
1788 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1789
1790 /* Now we got into completion */
1791 for (i = 0; i < vector_active(cmd_vector); i++)
1792 if ((cmd_element = vector_slot(cmd_vector, i))) {
1793 const char *string;
1794 vector strvec = cmd_element->strvec;
1795
1796 /* Check field length */
1797 if (index >= vector_active(strvec))
1798 vector_slot(cmd_vector, i) = NULL;
1799 else {
1800 unsigned int j;
1801
1802 descvec = vector_slot(strvec, index);
1803 for (j = 0; j < vector_active(descvec); j++)
1804 if ((desc = vector_slot(descvec, j))) {
1805 if ((string =
1806 cmd_entry_function
1807 (vector_slot(vline, index),
1808 desc->cmd)))
1809 if (cmd_unique_string
1810 (matchvec, string))
1811 vector_set
1812 (matchvec,
1813 strdup
1814 (string));
1815 }
1816 }
1817 }
1818
1819 /* We don't need cmd_vector any more. */
1820 vector_free(cmd_vector);
1821
1822 /* No matched command */
1823 if (vector_slot(matchvec, 0) == NULL) {
1824 vector_free(matchvec);
1825
1826 /* In case of 'command \t' pattern. Do you need '?' command at
1827 the end of the line. */
1828 if (vector_slot(vline, index) == '\0')
1829 *status = CMD_ERR_NOTHING_TODO;
1830 else
1831 *status = CMD_ERR_NO_MATCH;
1832 return NULL;
1833 }
1834
1835 /* Only one matched */
1836 if (vector_slot(matchvec, 1) == NULL) {
1837 match_str = (char **)matchvec->index;
1838 vector_only_wrapper_free(matchvec);
1839 *status = CMD_COMPLETE_FULL_MATCH;
1840 return match_str;
1841 }
1842 /* Make it sure last element is NULL. */
1843 vector_set(matchvec, NULL);
1844
1845 /* Check LCD of matched strings. */
1846 if (vector_slot(vline, index) != NULL) {
1847 lcd = cmd_lcd((char **)matchvec->index);
1848
1849 if (lcd) {
1850 int len = strlen(vector_slot(vline, index));
1851
1852 if (len < lcd) {
1853 char *lcdstr;
1854
1855 lcdstr = malloc(lcd + 1);
1856 memcpy(lcdstr, matchvec->index[0], lcd);
1857 lcdstr[lcd] = '\0';
1858
1859 /* match_str = (char **) &lcdstr; */
1860
1861 /* Free matchvec. */
1862 for (i = 0; i < vector_active(matchvec); i++) {
1863 if (vector_slot(matchvec, i))
1864 free(vector_slot(matchvec, i));
1865 }
1866 vector_free(matchvec);
1867
1868 /* Make new matchvec. */
1869 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1870 vector_set(matchvec, lcdstr);
1871 match_str = (char **)matchvec->index;
1872 vector_only_wrapper_free(matchvec);
1873
1874 *status = CMD_COMPLETE_MATCH;
1875 return match_str;
1876 }
1877 }
1878 }
1879
1880 match_str = (char **)matchvec->index;
1881 vector_only_wrapper_free(matchvec);
1882 *status = CMD_COMPLETE_LIST_MATCH;
1883 return match_str;
1884}
1885
1886char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1887{
1888 char **ret;
1889
1890 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1891 enum node_type onode;
1892 vector shifted_vline;
1893 unsigned int index;
1894
1895 onode = vty->node;
1896 vty->node = ENABLE_NODE;
1897 /* We can try it on enable node, cos' the vty is authenticated */
1898
1899 shifted_vline = vector_init(vector_count(vline));
1900 /* use memcpy? */
1901 for (index = 1; index < vector_active(vline); index++) {
1902 vector_set_index(shifted_vline, index - 1,
1903 vector_lookup(vline, index));
1904 }
1905
1906 ret = cmd_complete_command_real(shifted_vline, vty, status);
1907
1908 vector_free(shifted_vline);
1909 vty->node = onode;
1910 return ret;
1911 }
1912
1913 return cmd_complete_command_real(vline, vty, status);
1914}
1915
1916/* return parent node */
1917/* MUST eventually converge on CONFIG_NODE */
1918enum node_type node_parent(enum node_type node)
1919{
1920 enum node_type ret;
1921
1922 assert(node > CONFIG_NODE);
1923
1924 switch (node) {
1925 case BGP_VPNV4_NODE:
1926 case BGP_IPV4_NODE:
1927 case BGP_IPV4M_NODE:
1928 case BGP_IPV6_NODE:
1929 ret = BGP_NODE;
1930 break;
1931 case KEYCHAIN_KEY_NODE:
1932 ret = KEYCHAIN_NODE;
1933 break;
1934 default:
1935 ret = CONFIG_NODE;
1936 }
1937
1938 return ret;
1939}
1940
1941/* Execute command by argument vline vector. */
1942static int
1943cmd_execute_command_real(vector vline, struct vty *vty,
1944 struct cmd_element **cmd)
1945{
1946 unsigned int i;
1947 unsigned int index;
1948 vector cmd_vector;
1949 struct cmd_element *cmd_element;
1950 struct cmd_element *matched_element;
1951 unsigned int matched_count, incomplete_count;
1952 int argc;
1953 const char *argv[CMD_ARGC_MAX];
1954 enum match_type match = 0;
1955 int varflag;
1956 char *command;
1957
1958 /* Make copy of command elements. */
1959 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1960
1961 for (index = 0; index < vector_active(vline); index++)
1962 if ((command = vector_slot(vline, index))) {
1963 int ret;
1964
1965 match =
1966 cmd_filter_by_completion(command, cmd_vector,
1967 index);
1968
1969 if (match == vararg_match)
1970 break;
1971
1972 ret =
1973 is_cmd_ambiguous(command, cmd_vector, index, match);
1974
1975 if (ret == 1) {
1976 vector_free(cmd_vector);
1977 return CMD_ERR_AMBIGUOUS;
1978 } else if (ret == 2) {
1979 vector_free(cmd_vector);
1980 return CMD_ERR_NO_MATCH;
1981 }
1982 }
1983
1984 /* Check matched count. */
1985 matched_element = NULL;
1986 matched_count = 0;
1987 incomplete_count = 0;
1988
1989 for (i = 0; i < vector_active(cmd_vector); i++)
1990 if ((cmd_element = vector_slot(cmd_vector, i))) {
1991 if (match == vararg_match
1992 || index >= cmd_element->cmdsize) {
1993 matched_element = cmd_element;
1994#if 0
1995 printf("DEBUG: %s\n", cmd_element->string);
1996#endif
1997 matched_count++;
1998 } else {
1999 incomplete_count++;
2000 }
2001 }
2002
2003 /* Finish of using cmd_vector. */
2004 vector_free(cmd_vector);
2005
2006 /* To execute command, matched_count must be 1. */
2007 if (matched_count == 0) {
2008 if (incomplete_count)
2009 return CMD_ERR_INCOMPLETE;
2010 else
2011 return CMD_ERR_NO_MATCH;
2012 }
2013
2014 if (matched_count > 1)
2015 return CMD_ERR_AMBIGUOUS;
2016
2017 /* Argument treatment */
2018 varflag = 0;
2019 argc = 0;
2020
2021 for (i = 0; i < vector_active(vline); i++) {
2022 if (varflag)
2023 argv[argc++] = vector_slot(vline, i);
2024 else {
2025 vector descvec =
2026 vector_slot(matched_element->strvec, i);
2027
2028 if (vector_active(descvec) == 1) {
2029 struct desc *desc = vector_slot(descvec, 0);
2030
2031 if (CMD_VARARG(desc->cmd))
2032 varflag = 1;
2033
2034 if (varflag || CMD_VARIABLE(desc->cmd)
2035 || CMD_OPTION(desc->cmd))
2036 argv[argc++] = vector_slot(vline, i);
2037 } else
2038 argv[argc++] = vector_slot(vline, i);
2039 }
2040
2041 if (argc >= CMD_ARGC_MAX)
2042 return CMD_ERR_EXEED_ARGC_MAX;
2043 }
2044
2045 /* For vtysh execution. */
2046 if (cmd)
2047 *cmd = matched_element;
2048
2049 if (matched_element->daemon)
2050 return CMD_SUCCESS_DAEMON;
2051
2052 /* Execute matched command. */
2053 return (*matched_element->func) (matched_element, vty, argc, argv);
2054}
2055
2056int
2057cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2058 int vtysh)
2059{
2060 int ret, saved_ret, tried = 0;
2061 enum node_type onode, try_node;
2062
2063 onode = try_node = vty->node;
2064
2065 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2066 vector shifted_vline;
2067 unsigned int index;
2068
2069 vty->node = ENABLE_NODE;
2070 /* We can try it on enable node, cos' the vty is authenticated */
2071
2072 shifted_vline = vector_init(vector_count(vline));
2073 /* use memcpy? */
2074 for (index = 1; index < vector_active(vline); index++) {
2075 vector_set_index(shifted_vline, index - 1,
2076 vector_lookup(vline, index));
2077 }
2078
2079 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2080
2081 vector_free(shifted_vline);
2082 vty->node = onode;
2083 return ret;
2084 }
2085
2086 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2087
2088 if (vtysh)
2089 return saved_ret;
2090
2091 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
2092 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2093 && vty->node > CONFIG_NODE) {
2094 try_node = node_parent(try_node);
2095 vty->node = try_node;
2096 ret = cmd_execute_command_real(vline, vty, cmd);
2097 tried = 1;
2098 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2099 /* succesfull command, leave the node as is */
2100 return ret;
2101 }
2102 }
2103 /* no command succeeded, reset the vty to the original node and
2104 return the error for this node */
2105 if (tried)
2106 vty->node = onode;
2107 return saved_ret;
2108}
2109
2110/* Execute command by argument readline. */
2111int
2112cmd_execute_command_strict(vector vline, struct vty *vty,
2113 struct cmd_element **cmd)
2114{
2115 unsigned int i;
2116 unsigned int index;
2117 vector cmd_vector;
2118 struct cmd_element *cmd_element;
2119 struct cmd_element *matched_element;
2120 unsigned int matched_count, incomplete_count;
2121 int argc;
2122 const char *argv[CMD_ARGC_MAX];
2123 int varflag;
2124 enum match_type match = 0;
2125 char *command;
2126
2127 /* Make copy of command element */
2128 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2129
2130 for (index = 0; index < vector_active(vline); index++)
2131 if ((command = vector_slot(vline, index))) {
2132 int ret;
2133
2134 match = cmd_filter_by_string(vector_slot(vline, index),
2135 cmd_vector, index);
2136
2137 /* If command meets '.VARARG' then finish matching. */
2138 if (match == vararg_match)
2139 break;
2140
2141 ret =
2142 is_cmd_ambiguous(command, cmd_vector, index, match);
2143 if (ret == 1) {
2144 vector_free(cmd_vector);
2145 return CMD_ERR_AMBIGUOUS;
2146 }
2147 if (ret == 2) {
2148 vector_free(cmd_vector);
2149 return CMD_ERR_NO_MATCH;
2150 }
2151 }
2152
2153 /* Check matched count. */
2154 matched_element = NULL;
2155 matched_count = 0;
2156 incomplete_count = 0;
2157 for (i = 0; i < vector_active(cmd_vector); i++)
2158 if (vector_slot(cmd_vector, i) != NULL) {
2159 cmd_element = vector_slot(cmd_vector, i);
2160
2161 if (match == vararg_match
2162 || index >= cmd_element->cmdsize) {
2163 matched_element = cmd_element;
2164 matched_count++;
2165 } else
2166 incomplete_count++;
2167 }
2168
2169 /* Finish of using cmd_vector. */
2170 vector_free(cmd_vector);
2171
2172 /* To execute command, matched_count must be 1. */
2173 if (matched_count == 0) {
2174 if (incomplete_count)
2175 return CMD_ERR_INCOMPLETE;
2176 else
2177 return CMD_ERR_NO_MATCH;
2178 }
2179
2180 if (matched_count > 1)
2181 return CMD_ERR_AMBIGUOUS;
2182
2183 /* Argument treatment */
2184 varflag = 0;
2185 argc = 0;
2186
2187 for (i = 0; i < vector_active(vline); i++) {
2188 if (varflag)
2189 argv[argc++] = vector_slot(vline, i);
2190 else {
2191 vector descvec =
2192 vector_slot(matched_element->strvec, i);
2193
2194 if (vector_active(descvec) == 1) {
2195 struct desc *desc = vector_slot(descvec, 0);
2196
2197 if (CMD_VARARG(desc->cmd))
2198 varflag = 1;
2199
2200 if (varflag || CMD_VARIABLE(desc->cmd)
2201 || CMD_OPTION(desc->cmd))
2202 argv[argc++] = vector_slot(vline, i);
2203 } else
2204 argv[argc++] = vector_slot(vline, i);
2205 }
2206
2207 if (argc >= CMD_ARGC_MAX)
2208 return CMD_ERR_EXEED_ARGC_MAX;
2209 }
2210
2211 /* For vtysh execution. */
2212 if (cmd)
2213 *cmd = matched_element;
2214
2215 if (matched_element->daemon)
2216 return CMD_SUCCESS_DAEMON;
2217
2218 /* Now execute matched command */
2219 return (*matched_element->func) (matched_element, vty, argc, argv);
2220}
2221
Harald Welte59b04682009-06-10 05:40:52 +08002222/* Configration make from file. */
2223int config_from_file(struct vty *vty, FILE * fp)
2224{
2225 int ret;
2226 vector vline;
2227
2228 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2229 vline = cmd_make_strvec(vty->buf);
2230
2231 /* In case of comment line */
2232 if (vline == NULL)
2233 continue;
2234 /* Execute configuration command : this is strict match */
2235 ret = cmd_execute_command_strict(vline, vty, NULL);
2236
2237 /* Try again with setting node to CONFIG_NODE */
2238 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2239 && ret != CMD_ERR_NOTHING_TODO
2240 && vty->node != CONFIG_NODE) {
2241 vty->node = node_parent(vty->node);
2242 ret = cmd_execute_command_strict(vline, vty, NULL);
2243 }
2244
2245 cmd_free_strvec(vline);
2246
2247 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2248 && ret != CMD_ERR_NOTHING_TODO)
2249 return ret;
2250 }
2251 return CMD_SUCCESS;
2252}
Harald Welte59b04682009-06-10 05:40:52 +08002253
2254/* Configration from terminal */
2255DEFUN(config_terminal,
2256 config_terminal_cmd,
2257 "configure terminal",
2258 "Configuration from vty interface\n" "Configuration terminal\n")
2259{
2260 if (vty_config_lock(vty))
2261 vty->node = CONFIG_NODE;
2262 else {
2263 vty_out(vty, "VTY configuration is locked by other VTY%s",
2264 VTY_NEWLINE);
2265 return CMD_WARNING;
2266 }
2267 return CMD_SUCCESS;
2268}
2269
2270/* Enable command */
2271DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2272{
2273 /* If enable password is NULL, change to ENABLE_NODE */
2274 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2275 vty->type == VTY_SHELL_SERV)
2276 vty->node = ENABLE_NODE;
2277 else
2278 vty->node = AUTH_ENABLE_NODE;
2279
2280 return CMD_SUCCESS;
2281}
2282
2283/* Disable command */
2284DEFUN(disable,
2285 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2286{
2287 if (vty->node == ENABLE_NODE)
2288 vty->node = VIEW_NODE;
2289 return CMD_SUCCESS;
2290}
2291
2292/* Down vty node level. */
2293DEFUN(config_exit,
2294 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2295{
2296 switch (vty->node) {
2297 case BTS_NODE:
2298 vty->node = VIEW_NODE;
2299 vty->index = NULL;
2300 break;
2301 case TRX_NODE:
2302 vty->node = BTS_NODE;
2303 {
2304 /* set vty->index correctly ! */
2305 struct gsm_bts_trx *trx = vty->index;
2306 vty->index = trx->bts;
2307 }
2308 break;
2309 case TS_NODE:
2310 vty->node = TRX_NODE;
2311 {
2312 /* set vty->index correctly ! */
2313 struct gsm_bts_trx_ts *ts = vty->index;
2314 vty->index = ts->trx;
2315 }
2316 break;
2317 case SUBSCR_NODE:
2318 vty->node = VIEW_NODE;
2319 subscr_put(vty->index);
2320 vty->index = NULL;
2321 break;
2322 case VIEW_NODE:
2323 case ENABLE_NODE:
2324 if (0) //vty_shell (vty))
2325 exit(0);
2326 else
2327 vty->status = VTY_CLOSE;
2328 break;
2329 case CONFIG_NODE:
2330 vty->node = ENABLE_NODE;
2331 vty_config_unlock(vty);
2332 break;
2333 case INTERFACE_NODE:
2334 case ZEBRA_NODE:
2335 case BGP_NODE:
2336 case RIP_NODE:
2337 case RIPNG_NODE:
2338 case OSPF_NODE:
2339 case OSPF6_NODE:
2340 case ISIS_NODE:
2341 case KEYCHAIN_NODE:
2342 case MASC_NODE:
2343 case RMAP_NODE:
2344 case VTY_NODE:
2345 vty->node = CONFIG_NODE;
2346 break;
2347 case BGP_VPNV4_NODE:
2348 case BGP_IPV4_NODE:
2349 case BGP_IPV4M_NODE:
2350 case BGP_IPV6_NODE:
2351 vty->node = BGP_NODE;
2352 break;
2353 case KEYCHAIN_KEY_NODE:
2354 vty->node = KEYCHAIN_NODE;
2355 break;
2356 default:
2357 break;
2358 }
2359 return CMD_SUCCESS;
2360}
2361
2362/* quit is alias of exit. */
2363ALIAS(config_exit,
2364 config_quit_cmd, "quit", "Exit current mode and down to previous mode\n")
2365
2366/* End of configuration. */
2367 DEFUN(config_end,
2368 config_end_cmd, "end", "End current mode and change to enable mode.")
2369{
2370 switch (vty->node) {
2371 case VIEW_NODE:
2372 case ENABLE_NODE:
2373 /* Nothing to do. */
2374 break;
2375 case CONFIG_NODE:
2376 case INTERFACE_NODE:
2377 case ZEBRA_NODE:
2378 case RIP_NODE:
2379 case RIPNG_NODE:
2380 case BGP_NODE:
2381 case BGP_VPNV4_NODE:
2382 case BGP_IPV4_NODE:
2383 case BGP_IPV4M_NODE:
2384 case BGP_IPV6_NODE:
2385 case RMAP_NODE:
2386 case OSPF_NODE:
2387 case OSPF6_NODE:
2388 case ISIS_NODE:
2389 case KEYCHAIN_NODE:
2390 case KEYCHAIN_KEY_NODE:
2391 case MASC_NODE:
2392 case VTY_NODE:
2393 vty_config_unlock(vty);
2394 vty->node = ENABLE_NODE;
2395 break;
2396 default:
2397 break;
2398 }
2399 return CMD_SUCCESS;
2400}
2401
2402/* Show version. */
2403DEFUN(show_version,
2404 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2405{
2406 vty_out(vty, "%s %s (%s).%s", QUAGGA_PROGNAME, QUAGGA_VERSION,
2407 host.name ? host.name : "", VTY_NEWLINE);
2408 vty_out(vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE);
2409
2410 return CMD_SUCCESS;
2411}
2412
2413/* Help display function for all node. */
2414DEFUN(config_help,
2415 config_help_cmd, "help", "Description of the interactive help system\n")
2416{
2417 vty_out(vty,
2418 "This VTY provides advanced help features. When you need help,%s\
2419anytime at the command line please press '?'.%s\
2420%s\
2421If nothing matches, the help list will be empty and you must backup%s\
2422 until entering a '?' shows the available options.%s\
2423Two styles of help are provided:%s\
24241. Full help is available when you are ready to enter a%s\
2425command argument (e.g. 'show ?') and describes each possible%s\
2426argument.%s\
24272. Partial help is provided when an abbreviated argument is entered%s\
2428 and you want to know what arguments match the input%s\
2429 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2430 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2431 return CMD_SUCCESS;
2432}
2433
2434/* Help display function for all node. */
2435DEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2436{
2437 unsigned int i;
2438 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2439 struct cmd_element *cmd;
2440
2441 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2442 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2443 && !(cmd->attr == CMD_ATTR_DEPRECATED
2444 || cmd->attr == CMD_ATTR_HIDDEN))
2445 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2446 return CMD_SUCCESS;
2447}
2448
Harald Welte59b04682009-06-10 05:40:52 +08002449/* Write current configuration into file. */
2450DEFUN(config_write_file,
2451 config_write_file_cmd,
2452 "write file",
2453 "Write running configuration to memory, network, or terminal\n"
2454 "Write to configuration file\n")
2455{
2456 unsigned int i;
2457 int fd;
2458 struct cmd_node *node;
2459 char *config_file;
2460 char *config_file_tmp = NULL;
2461 char *config_file_sav = NULL;
2462 struct vty *file_vty;
2463
2464 /* Check and see if we are operating under vtysh configuration */
2465 if (host.config == NULL) {
2466 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2467 VTY_NEWLINE);
2468 return CMD_WARNING;
2469 }
2470
2471 /* Get filename. */
2472 config_file = host.config;
2473
2474 config_file_sav =
2475 malloc(strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1);
2476 strcpy(config_file_sav, config_file);
2477 strcat(config_file_sav, CONF_BACKUP_EXT);
2478
2479 config_file_tmp = malloc(strlen(config_file) + 8);
2480 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2481
2482 /* Open file to configuration write. */
2483 fd = mkstemp(config_file_tmp);
2484 if (fd < 0) {
2485 vty_out(vty, "Can't open configuration file %s.%s",
2486 config_file_tmp, VTY_NEWLINE);
2487 free(config_file_tmp);
2488 free(config_file_sav);
2489 return CMD_WARNING;
2490 }
2491
2492 /* Make vty for configuration file. */
2493 file_vty = vty_new();
2494 file_vty->fd = fd;
2495 file_vty->type = VTY_FILE;
2496
2497 /* Config file header print. */
2498 vty_out(file_vty, "!\n! Zebra configuration saved from vty\n! ");
2499 //vty_time_print (file_vty, 1);
2500 vty_out(file_vty, "!\n");
2501
2502 for (i = 0; i < vector_active(cmdvec); i++)
2503 if ((node = vector_slot(cmdvec, i)) && node->func) {
2504 if ((*node->func) (file_vty))
2505 vty_out(file_vty, "!\n");
2506 }
2507 vty_close(file_vty);
2508
2509 if (unlink(config_file_sav) != 0)
2510 if (errno != ENOENT) {
2511 vty_out(vty,
2512 "Can't unlink backup configuration file %s.%s",
2513 config_file_sav, VTY_NEWLINE);
2514 free(config_file_sav);
2515 free(config_file_tmp);
2516 unlink(config_file_tmp);
2517 return CMD_WARNING;
2518 }
2519 if (link(config_file, config_file_sav) != 0) {
2520 vty_out(vty, "Can't backup old configuration file %s.%s",
2521 config_file_sav, VTY_NEWLINE);
2522 free(config_file_sav);
2523 free(config_file_tmp);
2524 unlink(config_file_tmp);
2525 return CMD_WARNING;
2526 }
2527 sync();
2528 if (unlink(config_file) != 0) {
2529 vty_out(vty, "Can't unlink configuration file %s.%s",
2530 config_file, VTY_NEWLINE);
2531 free(config_file_sav);
2532 free(config_file_tmp);
2533 unlink(config_file_tmp);
2534 return CMD_WARNING;
2535 }
2536 if (link(config_file_tmp, config_file) != 0) {
2537 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2538 VTY_NEWLINE);
2539 free(config_file_sav);
2540 free(config_file_tmp);
2541 unlink(config_file_tmp);
2542 return CMD_WARNING;
2543 }
2544 unlink(config_file_tmp);
2545 sync();
2546
2547 free(config_file_sav);
2548 free(config_file_tmp);
2549
2550 if (chmod(config_file, CONFIGFILE_MASK) != 0) {
2551 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
Harald Weltec1a13942009-08-06 18:50:10 +02002552 config_file, strerror(errno), errno, VTY_NEWLINE);
Harald Welte59b04682009-06-10 05:40:52 +08002553 return CMD_WARNING;
2554 }
2555
2556 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2557 return CMD_SUCCESS;
2558}
2559
2560ALIAS(config_write_file,
2561 config_write_cmd,
2562 "write", "Write running configuration to memory, network, or terminal\n")
2563
2564 ALIAS(config_write_file,
2565 config_write_memory_cmd,
2566 "write memory",
2567 "Write running configuration to memory, network, or terminal\n"
2568 "Write configuration to the file (same as write file)\n")
2569
2570 ALIAS(config_write_file,
2571 copy_runningconfig_startupconfig_cmd,
2572 "copy running-config startup-config",
2573 "Copy configuration\n"
2574 "Copy running config to... \n"
2575 "Copy running config to startup config (same as write file)\n")
2576
2577/* Write current configuration into the terminal. */
2578 DEFUN(config_write_terminal,
2579 config_write_terminal_cmd,
2580 "write terminal",
2581 "Write running configuration to memory, network, or terminal\n"
2582 "Write to terminal\n")
2583{
2584 unsigned int i;
2585 struct cmd_node *node;
2586
2587 if (vty->type == VTY_SHELL_SERV) {
2588 for (i = 0; i < vector_active(cmdvec); i++)
2589 if ((node = vector_slot(cmdvec, i)) && node->func
2590 && node->vtysh) {
2591 if ((*node->func) (vty))
2592 vty_out(vty, "!%s", VTY_NEWLINE);
2593 }
2594 } else {
2595 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2596 VTY_NEWLINE);
2597 vty_out(vty, "!%s", VTY_NEWLINE);
2598
2599 for (i = 0; i < vector_active(cmdvec); i++)
2600 if ((node = vector_slot(cmdvec, i)) && node->func) {
2601 if ((*node->func) (vty))
2602 vty_out(vty, "!%s", VTY_NEWLINE);
2603 }
2604 vty_out(vty, "end%s", VTY_NEWLINE);
2605 }
2606 return CMD_SUCCESS;
2607}
2608
2609/* Write current configuration into the terminal. */
2610ALIAS(config_write_terminal,
2611 show_running_config_cmd,
2612 "show running-config", SHOW_STR "running configuration\n")
2613
2614/* Write startup configuration into the terminal. */
2615 DEFUN(show_startup_config,
2616 show_startup_config_cmd,
2617 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2618{
2619 char buf[BUFSIZ];
2620 FILE *confp;
2621
2622 confp = fopen(host.config, "r");
2623 if (confp == NULL) {
2624 vty_out(vty, "Can't open configuration file [%s]%s",
2625 host.config, VTY_NEWLINE);
2626 return CMD_WARNING;
2627 }
2628
2629 while (fgets(buf, BUFSIZ, confp)) {
2630 char *cp = buf;
2631
2632 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2633 cp++;
2634 *cp = '\0';
2635
2636 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2637 }
2638
2639 fclose(confp);
2640
2641 return CMD_SUCCESS;
2642}
Harald Welte59b04682009-06-10 05:40:52 +08002643
2644/* Hostname configuration */
2645DEFUN(config_hostname,
2646 hostname_cmd,
2647 "hostname WORD",
2648 "Set system's network name\n" "This system's network name\n")
2649{
2650 if (!isalpha((int)*argv[0])) {
2651 vty_out(vty, "Please specify string starting with alphabet%s",
2652 VTY_NEWLINE);
2653 return CMD_WARNING;
2654 }
2655
2656 if (host.name)
2657 free(host.name);
2658
2659 host.name = strdup(argv[0]);
2660 return CMD_SUCCESS;
2661}
2662
2663DEFUN(config_no_hostname,
2664 no_hostname_cmd,
2665 "no hostname [HOSTNAME]",
2666 NO_STR "Reset system's network name\n" "Host name of this router\n")
2667{
2668 if (host.name)
2669 free(host.name);
2670 host.name = NULL;
2671 return CMD_SUCCESS;
2672}
2673
2674/* VTY interface password set. */
2675DEFUN(config_password, password_cmd,
2676 "password (8|) WORD",
2677 "Assign the terminal connection password\n"
2678 "Specifies a HIDDEN password will follow\n"
2679 "dummy string \n" "The HIDDEN line password string\n")
2680{
2681 /* Argument check. */
2682 if (argc == 0) {
2683 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2684 return CMD_WARNING;
2685 }
2686
2687 if (argc == 2) {
2688 if (*argv[0] == '8') {
2689 if (host.password)
2690 free(host.password);
2691 host.password = NULL;
2692 if (host.password_encrypt)
2693 free(host.password_encrypt);
2694 host.password_encrypt = strdup(strdup(argv[1]));
2695 return CMD_SUCCESS;
2696 } else {
2697 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2698 return CMD_WARNING;
2699 }
2700 }
2701
2702 if (!isalnum((int)*argv[0])) {
2703 vty_out(vty,
2704 "Please specify string starting with alphanumeric%s",
2705 VTY_NEWLINE);
2706 return CMD_WARNING;
2707 }
2708
2709 if (host.password)
2710 free(host.password);
2711 host.password = NULL;
2712
2713#ifdef VTY_CRYPT_PW
2714 if (host.encrypt) {
2715 if (host.password_encrypt)
2716 free(host.password_encrypt);
2717 host.password_encrypt = strdup(zencrypt(argv[0]));
2718 } else
2719#endif
2720 host.password = strdup(argv[0]);
2721
2722 return CMD_SUCCESS;
2723}
2724
2725ALIAS(config_password, password_text_cmd,
2726 "password LINE",
2727 "Assign the terminal connection password\n"
2728 "The UNENCRYPTED (cleartext) line password\n")
2729
2730/* VTY enable password set. */
2731 DEFUN(config_enable_password, enable_password_cmd,
2732 "enable password (8|) WORD",
2733 "Modify enable password parameters\n"
2734 "Assign the privileged level password\n"
2735 "Specifies a HIDDEN password will follow\n"
2736 "dummy string \n" "The HIDDEN 'enable' password string\n")
2737{
2738 /* Argument check. */
2739 if (argc == 0) {
2740 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2741 return CMD_WARNING;
2742 }
2743
2744 /* Crypt type is specified. */
2745 if (argc == 2) {
2746 if (*argv[0] == '8') {
2747 if (host.enable)
2748 free(host.enable);
2749 host.enable = NULL;
2750
2751 if (host.enable_encrypt)
2752 free(host.enable_encrypt);
2753 host.enable_encrypt = strdup(argv[1]);
2754
2755 return CMD_SUCCESS;
2756 } else {
2757 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2758 return CMD_WARNING;
2759 }
2760 }
2761
2762 if (!isalnum((int)*argv[0])) {
2763 vty_out(vty,
2764 "Please specify string starting with alphanumeric%s",
2765 VTY_NEWLINE);
2766 return CMD_WARNING;
2767 }
2768
2769 if (host.enable)
2770 free(host.enable);
2771 host.enable = NULL;
2772
2773 /* Plain password input. */
2774#ifdef VTY_CRYPT_PW
2775 if (host.encrypt) {
2776 if (host.enable_encrypt)
2777 free(host.enable_encrypt);
2778 host.enable_encrypt = strdup(zencrypt(argv[0]));
2779 } else
2780#endif
2781 host.enable = strdup(argv[0]);
2782
2783 return CMD_SUCCESS;
2784}
2785
2786ALIAS(config_enable_password,
2787 enable_password_text_cmd,
2788 "enable password LINE",
2789 "Modify enable password parameters\n"
2790 "Assign the privileged level password\n"
2791 "The UNENCRYPTED (cleartext) 'enable' password\n")
2792
2793/* VTY enable password delete. */
2794 DEFUN(no_config_enable_password, no_enable_password_cmd,
2795 "no enable password",
2796 NO_STR
2797 "Modify enable password parameters\n"
2798 "Assign the privileged level password\n")
2799{
2800 if (host.enable)
2801 free(host.enable);
2802 host.enable = NULL;
2803
2804 if (host.enable_encrypt)
2805 free(host.enable_encrypt);
2806 host.enable_encrypt = NULL;
2807
2808 return CMD_SUCCESS;
2809}
2810
2811#ifdef VTY_CRYPT_PW
2812DEFUN(service_password_encrypt,
2813 service_password_encrypt_cmd,
2814 "service password-encryption",
2815 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2816{
2817 if (host.encrypt)
2818 return CMD_SUCCESS;
2819
2820 host.encrypt = 1;
2821
2822 if (host.password) {
2823 if (host.password_encrypt)
2824 free(host.password_encrypt);
2825 host.password_encrypt = strdup(zencrypt(host.password));
2826 }
2827 if (host.enable) {
2828 if (host.enable_encrypt)
2829 free(host.enable_encrypt);
2830 host.enable_encrypt = strdup(zencrypt(host.enable));
2831 }
2832
2833 return CMD_SUCCESS;
2834}
2835
2836DEFUN(no_service_password_encrypt,
2837 no_service_password_encrypt_cmd,
2838 "no service password-encryption",
2839 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2840{
2841 if (!host.encrypt)
2842 return CMD_SUCCESS;
2843
2844 host.encrypt = 0;
2845
2846 if (host.password_encrypt)
2847 free(host.password_encrypt);
2848 host.password_encrypt = NULL;
2849
2850 if (host.enable_encrypt)
2851 free(host.enable_encrypt);
2852 host.enable_encrypt = NULL;
2853
2854 return CMD_SUCCESS;
2855}
2856#endif
2857
2858DEFUN(config_terminal_length, config_terminal_length_cmd,
2859 "terminal length <0-512>",
2860 "Set terminal line parameters\n"
2861 "Set number of lines on a screen\n"
2862 "Number of lines on screen (0 for no pausing)\n")
2863{
2864 int lines;
2865 char *endptr = NULL;
2866
2867 lines = strtol(argv[0], &endptr, 10);
2868 if (lines < 0 || lines > 512 || *endptr != '\0') {
2869 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2870 return CMD_WARNING;
2871 }
2872 vty->lines = lines;
2873
2874 return CMD_SUCCESS;
2875}
2876
2877DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2878 "terminal no length",
2879 "Set terminal line parameters\n"
2880 NO_STR "Set number of lines on a screen\n")
2881{
2882 vty->lines = -1;
2883 return CMD_SUCCESS;
2884}
2885
2886DEFUN(service_terminal_length, service_terminal_length_cmd,
2887 "service terminal-length <0-512>",
2888 "Set up miscellaneous service\n"
2889 "System wide terminal length configuration\n"
2890 "Number of lines of VTY (0 means no line control)\n")
2891{
2892 int lines;
2893 char *endptr = NULL;
2894
2895 lines = strtol(argv[0], &endptr, 10);
2896 if (lines < 0 || lines > 512 || *endptr != '\0') {
2897 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2898 return CMD_WARNING;
2899 }
2900 host.lines = lines;
2901
2902 return CMD_SUCCESS;
2903}
2904
2905DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2906 "no service terminal-length [<0-512>]",
2907 NO_STR
2908 "Set up miscellaneous service\n"
2909 "System wide terminal length configuration\n"
2910 "Number of lines of VTY (0 means no line control)\n")
2911{
2912 host.lines = -1;
2913 return CMD_SUCCESS;
2914}
2915
2916DEFUN_HIDDEN(do_echo,
2917 echo_cmd,
2918 "echo .MESSAGE",
2919 "Echo a message back to the vty\n" "The message to echo\n")
2920{
2921 char *message;
2922
2923 vty_out(vty, "%s%s",
2924 ((message =
2925 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2926 if (message)
2927 free(message);
2928 return CMD_SUCCESS;
2929}
2930
2931#if 0
2932DEFUN(config_logmsg,
2933 config_logmsg_cmd,
2934 "logmsg " LOG_LEVELS " .MESSAGE",
2935 "Send a message to enabled logging destinations\n"
2936 LOG_LEVEL_DESC "The message to send\n")
2937{
2938 int level;
2939 char *message;
2940
2941 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2942 return CMD_ERR_NO_MATCH;
2943
2944 zlog(NULL, level,
2945 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2946 if (message)
2947 free(message);
2948 return CMD_SUCCESS;
2949}
2950
2951DEFUN(show_logging,
2952 show_logging_cmd,
2953 "show logging", SHOW_STR "Show current logging configuration\n")
2954{
2955 struct zlog *zl = zlog_default;
2956
2957 vty_out(vty, "Syslog logging: ");
2958 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2959 vty_out(vty, "disabled");
2960 else
2961 vty_out(vty, "level %s, facility %s, ident %s",
2962 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2963 facility_name(zl->facility), zl->ident);
2964 vty_out(vty, "%s", VTY_NEWLINE);
2965
2966 vty_out(vty, "Stdout logging: ");
2967 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2968 vty_out(vty, "disabled");
2969 else
2970 vty_out(vty, "level %s",
2971 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2972 vty_out(vty, "%s", VTY_NEWLINE);
2973
2974 vty_out(vty, "Monitor logging: ");
2975 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2976 vty_out(vty, "disabled");
2977 else
2978 vty_out(vty, "level %s",
2979 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2980 vty_out(vty, "%s", VTY_NEWLINE);
2981
2982 vty_out(vty, "File logging: ");
2983 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2984 vty_out(vty, "disabled");
2985 else
2986 vty_out(vty, "level %s, filename %s",
2987 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2988 zl->filename);
2989 vty_out(vty, "%s", VTY_NEWLINE);
2990
2991 vty_out(vty, "Protocol name: %s%s",
2992 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2993 vty_out(vty, "Record priority: %s%s",
2994 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2995
2996 return CMD_SUCCESS;
2997}
2998
2999DEFUN(config_log_stdout,
3000 config_log_stdout_cmd,
3001 "log stdout", "Logging control\n" "Set stdout logging level\n")
3002{
3003 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3004 return CMD_SUCCESS;
3005}
3006
3007DEFUN(config_log_stdout_level,
3008 config_log_stdout_level_cmd,
3009 "log stdout " LOG_LEVELS,
3010 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3011{
3012 int level;
3013
3014 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3015 return CMD_ERR_NO_MATCH;
3016 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3017 return CMD_SUCCESS;
3018}
3019
3020DEFUN(no_config_log_stdout,
3021 no_config_log_stdout_cmd,
3022 "no log stdout [LEVEL]",
3023 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3024{
3025 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3026 return CMD_SUCCESS;
3027}
3028
3029DEFUN(config_log_monitor,
3030 config_log_monitor_cmd,
3031 "log monitor",
3032 "Logging control\n" "Set terminal line (monitor) logging level\n")
3033{
3034 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3035 return CMD_SUCCESS;
3036}
3037
3038DEFUN(config_log_monitor_level,
3039 config_log_monitor_level_cmd,
3040 "log monitor " LOG_LEVELS,
3041 "Logging control\n"
3042 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3043{
3044 int level;
3045
3046 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3047 return CMD_ERR_NO_MATCH;
3048 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3049 return CMD_SUCCESS;
3050}
3051
3052DEFUN(no_config_log_monitor,
3053 no_config_log_monitor_cmd,
3054 "no log monitor [LEVEL]",
3055 NO_STR
3056 "Logging control\n"
3057 "Disable terminal line (monitor) logging\n" "Logging level\n")
3058{
3059 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3060 return CMD_SUCCESS;
3061}
3062
3063static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3064{
3065 int ret;
3066 char *p = NULL;
3067 const char *fullpath;
3068
3069 /* Path detection. */
3070 if (!IS_DIRECTORY_SEP(*fname)) {
3071 char cwd[MAXPATHLEN + 1];
3072 cwd[MAXPATHLEN] = '\0';
3073
3074 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3075 zlog_err("config_log_file: Unable to alloc mem!");
3076 return CMD_WARNING;
3077 }
3078
3079 if ((p = malloc(strlen(cwd) + strlen(fname) + 2))
3080 == 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)
3092 free(p);
3093
3094 if (!ret) {
3095 vty_out(vty, "can't open logfile %s\n", fname);
3096 return CMD_WARNING;
3097 }
3098
3099 if (host.logfile)
3100 free(host.logfile);
3101
3102 host.logfile = strdup(fname);
3103
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)
3137 free(host.logfile);
3138
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)
3291 free(host.motdfile);
3292 host.motdfile = strdup(argv[0]);
3293
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)
3312 free(host.motdfile);
3313 host.motdfile = NULL;
3314 return CMD_SUCCESS;
3315}
3316
3317/* Set config filename. Called from vty.c */
3318void host_config_set(char *filename)
3319{
3320 host.config = strdup(filename);
3321}
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 Welte59b04682009-06-10 05:40:52 +08003331 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 Welte59b04682009-06-10 05:40:52 +08003336}
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 Weltec1a13942009-08-06 18:50:10 +02003379 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
Harald Welte59b04682009-06-10 05:40:52 +08003380 }
Harald Weltec1a13942009-08-06 18:50:10 +02003381 install_element (ENABLE_NODE, &show_startup_config_cmd);
Harald Welte59b04682009-06-10 05:40:52 +08003382 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
3402#ifdef VTY_CRYPT_PW
3403 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3404 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3405#endif
3406 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}