vty.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. /*
  2. * Virtual terminal [aka TeletYpe] interface routine.
  3. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "linklist.h"
  24. #include "thread.h"
  25. #include "buffer.h"
  26. #include <lib/version.h>
  27. #include "command.h"
  28. #include "sockunion.h"
  29. #include "memory.h"
  30. #include "str.h"
  31. #include "log.h"
  32. #include "prefix.h"
  33. #include "filter.h"
  34. #include "vty.h"
  35. #include "privs.h"
  36. #include "network.h"
  37. #include <arpa/telnet.h>
  38. /* Vty events */
  39. enum event
  40. {
  41. VTY_SERV,
  42. VTY_READ,
  43. VTY_WRITE,
  44. VTY_TIMEOUT_RESET,
  45. #ifdef VTYSH
  46. VTYSH_SERV,
  47. VTYSH_READ,
  48. VTYSH_WRITE
  49. #endif /* VTYSH */
  50. };
  51. static void vty_event (enum event, int, struct vty *);
  52. /* Extern host structure from command.c */
  53. extern struct host host;
  54. /* Vector which store each vty structure. */
  55. static vector vtyvec;
  56. /* Vty timeout value. */
  57. static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
  58. /* Vty access-class command */
  59. static char *vty_accesslist_name = NULL;
  60. /* Vty access-calss for IPv6. */
  61. static char *vty_ipv6_accesslist_name = NULL;
  62. /* VTY server thread. */
  63. static vector Vvty_serv_thread;
  64. /* Current directory. */
  65. char *vty_cwd = NULL;
  66. /* Configure lock. */
  67. static int vty_config;
  68. /* Login password check. */
  69. static int no_password_check = 0;
  70. /* Restrict unauthenticated logins? */
  71. static const u_char restricted_mode_default = 0;
  72. static u_char restricted_mode = 0;
  73. /* Integrated configuration file path */
  74. char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
  75. /* VTY standard output function. */
  76. int
  77. vty_out (struct vty *vty, const char *format, ...)
  78. {
  79. va_list args;
  80. int len = 0;
  81. int size = 1024;
  82. char buf[1024];
  83. char *p = NULL;
  84. if (vty_shell (vty))
  85. {
  86. va_start (args, format);
  87. vprintf (format, args);
  88. va_end (args);
  89. }
  90. else
  91. {
  92. /* Try to write to initial buffer. */
  93. va_start (args, format);
  94. len = vsnprintf (buf, sizeof buf, format, args);
  95. va_end (args);
  96. /* Initial buffer is not enough. */
  97. if (len < 0 || len >= size)
  98. {
  99. while (1)
  100. {
  101. if (len > -1)
  102. size = len + 1;
  103. else
  104. size = size * 2;
  105. p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
  106. if (! p)
  107. return -1;
  108. va_start (args, format);
  109. len = vsnprintf (p, size, format, args);
  110. va_end (args);
  111. if (len > -1 && len < size)
  112. break;
  113. }
  114. }
  115. /* When initial buffer is enough to store all output. */
  116. if (! p)
  117. p = buf;
  118. /* Pointer p must point out buffer. */
  119. buffer_put (vty->obuf, (u_char *) p, len);
  120. /* If p is not different with buf, it is allocated buffer. */
  121. if (p != buf)
  122. XFREE (MTYPE_VTY_OUT_BUF, p);
  123. }
  124. return len;
  125. }
  126. static int
  127. vty_log_out (struct vty *vty, const char *level, const char *proto_str,
  128. const char *format, struct timestamp_control *ctl, va_list va)
  129. {
  130. int ret;
  131. int len;
  132. char buf[1024];
  133. if (!ctl->already_rendered)
  134. {
  135. ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
  136. ctl->already_rendered = 1;
  137. }
  138. if (ctl->len+1 >= sizeof(buf))
  139. return -1;
  140. memcpy(buf, ctl->buf, len = ctl->len);
  141. buf[len++] = ' ';
  142. buf[len] = '\0';
  143. if (level)
  144. ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
  145. else
  146. ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
  147. if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
  148. return -1;
  149. if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
  150. ((size_t)((len += ret)+2) > sizeof(buf)))
  151. return -1;
  152. buf[len++] = '\r';
  153. buf[len++] = '\n';
  154. if (write(vty->fd, buf, len) < 0)
  155. {
  156. if (ERRNO_IO_RETRY(errno))
  157. /* Kernel buffer is full, probably too much debugging output, so just
  158. drop the data and ignore. */
  159. return -1;
  160. /* Fatal I/O error. */
  161. vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
  162. zlog_warn("%s: write failed to vty client fd %d, closing: %s",
  163. __func__, vty->fd, safe_strerror(errno));
  164. buffer_reset(vty->obuf);
  165. /* cannot call vty_close, because a parent routine may still try
  166. to access the vty struct */
  167. vty->status = VTY_CLOSE;
  168. shutdown(vty->fd, SHUT_RDWR);
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. /* Output current time to the vty. */
  174. void
  175. vty_time_print (struct vty *vty, int cr)
  176. {
  177. char buf [25];
  178. if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
  179. {
  180. zlog (NULL, LOG_INFO, "quagga_timestamp error");
  181. return;
  182. }
  183. if (cr)
  184. vty_out (vty, "%s\n", buf);
  185. else
  186. vty_out (vty, "%s ", buf);
  187. return;
  188. }
  189. /* Say hello to vty interface. */
  190. void
  191. vty_hello (struct vty *vty)
  192. {
  193. if (host.motdfile)
  194. {
  195. FILE *f;
  196. char buf[4096];
  197. f = fopen (host.motdfile, "r");
  198. if (f)
  199. {
  200. while (fgets (buf, sizeof (buf), f))
  201. {
  202. char *s;
  203. /* work backwards to ignore trailling isspace() */
  204. for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
  205. s--);
  206. *s = '\0';
  207. vty_out (vty, "%s%s", buf, VTY_NEWLINE);
  208. }
  209. fclose (f);
  210. }
  211. else
  212. vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
  213. }
  214. else if (host.motd)
  215. vty_out (vty, "%s", host.motd);
  216. }
  217. /* Put out prompt and wait input from user. */
  218. static void
  219. vty_prompt (struct vty *vty)
  220. {
  221. struct utsname names;
  222. const char*hostname;
  223. if (vty->type == VTY_TERM)
  224. {
  225. hostname = host.name;
  226. if (!hostname)
  227. {
  228. uname (&names);
  229. hostname = names.nodename;
  230. }
  231. vty_out (vty, cmd_prompt (vty->node), hostname);
  232. }
  233. }
  234. /* Send WILL TELOPT_ECHO to remote server. */
  235. static void
  236. vty_will_echo (struct vty *vty)
  237. {
  238. unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
  239. vty_out (vty, "%s", cmd);
  240. }
  241. /* Make suppress Go-Ahead telnet option. */
  242. static void
  243. vty_will_suppress_go_ahead (struct vty *vty)
  244. {
  245. unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
  246. vty_out (vty, "%s", cmd);
  247. }
  248. /* Make don't use linemode over telnet. */
  249. static void
  250. vty_dont_linemode (struct vty *vty)
  251. {
  252. unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
  253. vty_out (vty, "%s", cmd);
  254. }
  255. /* Use window size. */
  256. static void
  257. vty_do_window_size (struct vty *vty)
  258. {
  259. unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
  260. vty_out (vty, "%s", cmd);
  261. }
  262. #if 0 /* Currently not used. */
  263. /* Make don't use lflow vty interface. */
  264. static void
  265. vty_dont_lflow_ahead (struct vty *vty)
  266. {
  267. unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
  268. vty_out (vty, "%s", cmd);
  269. }
  270. #endif /* 0 */
  271. /* Allocate new vty struct. */
  272. struct vty *
  273. vty_new ()
  274. {
  275. struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
  276. new->obuf = buffer_new(0); /* Use default buffer size. */
  277. new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
  278. new->max = VTY_BUFSIZ;
  279. return new;
  280. }
  281. /* Authentication of vty */
  282. static void
  283. vty_auth (struct vty *vty, char *buf)
  284. {
  285. char *passwd = NULL;
  286. enum node_type next_node = 0;
  287. int fail;
  288. char *crypt (const char *, const char *);
  289. switch (vty->node)
  290. {
  291. case AUTH_NODE:
  292. if (host.encrypt)
  293. passwd = host.password_encrypt;
  294. else
  295. passwd = host.password;
  296. if (host.advanced)
  297. next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
  298. else
  299. next_node = VIEW_NODE;
  300. break;
  301. case AUTH_ENABLE_NODE:
  302. if (host.encrypt)
  303. passwd = host.enable_encrypt;
  304. else
  305. passwd = host.enable;
  306. next_node = ENABLE_NODE;
  307. break;
  308. }
  309. if (passwd)
  310. {
  311. if (host.encrypt)
  312. fail = strcmp (crypt(buf, passwd), passwd);
  313. else
  314. fail = strcmp (buf, passwd);
  315. }
  316. else
  317. fail = 1;
  318. if (! fail)
  319. {
  320. vty->fail = 0;
  321. vty->node = next_node; /* Success ! */
  322. }
  323. else
  324. {
  325. vty->fail++;
  326. if (vty->fail >= 3)
  327. {
  328. if (vty->node == AUTH_NODE)
  329. {
  330. vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
  331. vty->status = VTY_CLOSE;
  332. }
  333. else
  334. {
  335. /* AUTH_ENABLE_NODE */
  336. vty->fail = 0;
  337. vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
  338. vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
  339. }
  340. }
  341. }
  342. }
  343. /* Command execution over the vty interface. */
  344. static int
  345. vty_command (struct vty *vty, char *buf)
  346. {
  347. int ret;
  348. vector vline;
  349. const char *protocolname;
  350. /* Split readline string up into the vector */
  351. vline = cmd_make_strvec (buf);
  352. if (vline == NULL)
  353. return CMD_SUCCESS;
  354. #ifdef CONSUMED_TIME_CHECK
  355. {
  356. RUSAGE_T before;
  357. RUSAGE_T after;
  358. unsigned long realtime, cputime;
  359. GETRUSAGE(&before);
  360. #endif /* CONSUMED_TIME_CHECK */
  361. ret = cmd_execute_command (vline, vty, NULL, 0);
  362. /* Get the name of the protocol if any */
  363. if (zlog_default)
  364. protocolname = zlog_proto_names[zlog_default->protocol];
  365. else
  366. protocolname = zlog_proto_names[ZLOG_NONE];
  367. #ifdef CONSUMED_TIME_CHECK
  368. GETRUSAGE(&after);
  369. if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
  370. CONSUMED_TIME_CHECK)
  371. /* Warn about CPU hog that must be fixed. */
  372. zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
  373. realtime/1000, cputime/1000, buf);
  374. }
  375. #endif /* CONSUMED_TIME_CHECK */
  376. if (ret != CMD_SUCCESS)
  377. switch (ret)
  378. {
  379. case CMD_WARNING:
  380. if (vty->type == VTY_FILE)
  381. vty_out (vty, "Warning...%s", VTY_NEWLINE);
  382. break;
  383. case CMD_ERR_AMBIGUOUS:
  384. vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
  385. break;
  386. case CMD_ERR_NO_MATCH:
  387. vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
  388. break;
  389. case CMD_ERR_INCOMPLETE:
  390. vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
  391. break;
  392. }
  393. cmd_free_strvec (vline);
  394. return ret;
  395. }
  396. static const char telnet_backward_char = 0x08;
  397. static const char telnet_space_char = ' ';
  398. /* Basic function to write buffer to vty. */
  399. static void
  400. vty_write (struct vty *vty, const char *buf, size_t nbytes)
  401. {
  402. if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
  403. return;
  404. /* Should we do buffering here ? And make vty_flush (vty) ? */
  405. buffer_put (vty->obuf, buf, nbytes);
  406. }
  407. /* Ensure length of input buffer. Is buffer is short, double it. */
  408. static void
  409. vty_ensure (struct vty *vty, int length)
  410. {
  411. if (vty->max <= length)
  412. {
  413. vty->max *= 2;
  414. vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
  415. }
  416. }
  417. /* Basic function to insert character into vty. */
  418. static void
  419. vty_self_insert (struct vty *vty, char c)
  420. {
  421. int i;
  422. int length;
  423. vty_ensure (vty, vty->length + 1);
  424. length = vty->length - vty->cp;
  425. memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
  426. vty->buf[vty->cp] = c;
  427. vty_write (vty, &vty->buf[vty->cp], length + 1);
  428. for (i = 0; i < length; i++)
  429. vty_write (vty, &telnet_backward_char, 1);
  430. vty->cp++;
  431. vty->length++;
  432. }
  433. /* Self insert character 'c' in overwrite mode. */
  434. static void
  435. vty_self_insert_overwrite (struct vty *vty, char c)
  436. {
  437. vty_ensure (vty, vty->length + 1);
  438. vty->buf[vty->cp++] = c;
  439. if (vty->cp > vty->length)
  440. vty->length++;
  441. if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
  442. return;
  443. vty_write (vty, &c, 1);
  444. }
  445. /* Insert a word into vty interface with overwrite mode. */
  446. static void
  447. vty_insert_word_overwrite (struct vty *vty, char *str)
  448. {
  449. int len = strlen (str);
  450. vty_write (vty, str, len);
  451. strcpy (&vty->buf[vty->cp], str);
  452. vty->cp += len;
  453. vty->length = vty->cp;
  454. }
  455. /* Forward character. */
  456. static void
  457. vty_forward_char (struct vty *vty)
  458. {
  459. if (vty->cp < vty->length)
  460. {
  461. vty_write (vty, &vty->buf[vty->cp], 1);
  462. vty->cp++;
  463. }
  464. }
  465. /* Backward character. */
  466. static void
  467. vty_backward_char (struct vty *vty)
  468. {
  469. if (vty->cp > 0)
  470. {
  471. vty->cp--;
  472. vty_write (vty, &telnet_backward_char, 1);
  473. }
  474. }
  475. /* Move to the beginning of the line. */
  476. static void
  477. vty_beginning_of_line (struct vty *vty)
  478. {
  479. while (vty->cp)
  480. vty_backward_char (vty);
  481. }
  482. /* Move to the end of the line. */
  483. static void
  484. vty_end_of_line (struct vty *vty)
  485. {
  486. while (vty->cp < vty->length)
  487. vty_forward_char (vty);
  488. }
  489. static void vty_kill_line_from_beginning (struct vty *);
  490. static void vty_redraw_line (struct vty *);
  491. /* Print command line history. This function is called from
  492. vty_next_line and vty_previous_line. */
  493. static void
  494. vty_history_print (struct vty *vty)
  495. {
  496. int length;
  497. vty_kill_line_from_beginning (vty);
  498. /* Get previous line from history buffer */
  499. length = strlen (vty->hist[vty->hp]);
  500. memcpy (vty->buf, vty->hist[vty->hp], length);
  501. vty->cp = vty->length = length;
  502. /* Redraw current line */
  503. vty_redraw_line (vty);
  504. }
  505. /* Show next command line history. */
  506. static void
  507. vty_next_line (struct vty *vty)
  508. {
  509. int try_index;
  510. if (vty->hp == vty->hindex)
  511. return;
  512. /* Try is there history exist or not. */
  513. try_index = vty->hp;
  514. if (try_index == (VTY_MAXHIST - 1))
  515. try_index = 0;
  516. else
  517. try_index++;
  518. /* If there is not history return. */
  519. if (vty->hist[try_index] == NULL)
  520. return;
  521. else
  522. vty->hp = try_index;
  523. vty_history_print (vty);
  524. }
  525. /* Show previous command line history. */
  526. static void
  527. vty_previous_line (struct vty *vty)
  528. {
  529. int try_index;
  530. try_index = vty->hp;
  531. if (try_index == 0)
  532. try_index = VTY_MAXHIST - 1;
  533. else
  534. try_index--;
  535. if (vty->hist[try_index] == NULL)
  536. return;
  537. else
  538. vty->hp = try_index;
  539. vty_history_print (vty);
  540. }
  541. /* This function redraw all of the command line character. */
  542. static void
  543. vty_redraw_line (struct vty *vty)
  544. {
  545. vty_write (vty, vty->buf, vty->length);
  546. vty->cp = vty->length;
  547. }
  548. /* Forward word. */
  549. static void
  550. vty_forward_word (struct vty *vty)
  551. {
  552. while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
  553. vty_forward_char (vty);
  554. while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
  555. vty_forward_char (vty);
  556. }
  557. /* Backward word without skipping training space. */
  558. static void
  559. vty_backward_pure_word (struct vty *vty)
  560. {
  561. while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
  562. vty_backward_char (vty);
  563. }
  564. /* Backward word. */
  565. static void
  566. vty_backward_word (struct vty *vty)
  567. {
  568. while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
  569. vty_backward_char (vty);
  570. while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
  571. vty_backward_char (vty);
  572. }
  573. /* When '^D' is typed at the beginning of the line we move to the down
  574. level. */
  575. static void
  576. vty_down_level (struct vty *vty)
  577. {
  578. vty_out (vty, "%s", VTY_NEWLINE);
  579. (*config_exit_cmd.func)(NULL, vty, 0, NULL);
  580. vty_prompt (vty);
  581. vty->cp = 0;
  582. }
  583. /* When '^Z' is received from vty, move down to the enable mode. */
  584. static void
  585. vty_end_config (struct vty *vty)
  586. {
  587. vty_out (vty, "%s", VTY_NEWLINE);
  588. switch (vty->node)
  589. {
  590. case VIEW_NODE:
  591. case ENABLE_NODE:
  592. case RESTRICTED_NODE:
  593. /* Nothing to do. */
  594. break;
  595. case CONFIG_NODE:
  596. case INTERFACE_NODE:
  597. case ZEBRA_NODE:
  598. case RIP_NODE:
  599. case RIPNG_NODE:
  600. case BABEL_NODE:
  601. case BGP_NODE:
  602. case BGP_VPNV4_NODE:
  603. case BGP_IPV4_NODE:
  604. case BGP_IPV4M_NODE:
  605. case BGP_IPV6_NODE:
  606. case BGP_IPV6M_NODE:
  607. case RMAP_NODE:
  608. case OSPF_NODE:
  609. case OSPF6_NODE:
  610. case ISIS_NODE:
  611. case KEYCHAIN_NODE:
  612. case KEYCHAIN_KEY_NODE:
  613. case MASC_NODE:
  614. case VTY_NODE:
  615. vty_config_unlock (vty);
  616. vty->node = ENABLE_NODE;
  617. break;
  618. default:
  619. /* Unknown node, we have to ignore it. */
  620. break;
  621. }
  622. vty_prompt (vty);
  623. vty->cp = 0;
  624. }
  625. /* Delete a charcter at the current point. */
  626. static void
  627. vty_delete_char (struct vty *vty)
  628. {
  629. int i;
  630. int size;
  631. if (vty->length == 0)
  632. {
  633. vty_down_level (vty);
  634. return;
  635. }
  636. if (vty->cp == vty->length)
  637. return; /* completion need here? */
  638. size = vty->length - vty->cp;
  639. vty->length--;
  640. memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
  641. vty->buf[vty->length] = '\0';
  642. if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
  643. return;
  644. vty_write (vty, &vty->buf[vty->cp], size - 1);
  645. vty_write (vty, &telnet_space_char, 1);
  646. for (i = 0; i < size; i++)
  647. vty_write (vty, &telnet_backward_char, 1);
  648. }
  649. /* Delete a character before the point. */
  650. static void
  651. vty_delete_backward_char (struct vty *vty)
  652. {
  653. if (vty->cp == 0)
  654. return;
  655. vty_backward_char (vty);
  656. vty_delete_char (vty);
  657. }
  658. /* Kill rest of line from current point. */
  659. static void
  660. vty_kill_line (struct vty *vty)
  661. {
  662. int i;
  663. int size;
  664. size = vty->length - vty->cp;
  665. if (size == 0)
  666. return;
  667. for (i = 0; i < size; i++)
  668. vty_write (vty, &telnet_space_char, 1);
  669. for (i = 0; i < size; i++)
  670. vty_write (vty, &telnet_backward_char, 1);
  671. memset (&vty->buf[vty->cp], 0, size);
  672. vty->length = vty->cp;
  673. }
  674. /* Kill line from the beginning. */
  675. static void
  676. vty_kill_line_from_beginning (struct vty *vty)
  677. {
  678. vty_beginning_of_line (vty);
  679. vty_kill_line (vty);
  680. }
  681. /* Delete a word before the point. */
  682. static void
  683. vty_forward_kill_word (struct vty *vty)
  684. {
  685. while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
  686. vty_delete_char (vty);
  687. while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
  688. vty_delete_char (vty);
  689. }
  690. /* Delete a word before the point. */
  691. static void
  692. vty_backward_kill_word (struct vty *vty)
  693. {
  694. while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
  695. vty_delete_backward_char (vty);
  696. while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
  697. vty_delete_backward_char (vty);
  698. }
  699. /* Transpose chars before or at the point. */
  700. static void
  701. vty_transpose_chars (struct vty *vty)
  702. {
  703. char c1, c2;
  704. /* If length is short or point is near by the beginning of line then
  705. return. */
  706. if (vty->length < 2 || vty->cp < 1)
  707. return;
  708. /* In case of point is located at the end of the line. */
  709. if (vty->cp == vty->length)
  710. {
  711. c1 = vty->buf[vty->cp - 1];
  712. c2 = vty->buf[vty->cp - 2];
  713. vty_backward_char (vty);
  714. vty_backward_char (vty);
  715. vty_self_insert_overwrite (vty, c1);
  716. vty_self_insert_overwrite (vty, c2);
  717. }
  718. else
  719. {
  720. c1 = vty->buf[vty->cp];
  721. c2 = vty->buf[vty->cp - 1];
  722. vty_backward_char (vty);
  723. vty_self_insert_overwrite (vty, c1);
  724. vty_self_insert_overwrite (vty, c2);
  725. }
  726. }
  727. /* Do completion at vty interface. */
  728. static void
  729. vty_complete_command (struct vty *vty)
  730. {
  731. int i;
  732. int ret;
  733. char **matched = NULL;
  734. vector vline;
  735. if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
  736. return;
  737. vline = cmd_make_strvec (vty->buf);
  738. if (vline == NULL)
  739. return;
  740. /* In case of 'help \t'. */
  741. if (isspace ((int) vty->buf[vty->length - 1]))
  742. vector_set (vline, '\0');
  743. matched = cmd_complete_command (vline, vty, &ret);
  744. cmd_free_strvec (vline);
  745. vty_out (vty, "%s", VTY_NEWLINE);
  746. switch (ret)
  747. {
  748. case CMD_ERR_AMBIGUOUS:
  749. vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
  750. vty_prompt (vty);
  751. vty_redraw_line (vty);
  752. break;
  753. case CMD_ERR_NO_MATCH:
  754. /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
  755. vty_prompt (vty);
  756. vty_redraw_line (vty);
  757. break;
  758. case CMD_COMPLETE_FULL_MATCH:
  759. vty_prompt (vty);
  760. vty_redraw_line (vty);
  761. vty_backward_pure_word (vty);
  762. vty_insert_word_overwrite (vty, matched[0]);
  763. vty_self_insert (vty, ' ');
  764. XFREE (MTYPE_TMP, matched[0]);
  765. break;
  766. case CMD_COMPLETE_MATCH:
  767. vty_prompt (vty);
  768. vty_redraw_line (vty);
  769. vty_backward_pure_word (vty);
  770. vty_insert_word_overwrite (vty, matched[0]);
  771. XFREE (MTYPE_TMP, matched[0]);
  772. vector_only_index_free (matched);
  773. return;
  774. break;
  775. case CMD_COMPLETE_LIST_MATCH:
  776. for (i = 0; matched[i] != NULL; i++)
  777. {
  778. if (i != 0 && ((i % 6) == 0))
  779. vty_out (vty, "%s", VTY_NEWLINE);
  780. vty_out (vty, "%-10s ", matched[i]);
  781. XFREE (MTYPE_TMP, matched[i]);
  782. }
  783. vty_out (vty, "%s", VTY_NEWLINE);
  784. vty_prompt (vty);
  785. vty_redraw_line (vty);
  786. break;
  787. case CMD_ERR_NOTHING_TODO:
  788. vty_prompt (vty);
  789. vty_redraw_line (vty);
  790. break;
  791. default:
  792. break;
  793. }
  794. if (matched)
  795. vector_only_index_free (matched);
  796. }
  797. static void
  798. vty_describe_fold (struct vty *vty, int cmd_width,
  799. unsigned int desc_width, struct cmd_token *token)
  800. {
  801. char *buf;
  802. const char *cmd, *p;
  803. int pos;
  804. cmd = token->cmd[0] == '.' ? token->cmd + 1 : token->cmd;
  805. if (desc_width <= 0)
  806. {
  807. vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
  808. return;
  809. }
  810. buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
  811. for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
  812. {
  813. for (pos = desc_width; pos > 0; pos--)
  814. if (*(p + pos) == ' ')
  815. break;
  816. if (pos == 0)
  817. break;
  818. strncpy (buf, p, pos);
  819. buf[pos] = '\0';
  820. vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
  821. cmd = "";
  822. }
  823. vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
  824. XFREE (MTYPE_TMP, buf);
  825. }
  826. /* Describe matched command function. */
  827. static void
  828. vty_describe_command (struct vty *vty)
  829. {
  830. int ret;
  831. vector vline;
  832. vector describe;
  833. unsigned int i, width, desc_width;
  834. struct cmd_token *token, *token_cr = NULL;
  835. vline = cmd_make_strvec (vty->buf);
  836. /* In case of '> ?'. */
  837. if (vline == NULL)
  838. {
  839. vline = vector_init (1);
  840. vector_set (vline, '\0');
  841. }
  842. else
  843. if (isspace ((int) vty->buf[vty->length - 1]))
  844. vector_set (vline, '\0');
  845. describe = cmd_describe_command (vline, vty, &ret);
  846. vty_out (vty, "%s", VTY_NEWLINE);
  847. /* Ambiguous error. */
  848. switch (ret)
  849. {
  850. case CMD_ERR_AMBIGUOUS:
  851. vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
  852. goto out;
  853. break;
  854. case CMD_ERR_NO_MATCH:
  855. vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
  856. goto out;
  857. break;
  858. }
  859. /* Get width of command string. */
  860. width = 0;
  861. for (i = 0; i < vector_active (describe); i++)
  862. if ((token = vector_slot (describe, i)) != NULL)
  863. {
  864. unsigned int len;
  865. if (token->cmd[0] == '\0')
  866. continue;
  867. len = strlen (token->cmd);
  868. if (token->cmd[0] == '.')
  869. len--;
  870. if (width < len)
  871. width = len;
  872. }
  873. /* Get width of description string. */
  874. desc_width = vty->width - (width + 6);
  875. /* Print out description. */
  876. for (i = 0; i < vector_active (describe); i++)
  877. if ((token = vector_slot (describe, i)) != NULL)
  878. {
  879. if (token->cmd[0] == '\0')
  880. continue;
  881. if (strcmp (token->cmd, command_cr) == 0)
  882. {
  883. token_cr = token;
  884. continue;
  885. }
  886. if (!token->desc)
  887. vty_out (vty, " %-s%s",
  888. token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
  889. VTY_NEWLINE);
  890. else if (desc_width >= strlen (token->desc))
  891. vty_out (vty, " %-*s %s%s", width,
  892. token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
  893. token->desc, VTY_NEWLINE);
  894. else
  895. vty_describe_fold (vty, width, desc_width, token);
  896. #if 0
  897. vty_out (vty, " %-*s %s%s", width
  898. desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
  899. desc->str ? desc->str : "", VTY_NEWLINE);
  900. #endif /* 0 */
  901. }
  902. if ((token = token_cr))
  903. {
  904. if (!token->desc)
  905. vty_out (vty, " %-s%s",
  906. token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
  907. VTY_NEWLINE);
  908. else if (desc_width >= strlen (token->desc))
  909. vty_out (vty, " %-*s %s%s", width,
  910. token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
  911. token->desc, VTY_NEWLINE);
  912. else
  913. vty_describe_fold (vty, width, desc_width, token);
  914. }
  915. out:
  916. cmd_free_strvec (vline);
  917. if (describe)
  918. vector_free (describe);
  919. vty_prompt (vty);
  920. vty_redraw_line (vty);
  921. }
  922. static void
  923. vty_clear_buf (struct vty *vty)
  924. {
  925. memset (vty->buf, 0, vty->max);
  926. }
  927. /* ^C stop current input and do not add command line to the history. */
  928. static void
  929. vty_stop_input (struct vty *vty)
  930. {
  931. vty->cp = vty->length = 0;
  932. vty_clear_buf (vty);
  933. vty_out (vty, "%s", VTY_NEWLINE);
  934. switch (vty->node)
  935. {
  936. case VIEW_NODE:
  937. case ENABLE_NODE:
  938. case RESTRICTED_NODE:
  939. /* Nothing to do. */
  940. break;
  941. case CONFIG_NODE:
  942. case INTERFACE_NODE:
  943. case ZEBRA_NODE:
  944. case RIP_NODE:
  945. case RIPNG_NODE:
  946. case BABEL_NODE:
  947. case BGP_NODE:
  948. case RMAP_NODE:
  949. case OSPF_NODE:
  950. case OSPF6_NODE:
  951. case ISIS_NODE:
  952. case KEYCHAIN_NODE:
  953. case KEYCHAIN_KEY_NODE:
  954. case MASC_NODE:
  955. case VTY_NODE:
  956. vty_config_unlock (vty);
  957. vty->node = ENABLE_NODE;
  958. break;
  959. default:
  960. /* Unknown node, we have to ignore it. */
  961. break;
  962. }
  963. vty_prompt (vty);
  964. /* Set history pointer to the latest one. */
  965. vty->hp = vty->hindex;
  966. }
  967. /* Add current command line to the history buffer. */
  968. static void
  969. vty_hist_add (struct vty *vty)
  970. {
  971. int index;
  972. if (vty->length == 0)
  973. return;
  974. index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
  975. /* Ignore the same string as previous one. */
  976. if (vty->hist[index])
  977. if (strcmp (vty->buf, vty->hist[index]) == 0)
  978. {
  979. vty->hp = vty->hindex;
  980. return;
  981. }
  982. /* Insert history entry. */
  983. if (vty->hist[vty->hindex])
  984. XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
  985. vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
  986. /* History index rotation. */
  987. vty->hindex++;
  988. if (vty->hindex == VTY_MAXHIST)
  989. vty->hindex = 0;
  990. vty->hp = vty->hindex;
  991. }
  992. /* #define TELNET_OPTION_DEBUG */
  993. /* Get telnet window size. */
  994. static int
  995. vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
  996. {
  997. #ifdef TELNET_OPTION_DEBUG
  998. int i;
  999. for (i = 0; i < nbytes; i++)
  1000. {
  1001. switch (buf[i])
  1002. {
  1003. case IAC:
  1004. vty_out (vty, "IAC ");
  1005. break;
  1006. case WILL:
  1007. vty_out (vty, "WILL ");
  1008. break;
  1009. case WONT:
  1010. vty_out (vty, "WONT ");
  1011. break;
  1012. case DO:
  1013. vty_out (vty, "DO ");
  1014. break;
  1015. case DONT:
  1016. vty_out (vty, "DONT ");
  1017. break;
  1018. case SB:
  1019. vty_out (vty, "SB ");
  1020. break;
  1021. case SE:
  1022. vty_out (vty, "SE ");
  1023. break;
  1024. case TELOPT_ECHO:
  1025. vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
  1026. break;
  1027. case TELOPT_SGA:
  1028. vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
  1029. break;
  1030. case TELOPT_NAWS:
  1031. vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
  1032. break;
  1033. default:
  1034. vty_out (vty, "%x ", buf[i]);
  1035. break;
  1036. }
  1037. }
  1038. vty_out (vty, "%s", VTY_NEWLINE);
  1039. #endif /* TELNET_OPTION_DEBUG */
  1040. switch (buf[0])
  1041. {
  1042. case SB:
  1043. vty->sb_len = 0;
  1044. vty->iac_sb_in_progress = 1;
  1045. return 0;
  1046. break;
  1047. case SE:
  1048. {
  1049. if (!vty->iac_sb_in_progress)
  1050. return 0;
  1051. if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
  1052. {
  1053. vty->iac_sb_in_progress = 0;
  1054. return 0;
  1055. }
  1056. switch (vty->sb_buf[0])
  1057. {
  1058. case TELOPT_NAWS:
  1059. if (vty->sb_len != TELNET_NAWS_SB_LEN)
  1060. zlog_warn("RFC 1073 violation detected: telnet NAWS option "
  1061. "should send %d characters, but we received %lu",
  1062. TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
  1063. else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
  1064. zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
  1065. "too small to handle the telnet NAWS option",
  1066. (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
  1067. else
  1068. {
  1069. vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
  1070. vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
  1071. #ifdef TELNET_OPTION_DEBUG
  1072. vty_out(vty, "TELNET NAWS window size negotiation completed: "
  1073. "width %d, height %d%s",
  1074. vty->width, vty->height, VTY_NEWLINE);
  1075. #endif
  1076. }
  1077. break;
  1078. }
  1079. vty->iac_sb_in_progress = 0;
  1080. return 0;
  1081. break;
  1082. }
  1083. default:
  1084. break;
  1085. }
  1086. return 1;
  1087. }
  1088. /* Execute current command line. */
  1089. static int
  1090. vty_execute (struct vty *vty)
  1091. {
  1092. int ret;
  1093. ret = CMD_SUCCESS;
  1094. switch (vty->node)
  1095. {
  1096. case AUTH_NODE:
  1097. case AUTH_ENABLE_NODE:
  1098. vty_auth (vty, vty->buf);
  1099. break;
  1100. default:
  1101. ret = vty_command (vty, vty->buf);
  1102. if (vty->type == VTY_TERM)
  1103. vty_hist_add (vty);
  1104. break;
  1105. }
  1106. /* Clear command line buffer. */
  1107. vty->cp = vty->length = 0;
  1108. vty_clear_buf (vty);
  1109. if (vty->status != VTY_CLOSE )
  1110. vty_prompt (vty);
  1111. return ret;
  1112. }
  1113. #define CONTROL(X) ((X) - '@')
  1114. #define VTY_NORMAL 0
  1115. #define VTY_PRE_ESCAPE 1
  1116. #define VTY_ESCAPE 2
  1117. /* Escape character command map. */
  1118. static void
  1119. vty_escape_map (unsigned char c, struct vty *vty)
  1120. {
  1121. switch (c)
  1122. {
  1123. case ('A'):
  1124. vty_previous_line (vty);
  1125. break;
  1126. case ('B'):
  1127. vty_next_line (vty);
  1128. break;
  1129. case ('C'):
  1130. vty_forward_char (vty);
  1131. break;
  1132. case ('D'):
  1133. vty_backward_char (vty);
  1134. break;
  1135. default:
  1136. break;
  1137. }
  1138. /* Go back to normal mode. */
  1139. vty->escape = VTY_NORMAL;
  1140. }
  1141. /* Quit print out to the buffer. */
  1142. static void
  1143. vty_buffer_reset (struct vty *vty)
  1144. {
  1145. buffer_reset (vty->obuf);
  1146. vty_prompt (vty);
  1147. vty_redraw_line (vty);
  1148. }
  1149. /* Read data via vty socket. */
  1150. static int
  1151. vty_read (struct thread *thread)
  1152. {
  1153. int i;
  1154. int nbytes;
  1155. unsigned char buf[VTY_READ_BUFSIZ];
  1156. int vty_sock = THREAD_FD (thread);
  1157. struct vty *vty = THREAD_ARG (thread);
  1158. vty->t_read = NULL;
  1159. /* Read raw data from socket */
  1160. if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
  1161. {
  1162. if (nbytes < 0)
  1163. {
  1164. if (ERRNO_IO_RETRY(errno))
  1165. {
  1166. vty_event (VTY_READ, vty_sock, vty);
  1167. return 0;
  1168. }
  1169. vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
  1170. zlog_warn("%s: read error on vty client fd %d, closing: %s",
  1171. __func__, vty->fd, safe_strerror(errno));
  1172. }
  1173. buffer_reset(vty->obuf);
  1174. vty->status = VTY_CLOSE;
  1175. }
  1176. for (i = 0; i < nbytes; i++)
  1177. {
  1178. if (buf[i] == IAC)
  1179. {
  1180. if (!vty->iac)
  1181. {
  1182. vty->iac = 1;
  1183. continue;
  1184. }
  1185. else
  1186. {
  1187. vty->iac = 0;
  1188. }
  1189. }
  1190. if (vty->iac_sb_in_progress && !vty->iac)
  1191. {
  1192. if (vty->sb_len < sizeof(vty->sb_buf))
  1193. vty->sb_buf[vty->sb_len] = buf[i];
  1194. vty->sb_len++;
  1195. continue;
  1196. }
  1197. if (vty->iac)
  1198. {
  1199. /* In case of telnet command */
  1200. int ret = 0;
  1201. ret = vty_telnet_option (vty, buf + i, nbytes - i);
  1202. vty->iac = 0;
  1203. i += ret;
  1204. continue;
  1205. }
  1206. if (vty->status == VTY_MORE)
  1207. {
  1208. switch (buf[i])
  1209. {
  1210. case CONTROL('C'):
  1211. case 'q':
  1212. case 'Q':
  1213. vty_buffer_reset (vty);
  1214. break;
  1215. #if 0 /* More line does not work for "show ip bgp". */
  1216. case '\n':
  1217. case '\r':
  1218. vty->status = VTY_MORELINE;
  1219. break;
  1220. #endif
  1221. default:
  1222. break;
  1223. }
  1224. continue;
  1225. }
  1226. /* Escape character. */
  1227. if (vty->escape == VTY_ESCAPE)
  1228. {
  1229. vty_escape_map (buf[i], vty);
  1230. continue;
  1231. }
  1232. /* Pre-escape status. */
  1233. if (vty->escape == VTY_PRE_ESCAPE)
  1234. {
  1235. switch (buf[i])
  1236. {
  1237. case '[':
  1238. vty->escape = VTY_ESCAPE;
  1239. break;
  1240. case 'b':
  1241. vty_backward_word (vty);
  1242. vty->escape = VTY_NORMAL;
  1243. break;
  1244. case 'f':
  1245. vty_forward_word (vty);
  1246. vty->escape = VTY_NORMAL;
  1247. break;
  1248. case 'd':
  1249. vty_forward_kill_word (vty);
  1250. vty->escape = VTY_NORMAL;
  1251. break;
  1252. case CONTROL('H'):
  1253. case 0x7f:
  1254. vty_backward_kill_word (vty);
  1255. vty->escape = VTY_NORMAL;
  1256. break;
  1257. default:
  1258. vty->escape = VTY_NORMAL;
  1259. break;
  1260. }
  1261. continue;
  1262. }
  1263. switch (buf[i])
  1264. {
  1265. case CONTROL('A'):
  1266. vty_beginning_of_line (vty);
  1267. break;
  1268. case CONTROL('B'):
  1269. vty_backward_char (vty);
  1270. break;
  1271. case CONTROL('C'):
  1272. vty_stop_input (vty);
  1273. break;
  1274. case CONTROL('D'):
  1275. vty_delete_char (vty);
  1276. break;
  1277. case CONTROL('E'):
  1278. vty_end_of_line (vty);
  1279. break;
  1280. case CONTROL('F'):
  1281. vty_forward_char (vty);
  1282. break;
  1283. case CONTROL('H'):
  1284. case 0x7f:
  1285. vty_delete_backward_char (vty);
  1286. break;
  1287. case CONTROL('K'):
  1288. vty_kill_line (vty);
  1289. break;
  1290. case CONTROL('N'):
  1291. vty_next_line (vty);
  1292. break;
  1293. case CONTROL('P'):
  1294. vty_previous_line (vty);
  1295. break;
  1296. case CONTROL('T'):
  1297. vty_transpose_chars (vty);
  1298. break;
  1299. case CONTROL('U'):
  1300. vty_kill_line_from_beginning (vty);
  1301. break;
  1302. case CONTROL('W'):
  1303. vty_backward_kill_word (vty);
  1304. break;
  1305. case CONTROL('Z'):
  1306. vty_end_config (vty);
  1307. break;
  1308. case '\n':
  1309. case '\r':
  1310. vty_out (vty, "%s", VTY_NEWLINE);
  1311. vty_execute (vty);
  1312. break;
  1313. case '\t':
  1314. vty_complete_command (vty);
  1315. break;
  1316. case '?':
  1317. if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
  1318. vty_self_insert (vty, buf[i]);
  1319. else
  1320. vty_describe_command (vty);
  1321. break;
  1322. case '\033':
  1323. if (i + 1 < nbytes && buf[i + 1] == '[')
  1324. {
  1325. vty->escape = VTY_ESCAPE;
  1326. i++;
  1327. }
  1328. else
  1329. vty->escape = VTY_PRE_ESCAPE;
  1330. break;
  1331. default:
  1332. if (buf[i] > 31 && buf[i] < 127)
  1333. vty_self_insert (vty, buf[i]);
  1334. break;
  1335. }
  1336. }
  1337. /* Check status. */
  1338. if (vty->status == VTY_CLOSE)
  1339. vty_close (vty);
  1340. else
  1341. {
  1342. vty_event (VTY_WRITE, vty_sock, vty);
  1343. vty_event (VTY_READ, vty_sock, vty);
  1344. }
  1345. return 0;
  1346. }
  1347. /* Flush buffer to the vty. */
  1348. static int
  1349. vty_flush (struct thread *thread)
  1350. {
  1351. int erase;
  1352. buffer_status_t flushrc;
  1353. int vty_sock = THREAD_FD (thread);
  1354. struct vty *vty = THREAD_ARG (thread);
  1355. vty->t_write = NULL;
  1356. /* Tempolary disable read thread. */
  1357. if ((vty->lines == 0) && vty->t_read)
  1358. {
  1359. thread_cancel (vty->t_read);
  1360. vty->t_read = NULL;
  1361. }
  1362. /* Function execution continue. */
  1363. erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
  1364. /* N.B. if width is 0, that means we don't know the window size. */
  1365. if ((vty->lines == 0) || (vty->width == 0))
  1366. flushrc = buffer_flush_available(vty->obuf, vty->fd);
  1367. else if (vty->status == VTY_MORELINE)
  1368. flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
  1369. 1, erase, 0);
  1370. else
  1371. flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
  1372. vty->lines >= 0 ? vty->lines :
  1373. vty->height,
  1374. erase, 0);
  1375. switch (flushrc)
  1376. {
  1377. case BUFFER_ERROR:
  1378. vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
  1379. zlog_warn("buffer_flush failed on vty client fd %d, closing",
  1380. vty->fd);
  1381. buffer_reset(vty->obuf);
  1382. vty_close(vty);
  1383. return 0;
  1384. case BUFFER_EMPTY:
  1385. if (vty->status == VTY_CLOSE)
  1386. vty_close (vty);
  1387. else
  1388. {
  1389. vty->status = VTY_NORMAL;
  1390. if (vty->lines == 0)
  1391. vty_event (VTY_READ, vty_sock, vty);
  1392. }
  1393. break;
  1394. case BUFFER_PENDING:
  1395. /* There is more data waiting to be written. */
  1396. vty->status = VTY_MORE;
  1397. if (vty->lines == 0)
  1398. vty_event (VTY_WRITE, vty_sock, vty);
  1399. break;
  1400. }
  1401. return 0;
  1402. }
  1403. /* Create new vty structure. */
  1404. static struct vty *
  1405. vty_create (int vty_sock, union sockunion *su)
  1406. {
  1407. char buf[SU_ADDRSTRLEN];
  1408. struct vty *vty;
  1409. sockunion2str(su, buf, SU_ADDRSTRLEN);
  1410. /* Allocate new vty structure and set up default values. */
  1411. vty = vty_new ();
  1412. vty->fd = vty_sock;
  1413. vty->type = VTY_TERM;
  1414. strcpy (vty->address, buf);
  1415. if (no_password_check)
  1416. {
  1417. if (restricted_mode)
  1418. vty->node = RESTRICTED_NODE;
  1419. else if (host.advanced)
  1420. vty->node = ENABLE_NODE;
  1421. else
  1422. vty->node = VIEW_NODE;
  1423. }
  1424. else
  1425. vty->node = AUTH_NODE;
  1426. vty->fail = 0;
  1427. vty->cp = 0;
  1428. vty_clear_buf (vty);
  1429. vty->length = 0;
  1430. memset (vty->hist, 0, sizeof (vty->hist));
  1431. vty->hp = 0;
  1432. vty->hindex = 0;
  1433. vector_set_index (vtyvec, vty_sock, vty);
  1434. vty->status = VTY_NORMAL;
  1435. vty->v_timeout = vty_timeout_val;
  1436. if (host.lines >= 0)
  1437. vty->lines = host.lines;
  1438. else
  1439. vty->lines = -1;
  1440. vty->iac = 0;
  1441. vty->iac_sb_in_progress = 0;
  1442. vty->sb_len = 0;
  1443. if (! no_password_check)
  1444. {
  1445. /* Vty is not available if password isn't set. */
  1446. if (host.password == NULL && host.password_encrypt == NULL)
  1447. {
  1448. vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
  1449. vty->status = VTY_CLOSE;
  1450. vty_close (vty);
  1451. return NULL;
  1452. }
  1453. }
  1454. /* Say hello to the world. */
  1455. vty_hello (vty);
  1456. if (! no_password_check)
  1457. vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
  1458. /* Setting up terminal. */
  1459. vty_will_echo (vty);
  1460. vty_will_suppress_go_ahead (vty);
  1461. vty_dont_linemode (vty);
  1462. vty_do_window_size (vty);
  1463. /* vty_dont_lflow_ahead (vty); */
  1464. vty_prompt (vty);
  1465. /* Add read/write thread. */
  1466. vty_event (VTY_WRITE, vty_sock, vty);
  1467. vty_event (VTY_READ, vty_sock, vty);
  1468. return vty;
  1469. }
  1470. /* Accept connection from the network. */
  1471. static int
  1472. vty_accept (struct thread *thread)
  1473. {
  1474. int vty_sock;
  1475. union sockunion su;
  1476. int ret;
  1477. unsigned int on;
  1478. int accept_sock;
  1479. struct prefix *p = NULL;
  1480. struct access_list *acl = NULL;
  1481. char buf[SU_ADDRSTRLEN];
  1482. accept_sock = THREAD_FD (thread);
  1483. /* We continue hearing vty socket. */
  1484. vty_event (VTY_SERV, accept_sock, NULL);
  1485. memset (&su, 0, sizeof (union sockunion));
  1486. /* We can handle IPv4 or IPv6 socket. */
  1487. vty_sock = sockunion_accept (accept_sock, &su);
  1488. if (vty_sock < 0)
  1489. {
  1490. zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
  1491. return -1;
  1492. }
  1493. set_nonblocking(vty_sock);
  1494. p = sockunion2hostprefix (&su);
  1495. /* VTY's accesslist apply. */
  1496. if (p->family == AF_INET && vty_accesslist_name)
  1497. {
  1498. if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
  1499. (access_list_apply (acl, p) == FILTER_DENY))
  1500. {
  1501. zlog (NULL, LOG_INFO, "Vty connection refused from %s",
  1502. sockunion2str (&su, buf, SU_ADDRSTRLEN));
  1503. close (vty_sock);
  1504. /* continue accepting connections */
  1505. vty_event (VTY_SERV, accept_sock, NULL);
  1506. prefix_free (p);
  1507. return 0;
  1508. }
  1509. }
  1510. #ifdef HAVE_IPV6
  1511. /* VTY's ipv6 accesslist apply. */
  1512. if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
  1513. {
  1514. if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
  1515. (access_list_apply (acl, p) == FILTER_DENY))
  1516. {
  1517. zlog (NULL, LOG_INFO, "Vty connection refused from %s",
  1518. sockunion2str (&su, buf, SU_ADDRSTRLEN));
  1519. close (vty_sock);
  1520. /* continue accepting connections */
  1521. vty_event (VTY_SERV, accept_sock, NULL);
  1522. prefix_free (p);
  1523. return 0;
  1524. }
  1525. }
  1526. #endif /* HAVE_IPV6 */
  1527. prefix_free (p);
  1528. on = 1;
  1529. ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
  1530. (char *) &on, sizeof (on));
  1531. if (ret < 0)
  1532. zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
  1533. safe_strerror (errno));
  1534. zlog (NULL, LOG_INFO, "Vty connection from %s",
  1535. sockunion2str (&su, buf, SU_ADDRSTRLEN));
  1536. vty_create (vty_sock, &su);
  1537. return 0;
  1538. }
  1539. #if defined(HAVE_IPV6) && !defined(NRL)
  1540. static void
  1541. vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
  1542. {
  1543. int ret;
  1544. struct addrinfo req;
  1545. struct addrinfo *ainfo;
  1546. struct addrinfo *ainfo_save;
  1547. int sock;
  1548. char port_str[BUFSIZ];
  1549. memset (&req, 0, sizeof (struct addrinfo));
  1550. req.ai_flags = AI_PASSIVE;
  1551. req.ai_family = AF_UNSPEC;
  1552. req.ai_socktype = SOCK_STREAM;
  1553. sprintf (port_str, "%d", port);
  1554. port_str[sizeof (port_str) - 1] = '\0';
  1555. ret = getaddrinfo (hostname, port_str, &req, &ainfo);
  1556. if (ret != 0)
  1557. {
  1558. fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
  1559. exit (1);
  1560. }
  1561. ainfo_save = ainfo;
  1562. do
  1563. {
  1564. if (ainfo->ai_family != AF_INET
  1565. #ifdef HAVE_IPV6
  1566. && ainfo->ai_family != AF_INET6
  1567. #endif /* HAVE_IPV6 */
  1568. )
  1569. continue;
  1570. sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
  1571. if (sock < 0)
  1572. continue;
  1573. sockopt_v6only (ainfo->ai_family, sock);
  1574. sockopt_reuseaddr (sock);
  1575. sockopt_reuseport (sock);
  1576. ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
  1577. if (ret < 0)
  1578. {
  1579. close (sock); /* Avoid sd leak. */
  1580. continue;
  1581. }
  1582. ret = listen (sock, 3);
  1583. if (ret < 0)
  1584. {
  1585. close (sock); /* Avoid sd leak. */
  1586. continue;
  1587. }
  1588. vty_event (VTY_SERV, sock, NULL);
  1589. }
  1590. while ((ainfo = ainfo->ai_next) != NULL);
  1591. freeaddrinfo (ainfo_save);
  1592. }
  1593. #else /* HAVE_IPV6 && ! NRL */
  1594. /* Make vty server socket. */
  1595. static void
  1596. vty_serv_sock_family (const char* addr, unsigned short port, int family)
  1597. {
  1598. int ret;
  1599. union sockunion su;
  1600. int accept_sock;
  1601. void* naddr=NULL;
  1602. memset (&su, 0, sizeof (union sockunion));
  1603. su.sa.sa_family = family;
  1604. if(addr)
  1605. switch(family)
  1606. {
  1607. case AF_INET:
  1608. naddr=&su.sin.sin_addr;
  1609. break;
  1610. #ifdef HAVE_IPV6
  1611. case AF_INET6:
  1612. naddr=&su.sin6.sin6_addr;
  1613. break;
  1614. #endif
  1615. }
  1616. if(naddr)
  1617. switch(inet_pton(family,addr,naddr))
  1618. {
  1619. case -1:
  1620. zlog_err("bad address %s",addr);
  1621. naddr=NULL;
  1622. break;
  1623. case 0:
  1624. zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
  1625. naddr=NULL;
  1626. }
  1627. /* Make new socket. */
  1628. accept_sock = sockunion_stream_socket (&su);
  1629. if (accept_sock < 0)
  1630. return;
  1631. /* This is server, so reuse address. */
  1632. sockopt_reuseaddr (accept_sock);
  1633. sockopt_reuseport (accept_sock);
  1634. /* Bind socket to universal address and given port. */
  1635. ret = sockunion_bind (accept_sock, &su, port, naddr);
  1636. if (ret < 0)
  1637. {
  1638. zlog_warn("can't bind socket");
  1639. close (accept_sock); /* Avoid sd leak. */
  1640. return;
  1641. }
  1642. /* Listen socket under queue 3. */
  1643. ret = listen (accept_sock, 3);
  1644. if (ret < 0)
  1645. {
  1646. zlog (NULL, LOG_WARNING, "can't listen socket");
  1647. close (accept_sock); /* Avoid sd leak. */
  1648. return;
  1649. }
  1650. /* Add vty server event. */
  1651. vty_event (VTY_SERV, accept_sock, NULL);
  1652. }
  1653. #endif /* HAVE_IPV6 && ! NRL */
  1654. #ifdef VTYSH
  1655. /* For sockaddr_un. */
  1656. #include <sys/un.h>
  1657. /* VTY shell UNIX domain socket. */
  1658. static void
  1659. vty_serv_un (const char *path)
  1660. {
  1661. int ret;
  1662. int sock, len;
  1663. struct sockaddr_un serv;
  1664. mode_t old_mask;
  1665. struct zprivs_ids_t ids;
  1666. /* First of all, unlink existing socket */
  1667. unlink (path);
  1668. /* Set umask */
  1669. old_mask = umask (0007);
  1670. /* Make UNIX domain socket. */
  1671. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  1672. if (sock < 0)
  1673. {
  1674. zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
  1675. return;
  1676. }
  1677. /* Make server socket. */
  1678. memset (&serv, 0, sizeof (struct sockaddr_un));
  1679. serv.sun_family = AF_UNIX;
  1680. strncpy (serv.sun_path, path, strlen (path));
  1681. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  1682. len = serv.sun_len = SUN_LEN(&serv);
  1683. #else
  1684. len = sizeof (serv.sun_family) + strlen (serv.sun_path);
  1685. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  1686. ret = bind (sock, (struct sockaddr *) &serv, len);
  1687. if (ret < 0)
  1688. {
  1689. zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
  1690. close (sock); /* Avoid sd leak. */
  1691. return;
  1692. }
  1693. ret = listen (sock, 5);
  1694. if (ret < 0)
  1695. {
  1696. zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
  1697. close (sock); /* Avoid sd leak. */
  1698. return;
  1699. }
  1700. umask (old_mask);
  1701. zprivs_get_ids(&ids);
  1702. if (ids.gid_vty > 0)
  1703. {
  1704. /* set group of socket */
  1705. if ( chown (path, -1, ids.gid_vty) )
  1706. {
  1707. zlog_err ("vty_serv_un: could chown socket, %s",
  1708. safe_strerror (errno) );
  1709. }
  1710. }
  1711. vty_event (VTYSH_SERV, sock, NULL);
  1712. }
  1713. /* #define VTYSH_DEBUG 1 */
  1714. static int
  1715. vtysh_accept (struct thread *thread)
  1716. {
  1717. int accept_sock;
  1718. int sock;
  1719. int client_len;
  1720. struct sockaddr_un client;
  1721. struct vty *vty;
  1722. accept_sock = THREAD_FD (thread);
  1723. vty_event (VTYSH_SERV, accept_sock, NULL);
  1724. memset (&client, 0, sizeof (struct sockaddr_un));
  1725. client_len = sizeof (struct sockaddr_un);
  1726. sock = accept (accept_sock, (struct sockaddr *) &client,
  1727. (socklen_t *) &client_len);
  1728. if (sock < 0)
  1729. {
  1730. zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
  1731. return -1;
  1732. }
  1733. if (set_nonblocking(sock) < 0)
  1734. {
  1735. zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
  1736. " %s, closing", sock, safe_strerror (errno));
  1737. close (sock);
  1738. return -1;
  1739. }
  1740. #ifdef VTYSH_DEBUG
  1741. printf ("VTY shell accept\n");
  1742. #endif /* VTYSH_DEBUG */
  1743. vty = vty_new ();
  1744. vty->fd = sock;
  1745. vty->type = VTY_SHELL_SERV;
  1746. vty->node = VIEW_NODE;
  1747. vty_event (VTYSH_READ, sock, vty);
  1748. return 0;
  1749. }
  1750. static int
  1751. vtysh_flush(struct vty *vty)
  1752. {
  1753. switch (buffer_flush_available(vty->obuf, vty->fd))
  1754. {
  1755. case BUFFER_PENDING:
  1756. vty_event(VTYSH_WRITE, vty->fd, vty);
  1757. break;
  1758. case BUFFER_ERROR:
  1759. vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
  1760. zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
  1761. buffer_reset(vty->obuf);
  1762. vty_close(vty);
  1763. return -1;
  1764. break;
  1765. case BUFFER_EMPTY:
  1766. break;
  1767. }
  1768. return 0;
  1769. }
  1770. static int
  1771. vtysh_read (struct thread *thread)
  1772. {
  1773. int ret;
  1774. int sock;
  1775. int nbytes;
  1776. struct vty *vty;
  1777. unsigned char buf[VTY_READ_BUFSIZ];
  1778. unsigned char *p;
  1779. u_char header[4] = {0, 0, 0, 0};
  1780. sock = THREAD_FD (thread);
  1781. vty = THREAD_ARG (thread);
  1782. vty->t_read = NULL;
  1783. if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
  1784. {
  1785. if (nbytes < 0)
  1786. {
  1787. if (ERRNO_IO_RETRY(errno))
  1788. {
  1789. vty_event (VTYSH_READ, sock, vty);
  1790. return 0;
  1791. }
  1792. vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
  1793. zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
  1794. __func__, sock, safe_strerror(errno));
  1795. }
  1796. buffer_reset(vty->obuf);
  1797. vty_close (vty);
  1798. #ifdef VTYSH_DEBUG
  1799. printf ("close vtysh\n");
  1800. #endif /* VTYSH_DEBUG */
  1801. return 0;
  1802. }
  1803. #ifdef VTYSH_DEBUG
  1804. printf ("line: %.*s\n", nbytes, buf);
  1805. #endif /* VTYSH_DEBUG */
  1806. for (p = buf; p < buf+nbytes; p++)
  1807. {
  1808. vty_ensure(vty, vty->length+1);
  1809. vty->buf[vty->length++] = *p;
  1810. if (*p == '\0')
  1811. {
  1812. /* Pass this line to parser. */
  1813. ret = vty_execute (vty);
  1814. /* Note that vty_execute clears the command buffer and resets
  1815. vty->length to 0. */
  1816. /* Return result. */
  1817. #ifdef VTYSH_DEBUG
  1818. printf ("result: %d\n", ret);
  1819. printf ("vtysh node: %d\n", vty->node);
  1820. #endif /* VTYSH_DEBUG */
  1821. header[3] = ret;
  1822. buffer_put(vty->obuf, header, 4);
  1823. if (!vty->t_write && (vtysh_flush(vty) < 0))
  1824. /* Try to flush results; exit if a write error occurs. */
  1825. return 0;
  1826. }
  1827. }
  1828. vty_event (VTYSH_READ, sock, vty);
  1829. return 0;
  1830. }
  1831. static int
  1832. vtysh_write (struct thread *thread)
  1833. {
  1834. struct vty *vty = THREAD_ARG (thread);
  1835. vty->t_write = NULL;
  1836. vtysh_flush(vty);
  1837. return 0;
  1838. }
  1839. #endif /* VTYSH */
  1840. /* Determine address family to bind. */
  1841. void
  1842. vty_serv_sock (const char *addr, unsigned short port, const char *path)
  1843. {
  1844. /* If port is set to 0, do not listen on TCP/IP at all! */
  1845. if (port)
  1846. {
  1847. #ifdef HAVE_IPV6
  1848. #ifdef NRL
  1849. vty_serv_sock_family (addr, port, AF_INET);
  1850. vty_serv_sock_family (addr, port, AF_INET6);
  1851. #else /* ! NRL */
  1852. vty_serv_sock_addrinfo (addr, port);
  1853. #endif /* NRL*/
  1854. #else /* ! HAVE_IPV6 */
  1855. vty_serv_sock_family (addr,port, AF_INET);
  1856. #endif /* HAVE_IPV6 */
  1857. }
  1858. #ifdef VTYSH
  1859. vty_serv_un (path);
  1860. #endif /* VTYSH */
  1861. }
  1862. /* Close vty interface. Warning: call this only from functions that
  1863. will be careful not to access the vty afterwards (since it has
  1864. now been freed). This is safest from top-level functions (called
  1865. directly by the thread dispatcher). */
  1866. void
  1867. vty_close (struct vty *vty)
  1868. {
  1869. int i;
  1870. /* Cancel threads.*/
  1871. if (vty->t_read)
  1872. thread_cancel (vty->t_read);
  1873. if (vty->t_write)
  1874. thread_cancel (vty->t_write);
  1875. if (vty->t_timeout)
  1876. thread_cancel (vty->t_timeout);
  1877. /* Flush buffer. */
  1878. buffer_flush_all (vty->obuf, vty->fd);
  1879. /* Free input buffer. */
  1880. buffer_free (vty->obuf);
  1881. /* Free command history. */
  1882. for (i = 0; i < VTY_MAXHIST; i++)
  1883. if (vty->hist[i])
  1884. XFREE (MTYPE_VTY_HIST, vty->hist[i]);
  1885. /* Unset vector. */
  1886. vector_unset (vtyvec, vty->fd);
  1887. /* Close socket. */
  1888. if (vty->fd > 0)
  1889. close (vty->fd);
  1890. if (vty->buf)
  1891. XFREE (MTYPE_VTY, vty->buf);
  1892. /* Check configure. */
  1893. vty_config_unlock (vty);
  1894. /* OK free vty. */
  1895. XFREE (MTYPE_VTY, vty);
  1896. }
  1897. /* When time out occur output message then close connection. */
  1898. static int
  1899. vty_timeout (struct thread *thread)
  1900. {
  1901. struct vty *vty;
  1902. vty = THREAD_ARG (thread);
  1903. vty->t_timeout = NULL;
  1904. vty->v_timeout = 0;
  1905. /* Clear buffer*/
  1906. buffer_reset (vty->obuf);
  1907. vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
  1908. /* Close connection. */
  1909. vty->status = VTY_CLOSE;
  1910. vty_close (vty);
  1911. return 0;
  1912. }
  1913. /* Read up configuration file from file_name. */
  1914. static void
  1915. vty_read_file (FILE *confp)
  1916. {
  1917. int ret;
  1918. struct vty *vty;
  1919. unsigned int line_num = 0;
  1920. vty = vty_new ();
  1921. vty->fd = dup(STDERR_FILENO); /* vty_close() will close this */
  1922. if (vty->fd < 0)
  1923. {
  1924. /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
  1925. vty->fd = STDOUT_FILENO;
  1926. }
  1927. vty->type = VTY_FILE;
  1928. vty->node = CONFIG_NODE;
  1929. /* Execute configuration file */
  1930. ret = config_from_file (vty, confp, &line_num);
  1931. /* Flush any previous errors before printing messages below */
  1932. buffer_flush_all (vty->obuf, vty->fd);
  1933. if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
  1934. {
  1935. switch (ret)
  1936. {
  1937. case CMD_ERR_AMBIGUOUS:
  1938. fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
  1939. break;
  1940. case CMD_ERR_NO_MATCH:
  1941. fprintf (stderr, "*** Error reading config: There is no such command.\n");
  1942. break;
  1943. }
  1944. fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
  1945. line_num, vty->buf);
  1946. vty_close (vty);
  1947. exit (1);
  1948. }
  1949. vty_close (vty);
  1950. }
  1951. static FILE *
  1952. vty_use_backup_config (char *fullpath)
  1953. {
  1954. char *fullpath_sav, *fullpath_tmp;
  1955. FILE *ret = NULL;
  1956. struct stat buf;
  1957. int tmp, sav;
  1958. int c;
  1959. char buffer[512];
  1960. fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
  1961. strcpy (fullpath_sav, fullpath);
  1962. strcat (fullpath_sav, CONF_BACKUP_EXT);
  1963. if (stat (fullpath_sav, &buf) == -1)
  1964. {
  1965. free (fullpath_sav);
  1966. return NULL;
  1967. }
  1968. fullpath_tmp = malloc (strlen (fullpath) + 8);
  1969. sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
  1970. /* Open file to configuration write. */
  1971. tmp = mkstemp (fullpath_tmp);
  1972. if (tmp < 0)
  1973. {
  1974. free (fullpath_sav);
  1975. free (fullpath_tmp);
  1976. return NULL;
  1977. }
  1978. sav = open (fullpath_sav, O_RDONLY);
  1979. if (sav < 0)
  1980. {
  1981. unlink (fullpath_tmp);
  1982. free (fullpath_sav);
  1983. free (fullpath_tmp);
  1984. return NULL;
  1985. }
  1986. while((c = read (sav, buffer, 512)) > 0)
  1987. write (tmp, buffer, c);
  1988. close (sav);
  1989. close (tmp);
  1990. if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
  1991. {
  1992. unlink (fullpath_tmp);
  1993. free (fullpath_sav);
  1994. free (fullpath_tmp);
  1995. return NULL;
  1996. }
  1997. if (link (fullpath_tmp, fullpath) == 0)
  1998. ret = fopen (fullpath, "r");
  1999. unlink (fullpath_tmp);
  2000. free (fullpath_sav);
  2001. free (fullpath_tmp);
  2002. return ret;
  2003. }
  2004. /* Read up configuration file from file_name. */
  2005. void
  2006. vty_read_config (char *config_file,
  2007. char *config_default_dir)
  2008. {
  2009. char cwd[MAXPATHLEN];
  2010. FILE *confp = NULL;
  2011. char *fullpath;
  2012. char *tmp = NULL;
  2013. /* If -f flag specified. */
  2014. if (config_file != NULL)
  2015. {
  2016. if (! IS_DIRECTORY_SEP (config_file[0]))
  2017. {
  2018. getcwd (cwd, MAXPATHLEN);
  2019. tmp = XMALLOC (MTYPE_TMP,
  2020. strlen (cwd) + strlen (config_file) + 2);
  2021. sprintf (tmp, "%s/%s", cwd, config_file);
  2022. fullpath = tmp;
  2023. }
  2024. else
  2025. fullpath = config_file;
  2026. confp = fopen (fullpath, "r");
  2027. if (confp == NULL)
  2028. {
  2029. fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
  2030. __func__, fullpath, safe_strerror (errno));
  2031. confp = vty_use_backup_config (fullpath);
  2032. if (confp)
  2033. fprintf (stderr, "WARNING: using backup configuration file!\n");
  2034. else
  2035. {
  2036. fprintf (stderr, "can't open configuration file [%s]\n",
  2037. config_file);
  2038. exit(1);
  2039. }
  2040. }
  2041. }
  2042. else
  2043. {
  2044. #ifdef VTYSH
  2045. int ret;
  2046. struct stat conf_stat;
  2047. /* !!!!PLEASE LEAVE!!!!
  2048. * This is NEEDED for use with vtysh -b, or else you can get
  2049. * a real configuration food fight with a lot garbage in the
  2050. * merged configuration file it creates coming from the per
  2051. * daemon configuration files. This also allows the daemons
  2052. * to start if there default configuration file is not
  2053. * present or ignore them, as needed when using vtysh -b to
  2054. * configure the daemons at boot - MAG
  2055. */
  2056. /* Stat for vtysh Zebra.conf, if found startup and wait for
  2057. * boot configuration
  2058. */
  2059. if ( strstr(config_default_dir, "vtysh") == NULL)
  2060. {
  2061. ret = stat (integrate_default, &conf_stat);
  2062. if (ret >= 0)
  2063. return;
  2064. }
  2065. #endif /* VTYSH */
  2066. confp = fopen (config_default_dir, "r");
  2067. if (confp == NULL)
  2068. {
  2069. fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
  2070. __func__, config_default_dir, safe_strerror (errno));
  2071. confp = vty_use_backup_config (config_default_dir);
  2072. if (confp)
  2073. {
  2074. fprintf (stderr, "WARNING: using backup configuration file!\n");
  2075. fullpath = config_default_dir;
  2076. }
  2077. else
  2078. {
  2079. fprintf (stderr, "can't open configuration file [%s]\n",
  2080. config_default_dir);
  2081. exit (1);
  2082. }
  2083. }
  2084. else
  2085. fullpath = config_default_dir;
  2086. }
  2087. vty_read_file (confp);
  2088. fclose (confp);
  2089. host_config_set (fullpath);
  2090. if (tmp)
  2091. XFREE (MTYPE_TMP, fullpath);
  2092. }
  2093. /* Small utility function which output log to the VTY. */
  2094. void
  2095. vty_log (const char *level, const char *proto_str,
  2096. const char *format, struct timestamp_control *ctl, va_list va)
  2097. {
  2098. unsigned int i;
  2099. struct vty *vty;
  2100. if (!vtyvec)
  2101. return;
  2102. for (i = 0; i < vector_active (vtyvec); i++)
  2103. if ((vty = vector_slot (vtyvec, i)) != NULL)
  2104. if (vty->monitor)
  2105. {
  2106. va_list ac;
  2107. va_copy(ac, va);
  2108. vty_log_out (vty, level, proto_str, format, ctl, ac);
  2109. va_end(ac);
  2110. }
  2111. }
  2112. /* Async-signal-safe version of vty_log for fixed strings. */
  2113. void
  2114. vty_log_fixed (const char *buf, size_t len)
  2115. {
  2116. unsigned int i;
  2117. struct iovec iov[2];
  2118. /* vty may not have been initialised */
  2119. if (!vtyvec)
  2120. return;
  2121. iov[0].iov_base = (void *)buf;
  2122. iov[0].iov_len = len;
  2123. iov[1].iov_base = (void *)"\r\n";
  2124. iov[1].iov_len = 2;
  2125. for (i = 0; i < vector_active (vtyvec); i++)
  2126. {
  2127. struct vty *vty;
  2128. if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
  2129. /* N.B. We don't care about the return code, since process is
  2130. most likely just about to die anyway. */
  2131. writev(vty->fd, iov, 2);
  2132. }
  2133. }
  2134. int
  2135. vty_config_lock (struct vty *vty)
  2136. {
  2137. if (vty_config == 0)
  2138. {
  2139. vty->config = 1;
  2140. vty_config = 1;
  2141. }
  2142. return vty->config;
  2143. }
  2144. int
  2145. vty_config_unlock (struct vty *vty)
  2146. {
  2147. if (vty_config == 1 && vty->config == 1)
  2148. {
  2149. vty->config = 0;
  2150. vty_config = 0;
  2151. }
  2152. return vty->config;
  2153. }
  2154. /* Master of the threads. */
  2155. static struct thread_master *master;
  2156. static void
  2157. vty_event (enum event event, int sock, struct vty *vty)
  2158. {
  2159. struct thread *vty_serv_thread;
  2160. switch (event)
  2161. {
  2162. case VTY_SERV:
  2163. vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
  2164. vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
  2165. break;
  2166. #ifdef VTYSH
  2167. case VTYSH_SERV:
  2168. vty_serv_thread = thread_add_read (master, vtysh_accept, vty, sock);
  2169. vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
  2170. break;
  2171. case VTYSH_READ:
  2172. vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
  2173. break;
  2174. case VTYSH_WRITE:
  2175. vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
  2176. break;
  2177. #endif /* VTYSH */
  2178. case VTY_READ:
  2179. vty->t_read = thread_add_read (master, vty_read, vty, sock);
  2180. /* Time out treatment. */
  2181. if (vty->v_timeout)
  2182. {
  2183. if (vty->t_timeout)
  2184. thread_cancel (vty->t_timeout);
  2185. vty->t_timeout =
  2186. thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
  2187. }
  2188. break;
  2189. case VTY_WRITE:
  2190. if (! vty->t_write)
  2191. vty->t_write = thread_add_write (master, vty_flush, vty, sock);
  2192. break;
  2193. case VTY_TIMEOUT_RESET:
  2194. if (vty->t_timeout)
  2195. {
  2196. thread_cancel (vty->t_timeout);
  2197. vty->t_timeout = NULL;
  2198. }
  2199. if (vty->v_timeout)
  2200. {
  2201. vty->t_timeout =
  2202. thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
  2203. }
  2204. break;
  2205. }
  2206. }
  2207. DEFUN (config_who,
  2208. config_who_cmd,
  2209. "who",
  2210. "Display who is on vty\n")
  2211. {
  2212. unsigned int i;
  2213. struct vty *v;
  2214. for (i = 0; i < vector_active (vtyvec); i++)
  2215. if ((v = vector_slot (vtyvec, i)) != NULL)
  2216. vty_out (vty, "%svty[%d] connected from %s.%s",
  2217. v->config ? "*" : " ",
  2218. i, v->address, VTY_NEWLINE);
  2219. return CMD_SUCCESS;
  2220. }
  2221. /* Move to vty configuration mode. */
  2222. DEFUN (line_vty,
  2223. line_vty_cmd,
  2224. "line vty",
  2225. "Configure a terminal line\n"
  2226. "Virtual terminal\n")
  2227. {
  2228. vty->node = VTY_NODE;
  2229. return CMD_SUCCESS;
  2230. }
  2231. /* Set time out value. */
  2232. static int
  2233. exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
  2234. {
  2235. unsigned long timeout = 0;
  2236. /* min_str and sec_str are already checked by parser. So it must be
  2237. all digit string. */
  2238. if (min_str)
  2239. {
  2240. timeout = strtol (min_str, NULL, 10);
  2241. timeout *= 60;
  2242. }
  2243. if (sec_str)
  2244. timeout += strtol (sec_str, NULL, 10);
  2245. vty_timeout_val = timeout;
  2246. vty->v_timeout = timeout;
  2247. vty_event (VTY_TIMEOUT_RESET, 0, vty);
  2248. return CMD_SUCCESS;
  2249. }
  2250. DEFUN (exec_timeout_min,
  2251. exec_timeout_min_cmd,
  2252. "exec-timeout <0-35791>",
  2253. "Set timeout value\n"
  2254. "Timeout value in minutes\n")
  2255. {
  2256. return exec_timeout (vty, argv[0], NULL);
  2257. }
  2258. DEFUN (exec_timeout_sec,
  2259. exec_timeout_sec_cmd,
  2260. "exec-timeout <0-35791> <0-2147483>",
  2261. "Set the EXEC timeout\n"
  2262. "Timeout in minutes\n"
  2263. "Timeout in seconds\n")
  2264. {
  2265. return exec_timeout (vty, argv[0], argv[1]);
  2266. }
  2267. DEFUN (no_exec_timeout,
  2268. no_exec_timeout_cmd,
  2269. "no exec-timeout",
  2270. NO_STR
  2271. "Set the EXEC timeout\n")
  2272. {
  2273. return exec_timeout (vty, NULL, NULL);
  2274. }
  2275. /* Set vty access class. */
  2276. DEFUN (vty_access_class,
  2277. vty_access_class_cmd,
  2278. "access-class WORD",
  2279. "Filter connections based on an IP access list\n"
  2280. "IP access list\n")
  2281. {
  2282. if (vty_accesslist_name)
  2283. XFREE(MTYPE_VTY, vty_accesslist_name);
  2284. vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
  2285. return CMD_SUCCESS;
  2286. }
  2287. /* Clear vty access class. */
  2288. DEFUN (no_vty_access_class,
  2289. no_vty_access_class_cmd,
  2290. "no access-class [WORD]",
  2291. NO_STR
  2292. "Filter connections based on an IP access list\n"
  2293. "IP access list\n")
  2294. {
  2295. if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
  2296. {
  2297. vty_out (vty, "Access-class is not currently applied to vty%s",
  2298. VTY_NEWLINE);
  2299. return CMD_WARNING;
  2300. }
  2301. XFREE(MTYPE_VTY, vty_accesslist_name);
  2302. vty_accesslist_name = NULL;
  2303. return CMD_SUCCESS;
  2304. }
  2305. #ifdef HAVE_IPV6
  2306. /* Set vty access class. */
  2307. DEFUN (vty_ipv6_access_class,
  2308. vty_ipv6_access_class_cmd,
  2309. "ipv6 access-class WORD",
  2310. IPV6_STR
  2311. "Filter connections based on an IP access list\n"
  2312. "IPv6 access list\n")
  2313. {
  2314. if (vty_ipv6_accesslist_name)
  2315. XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
  2316. vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
  2317. return CMD_SUCCESS;
  2318. }
  2319. /* Clear vty access class. */
  2320. DEFUN (no_vty_ipv6_access_class,
  2321. no_vty_ipv6_access_class_cmd,
  2322. "no ipv6 access-class [WORD]",
  2323. NO_STR
  2324. IPV6_STR
  2325. "Filter connections based on an IP access list\n"
  2326. "IPv6 access list\n")
  2327. {
  2328. if (! vty_ipv6_accesslist_name ||
  2329. (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
  2330. {
  2331. vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
  2332. VTY_NEWLINE);
  2333. return CMD_WARNING;
  2334. }
  2335. XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
  2336. vty_ipv6_accesslist_name = NULL;
  2337. return CMD_SUCCESS;
  2338. }
  2339. #endif /* HAVE_IPV6 */
  2340. /* vty login. */
  2341. DEFUN (vty_login,
  2342. vty_login_cmd,
  2343. "login",
  2344. "Enable password checking\n")
  2345. {
  2346. no_password_check = 0;
  2347. return CMD_SUCCESS;
  2348. }
  2349. DEFUN (no_vty_login,
  2350. no_vty_login_cmd,
  2351. "no login",
  2352. NO_STR
  2353. "Enable password checking\n")
  2354. {
  2355. no_password_check = 1;
  2356. return CMD_SUCCESS;
  2357. }
  2358. /* initial mode. */
  2359. DEFUN (vty_restricted_mode,
  2360. vty_restricted_mode_cmd,
  2361. "anonymous restricted",
  2362. "Restrict view commands available in anonymous, unauthenticated vty\n")
  2363. {
  2364. restricted_mode = 1;
  2365. return CMD_SUCCESS;
  2366. }
  2367. DEFUN (vty_no_restricted_mode,
  2368. vty_no_restricted_mode_cmd,
  2369. "no anonymous restricted",
  2370. NO_STR
  2371. "Enable password checking\n")
  2372. {
  2373. restricted_mode = 0;
  2374. return CMD_SUCCESS;
  2375. }
  2376. DEFUN (service_advanced_vty,
  2377. service_advanced_vty_cmd,
  2378. "service advanced-vty",
  2379. "Set up miscellaneous service\n"
  2380. "Enable advanced mode vty interface\n")
  2381. {
  2382. host.advanced = 1;
  2383. return CMD_SUCCESS;
  2384. }
  2385. DEFUN (no_service_advanced_vty,
  2386. no_service_advanced_vty_cmd,
  2387. "no service advanced-vty",
  2388. NO_STR
  2389. "Set up miscellaneous service\n"
  2390. "Enable advanced mode vty interface\n")
  2391. {
  2392. host.advanced = 0;
  2393. return CMD_SUCCESS;
  2394. }
  2395. DEFUN (terminal_monitor,
  2396. terminal_monitor_cmd,
  2397. "terminal monitor",
  2398. "Set terminal line parameters\n"
  2399. "Copy debug output to the current terminal line\n")
  2400. {
  2401. vty->monitor = 1;
  2402. return CMD_SUCCESS;
  2403. }
  2404. DEFUN (terminal_no_monitor,
  2405. terminal_no_monitor_cmd,
  2406. "terminal no monitor",
  2407. "Set terminal line parameters\n"
  2408. NO_STR
  2409. "Copy debug output to the current terminal line\n")
  2410. {
  2411. vty->monitor = 0;
  2412. return CMD_SUCCESS;
  2413. }
  2414. ALIAS (terminal_no_monitor,
  2415. no_terminal_monitor_cmd,
  2416. "no terminal monitor",
  2417. NO_STR
  2418. "Set terminal line parameters\n"
  2419. "Copy debug output to the current terminal line\n")
  2420. DEFUN (show_history,
  2421. show_history_cmd,
  2422. "show history",
  2423. SHOW_STR
  2424. "Display the session command history\n")
  2425. {
  2426. int index;
  2427. for (index = vty->hindex + 1; index != vty->hindex;)
  2428. {
  2429. if (index == VTY_MAXHIST)
  2430. {
  2431. index = 0;
  2432. continue;
  2433. }
  2434. if (vty->hist[index] != NULL)
  2435. vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
  2436. index++;
  2437. }
  2438. return CMD_SUCCESS;
  2439. }
  2440. /* Display current configuration. */
  2441. static int
  2442. vty_config_write (struct vty *vty)
  2443. {
  2444. vty_out (vty, "line vty%s", VTY_NEWLINE);
  2445. if (vty_accesslist_name)
  2446. vty_out (vty, " access-class %s%s",
  2447. vty_accesslist_name, VTY_NEWLINE);
  2448. if (vty_ipv6_accesslist_name)
  2449. vty_out (vty, " ipv6 access-class %s%s",
  2450. vty_ipv6_accesslist_name, VTY_NEWLINE);
  2451. /* exec-timeout */
  2452. if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
  2453. vty_out (vty, " exec-timeout %ld %ld%s",
  2454. vty_timeout_val / 60,
  2455. vty_timeout_val % 60, VTY_NEWLINE);
  2456. /* login */
  2457. if (no_password_check)
  2458. vty_out (vty, " no login%s", VTY_NEWLINE);
  2459. if (restricted_mode != restricted_mode_default)
  2460. {
  2461. if (restricted_mode_default)
  2462. vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
  2463. else
  2464. vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
  2465. }
  2466. vty_out (vty, "!%s", VTY_NEWLINE);
  2467. return CMD_SUCCESS;
  2468. }
  2469. struct cmd_node vty_node =
  2470. {
  2471. VTY_NODE,
  2472. "%s(config-line)# ",
  2473. 1,
  2474. };
  2475. /* Reset all VTY status. */
  2476. void
  2477. vty_reset ()
  2478. {
  2479. unsigned int i;
  2480. struct vty *vty;
  2481. struct thread *vty_serv_thread;
  2482. for (i = 0; i < vector_active (vtyvec); i++)
  2483. if ((vty = vector_slot (vtyvec, i)) != NULL)
  2484. {
  2485. buffer_reset (vty->obuf);
  2486. vty->status = VTY_CLOSE;
  2487. vty_close (vty);
  2488. }
  2489. for (i = 0; i < vector_active (Vvty_serv_thread); i++)
  2490. if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
  2491. {
  2492. thread_cancel (vty_serv_thread);
  2493. vector_slot (Vvty_serv_thread, i) = NULL;
  2494. close (i);
  2495. }
  2496. vty_timeout_val = VTY_TIMEOUT_DEFAULT;
  2497. if (vty_accesslist_name)
  2498. {
  2499. XFREE(MTYPE_VTY, vty_accesslist_name);
  2500. vty_accesslist_name = NULL;
  2501. }
  2502. if (vty_ipv6_accesslist_name)
  2503. {
  2504. XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
  2505. vty_ipv6_accesslist_name = NULL;
  2506. }
  2507. }
  2508. static void
  2509. vty_save_cwd (void)
  2510. {
  2511. char cwd[MAXPATHLEN];
  2512. char *c;
  2513. c = getcwd (cwd, MAXPATHLEN);
  2514. if (!c)
  2515. {
  2516. chdir (SYSCONFDIR);
  2517. getcwd (cwd, MAXPATHLEN);
  2518. }
  2519. vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
  2520. strcpy (vty_cwd, cwd);
  2521. }
  2522. char *
  2523. vty_get_cwd ()
  2524. {
  2525. return vty_cwd;
  2526. }
  2527. int
  2528. vty_shell (struct vty *vty)
  2529. {
  2530. return vty->type == VTY_SHELL ? 1 : 0;
  2531. }
  2532. int
  2533. vty_shell_serv (struct vty *vty)
  2534. {
  2535. return vty->type == VTY_SHELL_SERV ? 1 : 0;
  2536. }
  2537. void
  2538. vty_init_vtysh ()
  2539. {
  2540. vtyvec = vector_init (VECTOR_MIN_SIZE);
  2541. }
  2542. /* Install vty's own commands like `who' command. */
  2543. void
  2544. vty_init (struct thread_master *master_thread)
  2545. {
  2546. /* For further configuration read, preserve current directory. */
  2547. vty_save_cwd ();
  2548. vtyvec = vector_init (VECTOR_MIN_SIZE);
  2549. master = master_thread;
  2550. /* Initilize server thread vector. */
  2551. Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
  2552. /* Install bgp top node. */
  2553. install_node (&vty_node, vty_config_write);
  2554. install_element (RESTRICTED_NODE, &config_who_cmd);
  2555. install_element (RESTRICTED_NODE, &show_history_cmd);
  2556. install_element (VIEW_NODE, &config_who_cmd);
  2557. install_element (VIEW_NODE, &show_history_cmd);
  2558. install_element (ENABLE_NODE, &config_who_cmd);
  2559. install_element (CONFIG_NODE, &line_vty_cmd);
  2560. install_element (CONFIG_NODE, &service_advanced_vty_cmd);
  2561. install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
  2562. install_element (CONFIG_NODE, &show_history_cmd);
  2563. install_element (ENABLE_NODE, &terminal_monitor_cmd);
  2564. install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
  2565. install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
  2566. install_element (ENABLE_NODE, &show_history_cmd);
  2567. install_default (VTY_NODE);
  2568. install_element (VTY_NODE, &exec_timeout_min_cmd);
  2569. install_element (VTY_NODE, &exec_timeout_sec_cmd);
  2570. install_element (VTY_NODE, &no_exec_timeout_cmd);
  2571. install_element (VTY_NODE, &vty_access_class_cmd);
  2572. install_element (VTY_NODE, &no_vty_access_class_cmd);
  2573. install_element (VTY_NODE, &vty_login_cmd);
  2574. install_element (VTY_NODE, &no_vty_login_cmd);
  2575. install_element (VTY_NODE, &vty_restricted_mode_cmd);
  2576. install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
  2577. #ifdef HAVE_IPV6
  2578. install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
  2579. install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
  2580. #endif /* HAVE_IPV6 */
  2581. }
  2582. void
  2583. vty_terminate (void)
  2584. {
  2585. if (vty_cwd)
  2586. XFREE (MTYPE_TMP, vty_cwd);
  2587. if (vtyvec && Vvty_serv_thread)
  2588. {
  2589. vty_reset ();
  2590. vector_free (vtyvec);
  2591. vector_free (Vvty_serv_thread);
  2592. }
  2593. }