command.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730
  1. /*
  2. $Id$
  3. Command interpreter routine for virtual terminal [aka TeletYpe]
  4. Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
  5. This file is part of GNU Zebra.
  6. GNU Zebra is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published
  8. by the Free Software Foundation; either version 2, or (at your
  9. option) any later version.
  10. GNU Zebra is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Zebra; see the file COPYING. If not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #include <zebra.h>
  19. #include "memory.h"
  20. #include "log.h"
  21. #include <lib/version.h>
  22. #include "thread.h"
  23. #include "vector.h"
  24. #include "vty.h"
  25. #include "command.h"
  26. #include "workqueue.h"
  27. /* Command vector which includes some level of command lists. Normally
  28. each daemon maintains each own cmdvec. */
  29. vector cmdvec = NULL;
  30. struct desc desc_cr;
  31. char *command_cr = NULL;
  32. /* Host information structure. */
  33. struct host host;
  34. /* Standard command node structures. */
  35. static struct cmd_node auth_node =
  36. {
  37. AUTH_NODE,
  38. "Password: ",
  39. };
  40. static struct cmd_node view_node =
  41. {
  42. VIEW_NODE,
  43. "%s> ",
  44. };
  45. static struct cmd_node restricted_node =
  46. {
  47. RESTRICTED_NODE,
  48. "%s$ ",
  49. };
  50. static struct cmd_node auth_enable_node =
  51. {
  52. AUTH_ENABLE_NODE,
  53. "Password: ",
  54. };
  55. static struct cmd_node enable_node =
  56. {
  57. ENABLE_NODE,
  58. "%s# ",
  59. };
  60. static struct cmd_node config_node =
  61. {
  62. CONFIG_NODE,
  63. "%s(config)# ",
  64. 1
  65. };
  66. /* Default motd string. */
  67. static const char *default_motd =
  68. "\r\n\
  69. Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\
  70. " QUAGGA_COPYRIGHT "\r\n\
  71. \r\n";
  72. static const struct facility_map {
  73. int facility;
  74. const char *name;
  75. size_t match;
  76. } syslog_facilities[] =
  77. {
  78. { LOG_KERN, "kern", 1 },
  79. { LOG_USER, "user", 2 },
  80. { LOG_MAIL, "mail", 1 },
  81. { LOG_DAEMON, "daemon", 1 },
  82. { LOG_AUTH, "auth", 1 },
  83. { LOG_SYSLOG, "syslog", 1 },
  84. { LOG_LPR, "lpr", 2 },
  85. { LOG_NEWS, "news", 1 },
  86. { LOG_UUCP, "uucp", 2 },
  87. { LOG_CRON, "cron", 1 },
  88. #ifdef LOG_FTP
  89. { LOG_FTP, "ftp", 1 },
  90. #endif
  91. { LOG_LOCAL0, "local0", 6 },
  92. { LOG_LOCAL1, "local1", 6 },
  93. { LOG_LOCAL2, "local2", 6 },
  94. { LOG_LOCAL3, "local3", 6 },
  95. { LOG_LOCAL4, "local4", 6 },
  96. { LOG_LOCAL5, "local5", 6 },
  97. { LOG_LOCAL6, "local6", 6 },
  98. { LOG_LOCAL7, "local7", 6 },
  99. { 0, NULL, 0 },
  100. };
  101. static const char *
  102. facility_name(int facility)
  103. {
  104. const struct facility_map *fm;
  105. for (fm = syslog_facilities; fm->name; fm++)
  106. if (fm->facility == facility)
  107. return fm->name;
  108. return "";
  109. }
  110. static int
  111. facility_match(const char *str)
  112. {
  113. const struct facility_map *fm;
  114. for (fm = syslog_facilities; fm->name; fm++)
  115. if (!strncmp(str,fm->name,fm->match))
  116. return fm->facility;
  117. return -1;
  118. }
  119. static int
  120. level_match(const char *s)
  121. {
  122. int level ;
  123. for ( level = 0 ; zlog_priority [level] != NULL ; level ++ )
  124. if (!strncmp (s, zlog_priority[level], 2))
  125. return level;
  126. return ZLOG_DISABLED;
  127. }
  128. /* This is called from main when a daemon is invoked with -v or --version. */
  129. void
  130. print_version (const char *progname)
  131. {
  132. printf ("%s version %s\n", progname, QUAGGA_VERSION);
  133. printf ("%s\n", QUAGGA_COPYRIGHT);
  134. }
  135. /* Utility function to concatenate argv argument into a single string
  136. with inserting ' ' character between each argument. */
  137. char *
  138. argv_concat (const char **argv, int argc, int shift)
  139. {
  140. int i;
  141. size_t len;
  142. char *str;
  143. char *p;
  144. len = 0;
  145. for (i = shift; i < argc; i++)
  146. len += strlen(argv[i])+1;
  147. if (!len)
  148. return NULL;
  149. p = str = XMALLOC(MTYPE_TMP, len);
  150. for (i = shift; i < argc; i++)
  151. {
  152. size_t arglen;
  153. memcpy(p, argv[i], (arglen = strlen(argv[i])));
  154. p += arglen;
  155. *p++ = ' ';
  156. }
  157. *(p-1) = '\0';
  158. return str;
  159. }
  160. /* Install top node of command vector. */
  161. void
  162. install_node (struct cmd_node *node,
  163. int (*func) (struct vty *))
  164. {
  165. vector_set_index (cmdvec, node->node, node);
  166. node->func = func;
  167. node->cmd_vector = vector_init (VECTOR_MIN_SIZE);
  168. }
  169. /* Compare two command's string. Used in sort_node (). */
  170. static int
  171. cmp_node (const void *p, const void *q)
  172. {
  173. const struct cmd_element *a = *(struct cmd_element * const *)p;
  174. const struct cmd_element *b = *(struct cmd_element * const *)q;
  175. return strcmp (a->string, b->string);
  176. }
  177. static int
  178. cmp_desc (const void *p, const void *q)
  179. {
  180. const struct desc *a = *(struct desc * const *)p;
  181. const struct desc *b = *(struct desc * const *)q;
  182. return strcmp (a->cmd, b->cmd);
  183. }
  184. /* Sort each node's command element according to command string. */
  185. void
  186. sort_node ()
  187. {
  188. unsigned int i, j;
  189. struct cmd_node *cnode;
  190. vector descvec;
  191. struct cmd_element *cmd_element;
  192. for (i = 0; i < vector_active (cmdvec); i++)
  193. if ((cnode = vector_slot (cmdvec, i)) != NULL)
  194. {
  195. vector cmd_vector = cnode->cmd_vector;
  196. qsort (cmd_vector->index, vector_active (cmd_vector),
  197. sizeof (void *), cmp_node);
  198. for (j = 0; j < vector_active (cmd_vector); j++)
  199. if ((cmd_element = vector_slot (cmd_vector, j)) != NULL
  200. && vector_active (cmd_element->strvec))
  201. {
  202. descvec = vector_slot (cmd_element->strvec,
  203. vector_active (cmd_element->strvec) - 1);
  204. qsort (descvec->index, vector_active (descvec),
  205. sizeof (void *), cmp_desc);
  206. }
  207. }
  208. }
  209. /* Breaking up string into each command piece. I assume given
  210. character is separated by a space character. Return value is a
  211. vector which includes char ** data element. */
  212. vector
  213. cmd_make_strvec (const char *string)
  214. {
  215. const char *cp, *start;
  216. char *token;
  217. int strlen;
  218. vector strvec;
  219. if (string == NULL)
  220. return NULL;
  221. cp = string;
  222. /* Skip white spaces. */
  223. while (isspace ((int) *cp) && *cp != '\0')
  224. cp++;
  225. /* Return if there is only white spaces */
  226. if (*cp == '\0')
  227. return NULL;
  228. if (*cp == '!' || *cp == '#')
  229. return NULL;
  230. /* Prepare return vector. */
  231. strvec = vector_init (VECTOR_MIN_SIZE);
  232. /* Copy each command piece and set into vector. */
  233. while (1)
  234. {
  235. start = cp;
  236. while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') &&
  237. *cp != '\0')
  238. cp++;
  239. strlen = cp - start;
  240. token = XMALLOC (MTYPE_STRVEC, strlen + 1);
  241. memcpy (token, start, strlen);
  242. *(token + strlen) = '\0';
  243. vector_set (strvec, token);
  244. while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') &&
  245. *cp != '\0')
  246. cp++;
  247. if (*cp == '\0')
  248. return strvec;
  249. }
  250. }
  251. /* Free allocated string vector. */
  252. void
  253. cmd_free_strvec (vector v)
  254. {
  255. unsigned int i;
  256. char *cp;
  257. if (!v)
  258. return;
  259. for (i = 0; i < vector_active (v); i++)
  260. if ((cp = vector_slot (v, i)) != NULL)
  261. XFREE (MTYPE_STRVEC, cp);
  262. vector_free (v);
  263. }
  264. /* Fetch next description. Used in cmd_make_descvec(). */
  265. static char *
  266. cmd_desc_str (const char **string)
  267. {
  268. const char *cp, *start;
  269. char *token;
  270. int strlen;
  271. cp = *string;
  272. if (cp == NULL)
  273. return NULL;
  274. /* Skip white spaces. */
  275. while (isspace ((int) *cp) && *cp != '\0')
  276. cp++;
  277. /* Return if there is only white spaces */
  278. if (*cp == '\0')
  279. return NULL;
  280. start = cp;
  281. while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
  282. cp++;
  283. strlen = cp - start;
  284. token = XMALLOC (MTYPE_STRVEC, strlen + 1);
  285. memcpy (token, start, strlen);
  286. *(token + strlen) = '\0';
  287. *string = cp;
  288. return token;
  289. }
  290. /* New string vector. */
  291. static vector
  292. cmd_make_descvec (const char *string, const char *descstr)
  293. {
  294. int multiple = 0;
  295. const char *sp;
  296. char *token;
  297. int len;
  298. const char *cp;
  299. const char *dp;
  300. vector allvec;
  301. vector strvec = NULL;
  302. struct desc *desc;
  303. cp = string;
  304. dp = descstr;
  305. if (cp == NULL)
  306. return NULL;
  307. allvec = vector_init (VECTOR_MIN_SIZE);
  308. while (1)
  309. {
  310. while (isspace ((int) *cp) && *cp != '\0')
  311. cp++;
  312. if (*cp == '(')
  313. {
  314. multiple = 1;
  315. cp++;
  316. }
  317. if (*cp == ')')
  318. {
  319. multiple = 0;
  320. cp++;
  321. }
  322. if (*cp == '|')
  323. {
  324. if (! multiple)
  325. {
  326. fprintf (stderr, "Command parse error!: %s\n", string);
  327. exit (1);
  328. }
  329. cp++;
  330. }
  331. while (isspace ((int) *cp) && *cp != '\0')
  332. cp++;
  333. if (*cp == '(')
  334. {
  335. multiple = 1;
  336. cp++;
  337. }
  338. if (*cp == '\0')
  339. return allvec;
  340. sp = cp;
  341. while (! (isspace ((int) *cp) || *cp == '\r' || *cp == '\n' || *cp == ')' || *cp == '|') && *cp != '\0')
  342. cp++;
  343. len = cp - sp;
  344. token = XMALLOC (MTYPE_STRVEC, len + 1);
  345. memcpy (token, sp, len);
  346. *(token + len) = '\0';
  347. desc = XCALLOC (MTYPE_DESC, sizeof (struct desc));
  348. desc->cmd = token;
  349. desc->str = cmd_desc_str (&dp);
  350. if (multiple)
  351. {
  352. if (multiple == 1)
  353. {
  354. strvec = vector_init (VECTOR_MIN_SIZE);
  355. vector_set (allvec, strvec);
  356. }
  357. multiple++;
  358. }
  359. else
  360. {
  361. strvec = vector_init (VECTOR_MIN_SIZE);
  362. vector_set (allvec, strvec);
  363. }
  364. vector_set (strvec, desc);
  365. }
  366. }
  367. /* Count mandantory string vector size. This is to determine inputed
  368. command has enough command length. */
  369. static int
  370. cmd_cmdsize (vector strvec)
  371. {
  372. unsigned int i;
  373. int size = 0;
  374. vector descvec;
  375. struct desc *desc;
  376. for (i = 0; i < vector_active (strvec); i++)
  377. if ((descvec = vector_slot (strvec, i)) != NULL)
  378. {
  379. if ((vector_active (descvec)) == 1
  380. && (desc = vector_slot (descvec, 0)) != NULL)
  381. {
  382. if (desc->cmd == NULL || CMD_OPTION (desc->cmd))
  383. return size;
  384. else
  385. size++;
  386. }
  387. else
  388. size++;
  389. }
  390. return size;
  391. }
  392. /* Return prompt character of specified node. */
  393. const char *
  394. cmd_prompt (enum node_type node)
  395. {
  396. struct cmd_node *cnode;
  397. cnode = vector_slot (cmdvec, node);
  398. return cnode->prompt;
  399. }
  400. /* Install a command into a node. */
  401. void
  402. install_element (enum node_type ntype, struct cmd_element *cmd)
  403. {
  404. struct cmd_node *cnode;
  405. /* cmd_init hasn't been called */
  406. if (!cmdvec)
  407. return;
  408. cnode = vector_slot (cmdvec, ntype);
  409. if (cnode == NULL)
  410. {
  411. fprintf (stderr, "Command node %d doesn't exist, please check it\n",
  412. ntype);
  413. exit (1);
  414. }
  415. vector_set (cnode->cmd_vector, cmd);
  416. if (cmd->strvec == NULL)
  417. cmd->strvec = cmd_make_descvec (cmd->string, cmd->doc);
  418. cmd->cmdsize = cmd_cmdsize (cmd->strvec);
  419. }
  420. static const unsigned char itoa64[] =
  421. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  422. static void
  423. to64(char *s, long v, int n)
  424. {
  425. while (--n >= 0)
  426. {
  427. *s++ = itoa64[v&0x3f];
  428. v >>= 6;
  429. }
  430. }
  431. static char *
  432. zencrypt (const char *passwd)
  433. {
  434. char salt[6];
  435. struct timeval tv;
  436. char *crypt (const char *, const char *);
  437. gettimeofday(&tv,0);
  438. to64(&salt[0], random(), 3);
  439. to64(&salt[3], tv.tv_usec, 3);
  440. salt[5] = '\0';
  441. return crypt (passwd, salt);
  442. }
  443. /* This function write configuration of this host. */
  444. static int
  445. config_write_host (struct vty *vty)
  446. {
  447. if (host.name)
  448. vty_out (vty, "hostname %s%s", host.name, VTY_NEWLINE);
  449. if (host.encrypt)
  450. {
  451. if (host.password_encrypt)
  452. vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE);
  453. if (host.enable_encrypt)
  454. vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE);
  455. }
  456. else
  457. {
  458. if (host.password)
  459. vty_out (vty, "password %s%s", host.password, VTY_NEWLINE);
  460. if (host.enable)
  461. vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE);
  462. }
  463. if (zlog_default->default_lvl != LOG_DEBUG)
  464. {
  465. vty_out (vty, "! N.B. The 'log trap' command is deprecated.%s",
  466. VTY_NEWLINE);
  467. vty_out (vty, "log trap %s%s",
  468. zlog_priority[zlog_default->default_lvl], VTY_NEWLINE);
  469. }
  470. if (host.logfile && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED))
  471. {
  472. vty_out (vty, "log file %s", host.logfile);
  473. if (zlog_default->maxlvl[ZLOG_DEST_FILE] != zlog_default->default_lvl)
  474. vty_out (vty, " %s",
  475. zlog_priority[zlog_default->maxlvl[ZLOG_DEST_FILE]]);
  476. vty_out (vty, "%s", VTY_NEWLINE);
  477. }
  478. if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED)
  479. {
  480. vty_out (vty, "log stdout");
  481. if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != zlog_default->default_lvl)
  482. vty_out (vty, " %s",
  483. zlog_priority[zlog_default->maxlvl[ZLOG_DEST_STDOUT]]);
  484. vty_out (vty, "%s", VTY_NEWLINE);
  485. }
  486. if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
  487. vty_out(vty,"no log monitor%s",VTY_NEWLINE);
  488. else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] != zlog_default->default_lvl)
  489. vty_out(vty,"log monitor %s%s",
  490. zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],VTY_NEWLINE);
  491. if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
  492. {
  493. vty_out (vty, "log syslog");
  494. if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != zlog_default->default_lvl)
  495. vty_out (vty, " %s",
  496. zlog_priority[zlog_default->maxlvl[ZLOG_DEST_SYSLOG]]);
  497. vty_out (vty, "%s", VTY_NEWLINE);
  498. }
  499. if (zlog_default->facility != LOG_DAEMON)
  500. vty_out (vty, "log facility %s%s",
  501. facility_name(zlog_default->facility), VTY_NEWLINE);
  502. if (zlog_default->record_priority == 1)
  503. vty_out (vty, "log record-priority%s", VTY_NEWLINE);
  504. if (zlog_default->timestamp_precision > 0)
  505. vty_out (vty, "log timestamp precision %d%s",
  506. zlog_default->timestamp_precision, VTY_NEWLINE);
  507. if (host.advanced)
  508. vty_out (vty, "service advanced-vty%s", VTY_NEWLINE);
  509. if (host.encrypt)
  510. vty_out (vty, "service password-encryption%s", VTY_NEWLINE);
  511. if (host.lines >= 0)
  512. vty_out (vty, "service terminal-length %d%s", host.lines,
  513. VTY_NEWLINE);
  514. if (host.motdfile)
  515. vty_out (vty, "banner motd file %s%s", host.motdfile, VTY_NEWLINE);
  516. else if (! host.motd)
  517. vty_out (vty, "no banner motd%s", VTY_NEWLINE);
  518. return 1;
  519. }
  520. /* Utility function for getting command vector. */
  521. static vector
  522. cmd_node_vector (vector v, enum node_type ntype)
  523. {
  524. struct cmd_node *cnode = vector_slot (v, ntype);
  525. return cnode->cmd_vector;
  526. }
  527. #if 0
  528. /* Filter command vector by symbol. This function is not actually used;
  529. * should it be deleted? */
  530. static int
  531. cmd_filter_by_symbol (char *command, char *symbol)
  532. {
  533. int i, lim;
  534. if (strcmp (symbol, "IPV4_ADDRESS") == 0)
  535. {
  536. i = 0;
  537. lim = strlen (command);
  538. while (i < lim)
  539. {
  540. if (! (isdigit ((int) command[i]) || command[i] == '.' || command[i] == '/'))
  541. return 1;
  542. i++;
  543. }
  544. return 0;
  545. }
  546. if (strcmp (symbol, "STRING") == 0)
  547. {
  548. i = 0;
  549. lim = strlen (command);
  550. while (i < lim)
  551. {
  552. if (! (isalpha ((int) command[i]) || command[i] == '_' || command[i] == '-'))
  553. return 1;
  554. i++;
  555. }
  556. return 0;
  557. }
  558. if (strcmp (symbol, "IFNAME") == 0)
  559. {
  560. i = 0;
  561. lim = strlen (command);
  562. while (i < lim)
  563. {
  564. if (! isalnum ((int) command[i]))
  565. return 1;
  566. i++;
  567. }
  568. return 0;
  569. }
  570. return 0;
  571. }
  572. #endif
  573. /* Completion match types. */
  574. enum match_type
  575. {
  576. no_match,
  577. extend_match,
  578. ipv4_prefix_match,
  579. ipv4_match,
  580. ipv6_prefix_match,
  581. ipv6_match,
  582. range_match,
  583. vararg_match,
  584. partly_match,
  585. exact_match
  586. };
  587. static enum match_type
  588. cmd_ipv4_match (const char *str)
  589. {
  590. const char *sp;
  591. int dots = 0, nums = 0;
  592. char buf[4];
  593. if (str == NULL)
  594. return partly_match;
  595. for (;;)
  596. {
  597. memset (buf, 0, sizeof (buf));
  598. sp = str;
  599. while (*str != '\0')
  600. {
  601. if (*str == '.')
  602. {
  603. if (dots >= 3)
  604. return no_match;
  605. if (*(str + 1) == '.')
  606. return no_match;
  607. if (*(str + 1) == '\0')
  608. return partly_match;
  609. dots++;
  610. break;
  611. }
  612. if (!isdigit ((int) *str))
  613. return no_match;
  614. str++;
  615. }
  616. if (str - sp > 3)
  617. return no_match;
  618. strncpy (buf, sp, str - sp);
  619. if (atoi (buf) > 255)
  620. return no_match;
  621. nums++;
  622. if (*str == '\0')
  623. break;
  624. str++;
  625. }
  626. if (nums < 4)
  627. return partly_match;
  628. return exact_match;
  629. }
  630. static enum match_type
  631. cmd_ipv4_prefix_match (const char *str)
  632. {
  633. const char *sp;
  634. int dots = 0;
  635. char buf[4];
  636. if (str == NULL)
  637. return partly_match;
  638. for (;;)
  639. {
  640. memset (buf, 0, sizeof (buf));
  641. sp = str;
  642. while (*str != '\0' && *str != '/')
  643. {
  644. if (*str == '.')
  645. {
  646. if (dots == 3)
  647. return no_match;
  648. if (*(str + 1) == '.' || *(str + 1) == '/')
  649. return no_match;
  650. if (*(str + 1) == '\0')
  651. return partly_match;
  652. dots++;
  653. break;
  654. }
  655. if (!isdigit ((int) *str))
  656. return no_match;
  657. str++;
  658. }
  659. if (str - sp > 3)
  660. return no_match;
  661. strncpy (buf, sp, str - sp);
  662. if (atoi (buf) > 255)
  663. return no_match;
  664. if (dots == 3)
  665. {
  666. if (*str == '/')
  667. {
  668. if (*(str + 1) == '\0')
  669. return partly_match;
  670. str++;
  671. break;
  672. }
  673. else if (*str == '\0')
  674. return partly_match;
  675. }
  676. if (*str == '\0')
  677. return partly_match;
  678. str++;
  679. }
  680. sp = str;
  681. while (*str != '\0')
  682. {
  683. if (!isdigit ((int) *str))
  684. return no_match;
  685. str++;
  686. }
  687. if (atoi (sp) > 32)
  688. return no_match;
  689. return exact_match;
  690. }
  691. #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
  692. #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
  693. #define STATE_START 1
  694. #define STATE_COLON 2
  695. #define STATE_DOUBLE 3
  696. #define STATE_ADDR 4
  697. #define STATE_DOT 5
  698. #define STATE_SLASH 6
  699. #define STATE_MASK 7
  700. #ifdef HAVE_IPV6
  701. static enum match_type
  702. cmd_ipv6_match (const char *str)
  703. {
  704. int state = STATE_START;
  705. int colons = 0, nums = 0, double_colon = 0;
  706. const char *sp = NULL;
  707. struct sockaddr_in6 sin6_dummy;
  708. int ret;
  709. if (str == NULL)
  710. return partly_match;
  711. if (strspn (str, IPV6_ADDR_STR) != strlen (str))
  712. return no_match;
  713. /* use inet_pton that has a better support,
  714. * for example inet_pton can support the automatic addresses:
  715. * ::1.2.3.4
  716. */
  717. ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
  718. if (ret == 1)
  719. return exact_match;
  720. while (*str != '\0')
  721. {
  722. switch (state)
  723. {
  724. case STATE_START:
  725. if (*str == ':')
  726. {
  727. if (*(str + 1) != ':' && *(str + 1) != '\0')
  728. return no_match;
  729. colons--;
  730. state = STATE_COLON;
  731. }
  732. else
  733. {
  734. sp = str;
  735. state = STATE_ADDR;
  736. }
  737. continue;
  738. case STATE_COLON:
  739. colons++;
  740. if (*(str + 1) == ':')
  741. state = STATE_DOUBLE;
  742. else
  743. {
  744. sp = str + 1;
  745. state = STATE_ADDR;
  746. }
  747. break;
  748. case STATE_DOUBLE:
  749. if (double_colon)
  750. return no_match;
  751. if (*(str + 1) == ':')
  752. return no_match;
  753. else
  754. {
  755. if (*(str + 1) != '\0')
  756. colons++;
  757. sp = str + 1;
  758. state = STATE_ADDR;
  759. }
  760. double_colon++;
  761. nums++;
  762. break;
  763. case STATE_ADDR:
  764. if (*(str + 1) == ':' || *(str + 1) == '\0')
  765. {
  766. if (str - sp > 3)
  767. return no_match;
  768. nums++;
  769. state = STATE_COLON;
  770. }
  771. if (*(str + 1) == '.')
  772. state = STATE_DOT;
  773. break;
  774. case STATE_DOT:
  775. state = STATE_ADDR;
  776. break;
  777. default:
  778. break;
  779. }
  780. if (nums > 8)
  781. return no_match;
  782. if (colons > 7)
  783. return no_match;
  784. str++;
  785. }
  786. #if 0
  787. if (nums < 11)
  788. return partly_match;
  789. #endif /* 0 */
  790. return exact_match;
  791. }
  792. static enum match_type
  793. cmd_ipv6_prefix_match (const char *str)
  794. {
  795. int state = STATE_START;
  796. int colons = 0, nums = 0, double_colon = 0;
  797. int mask;
  798. const char *sp = NULL;
  799. char *endptr = NULL;
  800. if (str == NULL)
  801. return partly_match;
  802. if (strspn (str, IPV6_PREFIX_STR) != strlen (str))
  803. return no_match;
  804. while (*str != '\0' && state != STATE_MASK)
  805. {
  806. switch (state)
  807. {
  808. case STATE_START:
  809. if (*str == ':')
  810. {
  811. if (*(str + 1) != ':' && *(str + 1) != '\0')
  812. return no_match;
  813. colons--;
  814. state = STATE_COLON;
  815. }
  816. else
  817. {
  818. sp = str;
  819. state = STATE_ADDR;
  820. }
  821. continue;
  822. case STATE_COLON:
  823. colons++;
  824. if (*(str + 1) == '/')
  825. return no_match;
  826. else if (*(str + 1) == ':')
  827. state = STATE_DOUBLE;
  828. else
  829. {
  830. sp = str + 1;
  831. state = STATE_ADDR;
  832. }
  833. break;
  834. case STATE_DOUBLE:
  835. if (double_colon)
  836. return no_match;
  837. if (*(str + 1) == ':')
  838. return no_match;
  839. else
  840. {
  841. if (*(str + 1) != '\0' && *(str + 1) != '/')
  842. colons++;
  843. sp = str + 1;
  844. if (*(str + 1) == '/')
  845. state = STATE_SLASH;
  846. else
  847. state = STATE_ADDR;
  848. }
  849. double_colon++;
  850. nums += 1;
  851. break;
  852. case STATE_ADDR:
  853. if (*(str + 1) == ':' || *(str + 1) == '.'
  854. || *(str + 1) == '\0' || *(str + 1) == '/')
  855. {
  856. if (str - sp > 3)
  857. return no_match;
  858. for (; sp <= str; sp++)
  859. if (*sp == '/')
  860. return no_match;
  861. nums++;
  862. if (*(str + 1) == ':')
  863. state = STATE_COLON;
  864. else if (*(str + 1) == '.')
  865. state = STATE_DOT;
  866. else if (*(str + 1) == '/')
  867. state = STATE_SLASH;
  868. }
  869. break;
  870. case STATE_DOT:
  871. state = STATE_ADDR;
  872. break;
  873. case STATE_SLASH:
  874. if (*(str + 1) == '\0')
  875. return partly_match;
  876. state = STATE_MASK;
  877. break;
  878. default:
  879. break;
  880. }
  881. if (nums > 11)
  882. return no_match;
  883. if (colons > 7)
  884. return no_match;
  885. str++;
  886. }
  887. if (state < STATE_MASK)
  888. return partly_match;
  889. mask = strtol (str, &endptr, 10);
  890. if (*endptr != '\0')
  891. return no_match;
  892. if (mask < 0 || mask > 128)
  893. return no_match;
  894. /* I don't know why mask < 13 makes command match partly.
  895. Forgive me to make this comments. I Want to set static default route
  896. because of lack of function to originate default in ospf6d; sorry
  897. yasu
  898. if (mask < 13)
  899. return partly_match;
  900. */
  901. return exact_match;
  902. }
  903. #endif /* HAVE_IPV6 */
  904. #define DECIMAL_STRLEN_MAX 10
  905. static int
  906. cmd_range_match (const char *range, const char *str)
  907. {
  908. char *p;
  909. char buf[DECIMAL_STRLEN_MAX + 1];
  910. char *endptr = NULL;
  911. unsigned long min, max, val;
  912. if (str == NULL)
  913. return 1;
  914. val = strtoul (str, &endptr, 10);
  915. if (*endptr != '\0')
  916. return 0;
  917. range++;
  918. p = strchr (range, '-');
  919. if (p == NULL)
  920. return 0;
  921. if (p - range > DECIMAL_STRLEN_MAX)
  922. return 0;
  923. strncpy (buf, range, p - range);
  924. buf[p - range] = '\0';
  925. min = strtoul (buf, &endptr, 10);
  926. if (*endptr != '\0')
  927. return 0;
  928. range = p + 1;
  929. p = strchr (range, '>');
  930. if (p == NULL)
  931. return 0;
  932. if (p - range > DECIMAL_STRLEN_MAX)
  933. return 0;
  934. strncpy (buf, range, p - range);
  935. buf[p - range] = '\0';
  936. max = strtoul (buf, &endptr, 10);
  937. if (*endptr != '\0')
  938. return 0;
  939. if (val < min || val > max)
  940. return 0;
  941. return 1;
  942. }
  943. /* Make completion match and return match type flag. */
  944. static enum match_type
  945. cmd_filter_by_completion (char *command, vector v, unsigned int index)
  946. {
  947. unsigned int i;
  948. const char *str;
  949. struct cmd_element *cmd_element;
  950. enum match_type match_type;
  951. vector descvec;
  952. struct desc *desc;
  953. match_type = no_match;
  954. /* If command and cmd_element string does not match set NULL to vector */
  955. for (i = 0; i < vector_active (v); i++)
  956. if ((cmd_element = vector_slot (v, i)) != NULL)
  957. {
  958. if (index >= vector_active (cmd_element->strvec))
  959. vector_slot (v, i) = NULL;
  960. else
  961. {
  962. unsigned int j;
  963. int matched = 0;
  964. descvec = vector_slot (cmd_element->strvec, index);
  965. for (j = 0; j < vector_active (descvec); j++)
  966. if ((desc = vector_slot (descvec, j)))
  967. {
  968. str = desc->cmd;
  969. if (CMD_VARARG (str))
  970. {
  971. if (match_type < vararg_match)
  972. match_type = vararg_match;
  973. matched++;
  974. }
  975. else if (CMD_RANGE (str))
  976. {
  977. if (cmd_range_match (str, command))
  978. {
  979. if (match_type < range_match)
  980. match_type = range_match;
  981. matched++;
  982. }
  983. }
  984. #ifdef HAVE_IPV6
  985. else if (CMD_IPV6 (str))
  986. {
  987. if (cmd_ipv6_match (command))
  988. {
  989. if (match_type < ipv6_match)
  990. match_type = ipv6_match;
  991. matched++;
  992. }
  993. }
  994. else if (CMD_IPV6_PREFIX (str))
  995. {
  996. if (cmd_ipv6_prefix_match (command))
  997. {
  998. if (match_type < ipv6_prefix_match)
  999. match_type = ipv6_prefix_match;
  1000. matched++;
  1001. }
  1002. }
  1003. #endif /* HAVE_IPV6 */
  1004. else if (CMD_IPV4 (str))
  1005. {
  1006. if (cmd_ipv4_match (command))
  1007. {
  1008. if (match_type < ipv4_match)
  1009. match_type = ipv4_match;
  1010. matched++;
  1011. }
  1012. }
  1013. else if (CMD_IPV4_PREFIX (str))
  1014. {
  1015. if (cmd_ipv4_prefix_match (command))
  1016. {
  1017. if (match_type < ipv4_prefix_match)
  1018. match_type = ipv4_prefix_match;
  1019. matched++;
  1020. }
  1021. }
  1022. else
  1023. /* Check is this point's argument optional ? */
  1024. if (CMD_OPTION (str) || CMD_VARIABLE (str))
  1025. {
  1026. if (match_type < extend_match)
  1027. match_type = extend_match;
  1028. matched++;
  1029. }
  1030. else if (strncmp (command, str, strlen (command)) == 0)
  1031. {
  1032. if (strcmp (command, str) == 0)
  1033. match_type = exact_match;
  1034. else
  1035. {
  1036. if (match_type < partly_match)
  1037. match_type = partly_match;
  1038. }
  1039. matched++;
  1040. }
  1041. }
  1042. if (!matched)
  1043. vector_slot (v, i) = NULL;
  1044. }
  1045. }
  1046. return match_type;
  1047. }
  1048. /* Filter vector by command character with index. */
  1049. static enum match_type
  1050. cmd_filter_by_string (char *command, vector v, unsigned int index)
  1051. {
  1052. unsigned int i;
  1053. const char *str;
  1054. struct cmd_element *cmd_element;
  1055. enum match_type match_type;
  1056. vector descvec;
  1057. struct desc *desc;
  1058. match_type = no_match;
  1059. /* If command and cmd_element string does not match set NULL to vector */
  1060. for (i = 0; i < vector_active (v); i++)
  1061. if ((cmd_element = vector_slot (v, i)) != NULL)
  1062. {
  1063. /* If given index is bigger than max string vector of command,
  1064. set NULL */
  1065. if (index >= vector_active (cmd_element->strvec))
  1066. vector_slot (v, i) = NULL;
  1067. else
  1068. {
  1069. unsigned int j;
  1070. int matched = 0;
  1071. descvec = vector_slot (cmd_element->strvec, index);
  1072. for (j = 0; j < vector_active (descvec); j++)
  1073. if ((desc = vector_slot (descvec, j)))
  1074. {
  1075. str = desc->cmd;
  1076. if (CMD_VARARG (str))
  1077. {
  1078. if (match_type < vararg_match)
  1079. match_type = vararg_match;
  1080. matched++;
  1081. }
  1082. else if (CMD_RANGE (str))
  1083. {
  1084. if (cmd_range_match (str, command))
  1085. {
  1086. if (match_type < range_match)
  1087. match_type = range_match;
  1088. matched++;
  1089. }
  1090. }
  1091. #ifdef HAVE_IPV6
  1092. else if (CMD_IPV6 (str))
  1093. {
  1094. if (cmd_ipv6_match (command) == exact_match)
  1095. {
  1096. if (match_type < ipv6_match)
  1097. match_type = ipv6_match;
  1098. matched++;
  1099. }
  1100. }
  1101. else if (CMD_IPV6_PREFIX (str))
  1102. {
  1103. if (cmd_ipv6_prefix_match (command) == exact_match)
  1104. {
  1105. if (match_type < ipv6_prefix_match)
  1106. match_type = ipv6_prefix_match;
  1107. matched++;
  1108. }
  1109. }
  1110. #endif /* HAVE_IPV6 */
  1111. else if (CMD_IPV4 (str))
  1112. {
  1113. if (cmd_ipv4_match (command) == exact_match)
  1114. {
  1115. if (match_type < ipv4_match)
  1116. match_type = ipv4_match;
  1117. matched++;
  1118. }
  1119. }
  1120. else if (CMD_IPV4_PREFIX (str))
  1121. {
  1122. if (cmd_ipv4_prefix_match (command) == exact_match)
  1123. {
  1124. if (match_type < ipv4_prefix_match)
  1125. match_type = ipv4_prefix_match;
  1126. matched++;
  1127. }
  1128. }
  1129. else if (CMD_OPTION (str) || CMD_VARIABLE (str))
  1130. {
  1131. if (match_type < extend_match)
  1132. match_type = extend_match;
  1133. matched++;
  1134. }
  1135. else
  1136. {
  1137. if (strcmp (command, str) == 0)
  1138. {
  1139. match_type = exact_match;
  1140. matched++;
  1141. }
  1142. }
  1143. }
  1144. if (!matched)
  1145. vector_slot (v, i) = NULL;
  1146. }
  1147. }
  1148. return match_type;
  1149. }
  1150. /* Check ambiguous match */
  1151. static int
  1152. is_cmd_ambiguous (char *command, vector v, int index, enum match_type type)
  1153. {
  1154. unsigned int i;
  1155. unsigned int j;
  1156. const char *str = NULL;
  1157. struct cmd_element *cmd_element;
  1158. const char *matched = NULL;
  1159. vector descvec;
  1160. struct desc *desc;
  1161. for (i = 0; i < vector_active (v); i++)
  1162. if ((cmd_element = vector_slot (v, i)) != NULL)
  1163. {
  1164. int match = 0;
  1165. descvec = vector_slot (cmd_element->strvec, index);
  1166. for (j = 0; j < vector_active (descvec); j++)
  1167. if ((desc = vector_slot (descvec, j)))
  1168. {
  1169. enum match_type ret;
  1170. str = desc->cmd;
  1171. switch (type)
  1172. {
  1173. case exact_match:
  1174. if (!(CMD_OPTION (str) || CMD_VARIABLE (str))
  1175. && strcmp (command, str) == 0)
  1176. match++;
  1177. break;
  1178. case partly_match:
  1179. if (!(CMD_OPTION (str) || CMD_VARIABLE (str))
  1180. && strncmp (command, str, strlen (command)) == 0)
  1181. {
  1182. if (matched && strcmp (matched, str) != 0)
  1183. return 1; /* There is ambiguous match. */
  1184. else
  1185. matched = str;
  1186. match++;
  1187. }
  1188. break;
  1189. case range_match:
  1190. if (cmd_range_match (str, command))
  1191. {
  1192. if (matched && strcmp (matched, str) != 0)
  1193. return 1;
  1194. else
  1195. matched = str;
  1196. match++;
  1197. }
  1198. break;
  1199. #ifdef HAVE_IPV6
  1200. case ipv6_match:
  1201. if (CMD_IPV6 (str))
  1202. match++;
  1203. break;
  1204. case ipv6_prefix_match:
  1205. if ((ret = cmd_ipv6_prefix_match (command)) != no_match)
  1206. {
  1207. if (ret == partly_match)
  1208. return 2; /* There is incomplete match. */
  1209. match++;
  1210. }
  1211. break;
  1212. #endif /* HAVE_IPV6 */
  1213. case ipv4_match:
  1214. if (CMD_IPV4 (str))
  1215. match++;
  1216. break;
  1217. case ipv4_prefix_match:
  1218. if ((ret = cmd_ipv4_prefix_match (command)) != no_match)
  1219. {
  1220. if (ret == partly_match)
  1221. return 2; /* There is incomplete match. */
  1222. match++;
  1223. }
  1224. break;
  1225. case extend_match:
  1226. if (CMD_OPTION (str) || CMD_VARIABLE (str))
  1227. match++;
  1228. break;
  1229. case no_match:
  1230. default:
  1231. break;
  1232. }
  1233. }
  1234. if (!match)
  1235. vector_slot (v, i) = NULL;
  1236. }
  1237. return 0;
  1238. }
  1239. /* If src matches dst return dst string, otherwise return NULL */
  1240. static const char *
  1241. cmd_entry_function (const char *src, const char *dst)
  1242. {
  1243. /* Skip variable arguments. */
  1244. if (CMD_OPTION (dst) || CMD_VARIABLE (dst) || CMD_VARARG (dst) ||
  1245. CMD_IPV4 (dst) || CMD_IPV4_PREFIX (dst) || CMD_RANGE (dst))
  1246. return NULL;
  1247. /* In case of 'command \t', given src is NULL string. */
  1248. if (src == NULL)
  1249. return dst;
  1250. /* Matched with input string. */
  1251. if (strncmp (src, dst, strlen (src)) == 0)
  1252. return dst;
  1253. return NULL;
  1254. }
  1255. /* If src matches dst return dst string, otherwise return NULL */
  1256. /* This version will return the dst string always if it is
  1257. CMD_VARIABLE for '?' key processing */
  1258. static const char *
  1259. cmd_entry_function_desc (const char *src, const char *dst)
  1260. {
  1261. if (CMD_VARARG (dst))
  1262. return dst;
  1263. if (CMD_RANGE (dst))
  1264. {
  1265. if (cmd_range_match (dst, src))
  1266. return dst;
  1267. else
  1268. return NULL;
  1269. }
  1270. #ifdef HAVE_IPV6
  1271. if (CMD_IPV6 (dst))
  1272. {
  1273. if (cmd_ipv6_match (src))
  1274. return dst;
  1275. else
  1276. return NULL;
  1277. }
  1278. if (CMD_IPV6_PREFIX (dst))
  1279. {
  1280. if (cmd_ipv6_prefix_match (src))
  1281. return dst;
  1282. else
  1283. return NULL;
  1284. }
  1285. #endif /* HAVE_IPV6 */
  1286. if (CMD_IPV4 (dst))
  1287. {
  1288. if (cmd_ipv4_match (src))
  1289. return dst;
  1290. else
  1291. return NULL;
  1292. }
  1293. if (CMD_IPV4_PREFIX (dst))
  1294. {
  1295. if (cmd_ipv4_prefix_match (src))
  1296. return dst;
  1297. else
  1298. return NULL;
  1299. }
  1300. /* Optional or variable commands always match on '?' */
  1301. if (CMD_OPTION (dst) || CMD_VARIABLE (dst))
  1302. return dst;
  1303. /* In case of 'command \t', given src is NULL string. */
  1304. if (src == NULL)
  1305. return dst;
  1306. if (strncmp (src, dst, strlen (src)) == 0)
  1307. return dst;
  1308. else
  1309. return NULL;
  1310. }
  1311. /* Check same string element existence. If it isn't there return
  1312. 1. */
  1313. static int
  1314. cmd_unique_string (vector v, const char *str)
  1315. {
  1316. unsigned int i;
  1317. char *match;
  1318. for (i = 0; i < vector_active (v); i++)
  1319. if ((match = vector_slot (v, i)) != NULL)
  1320. if (strcmp (match, str) == 0)
  1321. return 0;
  1322. return 1;
  1323. }
  1324. /* Compare string to description vector. If there is same string
  1325. return 1 else return 0. */
  1326. static int
  1327. desc_unique_string (vector v, const char *str)
  1328. {
  1329. unsigned int i;
  1330. struct desc *desc;
  1331. for (i = 0; i < vector_active (v); i++)
  1332. if ((desc = vector_slot (v, i)) != NULL)
  1333. if (strcmp (desc->cmd, str) == 0)
  1334. return 1;
  1335. return 0;
  1336. }
  1337. static int
  1338. cmd_try_do_shortcut (enum node_type node, char* first_word) {
  1339. if ( first_word != NULL &&
  1340. node != AUTH_NODE &&
  1341. node != VIEW_NODE &&
  1342. node != AUTH_ENABLE_NODE &&
  1343. node != ENABLE_NODE &&
  1344. node != RESTRICTED_NODE &&
  1345. 0 == strcmp( "do", first_word ) )
  1346. return 1;
  1347. return 0;
  1348. }
  1349. /* '?' describe command support. */
  1350. static vector
  1351. cmd_describe_command_real (vector vline, struct vty *vty, int *status)
  1352. {
  1353. unsigned int i;
  1354. vector cmd_vector;
  1355. #define INIT_MATCHVEC_SIZE 10
  1356. vector matchvec;
  1357. struct cmd_element *cmd_element;
  1358. unsigned int index;
  1359. int ret;
  1360. enum match_type match;
  1361. char *command;
  1362. /* Set index. */
  1363. if (vector_active (vline) == 0)
  1364. {
  1365. *status = CMD_ERR_NO_MATCH;
  1366. return NULL;
  1367. }
  1368. else
  1369. index = vector_active (vline) - 1;
  1370. /* Make copy vector of current node's command vector. */
  1371. cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1372. /* Prepare match vector */
  1373. matchvec = vector_init (INIT_MATCHVEC_SIZE);
  1374. /* Filter commands. */
  1375. /* Only words precedes current word will be checked in this loop. */
  1376. for (i = 0; i < index; i++)
  1377. if ((command = vector_slot (vline, i)))
  1378. {
  1379. match = cmd_filter_by_completion (command, cmd_vector, i);
  1380. if (match == vararg_match)
  1381. {
  1382. struct cmd_element *cmd_element;
  1383. vector descvec;
  1384. unsigned int j, k;
  1385. for (j = 0; j < vector_active (cmd_vector); j++)
  1386. if ((cmd_element = vector_slot (cmd_vector, j)) != NULL
  1387. && (vector_active (cmd_element->strvec)))
  1388. {
  1389. descvec = vector_slot (cmd_element->strvec,
  1390. vector_active (cmd_element->strvec) - 1);
  1391. for (k = 0; k < vector_active (descvec); k++)
  1392. {
  1393. struct desc *desc = vector_slot (descvec, k);
  1394. vector_set (matchvec, desc);
  1395. }
  1396. }
  1397. vector_set (matchvec, &desc_cr);
  1398. vector_free (cmd_vector);
  1399. return matchvec;
  1400. }
  1401. if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1)
  1402. {
  1403. vector_free (cmd_vector);
  1404. vector_free (matchvec);
  1405. *status = CMD_ERR_AMBIGUOUS;
  1406. return NULL;
  1407. }
  1408. else if (ret == 2)
  1409. {
  1410. vector_free (cmd_vector);
  1411. vector_free (matchvec);
  1412. *status = CMD_ERR_NO_MATCH;
  1413. return NULL;
  1414. }
  1415. }
  1416. /* Prepare match vector */
  1417. /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
  1418. /* Make sure that cmd_vector is filtered based on current word */
  1419. command = vector_slot (vline, index);
  1420. if (command)
  1421. match = cmd_filter_by_completion (command, cmd_vector, index);
  1422. /* Make description vector. */
  1423. for (i = 0; i < vector_active (cmd_vector); i++)
  1424. if ((cmd_element = vector_slot (cmd_vector, i)) != NULL)
  1425. {
  1426. vector strvec = cmd_element->strvec;
  1427. /* if command is NULL, index may be equal to vector_active */
  1428. if (command && index >= vector_active (strvec))
  1429. vector_slot (cmd_vector, i) = NULL;
  1430. else
  1431. {
  1432. /* Check if command is completed. */
  1433. if (command == NULL && index == vector_active (strvec))
  1434. {
  1435. if (!desc_unique_string (matchvec, command_cr))
  1436. vector_set (matchvec, &desc_cr);
  1437. }
  1438. else
  1439. {
  1440. unsigned int j;
  1441. vector descvec = vector_slot (strvec, index);
  1442. struct desc *desc;
  1443. for (j = 0; j < vector_active (descvec); j++)
  1444. if ((desc = vector_slot (descvec, j)))
  1445. {
  1446. const char *string;
  1447. string = cmd_entry_function_desc (command, desc->cmd);
  1448. if (string)
  1449. {
  1450. /* Uniqueness check */
  1451. if (!desc_unique_string (matchvec, string))
  1452. vector_set (matchvec, desc);
  1453. }
  1454. }
  1455. }
  1456. }
  1457. }
  1458. vector_free (cmd_vector);
  1459. if (vector_slot (matchvec, 0) == NULL)
  1460. {
  1461. vector_free (matchvec);
  1462. *status = CMD_ERR_NO_MATCH;
  1463. return NULL;
  1464. }
  1465. *status = CMD_SUCCESS;
  1466. return matchvec;
  1467. }
  1468. vector
  1469. cmd_describe_command (vector vline, struct vty *vty, int *status)
  1470. {
  1471. vector ret;
  1472. if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
  1473. {
  1474. enum node_type onode;
  1475. vector shifted_vline;
  1476. unsigned int index;
  1477. onode = vty->node;
  1478. vty->node = ENABLE_NODE;
  1479. /* We can try it on enable node, cos' the vty is authenticated */
  1480. shifted_vline = vector_init (vector_count(vline));
  1481. /* use memcpy? */
  1482. for (index = 1; index < vector_active (vline); index++)
  1483. {
  1484. vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
  1485. }
  1486. ret = cmd_describe_command_real (shifted_vline, vty, status);
  1487. vector_free(shifted_vline);
  1488. vty->node = onode;
  1489. return ret;
  1490. }
  1491. return cmd_describe_command_real (vline, vty, status);
  1492. }
  1493. /* Check LCD of matched command. */
  1494. static int
  1495. cmd_lcd (char **matched)
  1496. {
  1497. int i;
  1498. int j;
  1499. int lcd = -1;
  1500. char *s1, *s2;
  1501. char c1, c2;
  1502. if (matched[0] == NULL || matched[1] == NULL)
  1503. return 0;
  1504. for (i = 1; matched[i] != NULL; i++)
  1505. {
  1506. s1 = matched[i - 1];
  1507. s2 = matched[i];
  1508. for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
  1509. if (c1 != c2)
  1510. break;
  1511. if (lcd < 0)
  1512. lcd = j;
  1513. else
  1514. {
  1515. if (lcd > j)
  1516. lcd = j;
  1517. }
  1518. }
  1519. return lcd;
  1520. }
  1521. /* Command line completion support. */
  1522. static char **
  1523. cmd_complete_command_real (vector vline, struct vty *vty, int *status)
  1524. {
  1525. unsigned int i;
  1526. vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1527. #define INIT_MATCHVEC_SIZE 10
  1528. vector matchvec;
  1529. struct cmd_element *cmd_element;
  1530. unsigned int index;
  1531. char **match_str;
  1532. struct desc *desc;
  1533. vector descvec;
  1534. char *command;
  1535. int lcd;
  1536. if (vector_active (vline) == 0)
  1537. {
  1538. vector_free (cmd_vector);
  1539. *status = CMD_ERR_NO_MATCH;
  1540. return NULL;
  1541. }
  1542. else
  1543. index = vector_active (vline) - 1;
  1544. /* First, filter by preceeding command string */
  1545. for (i = 0; i < index; i++)
  1546. if ((command = vector_slot (vline, i)))
  1547. {
  1548. enum match_type match;
  1549. int ret;
  1550. /* First try completion match, if there is exactly match return 1 */
  1551. match = cmd_filter_by_completion (command, cmd_vector, i);
  1552. /* If there is exact match then filter ambiguous match else check
  1553. ambiguousness. */
  1554. if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1)
  1555. {
  1556. vector_free (cmd_vector);
  1557. *status = CMD_ERR_AMBIGUOUS;
  1558. return NULL;
  1559. }
  1560. /*
  1561. else if (ret == 2)
  1562. {
  1563. vector_free (cmd_vector);
  1564. *status = CMD_ERR_NO_MATCH;
  1565. return NULL;
  1566. }
  1567. */
  1568. }
  1569. /* Prepare match vector. */
  1570. matchvec = vector_init (INIT_MATCHVEC_SIZE);
  1571. /* Now we got into completion */
  1572. for (i = 0; i < vector_active (cmd_vector); i++)
  1573. if ((cmd_element = vector_slot (cmd_vector, i)))
  1574. {
  1575. const char *string;
  1576. vector strvec = cmd_element->strvec;
  1577. /* Check field length */
  1578. if (index >= vector_active (strvec))
  1579. vector_slot (cmd_vector, i) = NULL;
  1580. else
  1581. {
  1582. unsigned int j;
  1583. descvec = vector_slot (strvec, index);
  1584. for (j = 0; j < vector_active (descvec); j++)
  1585. if ((desc = vector_slot (descvec, j)))
  1586. {
  1587. if ((string =
  1588. cmd_entry_function (vector_slot (vline, index),
  1589. desc->cmd)))
  1590. if (cmd_unique_string (matchvec, string))
  1591. vector_set (matchvec, XSTRDUP (MTYPE_TMP, string));
  1592. }
  1593. }
  1594. }
  1595. /* We don't need cmd_vector any more. */
  1596. vector_free (cmd_vector);
  1597. /* No matched command */
  1598. if (vector_slot (matchvec, 0) == NULL)
  1599. {
  1600. vector_free (matchvec);
  1601. /* In case of 'command \t' pattern. Do you need '?' command at
  1602. the end of the line. */
  1603. if (vector_slot (vline, index) == '\0')
  1604. *status = CMD_ERR_NOTHING_TODO;
  1605. else
  1606. *status = CMD_ERR_NO_MATCH;
  1607. return NULL;
  1608. }
  1609. /* Only one matched */
  1610. if (vector_slot (matchvec, 1) == NULL)
  1611. {
  1612. match_str = (char **) matchvec->index;
  1613. vector_only_wrapper_free (matchvec);
  1614. *status = CMD_COMPLETE_FULL_MATCH;
  1615. return match_str;
  1616. }
  1617. /* Make it sure last element is NULL. */
  1618. vector_set (matchvec, NULL);
  1619. /* Check LCD of matched strings. */
  1620. if (vector_slot (vline, index) != NULL)
  1621. {
  1622. lcd = cmd_lcd ((char **) matchvec->index);
  1623. if (lcd)
  1624. {
  1625. int len = strlen (vector_slot (vline, index));
  1626. if (len < lcd)
  1627. {
  1628. char *lcdstr;
  1629. lcdstr = XMALLOC (MTYPE_STRVEC, lcd + 1);
  1630. memcpy (lcdstr, matchvec->index[0], lcd);
  1631. lcdstr[lcd] = '\0';
  1632. /* match_str = (char **) &lcdstr; */
  1633. /* Free matchvec. */
  1634. for (i = 0; i < vector_active (matchvec); i++)
  1635. {
  1636. if (vector_slot (matchvec, i))
  1637. XFREE (MTYPE_STRVEC, vector_slot (matchvec, i));
  1638. }
  1639. vector_free (matchvec);
  1640. /* Make new matchvec. */
  1641. matchvec = vector_init (INIT_MATCHVEC_SIZE);
  1642. vector_set (matchvec, lcdstr);
  1643. match_str = (char **) matchvec->index;
  1644. vector_only_wrapper_free (matchvec);
  1645. *status = CMD_COMPLETE_MATCH;
  1646. return match_str;
  1647. }
  1648. }
  1649. }
  1650. match_str = (char **) matchvec->index;
  1651. vector_only_wrapper_free (matchvec);
  1652. *status = CMD_COMPLETE_LIST_MATCH;
  1653. return match_str;
  1654. }
  1655. char **
  1656. cmd_complete_command (vector vline, struct vty *vty, int *status)
  1657. {
  1658. char **ret;
  1659. if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
  1660. {
  1661. enum node_type onode;
  1662. vector shifted_vline;
  1663. unsigned int index;
  1664. onode = vty->node;
  1665. vty->node = ENABLE_NODE;
  1666. /* We can try it on enable node, cos' the vty is authenticated */
  1667. shifted_vline = vector_init (vector_count(vline));
  1668. /* use memcpy? */
  1669. for (index = 1; index < vector_active (vline); index++)
  1670. {
  1671. vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
  1672. }
  1673. ret = cmd_complete_command_real (shifted_vline, vty, status);
  1674. vector_free(shifted_vline);
  1675. vty->node = onode;
  1676. return ret;
  1677. }
  1678. return cmd_complete_command_real (vline, vty, status);
  1679. }
  1680. /* return parent node */
  1681. /* MUST eventually converge on CONFIG_NODE */
  1682. enum node_type
  1683. node_parent ( enum node_type node )
  1684. {
  1685. enum node_type ret;
  1686. assert (node > CONFIG_NODE);
  1687. switch (node)
  1688. {
  1689. case BGP_VPNV4_NODE:
  1690. case BGP_IPV4_NODE:
  1691. case BGP_IPV4M_NODE:
  1692. case BGP_IPV6_NODE:
  1693. case BGP_IPV6M_NODE:
  1694. ret = BGP_NODE;
  1695. break;
  1696. case KEYCHAIN_KEY_NODE:
  1697. ret = KEYCHAIN_NODE;
  1698. break;
  1699. default:
  1700. ret = CONFIG_NODE;
  1701. }
  1702. return ret;
  1703. }
  1704. /* Execute command by argument vline vector. */
  1705. static int
  1706. cmd_execute_command_real (vector vline, struct vty *vty,
  1707. struct cmd_element **cmd)
  1708. {
  1709. unsigned int i;
  1710. unsigned int index;
  1711. vector cmd_vector;
  1712. struct cmd_element *cmd_element;
  1713. struct cmd_element *matched_element;
  1714. unsigned int matched_count, incomplete_count;
  1715. int argc;
  1716. const char *argv[CMD_ARGC_MAX];
  1717. enum match_type match = 0;
  1718. int varflag;
  1719. char *command;
  1720. /* Make copy of command elements. */
  1721. cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1722. for (index = 0; index < vector_active (vline); index++)
  1723. if ((command = vector_slot (vline, index)))
  1724. {
  1725. int ret;
  1726. match = cmd_filter_by_completion (command, cmd_vector, index);
  1727. if (match == vararg_match)
  1728. break;
  1729. ret = is_cmd_ambiguous (command, cmd_vector, index, match);
  1730. if (ret == 1)
  1731. {
  1732. vector_free (cmd_vector);
  1733. return CMD_ERR_AMBIGUOUS;
  1734. }
  1735. else if (ret == 2)
  1736. {
  1737. vector_free (cmd_vector);
  1738. return CMD_ERR_NO_MATCH;
  1739. }
  1740. }
  1741. /* Check matched count. */
  1742. matched_element = NULL;
  1743. matched_count = 0;
  1744. incomplete_count = 0;
  1745. for (i = 0; i < vector_active (cmd_vector); i++)
  1746. if ((cmd_element = vector_slot (cmd_vector, i)))
  1747. {
  1748. if (match == vararg_match || index >= cmd_element->cmdsize)
  1749. {
  1750. matched_element = cmd_element;
  1751. #if 0
  1752. printf ("DEBUG: %s\n", cmd_element->string);
  1753. #endif
  1754. matched_count++;
  1755. }
  1756. else
  1757. {
  1758. incomplete_count++;
  1759. }
  1760. }
  1761. /* Finish of using cmd_vector. */
  1762. vector_free (cmd_vector);
  1763. /* To execute command, matched_count must be 1. */
  1764. if (matched_count == 0)
  1765. {
  1766. if (incomplete_count)
  1767. return CMD_ERR_INCOMPLETE;
  1768. else
  1769. return CMD_ERR_NO_MATCH;
  1770. }
  1771. if (matched_count > 1)
  1772. return CMD_ERR_AMBIGUOUS;
  1773. /* Argument treatment */
  1774. varflag = 0;
  1775. argc = 0;
  1776. for (i = 0; i < vector_active (vline); i++)
  1777. {
  1778. if (varflag)
  1779. argv[argc++] = vector_slot (vline, i);
  1780. else
  1781. {
  1782. vector descvec = vector_slot (matched_element->strvec, i);
  1783. if (vector_active (descvec) == 1)
  1784. {
  1785. struct desc *desc = vector_slot (descvec, 0);
  1786. if (CMD_VARARG (desc->cmd))
  1787. varflag = 1;
  1788. if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd))
  1789. argv[argc++] = vector_slot (vline, i);
  1790. }
  1791. else
  1792. argv[argc++] = vector_slot (vline, i);
  1793. }
  1794. if (argc >= CMD_ARGC_MAX)
  1795. return CMD_ERR_EXEED_ARGC_MAX;
  1796. }
  1797. /* For vtysh execution. */
  1798. if (cmd)
  1799. *cmd = matched_element;
  1800. if (matched_element->daemon)
  1801. return CMD_SUCCESS_DAEMON;
  1802. /* Execute matched command. */
  1803. return (*matched_element->func) (matched_element, vty, argc, argv);
  1804. }
  1805. int
  1806. cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
  1807. int vtysh) {
  1808. int ret, saved_ret, tried = 0;
  1809. enum node_type onode, try_node;
  1810. onode = try_node = vty->node;
  1811. if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
  1812. {
  1813. vector shifted_vline;
  1814. unsigned int index;
  1815. vty->node = ENABLE_NODE;
  1816. /* We can try it on enable node, cos' the vty is authenticated */
  1817. shifted_vline = vector_init (vector_count(vline));
  1818. /* use memcpy? */
  1819. for (index = 1; index < vector_active (vline); index++)
  1820. {
  1821. vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
  1822. }
  1823. ret = cmd_execute_command_real (shifted_vline, vty, cmd);
  1824. vector_free(shifted_vline);
  1825. vty->node = onode;
  1826. return ret;
  1827. }
  1828. saved_ret = ret = cmd_execute_command_real (vline, vty, cmd);
  1829. if (vtysh)
  1830. return saved_ret;
  1831. /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
  1832. while ( ret != CMD_SUCCESS && ret != CMD_WARNING
  1833. && vty->node > CONFIG_NODE )
  1834. {
  1835. try_node = node_parent(try_node);
  1836. vty->node = try_node;
  1837. ret = cmd_execute_command_real (vline, vty, cmd);
  1838. tried = 1;
  1839. if (ret == CMD_SUCCESS || ret == CMD_WARNING)
  1840. {
  1841. /* succesfull command, leave the node as is */
  1842. return ret;
  1843. }
  1844. }
  1845. /* no command succeeded, reset the vty to the original node and
  1846. return the error for this node */
  1847. if ( tried )
  1848. vty->node = onode;
  1849. return saved_ret;
  1850. }
  1851. /* Execute command by argument readline. */
  1852. int
  1853. cmd_execute_command_strict (vector vline, struct vty *vty,
  1854. struct cmd_element **cmd)
  1855. {
  1856. unsigned int i;
  1857. unsigned int index;
  1858. vector cmd_vector;
  1859. struct cmd_element *cmd_element;
  1860. struct cmd_element *matched_element;
  1861. unsigned int matched_count, incomplete_count;
  1862. int argc;
  1863. const char *argv[CMD_ARGC_MAX];
  1864. int varflag;
  1865. enum match_type match = 0;
  1866. char *command;
  1867. /* Make copy of command element */
  1868. cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1869. for (index = 0; index < vector_active (vline); index++)
  1870. if ((command = vector_slot (vline, index)))
  1871. {
  1872. int ret;
  1873. match = cmd_filter_by_string (vector_slot (vline, index),
  1874. cmd_vector, index);
  1875. /* If command meets '.VARARG' then finish matching. */
  1876. if (match == vararg_match)
  1877. break;
  1878. ret = is_cmd_ambiguous (command, cmd_vector, index, match);
  1879. if (ret == 1)
  1880. {
  1881. vector_free (cmd_vector);
  1882. return CMD_ERR_AMBIGUOUS;
  1883. }
  1884. if (ret == 2)
  1885. {
  1886. vector_free (cmd_vector);
  1887. return CMD_ERR_NO_MATCH;
  1888. }
  1889. }
  1890. /* Check matched count. */
  1891. matched_element = NULL;
  1892. matched_count = 0;
  1893. incomplete_count = 0;
  1894. for (i = 0; i < vector_active (cmd_vector); i++)
  1895. if (vector_slot (cmd_vector, i) != NULL)
  1896. {
  1897. cmd_element = vector_slot (cmd_vector, i);
  1898. if (match == vararg_match || index >= cmd_element->cmdsize)
  1899. {
  1900. matched_element = cmd_element;
  1901. matched_count++;
  1902. }
  1903. else
  1904. incomplete_count++;
  1905. }
  1906. /* Finish of using cmd_vector. */
  1907. vector_free (cmd_vector);
  1908. /* To execute command, matched_count must be 1. */
  1909. if (matched_count == 0)
  1910. {
  1911. if (incomplete_count)
  1912. return CMD_ERR_INCOMPLETE;
  1913. else
  1914. return CMD_ERR_NO_MATCH;
  1915. }
  1916. if (matched_count > 1)
  1917. return CMD_ERR_AMBIGUOUS;
  1918. /* Argument treatment */
  1919. varflag = 0;
  1920. argc = 0;
  1921. for (i = 0; i < vector_active (vline); i++)
  1922. {
  1923. if (varflag)
  1924. argv[argc++] = vector_slot (vline, i);
  1925. else
  1926. {
  1927. vector descvec = vector_slot (matched_element->strvec, i);
  1928. if (vector_active (descvec) == 1)
  1929. {
  1930. struct desc *desc = vector_slot (descvec, 0);
  1931. if (CMD_VARARG (desc->cmd))
  1932. varflag = 1;
  1933. if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd))
  1934. argv[argc++] = vector_slot (vline, i);
  1935. }
  1936. else
  1937. argv[argc++] = vector_slot (vline, i);
  1938. }
  1939. if (argc >= CMD_ARGC_MAX)
  1940. return CMD_ERR_EXEED_ARGC_MAX;
  1941. }
  1942. /* For vtysh execution. */
  1943. if (cmd)
  1944. *cmd = matched_element;
  1945. if (matched_element->daemon)
  1946. return CMD_SUCCESS_DAEMON;
  1947. /* Now execute matched command */
  1948. return (*matched_element->func) (matched_element, vty, argc, argv);
  1949. }
  1950. /* Configration make from file. */
  1951. int
  1952. config_from_file (struct vty *vty, FILE *fp)
  1953. {
  1954. int ret;
  1955. vector vline;
  1956. while (fgets (vty->buf, VTY_BUFSIZ, fp))
  1957. {
  1958. vline = cmd_make_strvec (vty->buf);
  1959. /* In case of comment line */
  1960. if (vline == NULL)
  1961. continue;
  1962. /* Execute configuration command : this is strict match */
  1963. ret = cmd_execute_command_strict (vline, vty, NULL);
  1964. /* Try again with setting node to CONFIG_NODE */
  1965. while (ret != CMD_SUCCESS && ret != CMD_WARNING
  1966. && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE)
  1967. {
  1968. vty->node = node_parent(vty->node);
  1969. ret = cmd_execute_command_strict (vline, vty, NULL);
  1970. }
  1971. cmd_free_strvec (vline);
  1972. if (ret != CMD_SUCCESS && ret != CMD_WARNING
  1973. && ret != CMD_ERR_NOTHING_TODO)
  1974. return ret;
  1975. }
  1976. return CMD_SUCCESS;
  1977. }
  1978. /* Configration from terminal */
  1979. DEFUN (config_terminal,
  1980. config_terminal_cmd,
  1981. "configure terminal",
  1982. "Configuration from vty interface\n"
  1983. "Configuration terminal\n")
  1984. {
  1985. if (vty_config_lock (vty))
  1986. vty->node = CONFIG_NODE;
  1987. else
  1988. {
  1989. vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);
  1990. return CMD_WARNING;
  1991. }
  1992. return CMD_SUCCESS;
  1993. }
  1994. /* Enable command */
  1995. DEFUN (enable,
  1996. config_enable_cmd,
  1997. "enable",
  1998. "Turn on privileged mode command\n")
  1999. {
  2000. /* If enable password is NULL, change to ENABLE_NODE */
  2001. if ((host.enable == NULL && host.enable_encrypt == NULL) ||
  2002. vty->type == VTY_SHELL_SERV)
  2003. vty->node = ENABLE_NODE;
  2004. else
  2005. vty->node = AUTH_ENABLE_NODE;
  2006. return CMD_SUCCESS;
  2007. }
  2008. /* Disable command */
  2009. DEFUN (disable,
  2010. config_disable_cmd,
  2011. "disable",
  2012. "Turn off privileged mode command\n")
  2013. {
  2014. if (vty->node == ENABLE_NODE)
  2015. vty->node = VIEW_NODE;
  2016. return CMD_SUCCESS;
  2017. }
  2018. /* Down vty node level. */
  2019. DEFUN (config_exit,
  2020. config_exit_cmd,
  2021. "exit",
  2022. "Exit current mode and down to previous mode\n")
  2023. {
  2024. switch (vty->node)
  2025. {
  2026. case VIEW_NODE:
  2027. case ENABLE_NODE:
  2028. case RESTRICTED_NODE:
  2029. if (vty_shell (vty))
  2030. exit (0);
  2031. else
  2032. vty->status = VTY_CLOSE;
  2033. break;
  2034. case CONFIG_NODE:
  2035. vty->node = ENABLE_NODE;
  2036. vty_config_unlock (vty);
  2037. break;
  2038. case INTERFACE_NODE:
  2039. case ZEBRA_NODE:
  2040. case BGP_NODE:
  2041. case RIP_NODE:
  2042. case RIPNG_NODE:
  2043. case OSPF_NODE:
  2044. case OSPF6_NODE:
  2045. case ISIS_NODE:
  2046. case KEYCHAIN_NODE:
  2047. case MASC_NODE:
  2048. case RMAP_NODE:
  2049. case VTY_NODE:
  2050. vty->node = CONFIG_NODE;
  2051. break;
  2052. case BGP_VPNV4_NODE:
  2053. case BGP_IPV4_NODE:
  2054. case BGP_IPV4M_NODE:
  2055. case BGP_IPV6_NODE:
  2056. case BGP_IPV6M_NODE:
  2057. vty->node = BGP_NODE;
  2058. break;
  2059. case KEYCHAIN_KEY_NODE:
  2060. vty->node = KEYCHAIN_NODE;
  2061. break;
  2062. default:
  2063. break;
  2064. }
  2065. return CMD_SUCCESS;
  2066. }
  2067. /* quit is alias of exit. */
  2068. ALIAS (config_exit,
  2069. config_quit_cmd,
  2070. "quit",
  2071. "Exit current mode and down to previous mode\n")
  2072. /* End of configuration. */
  2073. DEFUN (config_end,
  2074. config_end_cmd,
  2075. "end",
  2076. "End current mode and change to enable mode.")
  2077. {
  2078. switch (vty->node)
  2079. {
  2080. case VIEW_NODE:
  2081. case ENABLE_NODE:
  2082. case RESTRICTED_NODE:
  2083. /* Nothing to do. */
  2084. break;
  2085. case CONFIG_NODE:
  2086. case INTERFACE_NODE:
  2087. case ZEBRA_NODE:
  2088. case RIP_NODE:
  2089. case RIPNG_NODE:
  2090. case BGP_NODE:
  2091. case BGP_VPNV4_NODE:
  2092. case BGP_IPV4_NODE:
  2093. case BGP_IPV4M_NODE:
  2094. case BGP_IPV6_NODE:
  2095. case BGP_IPV6M_NODE:
  2096. case RMAP_NODE:
  2097. case OSPF_NODE:
  2098. case OSPF6_NODE:
  2099. case ISIS_NODE:
  2100. case KEYCHAIN_NODE:
  2101. case KEYCHAIN_KEY_NODE:
  2102. case MASC_NODE:
  2103. case VTY_NODE:
  2104. vty_config_unlock (vty);
  2105. vty->node = ENABLE_NODE;
  2106. break;
  2107. default:
  2108. break;
  2109. }
  2110. return CMD_SUCCESS;
  2111. }
  2112. /* Show version. */
  2113. DEFUN (show_version,
  2114. show_version_cmd,
  2115. "show version",
  2116. SHOW_STR
  2117. "Displays zebra version\n")
  2118. {
  2119. vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"",
  2120. VTY_NEWLINE);
  2121. vty_out (vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE);
  2122. return CMD_SUCCESS;
  2123. }
  2124. /* Help display function for all node. */
  2125. DEFUN (config_help,
  2126. config_help_cmd,
  2127. "help",
  2128. "Description of the interactive help system\n")
  2129. {
  2130. vty_out (vty,
  2131. "Quagga VTY provides advanced help feature. When you need help,%s\
  2132. anytime at the command line please press '?'.%s\
  2133. %s\
  2134. If nothing matches, the help list will be empty and you must backup%s\
  2135. until entering a '?' shows the available options.%s\
  2136. Two styles of help are provided:%s\
  2137. 1. Full help is available when you are ready to enter a%s\
  2138. command argument (e.g. 'show ?') and describes each possible%s\
  2139. argument.%s\
  2140. 2. Partial help is provided when an abbreviated argument is entered%s\
  2141. and you want to know what arguments match the input%s\
  2142. (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
  2143. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
  2144. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
  2145. return CMD_SUCCESS;
  2146. }
  2147. /* Help display function for all node. */
  2148. DEFUN (config_list,
  2149. config_list_cmd,
  2150. "list",
  2151. "Print command list\n")
  2152. {
  2153. unsigned int i;
  2154. struct cmd_node *cnode = vector_slot (cmdvec, vty->node);
  2155. struct cmd_element *cmd;
  2156. for (i = 0; i < vector_active (cnode->cmd_vector); i++)
  2157. if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL
  2158. && !(cmd->attr == CMD_ATTR_DEPRECATED
  2159. || cmd->attr == CMD_ATTR_HIDDEN))
  2160. vty_out (vty, " %s%s", cmd->string,
  2161. VTY_NEWLINE);
  2162. return CMD_SUCCESS;
  2163. }
  2164. /* Write current configuration into file. */
  2165. DEFUN (config_write_file,
  2166. config_write_file_cmd,
  2167. "write file",
  2168. "Write running configuration to memory, network, or terminal\n"
  2169. "Write to configuration file\n")
  2170. {
  2171. unsigned int i;
  2172. int fd;
  2173. struct cmd_node *node;
  2174. char *config_file;
  2175. char *config_file_tmp = NULL;
  2176. char *config_file_sav = NULL;
  2177. int ret = CMD_WARNING;
  2178. struct vty *file_vty;
  2179. /* Check and see if we are operating under vtysh configuration */
  2180. if (host.config == NULL)
  2181. {
  2182. vty_out (vty, "Can't save to configuration file, using vtysh.%s",
  2183. VTY_NEWLINE);
  2184. return CMD_WARNING;
  2185. }
  2186. /* Get filename. */
  2187. config_file = host.config;
  2188. config_file_sav =
  2189. XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
  2190. strcpy (config_file_sav, config_file);
  2191. strcat (config_file_sav, CONF_BACKUP_EXT);
  2192. config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
  2193. sprintf (config_file_tmp, "%s.XXXXXX", config_file);
  2194. /* Open file to configuration write. */
  2195. fd = mkstemp (config_file_tmp);
  2196. if (fd < 0)
  2197. {
  2198. vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,
  2199. VTY_NEWLINE);
  2200. goto finished;
  2201. }
  2202. /* Make vty for configuration file. */
  2203. file_vty = vty_new ();
  2204. file_vty->fd = fd;
  2205. file_vty->type = VTY_FILE;
  2206. /* Config file header print. */
  2207. vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! ");
  2208. vty_time_print (file_vty, 1);
  2209. vty_out (file_vty, "!\n");
  2210. for (i = 0; i < vector_active (cmdvec); i++)
  2211. if ((node = vector_slot (cmdvec, i)) && node->func)
  2212. {
  2213. if ((*node->func) (file_vty))
  2214. vty_out (file_vty, "!\n");
  2215. }
  2216. vty_close (file_vty);
  2217. if (unlink (config_file_sav) != 0)
  2218. if (errno != ENOENT)
  2219. {
  2220. vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,
  2221. VTY_NEWLINE);
  2222. goto finished;
  2223. }
  2224. if (link (config_file, config_file_sav) != 0)
  2225. {
  2226. vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,
  2227. VTY_NEWLINE);
  2228. goto finished;
  2229. }
  2230. sync ();
  2231. if (unlink (config_file) != 0)
  2232. {
  2233. vty_out (vty, "Can't unlink configuration file %s.%s", config_file,
  2234. VTY_NEWLINE);
  2235. goto finished;
  2236. }
  2237. if (link (config_file_tmp, config_file) != 0)
  2238. {
  2239. vty_out (vty, "Can't save configuration file %s.%s", config_file,
  2240. VTY_NEWLINE);
  2241. goto finished;
  2242. }
  2243. sync ();
  2244. if (chmod (config_file, CONFIGFILE_MASK) != 0)
  2245. {
  2246. vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
  2247. config_file, safe_strerror(errno), errno, VTY_NEWLINE);
  2248. goto finished;
  2249. }
  2250. vty_out (vty, "Configuration saved to %s%s", config_file,
  2251. VTY_NEWLINE);
  2252. ret = CMD_SUCCESS;
  2253. finished:
  2254. unlink (config_file_tmp);
  2255. XFREE (MTYPE_TMP, config_file_tmp);
  2256. XFREE (MTYPE_TMP, config_file_sav);
  2257. return ret;
  2258. }
  2259. ALIAS (config_write_file,
  2260. config_write_cmd,
  2261. "write",
  2262. "Write running configuration to memory, network, or terminal\n")
  2263. ALIAS (config_write_file,
  2264. config_write_memory_cmd,
  2265. "write memory",
  2266. "Write running configuration to memory, network, or terminal\n"
  2267. "Write configuration to the file (same as write file)\n")
  2268. ALIAS (config_write_file,
  2269. copy_runningconfig_startupconfig_cmd,
  2270. "copy running-config startup-config",
  2271. "Copy configuration\n"
  2272. "Copy running config to... \n"
  2273. "Copy running config to startup config (same as write file)\n")
  2274. /* Write current configuration into the terminal. */
  2275. DEFUN (config_write_terminal,
  2276. config_write_terminal_cmd,
  2277. "write terminal",
  2278. "Write running configuration to memory, network, or terminal\n"
  2279. "Write to terminal\n")
  2280. {
  2281. unsigned int i;
  2282. struct cmd_node *node;
  2283. if (vty->type == VTY_SHELL_SERV)
  2284. {
  2285. for (i = 0; i < vector_active (cmdvec); i++)
  2286. if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)
  2287. {
  2288. if ((*node->func) (vty))
  2289. vty_out (vty, "!%s", VTY_NEWLINE);
  2290. }
  2291. }
  2292. else
  2293. {
  2294. vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
  2295. VTY_NEWLINE);
  2296. vty_out (vty, "!%s", VTY_NEWLINE);
  2297. for (i = 0; i < vector_active (cmdvec); i++)
  2298. if ((node = vector_slot (cmdvec, i)) && node->func)
  2299. {
  2300. if ((*node->func) (vty))
  2301. vty_out (vty, "!%s", VTY_NEWLINE);
  2302. }
  2303. vty_out (vty, "end%s",VTY_NEWLINE);
  2304. }
  2305. return CMD_SUCCESS;
  2306. }
  2307. /* Write current configuration into the terminal. */
  2308. ALIAS (config_write_terminal,
  2309. show_running_config_cmd,
  2310. "show running-config",
  2311. SHOW_STR
  2312. "running configuration\n")
  2313. /* Write startup configuration into the terminal. */
  2314. DEFUN (show_startup_config,
  2315. show_startup_config_cmd,
  2316. "show startup-config",
  2317. SHOW_STR
  2318. "Contentes of startup configuration\n")
  2319. {
  2320. char buf[BUFSIZ];
  2321. FILE *confp;
  2322. confp = fopen (host.config, "r");
  2323. if (confp == NULL)
  2324. {
  2325. vty_out (vty, "Can't open configuration file [%s]%s",
  2326. host.config, VTY_NEWLINE);
  2327. return CMD_WARNING;
  2328. }
  2329. while (fgets (buf, BUFSIZ, confp))
  2330. {
  2331. char *cp = buf;
  2332. while (*cp != '\r' && *cp != '\n' && *cp != '\0')
  2333. cp++;
  2334. *cp = '\0';
  2335. vty_out (vty, "%s%s", buf, VTY_NEWLINE);
  2336. }
  2337. fclose (confp);
  2338. return CMD_SUCCESS;
  2339. }
  2340. /* Hostname configuration */
  2341. DEFUN (config_hostname,
  2342. hostname_cmd,
  2343. "hostname WORD",
  2344. "Set system's network name\n"
  2345. "This system's network name\n")
  2346. {
  2347. if (!isalpha((int) *argv[0]))
  2348. {
  2349. vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE);
  2350. return CMD_WARNING;
  2351. }
  2352. if (host.name)
  2353. XFREE (MTYPE_HOST, host.name);
  2354. host.name = XSTRDUP (MTYPE_HOST, argv[0]);
  2355. return CMD_SUCCESS;
  2356. }
  2357. DEFUN (config_no_hostname,
  2358. no_hostname_cmd,
  2359. "no hostname [HOSTNAME]",
  2360. NO_STR
  2361. "Reset system's network name\n"
  2362. "Host name of this router\n")
  2363. {
  2364. if (host.name)
  2365. XFREE (MTYPE_HOST, host.name);
  2366. host.name = NULL;
  2367. return CMD_SUCCESS;
  2368. }
  2369. /* VTY interface password set. */
  2370. DEFUN (config_password, password_cmd,
  2371. "password (8|) WORD",
  2372. "Assign the terminal connection password\n"
  2373. "Specifies a HIDDEN password will follow\n"
  2374. "dummy string \n"
  2375. "The HIDDEN line password string\n")
  2376. {
  2377. /* Argument check. */
  2378. if (argc == 0)
  2379. {
  2380. vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
  2381. return CMD_WARNING;
  2382. }
  2383. if (argc == 2)
  2384. {
  2385. if (*argv[0] == '8')
  2386. {
  2387. if (host.password)
  2388. XFREE (MTYPE_HOST, host.password);
  2389. host.password = NULL;
  2390. if (host.password_encrypt)
  2391. XFREE (MTYPE_HOST, host.password_encrypt);
  2392. host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
  2393. return CMD_SUCCESS;
  2394. }
  2395. else
  2396. {
  2397. vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
  2398. return CMD_WARNING;
  2399. }
  2400. }
  2401. if (!isalnum ((int) *argv[0]))
  2402. {
  2403. vty_out (vty,
  2404. "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
  2405. return CMD_WARNING;
  2406. }
  2407. if (host.password)
  2408. XFREE (MTYPE_HOST, host.password);
  2409. host.password = NULL;
  2410. if (host.encrypt)
  2411. {
  2412. if (host.password_encrypt)
  2413. XFREE (MTYPE_HOST, host.password_encrypt);
  2414. host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
  2415. }
  2416. else
  2417. host.password = XSTRDUP (MTYPE_HOST, argv[0]);
  2418. return CMD_SUCCESS;
  2419. }
  2420. ALIAS (config_password, password_text_cmd,
  2421. "password LINE",
  2422. "Assign the terminal connection password\n"
  2423. "The UNENCRYPTED (cleartext) line password\n")
  2424. /* VTY enable password set. */
  2425. DEFUN (config_enable_password, enable_password_cmd,
  2426. "enable password (8|) WORD",
  2427. "Modify enable password parameters\n"
  2428. "Assign the privileged level password\n"
  2429. "Specifies a HIDDEN password will follow\n"
  2430. "dummy string \n"
  2431. "The HIDDEN 'enable' password string\n")
  2432. {
  2433. /* Argument check. */
  2434. if (argc == 0)
  2435. {
  2436. vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
  2437. return CMD_WARNING;
  2438. }
  2439. /* Crypt type is specified. */
  2440. if (argc == 2)
  2441. {
  2442. if (*argv[0] == '8')
  2443. {
  2444. if (host.enable)
  2445. XFREE (MTYPE_HOST, host.enable);
  2446. host.enable = NULL;
  2447. if (host.enable_encrypt)
  2448. XFREE (MTYPE_HOST, host.enable_encrypt);
  2449. host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
  2450. return CMD_SUCCESS;
  2451. }
  2452. else
  2453. {
  2454. vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
  2455. return CMD_WARNING;
  2456. }
  2457. }
  2458. if (!isalnum ((int) *argv[0]))
  2459. {
  2460. vty_out (vty,
  2461. "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
  2462. return CMD_WARNING;
  2463. }
  2464. if (host.enable)
  2465. XFREE (MTYPE_HOST, host.enable);
  2466. host.enable = NULL;
  2467. /* Plain password input. */
  2468. if (host.encrypt)
  2469. {
  2470. if (host.enable_encrypt)
  2471. XFREE (MTYPE_HOST, host.enable_encrypt);
  2472. host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
  2473. }
  2474. else
  2475. host.enable = XSTRDUP (MTYPE_HOST, argv[0]);
  2476. return CMD_SUCCESS;
  2477. }
  2478. ALIAS (config_enable_password,
  2479. enable_password_text_cmd,
  2480. "enable password LINE",
  2481. "Modify enable password parameters\n"
  2482. "Assign the privileged level password\n"
  2483. "The UNENCRYPTED (cleartext) 'enable' password\n")
  2484. /* VTY enable password delete. */
  2485. DEFUN (no_config_enable_password, no_enable_password_cmd,
  2486. "no enable password",
  2487. NO_STR
  2488. "Modify enable password parameters\n"
  2489. "Assign the privileged level password\n")
  2490. {
  2491. if (host.enable)
  2492. XFREE (MTYPE_HOST, host.enable);
  2493. host.enable = NULL;
  2494. if (host.enable_encrypt)
  2495. XFREE (MTYPE_HOST, host.enable_encrypt);
  2496. host.enable_encrypt = NULL;
  2497. return CMD_SUCCESS;
  2498. }
  2499. DEFUN (service_password_encrypt,
  2500. service_password_encrypt_cmd,
  2501. "service password-encryption",
  2502. "Set up miscellaneous service\n"
  2503. "Enable encrypted passwords\n")
  2504. {
  2505. if (host.encrypt)
  2506. return CMD_SUCCESS;
  2507. host.encrypt = 1;
  2508. if (host.password)
  2509. {
  2510. if (host.password_encrypt)
  2511. XFREE (MTYPE_HOST, host.password_encrypt);
  2512. host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password));
  2513. }
  2514. if (host.enable)
  2515. {
  2516. if (host.enable_encrypt)
  2517. XFREE (MTYPE_HOST, host.enable_encrypt);
  2518. host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable));
  2519. }
  2520. return CMD_SUCCESS;
  2521. }
  2522. DEFUN (no_service_password_encrypt,
  2523. no_service_password_encrypt_cmd,
  2524. "no service password-encryption",
  2525. NO_STR
  2526. "Set up miscellaneous service\n"
  2527. "Enable encrypted passwords\n")
  2528. {
  2529. if (! host.encrypt)
  2530. return CMD_SUCCESS;
  2531. host.encrypt = 0;
  2532. if (host.password_encrypt)
  2533. XFREE (MTYPE_HOST, host.password_encrypt);
  2534. host.password_encrypt = NULL;
  2535. if (host.enable_encrypt)
  2536. XFREE (MTYPE_HOST, host.enable_encrypt);
  2537. host.enable_encrypt = NULL;
  2538. return CMD_SUCCESS;
  2539. }
  2540. DEFUN (config_terminal_length, config_terminal_length_cmd,
  2541. "terminal length <0-512>",
  2542. "Set terminal line parameters\n"
  2543. "Set number of lines on a screen\n"
  2544. "Number of lines on screen (0 for no pausing)\n")
  2545. {
  2546. int lines;
  2547. char *endptr = NULL;
  2548. lines = strtol (argv[0], &endptr, 10);
  2549. if (lines < 0 || lines > 512 || *endptr != '\0')
  2550. {
  2551. vty_out (vty, "length is malformed%s", VTY_NEWLINE);
  2552. return CMD_WARNING;
  2553. }
  2554. vty->lines = lines;
  2555. return CMD_SUCCESS;
  2556. }
  2557. DEFUN (config_terminal_no_length, config_terminal_no_length_cmd,
  2558. "terminal no length",
  2559. "Set terminal line parameters\n"
  2560. NO_STR
  2561. "Set number of lines on a screen\n")
  2562. {
  2563. vty->lines = -1;
  2564. return CMD_SUCCESS;
  2565. }
  2566. DEFUN (service_terminal_length, service_terminal_length_cmd,
  2567. "service terminal-length <0-512>",
  2568. "Set up miscellaneous service\n"
  2569. "System wide terminal length configuration\n"
  2570. "Number of lines of VTY (0 means no line control)\n")
  2571. {
  2572. int lines;
  2573. char *endptr = NULL;
  2574. lines = strtol (argv[0], &endptr, 10);
  2575. if (lines < 0 || lines > 512 || *endptr != '\0')
  2576. {
  2577. vty_out (vty, "length is malformed%s", VTY_NEWLINE);
  2578. return CMD_WARNING;
  2579. }
  2580. host.lines = lines;
  2581. return CMD_SUCCESS;
  2582. }
  2583. DEFUN (no_service_terminal_length, no_service_terminal_length_cmd,
  2584. "no service terminal-length [<0-512>]",
  2585. NO_STR
  2586. "Set up miscellaneous service\n"
  2587. "System wide terminal length configuration\n"
  2588. "Number of lines of VTY (0 means no line control)\n")
  2589. {
  2590. host.lines = -1;
  2591. return CMD_SUCCESS;
  2592. }
  2593. DEFUN_HIDDEN (do_echo,
  2594. echo_cmd,
  2595. "echo .MESSAGE",
  2596. "Echo a message back to the vty\n"
  2597. "The message to echo\n")
  2598. {
  2599. char *message;
  2600. vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
  2601. VTY_NEWLINE);
  2602. if (message)
  2603. XFREE(MTYPE_TMP, message);
  2604. return CMD_SUCCESS;
  2605. }
  2606. DEFUN (config_logmsg,
  2607. config_logmsg_cmd,
  2608. "logmsg "LOG_LEVELS" .MESSAGE",
  2609. "Send a message to enabled logging destinations\n"
  2610. LOG_LEVEL_DESC
  2611. "The message to send\n")
  2612. {
  2613. int level;
  2614. char *message;
  2615. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2616. return CMD_ERR_NO_MATCH;
  2617. zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, 1)) ? message : ""));
  2618. if (message)
  2619. XFREE(MTYPE_TMP, message);
  2620. return CMD_SUCCESS;
  2621. }
  2622. DEFUN (show_logging,
  2623. show_logging_cmd,
  2624. "show logging",
  2625. SHOW_STR
  2626. "Show current logging configuration\n")
  2627. {
  2628. struct zlog *zl = zlog_default;
  2629. vty_out (vty, "Syslog logging: ");
  2630. if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
  2631. vty_out (vty, "disabled");
  2632. else
  2633. vty_out (vty, "level %s, facility %s, ident %s",
  2634. zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
  2635. facility_name(zl->facility), zl->ident);
  2636. vty_out (vty, "%s", VTY_NEWLINE);
  2637. vty_out (vty, "Stdout logging: ");
  2638. if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
  2639. vty_out (vty, "disabled");
  2640. else
  2641. vty_out (vty, "level %s",
  2642. zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
  2643. vty_out (vty, "%s", VTY_NEWLINE);
  2644. vty_out (vty, "Monitor logging: ");
  2645. if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
  2646. vty_out (vty, "disabled");
  2647. else
  2648. vty_out (vty, "level %s",
  2649. zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
  2650. vty_out (vty, "%s", VTY_NEWLINE);
  2651. vty_out (vty, "File logging: ");
  2652. if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) ||
  2653. !zl->fp)
  2654. vty_out (vty, "disabled");
  2655. else
  2656. vty_out (vty, "level %s, filename %s",
  2657. zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
  2658. zl->filename);
  2659. vty_out (vty, "%s", VTY_NEWLINE);
  2660. vty_out (vty, "Protocol name: %s%s",
  2661. zlog_proto_names[zl->protocol], VTY_NEWLINE);
  2662. vty_out (vty, "Record priority: %s%s",
  2663. (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
  2664. vty_out (vty, "Timestamp precision: %d%s",
  2665. zl->timestamp_precision, VTY_NEWLINE);
  2666. return CMD_SUCCESS;
  2667. }
  2668. DEFUN (config_log_stdout,
  2669. config_log_stdout_cmd,
  2670. "log stdout",
  2671. "Logging control\n"
  2672. "Set stdout logging level\n")
  2673. {
  2674. zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
  2675. return CMD_SUCCESS;
  2676. }
  2677. DEFUN (config_log_stdout_level,
  2678. config_log_stdout_level_cmd,
  2679. "log stdout "LOG_LEVELS,
  2680. "Logging control\n"
  2681. "Set stdout logging level\n"
  2682. LOG_LEVEL_DESC)
  2683. {
  2684. int level;
  2685. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2686. return CMD_ERR_NO_MATCH;
  2687. zlog_set_level (NULL, ZLOG_DEST_STDOUT, level);
  2688. return CMD_SUCCESS;
  2689. }
  2690. DEFUN (no_config_log_stdout,
  2691. no_config_log_stdout_cmd,
  2692. "no log stdout [LEVEL]",
  2693. NO_STR
  2694. "Logging control\n"
  2695. "Cancel logging to stdout\n"
  2696. "Logging level\n")
  2697. {
  2698. zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
  2699. return CMD_SUCCESS;
  2700. }
  2701. DEFUN (config_log_monitor,
  2702. config_log_monitor_cmd,
  2703. "log monitor",
  2704. "Logging control\n"
  2705. "Set terminal line (monitor) logging level\n")
  2706. {
  2707. zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
  2708. return CMD_SUCCESS;
  2709. }
  2710. DEFUN (config_log_monitor_level,
  2711. config_log_monitor_level_cmd,
  2712. "log monitor "LOG_LEVELS,
  2713. "Logging control\n"
  2714. "Set terminal line (monitor) logging level\n"
  2715. LOG_LEVEL_DESC)
  2716. {
  2717. int level;
  2718. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2719. return CMD_ERR_NO_MATCH;
  2720. zlog_set_level (NULL, ZLOG_DEST_MONITOR, level);
  2721. return CMD_SUCCESS;
  2722. }
  2723. DEFUN (no_config_log_monitor,
  2724. no_config_log_monitor_cmd,
  2725. "no log monitor [LEVEL]",
  2726. NO_STR
  2727. "Logging control\n"
  2728. "Disable terminal line (monitor) logging\n"
  2729. "Logging level\n")
  2730. {
  2731. zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
  2732. return CMD_SUCCESS;
  2733. }
  2734. static int
  2735. set_log_file(struct vty *vty, const char *fname, int loglevel)
  2736. {
  2737. int ret;
  2738. char *p = NULL;
  2739. const char *fullpath;
  2740. /* Path detection. */
  2741. if (! IS_DIRECTORY_SEP (*fname))
  2742. {
  2743. char cwd[MAXPATHLEN+1];
  2744. cwd[MAXPATHLEN] = '\0';
  2745. if (getcwd (cwd, MAXPATHLEN) == NULL)
  2746. {
  2747. zlog_err ("config_log_file: Unable to alloc mem!");
  2748. return CMD_WARNING;
  2749. }
  2750. if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2))
  2751. == NULL)
  2752. {
  2753. zlog_err ("config_log_file: Unable to alloc mem!");
  2754. return CMD_WARNING;
  2755. }
  2756. sprintf (p, "%s/%s", cwd, fname);
  2757. fullpath = p;
  2758. }
  2759. else
  2760. fullpath = fname;
  2761. ret = zlog_set_file (NULL, fullpath, loglevel);
  2762. if (p)
  2763. XFREE (MTYPE_TMP, p);
  2764. if (!ret)
  2765. {
  2766. vty_out (vty, "can't open logfile %s\n", fname);
  2767. return CMD_WARNING;
  2768. }
  2769. if (host.logfile)
  2770. XFREE (MTYPE_HOST, host.logfile);
  2771. host.logfile = XSTRDUP (MTYPE_HOST, fname);
  2772. return CMD_SUCCESS;
  2773. }
  2774. DEFUN (config_log_file,
  2775. config_log_file_cmd,
  2776. "log file FILENAME",
  2777. "Logging control\n"
  2778. "Logging to file\n"
  2779. "Logging filename\n")
  2780. {
  2781. return set_log_file(vty, argv[0], zlog_default->default_lvl);
  2782. }
  2783. DEFUN (config_log_file_level,
  2784. config_log_file_level_cmd,
  2785. "log file FILENAME "LOG_LEVELS,
  2786. "Logging control\n"
  2787. "Logging to file\n"
  2788. "Logging filename\n"
  2789. LOG_LEVEL_DESC)
  2790. {
  2791. int level;
  2792. if ((level = level_match(argv[1])) == ZLOG_DISABLED)
  2793. return CMD_ERR_NO_MATCH;
  2794. return set_log_file(vty, argv[0], level);
  2795. }
  2796. DEFUN (no_config_log_file,
  2797. no_config_log_file_cmd,
  2798. "no log file [FILENAME]",
  2799. NO_STR
  2800. "Logging control\n"
  2801. "Cancel logging to file\n"
  2802. "Logging file name\n")
  2803. {
  2804. zlog_reset_file (NULL);
  2805. if (host.logfile)
  2806. XFREE (MTYPE_HOST, host.logfile);
  2807. host.logfile = NULL;
  2808. return CMD_SUCCESS;
  2809. }
  2810. ALIAS (no_config_log_file,
  2811. no_config_log_file_level_cmd,
  2812. "no log file FILENAME LEVEL",
  2813. NO_STR
  2814. "Logging control\n"
  2815. "Cancel logging to file\n"
  2816. "Logging file name\n"
  2817. "Logging level\n")
  2818. DEFUN (config_log_syslog,
  2819. config_log_syslog_cmd,
  2820. "log syslog",
  2821. "Logging control\n"
  2822. "Set syslog logging level\n")
  2823. {
  2824. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
  2825. return CMD_SUCCESS;
  2826. }
  2827. DEFUN (config_log_syslog_level,
  2828. config_log_syslog_level_cmd,
  2829. "log syslog "LOG_LEVELS,
  2830. "Logging control\n"
  2831. "Set syslog logging level\n"
  2832. LOG_LEVEL_DESC)
  2833. {
  2834. int level;
  2835. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2836. return CMD_ERR_NO_MATCH;
  2837. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level);
  2838. return CMD_SUCCESS;
  2839. }
  2840. DEFUN_DEPRECATED (config_log_syslog_facility,
  2841. config_log_syslog_facility_cmd,
  2842. "log syslog facility "LOG_FACILITIES,
  2843. "Logging control\n"
  2844. "Logging goes to syslog\n"
  2845. "(Deprecated) Facility parameter for syslog messages\n"
  2846. LOG_FACILITY_DESC)
  2847. {
  2848. int facility;
  2849. if ((facility = facility_match(argv[0])) < 0)
  2850. return CMD_ERR_NO_MATCH;
  2851. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
  2852. zlog_default->facility = facility;
  2853. return CMD_SUCCESS;
  2854. }
  2855. DEFUN (no_config_log_syslog,
  2856. no_config_log_syslog_cmd,
  2857. "no log syslog [LEVEL]",
  2858. NO_STR
  2859. "Logging control\n"
  2860. "Cancel logging to syslog\n"
  2861. "Logging level\n")
  2862. {
  2863. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
  2864. return CMD_SUCCESS;
  2865. }
  2866. ALIAS (no_config_log_syslog,
  2867. no_config_log_syslog_facility_cmd,
  2868. "no log syslog facility "LOG_FACILITIES,
  2869. NO_STR
  2870. "Logging control\n"
  2871. "Logging goes to syslog\n"
  2872. "Facility parameter for syslog messages\n"
  2873. LOG_FACILITY_DESC)
  2874. DEFUN (config_log_facility,
  2875. config_log_facility_cmd,
  2876. "log facility "LOG_FACILITIES,
  2877. "Logging control\n"
  2878. "Facility parameter for syslog messages\n"
  2879. LOG_FACILITY_DESC)
  2880. {
  2881. int facility;
  2882. if ((facility = facility_match(argv[0])) < 0)
  2883. return CMD_ERR_NO_MATCH;
  2884. zlog_default->facility = facility;
  2885. return CMD_SUCCESS;
  2886. }
  2887. DEFUN (no_config_log_facility,
  2888. no_config_log_facility_cmd,
  2889. "no log facility [FACILITY]",
  2890. NO_STR
  2891. "Logging control\n"
  2892. "Reset syslog facility to default (daemon)\n"
  2893. "Syslog facility\n")
  2894. {
  2895. zlog_default->facility = LOG_DAEMON;
  2896. return CMD_SUCCESS;
  2897. }
  2898. DEFUN_DEPRECATED (config_log_trap,
  2899. config_log_trap_cmd,
  2900. "log trap "LOG_LEVELS,
  2901. "Logging control\n"
  2902. "(Deprecated) Set logging level and default for all destinations\n"
  2903. LOG_LEVEL_DESC)
  2904. {
  2905. int new_level ;
  2906. int i;
  2907. if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
  2908. return CMD_ERR_NO_MATCH;
  2909. zlog_default->default_lvl = new_level;
  2910. for (i = 0; i < ZLOG_NUM_DESTS; i++)
  2911. if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
  2912. zlog_default->maxlvl[i] = new_level;
  2913. return CMD_SUCCESS;
  2914. }
  2915. DEFUN_DEPRECATED (no_config_log_trap,
  2916. no_config_log_trap_cmd,
  2917. "no log trap [LEVEL]",
  2918. NO_STR
  2919. "Logging control\n"
  2920. "Permit all logging information\n"
  2921. "Logging level\n")
  2922. {
  2923. zlog_default->default_lvl = LOG_DEBUG;
  2924. return CMD_SUCCESS;
  2925. }
  2926. DEFUN (config_log_record_priority,
  2927. config_log_record_priority_cmd,
  2928. "log record-priority",
  2929. "Logging control\n"
  2930. "Log the priority of the message within the message\n")
  2931. {
  2932. zlog_default->record_priority = 1 ;
  2933. return CMD_SUCCESS;
  2934. }
  2935. DEFUN (no_config_log_record_priority,
  2936. no_config_log_record_priority_cmd,
  2937. "no log record-priority",
  2938. NO_STR
  2939. "Logging control\n"
  2940. "Do not log the priority of the message within the message\n")
  2941. {
  2942. zlog_default->record_priority = 0 ;
  2943. return CMD_SUCCESS;
  2944. }
  2945. DEFUN (config_log_timestamp_precision,
  2946. config_log_timestamp_precision_cmd,
  2947. "log timestamp precision <0-6>",
  2948. "Logging control\n"
  2949. "Timestamp configuration\n"
  2950. "Set the timestamp precision\n"
  2951. "Number of subsecond digits\n")
  2952. {
  2953. if (argc != 1)
  2954. {
  2955. vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
  2956. return CMD_WARNING;
  2957. }
  2958. VTY_GET_INTEGER_RANGE("Timestamp Precision",
  2959. zlog_default->timestamp_precision, argv[0], 0, 6);
  2960. return CMD_SUCCESS;
  2961. }
  2962. DEFUN (no_config_log_timestamp_precision,
  2963. no_config_log_timestamp_precision_cmd,
  2964. "no log timestamp precision",
  2965. NO_STR
  2966. "Logging control\n"
  2967. "Timestamp configuration\n"
  2968. "Reset the timestamp precision to the default value of 0\n")
  2969. {
  2970. zlog_default->timestamp_precision = 0 ;
  2971. return CMD_SUCCESS;
  2972. }
  2973. DEFUN (banner_motd_file,
  2974. banner_motd_file_cmd,
  2975. "banner motd file [FILE]",
  2976. "Set banner\n"
  2977. "Banner for motd\n"
  2978. "Banner from a file\n"
  2979. "Filename\n")
  2980. {
  2981. if (host.motdfile)
  2982. XFREE (MTYPE_HOST, host.motdfile);
  2983. host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]);
  2984. return CMD_SUCCESS;
  2985. }
  2986. DEFUN (banner_motd_default,
  2987. banner_motd_default_cmd,
  2988. "banner motd default",
  2989. "Set banner string\n"
  2990. "Strings for motd\n"
  2991. "Default string\n")
  2992. {
  2993. host.motd = default_motd;
  2994. return CMD_SUCCESS;
  2995. }
  2996. DEFUN (no_banner_motd,
  2997. no_banner_motd_cmd,
  2998. "no banner motd",
  2999. NO_STR
  3000. "Set banner string\n"
  3001. "Strings for motd\n")
  3002. {
  3003. host.motd = NULL;
  3004. if (host.motdfile)
  3005. XFREE (MTYPE_HOST, host.motdfile);
  3006. host.motdfile = NULL;
  3007. return CMD_SUCCESS;
  3008. }
  3009. /* Set config filename. Called from vty.c */
  3010. void
  3011. host_config_set (char *filename)
  3012. {
  3013. if (host.config)
  3014. XFREE (MTYPE_HOST, host.config);
  3015. host.config = XSTRDUP (MTYPE_HOST, filename);
  3016. }
  3017. void
  3018. install_default (enum node_type node)
  3019. {
  3020. install_element (node, &config_exit_cmd);
  3021. install_element (node, &config_quit_cmd);
  3022. install_element (node, &config_end_cmd);
  3023. install_element (node, &config_help_cmd);
  3024. install_element (node, &config_list_cmd);
  3025. install_element (node, &config_write_terminal_cmd);
  3026. install_element (node, &config_write_file_cmd);
  3027. install_element (node, &config_write_memory_cmd);
  3028. install_element (node, &config_write_cmd);
  3029. install_element (node, &show_running_config_cmd);
  3030. }
  3031. /* Initialize command interface. Install basic nodes and commands. */
  3032. void
  3033. cmd_init (int terminal)
  3034. {
  3035. command_cr = XSTRDUP(MTYPE_STRVEC, "<cr>");
  3036. desc_cr.cmd = command_cr;
  3037. desc_cr.str = XSTRDUP(MTYPE_STRVEC, "");
  3038. /* Allocate initial top vector of commands. */
  3039. cmdvec = vector_init (VECTOR_MIN_SIZE);
  3040. /* Default host value settings. */
  3041. host.name = NULL;
  3042. host.password = NULL;
  3043. host.enable = NULL;
  3044. host.logfile = NULL;
  3045. host.config = NULL;
  3046. host.lines = -1;
  3047. host.motd = default_motd;
  3048. host.motdfile = NULL;
  3049. /* Install top nodes. */
  3050. install_node (&view_node, NULL);
  3051. install_node (&enable_node, NULL);
  3052. install_node (&auth_node, NULL);
  3053. install_node (&auth_enable_node, NULL);
  3054. install_node (&restricted_node, NULL);
  3055. install_node (&config_node, config_write_host);
  3056. /* Each node's basic commands. */
  3057. install_element (VIEW_NODE, &show_version_cmd);
  3058. if (terminal)
  3059. {
  3060. install_element (VIEW_NODE, &config_list_cmd);
  3061. install_element (VIEW_NODE, &config_exit_cmd);
  3062. install_element (VIEW_NODE, &config_quit_cmd);
  3063. install_element (VIEW_NODE, &config_help_cmd);
  3064. install_element (VIEW_NODE, &config_enable_cmd);
  3065. install_element (VIEW_NODE, &config_terminal_length_cmd);
  3066. install_element (VIEW_NODE, &config_terminal_no_length_cmd);
  3067. install_element (VIEW_NODE, &show_logging_cmd);
  3068. install_element (VIEW_NODE, &echo_cmd);
  3069. install_element (RESTRICTED_NODE, &config_list_cmd);
  3070. install_element (RESTRICTED_NODE, &config_exit_cmd);
  3071. install_element (RESTRICTED_NODE, &config_quit_cmd);
  3072. install_element (RESTRICTED_NODE, &config_help_cmd);
  3073. install_element (RESTRICTED_NODE, &config_enable_cmd);
  3074. install_element (RESTRICTED_NODE, &config_terminal_length_cmd);
  3075. install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd);
  3076. install_element (RESTRICTED_NODE, &echo_cmd);
  3077. }
  3078. if (terminal)
  3079. {
  3080. install_default (ENABLE_NODE);
  3081. install_element (ENABLE_NODE, &config_disable_cmd);
  3082. install_element (ENABLE_NODE, &config_terminal_cmd);
  3083. install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
  3084. }
  3085. install_element (ENABLE_NODE, &show_startup_config_cmd);
  3086. install_element (ENABLE_NODE, &show_version_cmd);
  3087. if (terminal)
  3088. {
  3089. install_element (ENABLE_NODE, &config_terminal_length_cmd);
  3090. install_element (ENABLE_NODE, &config_terminal_no_length_cmd);
  3091. install_element (ENABLE_NODE, &show_logging_cmd);
  3092. install_element (ENABLE_NODE, &echo_cmd);
  3093. install_element (ENABLE_NODE, &config_logmsg_cmd);
  3094. install_default (CONFIG_NODE);
  3095. }
  3096. install_element (CONFIG_NODE, &hostname_cmd);
  3097. install_element (CONFIG_NODE, &no_hostname_cmd);
  3098. if (terminal)
  3099. {
  3100. install_element (CONFIG_NODE, &password_cmd);
  3101. install_element (CONFIG_NODE, &password_text_cmd);
  3102. install_element (CONFIG_NODE, &enable_password_cmd);
  3103. install_element (CONFIG_NODE, &enable_password_text_cmd);
  3104. install_element (CONFIG_NODE, &no_enable_password_cmd);
  3105. install_element (CONFIG_NODE, &config_log_stdout_cmd);
  3106. install_element (CONFIG_NODE, &config_log_stdout_level_cmd);
  3107. install_element (CONFIG_NODE, &no_config_log_stdout_cmd);
  3108. install_element (CONFIG_NODE, &config_log_monitor_cmd);
  3109. install_element (CONFIG_NODE, &config_log_monitor_level_cmd);
  3110. install_element (CONFIG_NODE, &no_config_log_monitor_cmd);
  3111. install_element (CONFIG_NODE, &config_log_file_cmd);
  3112. install_element (CONFIG_NODE, &config_log_file_level_cmd);
  3113. install_element (CONFIG_NODE, &no_config_log_file_cmd);
  3114. install_element (CONFIG_NODE, &no_config_log_file_level_cmd);
  3115. install_element (CONFIG_NODE, &config_log_syslog_cmd);
  3116. install_element (CONFIG_NODE, &config_log_syslog_level_cmd);
  3117. install_element (CONFIG_NODE, &config_log_syslog_facility_cmd);
  3118. install_element (CONFIG_NODE, &no_config_log_syslog_cmd);
  3119. install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd);
  3120. install_element (CONFIG_NODE, &config_log_facility_cmd);
  3121. install_element (CONFIG_NODE, &no_config_log_facility_cmd);
  3122. install_element (CONFIG_NODE, &config_log_trap_cmd);
  3123. install_element (CONFIG_NODE, &no_config_log_trap_cmd);
  3124. install_element (CONFIG_NODE, &config_log_record_priority_cmd);
  3125. install_element (CONFIG_NODE, &no_config_log_record_priority_cmd);
  3126. install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd);
  3127. install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd);
  3128. install_element (CONFIG_NODE, &service_password_encrypt_cmd);
  3129. install_element (CONFIG_NODE, &no_service_password_encrypt_cmd);
  3130. install_element (CONFIG_NODE, &banner_motd_default_cmd);
  3131. install_element (CONFIG_NODE, &banner_motd_file_cmd);
  3132. install_element (CONFIG_NODE, &no_banner_motd_cmd);
  3133. install_element (CONFIG_NODE, &service_terminal_length_cmd);
  3134. install_element (CONFIG_NODE, &no_service_terminal_length_cmd);
  3135. install_element (VIEW_NODE, &show_thread_cpu_cmd);
  3136. install_element (ENABLE_NODE, &show_thread_cpu_cmd);
  3137. install_element (RESTRICTED_NODE, &show_thread_cpu_cmd);
  3138. install_element (ENABLE_NODE, &clear_thread_cpu_cmd);
  3139. install_element (VIEW_NODE, &show_work_queues_cmd);
  3140. install_element (ENABLE_NODE, &show_work_queues_cmd);
  3141. }
  3142. srand(time(NULL));
  3143. }
  3144. void
  3145. cmd_terminate ()
  3146. {
  3147. unsigned int i, j, k, l;
  3148. struct cmd_node *cmd_node;
  3149. struct cmd_element *cmd_element;
  3150. struct desc *desc;
  3151. vector cmd_node_v, cmd_element_v, desc_v;
  3152. if (cmdvec)
  3153. {
  3154. for (i = 0; i < vector_active (cmdvec); i++)
  3155. if ((cmd_node = vector_slot (cmdvec, i)) != NULL)
  3156. {
  3157. cmd_node_v = cmd_node->cmd_vector;
  3158. for (j = 0; j < vector_active (cmd_node_v); j++)
  3159. if ((cmd_element = vector_slot (cmd_node_v, j)) != NULL &&
  3160. cmd_element->strvec != NULL)
  3161. {
  3162. cmd_element_v = cmd_element->strvec;
  3163. for (k = 0; k < vector_active (cmd_element_v); k++)
  3164. if ((desc_v = vector_slot (cmd_element_v, k)) != NULL)
  3165. {
  3166. for (l = 0; l < vector_active (desc_v); l++)
  3167. if ((desc = vector_slot (desc_v, l)) != NULL)
  3168. {
  3169. if (desc->cmd)
  3170. XFREE (MTYPE_STRVEC, desc->cmd);
  3171. if (desc->str)
  3172. XFREE (MTYPE_STRVEC, desc->str);
  3173. XFREE (MTYPE_DESC, desc);
  3174. }
  3175. vector_free (desc_v);
  3176. }
  3177. cmd_element->strvec = NULL;
  3178. vector_free (cmd_element_v);
  3179. }
  3180. vector_free (cmd_node_v);
  3181. }
  3182. vector_free (cmdvec);
  3183. cmdvec = NULL;
  3184. }
  3185. if (command_cr)
  3186. XFREE(MTYPE_STRVEC, command_cr);
  3187. if (desc_cr.str)
  3188. XFREE(MTYPE_STRVEC, desc_cr.str);
  3189. if (host.name)
  3190. XFREE (MTYPE_HOST, host.name);
  3191. if (host.password)
  3192. XFREE (MTYPE_HOST, host.password);
  3193. if (host.password_encrypt)
  3194. XFREE (MTYPE_HOST, host.password_encrypt);
  3195. if (host.enable)
  3196. XFREE (MTYPE_HOST, host.enable);
  3197. if (host.enable_encrypt)
  3198. XFREE (MTYPE_HOST, host.enable_encrypt);
  3199. if (host.logfile)
  3200. XFREE (MTYPE_HOST, host.logfile);
  3201. if (host.motdfile)
  3202. XFREE (MTYPE_HOST, host.motdfile);
  3203. if (host.config)
  3204. XFREE (MTYPE_HOST, host.config);
  3205. }