command.c 85 KB

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