vty.c 65 KB

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