blob: 16e0dfb36408f544913bba505a5477715d02ce61 [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{
Harald Welte955049f2009-03-10 12:16:51 +0000510 if (host.name)
511 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
512
513 if (host.encrypt) {
514 if (host.password_encrypt)
515 vty_out(vty, "password 8 %s%s", host.password_encrypt,
516 VTY_NEWLINE);
517 if (host.enable_encrypt)
518 vty_out(vty, "enable password 8 %s%s",
519 host.enable_encrypt, VTY_NEWLINE);
520 } else {
521 if (host.password)
522 vty_out(vty, "password %s%s", host.password,
523 VTY_NEWLINE);
524 if (host.enable)
525 vty_out(vty, "enable password %s%s", host.enable,
526 VTY_NEWLINE);
527 }
528
Harald Weltec7c19822009-08-07 13:28:08 +0200529#if 0
Harald Welte955049f2009-03-10 12:16:51 +0000530 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);
Harald Weltec7c19822009-08-07 13:28:08 +0200582#endif
Harald Welte955049f2009-03-10 12:16:51 +0000583 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
Harald Welte955049f2009-03-10 12:16:51 +0000599 return 1;
600}
601
602/* Utility function for getting command vector. */
603static vector cmd_node_vector(vector v, enum node_type ntype)
604{
605 struct cmd_node *cnode = vector_slot(v, ntype);
606 return cnode->cmd_vector;
607}
608
609#if 0
610/* Filter command vector by symbol. This function is not actually used;
611 * should it be deleted? */
612static int cmd_filter_by_symbol(char *command, char *symbol)
613{
614 int i, lim;
615
616 if (strcmp(symbol, "IPV4_ADDRESS") == 0) {
617 i = 0;
618 lim = strlen(command);
619 while (i < lim) {
620 if (!
621 (isdigit((int)command[i]) || command[i] == '.'
622 || command[i] == '/'))
623 return 1;
624 i++;
625 }
626 return 0;
627 }
628 if (strcmp(symbol, "STRING") == 0) {
629 i = 0;
630 lim = strlen(command);
631 while (i < lim) {
632 if (!
633 (isalpha((int)command[i]) || command[i] == '_'
634 || command[i] == '-'))
635 return 1;
636 i++;
637 }
638 return 0;
639 }
640 if (strcmp(symbol, "IFNAME") == 0) {
641 i = 0;
642 lim = strlen(command);
643 while (i < lim) {
644 if (!isalnum((int)command[i]))
645 return 1;
646 i++;
647 }
648 return 0;
649 }
650 return 0;
651}
652#endif
653
654/* Completion match types. */
655enum match_type {
656 no_match,
657 extend_match,
658 ipv4_prefix_match,
659 ipv4_match,
660 ipv6_prefix_match,
661 ipv6_match,
662 range_match,
663 vararg_match,
664 partly_match,
665 exact_match
666};
667
668static enum match_type cmd_ipv4_match(const char *str)
669{
670 const char *sp;
671 int dots = 0, nums = 0;
672 char buf[4];
673
674 if (str == NULL)
675 return partly_match;
676
677 for (;;) {
678 memset(buf, 0, sizeof(buf));
679 sp = str;
680 while (*str != '\0') {
681 if (*str == '.') {
682 if (dots >= 3)
683 return no_match;
684
685 if (*(str + 1) == '.')
686 return no_match;
687
688 if (*(str + 1) == '\0')
689 return partly_match;
690
691 dots++;
692 break;
693 }
694 if (!isdigit((int)*str))
695 return no_match;
696
697 str++;
698 }
699
700 if (str - sp > 3)
701 return no_match;
702
703 strncpy(buf, sp, str - sp);
704 if (atoi(buf) > 255)
705 return no_match;
706
707 nums++;
708
709 if (*str == '\0')
710 break;
711
712 str++;
713 }
714
715 if (nums < 4)
716 return partly_match;
717
718 return exact_match;
719}
720
721static enum match_type cmd_ipv4_prefix_match(const char *str)
722{
723 const char *sp;
724 int dots = 0;
725 char buf[4];
726
727 if (str == NULL)
728 return partly_match;
729
730 for (;;) {
731 memset(buf, 0, sizeof(buf));
732 sp = str;
733 while (*str != '\0' && *str != '/') {
734 if (*str == '.') {
735 if (dots == 3)
736 return no_match;
737
738 if (*(str + 1) == '.' || *(str + 1) == '/')
739 return no_match;
740
741 if (*(str + 1) == '\0')
742 return partly_match;
743
744 dots++;
745 break;
746 }
747
748 if (!isdigit((int)*str))
749 return no_match;
750
751 str++;
752 }
753
754 if (str - sp > 3)
755 return no_match;
756
757 strncpy(buf, sp, str - sp);
758 if (atoi(buf) > 255)
759 return no_match;
760
761 if (dots == 3) {
762 if (*str == '/') {
763 if (*(str + 1) == '\0')
764 return partly_match;
765
766 str++;
767 break;
768 } else if (*str == '\0')
769 return partly_match;
770 }
771
772 if (*str == '\0')
773 return partly_match;
774
775 str++;
776 }
777
778 sp = str;
779 while (*str != '\0') {
780 if (!isdigit((int)*str))
781 return no_match;
782
783 str++;
784 }
785
786 if (atoi(sp) > 32)
787 return no_match;
788
789 return exact_match;
790}
791
792#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
793#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
794#define STATE_START 1
795#define STATE_COLON 2
796#define STATE_DOUBLE 3
797#define STATE_ADDR 4
798#define STATE_DOT 5
799#define STATE_SLASH 6
800#define STATE_MASK 7
801
802#ifdef HAVE_IPV6
803
804static enum match_type cmd_ipv6_match(const char *str)
805{
806 int state = STATE_START;
807 int colons = 0, nums = 0, double_colon = 0;
808 const char *sp = NULL;
809 struct sockaddr_in6 sin6_dummy;
810 int ret;
811
812 if (str == NULL)
813 return partly_match;
814
815 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
816 return no_match;
817
818 /* use inet_pton that has a better support,
819 * for example inet_pton can support the automatic addresses:
820 * ::1.2.3.4
821 */
822 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
823
824 if (ret == 1)
825 return exact_match;
826
827 while (*str != '\0') {
828 switch (state) {
829 case STATE_START:
830 if (*str == ':') {
831 if (*(str + 1) != ':' && *(str + 1) != '\0')
832 return no_match;
833 colons--;
834 state = STATE_COLON;
835 } else {
836 sp = str;
837 state = STATE_ADDR;
838 }
839
840 continue;
841 case STATE_COLON:
842 colons++;
843 if (*(str + 1) == ':')
844 state = STATE_DOUBLE;
845 else {
846 sp = str + 1;
847 state = STATE_ADDR;
848 }
849 break;
850 case STATE_DOUBLE:
851 if (double_colon)
852 return no_match;
853
854 if (*(str + 1) == ':')
855 return no_match;
856 else {
857 if (*(str + 1) != '\0')
858 colons++;
859 sp = str + 1;
860 state = STATE_ADDR;
861 }
862
863 double_colon++;
864 nums++;
865 break;
866 case STATE_ADDR:
867 if (*(str + 1) == ':' || *(str + 1) == '\0') {
868 if (str - sp > 3)
869 return no_match;
870
871 nums++;
872 state = STATE_COLON;
873 }
874 if (*(str + 1) == '.')
875 state = STATE_DOT;
876 break;
877 case STATE_DOT:
878 state = STATE_ADDR;
879 break;
880 default:
881 break;
882 }
883
884 if (nums > 8)
885 return no_match;
886
887 if (colons > 7)
888 return no_match;
889
890 str++;
891 }
892
893#if 0
894 if (nums < 11)
895 return partly_match;
896#endif /* 0 */
897
898 return exact_match;
899}
900
901static enum match_type cmd_ipv6_prefix_match(const char *str)
902{
903 int state = STATE_START;
904 int colons = 0, nums = 0, double_colon = 0;
905 int mask;
906 const char *sp = NULL;
907 char *endptr = NULL;
908
909 if (str == NULL)
910 return partly_match;
911
912 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
913 return no_match;
914
915 while (*str != '\0' && state != STATE_MASK) {
916 switch (state) {
917 case STATE_START:
918 if (*str == ':') {
919 if (*(str + 1) != ':' && *(str + 1) != '\0')
920 return no_match;
921 colons--;
922 state = STATE_COLON;
923 } else {
924 sp = str;
925 state = STATE_ADDR;
926 }
927
928 continue;
929 case STATE_COLON:
930 colons++;
931 if (*(str + 1) == '/')
932 return no_match;
933 else if (*(str + 1) == ':')
934 state = STATE_DOUBLE;
935 else {
936 sp = str + 1;
937 state = STATE_ADDR;
938 }
939 break;
940 case STATE_DOUBLE:
941 if (double_colon)
942 return no_match;
943
944 if (*(str + 1) == ':')
945 return no_match;
946 else {
947 if (*(str + 1) != '\0' && *(str + 1) != '/')
948 colons++;
949 sp = str + 1;
950
951 if (*(str + 1) == '/')
952 state = STATE_SLASH;
953 else
954 state = STATE_ADDR;
955 }
956
957 double_colon++;
958 nums += 1;
959 break;
960 case STATE_ADDR:
961 if (*(str + 1) == ':' || *(str + 1) == '.'
962 || *(str + 1) == '\0' || *(str + 1) == '/') {
963 if (str - sp > 3)
964 return no_match;
965
966 for (; sp <= str; sp++)
967 if (*sp == '/')
968 return no_match;
969
970 nums++;
971
972 if (*(str + 1) == ':')
973 state = STATE_COLON;
974 else if (*(str + 1) == '.')
975 state = STATE_DOT;
976 else if (*(str + 1) == '/')
977 state = STATE_SLASH;
978 }
979 break;
980 case STATE_DOT:
981 state = STATE_ADDR;
982 break;
983 case STATE_SLASH:
984 if (*(str + 1) == '\0')
985 return partly_match;
986
987 state = STATE_MASK;
988 break;
989 default:
990 break;
991 }
992
993 if (nums > 11)
994 return no_match;
995
996 if (colons > 7)
997 return no_match;
998
999 str++;
1000 }
1001
1002 if (state < STATE_MASK)
1003 return partly_match;
1004
1005 mask = strtol(str, &endptr, 10);
1006 if (*endptr != '\0')
1007 return no_match;
1008
1009 if (mask < 0 || mask > 128)
1010 return no_match;
1011
1012/* I don't know why mask < 13 makes command match partly.
1013 Forgive me to make this comments. I Want to set static default route
1014 because of lack of function to originate default in ospf6d; sorry
1015 yasu
1016 if (mask < 13)
1017 return partly_match;
1018*/
1019
1020 return exact_match;
1021}
1022
1023#endif /* HAVE_IPV6 */
1024
1025#define DECIMAL_STRLEN_MAX 10
1026
1027static int cmd_range_match(const char *range, const char *str)
1028{
1029 char *p;
1030 char buf[DECIMAL_STRLEN_MAX + 1];
1031 char *endptr = NULL;
1032 unsigned long min, max, val;
1033
1034 if (str == NULL)
1035 return 1;
1036
1037 val = strtoul(str, &endptr, 10);
1038 if (*endptr != '\0')
1039 return 0;
1040
1041 range++;
1042 p = strchr(range, '-');
1043 if (p == NULL)
1044 return 0;
1045 if (p - range > DECIMAL_STRLEN_MAX)
1046 return 0;
1047 strncpy(buf, range, p - range);
1048 buf[p - range] = '\0';
1049 min = strtoul(buf, &endptr, 10);
1050 if (*endptr != '\0')
1051 return 0;
1052
1053 range = p + 1;
1054 p = strchr(range, '>');
1055 if (p == NULL)
1056 return 0;
1057 if (p - range > DECIMAL_STRLEN_MAX)
1058 return 0;
1059 strncpy(buf, range, p - range);
1060 buf[p - range] = '\0';
1061 max = strtoul(buf, &endptr, 10);
1062 if (*endptr != '\0')
1063 return 0;
1064
1065 if (val < min || val > max)
1066 return 0;
1067
1068 return 1;
1069}
1070
1071/* Make completion match and return match type flag. */
1072static enum match_type
1073cmd_filter_by_completion(char *command, vector v, unsigned int index)
1074{
1075 unsigned int i;
1076 const char *str;
1077 struct cmd_element *cmd_element;
1078 enum match_type match_type;
1079 vector descvec;
1080 struct desc *desc;
1081
1082 match_type = no_match;
1083
1084 /* If command and cmd_element string does not match set NULL to vector */
1085 for (i = 0; i < vector_active(v); i++)
1086 if ((cmd_element = vector_slot(v, i)) != NULL) {
1087 if (index >= vector_active(cmd_element->strvec))
1088 vector_slot(v, i) = NULL;
1089 else {
1090 unsigned int j;
1091 int matched = 0;
1092
1093 descvec =
1094 vector_slot(cmd_element->strvec, index);
1095
1096 for (j = 0; j < vector_active(descvec); j++)
1097 if ((desc = vector_slot(descvec, j))) {
1098 str = desc->cmd;
1099
1100 if (CMD_VARARG(str)) {
1101 if (match_type <
1102 vararg_match)
1103 match_type =
1104 vararg_match;
1105 matched++;
1106 } else if (CMD_RANGE(str)) {
1107 if (cmd_range_match
1108 (str, command)) {
1109 if (match_type <
1110 range_match)
1111 match_type
1112 =
1113 range_match;
1114
1115 matched++;
1116 }
1117 }
1118#ifdef HAVE_IPV6
1119 else if (CMD_IPV6(str)) {
1120 if (cmd_ipv6_match
1121 (command)) {
1122 if (match_type <
1123 ipv6_match)
1124 match_type
1125 =
1126 ipv6_match;
1127
1128 matched++;
1129 }
1130 } else if (CMD_IPV6_PREFIX(str)) {
1131 if (cmd_ipv6_prefix_match(command)) {
1132 if (match_type <
1133 ipv6_prefix_match)
1134 match_type
1135 =
1136 ipv6_prefix_match;
1137
1138 matched++;
1139 }
1140 }
1141#endif /* HAVE_IPV6 */
1142 else if (CMD_IPV4(str)) {
1143 if (cmd_ipv4_match
1144 (command)) {
1145 if (match_type <
1146 ipv4_match)
1147 match_type
1148 =
1149 ipv4_match;
1150
1151 matched++;
1152 }
1153 } else if (CMD_IPV4_PREFIX(str)) {
1154 if (cmd_ipv4_prefix_match(command)) {
1155 if (match_type <
1156 ipv4_prefix_match)
1157 match_type
1158 =
1159 ipv4_prefix_match;
1160 matched++;
1161 }
1162 } else
1163 /* Check is this point's argument optional ? */
1164 if (CMD_OPTION(str)
1165 ||
1166 CMD_VARIABLE(str)) {
1167 if (match_type <
1168 extend_match)
1169 match_type =
1170 extend_match;
1171 matched++;
1172 } else
1173 if (strncmp
1174 (command, str,
1175 strlen(command)) ==
1176 0) {
1177 if (strcmp(command, str)
1178 == 0)
1179 match_type =
1180 exact_match;
1181 else {
1182 if (match_type <
1183 partly_match)
1184 match_type
1185 =
1186 partly_match;
1187 }
1188 matched++;
1189 }
1190 }
1191 if (!matched)
1192 vector_slot(v, i) = NULL;
1193 }
1194 }
1195 return match_type;
1196}
1197
1198/* Filter vector by command character with index. */
1199static enum match_type
1200cmd_filter_by_string(char *command, vector v, unsigned int index)
1201{
1202 unsigned int i;
1203 const char *str;
1204 struct cmd_element *cmd_element;
1205 enum match_type match_type;
1206 vector descvec;
1207 struct desc *desc;
1208
1209 match_type = no_match;
1210
1211 /* If command and cmd_element string does not match set NULL to vector */
1212 for (i = 0; i < vector_active(v); i++)
1213 if ((cmd_element = vector_slot(v, i)) != NULL) {
1214 /* If given index is bigger than max string vector of command,
1215 set NULL */
1216 if (index >= vector_active(cmd_element->strvec))
1217 vector_slot(v, i) = NULL;
1218 else {
1219 unsigned int j;
1220 int matched = 0;
1221
1222 descvec =
1223 vector_slot(cmd_element->strvec, index);
1224
1225 for (j = 0; j < vector_active(descvec); j++)
1226 if ((desc = vector_slot(descvec, j))) {
1227 str = desc->cmd;
1228
1229 if (CMD_VARARG(str)) {
1230 if (match_type <
1231 vararg_match)
1232 match_type =
1233 vararg_match;
1234 matched++;
1235 } else if (CMD_RANGE(str)) {
1236 if (cmd_range_match
1237 (str, command)) {
1238 if (match_type <
1239 range_match)
1240 match_type
1241 =
1242 range_match;
1243 matched++;
1244 }
1245 }
1246#ifdef HAVE_IPV6
1247 else if (CMD_IPV6(str)) {
1248 if (cmd_ipv6_match
1249 (command) ==
1250 exact_match) {
1251 if (match_type <
1252 ipv6_match)
1253 match_type
1254 =
1255 ipv6_match;
1256 matched++;
1257 }
1258 } else if (CMD_IPV6_PREFIX(str)) {
1259 if (cmd_ipv6_prefix_match(command) == exact_match) {
1260 if (match_type <
1261 ipv6_prefix_match)
1262 match_type
1263 =
1264 ipv6_prefix_match;
1265 matched++;
1266 }
1267 }
1268#endif /* HAVE_IPV6 */
1269 else if (CMD_IPV4(str)) {
1270 if (cmd_ipv4_match
1271 (command) ==
1272 exact_match) {
1273 if (match_type <
1274 ipv4_match)
1275 match_type
1276 =
1277 ipv4_match;
1278 matched++;
1279 }
1280 } else if (CMD_IPV4_PREFIX(str)) {
1281 if (cmd_ipv4_prefix_match(command) == exact_match) {
1282 if (match_type <
1283 ipv4_prefix_match)
1284 match_type
1285 =
1286 ipv4_prefix_match;
1287 matched++;
1288 }
1289 } else if (CMD_OPTION(str)
1290 || CMD_VARIABLE(str))
1291 {
1292 if (match_type <
1293 extend_match)
1294 match_type =
1295 extend_match;
1296 matched++;
1297 } else {
1298 if (strcmp(command, str)
1299 == 0) {
1300 match_type =
1301 exact_match;
1302 matched++;
1303 }
1304 }
1305 }
1306 if (!matched)
1307 vector_slot(v, i) = NULL;
1308 }
1309 }
1310 return match_type;
1311}
1312
1313/* Check ambiguous match */
1314static int
1315is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1316{
1317 unsigned int i;
1318 unsigned int j;
1319 const char *str = NULL;
1320 struct cmd_element *cmd_element;
1321 const char *matched = NULL;
1322 vector descvec;
1323 struct desc *desc;
1324
1325 for (i = 0; i < vector_active(v); i++)
1326 if ((cmd_element = vector_slot(v, i)) != NULL) {
1327 int match = 0;
1328
1329 descvec = vector_slot(cmd_element->strvec, index);
1330
1331 for (j = 0; j < vector_active(descvec); j++)
1332 if ((desc = vector_slot(descvec, j))) {
1333 enum match_type ret;
1334
1335 str = desc->cmd;
1336
1337 switch (type) {
1338 case exact_match:
1339 if (!
1340 (CMD_OPTION(str)
1341 || CMD_VARIABLE(str))
1342&& strcmp(command, str) == 0)
1343 match++;
1344 break;
1345 case partly_match:
1346 if (!
1347 (CMD_OPTION(str)
1348 || CMD_VARIABLE(str))
1349&& strncmp(command, str, strlen(command)) == 0) {
1350 if (matched
1351 && strcmp(matched,
1352 str) != 0)
1353 return 1; /* There is ambiguous match. */
1354 else
1355 matched = str;
1356 match++;
1357 }
1358 break;
1359 case range_match:
1360 if (cmd_range_match
1361 (str, command)) {
1362 if (matched
1363 && strcmp(matched,
1364 str) != 0)
1365 return 1;
1366 else
1367 matched = str;
1368 match++;
1369 }
1370 break;
1371#ifdef HAVE_IPV6
1372 case ipv6_match:
1373 if (CMD_IPV6(str))
1374 match++;
1375 break;
1376 case ipv6_prefix_match:
1377 if ((ret =
1378 cmd_ipv6_prefix_match
1379 (command)) != no_match) {
1380 if (ret == partly_match)
1381 return 2; /* There is incomplete match. */
1382
1383 match++;
1384 }
1385 break;
1386#endif /* HAVE_IPV6 */
1387 case ipv4_match:
1388 if (CMD_IPV4(str))
1389 match++;
1390 break;
1391 case ipv4_prefix_match:
1392 if ((ret =
1393 cmd_ipv4_prefix_match
1394 (command)) != no_match) {
1395 if (ret == partly_match)
1396 return 2; /* There is incomplete match. */
1397
1398 match++;
1399 }
1400 break;
1401 case extend_match:
1402 if (CMD_OPTION(str)
1403 || CMD_VARIABLE(str))
1404 match++;
1405 break;
1406 case no_match:
1407 default:
1408 break;
1409 }
1410 }
1411 if (!match)
1412 vector_slot(v, i) = NULL;
1413 }
1414 return 0;
1415}
1416
1417/* If src matches dst return dst string, otherwise return NULL */
1418static const char *cmd_entry_function(const char *src, const char *dst)
1419{
1420 /* Skip variable arguments. */
1421 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1422 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1423 return NULL;
1424
1425 /* In case of 'command \t', given src is NULL string. */
1426 if (src == NULL)
1427 return dst;
1428
1429 /* Matched with input string. */
1430 if (strncmp(src, dst, strlen(src)) == 0)
1431 return dst;
1432
1433 return NULL;
1434}
1435
1436/* If src matches dst return dst string, otherwise return NULL */
1437/* This version will return the dst string always if it is
1438 CMD_VARIABLE for '?' key processing */
1439static const char *cmd_entry_function_desc(const char *src, const char *dst)
1440{
1441 if (CMD_VARARG(dst))
1442 return dst;
1443
1444 if (CMD_RANGE(dst)) {
1445 if (cmd_range_match(dst, src))
1446 return dst;
1447 else
1448 return NULL;
1449 }
1450#ifdef HAVE_IPV6
1451 if (CMD_IPV6(dst)) {
1452 if (cmd_ipv6_match(src))
1453 return dst;
1454 else
1455 return NULL;
1456 }
1457
1458 if (CMD_IPV6_PREFIX(dst)) {
1459 if (cmd_ipv6_prefix_match(src))
1460 return dst;
1461 else
1462 return NULL;
1463 }
1464#endif /* HAVE_IPV6 */
1465
1466 if (CMD_IPV4(dst)) {
1467 if (cmd_ipv4_match(src))
1468 return dst;
1469 else
1470 return NULL;
1471 }
1472
1473 if (CMD_IPV4_PREFIX(dst)) {
1474 if (cmd_ipv4_prefix_match(src))
1475 return dst;
1476 else
1477 return NULL;
1478 }
1479
1480 /* Optional or variable commands always match on '?' */
1481 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1482 return dst;
1483
1484 /* In case of 'command \t', given src is NULL string. */
1485 if (src == NULL)
1486 return dst;
1487
1488 if (strncmp(src, dst, strlen(src)) == 0)
1489 return dst;
1490 else
1491 return NULL;
1492}
1493
1494/* Check same string element existence. If it isn't there return
1495 1. */
1496static int cmd_unique_string(vector v, const char *str)
1497{
1498 unsigned int i;
1499 char *match;
1500
1501 for (i = 0; i < vector_active(v); i++)
1502 if ((match = vector_slot(v, i)) != NULL)
1503 if (strcmp(match, str) == 0)
1504 return 0;
1505 return 1;
1506}
1507
1508/* Compare string to description vector. If there is same string
1509 return 1 else return 0. */
1510static int desc_unique_string(vector v, const char *str)
1511{
1512 unsigned int i;
1513 struct desc *desc;
1514
1515 for (i = 0; i < vector_active(v); i++)
1516 if ((desc = vector_slot(v, i)) != NULL)
1517 if (strcmp(desc->cmd, str) == 0)
1518 return 1;
1519 return 0;
1520}
1521
1522static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1523{
1524 if (first_word != NULL &&
1525 node != AUTH_NODE &&
1526 node != VIEW_NODE &&
1527 node != AUTH_ENABLE_NODE &&
1528 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1529 return 1;
1530 return 0;
1531}
1532
1533/* '?' describe command support. */
1534static vector
1535cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1536{
1537 unsigned int i;
1538 vector cmd_vector;
1539#define INIT_MATCHVEC_SIZE 10
1540 vector matchvec;
1541 struct cmd_element *cmd_element;
1542 unsigned int index;
1543 int ret;
1544 enum match_type match;
1545 char *command;
1546 static struct desc desc_cr = { "<cr>", "" };
1547
1548 /* Set index. */
1549 if (vector_active(vline) == 0) {
1550 *status = CMD_ERR_NO_MATCH;
1551 return NULL;
1552 } else
1553 index = vector_active(vline) - 1;
1554
1555 /* Make copy vector of current node's command vector. */
1556 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1557
1558 /* Prepare match vector */
1559 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1560
1561 /* Filter commands. */
1562 /* Only words precedes current word will be checked in this loop. */
1563 for (i = 0; i < index; i++)
1564 if ((command = vector_slot(vline, i))) {
1565 match =
1566 cmd_filter_by_completion(command, cmd_vector, i);
1567
1568 if (match == vararg_match) {
1569 struct cmd_element *cmd_element;
1570 vector descvec;
1571 unsigned int j, k;
1572
1573 for (j = 0; j < vector_active(cmd_vector); j++)
1574 if ((cmd_element =
1575 vector_slot(cmd_vector, j)) != NULL
1576 &&
1577 (vector_active
1578 (cmd_element->strvec))) {
1579 descvec =
1580 vector_slot(cmd_element->
1581 strvec,
1582 vector_active
1583 (cmd_element->
1584 strvec) - 1);
1585 for (k = 0;
1586 k < vector_active(descvec);
1587 k++) {
1588 struct desc *desc =
1589 vector_slot(descvec,
1590 k);
1591 vector_set(matchvec,
1592 desc);
1593 }
1594 }
1595
1596 vector_set(matchvec, &desc_cr);
1597 vector_free(cmd_vector);
1598
1599 return matchvec;
1600 }
1601
1602 if ((ret =
1603 is_cmd_ambiguous(command, cmd_vector, i,
1604 match)) == 1) {
1605 vector_free(cmd_vector);
1606 *status = CMD_ERR_AMBIGUOUS;
1607 return NULL;
1608 } else if (ret == 2) {
1609 vector_free(cmd_vector);
1610 *status = CMD_ERR_NO_MATCH;
1611 return NULL;
1612 }
1613 }
1614
1615 /* Prepare match vector */
1616 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1617
1618 /* Make sure that cmd_vector is filtered based on current word */
1619 command = vector_slot(vline, index);
1620 if (command)
1621 match = cmd_filter_by_completion(command, cmd_vector, index);
1622
1623 /* Make description vector. */
1624 for (i = 0; i < vector_active(cmd_vector); i++)
1625 if ((cmd_element = vector_slot(cmd_vector, i)) != NULL) {
1626 const char *string = NULL;
1627 vector strvec = cmd_element->strvec;
1628
1629 /* if command is NULL, index may be equal to vector_active */
1630 if (command && index >= vector_active(strvec))
1631 vector_slot(cmd_vector, i) = NULL;
1632 else {
1633 /* Check if command is completed. */
1634 if (command == NULL
1635 && index == vector_active(strvec)) {
1636 string = "<cr>";
1637 if (!desc_unique_string
1638 (matchvec, string))
1639 vector_set(matchvec, &desc_cr);
1640 } else {
1641 unsigned int j;
1642 vector descvec =
1643 vector_slot(strvec, index);
1644 struct desc *desc;
1645
1646 for (j = 0; j < vector_active(descvec);
1647 j++)
1648 if ((desc =
1649 vector_slot(descvec, j))) {
1650 string =
1651 cmd_entry_function_desc
1652 (command,
1653 desc->cmd);
1654 if (string) {
1655 /* Uniqueness check */
1656 if (!desc_unique_string(matchvec, string))
1657 vector_set
1658 (matchvec,
1659 desc);
1660 }
1661 }
1662 }
1663 }
1664 }
1665 vector_free(cmd_vector);
1666
1667 if (vector_slot(matchvec, 0) == NULL) {
1668 vector_free(matchvec);
1669 *status = CMD_ERR_NO_MATCH;
1670 } else
1671 *status = CMD_SUCCESS;
1672
1673 return matchvec;
1674}
1675
1676vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1677{
1678 vector ret;
1679
1680 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1681 enum node_type onode;
1682 vector shifted_vline;
1683 unsigned int index;
1684
1685 onode = vty->node;
1686 vty->node = ENABLE_NODE;
1687 /* We can try it on enable node, cos' the vty is authenticated */
1688
1689 shifted_vline = vector_init(vector_count(vline));
1690 /* use memcpy? */
1691 for (index = 1; index < vector_active(vline); index++) {
1692 vector_set_index(shifted_vline, index - 1,
1693 vector_lookup(vline, index));
1694 }
1695
1696 ret = cmd_describe_command_real(shifted_vline, vty, status);
1697
1698 vector_free(shifted_vline);
1699 vty->node = onode;
1700 return ret;
1701 }
1702
1703 return cmd_describe_command_real(vline, vty, status);
1704}
1705
1706/* Check LCD of matched command. */
1707static int cmd_lcd(char **matched)
1708{
1709 int i;
1710 int j;
1711 int lcd = -1;
1712 char *s1, *s2;
1713 char c1, c2;
1714
1715 if (matched[0] == NULL || matched[1] == NULL)
1716 return 0;
1717
1718 for (i = 1; matched[i] != NULL; i++) {
1719 s1 = matched[i - 1];
1720 s2 = matched[i];
1721
1722 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1723 if (c1 != c2)
1724 break;
1725
1726 if (lcd < 0)
1727 lcd = j;
1728 else {
1729 if (lcd > j)
1730 lcd = j;
1731 }
1732 }
1733 return lcd;
1734}
1735
1736/* Command line completion support. */
1737static char **cmd_complete_command_real(vector vline, struct vty *vty,
1738 int *status)
1739{
1740 unsigned int i;
1741 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1742#define INIT_MATCHVEC_SIZE 10
1743 vector matchvec;
1744 struct cmd_element *cmd_element;
1745 unsigned int index;
1746 char **match_str;
1747 struct desc *desc;
1748 vector descvec;
1749 char *command;
1750 int lcd;
1751
1752 if (vector_active(vline) == 0) {
1753 *status = CMD_ERR_NO_MATCH;
1754 return NULL;
1755 } else
1756 index = vector_active(vline) - 1;
1757
1758 /* First, filter by preceeding command string */
1759 for (i = 0; i < index; i++)
1760 if ((command = vector_slot(vline, i))) {
1761 enum match_type match;
1762 int ret;
1763
1764 /* First try completion match, if there is exactly match return 1 */
1765 match =
1766 cmd_filter_by_completion(command, cmd_vector, i);
1767
1768 /* If there is exact match then filter ambiguous match else check
1769 ambiguousness. */
1770 if ((ret =
1771 is_cmd_ambiguous(command, cmd_vector, i,
1772 match)) == 1) {
1773 vector_free(cmd_vector);
1774 *status = CMD_ERR_AMBIGUOUS;
1775 return NULL;
1776 }
1777 /*
1778 else if (ret == 2)
1779 {
1780 vector_free (cmd_vector);
1781 *status = CMD_ERR_NO_MATCH;
1782 return NULL;
1783 }
1784 */
1785 }
1786
1787 /* Prepare match vector. */
1788 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1789
1790 /* Now we got into completion */
1791 for (i = 0; i < vector_active(cmd_vector); i++)
1792 if ((cmd_element = vector_slot(cmd_vector, i))) {
1793 const char *string;
1794 vector strvec = cmd_element->strvec;
1795
1796 /* Check field length */
1797 if (index >= vector_active(strvec))
1798 vector_slot(cmd_vector, i) = NULL;
1799 else {
1800 unsigned int j;
1801
1802 descvec = vector_slot(strvec, index);
1803 for (j = 0; j < vector_active(descvec); j++)
1804 if ((desc = vector_slot(descvec, j))) {
Harald Weltefc1c3e52009-08-07 13:26:28 +02001805 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1806 if (cmd_unique_string (matchvec, string))
1807 vector_set (matchvec, talloc_strdup(tall_vty_ctx, string));
Harald Welte955049f2009-03-10 12:16:51 +00001808 }
1809 }
1810 }
1811
1812 /* We don't need cmd_vector any more. */
1813 vector_free(cmd_vector);
1814
1815 /* No matched command */
1816 if (vector_slot(matchvec, 0) == NULL) {
1817 vector_free(matchvec);
1818
1819 /* In case of 'command \t' pattern. Do you need '?' command at
1820 the end of the line. */
1821 if (vector_slot(vline, index) == '\0')
1822 *status = CMD_ERR_NOTHING_TODO;
1823 else
1824 *status = CMD_ERR_NO_MATCH;
1825 return NULL;
1826 }
1827
1828 /* Only one matched */
1829 if (vector_slot(matchvec, 1) == NULL) {
1830 match_str = (char **)matchvec->index;
1831 vector_only_wrapper_free(matchvec);
1832 *status = CMD_COMPLETE_FULL_MATCH;
1833 return match_str;
1834 }
1835 /* Make it sure last element is NULL. */
1836 vector_set(matchvec, NULL);
1837
1838 /* Check LCD of matched strings. */
1839 if (vector_slot(vline, index) != NULL) {
1840 lcd = cmd_lcd((char **)matchvec->index);
1841
1842 if (lcd) {
1843 int len = strlen(vector_slot(vline, index));
1844
1845 if (len < lcd) {
1846 char *lcdstr;
1847
Harald Welte0224e4d2009-08-07 13:25:41 +02001848 lcdstr = _talloc_zero(tall_vty_ctx, lcd + 1,
Harald Welte2477d932009-08-07 00:32:41 +02001849 "complete-lcdstr");
Harald Welte955049f2009-03-10 12:16:51 +00001850 memcpy(lcdstr, matchvec->index[0], lcd);
1851 lcdstr[lcd] = '\0';
1852
1853 /* match_str = (char **) &lcdstr; */
1854
1855 /* Free matchvec. */
1856 for (i = 0; i < vector_active(matchvec); i++) {
1857 if (vector_slot(matchvec, i))
Harald Welte2477d932009-08-07 00:32:41 +02001858 talloc_free(vector_slot(matchvec, i));
Harald Welte955049f2009-03-10 12:16:51 +00001859 }
1860 vector_free(matchvec);
1861
1862 /* Make new matchvec. */
1863 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1864 vector_set(matchvec, lcdstr);
1865 match_str = (char **)matchvec->index;
1866 vector_only_wrapper_free(matchvec);
1867
1868 *status = CMD_COMPLETE_MATCH;
1869 return match_str;
1870 }
1871 }
1872 }
1873
1874 match_str = (char **)matchvec->index;
1875 vector_only_wrapper_free(matchvec);
1876 *status = CMD_COMPLETE_LIST_MATCH;
1877 return match_str;
1878}
1879
1880char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1881{
1882 char **ret;
1883
1884 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1885 enum node_type onode;
1886 vector shifted_vline;
1887 unsigned int index;
1888
1889 onode = vty->node;
1890 vty->node = ENABLE_NODE;
1891 /* We can try it on enable node, cos' the vty is authenticated */
1892
1893 shifted_vline = vector_init(vector_count(vline));
1894 /* use memcpy? */
1895 for (index = 1; index < vector_active(vline); index++) {
1896 vector_set_index(shifted_vline, index - 1,
1897 vector_lookup(vline, index));
1898 }
1899
1900 ret = cmd_complete_command_real(shifted_vline, vty, status);
1901
1902 vector_free(shifted_vline);
1903 vty->node = onode;
1904 return ret;
1905 }
1906
1907 return cmd_complete_command_real(vline, vty, status);
1908}
1909
1910/* return parent node */
1911/* MUST eventually converge on CONFIG_NODE */
1912enum node_type node_parent(enum node_type node)
1913{
1914 enum node_type ret;
1915
1916 assert(node > CONFIG_NODE);
1917
1918 switch (node) {
1919 case BGP_VPNV4_NODE:
1920 case BGP_IPV4_NODE:
1921 case BGP_IPV4M_NODE:
1922 case BGP_IPV6_NODE:
1923 ret = BGP_NODE;
1924 break;
1925 case KEYCHAIN_KEY_NODE:
1926 ret = KEYCHAIN_NODE;
1927 break;
1928 default:
1929 ret = CONFIG_NODE;
1930 }
1931
1932 return ret;
1933}
1934
1935/* Execute command by argument vline vector. */
1936static int
1937cmd_execute_command_real(vector vline, struct vty *vty,
1938 struct cmd_element **cmd)
1939{
1940 unsigned int i;
1941 unsigned int index;
1942 vector cmd_vector;
1943 struct cmd_element *cmd_element;
1944 struct cmd_element *matched_element;
1945 unsigned int matched_count, incomplete_count;
1946 int argc;
1947 const char *argv[CMD_ARGC_MAX];
1948 enum match_type match = 0;
1949 int varflag;
1950 char *command;
1951
1952 /* Make copy of command elements. */
1953 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1954
1955 for (index = 0; index < vector_active(vline); index++)
1956 if ((command = vector_slot(vline, index))) {
1957 int ret;
1958
1959 match =
1960 cmd_filter_by_completion(command, cmd_vector,
1961 index);
1962
1963 if (match == vararg_match)
1964 break;
1965
1966 ret =
1967 is_cmd_ambiguous(command, cmd_vector, index, match);
1968
1969 if (ret == 1) {
1970 vector_free(cmd_vector);
1971 return CMD_ERR_AMBIGUOUS;
1972 } else if (ret == 2) {
1973 vector_free(cmd_vector);
1974 return CMD_ERR_NO_MATCH;
1975 }
1976 }
1977
1978 /* Check matched count. */
1979 matched_element = NULL;
1980 matched_count = 0;
1981 incomplete_count = 0;
1982
1983 for (i = 0; i < vector_active(cmd_vector); i++)
1984 if ((cmd_element = vector_slot(cmd_vector, i))) {
1985 if (match == vararg_match
1986 || index >= cmd_element->cmdsize) {
1987 matched_element = cmd_element;
1988#if 0
1989 printf("DEBUG: %s\n", cmd_element->string);
1990#endif
1991 matched_count++;
1992 } else {
1993 incomplete_count++;
1994 }
1995 }
1996
1997 /* Finish of using cmd_vector. */
1998 vector_free(cmd_vector);
1999
2000 /* To execute command, matched_count must be 1. */
2001 if (matched_count == 0) {
2002 if (incomplete_count)
2003 return CMD_ERR_INCOMPLETE;
2004 else
2005 return CMD_ERR_NO_MATCH;
2006 }
2007
2008 if (matched_count > 1)
2009 return CMD_ERR_AMBIGUOUS;
2010
2011 /* Argument treatment */
2012 varflag = 0;
2013 argc = 0;
2014
2015 for (i = 0; i < vector_active(vline); i++) {
2016 if (varflag)
2017 argv[argc++] = vector_slot(vline, i);
2018 else {
2019 vector descvec =
2020 vector_slot(matched_element->strvec, i);
2021
2022 if (vector_active(descvec) == 1) {
2023 struct desc *desc = vector_slot(descvec, 0);
2024
2025 if (CMD_VARARG(desc->cmd))
2026 varflag = 1;
2027
2028 if (varflag || CMD_VARIABLE(desc->cmd)
2029 || CMD_OPTION(desc->cmd))
2030 argv[argc++] = vector_slot(vline, i);
2031 } else
2032 argv[argc++] = vector_slot(vline, i);
2033 }
2034
2035 if (argc >= CMD_ARGC_MAX)
2036 return CMD_ERR_EXEED_ARGC_MAX;
2037 }
2038
2039 /* For vtysh execution. */
2040 if (cmd)
2041 *cmd = matched_element;
2042
2043 if (matched_element->daemon)
2044 return CMD_SUCCESS_DAEMON;
2045
2046 /* Execute matched command. */
2047 return (*matched_element->func) (matched_element, vty, argc, argv);
2048}
2049
2050int
2051cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2052 int vtysh)
2053{
2054 int ret, saved_ret, tried = 0;
2055 enum node_type onode, try_node;
2056
2057 onode = try_node = vty->node;
2058
2059 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2060 vector shifted_vline;
2061 unsigned int index;
2062
2063 vty->node = ENABLE_NODE;
2064 /* We can try it on enable node, cos' the vty is authenticated */
2065
2066 shifted_vline = vector_init(vector_count(vline));
2067 /* use memcpy? */
2068 for (index = 1; index < vector_active(vline); index++) {
2069 vector_set_index(shifted_vline, index - 1,
2070 vector_lookup(vline, index));
2071 }
2072
2073 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2074
2075 vector_free(shifted_vline);
2076 vty->node = onode;
2077 return ret;
2078 }
2079
2080 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2081
2082 if (vtysh)
2083 return saved_ret;
2084
2085 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
2086 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2087 && vty->node > CONFIG_NODE) {
2088 try_node = node_parent(try_node);
2089 vty->node = try_node;
2090 ret = cmd_execute_command_real(vline, vty, cmd);
2091 tried = 1;
2092 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2093 /* succesfull command, leave the node as is */
2094 return ret;
2095 }
2096 }
2097 /* no command succeeded, reset the vty to the original node and
2098 return the error for this node */
2099 if (tried)
2100 vty->node = onode;
2101 return saved_ret;
2102}
2103
2104/* Execute command by argument readline. */
2105int
2106cmd_execute_command_strict(vector vline, struct vty *vty,
2107 struct cmd_element **cmd)
2108{
2109 unsigned int i;
2110 unsigned int index;
2111 vector cmd_vector;
2112 struct cmd_element *cmd_element;
2113 struct cmd_element *matched_element;
2114 unsigned int matched_count, incomplete_count;
2115 int argc;
2116 const char *argv[CMD_ARGC_MAX];
2117 int varflag;
2118 enum match_type match = 0;
2119 char *command;
2120
2121 /* Make copy of command element */
2122 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2123
2124 for (index = 0; index < vector_active(vline); index++)
2125 if ((command = vector_slot(vline, index))) {
2126 int ret;
2127
2128 match = cmd_filter_by_string(vector_slot(vline, index),
2129 cmd_vector, index);
2130
2131 /* If command meets '.VARARG' then finish matching. */
2132 if (match == vararg_match)
2133 break;
2134
2135 ret =
2136 is_cmd_ambiguous(command, cmd_vector, index, match);
2137 if (ret == 1) {
2138 vector_free(cmd_vector);
2139 return CMD_ERR_AMBIGUOUS;
2140 }
2141 if (ret == 2) {
2142 vector_free(cmd_vector);
2143 return CMD_ERR_NO_MATCH;
2144 }
2145 }
2146
2147 /* Check matched count. */
2148 matched_element = NULL;
2149 matched_count = 0;
2150 incomplete_count = 0;
2151 for (i = 0; i < vector_active(cmd_vector); i++)
2152 if (vector_slot(cmd_vector, i) != NULL) {
2153 cmd_element = vector_slot(cmd_vector, i);
2154
2155 if (match == vararg_match
2156 || index >= cmd_element->cmdsize) {
2157 matched_element = cmd_element;
2158 matched_count++;
2159 } else
2160 incomplete_count++;
2161 }
2162
2163 /* Finish of using cmd_vector. */
2164 vector_free(cmd_vector);
2165
2166 /* To execute command, matched_count must be 1. */
2167 if (matched_count == 0) {
2168 if (incomplete_count)
2169 return CMD_ERR_INCOMPLETE;
2170 else
2171 return CMD_ERR_NO_MATCH;
2172 }
2173
2174 if (matched_count > 1)
2175 return CMD_ERR_AMBIGUOUS;
2176
2177 /* Argument treatment */
2178 varflag = 0;
2179 argc = 0;
2180
2181 for (i = 0; i < vector_active(vline); i++) {
2182 if (varflag)
2183 argv[argc++] = vector_slot(vline, i);
2184 else {
2185 vector descvec =
2186 vector_slot(matched_element->strvec, i);
2187
2188 if (vector_active(descvec) == 1) {
2189 struct desc *desc = vector_slot(descvec, 0);
2190
2191 if (CMD_VARARG(desc->cmd))
2192 varflag = 1;
2193
2194 if (varflag || CMD_VARIABLE(desc->cmd)
2195 || CMD_OPTION(desc->cmd))
2196 argv[argc++] = vector_slot(vline, i);
2197 } else
2198 argv[argc++] = vector_slot(vline, i);
2199 }
2200
2201 if (argc >= CMD_ARGC_MAX)
2202 return CMD_ERR_EXEED_ARGC_MAX;
2203 }
2204
2205 /* For vtysh execution. */
2206 if (cmd)
2207 *cmd = matched_element;
2208
2209 if (matched_element->daemon)
2210 return CMD_SUCCESS_DAEMON;
2211
2212 /* Now execute matched command */
2213 return (*matched_element->func) (matched_element, vty, argc, argv);
2214}
2215
Harald Welte955049f2009-03-10 12:16:51 +00002216/* Configration make from file. */
2217int config_from_file(struct vty *vty, FILE * fp)
2218{
2219 int ret;
2220 vector vline;
2221
2222 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2223 vline = cmd_make_strvec(vty->buf);
2224
2225 /* In case of comment line */
2226 if (vline == NULL)
2227 continue;
2228 /* Execute configuration command : this is strict match */
2229 ret = cmd_execute_command_strict(vline, vty, NULL);
2230
2231 /* Try again with setting node to CONFIG_NODE */
2232 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2233 && ret != CMD_ERR_NOTHING_TODO
2234 && vty->node != CONFIG_NODE) {
2235 vty->node = node_parent(vty->node);
2236 ret = cmd_execute_command_strict(vline, vty, NULL);
2237 }
2238
2239 cmd_free_strvec(vline);
2240
2241 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2242 && ret != CMD_ERR_NOTHING_TODO)
2243 return ret;
2244 }
2245 return CMD_SUCCESS;
2246}
Harald Welte955049f2009-03-10 12:16:51 +00002247
2248/* Configration from terminal */
2249DEFUN(config_terminal,
2250 config_terminal_cmd,
2251 "configure terminal",
2252 "Configuration from vty interface\n" "Configuration terminal\n")
2253{
2254 if (vty_config_lock(vty))
2255 vty->node = CONFIG_NODE;
2256 else {
2257 vty_out(vty, "VTY configuration is locked by other VTY%s",
2258 VTY_NEWLINE);
2259 return CMD_WARNING;
2260 }
2261 return CMD_SUCCESS;
2262}
2263
2264/* Enable command */
2265DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2266{
2267 /* If enable password is NULL, change to ENABLE_NODE */
2268 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2269 vty->type == VTY_SHELL_SERV)
2270 vty->node = ENABLE_NODE;
2271 else
2272 vty->node = AUTH_ENABLE_NODE;
2273
2274 return CMD_SUCCESS;
2275}
2276
2277/* Disable command */
2278DEFUN(disable,
2279 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2280{
2281 if (vty->node == ENABLE_NODE)
2282 vty->node = VIEW_NODE;
2283 return CMD_SUCCESS;
2284}
2285
2286/* Down vty node level. */
2287DEFUN(config_exit,
2288 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2289{
2290 switch (vty->node) {
Harald Welte5013b2a2009-08-07 13:29:14 +02002291 case GSMNET_NODE:
2292 vty->node = CONFIG_NODE;
Harald Welte5258fc42009-03-28 19:07:53 +00002293 vty->index = NULL;
Harald Welte955049f2009-03-10 12:16:51 +00002294 break;
Harald Welte5013b2a2009-08-07 13:29:14 +02002295 case BTS_NODE:
2296 vty->node = GSMNET_NODE;
2297 {
2298 /* set vty->index correctly ! */
2299 struct gsm_bts *bts = vty->index;
2300 vty->index = bts->network;
2301 }
2302 break;
Harald Welte955049f2009-03-10 12:16:51 +00002303 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
Harald Weltefc1c3e52009-08-07 13:26:28 +02002664 host.name = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00002665 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 Weltefc1c3e52009-08-07 13:26:28 +02002699 host.password_encrypt = talloc_strdup(tall_vty_ctx, argv[1]);
Harald Welte955049f2009-03-10 12:16:51 +00002700 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 Weltefc1c3e52009-08-07 13:26:28 +02002722 host.password_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(argv[0]));
Harald Welte955049f2009-03-10 12:16:51 +00002723 } else
Harald Welted6cab812009-05-21 07:31:48 +00002724#endif
Harald Weltefc1c3e52009-08-07 13:26:28 +02002725 host.password = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00002726
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 Weltefc1c3e52009-08-07 13:26:28 +02002758 host.enable_encrypt = talloc_strdup(tall_vty_ctx, argv[1]);
Harald Welte955049f2009-03-10 12:16:51 +00002759
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 Weltefc1c3e52009-08-07 13:26:28 +02002783 host.enable_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(argv[0]));
Harald Welte955049f2009-03-10 12:16:51 +00002784 } else
Harald Welted6cab812009-05-21 07:31:48 +00002785#endif
Harald Weltefc1c3e52009-08-07 13:26:28 +02002786 host.enable = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00002787
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 Weltefc1c3e52009-08-07 13:26:28 +02002830 host.password_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(host.password));
Harald Welte955049f2009-03-10 12:16:51 +00002831 }
2832 if (host.enable) {
2833 if (host.enable_encrypt)
Harald Welte2477d932009-08-07 00:32:41 +02002834 talloc_free(host.enable_encrypt);
Harald Weltefc1c3e52009-08-07 13:26:28 +02002835 host.enable_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(host.enable));
Harald Welte955049f2009-03-10 12:16:51 +00002836 }
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
Harald Weltefc1c3e52009-08-07 13:26:28 +02003109 host.logfile = talloc_strdup(tall_vty_ctx, fname);
Harald Welte955049f2009-03-10 12:16:51 +00003110
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 Weltefc1c3e52009-08-07 13:26:28 +02003299 host.motdfile = talloc_strdup(tall_vty_ctx, argv[0]);
Harald Welte955049f2009-03-10 12:16:51 +00003300
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{
Harald Weltefc1c3e52009-08-07 13:26:28 +02003327 host.config = talloc_strdup(tall_vty_ctx, filename);
Harald Welte955049f2009-03-10 12:16:51 +00003328}
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}