]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh_config.c
pimd: Modify order of command output for vty output
[mirror_frr.git] / vtysh / vtysh_config.c
CommitLineData
718e3744 1/* Configuration generator.
896014f4
DL
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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#include <zebra.h>
22
23#include "command.h"
24#include "linklist.h"
25#include "memory.h"
26
27#include "vtysh/vtysh.h"
88177fe3 28#include "vtysh/vtysh_user.h"
718e3744 29
4a1ab8e4 30DEFINE_MGROUP(MVTYSH, "vtysh")
d62a17ae 31DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG, "Vtysh configuration")
4a1ab8e4
DL
32DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG_LINE, "Vtysh configuration line")
33
718e3744 34vector configvec;
35
d62a17ae 36struct config {
37 /* Configuration node name. */
38 char *name;
718e3744 39
d62a17ae 40 /* Configuration string line. */
41 struct list *line;
718e3744 42
d62a17ae 43 /* Configuration can be nest. */
44 struct config *config;
718e3744 45
d62a17ae 46 /* Index of this config. */
d7c0a89a 47 uint32_t index;
718e3744 48};
49
50struct list *config_top;
95e735b5 51
d62a17ae 52static int line_cmp(char *c1, char *c2)
718e3744 53{
d62a17ae 54 return strcmp(c1, c2);
718e3744 55}
56
d62a17ae 57static void line_del(char *line)
718e3744 58{
d62a17ae 59 XFREE(MTYPE_VTYSH_CONFIG_LINE, line);
718e3744 60}
61
d62a17ae 62static struct config *config_new(void)
718e3744 63{
d62a17ae 64 struct config *config;
65 config = XCALLOC(MTYPE_VTYSH_CONFIG, sizeof(struct config));
66 return config;
718e3744 67}
68
d62a17ae 69static int config_cmp(struct config *c1, struct config *c2)
718e3744 70{
d62a17ae 71 return strcmp(c1->name, c2->name);
718e3744 72}
73
d62a17ae 74static void config_del(struct config *config)
718e3744 75{
affe9e99 76 list_delete_and_null(&config->line);
d62a17ae 77 if (config->name)
78 XFREE(MTYPE_VTYSH_CONFIG_LINE, config->name);
79 XFREE(MTYPE_VTYSH_CONFIG, config);
718e3744 80}
81
d62a17ae 82static struct config *config_get(int index, const char *line)
718e3744 83{
d62a17ae 84 struct config *config;
85 struct config *config_loop;
86 struct list *master;
87 struct listnode *node, *nnode;
88
89 config = config_loop = NULL;
90
91 master = vector_lookup_ensure(configvec, index);
92
93 if (!master) {
94 master = list_new();
95 master->del = (void (*)(void *))config_del;
96 master->cmp = (int (*)(void *, void *))config_cmp;
97 vector_set_index(configvec, index, master);
98 }
99
100 for (ALL_LIST_ELEMENTS(master, node, nnode, config_loop)) {
101 if (strcmp(config_loop->name, line) == 0)
102 config = config_loop;
103 }
104
105 if (!config) {
106 config = config_new();
107 config->line = list_new();
108 config->line->del = (void (*)(void *))line_del;
109 config->line->cmp = (int (*)(void *, void *))line_cmp;
110 config->name = XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line);
111 config->index = index;
112 listnode_add(master, config);
113 }
114 return config;
718e3744 115}
116
d62a17ae 117void config_add_line(struct list *config, const char *line)
718e3744 118{
d62a17ae 119 listnode_add(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line));
718e3744 120}
121
d62a17ae 122static void config_add_line_uniq(struct list *config, const char *line)
718e3744 123{
d62a17ae 124 struct listnode *node, *nnode;
125 char *pnt;
126
127 for (ALL_LIST_ELEMENTS(config, node, nnode, pnt)) {
128 if (strcmp(pnt, line) == 0)
129 return;
130 }
131 listnode_add_sort(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line));
718e3744 132}
133
a3c1db5e
DS
134/*
135 * I want to explicitly move this command to the end of the line
136 */
137static void config_add_line_end(struct list *config, const char *line)
138{
139 struct listnode *node;
140 void *item = XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line);
141
142 listnode_add(config, item);
143 node = listnode_lookup(config, item);
144 if (node)
145 listnode_move_to_tail(config, node);
146}
147
d62a17ae 148void vtysh_config_parse_line(void *arg, const char *line)
718e3744 149{
d62a17ae 150 char c;
151 static struct config *config = NULL;
152
153 if (!line)
154 return;
155
156 c = line[0];
157
158 if (c == '\0')
159 return;
160
161 /* printf ("[%s]\n", line); */
162
163 switch (c) {
164 /* Suppress exclamation points ! and commented lines. The !s are
165 * generated
166 * dynamically in vtysh_config_dump() */
167 case '!':
168 case '#':
169 break;
170 case ' ':
171 /* Store line to current configuration. */
172 if (config) {
173 if (strncmp(line, " link-params",
174 strlen(" link-params"))
175 == 0) {
176 config_add_line(config->line, line);
177 config->index = LINK_PARAMS_NODE;
996c9314
LB
178 } else if (strncmp(line, " ip multicast boundary",
179 strlen(" ip multicast boundary"))
180 == 0) {
a3c1db5e 181 config_add_line_end(config->line, line);
d62a17ae 182 } else if (config->index == LINK_PARAMS_NODE
183 && strncmp(line, " exit-link-params",
184 strlen(" exit"))
185 == 0) {
186 config_add_line(config->line, line);
187 config->index = INTERFACE_NODE;
c319e19d
QY
188 } else if (config->index == VRF_NODE
189 && strncmp(line, " exit-vrf",
190 strlen(" exit-vrf"))
191 == 0) {
192 config_add_line(config->line, line);
193 config->index = CONFIG_NODE;
d62a17ae 194 } else if (config->index == RMAP_NODE
195 || config->index == INTERFACE_NODE
f5d20fdb 196 || config->index == LOGICALROUTER_NODE
d62a17ae 197 || config->index == VTY_NODE
198 || config->index == VRF_NODE)
199 config_add_line_uniq(config->line, line);
200 else
201 config_add_line(config->line, line);
202 } else
203 config_add_line(config_top, line);
204 break;
205 default:
206 if (strncmp(line, "interface", strlen("interface")) == 0)
207 config = config_get(INTERFACE_NODE, line);
2dd0d726
RW
208 else if (strncmp(line, "pseudowire", strlen("pseudowire")) == 0)
209 config = config_get(PW_NODE, line);
34f6bdbe 210 else if (strncmp(line, "logical-router", strlen("logical-router")) == 0)
f5d20fdb 211 config = config_get(LOGICALROUTER_NODE, line);
d62a17ae 212 else if (strncmp(line, "vrf", strlen("vrf")) == 0)
213 config = config_get(VRF_NODE, line);
e5c83d9b
DS
214 else if (strncmp(line, "nexthop-group", strlen("nexthop-group"))
215 == 0)
216 config = config_get(NH_GROUP_NODE, line);
d62a17ae 217 else if (strncmp(line, "router-id", strlen("router-id")) == 0)
218 config = config_get(ZEBRA_NODE, line);
219 else if (strncmp(line, "router rip", strlen("router rip")) == 0)
220 config = config_get(RIP_NODE, line);
221 else if (strncmp(line, "router ripng", strlen("router ripng"))
222 == 0)
223 config = config_get(RIPNG_NODE, line);
224 else if (strncmp(line, "router eigrp", strlen("router eigrp"))
225 == 0)
226 config = config_get(EIGRP_NODE, line);
227 else if (strncmp(line, "router babel", strlen("router babel"))
228 == 0)
229 config = config_get(BABEL_NODE, line);
230 else if (strncmp(line, "router ospf", strlen("router ospf"))
231 == 0)
232 config = config_get(OSPF_NODE, line);
233 else if (strncmp(line, "router ospf6", strlen("router ospf6"))
234 == 0)
235 config = config_get(OSPF6_NODE, line);
236 else if (strncmp(line, "mpls ldp", strlen("mpls ldp")) == 0)
237 config = config_get(LDP_NODE, line);
238 else if (strncmp(line, "l2vpn", strlen("l2vpn")) == 0)
239 config = config_get(LDP_L2VPN_NODE, line);
240 else if (strncmp(line, "router bgp", strlen("router bgp")) == 0)
241 config = config_get(BGP_NODE, line);
242 else if (strncmp(line, "router isis", strlen("router isis"))
243 == 0)
244 config = config_get(ISIS_NODE, line);
245 else if (strncmp(line, "route-map", strlen("route-map")) == 0)
246 config = config_get(RMAP_NODE, line);
e5c83d9b
DS
247 else if (strncmp(line, "pbr-map", strlen("pbr-map")) == 0)
248 config = config_get(PBRMAP_NODE, line);
d62a17ae 249 else if (strncmp(line, "access-list", strlen("access-list"))
250 == 0)
251 config = config_get(ACCESS_NODE, line);
252 else if (strncmp(line, "ipv6 access-list",
253 strlen("ipv6 access-list"))
254 == 0)
255 config = config_get(ACCESS_IPV6_NODE, line);
d37ba549
MK
256 else if (strncmp(line, "mac access-list",
257 strlen("mac access-list"))
258 == 0)
259 config = config_get(ACCESS_MAC_NODE, line);
d62a17ae 260 else if (strncmp(line, "ip prefix-list",
261 strlen("ip prefix-list"))
262 == 0)
263 config = config_get(PREFIX_NODE, line);
264 else if (strncmp(line, "ipv6 prefix-list",
265 strlen("ipv6 prefix-list"))
266 == 0)
267 config = config_get(PREFIX_IPV6_NODE, line);
268 else if (strncmp(line, "ip as-path access-list",
269 strlen("ip as-path access-list"))
270 == 0)
271 config = config_get(AS_LIST_NODE, line);
272 else if (strncmp(line, "ip community-list",
273 strlen("ip community-list"))
274 == 0
275 || strncmp(line, "ip extcommunity-list",
276 strlen("ip extcommunity-list"))
996c9314 277 == 0
344d9016
NK
278 || strncmp(line, "ip large-community-list",
279 strlen("ip large-community-list"))
996c9314 280 == 0)
d62a17ae 281 config = config_get(COMMUNITY_LIST_NODE, line);
282 else if (strncmp(line, "ip route", strlen("ip route")) == 0)
283 config = config_get(IP_NODE, line);
284 else if (strncmp(line, "ipv6 route", strlen("ipv6 route")) == 0)
285 config = config_get(IP_NODE, line);
286 else if (strncmp(line, "key", strlen("key")) == 0)
287 config = config_get(KEYCHAIN_NODE, line);
288 else if (strncmp(line, "line", strlen("line")) == 0)
289 config = config_get(VTY_NODE, line);
290 else if ((strncmp(line, "ipv6 forwarding",
291 strlen("ipv6 forwarding"))
292 == 0)
293 || (strncmp(line, "ip forwarding",
294 strlen("ip forwarding"))
295 == 0))
296 config = config_get(FORWARDING_NODE, line);
d62a17ae 297 else if (strncmp(line, "debug vrf", strlen("debug vrf")) == 0)
298 config = config_get(VRF_DEBUG_NODE, line);
299 else if (strncmp(line, "debug", strlen("debug")) == 0)
300 config = config_get(DEBUG_NODE, line);
301 else if (strncmp(line, "password", strlen("password")) == 0
302 || strncmp(line, "enable password",
303 strlen("enable password"))
304 == 0)
305 config = config_get(AAA_NODE, line);
306 else if (strncmp(line, "ip protocol", strlen("ip protocol"))
307 == 0)
308 config = config_get(PROTOCOL_NODE, line);
309 else if (strncmp(line, "ipv6 protocol", strlen("ipv6 protocol"))
310 == 0)
311 config = config_get(PROTOCOL_NODE, line);
312 else if (strncmp(line, "ip nht", strlen("ip nht")) == 0)
313 config = config_get(PROTOCOL_NODE, line);
314 else if (strncmp(line, "ipv6 nht", strlen("ipv6 nht")) == 0)
315 config = config_get(PROTOCOL_NODE, line);
316 else if (strncmp(line, "mpls", strlen("mpls")) == 0)
317 config = config_get(MPLS_NODE, line);
318 else {
319 if (strncmp(line, "log", strlen("log")) == 0
320 || strncmp(line, "hostname", strlen("hostname"))
321 == 0
322 || strncmp(line, "frr", strlen("frr")) == 0
323 || strncmp(line, "agentx", strlen("agentx")) == 0
324 || strncmp(line, "no log", strlen("no log")) == 0)
325 config_add_line_uniq(config_top, line);
326 else
327 config_add_line(config_top, line);
328 config = NULL;
329 }
330 break;
718e3744 331 }
718e3744 332}
333
718e3744 334/* Macro to check delimiter is needed between each configuration line
95e735b5 335 * or not. */
d62a17ae 336#define NO_DELIMITER(I) \
337 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
338 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \
d37ba549 339 || (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE \
f108d487
QY
340 || (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE \
341 || (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE \
342 || (I) == MPLS_NODE)
718e3744 343
95e735b5 344/* Display configuration to file pointer. */
0a334343 345void vtysh_config_dump(void)
718e3744 346{
d62a17ae 347 struct listnode *node, *nnode;
348 struct listnode *mnode, *mnnode;
349 struct config *config;
350 struct list *master;
351 char *line;
352 unsigned int i;
353
2cddf2ff
QY
354 for (ALL_LIST_ELEMENTS(config_top, node, nnode, line))
355 vty_out(vty, "%s\n", line);
356
357 vty_out(vty, "!\n");
d62a17ae 358
359 for (i = 0; i < vector_active(configvec); i++)
360 if ((master = vector_slot(configvec, i)) != NULL) {
361 for (ALL_LIST_ELEMENTS(master, node, nnode, config)) {
1d72e48a 362 /* Don't print empty sections for interface.
d62a17ae 363 * Route maps on the
364 * other hand could have a legitimate empty
365 * section at the end.
1d72e48a 366 * VRF is handled in the backend, we could have
367 * "configured" VRFs with static routes which
368 * are not under the VRF node.
d62a17ae 369 */
1d72e48a 370 if (config->index == INTERFACE_NODE
d62a17ae 371 && list_isempty(config->line))
372 continue;
373
2cddf2ff 374 vty_out(vty, "%s\n", config->name);
d62a17ae 375
376 for (ALL_LIST_ELEMENTS(config->line, mnode,
2cddf2ff
QY
377 mnnode, line))
378 vty_out(vty, "%s\n", line);
379 if (!NO_DELIMITER(i))
380 vty_out(vty, "!\n");
d62a17ae 381 }
2cddf2ff
QY
382 if (NO_DELIMITER(i))
383 vty_out(vty, "!\n");
d62a17ae 384 }
385
386 for (i = 0; i < vector_active(configvec); i++)
387 if ((master = vector_slot(configvec, i)) != NULL) {
affe9e99 388 list_delete_and_null(&master);
d62a17ae 389 vector_slot(configvec, i) = NULL;
390 }
391 list_delete_all_node(config_top);
718e3744 392}
393
394/* Read up configuration file from file_name. */
d62a17ae 395static int vtysh_read_file(FILE *confp)
718e3744 396{
d62a17ae 397 struct vty *vty;
398 int ret;
399
400 vty = vty_new();
4a9746fd 401 vty->wfd = STDERR_FILENO;
d62a17ae 402 vty->type = VTY_TERM;
403 vty->node = CONFIG_NODE;
718e3744 404
d62a17ae 405 vtysh_execute_no_pager("enable");
406 vtysh_execute_no_pager("configure terminal");
718e3744 407
d62a17ae 408 /* Execute configuration file. */
409 ret = vtysh_config_from_file(vty, confp);
718e3744 410
d62a17ae 411 vtysh_execute_no_pager("end");
412 vtysh_execute_no_pager("disable");
718e3744 413
d62a17ae 414 vty_close(vty);
3221dca8 415
d62a17ae 416 return (ret);
718e3744 417}
418
e7168df4 419/* Read up configuration file from config_default_dir. */
d62a17ae 420int vtysh_read_config(const char *config_default_dir)
718e3744 421{
d62a17ae 422 FILE *confp = NULL;
423 int ret;
424
425 confp = fopen(config_default_dir, "r");
426 if (confp == NULL) {
427 fprintf(stderr,
428 "%% Can't open configuration file %s due to '%s'.\n",
429 config_default_dir, safe_strerror(errno));
430 return (CMD_ERR_NO_FILE);
431 }
67e29abc 432
d62a17ae 433 ret = vtysh_read_file(confp);
434 fclose(confp);
718e3744 435
d62a17ae 436 return (ret);
718e3744 437}
438
e7168df4 439/* We don't write vtysh specific into file from vtysh. vtysh.conf should
440 * be edited by hand. So, we handle only "write terminal" case here and
441 * integrate vtysh specific conf with conf from daemons.
442 */
d62a17ae 443void vtysh_config_write()
718e3744 444{
d62a17ae 445 char line[81];
d62a17ae 446
6b3ee3a0
MK
447 if (cmd_hostname_get()) {
448 sprintf(line, "hostname %s", cmd_hostname_get());
d62a17ae 449 vtysh_config_parse_line(NULL, line);
450 }
3b103fec
MK
451
452 if (cmd_domainname_get()) {
453 sprintf(line, "domainname %s", cmd_domainname_get());
454 vtysh_config_parse_line(NULL, line);
455 }
d62a17ae 456 if (vtysh_write_integrated == WRITE_INTEGRATED_NO)
457 vtysh_config_parse_line(NULL,
458 "no service integrated-vtysh-config");
459 if (vtysh_write_integrated == WRITE_INTEGRATED_YES)
460 vtysh_config_parse_line(NULL,
461 "service integrated-vtysh-config");
462
463 user_config_write();
718e3744 464}
465
d62a17ae 466void vtysh_config_init()
718e3744 467{
d62a17ae 468 config_top = list_new();
469 config_top->del = (void (*)(void *))line_del;
470 configvec = vector_init(1);
718e3744 471}