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