vty.c 67 KB

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