blob: 2834ef4d65e249e76c4c316dfdb3920c2b501bac [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Configuration generator.
2 Copyright (C) 2000 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "command.h"
24#include "linklist.h"
25#include "memory.h"
26
27#include "vtysh/vtysh.h"
28
29vector configvec;
30
hassoe7168df2004-10-03 20:11:32 +000031extern int vtysh_writeconfig_integrated;
32
paul718e3742002-12-13 20:15:29 +000033struct config
34{
35 /* Configuration node name. */
36 char *name;
37
38 /* Configuration string line. */
39 struct list *line;
40
41 /* Configuration can be nest. */
42 struct config *config;
43
44 /* Index of this config. */
45 u_int32_t index;
46};
47
48struct list *config_top;
hasso95e735b2004-08-26 13:08:30 +000049
David Lampartera9eb9062015-03-04 07:07:01 +010050static int
paul718e3742002-12-13 20:15:29 +000051line_cmp (char *c1, char *c2)
52{
53 return strcmp (c1, c2);
54}
55
David Lampartera9eb9062015-03-04 07:07:01 +010056static void
paul718e3742002-12-13 20:15:29 +000057line_del (char *line)
58{
59 XFREE (MTYPE_VTYSH_CONFIG_LINE, line);
60}
61
David Lampartera9eb9062015-03-04 07:07:01 +010062static struct config *
paul718e3742002-12-13 20:15:29 +000063config_new ()
64{
65 struct config *config;
66 config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config));
67 return config;
68}
69
David Lampartera9eb9062015-03-04 07:07:01 +010070static int
paul718e3742002-12-13 20:15:29 +000071config_cmp (struct config *c1, struct config *c2)
72{
73 return strcmp (c1->name, c2->name);
74}
75
David Lampartera9eb9062015-03-04 07:07:01 +010076static void
paul718e3742002-12-13 20:15:29 +000077config_del (struct config* config)
78{
79 list_delete (config->line);
80 if (config->name)
81 XFREE (MTYPE_VTYSH_CONFIG_LINE, config->name);
82 XFREE (MTYPE_VTYSH_CONFIG, config);
83}
84
David Lampartera9eb9062015-03-04 07:07:01 +010085static struct config *
hassodda09522004-10-07 21:40:25 +000086config_get (int index, const char *line)
paul718e3742002-12-13 20:15:29 +000087{
88 struct config *config;
89 struct config *config_loop;
90 struct list *master;
paul1eb8ef22005-04-07 07:30:20 +000091 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +000092
93 config = config_loop = NULL;
94
95 master = vector_lookup_ensure (configvec, index);
96
97 if (! master)
98 {
99 master = list_new ();
100 master->del = (void (*) (void *))config_del;
101 master->cmp = (int (*)(void *, void *)) config_cmp;
102 vector_set_index (configvec, index, master);
103 }
104
paul1eb8ef22005-04-07 07:30:20 +0000105 for (ALL_LIST_ELEMENTS (master, node, nnode, config_loop))
paul718e3742002-12-13 20:15:29 +0000106 {
107 if (strcmp (config_loop->name, line) == 0)
108 config = config_loop;
109 }
110
111 if (! config)
112 {
113 config = config_new ();
114 config->line = list_new ();
115 config->line->del = (void (*) (void *))line_del;
116 config->line->cmp = (int (*)(void *, void *)) line_cmp;
117 config->name = XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line);
118 config->index = index;
119 listnode_add (master, config);
120 }
121 return config;
122}
123
David Lampartera9eb9062015-03-04 07:07:01 +0100124static void
hassodda09522004-10-07 21:40:25 +0000125config_add_line (struct list *config, const char *line)
paul718e3742002-12-13 20:15:29 +0000126{
127 listnode_add (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
128}
129
David Lampartera9eb9062015-03-04 07:07:01 +0100130static void
hassodda09522004-10-07 21:40:25 +0000131config_add_line_uniq (struct list *config, const char *line)
paul718e3742002-12-13 20:15:29 +0000132{
paul1eb8ef22005-04-07 07:30:20 +0000133 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000134 char *pnt;
135
paul1eb8ef22005-04-07 07:30:20 +0000136 for (ALL_LIST_ELEMENTS (config, node, nnode, pnt))
paul718e3742002-12-13 20:15:29 +0000137 {
138 if (strcmp (pnt, line) == 0)
139 return;
140 }
141 listnode_add_sort (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
142}
143
David Lampartera9eb9062015-03-04 07:07:01 +0100144static void
hassodda09522004-10-07 21:40:25 +0000145vtysh_config_parse_line (const char *line)
paul718e3742002-12-13 20:15:29 +0000146{
147 char c;
148 static struct config *config = NULL;
149
150 if (! line)
151 return;
152
153 c = line[0];
154
155 if (c == '\0')
156 return;
157
158 /* printf ("[%s]\n", line); */
159
160 switch (c)
161 {
162 case '!':
163 case '#':
164 break;
165 case ' ':
166 /* Store line to current configuration. */
167 if (config)
168 {
Olivier Dugeonac10d302016-04-19 18:33:42 +0200169 if (strncmp (line, " address-family vpnv4",
hasso95e735b2004-08-26 13:08:30 +0000170 strlen (" address-family vpnv4")) == 0)
paul718e3742002-12-13 20:15:29 +0000171 config = config_get (BGP_VPNV4_NODE, line);
Lou Berger13c378d2016-01-12 13:41:56 -0500172 else if (strncmp (line, " address-family vpn6",
173 strlen (" address-family vpn6")) == 0)
174 config = config_get (BGP_VPNV6_NODE, line);
Lou Bergera3fda882016-01-12 13:42:04 -0500175 else if (strncmp (line, " address-family encapv6",
176 strlen (" address-family encapv6")) == 0)
177 config = config_get (BGP_ENCAPV6_NODE, line);
178 else if (strncmp (line, " address-family encap",
179 strlen (" address-family encap")) == 0)
180 config = config_get (BGP_ENCAP_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000181 else if (strncmp (line, " address-family ipv4 multicast",
182 strlen (" address-family ipv4 multicast")) == 0)
paul718e3742002-12-13 20:15:29 +0000183 config = config_get (BGP_IPV4M_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000184 else if (strncmp (line, " address-family ipv6",
185 strlen (" address-family ipv6")) == 0)
paul718e3742002-12-13 20:15:29 +0000186 config = config_get (BGP_IPV6_NODE, line);
David Lamparterf2f44ea2016-11-12 17:43:15 +0900187 else if (strncmp (line, " link-params", strlen (" link-params")) == 0)
188 {
189 config_add_line (config->line, line);
190 config->index = LINK_PARAMS_NODE;
191 }
192 else if (config->index == LINK_PARAMS_NODE &&
Donald Sharp2c0adbf2016-11-18 15:42:41 -0500193 strncmp (line, " exit-link-params", strlen (" exit")) == 0)
David Lamparterf2f44ea2016-11-12 17:43:15 +0900194 {
195 config_add_line (config->line, line);
196 config->index = INTERFACE_NODE;
197 }
hasso95e735b2004-08-26 13:08:30 +0000198 else if (config->index == RMAP_NODE ||
David Lamparterf2f44ea2016-11-12 17:43:15 +0900199 config->index == INTERFACE_NODE ||
200 config->index == VTY_NODE)
paul718e3742002-12-13 20:15:29 +0000201 config_add_line_uniq (config->line, line);
202 else
203 config_add_line (config->line, line);
204 }
205 else
206 config_add_line (config_top, line);
207 break;
208 default:
209 if (strncmp (line, "interface", strlen ("interface")) == 0)
210 config = config_get (INTERFACE_NODE, line);
hassodfbb9122004-12-22 11:53:09 +0000211 else if (strncmp (line, "router-id", strlen ("router-id")) == 0)
212 config = config_get (ZEBRA_NODE, line);
paul718e3742002-12-13 20:15:29 +0000213 else if (strncmp (line, "router rip", strlen ("router rip")) == 0)
214 config = config_get (RIP_NODE, line);
paul97e1c4d2003-03-28 02:25:23 +0000215 else if (strncmp (line, "router ripng", strlen ("router ripng")) == 0)
hassob094d262004-08-25 12:22:00 +0000216 config = config_get (RIPNG_NODE, line);
paul718e3742002-12-13 20:15:29 +0000217 else if (strncmp (line, "router ospf", strlen ("router ospf")) == 0)
hassob094d262004-08-25 12:22:00 +0000218 config = config_get (OSPF_NODE, line);
paul97e1c4d2003-03-28 02:25:23 +0000219 else if (strncmp (line, "router ospf6", strlen ("router ospf6")) == 0)
hassob094d262004-08-25 12:22:00 +0000220 config = config_get (OSPF6_NODE, line);
paul718e3742002-12-13 20:15:29 +0000221 else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
222 config = config_get (BGP_NODE, line);
paul13b8baa2004-01-15 01:00:49 +0000223 else if (strncmp (line, "router isis", strlen ("router isis")) == 0)
hassoc25e4582003-12-23 10:39:08 +0000224 config = config_get (ISIS_NODE, line);
hassodfbb9122004-12-22 11:53:09 +0000225 else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
paul718e3742002-12-13 20:15:29 +0000226 config = config_get (BGP_NODE, line);
227 else if (strncmp (line, "route-map", strlen ("route-map")) == 0)
228 config = config_get (RMAP_NODE, line);
229 else if (strncmp (line, "access-list", strlen ("access-list")) == 0)
230 config = config_get (ACCESS_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000231 else if (strncmp (line, "ipv6 access-list",
232 strlen ("ipv6 access-list")) == 0)
hassob094d262004-08-25 12:22:00 +0000233 config = config_get (ACCESS_IPV6_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000234 else if (strncmp (line, "ip prefix-list",
235 strlen ("ip prefix-list")) == 0)
paul718e3742002-12-13 20:15:29 +0000236 config = config_get (PREFIX_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000237 else if (strncmp (line, "ipv6 prefix-list",
238 strlen ("ipv6 prefix-list")) == 0)
hassob094d262004-08-25 12:22:00 +0000239 config = config_get (PREFIX_IPV6_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000240 else if (strncmp (line, "ip as-path access-list",
241 strlen ("ip as-path access-list")) == 0)
paul718e3742002-12-13 20:15:29 +0000242 config = config_get (AS_LIST_NODE, line);
hasso95e735b2004-08-26 13:08:30 +0000243 else if (strncmp (line, "ip community-list",
244 strlen ("ip community-list")) == 0)
paul718e3742002-12-13 20:15:29 +0000245 config = config_get (COMMUNITY_LIST_NODE, line);
246 else if (strncmp (line, "ip route", strlen ("ip route")) == 0)
247 config = config_get (IP_NODE, line);
paul97e1c4d2003-03-28 02:25:23 +0000248 else if (strncmp (line, "ipv6 route", strlen ("ipv6 route")) == 0)
249 config = config_get (IP_NODE, line);
paul718e3742002-12-13 20:15:29 +0000250 else if (strncmp (line, "key", strlen ("key")) == 0)
251 config = config_get (KEYCHAIN_NODE, line);
hassoe7168df2004-10-03 20:11:32 +0000252 else if (strncmp (line, "line", strlen ("line")) == 0)
253 config = config_get (VTY_NODE, line);
254 else if ( (strncmp (line, "ipv6 forwarding",
255 strlen ("ipv6 forwarding")) == 0)
256 || (strncmp (line, "ip forwarding",
257 strlen ("ip forwarding")) == 0) )
258 config = config_get (FORWARDING_NODE, line);
259 else if (strncmp (line, "service", strlen ("service")) == 0)
260 config = config_get (SERVICE_NODE, line);
hassodfbb9122004-12-22 11:53:09 +0000261 else if (strncmp (line, "debug", strlen ("debug")) == 0)
262 config = config_get (DEBUG_NODE, line);
hasso060d4382005-03-09 12:41:14 +0000263 else if (strncmp (line, "password", strlen ("password")) == 0
264 || strncmp (line, "enable password",
265 strlen ("enable password")) == 0)
266 config = config_get (AAA_NODE, line);
Chris Caputo6e79f8b2009-06-23 05:55:57 +0000267 else if (strncmp (line, "ip protocol", strlen ("ip protocol")) == 0)
268 config = config_get (PROTOCOL_NODE, line);
paul718e3742002-12-13 20:15:29 +0000269 else
270 {
271 if (strncmp (line, "log", strlen ("log")) == 0
272 || strncmp (line, "hostname", strlen ("hostname")) == 0
hassoe7168df2004-10-03 20:11:32 +0000273 )
paul718e3742002-12-13 20:15:29 +0000274 config_add_line_uniq (config_top, line);
275 else
276 config_add_line (config_top, line);
277 config = NULL;
278 }
279 break;
280 }
281}
282
283void
284vtysh_config_parse (char *line)
285{
286 char *begin;
287 char *pnt;
288
289 begin = pnt = line;
290
291 while (*pnt != '\0')
292 {
293 if (*pnt == '\n')
294 {
295 *pnt++ = '\0';
296 vtysh_config_parse_line (begin);
297 begin = pnt;
298 }
299 else
300 {
301 pnt++;
302 }
303 }
304}
305
306/* Macro to check delimiter is needed between each configuration line
hasso95e735b2004-08-26 13:08:30 +0000307 * or not. */
paul718e3742002-12-13 20:15:29 +0000308#define NO_DELIMITER(I) \
309 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
paul97e1c4d2003-03-28 02:25:23 +0000310 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE || \
hassoe7168df2004-10-03 20:11:32 +0000311 (I) == ACCESS_IPV6_NODE || (I) == PREFIX_IPV6_NODE \
hasso060d4382005-03-09 12:41:14 +0000312 || (I) == SERVICE_NODE || (I) == FORWARDING_NODE || (I) == DEBUG_NODE \
313 || (I) == AAA_NODE)
paul718e3742002-12-13 20:15:29 +0000314
hasso95e735b2004-08-26 13:08:30 +0000315/* Display configuration to file pointer. */
paul718e3742002-12-13 20:15:29 +0000316void
317vtysh_config_dump (FILE *fp)
318{
paul1eb8ef22005-04-07 07:30:20 +0000319 struct listnode *node, *nnode;
320 struct listnode *mnode, *mnnode;
paul718e3742002-12-13 20:15:29 +0000321 struct config *config;
322 struct list *master;
323 char *line;
hassodda09522004-10-07 21:40:25 +0000324 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000325
paul1eb8ef22005-04-07 07:30:20 +0000326 for (ALL_LIST_ELEMENTS (config_top, node, nnode, line))
paul718e3742002-12-13 20:15:29 +0000327 {
328 fprintf (fp, "%s\n", line);
329 fflush (fp);
330 }
331 fprintf (fp, "!\n");
332 fflush (fp);
333
paul55468c82005-03-14 20:19:01 +0000334 for (i = 0; i < vector_active (configvec); i++)
paul718e3742002-12-13 20:15:29 +0000335 if ((master = vector_slot (configvec, i)) != NULL)
336 {
paul1eb8ef22005-04-07 07:30:20 +0000337 for (ALL_LIST_ELEMENTS (master, node, nnode, config))
paul718e3742002-12-13 20:15:29 +0000338 {
339 fprintf (fp, "%s\n", config->name);
hassob094d262004-08-25 12:22:00 +0000340 fflush (fp);
paul718e3742002-12-13 20:15:29 +0000341
paul1eb8ef22005-04-07 07:30:20 +0000342 for (ALL_LIST_ELEMENTS (config->line, mnode, mnnode, line))
paul718e3742002-12-13 20:15:29 +0000343 {
344 fprintf (fp, "%s\n", line);
345 fflush (fp);
346 }
347 if (! NO_DELIMITER (i))
348 {
349 fprintf (fp, "!\n");
350 fflush (fp);
351 }
352 }
353 if (NO_DELIMITER (i))
354 {
355 fprintf (fp, "!\n");
356 fflush (fp);
357 }
358 }
359
paul55468c82005-03-14 20:19:01 +0000360 for (i = 0; i < vector_active (configvec); i++)
paul718e3742002-12-13 20:15:29 +0000361 if ((master = vector_slot (configvec, i)) != NULL)
362 {
363 list_delete (master);
364 vector_slot (configvec, i) = NULL;
365 }
366 list_delete_all_node (config_top);
367}
368
369/* Read up configuration file from file_name. */
370static void
371vtysh_read_file (FILE *confp)
372{
373 int ret;
374 struct vty *vty;
375
376 vty = vty_new ();
377 vty->fd = 0; /* stdout */
378 vty->type = VTY_TERM;
379 vty->node = CONFIG_NODE;
380
381 vtysh_execute_no_pager ("enable");
382 vtysh_execute_no_pager ("configure terminal");
383
hasso95e735b2004-08-26 13:08:30 +0000384 /* Execute configuration file. */
paul718e3742002-12-13 20:15:29 +0000385 ret = vtysh_config_from_file (vty, confp);
386
387 vtysh_execute_no_pager ("end");
388 vtysh_execute_no_pager ("disable");
389
390 vty_close (vty);
391
392 if (ret != CMD_SUCCESS)
393 {
394 switch (ret)
395 {
396 case CMD_ERR_AMBIGUOUS:
397 fprintf (stderr, "Ambiguous command.\n");
398 break;
399 case CMD_ERR_NO_MATCH:
400 fprintf (stderr, "There is no such command.\n");
401 break;
402 }
403 fprintf (stderr, "Error occured during reading below line.\n%s\n",
404 vty->buf);
405 exit (1);
406 }
407}
408
hassoe7168df2004-10-03 20:11:32 +0000409/* Read up configuration file from config_default_dir. */
410int
411vtysh_read_config (char *config_default_dir)
paul718e3742002-12-13 20:15:29 +0000412{
paul718e3742002-12-13 20:15:29 +0000413 FILE *confp = NULL;
paul718e3742002-12-13 20:15:29 +0000414
hassoe7168df2004-10-03 20:11:32 +0000415 confp = fopen (config_default_dir, "r");
416 if (confp == NULL)
417 return (1);
hasso67e29ab2004-08-26 22:21:31 +0000418
paul718e3742002-12-13 20:15:29 +0000419 vtysh_read_file (confp);
paul718e3742002-12-13 20:15:29 +0000420 fclose (confp);
hassoe7168df2004-10-03 20:11:32 +0000421 host_config_set (config_default_dir);
paul718e3742002-12-13 20:15:29 +0000422
hassoe7168df2004-10-03 20:11:32 +0000423 return (0);
paul718e3742002-12-13 20:15:29 +0000424}
425
hassoe7168df2004-10-03 20:11:32 +0000426/* We don't write vtysh specific into file from vtysh. vtysh.conf should
427 * be edited by hand. So, we handle only "write terminal" case here and
428 * integrate vtysh specific conf with conf from daemons.
429 */
paul718e3742002-12-13 20:15:29 +0000430void
hassoe7168df2004-10-03 20:11:32 +0000431vtysh_config_write ()
paul718e3742002-12-13 20:15:29 +0000432{
hassodda09522004-10-07 21:40:25 +0000433 char line[81];
paul718e3742002-12-13 20:15:29 +0000434 extern struct host host;
435
436 if (host.name)
hassoe7168df2004-10-03 20:11:32 +0000437 {
438 sprintf (line, "hostname %s", host.name);
439 vtysh_config_parse_line(line);
440 }
441 if (vtysh_writeconfig_integrated)
442 vtysh_config_parse_line ("service integrated-vtysh-config");
paul718e3742002-12-13 20:15:29 +0000443}
444
445void
446vtysh_config_init ()
447{
448 config_top = list_new ();
449 config_top->del = (void (*) (void *))line_del;
450 configvec = vector_init (1);
451}