vtysh.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  1. /* Virtual terminal interface shell.
  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. #include <sys/un.h>
  23. #include <setjmp.h>
  24. #include <sys/wait.h>
  25. #include <sys/resource.h>
  26. #include <sys/stat.h>
  27. #include <readline/readline.h>
  28. #include <readline/history.h>
  29. #include "command.h"
  30. #include "memory.h"
  31. #include "vtysh/vtysh.h"
  32. #include "log.h"
  33. #include "bgpd/bgp_vty.h"
  34. /* Struct VTY. */
  35. struct vty *vty;
  36. /* VTY shell pager name. */
  37. char *vtysh_pager_name = NULL;
  38. /* VTY shell client structure. */
  39. struct vtysh_client
  40. {
  41. int fd;
  42. const char *name;
  43. int flag;
  44. const char *path;
  45. } vtysh_client[] =
  46. {
  47. { .fd = -1, .name = "zebra", .flag = VTYSH_ZEBRA, .path = ZEBRA_VTYSH_PATH},
  48. { .fd = -1, .name = "ripd", .flag = VTYSH_RIPD, .path = RIP_VTYSH_PATH},
  49. { .fd = -1, .name = "ripngd", .flag = VTYSH_RIPNGD, .path = RIPNG_VTYSH_PATH},
  50. { .fd = -1, .name = "ospfd", .flag = VTYSH_OSPFD, .path = OSPF_VTYSH_PATH},
  51. { .fd = -1, .name = "ospf6d", .flag = VTYSH_OSPF6D, .path = OSPF6_VTYSH_PATH},
  52. { .fd = -1, .name = "bgpd", .flag = VTYSH_BGPD, .path = BGP_VTYSH_PATH},
  53. { .fd = -1, .name = "isisd", .flag = VTYSH_ISISD, .path = ISIS_VTYSH_PATH},
  54. { .fd = -1, .name = "pimd", .flag = VTYSH_PIMD, .path = PIM_VTYSH_PATH},
  55. };
  56. /* We need direct access to ripd to implement vtysh_exit_ripd_only. */
  57. static struct vtysh_client *ripd_client = NULL;
  58. /* Using integrated config from Quagga.conf. Default is no. */
  59. int vtysh_writeconfig_integrated = 0;
  60. extern char config_default[];
  61. static void
  62. vclient_close (struct vtysh_client *vclient)
  63. {
  64. if (vclient->fd >= 0)
  65. {
  66. fprintf(stderr,
  67. "Warning: closing connection to %s because of an I/O error!\n",
  68. vclient->name);
  69. close (vclient->fd);
  70. vclient->fd = -1;
  71. }
  72. }
  73. /* Following filled with debug code to trace a problematic condition
  74. * under load - it SHOULD handle it. */
  75. #define ERR_WHERE_STRING "vtysh(): vtysh_client_config(): "
  76. static int
  77. vtysh_client_config (struct vtysh_client *vclient, char *line)
  78. {
  79. int ret;
  80. char *buf;
  81. size_t bufsz;
  82. char *pbuf;
  83. size_t left;
  84. char *eoln;
  85. int nbytes;
  86. int i;
  87. int readln;
  88. if (vclient->fd < 0)
  89. return CMD_SUCCESS;
  90. ret = write (vclient->fd, line, strlen (line) + 1);
  91. if (ret <= 0)
  92. {
  93. vclient_close (vclient);
  94. return CMD_SUCCESS;
  95. }
  96. /* Allow enough room for buffer to read more than a few pages from socket. */
  97. bufsz = 5 * getpagesize() + 1;
  98. buf = XMALLOC(MTYPE_TMP, bufsz);
  99. memset(buf, 0, bufsz);
  100. pbuf = buf;
  101. while (1)
  102. {
  103. if (pbuf >= ((buf + bufsz) -1))
  104. {
  105. fprintf (stderr, ERR_WHERE_STRING \
  106. "warning - pbuf beyond buffer end.\n");
  107. return CMD_WARNING;
  108. }
  109. readln = (buf + bufsz) - pbuf - 1;
  110. nbytes = read (vclient->fd, pbuf, readln);
  111. if (nbytes <= 0)
  112. {
  113. if (errno == EINTR)
  114. continue;
  115. fprintf(stderr, ERR_WHERE_STRING "(%u)", errno);
  116. perror("");
  117. if (errno == EAGAIN || errno == EIO)
  118. continue;
  119. vclient_close (vclient);
  120. XFREE(MTYPE_TMP, buf);
  121. return CMD_SUCCESS;
  122. }
  123. pbuf[nbytes] = '\0';
  124. if (nbytes >= 4)
  125. {
  126. i = nbytes - 4;
  127. if (pbuf[i] == '\0' && pbuf[i + 1] == '\0' && pbuf[i + 2] == '\0')
  128. {
  129. ret = pbuf[i + 3];
  130. break;
  131. }
  132. }
  133. pbuf += nbytes;
  134. /* See if a line exists in buffer, if so parse and consume it, and
  135. * reset read position. */
  136. if ((eoln = strrchr(buf, '\n')) == NULL)
  137. continue;
  138. if (eoln >= ((buf + bufsz) - 1))
  139. {
  140. fprintf (stderr, ERR_WHERE_STRING \
  141. "warning - eoln beyond buffer end.\n");
  142. }
  143. vtysh_config_parse(buf);
  144. eoln++;
  145. left = (size_t)(buf + bufsz - eoln);
  146. memmove(buf, eoln, left);
  147. buf[bufsz-1] = '\0';
  148. pbuf = buf + strlen(buf);
  149. }
  150. /* Parse anything left in the buffer. */
  151. vtysh_config_parse (buf);
  152. XFREE(MTYPE_TMP, buf);
  153. return ret;
  154. }
  155. static int
  156. vtysh_client_execute (struct vtysh_client *vclient, const char *line, FILE *fp)
  157. {
  158. int ret;
  159. char buf[1001];
  160. int nbytes;
  161. int i;
  162. int numnulls = 0;
  163. if (vclient->fd < 0)
  164. return CMD_SUCCESS;
  165. ret = write (vclient->fd, line, strlen (line) + 1);
  166. if (ret <= 0)
  167. {
  168. vclient_close (vclient);
  169. return CMD_SUCCESS;
  170. }
  171. while (1)
  172. {
  173. nbytes = read (vclient->fd, buf, sizeof(buf)-1);
  174. if (nbytes <= 0 && errno != EINTR)
  175. {
  176. vclient_close (vclient);
  177. return CMD_SUCCESS;
  178. }
  179. if (nbytes > 0)
  180. {
  181. if ((numnulls == 3) && (nbytes == 1))
  182. return buf[0];
  183. buf[nbytes] = '\0';
  184. fputs (buf, fp);
  185. fflush (fp);
  186. /* check for trailling \0\0\0<ret code>,
  187. * even if split across reads
  188. * (see lib/vty.c::vtysh_read)
  189. */
  190. if (nbytes >= 4)
  191. {
  192. i = nbytes-4;
  193. numnulls = 0;
  194. }
  195. else
  196. i = 0;
  197. while (i < nbytes && numnulls < 3)
  198. {
  199. if (buf[i++] == '\0')
  200. numnulls++;
  201. else
  202. numnulls = 0;
  203. }
  204. /* got 3 or more trailing NULs? */
  205. if ((numnulls >= 3) && (i < nbytes))
  206. return (buf[nbytes-1]);
  207. }
  208. }
  209. }
  210. static void
  211. vtysh_exit_ripd_only (void)
  212. {
  213. if (ripd_client)
  214. vtysh_client_execute (ripd_client, "exit", stdout);
  215. }
  216. void
  217. vtysh_pager_init (void)
  218. {
  219. char *pager_defined;
  220. pager_defined = getenv ("VTYSH_PAGER");
  221. if (pager_defined)
  222. vtysh_pager_name = strdup (pager_defined);
  223. else
  224. vtysh_pager_name = strdup ("more");
  225. }
  226. /* Command execution over the vty interface. */
  227. static int
  228. vtysh_execute_func (const char *line, int pager)
  229. {
  230. int ret, cmd_stat;
  231. u_int i;
  232. vector vline;
  233. struct cmd_element *cmd;
  234. FILE *fp = NULL;
  235. int closepager = 0;
  236. int tried = 0;
  237. int saved_ret, saved_node;
  238. /* Split readline string up into the vector. */
  239. vline = cmd_make_strvec (line);
  240. if (vline == NULL)
  241. return CMD_SUCCESS;
  242. saved_ret = ret = cmd_execute_command (vline, vty, &cmd, 1);
  243. saved_node = vty->node;
  244. /* If command doesn't succeeded in current node, try to walk up in node tree.
  245. * Changing vty->node is enough to try it just out without actual walkup in
  246. * the vtysh. */
  247. while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON && ret != CMD_WARNING
  248. && vty->node > CONFIG_NODE)
  249. {
  250. vty->node = node_parent(vty->node);
  251. ret = cmd_execute_command (vline, vty, &cmd, 1);
  252. tried++;
  253. }
  254. vty->node = saved_node;
  255. /* If command succeeded in any other node than current (tried > 0) we have
  256. * to move into node in the vtysh where it succeeded. */
  257. if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON || ret == CMD_WARNING)
  258. {
  259. if ((saved_node == BGP_VPNV4_NODE || saved_node == BGP_IPV4_NODE
  260. || saved_node == BGP_IPV6_NODE || saved_node == BGP_IPV4M_NODE
  261. || saved_node == BGP_IPV6M_NODE)
  262. && (tried == 1))
  263. {
  264. vtysh_execute("exit-address-family");
  265. }
  266. else if ((saved_node == KEYCHAIN_KEY_NODE) && (tried == 1))
  267. {
  268. vtysh_execute("exit");
  269. }
  270. else if (tried)
  271. {
  272. vtysh_execute ("end");
  273. vtysh_execute ("configure terminal");
  274. }
  275. }
  276. /* If command didn't succeed in any node, continue with return value from
  277. * first try. */
  278. else if (tried)
  279. {
  280. ret = saved_ret;
  281. }
  282. cmd_free_strvec (vline);
  283. cmd_stat = ret;
  284. switch (ret)
  285. {
  286. case CMD_WARNING:
  287. if (vty->type == VTY_FILE)
  288. fprintf (stdout,"Warning...\n");
  289. break;
  290. case CMD_ERR_AMBIGUOUS:
  291. fprintf (stdout,"%% Ambiguous command.\n");
  292. break;
  293. case CMD_ERR_NO_MATCH:
  294. fprintf (stdout,"%% Unknown command.\n");
  295. break;
  296. case CMD_ERR_INCOMPLETE:
  297. fprintf (stdout,"%% Command incomplete.\n");
  298. break;
  299. case CMD_SUCCESS_DAEMON:
  300. {
  301. /* FIXME: Don't open pager for exit commands. popen() causes problems
  302. * if exited from vtysh at all. This hack shouldn't cause any problem
  303. * but is really ugly. */
  304. if (pager && vtysh_pager_name && (strncmp(line, "exit", 4) != 0))
  305. {
  306. fp = popen (vtysh_pager_name, "w");
  307. if (fp == NULL)
  308. {
  309. perror ("popen failed for pager");
  310. fp = stdout;
  311. }
  312. else
  313. closepager=1;
  314. }
  315. else
  316. fp = stdout;
  317. if (! strcmp(cmd->string,"configure terminal"))
  318. {
  319. for (i = 0; i < array_size(vtysh_client); i++)
  320. {
  321. cmd_stat = vtysh_client_execute(&vtysh_client[i], line, fp);
  322. if (cmd_stat == CMD_WARNING)
  323. break;
  324. }
  325. if (cmd_stat)
  326. {
  327. line = "end";
  328. vline = cmd_make_strvec (line);
  329. if (vline == NULL)
  330. {
  331. if (pager && vtysh_pager_name && fp && closepager)
  332. {
  333. if (pclose (fp) == -1)
  334. {
  335. perror ("pclose failed for pager");
  336. }
  337. fp = NULL;
  338. }
  339. return CMD_SUCCESS;
  340. }
  341. ret = cmd_execute_command (vline, vty, &cmd, 1);
  342. cmd_free_strvec (vline);
  343. if (ret != CMD_SUCCESS_DAEMON)
  344. break;
  345. }
  346. else
  347. if (cmd->func)
  348. {
  349. (*cmd->func) (cmd, vty, 0, NULL);
  350. break;
  351. }
  352. }
  353. cmd_stat = CMD_SUCCESS;
  354. for (i = 0; i < array_size(vtysh_client); i++)
  355. {
  356. if (cmd->daemon & vtysh_client[i].flag)
  357. {
  358. cmd_stat = vtysh_client_execute(&vtysh_client[i], line, fp);
  359. if (cmd_stat != CMD_SUCCESS)
  360. break;
  361. }
  362. }
  363. if (cmd_stat != CMD_SUCCESS)
  364. break;
  365. if (cmd->func)
  366. (*cmd->func) (cmd, vty, 0, NULL);
  367. }
  368. }
  369. if (pager && vtysh_pager_name && fp && closepager)
  370. {
  371. if (pclose (fp) == -1)
  372. {
  373. perror ("pclose failed for pager");
  374. }
  375. fp = NULL;
  376. }
  377. return cmd_stat;
  378. }
  379. int
  380. vtysh_execute_no_pager (const char *line)
  381. {
  382. return vtysh_execute_func (line, 0);
  383. }
  384. int
  385. vtysh_execute (const char *line)
  386. {
  387. return vtysh_execute_func (line, 1);
  388. }
  389. /* Configration make from file. */
  390. int
  391. vtysh_config_from_file (struct vty *vty, FILE *fp)
  392. {
  393. int ret;
  394. vector vline;
  395. struct cmd_element *cmd;
  396. while (fgets (vty->buf, VTY_BUFSIZ, fp))
  397. {
  398. if (vty->buf[0] == '!' || vty->buf[1] == '#')
  399. continue;
  400. vline = cmd_make_strvec (vty->buf);
  401. /* In case of comment line. */
  402. if (vline == NULL)
  403. continue;
  404. /* Execute configuration command : this is strict match. */
  405. ret = cmd_execute_command_strict (vline, vty, &cmd);
  406. /* Try again with setting node to CONFIG_NODE. */
  407. if (ret != CMD_SUCCESS
  408. && ret != CMD_SUCCESS_DAEMON
  409. && ret != CMD_WARNING)
  410. {
  411. if (vty->node == KEYCHAIN_KEY_NODE)
  412. {
  413. vty->node = KEYCHAIN_NODE;
  414. vtysh_exit_ripd_only ();
  415. ret = cmd_execute_command_strict (vline, vty, &cmd);
  416. if (ret != CMD_SUCCESS
  417. && ret != CMD_SUCCESS_DAEMON
  418. && ret != CMD_WARNING)
  419. {
  420. vtysh_exit_ripd_only ();
  421. vty->node = CONFIG_NODE;
  422. ret = cmd_execute_command_strict (vline, vty, &cmd);
  423. }
  424. }
  425. else
  426. {
  427. vtysh_execute ("end");
  428. vtysh_execute ("configure terminal");
  429. vty->node = CONFIG_NODE;
  430. ret = cmd_execute_command_strict (vline, vty, &cmd);
  431. }
  432. }
  433. cmd_free_strvec (vline);
  434. switch (ret)
  435. {
  436. case CMD_WARNING:
  437. if (vty->type == VTY_FILE)
  438. fprintf (stdout,"Warning...\n");
  439. break;
  440. case CMD_ERR_AMBIGUOUS:
  441. fprintf (stdout,"%% Ambiguous command.\n");
  442. break;
  443. case CMD_ERR_NO_MATCH:
  444. fprintf (stdout,"%% Unknown command: %s", vty->buf);
  445. break;
  446. case CMD_ERR_INCOMPLETE:
  447. fprintf (stdout,"%% Command incomplete.\n");
  448. break;
  449. case CMD_SUCCESS_DAEMON:
  450. {
  451. u_int i;
  452. int cmd_stat = CMD_SUCCESS;
  453. for (i = 0; i < array_size(vtysh_client); i++)
  454. {
  455. if (cmd->daemon & vtysh_client[i].flag)
  456. {
  457. cmd_stat = vtysh_client_execute (&vtysh_client[i],
  458. vty->buf, stdout);
  459. if (cmd_stat != CMD_SUCCESS)
  460. break;
  461. }
  462. }
  463. if (cmd_stat != CMD_SUCCESS)
  464. break;
  465. if (cmd->func)
  466. (*cmd->func) (cmd, vty, 0, NULL);
  467. }
  468. }
  469. }
  470. return CMD_SUCCESS;
  471. }
  472. /* We don't care about the point of the cursor when '?' is typed. */
  473. static int
  474. vtysh_rl_describe (void)
  475. {
  476. int ret;
  477. unsigned int i;
  478. vector vline;
  479. vector describe;
  480. int width;
  481. struct cmd_token *token;
  482. vline = cmd_make_strvec (rl_line_buffer);
  483. /* In case of '> ?'. */
  484. if (vline == NULL)
  485. {
  486. vline = vector_init (1);
  487. vector_set (vline, NULL);
  488. }
  489. else
  490. if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
  491. vector_set (vline, NULL);
  492. describe = cmd_describe_command (vline, vty, &ret);
  493. fprintf (stdout,"\n");
  494. /* Ambiguous and no match error. */
  495. switch (ret)
  496. {
  497. case CMD_ERR_AMBIGUOUS:
  498. cmd_free_strvec (vline);
  499. fprintf (stdout,"%% Ambiguous command.\n");
  500. rl_on_new_line ();
  501. return 0;
  502. break;
  503. case CMD_ERR_NO_MATCH:
  504. cmd_free_strvec (vline);
  505. fprintf (stdout,"%% There is no matched command.\n");
  506. rl_on_new_line ();
  507. return 0;
  508. break;
  509. }
  510. /* Get width of command string. */
  511. width = 0;
  512. for (i = 0; i < vector_active (describe); i++)
  513. if ((token = vector_slot (describe, i)) != NULL)
  514. {
  515. int len;
  516. if (token->cmd[0] == '\0')
  517. continue;
  518. len = strlen (token->cmd);
  519. if (token->cmd[0] == '.')
  520. len--;
  521. if (width < len)
  522. width = len;
  523. }
  524. for (i = 0; i < vector_active (describe); i++)
  525. if ((token = vector_slot (describe, i)) != NULL)
  526. {
  527. if (token->cmd[0] == '\0')
  528. continue;
  529. if (! token->desc)
  530. fprintf (stdout," %-s\n",
  531. token->cmd[0] == '.' ? token->cmd + 1 : token->cmd);
  532. else
  533. fprintf (stdout," %-*s %s\n",
  534. width,
  535. token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
  536. token->desc);
  537. }
  538. cmd_free_strvec (vline);
  539. vector_free (describe);
  540. rl_on_new_line();
  541. return 0;
  542. }
  543. /* Result of cmd_complete_command() call will be stored here
  544. * and used in new_completion() in order to put the space in
  545. * correct places only. */
  546. int complete_status;
  547. static char *
  548. command_generator (const char *text, int state)
  549. {
  550. vector vline;
  551. static char **matched = NULL;
  552. static int index = 0;
  553. /* First call. */
  554. if (! state)
  555. {
  556. index = 0;
  557. if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
  558. return NULL;
  559. vline = cmd_make_strvec (rl_line_buffer);
  560. if (vline == NULL)
  561. return NULL;
  562. if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
  563. vector_set (vline, NULL);
  564. matched = cmd_complete_command (vline, vty, &complete_status);
  565. }
  566. if (matched && matched[index])
  567. return matched[index++];
  568. return NULL;
  569. }
  570. static char **
  571. new_completion (char *text, int start, int end)
  572. {
  573. char **matches;
  574. matches = rl_completion_matches (text, command_generator);
  575. if (matches)
  576. {
  577. rl_point = rl_end;
  578. if (complete_status != CMD_COMPLETE_FULL_MATCH)
  579. /* only append a space on full match */
  580. rl_completion_append_character = '\0';
  581. }
  582. return matches;
  583. }
  584. #if 0
  585. /* This function is not actually being used. */
  586. static char **
  587. vtysh_completion (char *text, int start, int end)
  588. {
  589. int ret;
  590. vector vline;
  591. char **matched = NULL;
  592. if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
  593. return NULL;
  594. vline = cmd_make_strvec (rl_line_buffer);
  595. if (vline == NULL)
  596. return NULL;
  597. /* In case of 'help \t'. */
  598. if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
  599. vector_set (vline, '\0');
  600. matched = cmd_complete_command (vline, vty, &ret);
  601. cmd_free_strvec (vline);
  602. return (char **) matched;
  603. }
  604. #endif
  605. /* Vty node structures. */
  606. static struct cmd_node bgp_node =
  607. {
  608. BGP_NODE,
  609. "%s(config-router)# ",
  610. };
  611. static struct cmd_node rip_node =
  612. {
  613. RIP_NODE,
  614. "%s(config-router)# ",
  615. };
  616. static struct cmd_node isis_node =
  617. {
  618. ISIS_NODE,
  619. "%s(config-router)# ",
  620. };
  621. static struct cmd_node interface_node =
  622. {
  623. INTERFACE_NODE,
  624. "%s(config-if)# ",
  625. };
  626. static struct cmd_node rmap_node =
  627. {
  628. RMAP_NODE,
  629. "%s(config-route-map)# "
  630. };
  631. static struct cmd_node zebra_node =
  632. {
  633. ZEBRA_NODE,
  634. "%s(config-router)# "
  635. };
  636. static struct cmd_node bgp_vpnv4_node =
  637. {
  638. BGP_VPNV4_NODE,
  639. "%s(config-router-af)# "
  640. };
  641. static struct cmd_node bgp_ipv4_node =
  642. {
  643. BGP_IPV4_NODE,
  644. "%s(config-router-af)# "
  645. };
  646. static struct cmd_node bgp_ipv4m_node =
  647. {
  648. BGP_IPV4M_NODE,
  649. "%s(config-router-af)# "
  650. };
  651. static struct cmd_node bgp_ipv6_node =
  652. {
  653. BGP_IPV6_NODE,
  654. "%s(config-router-af)# "
  655. };
  656. static struct cmd_node bgp_ipv6m_node =
  657. {
  658. BGP_IPV6M_NODE,
  659. "%s(config-router-af)# "
  660. };
  661. static struct cmd_node ospf_node =
  662. {
  663. OSPF_NODE,
  664. "%s(config-router)# "
  665. };
  666. static struct cmd_node ripng_node =
  667. {
  668. RIPNG_NODE,
  669. "%s(config-router)# "
  670. };
  671. static struct cmd_node ospf6_node =
  672. {
  673. OSPF6_NODE,
  674. "%s(config-ospf6)# "
  675. };
  676. static struct cmd_node keychain_node =
  677. {
  678. KEYCHAIN_NODE,
  679. "%s(config-keychain)# "
  680. };
  681. static struct cmd_node keychain_key_node =
  682. {
  683. KEYCHAIN_KEY_NODE,
  684. "%s(config-keychain-key)# "
  685. };
  686. /* Defined in lib/vty.c */
  687. extern struct cmd_node vty_node;
  688. /* When '^Z' is received from vty, move down to the enable mode. */
  689. static int
  690. vtysh_end (void)
  691. {
  692. switch (vty->node)
  693. {
  694. case VIEW_NODE:
  695. case ENABLE_NODE:
  696. /* Nothing to do. */
  697. break;
  698. default:
  699. vty->node = ENABLE_NODE;
  700. break;
  701. }
  702. return CMD_SUCCESS;
  703. }
  704. DEFUNSH (VTYSH_ALL,
  705. vtysh_end_all,
  706. vtysh_end_all_cmd,
  707. "end",
  708. "End current mode and change to enable mode\n")
  709. {
  710. return vtysh_end ();
  711. }
  712. DEFUNSH (VTYSH_BGPD,
  713. router_bgp,
  714. router_bgp_cmd,
  715. "router bgp " CMD_AS_RANGE,
  716. ROUTER_STR
  717. BGP_STR
  718. AS_STR)
  719. {
  720. vty->node = BGP_NODE;
  721. return CMD_SUCCESS;
  722. }
  723. ALIAS_SH (VTYSH_BGPD,
  724. router_bgp,
  725. router_bgp_view_cmd,
  726. "router bgp " CMD_AS_RANGE " view WORD",
  727. ROUTER_STR
  728. BGP_STR
  729. AS_STR
  730. "BGP view\n"
  731. "view name\n")
  732. DEFUNSH (VTYSH_BGPD,
  733. address_family_vpnv4,
  734. address_family_vpnv4_cmd,
  735. "address-family vpnv4",
  736. "Enter Address Family command mode\n"
  737. "Address family\n")
  738. {
  739. vty->node = BGP_VPNV4_NODE;
  740. return CMD_SUCCESS;
  741. }
  742. DEFUNSH (VTYSH_BGPD,
  743. address_family_vpnv4_unicast,
  744. address_family_vpnv4_unicast_cmd,
  745. "address-family vpnv4 unicast",
  746. "Enter Address Family command mode\n"
  747. "Address family\n"
  748. "Address Family Modifier\n")
  749. {
  750. vty->node = BGP_VPNV4_NODE;
  751. return CMD_SUCCESS;
  752. }
  753. DEFUNSH (VTYSH_BGPD,
  754. address_family_ipv4_unicast,
  755. address_family_ipv4_unicast_cmd,
  756. "address-family ipv4 unicast",
  757. "Enter Address Family command mode\n"
  758. "Address family\n"
  759. "Address Family Modifier\n")
  760. {
  761. vty->node = BGP_IPV4_NODE;
  762. return CMD_SUCCESS;
  763. }
  764. DEFUNSH (VTYSH_BGPD,
  765. address_family_ipv4_multicast,
  766. address_family_ipv4_multicast_cmd,
  767. "address-family ipv4 multicast",
  768. "Enter Address Family command mode\n"
  769. "Address family\n"
  770. "Address Family Modifier\n")
  771. {
  772. vty->node = BGP_IPV4M_NODE;
  773. return CMD_SUCCESS;
  774. }
  775. DEFUNSH (VTYSH_BGPD,
  776. address_family_ipv6,
  777. address_family_ipv6_cmd,
  778. "address-family ipv6",
  779. "Enter Address Family command mode\n"
  780. "Address family\n")
  781. {
  782. vty->node = BGP_IPV6_NODE;
  783. return CMD_SUCCESS;
  784. }
  785. DEFUNSH (VTYSH_BGPD,
  786. address_family_ipv6_unicast,
  787. address_family_ipv6_unicast_cmd,
  788. "address-family ipv6 unicast",
  789. "Enter Address Family command mode\n"
  790. "Address family\n"
  791. "Address Family Modifier\n")
  792. {
  793. vty->node = BGP_IPV6_NODE;
  794. return CMD_SUCCESS;
  795. }
  796. DEFUNSH (VTYSH_BGPD,
  797. address_family_ipv6_multicast,
  798. address_family_ipv6_multicast_cmd,
  799. "address-family ipv6 multicast",
  800. "Enter Address Family command mode\n"
  801. "Address family\n"
  802. "Address Family Modifier\n")
  803. {
  804. vty->node = BGP_IPV6M_NODE;
  805. return CMD_SUCCESS;
  806. }
  807. DEFUNSH (VTYSH_RIPD,
  808. key_chain,
  809. key_chain_cmd,
  810. "key chain WORD",
  811. "Authentication key management\n"
  812. "Key-chain management\n"
  813. "Key-chain name\n")
  814. {
  815. vty->node = KEYCHAIN_NODE;
  816. return CMD_SUCCESS;
  817. }
  818. DEFUNSH (VTYSH_RIPD,
  819. key,
  820. key_cmd,
  821. "key <0-2147483647>",
  822. "Configure a key\n"
  823. "Key identifier number\n")
  824. {
  825. vty->node = KEYCHAIN_KEY_NODE;
  826. return CMD_SUCCESS;
  827. }
  828. DEFUNSH (VTYSH_RIPD,
  829. router_rip,
  830. router_rip_cmd,
  831. "router rip",
  832. ROUTER_STR
  833. "RIP")
  834. {
  835. vty->node = RIP_NODE;
  836. return CMD_SUCCESS;
  837. }
  838. DEFUNSH (VTYSH_RIPNGD,
  839. router_ripng,
  840. router_ripng_cmd,
  841. "router ripng",
  842. ROUTER_STR
  843. "RIPng")
  844. {
  845. vty->node = RIPNG_NODE;
  846. return CMD_SUCCESS;
  847. }
  848. DEFUNSH (VTYSH_OSPFD,
  849. router_ospf,
  850. router_ospf_cmd,
  851. "router ospf",
  852. "Enable a routing process\n"
  853. "Start OSPF configuration\n")
  854. {
  855. vty->node = OSPF_NODE;
  856. return CMD_SUCCESS;
  857. }
  858. DEFUNSH (VTYSH_OSPF6D,
  859. router_ospf6,
  860. router_ospf6_cmd,
  861. "router ospf6",
  862. OSPF6_ROUTER_STR
  863. OSPF6_STR)
  864. {
  865. vty->node = OSPF6_NODE;
  866. return CMD_SUCCESS;
  867. }
  868. DEFUNSH (VTYSH_ISISD,
  869. router_isis,
  870. router_isis_cmd,
  871. "router isis WORD",
  872. ROUTER_STR
  873. "ISO IS-IS\n"
  874. "ISO Routing area tag")
  875. {
  876. vty->node = ISIS_NODE;
  877. return CMD_SUCCESS;
  878. }
  879. DEFUNSH (VTYSH_RMAP,
  880. route_map,
  881. route_map_cmd,
  882. "route-map WORD (deny|permit) <1-65535>",
  883. "Create route-map or enter route-map command mode\n"
  884. "Route map tag\n"
  885. "Route map denies set operations\n"
  886. "Route map permits set operations\n"
  887. "Sequence to insert to/delete from existing route-map entry\n")
  888. {
  889. vty->node = RMAP_NODE;
  890. return CMD_SUCCESS;
  891. }
  892. DEFUNSH (VTYSH_ALL,
  893. vtysh_line_vty,
  894. vtysh_line_vty_cmd,
  895. "line vty",
  896. "Configure a terminal line\n"
  897. "Virtual terminal\n")
  898. {
  899. vty->node = VTY_NODE;
  900. return CMD_SUCCESS;
  901. }
  902. DEFUNSH (VTYSH_ALL,
  903. vtysh_enable,
  904. vtysh_enable_cmd,
  905. "enable",
  906. "Turn on privileged mode command\n")
  907. {
  908. vty->node = ENABLE_NODE;
  909. return CMD_SUCCESS;
  910. }
  911. DEFUNSH (VTYSH_ALL,
  912. vtysh_disable,
  913. vtysh_disable_cmd,
  914. "disable",
  915. "Turn off privileged mode command\n")
  916. {
  917. if (vty->node == ENABLE_NODE)
  918. vty->node = VIEW_NODE;
  919. return CMD_SUCCESS;
  920. }
  921. DEFUNSH (VTYSH_ALL,
  922. vtysh_config_terminal,
  923. vtysh_config_terminal_cmd,
  924. "configure terminal",
  925. "Configuration from vty interface\n"
  926. "Configuration terminal\n")
  927. {
  928. vty->node = CONFIG_NODE;
  929. return CMD_SUCCESS;
  930. }
  931. static int
  932. vtysh_exit (struct vty *vty)
  933. {
  934. switch (vty->node)
  935. {
  936. case VIEW_NODE:
  937. case ENABLE_NODE:
  938. exit (0);
  939. break;
  940. case CONFIG_NODE:
  941. vty->node = ENABLE_NODE;
  942. break;
  943. case INTERFACE_NODE:
  944. case ZEBRA_NODE:
  945. case BGP_NODE:
  946. case RIP_NODE:
  947. case RIPNG_NODE:
  948. case OSPF_NODE:
  949. case OSPF6_NODE:
  950. case BABEL_NODE:
  951. case ISIS_NODE:
  952. case MASC_NODE:
  953. case RMAP_NODE:
  954. case VTY_NODE:
  955. case KEYCHAIN_NODE:
  956. vtysh_execute("end");
  957. vtysh_execute("configure terminal");
  958. vty->node = CONFIG_NODE;
  959. break;
  960. case BGP_VPNV4_NODE:
  961. case BGP_IPV4_NODE:
  962. case BGP_IPV4M_NODE:
  963. case BGP_IPV6_NODE:
  964. case BGP_IPV6M_NODE:
  965. vty->node = BGP_NODE;
  966. break;
  967. case KEYCHAIN_KEY_NODE:
  968. vty->node = KEYCHAIN_NODE;
  969. break;
  970. default:
  971. break;
  972. }
  973. return CMD_SUCCESS;
  974. }
  975. DEFUNSH (VTYSH_ALL,
  976. vtysh_exit_all,
  977. vtysh_exit_all_cmd,
  978. "exit",
  979. "Exit current mode and down to previous mode\n")
  980. {
  981. return vtysh_exit (vty);
  982. }
  983. ALIAS (vtysh_exit_all,
  984. vtysh_quit_all_cmd,
  985. "quit",
  986. "Exit current mode and down to previous mode\n")
  987. DEFUNSH (VTYSH_BGPD,
  988. exit_address_family,
  989. exit_address_family_cmd,
  990. "exit-address-family",
  991. "Exit from Address Family configuration mode\n")
  992. {
  993. if (vty->node == BGP_IPV4_NODE
  994. || vty->node == BGP_IPV4M_NODE
  995. || vty->node == BGP_VPNV4_NODE
  996. || vty->node == BGP_IPV6_NODE
  997. || vty->node == BGP_IPV6M_NODE)
  998. vty->node = BGP_NODE;
  999. return CMD_SUCCESS;
  1000. }
  1001. DEFUNSH (VTYSH_ZEBRA,
  1002. vtysh_exit_zebra,
  1003. vtysh_exit_zebra_cmd,
  1004. "exit",
  1005. "Exit current mode and down to previous mode\n")
  1006. {
  1007. return vtysh_exit (vty);
  1008. }
  1009. ALIAS (vtysh_exit_zebra,
  1010. vtysh_quit_zebra_cmd,
  1011. "quit",
  1012. "Exit current mode and down to previous mode\n")
  1013. DEFUNSH (VTYSH_RIPD,
  1014. vtysh_exit_ripd,
  1015. vtysh_exit_ripd_cmd,
  1016. "exit",
  1017. "Exit current mode and down to previous mode\n")
  1018. {
  1019. return vtysh_exit (vty);
  1020. }
  1021. ALIAS (vtysh_exit_ripd,
  1022. vtysh_quit_ripd_cmd,
  1023. "quit",
  1024. "Exit current mode and down to previous mode\n")
  1025. DEFUNSH (VTYSH_RIPNGD,
  1026. vtysh_exit_ripngd,
  1027. vtysh_exit_ripngd_cmd,
  1028. "exit",
  1029. "Exit current mode and down to previous mode\n")
  1030. {
  1031. return vtysh_exit (vty);
  1032. }
  1033. ALIAS (vtysh_exit_ripngd,
  1034. vtysh_quit_ripngd_cmd,
  1035. "quit",
  1036. "Exit current mode and down to previous mode\n")
  1037. DEFUNSH (VTYSH_RMAP,
  1038. vtysh_exit_rmap,
  1039. vtysh_exit_rmap_cmd,
  1040. "exit",
  1041. "Exit current mode and down to previous mode\n")
  1042. {
  1043. return vtysh_exit (vty);
  1044. }
  1045. ALIAS (vtysh_exit_rmap,
  1046. vtysh_quit_rmap_cmd,
  1047. "quit",
  1048. "Exit current mode and down to previous mode\n")
  1049. DEFUNSH (VTYSH_BGPD,
  1050. vtysh_exit_bgpd,
  1051. vtysh_exit_bgpd_cmd,
  1052. "exit",
  1053. "Exit current mode and down to previous mode\n")
  1054. {
  1055. return vtysh_exit (vty);
  1056. }
  1057. ALIAS (vtysh_exit_bgpd,
  1058. vtysh_quit_bgpd_cmd,
  1059. "quit",
  1060. "Exit current mode and down to previous mode\n")
  1061. DEFUNSH (VTYSH_OSPFD,
  1062. vtysh_exit_ospfd,
  1063. vtysh_exit_ospfd_cmd,
  1064. "exit",
  1065. "Exit current mode and down to previous mode\n")
  1066. {
  1067. return vtysh_exit (vty);
  1068. }
  1069. ALIAS (vtysh_exit_ospfd,
  1070. vtysh_quit_ospfd_cmd,
  1071. "quit",
  1072. "Exit current mode and down to previous mode\n")
  1073. DEFUNSH (VTYSH_OSPF6D,
  1074. vtysh_exit_ospf6d,
  1075. vtysh_exit_ospf6d_cmd,
  1076. "exit",
  1077. "Exit current mode and down to previous mode\n")
  1078. {
  1079. return vtysh_exit (vty);
  1080. }
  1081. ALIAS (vtysh_exit_ospf6d,
  1082. vtysh_quit_ospf6d_cmd,
  1083. "quit",
  1084. "Exit current mode and down to previous mode\n")
  1085. DEFUNSH (VTYSH_ISISD,
  1086. vtysh_exit_isisd,
  1087. vtysh_exit_isisd_cmd,
  1088. "exit",
  1089. "Exit current mode and down to previous mode\n")
  1090. {
  1091. return vtysh_exit (vty);
  1092. }
  1093. ALIAS (vtysh_exit_isisd,
  1094. vtysh_quit_isisd_cmd,
  1095. "quit",
  1096. "Exit current mode and down to previous mode\n")
  1097. DEFUNSH (VTYSH_ALL,
  1098. vtysh_exit_line_vty,
  1099. vtysh_exit_line_vty_cmd,
  1100. "exit",
  1101. "Exit current mode and down to previous mode\n")
  1102. {
  1103. return vtysh_exit (vty);
  1104. }
  1105. ALIAS (vtysh_exit_line_vty,
  1106. vtysh_quit_line_vty_cmd,
  1107. "quit",
  1108. "Exit current mode and down to previous mode\n")
  1109. DEFUNSH (VTYSH_INTERFACE,
  1110. vtysh_interface,
  1111. vtysh_interface_cmd,
  1112. "interface IFNAME",
  1113. "Select an interface to configure\n"
  1114. "Interface's name\n")
  1115. {
  1116. vty->node = INTERFACE_NODE;
  1117. return CMD_SUCCESS;
  1118. }
  1119. /* TODO Implement "no interface command in isisd. */
  1120. DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
  1121. vtysh_no_interface_cmd,
  1122. "no interface IFNAME",
  1123. NO_STR
  1124. "Delete a pseudo interface's configuration\n"
  1125. "Interface's name\n")
  1126. /* TODO Implement interface description commands in ripngd, ospf6d
  1127. * and isisd. */
  1128. DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
  1129. interface_desc_cmd,
  1130. "description .LINE",
  1131. "Interface specific description\n"
  1132. "Characters describing this interface\n")
  1133. DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
  1134. no_interface_desc_cmd,
  1135. "no description",
  1136. NO_STR
  1137. "Interface specific description\n")
  1138. DEFUNSH (VTYSH_INTERFACE,
  1139. vtysh_exit_interface,
  1140. vtysh_exit_interface_cmd,
  1141. "exit",
  1142. "Exit current mode and down to previous mode\n")
  1143. {
  1144. return vtysh_exit (vty);
  1145. }
  1146. ALIAS (vtysh_exit_interface,
  1147. vtysh_quit_interface_cmd,
  1148. "quit",
  1149. "Exit current mode and down to previous mode\n")
  1150. /* Memory */
  1151. DEFUN (vtysh_show_memory,
  1152. vtysh_show_memory_cmd,
  1153. "show memory",
  1154. SHOW_STR
  1155. "Memory statistics\n")
  1156. {
  1157. unsigned int i;
  1158. int ret = CMD_SUCCESS;
  1159. char line[] = "show memory\n";
  1160. for (i = 0; i < array_size(vtysh_client); i++)
  1161. if ( vtysh_client[i].fd >= 0 )
  1162. {
  1163. fprintf (stdout, "Memory statistics for %s:\n",
  1164. vtysh_client[i].name);
  1165. ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
  1166. fprintf (stdout,"\n");
  1167. }
  1168. return ret;
  1169. }
  1170. /* Logging commands. */
  1171. DEFUN (vtysh_show_logging,
  1172. vtysh_show_logging_cmd,
  1173. "show logging",
  1174. SHOW_STR
  1175. "Show current logging configuration\n")
  1176. {
  1177. unsigned int i;
  1178. int ret = CMD_SUCCESS;
  1179. char line[] = "show logging\n";
  1180. for (i = 0; i < array_size(vtysh_client); i++)
  1181. if ( vtysh_client[i].fd >= 0 )
  1182. {
  1183. fprintf (stdout,"Logging configuration for %s:\n",
  1184. vtysh_client[i].name);
  1185. ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
  1186. fprintf (stdout,"\n");
  1187. }
  1188. return ret;
  1189. }
  1190. DEFUNSH (VTYSH_ALL,
  1191. vtysh_log_stdout,
  1192. vtysh_log_stdout_cmd,
  1193. "log stdout",
  1194. "Logging control\n"
  1195. "Set stdout logging level\n")
  1196. {
  1197. return CMD_SUCCESS;
  1198. }
  1199. DEFUNSH (VTYSH_ALL,
  1200. vtysh_log_stdout_level,
  1201. vtysh_log_stdout_level_cmd,
  1202. "log stdout "LOG_LEVELS,
  1203. "Logging control\n"
  1204. "Set stdout logging level\n"
  1205. LOG_LEVEL_DESC)
  1206. {
  1207. return CMD_SUCCESS;
  1208. }
  1209. DEFUNSH (VTYSH_ALL,
  1210. no_vtysh_log_stdout,
  1211. no_vtysh_log_stdout_cmd,
  1212. "no log stdout [LEVEL]",
  1213. NO_STR
  1214. "Logging control\n"
  1215. "Cancel logging to stdout\n"
  1216. "Logging level\n")
  1217. {
  1218. return CMD_SUCCESS;
  1219. }
  1220. DEFUNSH (VTYSH_ALL,
  1221. vtysh_log_file,
  1222. vtysh_log_file_cmd,
  1223. "log file FILENAME",
  1224. "Logging control\n"
  1225. "Logging to file\n"
  1226. "Logging filename\n")
  1227. {
  1228. return CMD_SUCCESS;
  1229. }
  1230. DEFUNSH (VTYSH_ALL,
  1231. vtysh_log_file_level,
  1232. vtysh_log_file_level_cmd,
  1233. "log file FILENAME "LOG_LEVELS,
  1234. "Logging control\n"
  1235. "Logging to file\n"
  1236. "Logging filename\n"
  1237. LOG_LEVEL_DESC)
  1238. {
  1239. return CMD_SUCCESS;
  1240. }
  1241. DEFUNSH (VTYSH_ALL,
  1242. no_vtysh_log_file,
  1243. no_vtysh_log_file_cmd,
  1244. "no log file [FILENAME]",
  1245. NO_STR
  1246. "Logging control\n"
  1247. "Cancel logging to file\n"
  1248. "Logging file name\n")
  1249. {
  1250. return CMD_SUCCESS;
  1251. }
  1252. ALIAS_SH (VTYSH_ALL,
  1253. no_vtysh_log_file,
  1254. no_vtysh_log_file_level_cmd,
  1255. "no log file FILENAME LEVEL",
  1256. NO_STR
  1257. "Logging control\n"
  1258. "Cancel logging to file\n"
  1259. "Logging file name\n"
  1260. "Logging level\n")
  1261. DEFUNSH (VTYSH_ALL,
  1262. vtysh_log_monitor,
  1263. vtysh_log_monitor_cmd,
  1264. "log monitor",
  1265. "Logging control\n"
  1266. "Set terminal line (monitor) logging level\n")
  1267. {
  1268. return CMD_SUCCESS;
  1269. }
  1270. DEFUNSH (VTYSH_ALL,
  1271. vtysh_log_monitor_level,
  1272. vtysh_log_monitor_level_cmd,
  1273. "log monitor "LOG_LEVELS,
  1274. "Logging control\n"
  1275. "Set terminal line (monitor) logging level\n"
  1276. LOG_LEVEL_DESC)
  1277. {
  1278. return CMD_SUCCESS;
  1279. }
  1280. DEFUNSH (VTYSH_ALL,
  1281. no_vtysh_log_monitor,
  1282. no_vtysh_log_monitor_cmd,
  1283. "no log monitor [LEVEL]",
  1284. NO_STR
  1285. "Logging control\n"
  1286. "Disable terminal line (monitor) logging\n"
  1287. "Logging level\n")
  1288. {
  1289. return CMD_SUCCESS;
  1290. }
  1291. DEFUNSH (VTYSH_ALL,
  1292. vtysh_log_syslog,
  1293. vtysh_log_syslog_cmd,
  1294. "log syslog",
  1295. "Logging control\n"
  1296. "Set syslog logging level\n")
  1297. {
  1298. return CMD_SUCCESS;
  1299. }
  1300. DEFUNSH (VTYSH_ALL,
  1301. vtysh_log_syslog_level,
  1302. vtysh_log_syslog_level_cmd,
  1303. "log syslog "LOG_LEVELS,
  1304. "Logging control\n"
  1305. "Set syslog logging level\n"
  1306. LOG_LEVEL_DESC)
  1307. {
  1308. return CMD_SUCCESS;
  1309. }
  1310. DEFUNSH (VTYSH_ALL,
  1311. no_vtysh_log_syslog,
  1312. no_vtysh_log_syslog_cmd,
  1313. "no log syslog [LEVEL]",
  1314. NO_STR
  1315. "Logging control\n"
  1316. "Cancel logging to syslog\n"
  1317. "Logging level\n")
  1318. {
  1319. return CMD_SUCCESS;
  1320. }
  1321. DEFUNSH (VTYSH_ALL,
  1322. vtysh_log_facility,
  1323. vtysh_log_facility_cmd,
  1324. "log facility "LOG_FACILITIES,
  1325. "Logging control\n"
  1326. "Facility parameter for syslog messages\n"
  1327. LOG_FACILITY_DESC)
  1328. {
  1329. return CMD_SUCCESS;
  1330. }
  1331. DEFUNSH (VTYSH_ALL,
  1332. no_vtysh_log_facility,
  1333. no_vtysh_log_facility_cmd,
  1334. "no log facility [FACILITY]",
  1335. NO_STR
  1336. "Logging control\n"
  1337. "Reset syslog facility to default (daemon)\n"
  1338. "Syslog facility\n")
  1339. {
  1340. return CMD_SUCCESS;
  1341. }
  1342. DEFUNSH_DEPRECATED (VTYSH_ALL,
  1343. vtysh_log_trap,
  1344. vtysh_log_trap_cmd,
  1345. "log trap "LOG_LEVELS,
  1346. "Logging control\n"
  1347. "(Deprecated) Set logging level and default for all destinations\n"
  1348. LOG_LEVEL_DESC)
  1349. {
  1350. return CMD_SUCCESS;
  1351. }
  1352. DEFUNSH_DEPRECATED (VTYSH_ALL,
  1353. no_vtysh_log_trap,
  1354. no_vtysh_log_trap_cmd,
  1355. "no log trap [LEVEL]",
  1356. NO_STR
  1357. "Logging control\n"
  1358. "Permit all logging information\n"
  1359. "Logging level\n")
  1360. {
  1361. return CMD_SUCCESS;
  1362. }
  1363. DEFUNSH (VTYSH_ALL,
  1364. vtysh_log_record_priority,
  1365. vtysh_log_record_priority_cmd,
  1366. "log record-priority",
  1367. "Logging control\n"
  1368. "Log the priority of the message within the message\n")
  1369. {
  1370. return CMD_SUCCESS;
  1371. }
  1372. DEFUNSH (VTYSH_ALL,
  1373. no_vtysh_log_record_priority,
  1374. no_vtysh_log_record_priority_cmd,
  1375. "no log record-priority",
  1376. NO_STR
  1377. "Logging control\n"
  1378. "Do not log the priority of the message within the message\n")
  1379. {
  1380. return CMD_SUCCESS;
  1381. }
  1382. DEFUNSH (VTYSH_ALL,
  1383. vtysh_log_timestamp_precision,
  1384. vtysh_log_timestamp_precision_cmd,
  1385. "log timestamp precision <0-6>",
  1386. "Logging control\n"
  1387. "Timestamp configuration\n"
  1388. "Set the timestamp precision\n"
  1389. "Number of subsecond digits\n")
  1390. {
  1391. return CMD_SUCCESS;
  1392. }
  1393. DEFUNSH (VTYSH_ALL,
  1394. no_vtysh_log_timestamp_precision,
  1395. no_vtysh_log_timestamp_precision_cmd,
  1396. "no log timestamp precision",
  1397. NO_STR
  1398. "Logging control\n"
  1399. "Timestamp configuration\n"
  1400. "Reset the timestamp precision to the default value of 0\n")
  1401. {
  1402. return CMD_SUCCESS;
  1403. }
  1404. DEFUNSH (VTYSH_ALL,
  1405. vtysh_service_password_encrypt,
  1406. vtysh_service_password_encrypt_cmd,
  1407. "service password-encryption",
  1408. "Set up miscellaneous service\n"
  1409. "Enable encrypted passwords\n")
  1410. {
  1411. return CMD_SUCCESS;
  1412. }
  1413. DEFUNSH (VTYSH_ALL,
  1414. no_vtysh_service_password_encrypt,
  1415. no_vtysh_service_password_encrypt_cmd,
  1416. "no service password-encryption",
  1417. NO_STR
  1418. "Set up miscellaneous service\n"
  1419. "Enable encrypted passwords\n")
  1420. {
  1421. return CMD_SUCCESS;
  1422. }
  1423. DEFUNSH (VTYSH_ALL,
  1424. vtysh_config_password,
  1425. vtysh_password_cmd,
  1426. "password (8|) WORD",
  1427. "Assign the terminal connection password\n"
  1428. "Specifies a HIDDEN password will follow\n"
  1429. "dummy string \n"
  1430. "The HIDDEN line password string\n")
  1431. {
  1432. return CMD_SUCCESS;
  1433. }
  1434. DEFUNSH (VTYSH_ALL,
  1435. vtysh_password_text,
  1436. vtysh_password_text_cmd,
  1437. "password LINE",
  1438. "Assign the terminal connection password\n"
  1439. "The UNENCRYPTED (cleartext) line password\n")
  1440. {
  1441. return CMD_SUCCESS;
  1442. }
  1443. DEFUNSH (VTYSH_ALL,
  1444. vtysh_config_enable_password,
  1445. vtysh_enable_password_cmd,
  1446. "enable password (8|) WORD",
  1447. "Modify enable password parameters\n"
  1448. "Assign the privileged level password\n"
  1449. "Specifies a HIDDEN password will follow\n"
  1450. "dummy string \n"
  1451. "The HIDDEN 'enable' password string\n")
  1452. {
  1453. return CMD_SUCCESS;
  1454. }
  1455. DEFUNSH (VTYSH_ALL,
  1456. vtysh_enable_password_text,
  1457. vtysh_enable_password_text_cmd,
  1458. "enable password LINE",
  1459. "Modify enable password parameters\n"
  1460. "Assign the privileged level password\n"
  1461. "The UNENCRYPTED (cleartext) 'enable' password\n")
  1462. {
  1463. return CMD_SUCCESS;
  1464. }
  1465. DEFUNSH (VTYSH_ALL,
  1466. no_vtysh_config_enable_password,
  1467. no_vtysh_enable_password_cmd,
  1468. "no enable password",
  1469. NO_STR
  1470. "Modify enable password parameters\n"
  1471. "Assign the privileged level password\n")
  1472. {
  1473. return CMD_SUCCESS;
  1474. }
  1475. DEFUN (vtysh_write_terminal,
  1476. vtysh_write_terminal_cmd,
  1477. "write terminal",
  1478. "Write running configuration to memory, network, or terminal\n"
  1479. "Write to terminal\n")
  1480. {
  1481. u_int i;
  1482. char line[] = "write terminal\n";
  1483. FILE *fp = NULL;
  1484. if (vtysh_pager_name)
  1485. {
  1486. fp = popen (vtysh_pager_name, "w");
  1487. if (fp == NULL)
  1488. {
  1489. perror ("popen");
  1490. exit (1);
  1491. }
  1492. }
  1493. else
  1494. fp = stdout;
  1495. vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
  1496. vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
  1497. VTY_NEWLINE);
  1498. vty_out (vty, "!%s", VTY_NEWLINE);
  1499. for (i = 0; i < array_size(vtysh_client); i++)
  1500. vtysh_client_config (&vtysh_client[i], line);
  1501. /* Integrate vtysh specific configuration. */
  1502. vtysh_config_write ();
  1503. vtysh_config_dump (fp);
  1504. if (vtysh_pager_name && fp)
  1505. {
  1506. fflush (fp);
  1507. if (pclose (fp) == -1)
  1508. {
  1509. perror ("pclose");
  1510. exit (1);
  1511. }
  1512. fp = NULL;
  1513. }
  1514. vty_out (vty, "end%s", VTY_NEWLINE);
  1515. return CMD_SUCCESS;
  1516. }
  1517. DEFUN (vtysh_integrated_config,
  1518. vtysh_integrated_config_cmd,
  1519. "service integrated-vtysh-config",
  1520. "Set up miscellaneous service\n"
  1521. "Write configuration into integrated file\n")
  1522. {
  1523. vtysh_writeconfig_integrated = 1;
  1524. return CMD_SUCCESS;
  1525. }
  1526. DEFUN (no_vtysh_integrated_config,
  1527. no_vtysh_integrated_config_cmd,
  1528. "no service integrated-vtysh-config",
  1529. NO_STR
  1530. "Set up miscellaneous service\n"
  1531. "Write configuration into integrated file\n")
  1532. {
  1533. vtysh_writeconfig_integrated = 0;
  1534. return CMD_SUCCESS;
  1535. }
  1536. static int
  1537. write_config_integrated(void)
  1538. {
  1539. u_int i;
  1540. char line[] = "write terminal\n";
  1541. FILE *fp;
  1542. char *integrate_sav = NULL;
  1543. integrate_sav = malloc (strlen (integrate_default) +
  1544. strlen (CONF_BACKUP_EXT) + 1);
  1545. strcpy (integrate_sav, integrate_default);
  1546. strcat (integrate_sav, CONF_BACKUP_EXT);
  1547. fprintf (stdout,"Building Configuration...\n");
  1548. /* Move current configuration file to backup config file. */
  1549. unlink (integrate_sav);
  1550. rename (integrate_default, integrate_sav);
  1551. free (integrate_sav);
  1552. fp = fopen (integrate_default, "w");
  1553. if (fp == NULL)
  1554. {
  1555. fprintf (stdout,"%% Can't open configuration file %s.\n",
  1556. integrate_default);
  1557. return CMD_SUCCESS;
  1558. }
  1559. for (i = 0; i < array_size(vtysh_client); i++)
  1560. vtysh_client_config (&vtysh_client[i], line);
  1561. vtysh_config_dump (fp);
  1562. fclose (fp);
  1563. if (chmod (integrate_default, CONFIGFILE_MASK) != 0)
  1564. {
  1565. fprintf (stdout,"%% Can't chmod configuration file %s: %s (%d)\n",
  1566. integrate_default, safe_strerror(errno), errno);
  1567. return CMD_WARNING;
  1568. }
  1569. fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
  1570. fprintf (stdout,"[OK]\n");
  1571. return CMD_SUCCESS;
  1572. }
  1573. DEFUN (vtysh_write_memory,
  1574. vtysh_write_memory_cmd,
  1575. "write memory",
  1576. "Write running configuration to memory, network, or terminal\n"
  1577. "Write configuration to the file (same as write file)\n")
  1578. {
  1579. int ret = CMD_SUCCESS;
  1580. char line[] = "write memory\n";
  1581. u_int i;
  1582. /* If integrated Quagga.conf explicitely set. */
  1583. if (vtysh_writeconfig_integrated)
  1584. return write_config_integrated();
  1585. fprintf (stdout,"Building Configuration...\n");
  1586. for (i = 0; i < array_size(vtysh_client); i++)
  1587. ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
  1588. fprintf (stdout,"[OK]\n");
  1589. return ret;
  1590. }
  1591. ALIAS (vtysh_write_memory,
  1592. vtysh_copy_runningconfig_startupconfig_cmd,
  1593. "copy running-config startup-config",
  1594. "Copy from one file to another\n"
  1595. "Copy from current system configuration\n"
  1596. "Copy to startup configuration\n")
  1597. ALIAS (vtysh_write_memory,
  1598. vtysh_write_file_cmd,
  1599. "write file",
  1600. "Write running configuration to memory, network, or terminal\n"
  1601. "Write configuration to the file (same as write memory)\n")
  1602. ALIAS (vtysh_write_memory,
  1603. vtysh_write_cmd,
  1604. "write",
  1605. "Write running configuration to memory, network, or terminal\n")
  1606. ALIAS (vtysh_write_terminal,
  1607. vtysh_show_running_config_cmd,
  1608. "show running-config",
  1609. SHOW_STR
  1610. "Current operating configuration\n")
  1611. DEFUN (vtysh_terminal_length,
  1612. vtysh_terminal_length_cmd,
  1613. "terminal length <0-512>",
  1614. "Set terminal line parameters\n"
  1615. "Set number of lines on a screen\n"
  1616. "Number of lines on screen (0 for no pausing)\n")
  1617. {
  1618. int lines;
  1619. char *endptr = NULL;
  1620. char default_pager[10];
  1621. lines = strtol (argv[0], &endptr, 10);
  1622. if (lines < 0 || lines > 512 || *endptr != '\0')
  1623. {
  1624. vty_out (vty, "length is malformed%s", VTY_NEWLINE);
  1625. return CMD_WARNING;
  1626. }
  1627. if (vtysh_pager_name)
  1628. {
  1629. free (vtysh_pager_name);
  1630. vtysh_pager_name = NULL;
  1631. }
  1632. if (lines != 0)
  1633. {
  1634. snprintf(default_pager, 10, "more -%i", lines);
  1635. vtysh_pager_name = strdup (default_pager);
  1636. }
  1637. return CMD_SUCCESS;
  1638. }
  1639. DEFUN (vtysh_terminal_no_length,
  1640. vtysh_terminal_no_length_cmd,
  1641. "terminal no length",
  1642. "Set terminal line parameters\n"
  1643. NO_STR
  1644. "Set number of lines on a screen\n")
  1645. {
  1646. if (vtysh_pager_name)
  1647. {
  1648. free (vtysh_pager_name);
  1649. vtysh_pager_name = NULL;
  1650. }
  1651. vtysh_pager_init();
  1652. return CMD_SUCCESS;
  1653. }
  1654. DEFUN (vtysh_show_daemons,
  1655. vtysh_show_daemons_cmd,
  1656. "show daemons",
  1657. SHOW_STR
  1658. "Show list of running daemons\n")
  1659. {
  1660. u_int i;
  1661. for (i = 0; i < array_size(vtysh_client); i++)
  1662. if ( vtysh_client[i].fd >= 0 )
  1663. vty_out(vty, " %s", vtysh_client[i].name);
  1664. vty_out(vty, "%s", VTY_NEWLINE);
  1665. return CMD_SUCCESS;
  1666. }
  1667. /* Execute command in child process. */
  1668. static int
  1669. execute_command (const char *command, int argc, const char *arg1,
  1670. const char *arg2)
  1671. {
  1672. pid_t pid;
  1673. int status;
  1674. /* Call fork(). */
  1675. pid = fork ();
  1676. if (pid < 0)
  1677. {
  1678. /* Failure of fork(). */
  1679. fprintf (stderr, "Can't fork: %s\n", safe_strerror (errno));
  1680. exit (1);
  1681. }
  1682. else if (pid == 0)
  1683. {
  1684. /* This is child process. */
  1685. switch (argc)
  1686. {
  1687. case 0:
  1688. execlp (command, command, (const char *)NULL);
  1689. break;
  1690. case 1:
  1691. execlp (command, command, arg1, (const char *)NULL);
  1692. break;
  1693. case 2:
  1694. execlp (command, command, arg1, arg2, (const char *)NULL);
  1695. break;
  1696. }
  1697. /* When execlp suceed, this part is not executed. */
  1698. fprintf (stderr, "Can't execute %s: %s\n", command, safe_strerror (errno));
  1699. exit (1);
  1700. }
  1701. else
  1702. {
  1703. /* This is parent. */
  1704. execute_flag = 1;
  1705. wait4 (pid, &status, 0, NULL);
  1706. execute_flag = 0;
  1707. }
  1708. return 0;
  1709. }
  1710. DEFUN (vtysh_ping,
  1711. vtysh_ping_cmd,
  1712. "ping WORD",
  1713. "Send echo messages\n"
  1714. "Ping destination address or hostname\n")
  1715. {
  1716. execute_command ("ping", 1, argv[0], NULL);
  1717. return CMD_SUCCESS;
  1718. }
  1719. ALIAS (vtysh_ping,
  1720. vtysh_ping_ip_cmd,
  1721. "ping ip WORD",
  1722. "Send echo messages\n"
  1723. "IP echo\n"
  1724. "Ping destination address or hostname\n")
  1725. DEFUN (vtysh_traceroute,
  1726. vtysh_traceroute_cmd,
  1727. "traceroute WORD",
  1728. "Trace route to destination\n"
  1729. "Trace route to destination address or hostname\n")
  1730. {
  1731. execute_command ("traceroute", 1, argv[0], NULL);
  1732. return CMD_SUCCESS;
  1733. }
  1734. ALIAS (vtysh_traceroute,
  1735. vtysh_traceroute_ip_cmd,
  1736. "traceroute ip WORD",
  1737. "Trace route to destination\n"
  1738. "IP trace\n"
  1739. "Trace route to destination address or hostname\n")
  1740. #ifdef HAVE_IPV6
  1741. DEFUN (vtysh_ping6,
  1742. vtysh_ping6_cmd,
  1743. "ping ipv6 WORD",
  1744. "Send echo messages\n"
  1745. "IPv6 echo\n"
  1746. "Ping destination address or hostname\n")
  1747. {
  1748. execute_command ("ping6", 1, argv[0], NULL);
  1749. return CMD_SUCCESS;
  1750. }
  1751. DEFUN (vtysh_traceroute6,
  1752. vtysh_traceroute6_cmd,
  1753. "traceroute ipv6 WORD",
  1754. "Trace route to destination\n"
  1755. "IPv6 trace\n"
  1756. "Trace route to destination address or hostname\n")
  1757. {
  1758. execute_command ("traceroute6", 1, argv[0], NULL);
  1759. return CMD_SUCCESS;
  1760. }
  1761. #endif
  1762. DEFUN (vtysh_telnet,
  1763. vtysh_telnet_cmd,
  1764. "telnet WORD",
  1765. "Open a telnet connection\n"
  1766. "IP address or hostname of a remote system\n")
  1767. {
  1768. execute_command ("telnet", 1, argv[0], NULL);
  1769. return CMD_SUCCESS;
  1770. }
  1771. DEFUN (vtysh_telnet_port,
  1772. vtysh_telnet_port_cmd,
  1773. "telnet WORD PORT",
  1774. "Open a telnet connection\n"
  1775. "IP address or hostname of a remote system\n"
  1776. "TCP Port number\n")
  1777. {
  1778. execute_command ("telnet", 2, argv[0], argv[1]);
  1779. return CMD_SUCCESS;
  1780. }
  1781. DEFUN (vtysh_ssh,
  1782. vtysh_ssh_cmd,
  1783. "ssh WORD",
  1784. "Open an ssh connection\n"
  1785. "[user@]host\n")
  1786. {
  1787. execute_command ("ssh", 1, argv[0], NULL);
  1788. return CMD_SUCCESS;
  1789. }
  1790. DEFUN (vtysh_start_shell,
  1791. vtysh_start_shell_cmd,
  1792. "start-shell",
  1793. "Start UNIX shell\n")
  1794. {
  1795. execute_command ("sh", 0, NULL, NULL);
  1796. return CMD_SUCCESS;
  1797. }
  1798. DEFUN (vtysh_start_bash,
  1799. vtysh_start_bash_cmd,
  1800. "start-shell bash",
  1801. "Start UNIX shell\n"
  1802. "Start bash\n")
  1803. {
  1804. execute_command ("bash", 0, NULL, NULL);
  1805. return CMD_SUCCESS;
  1806. }
  1807. DEFUN (vtysh_start_zsh,
  1808. vtysh_start_zsh_cmd,
  1809. "start-shell zsh",
  1810. "Start UNIX shell\n"
  1811. "Start Z shell\n")
  1812. {
  1813. execute_command ("zsh", 0, NULL, NULL);
  1814. return CMD_SUCCESS;
  1815. }
  1816. static void
  1817. vtysh_install_default (enum node_type node)
  1818. {
  1819. install_element (node, &config_list_cmd);
  1820. }
  1821. /* Making connection to protocol daemon. */
  1822. static int
  1823. vtysh_connect (struct vtysh_client *vclient)
  1824. {
  1825. int ret;
  1826. int sock, len;
  1827. struct sockaddr_un addr;
  1828. struct stat s_stat;
  1829. /* Stat socket to see if we have permission to access it. */
  1830. ret = stat (vclient->path, &s_stat);
  1831. if (ret < 0 && errno != ENOENT)
  1832. {
  1833. fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
  1834. vclient->path, safe_strerror(errno));
  1835. exit(1);
  1836. }
  1837. if (ret >= 0)
  1838. {
  1839. if (! S_ISSOCK(s_stat.st_mode))
  1840. {
  1841. fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
  1842. vclient->path);
  1843. exit (1);
  1844. }
  1845. }
  1846. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  1847. if (sock < 0)
  1848. {
  1849. #ifdef DEBUG
  1850. fprintf(stderr, "vtysh_connect(%s): socket = %s\n", vclient->path,
  1851. safe_strerror(errno));
  1852. #endif /* DEBUG */
  1853. return -1;
  1854. }
  1855. memset (&addr, 0, sizeof (struct sockaddr_un));
  1856. addr.sun_family = AF_UNIX;
  1857. strncpy (addr.sun_path, vclient->path, strlen (vclient->path));
  1858. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  1859. len = addr.sun_len = SUN_LEN(&addr);
  1860. #else
  1861. len = sizeof (addr.sun_family) + strlen (addr.sun_path);
  1862. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  1863. ret = connect (sock, (struct sockaddr *) &addr, len);
  1864. if (ret < 0)
  1865. {
  1866. #ifdef DEBUG
  1867. fprintf(stderr, "vtysh_connect(%s): connect = %s\n", vclient->path,
  1868. safe_strerror(errno));
  1869. #endif /* DEBUG */
  1870. close (sock);
  1871. return -1;
  1872. }
  1873. vclient->fd = sock;
  1874. return 0;
  1875. }
  1876. int
  1877. vtysh_connect_all(const char *daemon_name)
  1878. {
  1879. u_int i;
  1880. int rc = 0;
  1881. int matches = 0;
  1882. for (i = 0; i < array_size(vtysh_client); i++)
  1883. {
  1884. if (!daemon_name || !strcmp(daemon_name, vtysh_client[i].name))
  1885. {
  1886. matches++;
  1887. if (vtysh_connect(&vtysh_client[i]) == 0)
  1888. rc++;
  1889. /* We need direct access to ripd in vtysh_exit_ripd_only. */
  1890. if (vtysh_client[i].flag == VTYSH_RIPD)
  1891. ripd_client = &vtysh_client[i];
  1892. }
  1893. }
  1894. if (!matches)
  1895. fprintf(stderr, "Error: no daemons match name %s!\n", daemon_name);
  1896. return rc;
  1897. }
  1898. /* To disable readline's filename completion. */
  1899. static char *
  1900. vtysh_completion_entry_function (const char *ignore, int invoking_key)
  1901. {
  1902. return NULL;
  1903. }
  1904. void
  1905. vtysh_readline_init (void)
  1906. {
  1907. /* readline related settings. */
  1908. rl_bind_key ('?', (rl_command_func_t *) vtysh_rl_describe);
  1909. rl_completion_entry_function = vtysh_completion_entry_function;
  1910. rl_attempted_completion_function = (rl_completion_func_t *)new_completion;
  1911. }
  1912. char *
  1913. vtysh_prompt (void)
  1914. {
  1915. static struct utsname names;
  1916. static char buf[100];
  1917. const char*hostname;
  1918. extern struct host host;
  1919. hostname = host.name;
  1920. if (!hostname)
  1921. {
  1922. if (!names.nodename[0])
  1923. uname (&names);
  1924. hostname = names.nodename;
  1925. }
  1926. snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
  1927. return buf;
  1928. }
  1929. void
  1930. vtysh_init_vty (void)
  1931. {
  1932. /* Make vty structure. */
  1933. vty = vty_new ();
  1934. vty->type = VTY_SHELL;
  1935. vty->node = VIEW_NODE;
  1936. /* Initialize commands. */
  1937. cmd_init (0);
  1938. /* Install nodes. */
  1939. install_node (&bgp_node, NULL);
  1940. install_node (&rip_node, NULL);
  1941. install_node (&interface_node, NULL);
  1942. install_node (&rmap_node, NULL);
  1943. install_node (&zebra_node, NULL);
  1944. install_node (&bgp_vpnv4_node, NULL);
  1945. install_node (&bgp_ipv4_node, NULL);
  1946. install_node (&bgp_ipv4m_node, NULL);
  1947. /* #ifdef HAVE_IPV6 */
  1948. install_node (&bgp_ipv6_node, NULL);
  1949. install_node (&bgp_ipv6m_node, NULL);
  1950. /* #endif */
  1951. install_node (&ospf_node, NULL);
  1952. /* #ifdef HAVE_IPV6 */
  1953. install_node (&ripng_node, NULL);
  1954. install_node (&ospf6_node, NULL);
  1955. /* #endif */
  1956. install_node (&keychain_node, NULL);
  1957. install_node (&keychain_key_node, NULL);
  1958. install_node (&isis_node, NULL);
  1959. install_node (&vty_node, NULL);
  1960. vtysh_install_default (VIEW_NODE);
  1961. vtysh_install_default (ENABLE_NODE);
  1962. vtysh_install_default (CONFIG_NODE);
  1963. vtysh_install_default (BGP_NODE);
  1964. vtysh_install_default (RIP_NODE);
  1965. vtysh_install_default (INTERFACE_NODE);
  1966. vtysh_install_default (RMAP_NODE);
  1967. vtysh_install_default (ZEBRA_NODE);
  1968. vtysh_install_default (BGP_VPNV4_NODE);
  1969. vtysh_install_default (BGP_IPV4_NODE);
  1970. vtysh_install_default (BGP_IPV4M_NODE);
  1971. vtysh_install_default (BGP_IPV6_NODE);
  1972. vtysh_install_default (BGP_IPV6M_NODE);
  1973. vtysh_install_default (OSPF_NODE);
  1974. vtysh_install_default (RIPNG_NODE);
  1975. vtysh_install_default (OSPF6_NODE);
  1976. vtysh_install_default (BABEL_NODE);
  1977. vtysh_install_default (ISIS_NODE);
  1978. vtysh_install_default (KEYCHAIN_NODE);
  1979. vtysh_install_default (KEYCHAIN_KEY_NODE);
  1980. vtysh_install_default (VTY_NODE);
  1981. install_element (VIEW_NODE, &vtysh_enable_cmd);
  1982. install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
  1983. install_element (ENABLE_NODE, &vtysh_disable_cmd);
  1984. /* "exit" command. */
  1985. install_element (VIEW_NODE, &vtysh_exit_all_cmd);
  1986. install_element (VIEW_NODE, &vtysh_quit_all_cmd);
  1987. install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
  1988. /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
  1989. install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
  1990. install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
  1991. install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
  1992. install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
  1993. install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
  1994. install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
  1995. install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
  1996. install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
  1997. install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
  1998. install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
  1999. install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
  2000. install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
  2001. install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
  2002. install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
  2003. install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
  2004. install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
  2005. install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
  2006. install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
  2007. install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
  2008. install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
  2009. install_element (BGP_IPV6M_NODE, &vtysh_exit_bgpd_cmd);
  2010. install_element (BGP_IPV6M_NODE, &vtysh_quit_bgpd_cmd);
  2011. install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
  2012. install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
  2013. install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
  2014. install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
  2015. install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
  2016. install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
  2017. install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
  2018. install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
  2019. install_element (VTY_NODE, &vtysh_exit_line_vty_cmd);
  2020. install_element (VTY_NODE, &vtysh_quit_line_vty_cmd);
  2021. /* "end" command. */
  2022. install_element (CONFIG_NODE, &vtysh_end_all_cmd);
  2023. install_element (ENABLE_NODE, &vtysh_end_all_cmd);
  2024. install_element (RIP_NODE, &vtysh_end_all_cmd);
  2025. install_element (RIPNG_NODE, &vtysh_end_all_cmd);
  2026. install_element (OSPF_NODE, &vtysh_end_all_cmd);
  2027. install_element (OSPF6_NODE, &vtysh_end_all_cmd);
  2028. install_element (BABEL_NODE, &vtysh_end_all_cmd);
  2029. install_element (BGP_NODE, &vtysh_end_all_cmd);
  2030. install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
  2031. install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
  2032. install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
  2033. install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
  2034. install_element (BGP_IPV6M_NODE, &vtysh_end_all_cmd);
  2035. install_element (ISIS_NODE, &vtysh_end_all_cmd);
  2036. install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
  2037. install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
  2038. install_element (RMAP_NODE, &vtysh_end_all_cmd);
  2039. install_element (VTY_NODE, &vtysh_end_all_cmd);
  2040. install_element (INTERFACE_NODE, &interface_desc_cmd);
  2041. install_element (INTERFACE_NODE, &no_interface_desc_cmd);
  2042. install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
  2043. install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
  2044. install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
  2045. install_element (CONFIG_NODE, &router_rip_cmd);
  2046. #ifdef HAVE_IPV6
  2047. install_element (CONFIG_NODE, &router_ripng_cmd);
  2048. #endif
  2049. install_element (CONFIG_NODE, &router_ospf_cmd);
  2050. #ifdef HAVE_IPV6
  2051. install_element (CONFIG_NODE, &router_ospf6_cmd);
  2052. #endif
  2053. install_element (CONFIG_NODE, &router_isis_cmd);
  2054. install_element (CONFIG_NODE, &router_bgp_cmd);
  2055. install_element (CONFIG_NODE, &router_bgp_view_cmd);
  2056. install_element (BGP_NODE, &address_family_vpnv4_cmd);
  2057. install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
  2058. install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
  2059. install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
  2060. #ifdef HAVE_IPV6
  2061. install_element (BGP_NODE, &address_family_ipv6_cmd);
  2062. install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
  2063. #endif
  2064. install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
  2065. install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
  2066. install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
  2067. install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
  2068. install_element (BGP_IPV6M_NODE, &exit_address_family_cmd);
  2069. install_element (CONFIG_NODE, &key_chain_cmd);
  2070. install_element (CONFIG_NODE, &route_map_cmd);
  2071. install_element (CONFIG_NODE, &vtysh_line_vty_cmd);
  2072. install_element (KEYCHAIN_NODE, &key_cmd);
  2073. install_element (KEYCHAIN_NODE, &key_chain_cmd);
  2074. install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
  2075. install_element (CONFIG_NODE, &vtysh_interface_cmd);
  2076. install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
  2077. install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
  2078. install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
  2079. install_element (ENABLE_NODE, &vtysh_write_file_cmd);
  2080. install_element (ENABLE_NODE, &vtysh_write_cmd);
  2081. /* "write terminal" command. */
  2082. install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
  2083. install_element (CONFIG_NODE, &vtysh_integrated_config_cmd);
  2084. install_element (CONFIG_NODE, &no_vtysh_integrated_config_cmd);
  2085. /* "write memory" command. */
  2086. install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
  2087. install_element (VIEW_NODE, &vtysh_terminal_length_cmd);
  2088. install_element (ENABLE_NODE, &vtysh_terminal_length_cmd);
  2089. install_element (VIEW_NODE, &vtysh_terminal_no_length_cmd);
  2090. install_element (ENABLE_NODE, &vtysh_terminal_no_length_cmd);
  2091. install_element (VIEW_NODE, &vtysh_show_daemons_cmd);
  2092. install_element (ENABLE_NODE, &vtysh_show_daemons_cmd);
  2093. install_element (VIEW_NODE, &vtysh_ping_cmd);
  2094. install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
  2095. install_element (VIEW_NODE, &vtysh_traceroute_cmd);
  2096. install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
  2097. #ifdef HAVE_IPV6
  2098. install_element (VIEW_NODE, &vtysh_ping6_cmd);
  2099. install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
  2100. #endif
  2101. install_element (VIEW_NODE, &vtysh_telnet_cmd);
  2102. install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
  2103. install_element (VIEW_NODE, &vtysh_ssh_cmd);
  2104. install_element (ENABLE_NODE, &vtysh_ping_cmd);
  2105. install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
  2106. install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
  2107. install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
  2108. #ifdef HAVE_IPV6
  2109. install_element (ENABLE_NODE, &vtysh_ping6_cmd);
  2110. install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
  2111. #endif
  2112. install_element (ENABLE_NODE, &vtysh_telnet_cmd);
  2113. install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
  2114. install_element (ENABLE_NODE, &vtysh_ssh_cmd);
  2115. install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
  2116. install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
  2117. install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
  2118. install_element (VIEW_NODE, &vtysh_show_memory_cmd);
  2119. install_element (ENABLE_NODE, &vtysh_show_memory_cmd);
  2120. /* Logging */
  2121. install_element (ENABLE_NODE, &vtysh_show_logging_cmd);
  2122. install_element (VIEW_NODE, &vtysh_show_logging_cmd);
  2123. install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
  2124. install_element (CONFIG_NODE, &vtysh_log_stdout_level_cmd);
  2125. install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
  2126. install_element (CONFIG_NODE, &vtysh_log_file_cmd);
  2127. install_element (CONFIG_NODE, &vtysh_log_file_level_cmd);
  2128. install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
  2129. install_element (CONFIG_NODE, &no_vtysh_log_file_level_cmd);
  2130. install_element (CONFIG_NODE, &vtysh_log_monitor_cmd);
  2131. install_element (CONFIG_NODE, &vtysh_log_monitor_level_cmd);
  2132. install_element (CONFIG_NODE, &no_vtysh_log_monitor_cmd);
  2133. install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
  2134. install_element (CONFIG_NODE, &vtysh_log_syslog_level_cmd);
  2135. install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
  2136. install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
  2137. install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
  2138. install_element (CONFIG_NODE, &vtysh_log_facility_cmd);
  2139. install_element (CONFIG_NODE, &no_vtysh_log_facility_cmd);
  2140. install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
  2141. install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
  2142. install_element (CONFIG_NODE, &vtysh_log_timestamp_precision_cmd);
  2143. install_element (CONFIG_NODE, &no_vtysh_log_timestamp_precision_cmd);
  2144. install_element (CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
  2145. install_element (CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
  2146. install_element (CONFIG_NODE, &vtysh_password_cmd);
  2147. install_element (CONFIG_NODE, &vtysh_password_text_cmd);
  2148. install_element (CONFIG_NODE, &vtysh_enable_password_cmd);
  2149. install_element (CONFIG_NODE, &vtysh_enable_password_text_cmd);
  2150. install_element (CONFIG_NODE, &no_vtysh_enable_password_cmd);
  2151. }