blob: b6fd2ad92329cdb7154c949536007d89be0b9fbc [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;
2315 case VIEW_NODE:
2316 case ENABLE_NODE:
2317 if (0) //vty_shell (vty))
2318 exit(0);
2319 else
2320 vty->status = VTY_CLOSE;
2321 break;
2322 case CONFIG_NODE:
2323 vty->node = ENABLE_NODE;
2324 vty_config_unlock(vty);
2325 break;
2326 case INTERFACE_NODE:
2327 case ZEBRA_NODE:
2328 case BGP_NODE:
2329 case RIP_NODE:
2330 case RIPNG_NODE:
2331 case OSPF_NODE:
2332 case OSPF6_NODE:
2333 case ISIS_NODE:
2334 case KEYCHAIN_NODE:
2335 case MASC_NODE:
2336 case RMAP_NODE:
2337 case VTY_NODE:
2338 vty->node = CONFIG_NODE;
2339 break;
2340 case BGP_VPNV4_NODE:
2341 case BGP_IPV4_NODE:
2342 case BGP_IPV4M_NODE:
2343 case BGP_IPV6_NODE:
2344 vty->node = BGP_NODE;
2345 break;
2346 case KEYCHAIN_KEY_NODE:
2347 vty->node = KEYCHAIN_NODE;
2348 break;
2349 default:
2350 break;
2351 }
2352 return CMD_SUCCESS;
2353}
2354
2355/* quit is alias of exit. */
2356ALIAS(config_exit,
2357 config_quit_cmd, "quit", "Exit current mode and down to previous mode\n")
2358
2359/* End of configuration. */
2360 DEFUN(config_end,
2361 config_end_cmd, "end", "End current mode and change to enable mode.")
2362{
2363 switch (vty->node) {
2364 case VIEW_NODE:
2365 case ENABLE_NODE:
2366 /* Nothing to do. */
2367 break;
2368 case CONFIG_NODE:
2369 case INTERFACE_NODE:
2370 case ZEBRA_NODE:
2371 case RIP_NODE:
2372 case RIPNG_NODE:
2373 case BGP_NODE:
2374 case BGP_VPNV4_NODE:
2375 case BGP_IPV4_NODE:
2376 case BGP_IPV4M_NODE:
2377 case BGP_IPV6_NODE:
2378 case RMAP_NODE:
2379 case OSPF_NODE:
2380 case OSPF6_NODE:
2381 case ISIS_NODE:
2382 case KEYCHAIN_NODE:
2383 case KEYCHAIN_KEY_NODE:
2384 case MASC_NODE:
2385 case VTY_NODE:
2386 vty_config_unlock(vty);
2387 vty->node = ENABLE_NODE;
2388 break;
2389 default:
2390 break;
2391 }
2392 return CMD_SUCCESS;
2393}
2394
2395/* Show version. */
2396DEFUN(show_version,
2397 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2398{
2399 vty_out(vty, "%s %s (%s).%s", QUAGGA_PROGNAME, QUAGGA_VERSION,
2400 host.name ? host.name : "", VTY_NEWLINE);
2401 vty_out(vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE);
2402
2403 return CMD_SUCCESS;
2404}
2405
2406/* Help display function for all node. */
2407DEFUN(config_help,
2408 config_help_cmd, "help", "Description of the interactive help system\n")
2409{
2410 vty_out(vty,
2411 "This VTY provides advanced help features. When you need help,%s\
2412anytime at the command line please press '?'.%s\
2413%s\
2414If nothing matches, the help list will be empty and you must backup%s\
2415 until entering a '?' shows the available options.%s\
2416Two styles of help are provided:%s\
24171. Full help is available when you are ready to enter a%s\
2418command argument (e.g. 'show ?') and describes each possible%s\
2419argument.%s\
24202. Partial help is provided when an abbreviated argument is entered%s\
2421 and you want to know what arguments match the input%s\
2422 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2423 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2424 return CMD_SUCCESS;
2425}
2426
2427/* Help display function for all node. */
2428DEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2429{
2430 unsigned int i;
2431 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2432 struct cmd_element *cmd;
2433
2434 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2435 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2436 && !(cmd->attr == CMD_ATTR_DEPRECATED
2437 || cmd->attr == CMD_ATTR_HIDDEN))
2438 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2439 return CMD_SUCCESS;
2440}
2441
2442#if 0
2443/* Write current configuration into file. */
2444DEFUN(config_write_file,
2445 config_write_file_cmd,
2446 "write file",
2447 "Write running configuration to memory, network, or terminal\n"
2448 "Write to configuration file\n")
2449{
2450 unsigned int i;
2451 int fd;
2452 struct cmd_node *node;
2453 char *config_file;
2454 char *config_file_tmp = NULL;
2455 char *config_file_sav = NULL;
2456 struct vty *file_vty;
2457
2458 /* Check and see if we are operating under vtysh configuration */
2459 if (host.config == NULL) {
2460 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2461 VTY_NEWLINE);
2462 return CMD_WARNING;
2463 }
2464
2465 /* Get filename. */
2466 config_file = host.config;
2467
2468 config_file_sav =
2469 malloc(strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1);
2470 strcpy(config_file_sav, config_file);
2471 strcat(config_file_sav, CONF_BACKUP_EXT);
2472
2473 config_file_tmp = malloc(strlen(config_file) + 8);
2474 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2475
2476 /* Open file to configuration write. */
2477 fd = mkstemp(config_file_tmp);
2478 if (fd < 0) {
2479 vty_out(vty, "Can't open configuration file %s.%s",
2480 config_file_tmp, VTY_NEWLINE);
2481 free(config_file_tmp);
2482 free(config_file_sav);
2483 return CMD_WARNING;
2484 }
2485
2486 /* Make vty for configuration file. */
2487 file_vty = vty_new();
2488 file_vty->fd = fd;
2489 file_vty->type = VTY_FILE;
2490
2491 /* Config file header print. */
2492 vty_out(file_vty, "!\n! Zebra configuration saved from vty\n! ");
2493 //vty_time_print (file_vty, 1);
2494 vty_out(file_vty, "!\n");
2495
2496 for (i = 0; i < vector_active(cmdvec); i++)
2497 if ((node = vector_slot(cmdvec, i)) && node->func) {
2498 if ((*node->func) (file_vty))
2499 vty_out(file_vty, "!\n");
2500 }
2501 vty_close(file_vty);
2502
2503 if (unlink(config_file_sav) != 0)
2504 if (errno != ENOENT) {
2505 vty_out(vty,
2506 "Can't unlink backup configuration file %s.%s",
2507 config_file_sav, VTY_NEWLINE);
2508 free(config_file_sav);
2509 free(config_file_tmp);
2510 unlink(config_file_tmp);
2511 return CMD_WARNING;
2512 }
2513 if (link(config_file, config_file_sav) != 0) {
2514 vty_out(vty, "Can't backup old configuration file %s.%s",
2515 config_file_sav, VTY_NEWLINE);
2516 free(config_file_sav);
2517 free(config_file_tmp);
2518 unlink(config_file_tmp);
2519 return CMD_WARNING;
2520 }
2521 sync();
2522 if (unlink(config_file) != 0) {
2523 vty_out(vty, "Can't unlink configuration file %s.%s",
2524 config_file, VTY_NEWLINE);
2525 free(config_file_sav);
2526 free(config_file_tmp);
2527 unlink(config_file_tmp);
2528 return CMD_WARNING;
2529 }
2530 if (link(config_file_tmp, config_file) != 0) {
2531 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2532 VTY_NEWLINE);
2533 free(config_file_sav);
2534 free(config_file_tmp);
2535 unlink(config_file_tmp);
2536 return CMD_WARNING;
2537 }
2538 unlink(config_file_tmp);
2539 sync();
2540
2541 free(config_file_sav);
2542 free(config_file_tmp);
2543
2544 if (chmod(config_file, CONFIGFILE_MASK) != 0) {
2545 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2546 config_file, safe_strerror(errno), errno, VTY_NEWLINE);
2547 return CMD_WARNING;
2548 }
2549
2550 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2551 return CMD_SUCCESS;
2552}
2553
2554ALIAS(config_write_file,
2555 config_write_cmd,
2556 "write", "Write running configuration to memory, network, or terminal\n")
2557
2558 ALIAS(config_write_file,
2559 config_write_memory_cmd,
2560 "write memory",
2561 "Write running configuration to memory, network, or terminal\n"
2562 "Write configuration to the file (same as write file)\n")
2563
2564 ALIAS(config_write_file,
2565 copy_runningconfig_startupconfig_cmd,
2566 "copy running-config startup-config",
2567 "Copy configuration\n"
2568 "Copy running config to... \n"
2569 "Copy running config to startup config (same as write file)\n")
2570
2571/* Write current configuration into the terminal. */
2572 DEFUN(config_write_terminal,
2573 config_write_terminal_cmd,
2574 "write terminal",
2575 "Write running configuration to memory, network, or terminal\n"
2576 "Write to terminal\n")
2577{
2578 unsigned int i;
2579 struct cmd_node *node;
2580
2581 if (vty->type == VTY_SHELL_SERV) {
2582 for (i = 0; i < vector_active(cmdvec); i++)
2583 if ((node = vector_slot(cmdvec, i)) && node->func
2584 && node->vtysh) {
2585 if ((*node->func) (vty))
2586 vty_out(vty, "!%s", VTY_NEWLINE);
2587 }
2588 } else {
2589 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2590 VTY_NEWLINE);
2591 vty_out(vty, "!%s", VTY_NEWLINE);
2592
2593 for (i = 0; i < vector_active(cmdvec); i++)
2594 if ((node = vector_slot(cmdvec, i)) && node->func) {
2595 if ((*node->func) (vty))
2596 vty_out(vty, "!%s", VTY_NEWLINE);
2597 }
2598 vty_out(vty, "end%s", VTY_NEWLINE);
2599 }
2600 return CMD_SUCCESS;
2601}
2602
2603/* Write current configuration into the terminal. */
2604ALIAS(config_write_terminal,
2605 show_running_config_cmd,
2606 "show running-config", SHOW_STR "running configuration\n")
2607
2608/* Write startup configuration into the terminal. */
2609 DEFUN(show_startup_config,
2610 show_startup_config_cmd,
2611 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2612{
2613 char buf[BUFSIZ];
2614 FILE *confp;
2615
2616 confp = fopen(host.config, "r");
2617 if (confp == NULL) {
2618 vty_out(vty, "Can't open configuration file [%s]%s",
2619 host.config, VTY_NEWLINE);
2620 return CMD_WARNING;
2621 }
2622
2623 while (fgets(buf, BUFSIZ, confp)) {
2624 char *cp = buf;
2625
2626 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2627 cp++;
2628 *cp = '\0';
2629
2630 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2631 }
2632
2633 fclose(confp);
2634
2635 return CMD_SUCCESS;
2636}
2637#endif
2638
2639/* Hostname configuration */
2640DEFUN(config_hostname,
2641 hostname_cmd,
2642 "hostname WORD",
2643 "Set system's network name\n" "This system's network name\n")
2644{
2645 if (!isalpha((int)*argv[0])) {
2646 vty_out(vty, "Please specify string starting with alphabet%s",
2647 VTY_NEWLINE);
2648 return CMD_WARNING;
2649 }
2650
2651 if (host.name)
2652 free(host.name);
2653
2654 host.name = strdup(argv[0]);
2655 return CMD_SUCCESS;
2656}
2657
2658DEFUN(config_no_hostname,
2659 no_hostname_cmd,
2660 "no hostname [HOSTNAME]",
2661 NO_STR "Reset system's network name\n" "Host name of this router\n")
2662{
2663 if (host.name)
2664 free(host.name);
2665 host.name = NULL;
2666 return CMD_SUCCESS;
2667}
2668
2669/* VTY interface password set. */
2670DEFUN(config_password, password_cmd,
2671 "password (8|) WORD",
2672 "Assign the terminal connection password\n"
2673 "Specifies a HIDDEN password will follow\n"
2674 "dummy string \n" "The HIDDEN line password string\n")
2675{
2676 /* Argument check. */
2677 if (argc == 0) {
2678 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2679 return CMD_WARNING;
2680 }
2681
2682 if (argc == 2) {
2683 if (*argv[0] == '8') {
2684 if (host.password)
2685 free(host.password);
2686 host.password = NULL;
2687 if (host.password_encrypt)
2688 free(host.password_encrypt);
2689 host.password_encrypt = strdup(strdup(argv[1]));
2690 return CMD_SUCCESS;
2691 } else {
2692 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2693 return CMD_WARNING;
2694 }
2695 }
2696
2697 if (!isalnum((int)*argv[0])) {
2698 vty_out(vty,
2699 "Please specify string starting with alphanumeric%s",
2700 VTY_NEWLINE);
2701 return CMD_WARNING;
2702 }
2703
2704 if (host.password)
2705 free(host.password);
2706 host.password = NULL;
2707
2708 if (host.encrypt) {
2709 if (host.password_encrypt)
2710 free(host.password_encrypt);
2711 host.password_encrypt = strdup(zencrypt(argv[0]));
2712 } else
2713 host.password = strdup(argv[0]);
2714
2715 return CMD_SUCCESS;
2716}
2717
2718ALIAS(config_password, password_text_cmd,
2719 "password LINE",
2720 "Assign the terminal connection password\n"
2721 "The UNENCRYPTED (cleartext) line password\n")
2722
2723/* VTY enable password set. */
2724 DEFUN(config_enable_password, enable_password_cmd,
2725 "enable password (8|) WORD",
2726 "Modify enable password parameters\n"
2727 "Assign the privileged level password\n"
2728 "Specifies a HIDDEN password will follow\n"
2729 "dummy string \n" "The HIDDEN 'enable' password string\n")
2730{
2731 /* Argument check. */
2732 if (argc == 0) {
2733 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2734 return CMD_WARNING;
2735 }
2736
2737 /* Crypt type is specified. */
2738 if (argc == 2) {
2739 if (*argv[0] == '8') {
2740 if (host.enable)
2741 free(host.enable);
2742 host.enable = NULL;
2743
2744 if (host.enable_encrypt)
2745 free(host.enable_encrypt);
2746 host.enable_encrypt = strdup(argv[1]);
2747
2748 return CMD_SUCCESS;
2749 } else {
2750 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2751 return CMD_WARNING;
2752 }
2753 }
2754
2755 if (!isalnum((int)*argv[0])) {
2756 vty_out(vty,
2757 "Please specify string starting with alphanumeric%s",
2758 VTY_NEWLINE);
2759 return CMD_WARNING;
2760 }
2761
2762 if (host.enable)
2763 free(host.enable);
2764 host.enable = NULL;
2765
2766 /* Plain password input. */
2767 if (host.encrypt) {
2768 if (host.enable_encrypt)
2769 free(host.enable_encrypt);
2770 host.enable_encrypt = strdup(zencrypt(argv[0]));
2771 } else
2772 host.enable = strdup(argv[0]);
2773
2774 return CMD_SUCCESS;
2775}
2776
2777ALIAS(config_enable_password,
2778 enable_password_text_cmd,
2779 "enable password LINE",
2780 "Modify enable password parameters\n"
2781 "Assign the privileged level password\n"
2782 "The UNENCRYPTED (cleartext) 'enable' password\n")
2783
2784/* VTY enable password delete. */
2785 DEFUN(no_config_enable_password, no_enable_password_cmd,
2786 "no enable password",
2787 NO_STR
2788 "Modify enable password parameters\n"
2789 "Assign the privileged level password\n")
2790{
2791 if (host.enable)
2792 free(host.enable);
2793 host.enable = NULL;
2794
2795 if (host.enable_encrypt)
2796 free(host.enable_encrypt);
2797 host.enable_encrypt = NULL;
2798
2799 return CMD_SUCCESS;
2800}
2801
2802DEFUN(service_password_encrypt,
2803 service_password_encrypt_cmd,
2804 "service password-encryption",
2805 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2806{
2807 if (host.encrypt)
2808 return CMD_SUCCESS;
2809
2810 host.encrypt = 1;
2811
2812 if (host.password) {
2813 if (host.password_encrypt)
2814 free(host.password_encrypt);
2815 host.password_encrypt = strdup(zencrypt(host.password));
2816 }
2817 if (host.enable) {
2818 if (host.enable_encrypt)
2819 free(host.enable_encrypt);
2820 host.enable_encrypt = strdup(zencrypt(host.enable));
2821 }
2822
2823 return CMD_SUCCESS;
2824}
2825
2826DEFUN(no_service_password_encrypt,
2827 no_service_password_encrypt_cmd,
2828 "no service password-encryption",
2829 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2830{
2831 if (!host.encrypt)
2832 return CMD_SUCCESS;
2833
2834 host.encrypt = 0;
2835
2836 if (host.password_encrypt)
2837 free(host.password_encrypt);
2838 host.password_encrypt = NULL;
2839
2840 if (host.enable_encrypt)
2841 free(host.enable_encrypt);
2842 host.enable_encrypt = NULL;
2843
2844 return CMD_SUCCESS;
2845}
2846
2847DEFUN(config_terminal_length, config_terminal_length_cmd,
2848 "terminal length <0-512>",
2849 "Set terminal line parameters\n"
2850 "Set number of lines on a screen\n"
2851 "Number of lines on screen (0 for no pausing)\n")
2852{
2853 int lines;
2854 char *endptr = NULL;
2855
2856 lines = strtol(argv[0], &endptr, 10);
2857 if (lines < 0 || lines > 512 || *endptr != '\0') {
2858 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2859 return CMD_WARNING;
2860 }
2861 vty->lines = lines;
2862
2863 return CMD_SUCCESS;
2864}
2865
2866DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2867 "terminal no length",
2868 "Set terminal line parameters\n"
2869 NO_STR "Set number of lines on a screen\n")
2870{
2871 vty->lines = -1;
2872 return CMD_SUCCESS;
2873}
2874
2875DEFUN(service_terminal_length, service_terminal_length_cmd,
2876 "service terminal-length <0-512>",
2877 "Set up miscellaneous service\n"
2878 "System wide terminal length configuration\n"
2879 "Number of lines of VTY (0 means no line control)\n")
2880{
2881 int lines;
2882 char *endptr = NULL;
2883
2884 lines = strtol(argv[0], &endptr, 10);
2885 if (lines < 0 || lines > 512 || *endptr != '\0') {
2886 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2887 return CMD_WARNING;
2888 }
2889 host.lines = lines;
2890
2891 return CMD_SUCCESS;
2892}
2893
2894DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2895 "no service terminal-length [<0-512>]",
2896 NO_STR
2897 "Set up miscellaneous service\n"
2898 "System wide terminal length configuration\n"
2899 "Number of lines of VTY (0 means no line control)\n")
2900{
2901 host.lines = -1;
2902 return CMD_SUCCESS;
2903}
2904
2905DEFUN_HIDDEN(do_echo,
2906 echo_cmd,
2907 "echo .MESSAGE",
2908 "Echo a message back to the vty\n" "The message to echo\n")
2909{
2910 char *message;
2911
2912 vty_out(vty, "%s%s",
2913 ((message =
2914 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2915 if (message)
2916 free(message);
2917 return CMD_SUCCESS;
2918}
2919
2920#if 0
2921DEFUN(config_logmsg,
2922 config_logmsg_cmd,
2923 "logmsg " LOG_LEVELS " .MESSAGE",
2924 "Send a message to enabled logging destinations\n"
2925 LOG_LEVEL_DESC "The message to send\n")
2926{
2927 int level;
2928 char *message;
2929
2930 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2931 return CMD_ERR_NO_MATCH;
2932
2933 zlog(NULL, level,
2934 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2935 if (message)
2936 free(message);
2937 return CMD_SUCCESS;
2938}
2939
2940DEFUN(show_logging,
2941 show_logging_cmd,
2942 "show logging", SHOW_STR "Show current logging configuration\n")
2943{
2944 struct zlog *zl = zlog_default;
2945
2946 vty_out(vty, "Syslog logging: ");
2947 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2948 vty_out(vty, "disabled");
2949 else
2950 vty_out(vty, "level %s, facility %s, ident %s",
2951 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2952 facility_name(zl->facility), zl->ident);
2953 vty_out(vty, "%s", VTY_NEWLINE);
2954
2955 vty_out(vty, "Stdout logging: ");
2956 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2957 vty_out(vty, "disabled");
2958 else
2959 vty_out(vty, "level %s",
2960 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2961 vty_out(vty, "%s", VTY_NEWLINE);
2962
2963 vty_out(vty, "Monitor logging: ");
2964 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2965 vty_out(vty, "disabled");
2966 else
2967 vty_out(vty, "level %s",
2968 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2969 vty_out(vty, "%s", VTY_NEWLINE);
2970
2971 vty_out(vty, "File logging: ");
2972 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2973 vty_out(vty, "disabled");
2974 else
2975 vty_out(vty, "level %s, filename %s",
2976 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2977 zl->filename);
2978 vty_out(vty, "%s", VTY_NEWLINE);
2979
2980 vty_out(vty, "Protocol name: %s%s",
2981 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2982 vty_out(vty, "Record priority: %s%s",
2983 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2984
2985 return CMD_SUCCESS;
2986}
2987
2988DEFUN(config_log_stdout,
2989 config_log_stdout_cmd,
2990 "log stdout", "Logging control\n" "Set stdout logging level\n")
2991{
2992 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2993 return CMD_SUCCESS;
2994}
2995
2996DEFUN(config_log_stdout_level,
2997 config_log_stdout_level_cmd,
2998 "log stdout " LOG_LEVELS,
2999 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3000{
3001 int level;
3002
3003 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3004 return CMD_ERR_NO_MATCH;
3005 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3006 return CMD_SUCCESS;
3007}
3008
3009DEFUN(no_config_log_stdout,
3010 no_config_log_stdout_cmd,
3011 "no log stdout [LEVEL]",
3012 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3013{
3014 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3015 return CMD_SUCCESS;
3016}
3017
3018DEFUN(config_log_monitor,
3019 config_log_monitor_cmd,
3020 "log monitor",
3021 "Logging control\n" "Set terminal line (monitor) logging level\n")
3022{
3023 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3024 return CMD_SUCCESS;
3025}
3026
3027DEFUN(config_log_monitor_level,
3028 config_log_monitor_level_cmd,
3029 "log monitor " LOG_LEVELS,
3030 "Logging control\n"
3031 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3032{
3033 int level;
3034
3035 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3036 return CMD_ERR_NO_MATCH;
3037 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3038 return CMD_SUCCESS;
3039}
3040
3041DEFUN(no_config_log_monitor,
3042 no_config_log_monitor_cmd,
3043 "no log monitor [LEVEL]",
3044 NO_STR
3045 "Logging control\n"
3046 "Disable terminal line (monitor) logging\n" "Logging level\n")
3047{
3048 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3049 return CMD_SUCCESS;
3050}
3051
3052static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3053{
3054 int ret;
3055 char *p = NULL;
3056 const char *fullpath;
3057
3058 /* Path detection. */
3059 if (!IS_DIRECTORY_SEP(*fname)) {
3060 char cwd[MAXPATHLEN + 1];
3061 cwd[MAXPATHLEN] = '\0';
3062
3063 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3064 zlog_err("config_log_file: Unable to alloc mem!");
3065 return CMD_WARNING;
3066 }
3067
3068 if ((p = malloc(strlen(cwd) + strlen(fname) + 2))
3069 == NULL) {
3070 zlog_err("config_log_file: Unable to alloc mem!");
3071 return CMD_WARNING;
3072 }
3073 sprintf(p, "%s/%s", cwd, fname);
3074 fullpath = p;
3075 } else
3076 fullpath = fname;
3077
3078 ret = zlog_set_file(NULL, fullpath, loglevel);
3079
3080 if (p)
3081 free(p);
3082
3083 if (!ret) {
3084 vty_out(vty, "can't open logfile %s\n", fname);
3085 return CMD_WARNING;
3086 }
3087
3088 if (host.logfile)
3089 free(host.logfile);
3090
3091 host.logfile = strdup(fname);
3092
3093 return CMD_SUCCESS;
3094}
3095
3096DEFUN(config_log_file,
3097 config_log_file_cmd,
3098 "log file FILENAME",
3099 "Logging control\n" "Logging to file\n" "Logging filename\n")
3100{
3101 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3102}
3103
3104DEFUN(config_log_file_level,
3105 config_log_file_level_cmd,
3106 "log file FILENAME " LOG_LEVELS,
3107 "Logging control\n"
3108 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3109{
3110 int level;
3111
3112 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3113 return CMD_ERR_NO_MATCH;
3114 return set_log_file(vty, argv[0], level);
3115}
3116
3117DEFUN(no_config_log_file,
3118 no_config_log_file_cmd,
3119 "no log file [FILENAME]",
3120 NO_STR
3121 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3122{
3123 zlog_reset_file(NULL);
3124
3125 if (host.logfile)
3126 free(host.logfile);
3127
3128 host.logfile = NULL;
3129
3130 return CMD_SUCCESS;
3131}
3132
3133ALIAS(no_config_log_file,
3134 no_config_log_file_level_cmd,
3135 "no log file FILENAME LEVEL",
3136 NO_STR
3137 "Logging control\n"
3138 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3139
3140 DEFUN(config_log_syslog,
3141 config_log_syslog_cmd,
3142 "log syslog", "Logging control\n" "Set syslog logging level\n")
3143{
3144 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3145 return CMD_SUCCESS;
3146}
3147
3148DEFUN(config_log_syslog_level,
3149 config_log_syslog_level_cmd,
3150 "log syslog " LOG_LEVELS,
3151 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3152{
3153 int level;
3154
3155 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3156 return CMD_ERR_NO_MATCH;
3157 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3158 return CMD_SUCCESS;
3159}
3160
3161DEFUN_DEPRECATED(config_log_syslog_facility,
3162 config_log_syslog_facility_cmd,
3163 "log syslog facility " LOG_FACILITIES,
3164 "Logging control\n"
3165 "Logging goes to syslog\n"
3166 "(Deprecated) Facility parameter for syslog messages\n"
3167 LOG_FACILITY_DESC)
3168{
3169 int facility;
3170
3171 if ((facility = facility_match(argv[0])) < 0)
3172 return CMD_ERR_NO_MATCH;
3173
3174 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3175 zlog_default->facility = facility;
3176 return CMD_SUCCESS;
3177}
3178
3179DEFUN(no_config_log_syslog,
3180 no_config_log_syslog_cmd,
3181 "no log syslog [LEVEL]",
3182 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3183{
3184 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3185 return CMD_SUCCESS;
3186}
3187
3188ALIAS(no_config_log_syslog,
3189 no_config_log_syslog_facility_cmd,
3190 "no log syslog facility " LOG_FACILITIES,
3191 NO_STR
3192 "Logging control\n"
3193 "Logging goes to syslog\n"
3194 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3195
3196 DEFUN(config_log_facility,
3197 config_log_facility_cmd,
3198 "log facility " LOG_FACILITIES,
3199 "Logging control\n"
3200 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3201{
3202 int facility;
3203
3204 if ((facility = facility_match(argv[0])) < 0)
3205 return CMD_ERR_NO_MATCH;
3206 zlog_default->facility = facility;
3207 return CMD_SUCCESS;
3208}
3209
3210DEFUN(no_config_log_facility,
3211 no_config_log_facility_cmd,
3212 "no log facility [FACILITY]",
3213 NO_STR
3214 "Logging control\n"
3215 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3216{
3217 zlog_default->facility = LOG_DAEMON;
3218 return CMD_SUCCESS;
3219}
3220
3221DEFUN_DEPRECATED(config_log_trap,
3222 config_log_trap_cmd,
3223 "log trap " LOG_LEVELS,
3224 "Logging control\n"
3225 "(Deprecated) Set logging level and default for all destinations\n"
3226 LOG_LEVEL_DESC)
3227{
3228 int new_level;
3229 int i;
3230
3231 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3232 return CMD_ERR_NO_MATCH;
3233
3234 zlog_default->default_lvl = new_level;
3235 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3236 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3237 zlog_default->maxlvl[i] = new_level;
3238 return CMD_SUCCESS;
3239}
3240
3241DEFUN_DEPRECATED(no_config_log_trap,
3242 no_config_log_trap_cmd,
3243 "no log trap [LEVEL]",
3244 NO_STR
3245 "Logging control\n"
3246 "Permit all logging information\n" "Logging level\n")
3247{
3248 zlog_default->default_lvl = LOG_DEBUG;
3249 return CMD_SUCCESS;
3250}
3251
3252DEFUN(config_log_record_priority,
3253 config_log_record_priority_cmd,
3254 "log record-priority",
3255 "Logging control\n"
3256 "Log the priority of the message within the message\n")
3257{
3258 zlog_default->record_priority = 1;
3259 return CMD_SUCCESS;
3260}
3261
3262DEFUN(no_config_log_record_priority,
3263 no_config_log_record_priority_cmd,
3264 "no log record-priority",
3265 NO_STR
3266 "Logging control\n"
3267 "Do not log the priority of the message within the message\n")
3268{
3269 zlog_default->record_priority = 0;
3270 return CMD_SUCCESS;
3271}
3272#endif
3273
3274DEFUN(banner_motd_file,
3275 banner_motd_file_cmd,
3276 "banner motd file [FILE]",
3277 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3278{
3279 if (host.motdfile)
3280 free(host.motdfile);
3281 host.motdfile = strdup(argv[0]);
3282
3283 return CMD_SUCCESS;
3284}
3285
3286DEFUN(banner_motd_default,
3287 banner_motd_default_cmd,
3288 "banner motd default",
3289 "Set banner string\n" "Strings for motd\n" "Default string\n")
3290{
3291 host.motd = default_motd;
3292 return CMD_SUCCESS;
3293}
3294
3295DEFUN(no_banner_motd,
3296 no_banner_motd_cmd,
3297 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3298{
3299 host.motd = NULL;
3300 if (host.motdfile)
3301 free(host.motdfile);
3302 host.motdfile = NULL;
3303 return CMD_SUCCESS;
3304}
3305
3306/* Set config filename. Called from vty.c */
3307void host_config_set(char *filename)
3308{
3309 host.config = strdup(filename);
3310}
3311
3312void install_default(enum node_type node)
3313{
3314 install_element(node, &config_exit_cmd);
3315 install_element(node, &config_quit_cmd);
3316 install_element(node, &config_end_cmd);
3317 install_element(node, &config_help_cmd);
3318 install_element(node, &config_list_cmd);
3319
3320#if 0
3321 install_element(node, &config_write_terminal_cmd);
3322 install_element(node, &config_write_file_cmd);
3323 install_element(node, &config_write_memory_cmd);
3324 install_element(node, &config_write_cmd);
3325 install_element(node, &show_running_config_cmd);
3326#endif
3327}
3328
3329/* Initialize command interface. Install basic nodes and commands. */
3330void cmd_init(int terminal)
3331{
3332 /* Allocate initial top vector of commands. */
3333 cmdvec = vector_init(VECTOR_MIN_SIZE);
3334
3335 /* Default host value settings. */
3336 host.name = NULL;
3337 //host.password = NULL;
3338 host.password = "foo";
3339 host.enable = NULL;
3340 host.logfile = NULL;
3341 host.config = NULL;
3342 host.lines = -1;
3343 host.motd = default_motd;
3344 host.motdfile = NULL;
3345
3346 /* Install top nodes. */
3347 install_node(&view_node, NULL);
3348 install_node(&enable_node, NULL);
3349 install_node(&auth_node, NULL);
3350 install_node(&auth_enable_node, NULL);
3351 install_node(&config_node, config_write_host);
3352
3353 /* Each node's basic commands. */
3354 install_element(VIEW_NODE, &show_version_cmd);
3355 if (terminal) {
3356 install_element(VIEW_NODE, &config_list_cmd);
3357 install_element(VIEW_NODE, &config_exit_cmd);
3358 install_element(VIEW_NODE, &config_quit_cmd);
3359 install_element(VIEW_NODE, &config_help_cmd);
3360 install_element(VIEW_NODE, &config_enable_cmd);
3361 install_element(VIEW_NODE, &config_terminal_length_cmd);
3362 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3363 install_element(VIEW_NODE, &echo_cmd);
3364 }
3365
3366 if (terminal) {
3367 install_default(ENABLE_NODE);
3368 install_element(ENABLE_NODE, &config_disable_cmd);
3369 install_element(ENABLE_NODE, &config_terminal_cmd);
3370 //install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3371 }
3372 //install_element (ENABLE_NODE, &show_startup_config_cmd);
3373 install_element(ENABLE_NODE, &show_version_cmd);
3374
3375 if (terminal) {
3376 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3377 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3378 install_element(ENABLE_NODE, &echo_cmd);
3379
3380 install_default(CONFIG_NODE);
3381 }
3382
3383 install_element(CONFIG_NODE, &hostname_cmd);
3384 install_element(CONFIG_NODE, &no_hostname_cmd);
3385
3386 if (terminal) {
3387 install_element(CONFIG_NODE, &password_cmd);
3388 install_element(CONFIG_NODE, &password_text_cmd);
3389 install_element(CONFIG_NODE, &enable_password_cmd);
3390 install_element(CONFIG_NODE, &enable_password_text_cmd);
3391 install_element(CONFIG_NODE, &no_enable_password_cmd);
3392
3393 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3394 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3395 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3396 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3397 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3398 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3399 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3400
3401 }
3402 srand(time(NULL));
3403}