vtysh.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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 = "babeld", .flag = VTYSH_BABELD, .path = BABEL_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. 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. 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 desc *desc;
  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, '\0');
  488. }
  489. else
  490. if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
  491. vector_set (vline, '\0');
  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 ((desc = vector_slot (describe, i)) != NULL)
  514. {
  515. int len;
  516. if (desc->cmd[0] == '\0')
  517. continue;
  518. len = strlen (desc->cmd);
  519. if (desc->cmd[0] == '.')
  520. len--;
  521. if (width < len)
  522. width = len;
  523. }
  524. for (i = 0; i < vector_active (describe); i++)
  525. if ((desc = vector_slot (describe, i)) != NULL)
  526. {
  527. if (desc->cmd[0] == '\0')
  528. continue;
  529. if (! desc->str)
  530. fprintf (stdout," %-s\n",
  531. desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd);
  532. else
  533. fprintf (stdout," %-*s %s\n",
  534. width,
  535. desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
  536. desc->str);
  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, '\0');
  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 babel_node =
  677. {
  678. BABEL_NODE,
  679. "%s(config-babel)# "
  680. };
  681. static struct cmd_node keychain_node =
  682. {
  683. KEYCHAIN_NODE,
  684. "%s(config-keychain)# "
  685. };
  686. static struct cmd_node keychain_key_node =
  687. {
  688. KEYCHAIN_KEY_NODE,
  689. "%s(config-keychain-key)# "
  690. };
  691. /* Defined in lib/vty.c */
  692. extern struct cmd_node vty_node;
  693. /* When '^Z' is received from vty, move down to the enable mode. */
  694. int
  695. vtysh_end (void)
  696. {
  697. switch (vty->node)
  698. {
  699. case VIEW_NODE:
  700. case ENABLE_NODE:
  701. /* Nothing to do. */
  702. break;
  703. default:
  704. vty->node = ENABLE_NODE;
  705. break;
  706. }
  707. return CMD_SUCCESS;
  708. }
  709. DEFUNSH (VTYSH_ALL,
  710. vtysh_end_all,
  711. vtysh_end_all_cmd,
  712. "end",
  713. "End current mode and change to enable mode\n")
  714. {
  715. return vtysh_end ();
  716. }
  717. DEFUNSH (VTYSH_BGPD,
  718. router_bgp,
  719. router_bgp_cmd,
  720. "router bgp " CMD_AS_RANGE,
  721. ROUTER_STR
  722. BGP_STR
  723. AS_STR)
  724. {
  725. vty->node = BGP_NODE;
  726. return CMD_SUCCESS;
  727. }
  728. ALIAS_SH (VTYSH_BGPD,
  729. router_bgp,
  730. router_bgp_view_cmd,
  731. "router bgp " CMD_AS_RANGE " view WORD",
  732. ROUTER_STR
  733. BGP_STR
  734. AS_STR
  735. "BGP view\n"
  736. "view name\n")
  737. DEFUNSH (VTYSH_BGPD,
  738. address_family_vpnv4,
  739. address_family_vpnv4_cmd,
  740. "address-family vpnv4",
  741. "Enter Address Family command mode\n"
  742. "Address family\n")
  743. {
  744. vty->node = BGP_VPNV4_NODE;
  745. return CMD_SUCCESS;
  746. }
  747. DEFUNSH (VTYSH_BGPD,
  748. address_family_vpnv4_unicast,
  749. address_family_vpnv4_unicast_cmd,
  750. "address-family vpnv4 unicast",
  751. "Enter Address Family command mode\n"
  752. "Address family\n"
  753. "Address Family Modifier\n")
  754. {
  755. vty->node = BGP_VPNV4_NODE;
  756. return CMD_SUCCESS;
  757. }
  758. DEFUNSH (VTYSH_BGPD,
  759. address_family_ipv4_unicast,
  760. address_family_ipv4_unicast_cmd,
  761. "address-family ipv4 unicast",
  762. "Enter Address Family command mode\n"
  763. "Address family\n"
  764. "Address Family Modifier\n")
  765. {
  766. vty->node = BGP_IPV4_NODE;
  767. return CMD_SUCCESS;
  768. }
  769. DEFUNSH (VTYSH_BGPD,
  770. address_family_ipv4_multicast,
  771. address_family_ipv4_multicast_cmd,
  772. "address-family ipv4 multicast",
  773. "Enter Address Family command mode\n"
  774. "Address family\n"
  775. "Address Family Modifier\n")
  776. {
  777. vty->node = BGP_IPV4M_NODE;
  778. return CMD_SUCCESS;
  779. }
  780. DEFUNSH (VTYSH_BGPD,
  781. address_family_ipv6,
  782. address_family_ipv6_cmd,
  783. "address-family ipv6",
  784. "Enter Address Family command mode\n"
  785. "Address family\n")
  786. {
  787. vty->node = BGP_IPV6_NODE;
  788. return CMD_SUCCESS;
  789. }
  790. DEFUNSH (VTYSH_BGPD,
  791. address_family_ipv6_unicast,
  792. address_family_ipv6_unicast_cmd,
  793. "address-family ipv6 unicast",
  794. "Enter Address Family command mode\n"
  795. "Address family\n"
  796. "Address Family Modifier\n")
  797. {
  798. vty->node = BGP_IPV6_NODE;
  799. return CMD_SUCCESS;
  800. }
  801. DEFUNSH (VTYSH_BGPD,
  802. address_family_ipv6_multicast,
  803. address_family_ipv6_multicast_cmd,
  804. "address-family ipv6 multicast",
  805. "Enter Address Family command mode\n"
  806. "Address family\n"
  807. "Address Family Modifier\n")
  808. {
  809. vty->node = BGP_IPV6M_NODE;
  810. return CMD_SUCCESS;
  811. }
  812. DEFUNSH (VTYSH_RIPD,
  813. key_chain,
  814. key_chain_cmd,
  815. "key chain WORD",
  816. "Authentication key management\n"
  817. "Key-chain management\n"
  818. "Key-chain name\n")
  819. {
  820. vty->node = KEYCHAIN_NODE;
  821. return CMD_SUCCESS;
  822. }
  823. DEFUNSH (VTYSH_RIPD,
  824. key,
  825. key_cmd,
  826. "key <0-2147483647>",
  827. "Configure a key\n"
  828. "Key identifier number\n")
  829. {
  830. vty->node = KEYCHAIN_KEY_NODE;
  831. return CMD_SUCCESS;
  832. }
  833. DEFUNSH (VTYSH_RIPD,
  834. router_rip,
  835. router_rip_cmd,
  836. "router rip",
  837. ROUTER_STR
  838. "RIP")
  839. {
  840. vty->node = RIP_NODE;
  841. return CMD_SUCCESS;
  842. }
  843. DEFUNSH (VTYSH_RIPNGD,
  844. router_ripng,
  845. router_ripng_cmd,
  846. "router ripng",
  847. ROUTER_STR
  848. "RIPng")
  849. {
  850. vty->node = RIPNG_NODE;
  851. return CMD_SUCCESS;
  852. }
  853. DEFUNSH (VTYSH_OSPFD,
  854. router_ospf,
  855. router_ospf_cmd,
  856. "router ospf",
  857. "Enable a routing process\n"
  858. "Start OSPF configuration\n")
  859. {
  860. vty->node = OSPF_NODE;
  861. return CMD_SUCCESS;
  862. }
  863. DEFUNSH (VTYSH_OSPF6D,
  864. router_ospf6,
  865. router_ospf6_cmd,
  866. "router ospf6",
  867. OSPF6_ROUTER_STR
  868. OSPF6_STR)
  869. {
  870. vty->node = OSPF6_NODE;
  871. return CMD_SUCCESS;
  872. }
  873. DEFUNSH (VTYSH_BABELD,
  874. router_babel,
  875. router_babel_cmd,
  876. "router babel",
  877. ROUTER_STR
  878. "Babel")
  879. {
  880. vty->node = BABEL_NODE;
  881. return CMD_SUCCESS;
  882. }
  883. DEFUNSH (VTYSH_ISISD,
  884. router_isis,
  885. router_isis_cmd,
  886. "router isis WORD",
  887. ROUTER_STR
  888. "ISO IS-IS\n"
  889. "ISO Routing area tag")
  890. {
  891. vty->node = ISIS_NODE;
  892. return CMD_SUCCESS;
  893. }
  894. DEFUNSH (VTYSH_RMAP,
  895. route_map,
  896. route_map_cmd,
  897. "route-map WORD (deny|permit) <1-65535>",
  898. "Create route-map or enter route-map command mode\n"
  899. "Route map tag\n"
  900. "Route map denies set operations\n"
  901. "Route map permits set operations\n"
  902. "Sequence to insert to/delete from existing route-map entry\n")
  903. {
  904. vty->node = RMAP_NODE;
  905. return CMD_SUCCESS;
  906. }
  907. DEFUNSH (VTYSH_ALL,
  908. vtysh_line_vty,
  909. vtysh_line_vty_cmd,
  910. "line vty",
  911. "Configure a terminal line\n"
  912. "Virtual terminal\n")
  913. {
  914. vty->node = VTY_NODE;
  915. return CMD_SUCCESS;
  916. }
  917. DEFUNSH (VTYSH_ALL,
  918. vtysh_enable,
  919. vtysh_enable_cmd,
  920. "enable",
  921. "Turn on privileged mode command\n")
  922. {
  923. vty->node = ENABLE_NODE;
  924. return CMD_SUCCESS;
  925. }
  926. DEFUNSH (VTYSH_ALL,
  927. vtysh_disable,
  928. vtysh_disable_cmd,
  929. "disable",
  930. "Turn off privileged mode command\n")
  931. {
  932. if (vty->node == ENABLE_NODE)
  933. vty->node = VIEW_NODE;
  934. return CMD_SUCCESS;
  935. }
  936. DEFUNSH (VTYSH_ALL,
  937. vtysh_config_terminal,
  938. vtysh_config_terminal_cmd,
  939. "configure terminal",
  940. "Configuration from vty interface\n"
  941. "Configuration terminal\n")
  942. {
  943. vty->node = CONFIG_NODE;
  944. return CMD_SUCCESS;
  945. }
  946. static int
  947. vtysh_exit (struct vty *vty)
  948. {
  949. switch (vty->node)
  950. {
  951. case VIEW_NODE:
  952. case ENABLE_NODE:
  953. exit (0);
  954. break;
  955. case CONFIG_NODE:
  956. vty->node = ENABLE_NODE;
  957. break;
  958. case INTERFACE_NODE:
  959. case ZEBRA_NODE:
  960. case BGP_NODE:
  961. case RIP_NODE:
  962. case RIPNG_NODE:
  963. case OSPF_NODE:
  964. case OSPF6_NODE:
  965. case BABEL_NODE:
  966. case ISIS_NODE:
  967. case MASC_NODE:
  968. case RMAP_NODE:
  969. case VTY_NODE:
  970. case KEYCHAIN_NODE:
  971. vtysh_execute("end");
  972. vtysh_execute("configure terminal");
  973. vty->node = CONFIG_NODE;
  974. break;
  975. case BGP_VPNV4_NODE:
  976. case BGP_IPV4_NODE:
  977. case BGP_IPV4M_NODE:
  978. case BGP_IPV6_NODE:
  979. case BGP_IPV6M_NODE:
  980. vty->node = BGP_NODE;
  981. break;
  982. case KEYCHAIN_KEY_NODE:
  983. vty->node = KEYCHAIN_NODE;
  984. break;
  985. default:
  986. break;
  987. }
  988. return CMD_SUCCESS;
  989. }
  990. DEFUNSH (VTYSH_ALL,
  991. vtysh_exit_all,
  992. vtysh_exit_all_cmd,
  993. "exit",
  994. "Exit current mode and down to previous mode\n")
  995. {
  996. return vtysh_exit (vty);
  997. }
  998. ALIAS (vtysh_exit_all,
  999. vtysh_quit_all_cmd,
  1000. "quit",
  1001. "Exit current mode and down to previous mode\n")
  1002. DEFUNSH (VTYSH_BGPD,
  1003. exit_address_family,
  1004. exit_address_family_cmd,
  1005. "exit-address-family",
  1006. "Exit from Address Family configuration mode\n")
  1007. {
  1008. if (vty->node == BGP_IPV4_NODE
  1009. || vty->node == BGP_IPV4M_NODE
  1010. || vty->node == BGP_VPNV4_NODE
  1011. || vty->node == BGP_IPV6_NODE
  1012. || vty->node == BGP_IPV6M_NODE)
  1013. vty->node = BGP_NODE;
  1014. return CMD_SUCCESS;
  1015. }
  1016. DEFUNSH (VTYSH_ZEBRA,
  1017. vtysh_exit_zebra,
  1018. vtysh_exit_zebra_cmd,
  1019. "exit",
  1020. "Exit current mode and down to previous mode\n")
  1021. {
  1022. return vtysh_exit (vty);
  1023. }
  1024. ALIAS (vtysh_exit_zebra,
  1025. vtysh_quit_zebra_cmd,
  1026. "quit",
  1027. "Exit current mode and down to previous mode\n")
  1028. DEFUNSH (VTYSH_RIPD,
  1029. vtysh_exit_ripd,
  1030. vtysh_exit_ripd_cmd,
  1031. "exit",
  1032. "Exit current mode and down to previous mode\n")
  1033. {
  1034. return vtysh_exit (vty);
  1035. }
  1036. ALIAS (vtysh_exit_ripd,
  1037. vtysh_quit_ripd_cmd,
  1038. "quit",
  1039. "Exit current mode and down to previous mode\n")
  1040. DEFUNSH (VTYSH_RIPNGD,
  1041. vtysh_exit_ripngd,
  1042. vtysh_exit_ripngd_cmd,
  1043. "exit",
  1044. "Exit current mode and down to previous mode\n")
  1045. {
  1046. return vtysh_exit (vty);
  1047. }
  1048. ALIAS (vtysh_exit_ripngd,
  1049. vtysh_quit_ripngd_cmd,
  1050. "quit",
  1051. "Exit current mode and down to previous mode\n")
  1052. DEFUNSH (VTYSH_RMAP,
  1053. vtysh_exit_rmap,
  1054. vtysh_exit_rmap_cmd,
  1055. "exit",
  1056. "Exit current mode and down to previous mode\n")
  1057. {
  1058. return vtysh_exit (vty);
  1059. }
  1060. ALIAS (vtysh_exit_rmap,
  1061. vtysh_quit_rmap_cmd,
  1062. "quit",
  1063. "Exit current mode and down to previous mode\n")
  1064. DEFUNSH (VTYSH_BGPD,
  1065. vtysh_exit_bgpd,
  1066. vtysh_exit_bgpd_cmd,
  1067. "exit",
  1068. "Exit current mode and down to previous mode\n")
  1069. {
  1070. return vtysh_exit (vty);
  1071. }
  1072. ALIAS (vtysh_exit_bgpd,
  1073. vtysh_quit_bgpd_cmd,
  1074. "quit",
  1075. "Exit current mode and down to previous mode\n")
  1076. DEFUNSH (VTYSH_OSPFD,
  1077. vtysh_exit_ospfd,
  1078. vtysh_exit_ospfd_cmd,
  1079. "exit",
  1080. "Exit current mode and down to previous mode\n")
  1081. {
  1082. return vtysh_exit (vty);
  1083. }
  1084. ALIAS (vtysh_exit_ospfd,
  1085. vtysh_quit_ospfd_cmd,
  1086. "quit",
  1087. "Exit current mode and down to previous mode\n")
  1088. DEFUNSH (VTYSH_OSPF6D,
  1089. vtysh_exit_ospf6d,
  1090. vtysh_exit_ospf6d_cmd,
  1091. "exit",
  1092. "Exit current mode and down to previous mode\n")
  1093. {
  1094. return vtysh_exit (vty);
  1095. }
  1096. ALIAS (vtysh_exit_ospf6d,
  1097. vtysh_quit_ospf6d_cmd,
  1098. "quit",
  1099. "Exit current mode and down to previous mode\n")
  1100. DEFUNSH (VTYSH_ISISD,
  1101. vtysh_exit_isisd,
  1102. vtysh_exit_isisd_cmd,
  1103. "exit",
  1104. "Exit current mode and down to previous mode\n")
  1105. {
  1106. return vtysh_exit (vty);
  1107. }
  1108. ALIAS (vtysh_exit_isisd,
  1109. vtysh_quit_isisd_cmd,
  1110. "quit",
  1111. "Exit current mode and down to previous mode\n")
  1112. DEFUNSH (VTYSH_ALL,
  1113. vtysh_exit_line_vty,
  1114. vtysh_exit_line_vty_cmd,
  1115. "exit",
  1116. "Exit current mode and down to previous mode\n")
  1117. {
  1118. return vtysh_exit (vty);
  1119. }
  1120. ALIAS (vtysh_exit_line_vty,
  1121. vtysh_quit_line_vty_cmd,
  1122. "quit",
  1123. "Exit current mode and down to previous mode\n")
  1124. DEFUNSH (VTYSH_INTERFACE,
  1125. vtysh_interface,
  1126. vtysh_interface_cmd,
  1127. "interface IFNAME",
  1128. "Select an interface to configure\n"
  1129. "Interface's name\n")
  1130. {
  1131. vty->node = INTERFACE_NODE;
  1132. return CMD_SUCCESS;
  1133. }
  1134. /* TODO Implement "no interface command in isisd. */
  1135. DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
  1136. vtysh_no_interface_cmd,
  1137. "no interface IFNAME",
  1138. NO_STR
  1139. "Delete a pseudo interface's configuration\n"
  1140. "Interface's name\n")
  1141. /* TODO Implement interface description commands in ripngd, ospf6d
  1142. * and isisd. */
  1143. DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
  1144. interface_desc_cmd,
  1145. "description .LINE",
  1146. "Interface specific description\n"
  1147. "Characters describing this interface\n")
  1148. DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
  1149. no_interface_desc_cmd,
  1150. "no description",
  1151. NO_STR
  1152. "Interface specific description\n")
  1153. DEFUNSH (VTYSH_INTERFACE,
  1154. vtysh_exit_interface,
  1155. vtysh_exit_interface_cmd,
  1156. "exit",
  1157. "Exit current mode and down to previous mode\n")
  1158. {
  1159. return vtysh_exit (vty);
  1160. }
  1161. ALIAS (vtysh_exit_interface,
  1162. vtysh_quit_interface_cmd,
  1163. "quit",
  1164. "Exit current mode and down to previous mode\n")
  1165. /* Memory */
  1166. DEFUN (vtysh_show_memory,
  1167. vtysh_show_memory_cmd,
  1168. "show memory",
  1169. SHOW_STR
  1170. "Memory statistics\n")
  1171. {
  1172. unsigned int i;
  1173. int ret = CMD_SUCCESS;
  1174. char line[] = "show memory\n";
  1175. for (i = 0; i < array_size(vtysh_client); i++)
  1176. if ( vtysh_client[i].fd >= 0 )
  1177. {
  1178. fprintf (stdout, "Memory statistics for %s:\n",
  1179. vtysh_client[i].name);
  1180. ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
  1181. fprintf (stdout,"\n");
  1182. }
  1183. return ret;
  1184. }
  1185. /* Logging commands. */
  1186. DEFUN (vtysh_show_logging,
  1187. vtysh_show_logging_cmd,
  1188. "show logging",
  1189. SHOW_STR
  1190. "Show current logging configuration\n")
  1191. {
  1192. unsigned int i;
  1193. int ret = CMD_SUCCESS;
  1194. char line[] = "show logging\n";
  1195. for (i = 0; i < array_size(vtysh_client); i++)
  1196. if ( vtysh_client[i].fd >= 0 )
  1197. {
  1198. fprintf (stdout,"Logging configuration for %s:\n",
  1199. vtysh_client[i].name);
  1200. ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
  1201. fprintf (stdout,"\n");
  1202. }
  1203. return ret;
  1204. }
  1205. DEFUNSH (VTYSH_ALL,
  1206. vtysh_log_stdout,
  1207. vtysh_log_stdout_cmd,
  1208. "log stdout",
  1209. "Logging control\n"
  1210. "Set stdout logging level\n")
  1211. {
  1212. return CMD_SUCCESS;
  1213. }
  1214. DEFUNSH (VTYSH_ALL,
  1215. vtysh_log_stdout_level,
  1216. vtysh_log_stdout_level_cmd,
  1217. "log stdout "LOG_LEVELS,
  1218. "Logging control\n"
  1219. "Set stdout logging level\n"
  1220. LOG_LEVEL_DESC)
  1221. {
  1222. return CMD_SUCCESS;
  1223. }
  1224. DEFUNSH (VTYSH_ALL,
  1225. no_vtysh_log_stdout,
  1226. no_vtysh_log_stdout_cmd,
  1227. "no log stdout [LEVEL]",
  1228. NO_STR
  1229. "Logging control\n"
  1230. "Cancel logging to stdout\n"
  1231. "Logging level\n")
  1232. {
  1233. return CMD_SUCCESS;
  1234. }
  1235. DEFUNSH (VTYSH_ALL,
  1236. vtysh_log_file,
  1237. vtysh_log_file_cmd,
  1238. "log file FILENAME",
  1239. "Logging control\n"
  1240. "Logging to file\n"
  1241. "Logging filename\n")
  1242. {
  1243. return CMD_SUCCESS;
  1244. }
  1245. DEFUNSH (VTYSH_ALL,
  1246. vtysh_log_file_level,
  1247. vtysh_log_file_level_cmd,
  1248. "log file FILENAME "LOG_LEVELS,
  1249. "Logging control\n"
  1250. "Logging to file\n"
  1251. "Logging filename\n"
  1252. LOG_LEVEL_DESC)
  1253. {
  1254. return CMD_SUCCESS;
  1255. }
  1256. DEFUNSH (VTYSH_ALL,
  1257. no_vtysh_log_file,
  1258. no_vtysh_log_file_cmd,
  1259. "no log file [FILENAME]",
  1260. NO_STR
  1261. "Logging control\n"
  1262. "Cancel logging to file\n"
  1263. "Logging file name\n")
  1264. {
  1265. return CMD_SUCCESS;
  1266. }
  1267. ALIAS_SH (VTYSH_ALL,
  1268. no_vtysh_log_file,
  1269. no_vtysh_log_file_level_cmd,
  1270. "no log file FILENAME LEVEL",
  1271. NO_STR
  1272. "Logging control\n"
  1273. "Cancel logging to file\n"
  1274. "Logging file name\n"
  1275. "Logging level\n")
  1276. DEFUNSH (VTYSH_ALL,
  1277. vtysh_log_monitor,
  1278. vtysh_log_monitor_cmd,
  1279. "log monitor",
  1280. "Logging control\n"
  1281. "Set terminal line (monitor) logging level\n")
  1282. {
  1283. return CMD_SUCCESS;
  1284. }
  1285. DEFUNSH (VTYSH_ALL,
  1286. vtysh_log_monitor_level,
  1287. vtysh_log_monitor_level_cmd,
  1288. "log monitor "LOG_LEVELS,
  1289. "Logging control\n"
  1290. "Set terminal line (monitor) logging level\n"
  1291. LOG_LEVEL_DESC)
  1292. {
  1293. return CMD_SUCCESS;
  1294. }
  1295. DEFUNSH (VTYSH_ALL,
  1296. no_vtysh_log_monitor,
  1297. no_vtysh_log_monitor_cmd,
  1298. "no log monitor [LEVEL]",
  1299. NO_STR
  1300. "Logging control\n"
  1301. "Disable terminal line (monitor) logging\n"
  1302. "Logging level\n")
  1303. {
  1304. return CMD_SUCCESS;
  1305. }
  1306. DEFUNSH (VTYSH_ALL,
  1307. vtysh_log_syslog,
  1308. vtysh_log_syslog_cmd,
  1309. "log syslog",
  1310. "Logging control\n"
  1311. "Set syslog logging level\n")
  1312. {
  1313. return CMD_SUCCESS;
  1314. }
  1315. DEFUNSH (VTYSH_ALL,
  1316. vtysh_log_syslog_level,
  1317. vtysh_log_syslog_level_cmd,
  1318. "log syslog "LOG_LEVELS,
  1319. "Logging control\n"
  1320. "Set syslog logging level\n"
  1321. LOG_LEVEL_DESC)
  1322. {
  1323. return CMD_SUCCESS;
  1324. }
  1325. DEFUNSH (VTYSH_ALL,
  1326. no_vtysh_log_syslog,
  1327. no_vtysh_log_syslog_cmd,
  1328. "no log syslog [LEVEL]",
  1329. NO_STR
  1330. "Logging control\n"
  1331. "Cancel logging to syslog\n"
  1332. "Logging level\n")
  1333. {
  1334. return CMD_SUCCESS;
  1335. }
  1336. DEFUNSH (VTYSH_ALL,
  1337. vtysh_log_facility,
  1338. vtysh_log_facility_cmd,
  1339. "log facility "LOG_FACILITIES,
  1340. "Logging control\n"
  1341. "Facility parameter for syslog messages\n"
  1342. LOG_FACILITY_DESC)
  1343. {
  1344. return CMD_SUCCESS;
  1345. }
  1346. DEFUNSH (VTYSH_ALL,
  1347. no_vtysh_log_facility,
  1348. no_vtysh_log_facility_cmd,
  1349. "no log facility [FACILITY]",
  1350. NO_STR
  1351. "Logging control\n"
  1352. "Reset syslog facility to default (daemon)\n"
  1353. "Syslog facility\n")
  1354. {
  1355. return CMD_SUCCESS;
  1356. }
  1357. DEFUNSH_DEPRECATED (VTYSH_ALL,
  1358. vtysh_log_trap,
  1359. vtysh_log_trap_cmd,
  1360. "log trap "LOG_LEVELS,
  1361. "Logging control\n"
  1362. "(Deprecated) Set logging level and default for all destinations\n"
  1363. LOG_LEVEL_DESC)
  1364. {
  1365. return CMD_SUCCESS;
  1366. }
  1367. DEFUNSH_DEPRECATED (VTYSH_ALL,
  1368. no_vtysh_log_trap,
  1369. no_vtysh_log_trap_cmd,
  1370. "no log trap [LEVEL]",
  1371. NO_STR
  1372. "Logging control\n"
  1373. "Permit all logging information\n"
  1374. "Logging level\n")
  1375. {
  1376. return CMD_SUCCESS;
  1377. }
  1378. DEFUNSH (VTYSH_ALL,
  1379. vtysh_log_record_priority,
  1380. vtysh_log_record_priority_cmd,
  1381. "log record-priority",
  1382. "Logging control\n"
  1383. "Log the priority of the message within the message\n")
  1384. {
  1385. return CMD_SUCCESS;
  1386. }
  1387. DEFUNSH (VTYSH_ALL,
  1388. no_vtysh_log_record_priority,
  1389. no_vtysh_log_record_priority_cmd,
  1390. "no log record-priority",
  1391. NO_STR
  1392. "Logging control\n"
  1393. "Do not log the priority of the message within the message\n")
  1394. {
  1395. return CMD_SUCCESS;
  1396. }
  1397. DEFUNSH (VTYSH_ALL,
  1398. vtysh_log_timestamp_precision,
  1399. vtysh_log_timestamp_precision_cmd,
  1400. "log timestamp precision <0-6>",
  1401. "Logging control\n"
  1402. "Timestamp configuration\n"
  1403. "Set the timestamp precision\n"
  1404. "Number of subsecond digits\n")
  1405. {
  1406. return CMD_SUCCESS;
  1407. }
  1408. DEFUNSH (VTYSH_ALL,
  1409. no_vtysh_log_timestamp_precision,
  1410. no_vtysh_log_timestamp_precision_cmd,
  1411. "no log timestamp precision",
  1412. NO_STR
  1413. "Logging control\n"
  1414. "Timestamp configuration\n"
  1415. "Reset the timestamp precision to the default value of 0\n")
  1416. {
  1417. return CMD_SUCCESS;
  1418. }
  1419. DEFUNSH (VTYSH_ALL,
  1420. vtysh_service_password_encrypt,
  1421. vtysh_service_password_encrypt_cmd,
  1422. "service password-encryption",
  1423. "Set up miscellaneous service\n"
  1424. "Enable encrypted passwords\n")
  1425. {
  1426. return CMD_SUCCESS;
  1427. }
  1428. DEFUNSH (VTYSH_ALL,
  1429. no_vtysh_service_password_encrypt,
  1430. no_vtysh_service_password_encrypt_cmd,
  1431. "no service password-encryption",
  1432. NO_STR
  1433. "Set up miscellaneous service\n"
  1434. "Enable encrypted passwords\n")
  1435. {
  1436. return CMD_SUCCESS;
  1437. }
  1438. DEFUNSH (VTYSH_ALL,
  1439. vtysh_config_password,
  1440. vtysh_password_cmd,
  1441. "password (8|) WORD",
  1442. "Assign the terminal connection password\n"
  1443. "Specifies a HIDDEN password will follow\n"
  1444. "dummy string \n"
  1445. "The HIDDEN line password string\n")
  1446. {
  1447. return CMD_SUCCESS;
  1448. }
  1449. DEFUNSH (VTYSH_ALL,
  1450. vtysh_password_text,
  1451. vtysh_password_text_cmd,
  1452. "password LINE",
  1453. "Assign the terminal connection password\n"
  1454. "The UNENCRYPTED (cleartext) line password\n")
  1455. {
  1456. return CMD_SUCCESS;
  1457. }
  1458. DEFUNSH (VTYSH_ALL,
  1459. vtysh_config_enable_password,
  1460. vtysh_enable_password_cmd,
  1461. "enable password (8|) WORD",
  1462. "Modify enable password parameters\n"
  1463. "Assign the privileged level password\n"
  1464. "Specifies a HIDDEN password will follow\n"
  1465. "dummy string \n"
  1466. "The HIDDEN 'enable' password string\n")
  1467. {
  1468. return CMD_SUCCESS;
  1469. }
  1470. DEFUNSH (VTYSH_ALL,
  1471. vtysh_enable_password_text,
  1472. vtysh_enable_password_text_cmd,
  1473. "enable password LINE",
  1474. "Modify enable password parameters\n"
  1475. "Assign the privileged level password\n"
  1476. "The UNENCRYPTED (cleartext) 'enable' password\n")
  1477. {
  1478. return CMD_SUCCESS;
  1479. }
  1480. DEFUNSH (VTYSH_ALL,
  1481. no_vtysh_config_enable_password,
  1482. no_vtysh_enable_password_cmd,
  1483. "no enable password",
  1484. NO_STR
  1485. "Modify enable password parameters\n"
  1486. "Assign the privileged level password\n")
  1487. {
  1488. return CMD_SUCCESS;
  1489. }
  1490. DEFUN (vtysh_write_terminal,
  1491. vtysh_write_terminal_cmd,
  1492. "write terminal",
  1493. "Write running configuration to memory, network, or terminal\n"
  1494. "Write to terminal\n")
  1495. {
  1496. u_int i;
  1497. int ret;
  1498. char line[] = "write terminal\n";
  1499. FILE *fp = NULL;
  1500. if (vtysh_pager_name)
  1501. {
  1502. fp = popen (vtysh_pager_name, "w");
  1503. if (fp == NULL)
  1504. {
  1505. perror ("popen");
  1506. exit (1);
  1507. }
  1508. }
  1509. else
  1510. fp = stdout;
  1511. vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
  1512. vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
  1513. VTY_NEWLINE);
  1514. vty_out (vty, "!%s", VTY_NEWLINE);
  1515. for (i = 0; i < array_size(vtysh_client); i++)
  1516. ret = vtysh_client_config (&vtysh_client[i], line);
  1517. /* Integrate vtysh specific configuration. */
  1518. vtysh_config_write ();
  1519. vtysh_config_dump (fp);
  1520. if (vtysh_pager_name && fp)
  1521. {
  1522. fflush (fp);
  1523. if (pclose (fp) == -1)
  1524. {
  1525. perror ("pclose");
  1526. exit (1);
  1527. }
  1528. fp = NULL;
  1529. }
  1530. vty_out (vty, "end%s", VTY_NEWLINE);
  1531. return CMD_SUCCESS;
  1532. }
  1533. DEFUN (vtysh_integrated_config,
  1534. vtysh_integrated_config_cmd,
  1535. "service integrated-vtysh-config",
  1536. "Set up miscellaneous service\n"
  1537. "Write configuration into integrated file\n")
  1538. {
  1539. vtysh_writeconfig_integrated = 1;
  1540. return CMD_SUCCESS;
  1541. }
  1542. DEFUN (no_vtysh_integrated_config,
  1543. no_vtysh_integrated_config_cmd,
  1544. "no service integrated-vtysh-config",
  1545. NO_STR
  1546. "Set up miscellaneous service\n"
  1547. "Write configuration into integrated file\n")
  1548. {
  1549. vtysh_writeconfig_integrated = 0;
  1550. return CMD_SUCCESS;
  1551. }
  1552. static int
  1553. write_config_integrated(void)
  1554. {
  1555. u_int i;
  1556. int ret;
  1557. char line[] = "write terminal\n";
  1558. FILE *fp;
  1559. char *integrate_sav = NULL;
  1560. integrate_sav = malloc (strlen (integrate_default) +
  1561. strlen (CONF_BACKUP_EXT) + 1);
  1562. strcpy (integrate_sav, integrate_default);
  1563. strcat (integrate_sav, CONF_BACKUP_EXT);
  1564. fprintf (stdout,"Building Configuration...\n");
  1565. /* Move current configuration file to backup config file. */
  1566. unlink (integrate_sav);
  1567. rename (integrate_default, integrate_sav);
  1568. free (integrate_sav);
  1569. fp = fopen (integrate_default, "w");
  1570. if (fp == NULL)
  1571. {
  1572. fprintf (stdout,"%% Can't open configuration file %s.\n",
  1573. integrate_default);
  1574. return CMD_SUCCESS;
  1575. }
  1576. for (i = 0; i < array_size(vtysh_client); i++)
  1577. ret = vtysh_client_config (&vtysh_client[i], line);
  1578. vtysh_config_dump (fp);
  1579. fclose (fp);
  1580. if (chmod (integrate_default, CONFIGFILE_MASK) != 0)
  1581. {
  1582. fprintf (stdout,"%% Can't chmod configuration file %s: %s (%d)\n",
  1583. integrate_default, safe_strerror(errno), errno);
  1584. return CMD_WARNING;
  1585. }
  1586. fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
  1587. fprintf (stdout,"[OK]\n");
  1588. return CMD_SUCCESS;
  1589. }
  1590. DEFUN (vtysh_write_memory,
  1591. vtysh_write_memory_cmd,
  1592. "write memory",
  1593. "Write running configuration to memory, network, or terminal\n"
  1594. "Write configuration to the file (same as write file)\n")
  1595. {
  1596. int ret = CMD_SUCCESS;
  1597. char line[] = "write memory\n";
  1598. u_int i;
  1599. /* If integrated Quagga.conf explicitely set. */
  1600. if (vtysh_writeconfig_integrated)
  1601. return write_config_integrated();
  1602. fprintf (stdout,"Building Configuration...\n");
  1603. for (i = 0; i < array_size(vtysh_client); i++)
  1604. ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
  1605. fprintf (stdout,"[OK]\n");
  1606. return ret;
  1607. }
  1608. ALIAS (vtysh_write_memory,
  1609. vtysh_copy_runningconfig_startupconfig_cmd,
  1610. "copy running-config startup-config",
  1611. "Copy from one file to another\n"
  1612. "Copy from current system configuration\n"
  1613. "Copy to startup configuration\n")
  1614. ALIAS (vtysh_write_memory,
  1615. vtysh_write_file_cmd,
  1616. "write file",
  1617. "Write running configuration to memory, network, or terminal\n"
  1618. "Write configuration to the file (same as write memory)\n")
  1619. ALIAS (vtysh_write_memory,
  1620. vtysh_write_cmd,
  1621. "write",
  1622. "Write running configuration to memory, network, or terminal\n")
  1623. ALIAS (vtysh_write_terminal,
  1624. vtysh_show_running_config_cmd,
  1625. "show running-config",
  1626. SHOW_STR
  1627. "Current operating configuration\n")
  1628. DEFUN (vtysh_terminal_length,
  1629. vtysh_terminal_length_cmd,
  1630. "terminal length <0-512>",
  1631. "Set terminal line parameters\n"
  1632. "Set number of lines on a screen\n"
  1633. "Number of lines on screen (0 for no pausing)\n")
  1634. {
  1635. int lines;
  1636. char *endptr = NULL;
  1637. char default_pager[10];
  1638. lines = strtol (argv[0], &endptr, 10);
  1639. if (lines < 0 || lines > 512 || *endptr != '\0')
  1640. {
  1641. vty_out (vty, "length is malformed%s", VTY_NEWLINE);
  1642. return CMD_WARNING;
  1643. }
  1644. if (vtysh_pager_name)
  1645. {
  1646. free (vtysh_pager_name);
  1647. vtysh_pager_name = NULL;
  1648. }
  1649. if (lines != 0)
  1650. {
  1651. snprintf(default_pager, 10, "more -%i", lines);
  1652. vtysh_pager_name = strdup (default_pager);
  1653. }
  1654. return CMD_SUCCESS;
  1655. }
  1656. DEFUN (vtysh_terminal_no_length,
  1657. vtysh_terminal_no_length_cmd,
  1658. "terminal no length",
  1659. "Set terminal line parameters\n"
  1660. NO_STR
  1661. "Set number of lines on a screen\n")
  1662. {
  1663. if (vtysh_pager_name)
  1664. {
  1665. free (vtysh_pager_name);
  1666. vtysh_pager_name = NULL;
  1667. }
  1668. vtysh_pager_init();
  1669. return CMD_SUCCESS;
  1670. }
  1671. DEFUN (vtysh_show_daemons,
  1672. vtysh_show_daemons_cmd,
  1673. "show daemons",
  1674. SHOW_STR
  1675. "Show list of running daemons\n")
  1676. {
  1677. u_int i;
  1678. for (i = 0; i < array_size(vtysh_client); i++)
  1679. if ( vtysh_client[i].fd >= 0 )
  1680. vty_out(vty, " %s", vtysh_client[i].name);
  1681. vty_out(vty, "%s", VTY_NEWLINE);
  1682. return CMD_SUCCESS;
  1683. }
  1684. /* Execute command in child process. */
  1685. static int
  1686. execute_command (const char *command, int argc, const char *arg1,
  1687. const char *arg2)
  1688. {
  1689. int ret;
  1690. pid_t pid;
  1691. int status;
  1692. /* Call fork(). */
  1693. pid = fork ();
  1694. if (pid < 0)
  1695. {
  1696. /* Failure of fork(). */
  1697. fprintf (stderr, "Can't fork: %s\n", safe_strerror (errno));
  1698. exit (1);
  1699. }
  1700. else if (pid == 0)
  1701. {
  1702. /* This is child process. */
  1703. switch (argc)
  1704. {
  1705. case 0:
  1706. ret = execlp (command, command, (const char *)NULL);
  1707. break;
  1708. case 1:
  1709. ret = execlp (command, command, arg1, (const char *)NULL);
  1710. break;
  1711. case 2:
  1712. ret = execlp (command, command, arg1, arg2, (const char *)NULL);
  1713. break;
  1714. }
  1715. /* When execlp suceed, this part is not executed. */
  1716. fprintf (stderr, "Can't execute %s: %s\n", command, safe_strerror (errno));
  1717. exit (1);
  1718. }
  1719. else
  1720. {
  1721. /* This is parent. */
  1722. execute_flag = 1;
  1723. ret = wait4 (pid, &status, 0, NULL);
  1724. execute_flag = 0;
  1725. }
  1726. return 0;
  1727. }
  1728. DEFUN (vtysh_ping,
  1729. vtysh_ping_cmd,
  1730. "ping WORD",
  1731. "Send echo messages\n"
  1732. "Ping destination address or hostname\n")
  1733. {
  1734. execute_command ("ping", 1, argv[0], NULL);
  1735. return CMD_SUCCESS;
  1736. }
  1737. ALIAS (vtysh_ping,
  1738. vtysh_ping_ip_cmd,
  1739. "ping ip WORD",
  1740. "Send echo messages\n"
  1741. "IP echo\n"
  1742. "Ping destination address or hostname\n")
  1743. DEFUN (vtysh_traceroute,
  1744. vtysh_traceroute_cmd,
  1745. "traceroute WORD",
  1746. "Trace route to destination\n"
  1747. "Trace route to destination address or hostname\n")
  1748. {
  1749. execute_command ("traceroute", 1, argv[0], NULL);
  1750. return CMD_SUCCESS;
  1751. }
  1752. ALIAS (vtysh_traceroute,
  1753. vtysh_traceroute_ip_cmd,
  1754. "traceroute ip WORD",
  1755. "Trace route to destination\n"
  1756. "IP trace\n"
  1757. "Trace route to destination address or hostname\n")
  1758. #ifdef HAVE_IPV6
  1759. DEFUN (vtysh_ping6,
  1760. vtysh_ping6_cmd,
  1761. "ping ipv6 WORD",
  1762. "Send echo messages\n"
  1763. "IPv6 echo\n"
  1764. "Ping destination address or hostname\n")
  1765. {
  1766. execute_command ("ping6", 1, argv[0], NULL);
  1767. return CMD_SUCCESS;
  1768. }
  1769. DEFUN (vtysh_traceroute6,
  1770. vtysh_traceroute6_cmd,
  1771. "traceroute ipv6 WORD",
  1772. "Trace route to destination\n"
  1773. "IPv6 trace\n"
  1774. "Trace route to destination address or hostname\n")
  1775. {
  1776. execute_command ("traceroute6", 1, argv[0], NULL);
  1777. return CMD_SUCCESS;
  1778. }
  1779. #endif
  1780. DEFUN (vtysh_telnet,
  1781. vtysh_telnet_cmd,
  1782. "telnet WORD",
  1783. "Open a telnet connection\n"
  1784. "IP address or hostname of a remote system\n")
  1785. {
  1786. execute_command ("telnet", 1, argv[0], NULL);
  1787. return CMD_SUCCESS;
  1788. }
  1789. DEFUN (vtysh_telnet_port,
  1790. vtysh_telnet_port_cmd,
  1791. "telnet WORD PORT",
  1792. "Open a telnet connection\n"
  1793. "IP address or hostname of a remote system\n"
  1794. "TCP Port number\n")
  1795. {
  1796. execute_command ("telnet", 2, argv[0], argv[1]);
  1797. return CMD_SUCCESS;
  1798. }
  1799. DEFUN (vtysh_ssh,
  1800. vtysh_ssh_cmd,
  1801. "ssh WORD",
  1802. "Open an ssh connection\n"
  1803. "[user@]host\n")
  1804. {
  1805. execute_command ("ssh", 1, argv[0], NULL);
  1806. return CMD_SUCCESS;
  1807. }
  1808. DEFUN (vtysh_start_shell,
  1809. vtysh_start_shell_cmd,
  1810. "start-shell",
  1811. "Start UNIX shell\n")
  1812. {
  1813. execute_command ("sh", 0, NULL, NULL);
  1814. return CMD_SUCCESS;
  1815. }
  1816. DEFUN (vtysh_start_bash,
  1817. vtysh_start_bash_cmd,
  1818. "start-shell bash",
  1819. "Start UNIX shell\n"
  1820. "Start bash\n")
  1821. {
  1822. execute_command ("bash", 0, NULL, NULL);
  1823. return CMD_SUCCESS;
  1824. }
  1825. DEFUN (vtysh_start_zsh,
  1826. vtysh_start_zsh_cmd,
  1827. "start-shell zsh",
  1828. "Start UNIX shell\n"
  1829. "Start Z shell\n")
  1830. {
  1831. execute_command ("zsh", 0, NULL, NULL);
  1832. return CMD_SUCCESS;
  1833. }
  1834. static void
  1835. vtysh_install_default (enum node_type node)
  1836. {
  1837. install_element (node, &config_list_cmd);
  1838. }
  1839. /* Making connection to protocol daemon. */
  1840. static int
  1841. vtysh_connect (struct vtysh_client *vclient)
  1842. {
  1843. int ret;
  1844. int sock, len;
  1845. struct sockaddr_un addr;
  1846. struct stat s_stat;
  1847. /* Stat socket to see if we have permission to access it. */
  1848. ret = stat (vclient->path, &s_stat);
  1849. if (ret < 0 && errno != ENOENT)
  1850. {
  1851. fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
  1852. vclient->path, safe_strerror(errno));
  1853. exit(1);
  1854. }
  1855. if (ret >= 0)
  1856. {
  1857. if (! S_ISSOCK(s_stat.st_mode))
  1858. {
  1859. fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
  1860. vclient->path);
  1861. exit (1);
  1862. }
  1863. }
  1864. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  1865. if (sock < 0)
  1866. {
  1867. #ifdef DEBUG
  1868. fprintf(stderr, "vtysh_connect(%s): socket = %s\n", vclient->path,
  1869. safe_strerror(errno));
  1870. #endif /* DEBUG */
  1871. return -1;
  1872. }
  1873. memset (&addr, 0, sizeof (struct sockaddr_un));
  1874. addr.sun_family = AF_UNIX;
  1875. strncpy (addr.sun_path, vclient->path, strlen (vclient->path));
  1876. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  1877. len = addr.sun_len = SUN_LEN(&addr);
  1878. #else
  1879. len = sizeof (addr.sun_family) + strlen (addr.sun_path);
  1880. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  1881. ret = connect (sock, (struct sockaddr *) &addr, len);
  1882. if (ret < 0)
  1883. {
  1884. #ifdef DEBUG
  1885. fprintf(stderr, "vtysh_connect(%s): connect = %s\n", vclient->path,
  1886. safe_strerror(errno));
  1887. #endif /* DEBUG */
  1888. close (sock);
  1889. return -1;
  1890. }
  1891. vclient->fd = sock;
  1892. return 0;
  1893. }
  1894. int
  1895. vtysh_connect_all(const char *daemon_name)
  1896. {
  1897. u_int i;
  1898. int rc = 0;
  1899. int matches = 0;
  1900. for (i = 0; i < array_size(vtysh_client); i++)
  1901. {
  1902. if (!daemon_name || !strcmp(daemon_name, vtysh_client[i].name))
  1903. {
  1904. matches++;
  1905. if (vtysh_connect(&vtysh_client[i]) == 0)
  1906. rc++;
  1907. /* We need direct access to ripd in vtysh_exit_ripd_only. */
  1908. if (vtysh_client[i].flag == VTYSH_RIPD)
  1909. ripd_client = &vtysh_client[i];
  1910. }
  1911. }
  1912. if (!matches)
  1913. fprintf(stderr, "Error: no daemons match name %s!\n", daemon_name);
  1914. return rc;
  1915. }
  1916. /* To disable readline's filename completion. */
  1917. static char *
  1918. vtysh_completion_entry_function (const char *ignore, int invoking_key)
  1919. {
  1920. return NULL;
  1921. }
  1922. void
  1923. vtysh_readline_init (void)
  1924. {
  1925. /* readline related settings. */
  1926. rl_bind_key ('?', (Function *) vtysh_rl_describe);
  1927. rl_completion_entry_function = vtysh_completion_entry_function;
  1928. rl_attempted_completion_function = (CPPFunction *)new_completion;
  1929. }
  1930. char *
  1931. vtysh_prompt (void)
  1932. {
  1933. static struct utsname names;
  1934. static char buf[100];
  1935. const char*hostname;
  1936. extern struct host host;
  1937. hostname = host.name;
  1938. if (!hostname)
  1939. {
  1940. if (!names.nodename[0])
  1941. uname (&names);
  1942. hostname = names.nodename;
  1943. }
  1944. snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
  1945. return buf;
  1946. }
  1947. void
  1948. vtysh_init_vty (void)
  1949. {
  1950. /* Make vty structure. */
  1951. vty = vty_new ();
  1952. vty->type = VTY_SHELL;
  1953. vty->node = VIEW_NODE;
  1954. /* Initialize commands. */
  1955. cmd_init (0);
  1956. /* Install nodes. */
  1957. install_node (&bgp_node, NULL);
  1958. install_node (&rip_node, NULL);
  1959. install_node (&interface_node, NULL);
  1960. install_node (&rmap_node, NULL);
  1961. install_node (&zebra_node, NULL);
  1962. install_node (&bgp_vpnv4_node, NULL);
  1963. install_node (&bgp_ipv4_node, NULL);
  1964. install_node (&bgp_ipv4m_node, NULL);
  1965. /* #ifdef HAVE_IPV6 */
  1966. install_node (&bgp_ipv6_node, NULL);
  1967. install_node (&bgp_ipv6m_node, NULL);
  1968. /* #endif */
  1969. install_node (&ospf_node, NULL);
  1970. /* #ifdef HAVE_IPV6 */
  1971. install_node (&ripng_node, NULL);
  1972. install_node (&ospf6_node, NULL);
  1973. /* #endif */
  1974. install_node (&babel_node, NULL);
  1975. install_node (&keychain_node, NULL);
  1976. install_node (&keychain_key_node, NULL);
  1977. install_node (&isis_node, NULL);
  1978. install_node (&vty_node, NULL);
  1979. vtysh_install_default (VIEW_NODE);
  1980. vtysh_install_default (ENABLE_NODE);
  1981. vtysh_install_default (CONFIG_NODE);
  1982. vtysh_install_default (BGP_NODE);
  1983. vtysh_install_default (RIP_NODE);
  1984. vtysh_install_default (INTERFACE_NODE);
  1985. vtysh_install_default (RMAP_NODE);
  1986. vtysh_install_default (ZEBRA_NODE);
  1987. vtysh_install_default (BGP_VPNV4_NODE);
  1988. vtysh_install_default (BGP_IPV4_NODE);
  1989. vtysh_install_default (BGP_IPV4M_NODE);
  1990. vtysh_install_default (BGP_IPV6_NODE);
  1991. vtysh_install_default (BGP_IPV6M_NODE);
  1992. vtysh_install_default (OSPF_NODE);
  1993. vtysh_install_default (RIPNG_NODE);
  1994. vtysh_install_default (OSPF6_NODE);
  1995. vtysh_install_default (BABEL_NODE);
  1996. vtysh_install_default (ISIS_NODE);
  1997. vtysh_install_default (KEYCHAIN_NODE);
  1998. vtysh_install_default (KEYCHAIN_KEY_NODE);
  1999. vtysh_install_default (VTY_NODE);
  2000. install_element (VIEW_NODE, &vtysh_enable_cmd);
  2001. install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
  2002. install_element (ENABLE_NODE, &vtysh_disable_cmd);
  2003. /* "exit" command. */
  2004. install_element (VIEW_NODE, &vtysh_exit_all_cmd);
  2005. install_element (VIEW_NODE, &vtysh_quit_all_cmd);
  2006. install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
  2007. /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
  2008. install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
  2009. install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
  2010. install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
  2011. install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
  2012. install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
  2013. install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
  2014. install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
  2015. install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
  2016. install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
  2017. install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
  2018. install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
  2019. install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
  2020. install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
  2021. install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
  2022. install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
  2023. install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
  2024. install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
  2025. install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
  2026. install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
  2027. install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
  2028. install_element (BGP_IPV6M_NODE, &vtysh_exit_bgpd_cmd);
  2029. install_element (BGP_IPV6M_NODE, &vtysh_quit_bgpd_cmd);
  2030. install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
  2031. install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
  2032. install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
  2033. install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
  2034. install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
  2035. install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
  2036. install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
  2037. install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
  2038. install_element (VTY_NODE, &vtysh_exit_line_vty_cmd);
  2039. install_element (VTY_NODE, &vtysh_quit_line_vty_cmd);
  2040. /* "end" command. */
  2041. install_element (CONFIG_NODE, &vtysh_end_all_cmd);
  2042. install_element (ENABLE_NODE, &vtysh_end_all_cmd);
  2043. install_element (RIP_NODE, &vtysh_end_all_cmd);
  2044. install_element (RIPNG_NODE, &vtysh_end_all_cmd);
  2045. install_element (OSPF_NODE, &vtysh_end_all_cmd);
  2046. install_element (OSPF6_NODE, &vtysh_end_all_cmd);
  2047. install_element (BABEL_NODE, &vtysh_end_all_cmd);
  2048. install_element (BGP_NODE, &vtysh_end_all_cmd);
  2049. install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
  2050. install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
  2051. install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
  2052. install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
  2053. install_element (BGP_IPV6M_NODE, &vtysh_end_all_cmd);
  2054. install_element (ISIS_NODE, &vtysh_end_all_cmd);
  2055. install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
  2056. install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
  2057. install_element (RMAP_NODE, &vtysh_end_all_cmd);
  2058. install_element (VTY_NODE, &vtysh_end_all_cmd);
  2059. install_element (INTERFACE_NODE, &interface_desc_cmd);
  2060. install_element (INTERFACE_NODE, &no_interface_desc_cmd);
  2061. install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
  2062. install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
  2063. install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
  2064. install_element (CONFIG_NODE, &router_rip_cmd);
  2065. #ifdef HAVE_IPV6
  2066. install_element (CONFIG_NODE, &router_ripng_cmd);
  2067. #endif
  2068. install_element (CONFIG_NODE, &router_ospf_cmd);
  2069. #ifdef HAVE_IPV6
  2070. install_element (CONFIG_NODE, &router_ospf6_cmd);
  2071. #endif
  2072. install_element (CONFIG_NODE, &router_babel_cmd);
  2073. install_element (CONFIG_NODE, &router_isis_cmd);
  2074. install_element (CONFIG_NODE, &router_bgp_cmd);
  2075. install_element (CONFIG_NODE, &router_bgp_view_cmd);
  2076. install_element (BGP_NODE, &address_family_vpnv4_cmd);
  2077. install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
  2078. install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
  2079. install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
  2080. #ifdef HAVE_IPV6
  2081. install_element (BGP_NODE, &address_family_ipv6_cmd);
  2082. install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
  2083. #endif
  2084. install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
  2085. install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
  2086. install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
  2087. install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
  2088. install_element (BGP_IPV6M_NODE, &exit_address_family_cmd);
  2089. install_element (CONFIG_NODE, &key_chain_cmd);
  2090. install_element (CONFIG_NODE, &route_map_cmd);
  2091. install_element (CONFIG_NODE, &vtysh_line_vty_cmd);
  2092. install_element (KEYCHAIN_NODE, &key_cmd);
  2093. install_element (KEYCHAIN_NODE, &key_chain_cmd);
  2094. install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
  2095. install_element (CONFIG_NODE, &vtysh_interface_cmd);
  2096. install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
  2097. install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
  2098. install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
  2099. install_element (ENABLE_NODE, &vtysh_write_file_cmd);
  2100. install_element (ENABLE_NODE, &vtysh_write_cmd);
  2101. /* "write terminal" command. */
  2102. install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
  2103. install_element (CONFIG_NODE, &vtysh_integrated_config_cmd);
  2104. install_element (CONFIG_NODE, &no_vtysh_integrated_config_cmd);
  2105. /* "write memory" command. */
  2106. install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
  2107. install_element (VIEW_NODE, &vtysh_terminal_length_cmd);
  2108. install_element (ENABLE_NODE, &vtysh_terminal_length_cmd);
  2109. install_element (VIEW_NODE, &vtysh_terminal_no_length_cmd);
  2110. install_element (ENABLE_NODE, &vtysh_terminal_no_length_cmd);
  2111. install_element (VIEW_NODE, &vtysh_show_daemons_cmd);
  2112. install_element (ENABLE_NODE, &vtysh_show_daemons_cmd);
  2113. install_element (VIEW_NODE, &vtysh_ping_cmd);
  2114. install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
  2115. install_element (VIEW_NODE, &vtysh_traceroute_cmd);
  2116. install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
  2117. #ifdef HAVE_IPV6
  2118. install_element (VIEW_NODE, &vtysh_ping6_cmd);
  2119. install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
  2120. #endif
  2121. install_element (VIEW_NODE, &vtysh_telnet_cmd);
  2122. install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
  2123. install_element (VIEW_NODE, &vtysh_ssh_cmd);
  2124. install_element (ENABLE_NODE, &vtysh_ping_cmd);
  2125. install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
  2126. install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
  2127. install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
  2128. #ifdef HAVE_IPV6
  2129. install_element (ENABLE_NODE, &vtysh_ping6_cmd);
  2130. install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
  2131. #endif
  2132. install_element (ENABLE_NODE, &vtysh_telnet_cmd);
  2133. install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
  2134. install_element (ENABLE_NODE, &vtysh_ssh_cmd);
  2135. install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
  2136. install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
  2137. install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
  2138. install_element (VIEW_NODE, &vtysh_show_memory_cmd);
  2139. install_element (ENABLE_NODE, &vtysh_show_memory_cmd);
  2140. /* Logging */
  2141. install_element (ENABLE_NODE, &vtysh_show_logging_cmd);
  2142. install_element (VIEW_NODE, &vtysh_show_logging_cmd);
  2143. install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
  2144. install_element (CONFIG_NODE, &vtysh_log_stdout_level_cmd);
  2145. install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
  2146. install_element (CONFIG_NODE, &vtysh_log_file_cmd);
  2147. install_element (CONFIG_NODE, &vtysh_log_file_level_cmd);
  2148. install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
  2149. install_element (CONFIG_NODE, &no_vtysh_log_file_level_cmd);
  2150. install_element (CONFIG_NODE, &vtysh_log_monitor_cmd);
  2151. install_element (CONFIG_NODE, &vtysh_log_monitor_level_cmd);
  2152. install_element (CONFIG_NODE, &no_vtysh_log_monitor_cmd);
  2153. install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
  2154. install_element (CONFIG_NODE, &vtysh_log_syslog_level_cmd);
  2155. install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
  2156. install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
  2157. install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
  2158. install_element (CONFIG_NODE, &vtysh_log_facility_cmd);
  2159. install_element (CONFIG_NODE, &no_vtysh_log_facility_cmd);
  2160. install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
  2161. install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
  2162. install_element (CONFIG_NODE, &vtysh_log_timestamp_precision_cmd);
  2163. install_element (CONFIG_NODE, &no_vtysh_log_timestamp_precision_cmd);
  2164. install_element (CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
  2165. install_element (CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
  2166. install_element (CONFIG_NODE, &vtysh_password_cmd);
  2167. install_element (CONFIG_NODE, &vtysh_password_text_cmd);
  2168. install_element (CONFIG_NODE, &vtysh_enable_password_cmd);
  2169. install_element (CONFIG_NODE, &vtysh_enable_password_text_cmd);
  2170. install_element (CONFIG_NODE, &no_vtysh_enable_password_cmd);
  2171. }