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