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