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