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