]>
Commit | Line | Data |
---|---|---|
1 | /* Configuration generator. | |
2 | Copyright (C) 2000 Kunihiro Ishiguro | |
3 | ||
4 | This file is part of GNU Zebra. | |
5 | ||
6 | GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | GNU Zebra is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Zebra; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-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 | ||
29 | vector configvec; | |
30 | ||
31 | struct 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 | ||
46 | struct list *config_top; | |
47 | \f | |
48 | int | |
49 | line_cmp (char *c1, char *c2) | |
50 | { | |
51 | return strcmp (c1, c2); | |
52 | } | |
53 | ||
54 | void | |
55 | line_del (char *line) | |
56 | { | |
57 | XFREE (MTYPE_VTYSH_CONFIG_LINE, line); | |
58 | } | |
59 | ||
60 | struct config * | |
61 | config_new () | |
62 | { | |
63 | struct config *config; | |
64 | config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config)); | |
65 | return config; | |
66 | } | |
67 | ||
68 | int | |
69 | config_cmp (struct config *c1, struct config *c2) | |
70 | { | |
71 | return strcmp (c1->name, c2->name); | |
72 | } | |
73 | ||
74 | void | |
75 | config_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 | ||
83 | struct config * | |
84 | config_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 | ||
122 | void | |
123 | config_add_line (struct list *config, char *line) | |
124 | { | |
125 | listnode_add (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line)); | |
126 | } | |
127 | ||
128 | void | |
129 | config_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 | ||
142 | void | |
143 | vtysh_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 || config->index == INTERFACE_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 ripng", strlen ("router ripng")) == 0) | |
187 | config = config_get (RIPNG_NODE, line); | |
188 | else if (strncmp (line, "router ospf", strlen ("router ospf")) == 0) | |
189 | config = config_get (OSPF_NODE, line); | |
190 | else if (strncmp (line, "router ospf6", strlen ("router ospf6")) == 0) | |
191 | config = config_get (OSPF6_NODE, line); | |
192 | else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0) | |
193 | config = config_get (BGP_NODE, line); | |
194 | else if (strncmp (line, "router", strlen ("router")) == 0) | |
195 | config = config_get (BGP_NODE, line); | |
196 | else if (strncmp (line, "route-map", strlen ("route-map")) == 0) | |
197 | config = config_get (RMAP_NODE, line); | |
198 | else if (strncmp (line, "access-list", strlen ("access-list")) == 0) | |
199 | config = config_get (ACCESS_NODE, line); | |
200 | else if (strncmp (line, "ipv6 access-list", strlen ("ipv6 access-list")) == 0) | |
201 | config = config_get (ACCESS_IPV6_NODE, line); | |
202 | else if (strncmp (line, "ip prefix-list", strlen ("ip prefix-list")) == 0) | |
203 | config = config_get (PREFIX_NODE, line); | |
204 | else if (strncmp (line, "ipv6 prefix-list", strlen ("ipv6 prefix-list")) == 0) | |
205 | config = config_get (PREFIX_IPV6_NODE, line); | |
206 | else if (strncmp (line, "ip as-path access-list", strlen ("ip as-path access-list")) == 0) | |
207 | config = config_get (AS_LIST_NODE, line); | |
208 | else if (strncmp (line, "ip community-list", strlen ("ip community-list")) == 0) | |
209 | config = config_get (COMMUNITY_LIST_NODE, line); | |
210 | else if (strncmp (line, "ip route", strlen ("ip route")) == 0) | |
211 | config = config_get (IP_NODE, line); | |
212 | else if (strncmp (line, "ipv6 route", strlen ("ipv6 route")) == 0) | |
213 | config = config_get (IP_NODE, line); | |
214 | else if (strncmp (line, "key", strlen ("key")) == 0) | |
215 | config = config_get (KEYCHAIN_NODE, line); | |
216 | else | |
217 | { | |
218 | if (strncmp (line, "log", strlen ("log")) == 0 | |
219 | || strncmp (line, "hostname", strlen ("hostname")) == 0 | |
220 | || strncmp (line, "password", strlen ("hostname")) == 0) | |
221 | config_add_line_uniq (config_top, line); | |
222 | else | |
223 | config_add_line (config_top, line); | |
224 | config = NULL; | |
225 | } | |
226 | break; | |
227 | } | |
228 | } | |
229 | ||
230 | void | |
231 | vtysh_config_parse (char *line) | |
232 | { | |
233 | char *begin; | |
234 | char *pnt; | |
235 | ||
236 | begin = pnt = line; | |
237 | ||
238 | while (*pnt != '\0') | |
239 | { | |
240 | if (*pnt == '\n') | |
241 | { | |
242 | *pnt++ = '\0'; | |
243 | vtysh_config_parse_line (begin); | |
244 | begin = pnt; | |
245 | } | |
246 | else | |
247 | { | |
248 | pnt++; | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
253 | /* Macro to check delimiter is needed between each configuration line | |
254 | or not. */ | |
255 | #define NO_DELIMITER(I) \ | |
256 | ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \ | |
257 | || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE || \ | |
258 | (I) == ACCESS_IPV6_NODE || (I) == PREFIX_IPV6_NODE) | |
259 | ||
260 | /* Display configuration to file pointer. */ | |
261 | void | |
262 | vtysh_config_dump (FILE *fp) | |
263 | { | |
264 | struct listnode *nn; | |
265 | struct listnode *nm; | |
266 | struct config *config; | |
267 | struct list *master; | |
268 | char *line; | |
269 | int i; | |
270 | ||
271 | LIST_LOOP (config_top, line, nn) | |
272 | { | |
273 | fprintf (fp, "%s\n", line); | |
274 | fflush (fp); | |
275 | } | |
276 | fprintf (fp, "!\n"); | |
277 | fflush (fp); | |
278 | ||
279 | for (i = 0; i < vector_max (configvec); i++) | |
280 | if ((master = vector_slot (configvec, i)) != NULL) | |
281 | { | |
282 | LIST_LOOP (master, config, nn) | |
283 | { | |
284 | fprintf (fp, "%s\n", config->name); | |
285 | fflush (fp); | |
286 | ||
287 | LIST_LOOP (config->line, line, nm) | |
288 | { | |
289 | fprintf (fp, "%s\n", line); | |
290 | fflush (fp); | |
291 | } | |
292 | if (! NO_DELIMITER (i)) | |
293 | { | |
294 | fprintf (fp, "!\n"); | |
295 | fflush (fp); | |
296 | } | |
297 | } | |
298 | if (NO_DELIMITER (i)) | |
299 | { | |
300 | fprintf (fp, "!\n"); | |
301 | fflush (fp); | |
302 | } | |
303 | } | |
304 | ||
305 | for (i = 0; i < vector_max (configvec); i++) | |
306 | if ((master = vector_slot (configvec, i)) != NULL) | |
307 | { | |
308 | list_delete (master); | |
309 | vector_slot (configvec, i) = NULL; | |
310 | } | |
311 | list_delete_all_node (config_top); | |
312 | } | |
313 | ||
314 | /* Read up configuration file from file_name. */ | |
315 | static void | |
316 | vtysh_read_file (FILE *confp) | |
317 | { | |
318 | int ret; | |
319 | struct vty *vty; | |
320 | ||
321 | vty = vty_new (); | |
322 | vty->fd = 0; /* stdout */ | |
323 | vty->type = VTY_TERM; | |
324 | vty->node = CONFIG_NODE; | |
325 | ||
326 | vtysh_execute_no_pager ("enable"); | |
327 | vtysh_execute_no_pager ("configure terminal"); | |
328 | ||
329 | /* Execute configuration file */ | |
330 | ret = vtysh_config_from_file (vty, confp); | |
331 | ||
332 | vtysh_execute_no_pager ("end"); | |
333 | vtysh_execute_no_pager ("disable"); | |
334 | ||
335 | vty_close (vty); | |
336 | ||
337 | if (ret != CMD_SUCCESS) | |
338 | { | |
339 | switch (ret) | |
340 | { | |
341 | case CMD_ERR_AMBIGUOUS: | |
342 | fprintf (stderr, "Ambiguous command.\n"); | |
343 | break; | |
344 | case CMD_ERR_NO_MATCH: | |
345 | fprintf (stderr, "There is no such command.\n"); | |
346 | break; | |
347 | } | |
348 | fprintf (stderr, "Error occured during reading below line.\n%s\n", | |
349 | vty->buf); | |
350 | exit (1); | |
351 | } | |
352 | } | |
353 | ||
354 | /* Read up configuration file from file_name. */ | |
355 | void | |
356 | vtysh_read_config (char *config_file, | |
357 | char *config_current_dir, | |
358 | char *config_default_dir) | |
359 | { | |
360 | char *cwd; | |
361 | FILE *confp = NULL; | |
362 | char *fullpath; | |
363 | ||
364 | /* If -f flag specified. */ | |
365 | if (config_file != NULL) | |
366 | { | |
367 | if (! IS_DIRECTORY_SEP (config_file[0])) | |
368 | { | |
369 | cwd = getcwd (NULL, MAXPATHLEN); | |
370 | fullpath = XMALLOC (MTYPE_TMP, | |
371 | strlen (cwd) + strlen (config_file) + 2); | |
372 | sprintf (fullpath, "%s/%s", cwd, config_file); | |
373 | } | |
374 | else | |
375 | fullpath = config_file; | |
376 | ||
377 | confp = fopen (fullpath, "r"); | |
378 | ||
379 | if (confp == NULL) | |
380 | { | |
381 | fprintf (stderr, "can't open configuration file [%s]\n", | |
382 | config_file); | |
383 | exit(1); | |
384 | } | |
385 | } | |
386 | else | |
387 | { | |
388 | /* Relative path configuration file open. */ | |
389 | if (config_current_dir) | |
390 | confp = fopen (config_current_dir, "r"); | |
391 | ||
392 | /* If there is no relative path exists, open system default file. */ | |
393 | if (confp == NULL) | |
394 | { | |
395 | confp = fopen (config_default_dir, "r"); | |
396 | if (confp == NULL) | |
397 | { | |
398 | fprintf (stderr, "can't open configuration file [%s]\n", | |
399 | config_default_dir); | |
400 | exit (1); | |
401 | } | |
402 | else | |
403 | fullpath = config_default_dir; | |
404 | } | |
405 | else | |
406 | { | |
407 | /* Rleative path configuration file. */ | |
408 | cwd = getcwd (NULL, MAXPATHLEN); | |
409 | fullpath = XMALLOC (MTYPE_TMP, | |
410 | strlen (cwd) + strlen (config_current_dir) + 2); | |
411 | sprintf (fullpath, "%s/%s", cwd, config_current_dir); | |
412 | } | |
413 | } | |
414 | vtysh_read_file (confp); | |
415 | ||
416 | fclose (confp); | |
417 | ||
418 | host_config_set (fullpath); | |
419 | } | |
420 | ||
421 | void | |
422 | vtysh_config_write (FILE *fp) | |
423 | { | |
424 | extern struct host host; | |
425 | ||
426 | if (host.name) | |
427 | fprintf (fp, "hostname %s\n", host.name); | |
428 | fprintf (fp, "!\n"); | |
429 | } | |
430 | ||
431 | void | |
432 | vtysh_config_init () | |
433 | { | |
434 | config_top = list_new (); | |
435 | config_top->del = (void (*) (void *))line_del; | |
436 | configvec = vector_init (1); | |
437 | } |