vtysh.c 56 KB

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