vty.c 70 KB

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