vtysh_config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* Configuration generator.
  2. Copyright (C) 2000 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "command.h"
  18. #include "linklist.h"
  19. #include "memory.h"
  20. #include "vtysh/vtysh.h"
  21. vector configvec;
  22. extern int vtysh_writeconfig_integrated;
  23. struct config
  24. {
  25. /* Configuration node name. */
  26. char *name;
  27. /* Configuration string line. */
  28. struct list *line;
  29. /* Configuration can be nest. */
  30. struct config *config;
  31. /* Index of this config. */
  32. u_int32_t index;
  33. };
  34. struct list *config_top;
  35. int
  36. line_cmp (char *c1, char *c2)
  37. {
  38. return strcmp (c1, c2);
  39. }
  40. void
  41. line_del (char *line)
  42. {
  43. XFREE (MTYPE_VTYSH_CONFIG_LINE, line);
  44. }
  45. struct config *
  46. config_new ()
  47. {
  48. struct config *config;
  49. config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config));
  50. return config;
  51. }
  52. int
  53. config_cmp (struct config *c1, struct config *c2)
  54. {
  55. return strcmp (c1->name, c2->name);
  56. }
  57. void
  58. config_del (struct config* config)
  59. {
  60. list_delete (config->line);
  61. if (config->name)
  62. XFREE (MTYPE_VTYSH_CONFIG_LINE, config->name);
  63. XFREE (MTYPE_VTYSH_CONFIG, config);
  64. }
  65. struct config *
  66. config_get (int index, const char *line)
  67. {
  68. struct config *config;
  69. struct config *config_loop;
  70. struct list *master;
  71. struct listnode *node, *nnode;
  72. config = config_loop = NULL;
  73. master = vector_lookup_ensure (configvec, index);
  74. if (! master)
  75. {
  76. master = list_new ();
  77. master->del = (void (*) (void *))config_del;
  78. master->cmp = (int (*)(void *, void *)) config_cmp;
  79. vector_set_index (configvec, index, master);
  80. }
  81. for (ALL_LIST_ELEMENTS (master, node, nnode, config_loop))
  82. {
  83. if (strcmp (config_loop->name, line) == 0)
  84. config = config_loop;
  85. }
  86. if (! config)
  87. {
  88. config = config_new ();
  89. config->line = list_new ();
  90. config->line->del = (void (*) (void *))line_del;
  91. config->line->cmp = (int (*)(void *, void *)) line_cmp;
  92. config->name = XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line);
  93. config->index = index;
  94. listnode_add (master, config);
  95. }
  96. return config;
  97. }
  98. void
  99. config_add_line (struct list *config, const char *line)
  100. {
  101. listnode_add (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
  102. }
  103. void
  104. config_add_line_uniq (struct list *config, const char *line)
  105. {
  106. struct listnode *node, *nnode;
  107. char *pnt;
  108. for (ALL_LIST_ELEMENTS (config, node, nnode, pnt))
  109. {
  110. if (strcmp (pnt, line) == 0)
  111. return;
  112. }
  113. listnode_add_sort (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
  114. }
  115. void
  116. vtysh_config_parse_line (const char *line)
  117. {
  118. char c;
  119. static struct config *config = NULL;
  120. if (! line)
  121. return;
  122. c = line[0];
  123. if (c == '\0')
  124. return;
  125. /* printf ("[%s]\n", line); */
  126. switch (c)
  127. {
  128. case '!':
  129. case '#':
  130. break;
  131. case ' ':
  132. /* Store line to current configuration. */
  133. if (config)
  134. {
  135. if (strncmp (line, " address-family vpnv4",
  136. strlen (" address-family vpnv4")) == 0)
  137. config = config_get (BGP_VPNV4_NODE, line);
  138. else if (strncmp (line, " address-family ipv4 multicast",
  139. strlen (" address-family ipv4 multicast")) == 0)
  140. config = config_get (BGP_IPV4M_NODE, line);
  141. else if (strncmp (line, " address-family ipv6",
  142. strlen (" address-family ipv6")) == 0)
  143. config = config_get (BGP_IPV6_NODE, line);
  144. else if (config->index == RMAP_NODE ||
  145. config->index == INTERFACE_NODE ||
  146. config->index == VTY_NODE)
  147. config_add_line_uniq (config->line, line);
  148. else
  149. config_add_line (config->line, line);
  150. }
  151. else
  152. config_add_line (config_top, line);
  153. break;
  154. default:
  155. if (strncmp (line, "interface", strlen ("interface")) == 0)
  156. config = config_get (INTERFACE_NODE, line);
  157. else if (strncmp (line, "router-id", strlen ("router-id")) == 0)
  158. config = config_get (ZEBRA_NODE, line);
  159. else if (strncmp (line, "router rip", strlen ("router rip")) == 0)
  160. config = config_get (RIP_NODE, line);
  161. else if (strncmp (line, "router ripng", strlen ("router ripng")) == 0)
  162. config = config_get (RIPNG_NODE, line);
  163. else if (strncmp (line, "router ospf", strlen ("router ospf")) == 0)
  164. config = config_get (OSPF_NODE, line);
  165. else if (strncmp (line, "router ospf6", strlen ("router ospf6")) == 0)
  166. config = config_get (OSPF6_NODE, line);
  167. else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
  168. config = config_get (BGP_NODE, line);
  169. else if (strncmp (line, "router isis", strlen ("router isis")) == 0)
  170. config = config_get (ISIS_NODE, line);
  171. else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
  172. config = config_get (BGP_NODE, line);
  173. else if (strncmp (line, "route-map", strlen ("route-map")) == 0)
  174. config = config_get (RMAP_NODE, line);
  175. else if (strncmp (line, "access-list", strlen ("access-list")) == 0)
  176. config = config_get (ACCESS_NODE, line);
  177. else if (strncmp (line, "ipv6 access-list",
  178. strlen ("ipv6 access-list")) == 0)
  179. config = config_get (ACCESS_IPV6_NODE, line);
  180. else if (strncmp (line, "ip prefix-list",
  181. strlen ("ip prefix-list")) == 0)
  182. config = config_get (PREFIX_NODE, line);
  183. else if (strncmp (line, "ipv6 prefix-list",
  184. strlen ("ipv6 prefix-list")) == 0)
  185. config = config_get (PREFIX_IPV6_NODE, line);
  186. else if (strncmp (line, "ip as-path access-list",
  187. strlen ("ip as-path access-list")) == 0)
  188. config = config_get (AS_LIST_NODE, line);
  189. else if (strncmp (line, "ip community-list",
  190. strlen ("ip community-list")) == 0)
  191. config = config_get (COMMUNITY_LIST_NODE, line);
  192. else if (strncmp (line, "ip route", strlen ("ip route")) == 0)
  193. config = config_get (IP_NODE, line);
  194. else if (strncmp (line, "ipv6 route", strlen ("ipv6 route")) == 0)
  195. config = config_get (IP_NODE, line);
  196. else if (strncmp (line, "key", strlen ("key")) == 0)
  197. config = config_get (KEYCHAIN_NODE, line);
  198. else if (strncmp (line, "line", strlen ("line")) == 0)
  199. config = config_get (VTY_NODE, line);
  200. else if ( (strncmp (line, "ipv6 forwarding",
  201. strlen ("ipv6 forwarding")) == 0)
  202. || (strncmp (line, "ip forwarding",
  203. strlen ("ip forwarding")) == 0) )
  204. config = config_get (FORWARDING_NODE, line);
  205. else if (strncmp (line, "service", strlen ("service")) == 0)
  206. config = config_get (SERVICE_NODE, line);
  207. else if (strncmp (line, "debug", strlen ("debug")) == 0)
  208. config = config_get (DEBUG_NODE, line);
  209. else if (strncmp (line, "password", strlen ("password")) == 0
  210. || strncmp (line, "enable password",
  211. strlen ("enable password")) == 0)
  212. config = config_get (AAA_NODE, line);
  213. else
  214. {
  215. if (strncmp (line, "log", strlen ("log")) == 0
  216. || strncmp (line, "hostname", strlen ("hostname")) == 0
  217. )
  218. config_add_line_uniq (config_top, line);
  219. else
  220. config_add_line (config_top, line);
  221. config = NULL;
  222. }
  223. break;
  224. }
  225. }
  226. void
  227. vtysh_config_parse (char *line)
  228. {
  229. char *begin;
  230. char *pnt;
  231. begin = pnt = line;
  232. while (*pnt != '\0')
  233. {
  234. if (*pnt == '\n')
  235. {
  236. *pnt++ = '\0';
  237. vtysh_config_parse_line (begin);
  238. begin = pnt;
  239. }
  240. else
  241. {
  242. pnt++;
  243. }
  244. }
  245. }
  246. /* Macro to check delimiter is needed between each configuration line
  247. * or not. */
  248. #define NO_DELIMITER(I) \
  249. ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
  250. || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE || \
  251. (I) == ACCESS_IPV6_NODE || (I) == PREFIX_IPV6_NODE \
  252. || (I) == SERVICE_NODE || (I) == FORWARDING_NODE || (I) == DEBUG_NODE \
  253. || (I) == AAA_NODE)
  254. /* Display configuration to file pointer. */
  255. void
  256. vtysh_config_dump (FILE *fp)
  257. {
  258. struct listnode *node, *nnode;
  259. struct listnode *mnode, *mnnode;
  260. struct config *config;
  261. struct list *master;
  262. char *line;
  263. unsigned int i;
  264. for (ALL_LIST_ELEMENTS (config_top, node, nnode, line))
  265. {
  266. fprintf (fp, "%s\n", line);
  267. fflush (fp);
  268. }
  269. fprintf (fp, "!\n");
  270. fflush (fp);
  271. for (i = 0; i < vector_active (configvec); i++)
  272. if ((master = vector_slot (configvec, i)) != NULL)
  273. {
  274. for (ALL_LIST_ELEMENTS (master, node, nnode, config))
  275. {
  276. fprintf (fp, "%s\n", config->name);
  277. fflush (fp);
  278. for (ALL_LIST_ELEMENTS (config->line, mnode, mnnode, line))
  279. {
  280. fprintf (fp, "%s\n", line);
  281. fflush (fp);
  282. }
  283. if (! NO_DELIMITER (i))
  284. {
  285. fprintf (fp, "!\n");
  286. fflush (fp);
  287. }
  288. }
  289. if (NO_DELIMITER (i))
  290. {
  291. fprintf (fp, "!\n");
  292. fflush (fp);
  293. }
  294. }
  295. for (i = 0; i < vector_active (configvec); i++)
  296. if ((master = vector_slot (configvec, i)) != NULL)
  297. {
  298. list_delete (master);
  299. vector_slot (configvec, i) = NULL;
  300. }
  301. list_delete_all_node (config_top);
  302. }
  303. /* Read up configuration file from file_name. */
  304. static void
  305. vtysh_read_file (FILE *confp)
  306. {
  307. int ret;
  308. struct vty *vty;
  309. vty = vty_new ();
  310. vty->fd = 0; /* stdout */
  311. vty->type = VTY_TERM;
  312. vty->node = CONFIG_NODE;
  313. vtysh_execute_no_pager ("enable");
  314. vtysh_execute_no_pager ("configure terminal");
  315. /* Execute configuration file. */
  316. ret = vtysh_config_from_file (vty, confp);
  317. vtysh_execute_no_pager ("end");
  318. vtysh_execute_no_pager ("disable");
  319. vty_close (vty);
  320. if (ret != CMD_SUCCESS)
  321. {
  322. switch (ret)
  323. {
  324. case CMD_ERR_AMBIGUOUS:
  325. fprintf (stderr, "Ambiguous command.\n");
  326. break;
  327. case CMD_ERR_NO_MATCH:
  328. fprintf (stderr, "There is no such command.\n");
  329. break;
  330. }
  331. fprintf (stderr, "Error occured during reading below line.\n%s\n",
  332. vty->buf);
  333. exit (1);
  334. }
  335. }
  336. /* Read up configuration file from config_default_dir. */
  337. int
  338. vtysh_read_config (char *config_default_dir)
  339. {
  340. FILE *confp = NULL;
  341. confp = fopen (config_default_dir, "r");
  342. if (confp == NULL)
  343. return (1);
  344. vtysh_read_file (confp);
  345. fclose (confp);
  346. host_config_set (config_default_dir);
  347. return (0);
  348. }
  349. /* We don't write vtysh specific into file from vtysh. vtysh.conf should
  350. * be edited by hand. So, we handle only "write terminal" case here and
  351. * integrate vtysh specific conf with conf from daemons.
  352. */
  353. void
  354. vtysh_config_write ()
  355. {
  356. char line[81];
  357. extern struct host host;
  358. if (host.name)
  359. {
  360. sprintf (line, "hostname %s", host.name);
  361. vtysh_config_parse_line(line);
  362. }
  363. if (vtysh_writeconfig_integrated)
  364. vtysh_config_parse_line ("service integrated-vtysh-config");
  365. }
  366. void
  367. vtysh_config_init ()
  368. {
  369. config_top = list_new ();
  370. config_top->del = (void (*) (void *))line_del;
  371. configvec = vector_init (1);
  372. }