]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh_config.c
Initial revision
[mirror_frr.git] / vtysh / vtysh_config.c
CommitLineData
718e3744 1/* 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
31struct config
32{
33 /* Configuration node name. */
34 char *name;
35
36 /* Configuration string line. */
37 struct list *line;
38
39 /* Configuration can be nest. */
40 struct config *config;
41
42 /* Index of this config. */
43 u_int32_t index;
44};
45
46struct list *config_top;
47\f
48int
49line_cmp (char *c1, char *c2)
50{
51 return strcmp (c1, c2);
52}
53
54void
55line_del (char *line)
56{
57 XFREE (MTYPE_VTYSH_CONFIG_LINE, line);
58}
59
60struct config *
61config_new ()
62{
63 struct config *config;
64 config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config));
65 return config;
66}
67
68int
69config_cmp (struct config *c1, struct config *c2)
70{
71 return strcmp (c1->name, c2->name);
72}
73
74void
75config_del (struct config* config)
76{
77 list_delete (config->line);
78 if (config->name)
79 XFREE (MTYPE_VTYSH_CONFIG_LINE, config->name);
80 XFREE (MTYPE_VTYSH_CONFIG, config);
81}
82
83struct config *
84config_get (int index, char *line)
85{
86 struct config *config;
87 struct config *config_loop;
88 struct list *master;
89 struct listnode *nn;
90
91 config = config_loop = NULL;
92
93 master = vector_lookup_ensure (configvec, index);
94
95 if (! master)
96 {
97 master = list_new ();
98 master->del = (void (*) (void *))config_del;
99 master->cmp = (int (*)(void *, void *)) config_cmp;
100 vector_set_index (configvec, index, master);
101 }
102
103 LIST_LOOP (master, config_loop, nn)
104 {
105 if (strcmp (config_loop->name, line) == 0)
106 config = config_loop;
107 }
108
109 if (! config)
110 {
111 config = config_new ();
112 config->line = list_new ();
113 config->line->del = (void (*) (void *))line_del;
114 config->line->cmp = (int (*)(void *, void *)) line_cmp;
115 config->name = XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line);
116 config->index = index;
117 listnode_add (master, config);
118 }
119 return config;
120}
121
122void
123config_add_line (struct list *config, char *line)
124{
125 listnode_add (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
126}
127
128void
129config_add_line_uniq (struct list *config, char *line)
130{
131 struct listnode *nn;
132 char *pnt;
133
134 LIST_LOOP (config, pnt, nn)
135 {
136 if (strcmp (pnt, line) == 0)
137 return;
138 }
139 listnode_add_sort (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
140}
141
142void
143vtysh_config_parse_line (char *line)
144{
145 char c;
146 static struct config *config = NULL;
147
148 if (! line)
149 return;
150
151 c = line[0];
152
153 if (c == '\0')
154 return;
155
156 /* printf ("[%s]\n", line); */
157
158 switch (c)
159 {
160 case '!':
161 case '#':
162 break;
163 case ' ':
164 /* Store line to current configuration. */
165 if (config)
166 {
167 if (strncmp (line, " address-family vpnv4", strlen (" address-family vpnv4")) == 0)
168 config = config_get (BGP_VPNV4_NODE, line);
169 else if (strncmp (line, " address-family ipv4 multicast", strlen (" address-family ipv4 multicast")) == 0)
170 config = config_get (BGP_IPV4M_NODE, line);
171 else if (strncmp (line, " address-family ipv6", strlen (" address-family ipv6")) == 0)
172 config = config_get (BGP_IPV6_NODE, line);
173 else if (config->index == RMAP_NODE)
174 config_add_line_uniq (config->line, line);
175 else
176 config_add_line (config->line, line);
177 }
178 else
179 config_add_line (config_top, line);
180 break;
181 default:
182 if (strncmp (line, "interface", strlen ("interface")) == 0)
183 config = config_get (INTERFACE_NODE, line);
184 else if (strncmp (line, "router rip", strlen ("router rip")) == 0)
185 config = config_get (RIP_NODE, line);
186 else if (strncmp (line, "router ospf", strlen ("router ospf")) == 0)
187 config = config_get (OSPF_NODE, line);
188 else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
189 config = config_get (BGP_NODE, line);
190 else if (strncmp (line, "router", strlen ("router")) == 0)
191 config = config_get (BGP_NODE, line);
192 else if (strncmp (line, "route-map", strlen ("route-map")) == 0)
193 config = config_get (RMAP_NODE, line);
194 else if (strncmp (line, "access-list", strlen ("access-list")) == 0)
195 config = config_get (ACCESS_NODE, line);
196 else if (strncmp (line, "ip prefix-list", strlen ("ip prefix-list")) == 0)
197 config = config_get (PREFIX_NODE, line);
198 else if (strncmp (line, "ip as-path access-list", strlen ("ip as-path access-list")) == 0)
199 config = config_get (AS_LIST_NODE, line);
200 else if (strncmp (line, "ip community-list", strlen ("ip community-list")) == 0)
201 config = config_get (COMMUNITY_LIST_NODE, line);
202 else if (strncmp (line, "ip route", strlen ("ip route")) == 0)
203 config = config_get (IP_NODE, line);
204 else if (strncmp (line, "key", strlen ("key")) == 0)
205 config = config_get (KEYCHAIN_NODE, line);
206 else
207 {
208 if (strncmp (line, "log", strlen ("log")) == 0
209 || strncmp (line, "hostname", strlen ("hostname")) == 0
210 || strncmp (line, "password", strlen ("hostname")) == 0)
211 config_add_line_uniq (config_top, line);
212 else
213 config_add_line (config_top, line);
214 config = NULL;
215 }
216 break;
217 }
218}
219
220void
221vtysh_config_parse (char *line)
222{
223 char *begin;
224 char *pnt;
225
226 begin = pnt = line;
227
228 while (*pnt != '\0')
229 {
230 if (*pnt == '\n')
231 {
232 *pnt++ = '\0';
233 vtysh_config_parse_line (begin);
234 begin = pnt;
235 }
236 else
237 {
238 pnt++;
239 }
240 }
241}
242
243/* Macro to check delimiter is needed between each configuration line
244 or not. */
245#define NO_DELIMITER(I) \
246 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
247 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE)
248
249/* Display configuration to file pointer. */
250void
251vtysh_config_dump (FILE *fp)
252{
253 struct listnode *nn;
254 struct listnode *nm;
255 struct config *config;
256 struct list *master;
257 char *line;
258 int i;
259
260 LIST_LOOP (config_top, line, nn)
261 {
262 fprintf (fp, "%s\n", line);
263 fflush (fp);
264 }
265 fprintf (fp, "!\n");
266 fflush (fp);
267
268 for (i = 0; i < vector_max (configvec); i++)
269 if ((master = vector_slot (configvec, i)) != NULL)
270 {
271 LIST_LOOP (master, config, nn)
272 {
273 fprintf (fp, "%s\n", config->name);
274 fflush (fp);
275
276 LIST_LOOP (config->line, line, nm)
277 {
278 fprintf (fp, "%s\n", line);
279 fflush (fp);
280 }
281 if (! NO_DELIMITER (i))
282 {
283 fprintf (fp, "!\n");
284 fflush (fp);
285 }
286 }
287 if (NO_DELIMITER (i))
288 {
289 fprintf (fp, "!\n");
290 fflush (fp);
291 }
292 }
293
294 for (i = 0; i < vector_max (configvec); i++)
295 if ((master = vector_slot (configvec, i)) != NULL)
296 {
297 list_delete (master);
298 vector_slot (configvec, i) = NULL;
299 }
300 list_delete_all_node (config_top);
301}
302
303/* Read up configuration file from file_name. */
304static void
305vtysh_read_file (FILE *confp)
306{
307 int ret;
308 struct vty *vty;
309
310 vty = vty_new ();
311 vty->fd = 0; /* stdout */
312 vty->type = VTY_TERM;
313 vty->node = CONFIG_NODE;
314
315 vtysh_execute_no_pager ("enable");
316 vtysh_execute_no_pager ("configure terminal");
317
318 /* Execute configuration file */
319 ret = vtysh_config_from_file (vty, confp);
320
321 vtysh_execute_no_pager ("end");
322 vtysh_execute_no_pager ("disable");
323
324 vty_close (vty);
325
326 if (ret != CMD_SUCCESS)
327 {
328 switch (ret)
329 {
330 case CMD_ERR_AMBIGUOUS:
331 fprintf (stderr, "Ambiguous command.\n");
332 break;
333 case CMD_ERR_NO_MATCH:
334 fprintf (stderr, "There is no such command.\n");
335 break;
336 }
337 fprintf (stderr, "Error occured during reading below line.\n%s\n",
338 vty->buf);
339 exit (1);
340 }
341}
342
343/* Read up configuration file from file_name. */
344void
345vtysh_read_config (char *config_file,
346 char *config_current_dir,
347 char *config_default_dir)
348{
349 char *cwd;
350 FILE *confp = NULL;
351 char *fullpath;
352
353 /* If -f flag specified. */
354 if (config_file != NULL)
355 {
356 if (! IS_DIRECTORY_SEP (config_file[0]))
357 {
358 cwd = getcwd (NULL, MAXPATHLEN);
359 fullpath = XMALLOC (MTYPE_TMP,
360 strlen (cwd) + strlen (config_file) + 2);
361 sprintf (fullpath, "%s/%s", cwd, config_file);
362 }
363 else
364 fullpath = config_file;
365
366 confp = fopen (fullpath, "r");
367
368 if (confp == NULL)
369 {
370 fprintf (stderr, "can't open configuration file [%s]\n",
371 config_file);
372 exit(1);
373 }
374 }
375 else
376 {
377 /* Relative path configuration file open. */
378 if (config_current_dir)
379 confp = fopen (config_current_dir, "r");
380
381 /* If there is no relative path exists, open system default file. */
382 if (confp == NULL)
383 {
384 confp = fopen (config_default_dir, "r");
385 if (confp == NULL)
386 {
387 fprintf (stderr, "can't open configuration file [%s]\n",
388 config_default_dir);
389 exit (1);
390 }
391 else
392 fullpath = config_default_dir;
393 }
394 else
395 {
396 /* Rleative path configuration file. */
397 cwd = getcwd (NULL, MAXPATHLEN);
398 fullpath = XMALLOC (MTYPE_TMP,
399 strlen (cwd) + strlen (config_current_dir) + 2);
400 sprintf (fullpath, "%s/%s", cwd, config_current_dir);
401 }
402 }
403 vtysh_read_file (confp);
404
405 fclose (confp);
406
407 host_config_set (fullpath);
408}
409
410void
411vtysh_config_write (FILE *fp)
412{
413 extern struct host host;
414
415 if (host.name)
416 fprintf (fp, "hostname %s\n", host.name);
417 fprintf (fp, "!\n");
418}
419
420void
421vtysh_config_init ()
422{
423 config_top = list_new ();
424 config_top->del = (void (*) (void *))line_del;
425 configvec = vector_init (1);
426}