command.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656
  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. " GIT_INFO "\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. return no_match;
  720. }
  721. static enum match_type
  722. cmd_ipv6_prefix_match (const char *str)
  723. {
  724. int state = STATE_START;
  725. int colons = 0, nums = 0, double_colon = 0;
  726. int mask;
  727. const char *sp = NULL;
  728. char *endptr = NULL;
  729. if (str == NULL)
  730. return partly_match;
  731. if (strspn (str, IPV6_PREFIX_STR) != strlen (str))
  732. return no_match;
  733. while (*str != '\0' && state != STATE_MASK)
  734. {
  735. switch (state)
  736. {
  737. case STATE_START:
  738. if (*str == ':')
  739. {
  740. if (*(str + 1) != ':' && *(str + 1) != '\0')
  741. return no_match;
  742. colons--;
  743. state = STATE_COLON;
  744. }
  745. else
  746. {
  747. sp = str;
  748. state = STATE_ADDR;
  749. }
  750. continue;
  751. case STATE_COLON:
  752. colons++;
  753. if (*(str + 1) == '/')
  754. return no_match;
  755. else if (*(str + 1) == ':')
  756. state = STATE_DOUBLE;
  757. else
  758. {
  759. sp = str + 1;
  760. state = STATE_ADDR;
  761. }
  762. break;
  763. case STATE_DOUBLE:
  764. if (double_colon)
  765. return no_match;
  766. if (*(str + 1) == ':')
  767. return no_match;
  768. else
  769. {
  770. if (*(str + 1) != '\0' && *(str + 1) != '/')
  771. colons++;
  772. sp = str + 1;
  773. if (*(str + 1) == '/')
  774. state = STATE_SLASH;
  775. else
  776. state = STATE_ADDR;
  777. }
  778. double_colon++;
  779. nums += 1;
  780. break;
  781. case STATE_ADDR:
  782. if (*(str + 1) == ':' || *(str + 1) == '.'
  783. || *(str + 1) == '\0' || *(str + 1) == '/')
  784. {
  785. if (str - sp > 3)
  786. return no_match;
  787. for (; sp <= str; sp++)
  788. if (*sp == '/')
  789. return no_match;
  790. nums++;
  791. if (*(str + 1) == ':')
  792. state = STATE_COLON;
  793. else if (*(str + 1) == '.')
  794. {
  795. if (colons || double_colon)
  796. state = STATE_DOT;
  797. else
  798. return no_match;
  799. }
  800. else if (*(str + 1) == '/')
  801. state = STATE_SLASH;
  802. }
  803. break;
  804. case STATE_DOT:
  805. state = STATE_ADDR;
  806. break;
  807. case STATE_SLASH:
  808. if (*(str + 1) == '\0')
  809. return partly_match;
  810. state = STATE_MASK;
  811. break;
  812. default:
  813. break;
  814. }
  815. if (nums > 11)
  816. return no_match;
  817. if (colons > 7)
  818. return no_match;
  819. str++;
  820. }
  821. if (state < STATE_MASK)
  822. return partly_match;
  823. mask = strtol (str, &endptr, 10);
  824. if (*endptr != '\0')
  825. return no_match;
  826. if (mask < 0 || mask > 128)
  827. return no_match;
  828. /* I don't know why mask < 13 makes command match partly.
  829. Forgive me to make this comments. I Want to set static default route
  830. because of lack of function to originate default in ospf6d; sorry
  831. yasu
  832. if (mask < 13)
  833. return partly_match;
  834. */
  835. return exact_match;
  836. }
  837. #endif /* HAVE_IPV6 */
  838. #define DECIMAL_STRLEN_MAX 10
  839. static int
  840. cmd_range_match (const char *range, const char *str)
  841. {
  842. char *p;
  843. char buf[DECIMAL_STRLEN_MAX + 1];
  844. char *endptr = NULL;
  845. unsigned long min, max, val;
  846. if (str == NULL)
  847. return 1;
  848. val = strtoul (str, &endptr, 10);
  849. if (*endptr != '\0')
  850. return 0;
  851. range++;
  852. p = strchr (range, '-');
  853. if (p == NULL)
  854. return 0;
  855. if (p - range > DECIMAL_STRLEN_MAX)
  856. return 0;
  857. strncpy (buf, range, p - range);
  858. buf[p - range] = '\0';
  859. min = strtoul (buf, &endptr, 10);
  860. if (*endptr != '\0')
  861. return 0;
  862. range = p + 1;
  863. p = strchr (range, '>');
  864. if (p == NULL)
  865. return 0;
  866. if (p - range > DECIMAL_STRLEN_MAX)
  867. return 0;
  868. strncpy (buf, range, p - range);
  869. buf[p - range] = '\0';
  870. max = strtoul (buf, &endptr, 10);
  871. if (*endptr != '\0')
  872. return 0;
  873. if (val < min || val > max)
  874. return 0;
  875. return 1;
  876. }
  877. /* Make completion match and return match type flag. */
  878. static enum match_type
  879. cmd_filter_by_completion (char *command, vector v, unsigned int index)
  880. {
  881. unsigned int i;
  882. const char *str;
  883. struct cmd_element *cmd_element;
  884. enum match_type match_type;
  885. vector descvec;
  886. struct desc *desc;
  887. match_type = no_match;
  888. /* If command and cmd_element string does not match set NULL to vector */
  889. for (i = 0; i < vector_active (v); i++)
  890. if ((cmd_element = vector_slot (v, i)) != NULL)
  891. {
  892. if (index >= vector_active (cmd_element->strvec))
  893. vector_slot (v, i) = NULL;
  894. else
  895. {
  896. unsigned int j;
  897. int matched = 0;
  898. descvec = vector_slot (cmd_element->strvec, index);
  899. for (j = 0; j < vector_active (descvec); j++)
  900. if ((desc = vector_slot (descvec, j)))
  901. {
  902. str = desc->cmd;
  903. if (CMD_VARARG (str))
  904. {
  905. if (match_type < vararg_match)
  906. match_type = vararg_match;
  907. matched++;
  908. }
  909. else if (CMD_RANGE (str))
  910. {
  911. if (cmd_range_match (str, command))
  912. {
  913. if (match_type < range_match)
  914. match_type = range_match;
  915. matched++;
  916. }
  917. }
  918. #ifdef HAVE_IPV6
  919. else if (CMD_IPV6 (str))
  920. {
  921. if (cmd_ipv6_match (command))
  922. {
  923. if (match_type < ipv6_match)
  924. match_type = ipv6_match;
  925. matched++;
  926. }
  927. }
  928. else if (CMD_IPV6_PREFIX (str))
  929. {
  930. if (cmd_ipv6_prefix_match (command))
  931. {
  932. if (match_type < ipv6_prefix_match)
  933. match_type = ipv6_prefix_match;
  934. matched++;
  935. }
  936. }
  937. #endif /* HAVE_IPV6 */
  938. else if (CMD_IPV4 (str))
  939. {
  940. if (cmd_ipv4_match (command))
  941. {
  942. if (match_type < ipv4_match)
  943. match_type = ipv4_match;
  944. matched++;
  945. }
  946. }
  947. else if (CMD_IPV4_PREFIX (str))
  948. {
  949. if (cmd_ipv4_prefix_match (command))
  950. {
  951. if (match_type < ipv4_prefix_match)
  952. match_type = ipv4_prefix_match;
  953. matched++;
  954. }
  955. }
  956. else
  957. /* Check is this point's argument optional ? */
  958. if (CMD_OPTION (str) || CMD_VARIABLE (str))
  959. {
  960. if (match_type < extend_match)
  961. match_type = extend_match;
  962. matched++;
  963. }
  964. else if (strncmp (command, str, strlen (command)) == 0)
  965. {
  966. if (strcmp (command, str) == 0)
  967. match_type = exact_match;
  968. else
  969. {
  970. if (match_type < partly_match)
  971. match_type = partly_match;
  972. }
  973. matched++;
  974. }
  975. }
  976. if (!matched)
  977. vector_slot (v, i) = NULL;
  978. }
  979. }
  980. return match_type;
  981. }
  982. /* Filter vector by command character with index. */
  983. static enum match_type
  984. cmd_filter_by_string (char *command, vector v, unsigned int index)
  985. {
  986. unsigned int i;
  987. const char *str;
  988. struct cmd_element *cmd_element;
  989. enum match_type match_type;
  990. vector descvec;
  991. struct desc *desc;
  992. match_type = no_match;
  993. /* If command and cmd_element string does not match set NULL to vector */
  994. for (i = 0; i < vector_active (v); i++)
  995. if ((cmd_element = vector_slot (v, i)) != NULL)
  996. {
  997. /* If given index is bigger than max string vector of command,
  998. set NULL */
  999. if (index >= vector_active (cmd_element->strvec))
  1000. vector_slot (v, i) = NULL;
  1001. else
  1002. {
  1003. unsigned int j;
  1004. int matched = 0;
  1005. descvec = vector_slot (cmd_element->strvec, index);
  1006. for (j = 0; j < vector_active (descvec); j++)
  1007. if ((desc = vector_slot (descvec, j)))
  1008. {
  1009. str = desc->cmd;
  1010. if (CMD_VARARG (str))
  1011. {
  1012. if (match_type < vararg_match)
  1013. match_type = vararg_match;
  1014. matched++;
  1015. }
  1016. else if (CMD_RANGE (str))
  1017. {
  1018. if (cmd_range_match (str, command))
  1019. {
  1020. if (match_type < range_match)
  1021. match_type = range_match;
  1022. matched++;
  1023. }
  1024. }
  1025. #ifdef HAVE_IPV6
  1026. else if (CMD_IPV6 (str))
  1027. {
  1028. if (cmd_ipv6_match (command) == exact_match)
  1029. {
  1030. if (match_type < ipv6_match)
  1031. match_type = ipv6_match;
  1032. matched++;
  1033. }
  1034. }
  1035. else if (CMD_IPV6_PREFIX (str))
  1036. {
  1037. if (cmd_ipv6_prefix_match (command) == exact_match)
  1038. {
  1039. if (match_type < ipv6_prefix_match)
  1040. match_type = ipv6_prefix_match;
  1041. matched++;
  1042. }
  1043. }
  1044. #endif /* HAVE_IPV6 */
  1045. else if (CMD_IPV4 (str))
  1046. {
  1047. if (cmd_ipv4_match (command) == exact_match)
  1048. {
  1049. if (match_type < ipv4_match)
  1050. match_type = ipv4_match;
  1051. matched++;
  1052. }
  1053. }
  1054. else if (CMD_IPV4_PREFIX (str))
  1055. {
  1056. if (cmd_ipv4_prefix_match (command) == exact_match)
  1057. {
  1058. if (match_type < ipv4_prefix_match)
  1059. match_type = ipv4_prefix_match;
  1060. matched++;
  1061. }
  1062. }
  1063. else if (CMD_OPTION (str) || CMD_VARIABLE (str))
  1064. {
  1065. if (match_type < extend_match)
  1066. match_type = extend_match;
  1067. matched++;
  1068. }
  1069. else
  1070. {
  1071. if (strcmp (command, str) == 0)
  1072. {
  1073. match_type = exact_match;
  1074. matched++;
  1075. }
  1076. }
  1077. }
  1078. if (!matched)
  1079. vector_slot (v, i) = NULL;
  1080. }
  1081. }
  1082. return match_type;
  1083. }
  1084. /* Check ambiguous match */
  1085. static int
  1086. is_cmd_ambiguous (char *command, vector v, int index, enum match_type type)
  1087. {
  1088. unsigned int i;
  1089. unsigned int j;
  1090. const char *str = NULL;
  1091. struct cmd_element *cmd_element;
  1092. const char *matched = NULL;
  1093. vector descvec;
  1094. struct desc *desc;
  1095. for (i = 0; i < vector_active (v); i++)
  1096. if ((cmd_element = vector_slot (v, i)) != NULL)
  1097. {
  1098. int match = 0;
  1099. descvec = vector_slot (cmd_element->strvec, index);
  1100. for (j = 0; j < vector_active (descvec); j++)
  1101. if ((desc = vector_slot (descvec, j)))
  1102. {
  1103. enum match_type ret;
  1104. str = desc->cmd;
  1105. switch (type)
  1106. {
  1107. case exact_match:
  1108. if (!(CMD_OPTION (str) || CMD_VARIABLE (str))
  1109. && strcmp (command, str) == 0)
  1110. match++;
  1111. break;
  1112. case partly_match:
  1113. if (!(CMD_OPTION (str) || CMD_VARIABLE (str))
  1114. && strncmp (command, str, strlen (command)) == 0)
  1115. {
  1116. if (matched && strcmp (matched, str) != 0)
  1117. return 1; /* There is ambiguous match. */
  1118. else
  1119. matched = str;
  1120. match++;
  1121. }
  1122. break;
  1123. case range_match:
  1124. if (cmd_range_match (str, command))
  1125. {
  1126. if (matched && strcmp (matched, str) != 0)
  1127. return 1;
  1128. else
  1129. matched = str;
  1130. match++;
  1131. }
  1132. break;
  1133. #ifdef HAVE_IPV6
  1134. case ipv6_match:
  1135. if (CMD_IPV6 (str))
  1136. match++;
  1137. break;
  1138. case ipv6_prefix_match:
  1139. if ((ret = cmd_ipv6_prefix_match (command)) != no_match)
  1140. {
  1141. if (ret == partly_match)
  1142. return 2; /* There is incomplete match. */
  1143. match++;
  1144. }
  1145. break;
  1146. #endif /* HAVE_IPV6 */
  1147. case ipv4_match:
  1148. if (CMD_IPV4 (str))
  1149. match++;
  1150. break;
  1151. case ipv4_prefix_match:
  1152. if ((ret = cmd_ipv4_prefix_match (command)) != no_match)
  1153. {
  1154. if (ret == partly_match)
  1155. return 2; /* There is incomplete match. */
  1156. match++;
  1157. }
  1158. break;
  1159. case extend_match:
  1160. if (CMD_OPTION (str) || CMD_VARIABLE (str))
  1161. match++;
  1162. break;
  1163. case no_match:
  1164. default:
  1165. break;
  1166. }
  1167. }
  1168. if (!match)
  1169. vector_slot (v, i) = NULL;
  1170. }
  1171. return 0;
  1172. }
  1173. /* If src matches dst return dst string, otherwise return NULL */
  1174. static const char *
  1175. cmd_entry_function (const char *src, const char *dst)
  1176. {
  1177. /* Skip variable arguments. */
  1178. if (CMD_OPTION (dst) || CMD_VARIABLE (dst) || CMD_VARARG (dst) ||
  1179. CMD_IPV4 (dst) || CMD_IPV4_PREFIX (dst) || CMD_RANGE (dst))
  1180. return NULL;
  1181. /* In case of 'command \t', given src is NULL string. */
  1182. if (src == NULL)
  1183. return dst;
  1184. /* Matched with input string. */
  1185. if (strncmp (src, dst, strlen (src)) == 0)
  1186. return dst;
  1187. return NULL;
  1188. }
  1189. /* If src matches dst return dst string, otherwise return NULL */
  1190. /* This version will return the dst string always if it is
  1191. CMD_VARIABLE for '?' key processing */
  1192. static const char *
  1193. cmd_entry_function_desc (const char *src, const char *dst)
  1194. {
  1195. if (CMD_VARARG (dst))
  1196. return dst;
  1197. if (CMD_RANGE (dst))
  1198. {
  1199. if (cmd_range_match (dst, src))
  1200. return dst;
  1201. else
  1202. return NULL;
  1203. }
  1204. #ifdef HAVE_IPV6
  1205. if (CMD_IPV6 (dst))
  1206. {
  1207. if (cmd_ipv6_match (src))
  1208. return dst;
  1209. else
  1210. return NULL;
  1211. }
  1212. if (CMD_IPV6_PREFIX (dst))
  1213. {
  1214. if (cmd_ipv6_prefix_match (src))
  1215. return dst;
  1216. else
  1217. return NULL;
  1218. }
  1219. #endif /* HAVE_IPV6 */
  1220. if (CMD_IPV4 (dst))
  1221. {
  1222. if (cmd_ipv4_match (src))
  1223. return dst;
  1224. else
  1225. return NULL;
  1226. }
  1227. if (CMD_IPV4_PREFIX (dst))
  1228. {
  1229. if (cmd_ipv4_prefix_match (src))
  1230. return dst;
  1231. else
  1232. return NULL;
  1233. }
  1234. /* Optional or variable commands always match on '?' */
  1235. if (CMD_OPTION (dst) || CMD_VARIABLE (dst))
  1236. return dst;
  1237. /* In case of 'command \t', given src is NULL string. */
  1238. if (src == NULL)
  1239. return dst;
  1240. if (strncmp (src, dst, strlen (src)) == 0)
  1241. return dst;
  1242. else
  1243. return NULL;
  1244. }
  1245. /* Check same string element existence. If it isn't there return
  1246. 1. */
  1247. static int
  1248. cmd_unique_string (vector v, const char *str)
  1249. {
  1250. unsigned int i;
  1251. char *match;
  1252. for (i = 0; i < vector_active (v); i++)
  1253. if ((match = vector_slot (v, i)) != NULL)
  1254. if (strcmp (match, str) == 0)
  1255. return 0;
  1256. return 1;
  1257. }
  1258. /* Compare string to description vector. If there is same string
  1259. return 1 else return 0. */
  1260. static int
  1261. desc_unique_string (vector v, const char *str)
  1262. {
  1263. unsigned int i;
  1264. struct desc *desc;
  1265. for (i = 0; i < vector_active (v); i++)
  1266. if ((desc = vector_slot (v, i)) != NULL)
  1267. if (strcmp (desc->cmd, str) == 0)
  1268. return 1;
  1269. return 0;
  1270. }
  1271. static int
  1272. cmd_try_do_shortcut (enum node_type node, char* first_word) {
  1273. if ( first_word != NULL &&
  1274. node != AUTH_NODE &&
  1275. node != VIEW_NODE &&
  1276. node != AUTH_ENABLE_NODE &&
  1277. node != ENABLE_NODE &&
  1278. node != RESTRICTED_NODE &&
  1279. 0 == strcmp( "do", first_word ) )
  1280. return 1;
  1281. return 0;
  1282. }
  1283. /* '?' describe command support. */
  1284. static vector
  1285. cmd_describe_command_real (vector vline, struct vty *vty, int *status)
  1286. {
  1287. unsigned int i;
  1288. vector cmd_vector;
  1289. #define INIT_MATCHVEC_SIZE 10
  1290. vector matchvec;
  1291. struct cmd_element *cmd_element;
  1292. unsigned int index;
  1293. int ret;
  1294. enum match_type match;
  1295. char *command;
  1296. /* Set index. */
  1297. if (vector_active (vline) == 0)
  1298. {
  1299. *status = CMD_ERR_NO_MATCH;
  1300. return NULL;
  1301. }
  1302. else
  1303. index = vector_active (vline) - 1;
  1304. /* Make copy vector of current node's command vector. */
  1305. cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1306. /* Prepare match vector */
  1307. matchvec = vector_init (INIT_MATCHVEC_SIZE);
  1308. /* Filter commands. */
  1309. /* Only words precedes current word will be checked in this loop. */
  1310. for (i = 0; i < index; i++)
  1311. if ((command = vector_slot (vline, i)))
  1312. {
  1313. match = cmd_filter_by_completion (command, cmd_vector, i);
  1314. if (match == vararg_match)
  1315. {
  1316. struct cmd_element *cmd_element;
  1317. vector descvec;
  1318. unsigned int j, k;
  1319. for (j = 0; j < vector_active (cmd_vector); j++)
  1320. if ((cmd_element = vector_slot (cmd_vector, j)) != NULL
  1321. && (vector_active (cmd_element->strvec)))
  1322. {
  1323. descvec = vector_slot (cmd_element->strvec,
  1324. vector_active (cmd_element->strvec) - 1);
  1325. for (k = 0; k < vector_active (descvec); k++)
  1326. {
  1327. struct desc *desc = vector_slot (descvec, k);
  1328. vector_set (matchvec, desc);
  1329. }
  1330. }
  1331. vector_set (matchvec, &desc_cr);
  1332. vector_free (cmd_vector);
  1333. return matchvec;
  1334. }
  1335. if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1)
  1336. {
  1337. vector_free (cmd_vector);
  1338. vector_free (matchvec);
  1339. *status = CMD_ERR_AMBIGUOUS;
  1340. return NULL;
  1341. }
  1342. else if (ret == 2)
  1343. {
  1344. vector_free (cmd_vector);
  1345. vector_free (matchvec);
  1346. *status = CMD_ERR_NO_MATCH;
  1347. return NULL;
  1348. }
  1349. }
  1350. /* Prepare match vector */
  1351. /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
  1352. /* Make sure that cmd_vector is filtered based on current word */
  1353. command = vector_slot (vline, index);
  1354. if (command)
  1355. match = cmd_filter_by_completion (command, cmd_vector, index);
  1356. /* Make description vector. */
  1357. for (i = 0; i < vector_active (cmd_vector); i++)
  1358. if ((cmd_element = vector_slot (cmd_vector, i)) != NULL)
  1359. {
  1360. vector strvec = cmd_element->strvec;
  1361. /* if command is NULL, index may be equal to vector_active */
  1362. if (command && index >= vector_active (strvec))
  1363. vector_slot (cmd_vector, i) = NULL;
  1364. else
  1365. {
  1366. /* Check if command is completed. */
  1367. if (command == NULL && index == vector_active (strvec))
  1368. {
  1369. if (!desc_unique_string (matchvec, command_cr))
  1370. vector_set (matchvec, &desc_cr);
  1371. }
  1372. else
  1373. {
  1374. unsigned int j;
  1375. vector descvec = vector_slot (strvec, index);
  1376. struct desc *desc;
  1377. for (j = 0; j < vector_active (descvec); j++)
  1378. if ((desc = vector_slot (descvec, j)))
  1379. {
  1380. const char *string;
  1381. string = cmd_entry_function_desc (command, desc->cmd);
  1382. if (string)
  1383. {
  1384. /* Uniqueness check */
  1385. if (!desc_unique_string (matchvec, string))
  1386. vector_set (matchvec, desc);
  1387. }
  1388. }
  1389. }
  1390. }
  1391. }
  1392. vector_free (cmd_vector);
  1393. if (vector_slot (matchvec, 0) == NULL)
  1394. {
  1395. vector_free (matchvec);
  1396. *status = CMD_ERR_NO_MATCH;
  1397. return NULL;
  1398. }
  1399. *status = CMD_SUCCESS;
  1400. return matchvec;
  1401. }
  1402. vector
  1403. cmd_describe_command (vector vline, struct vty *vty, int *status)
  1404. {
  1405. vector ret;
  1406. if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
  1407. {
  1408. enum node_type onode;
  1409. vector shifted_vline;
  1410. unsigned int index;
  1411. onode = vty->node;
  1412. vty->node = ENABLE_NODE;
  1413. /* We can try it on enable node, cos' the vty is authenticated */
  1414. shifted_vline = vector_init (vector_count(vline));
  1415. /* use memcpy? */
  1416. for (index = 1; index < vector_active (vline); index++)
  1417. {
  1418. vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
  1419. }
  1420. ret = cmd_describe_command_real (shifted_vline, vty, status);
  1421. vector_free(shifted_vline);
  1422. vty->node = onode;
  1423. return ret;
  1424. }
  1425. return cmd_describe_command_real (vline, vty, status);
  1426. }
  1427. /* Check LCD of matched command. */
  1428. static int
  1429. cmd_lcd (char **matched)
  1430. {
  1431. int i;
  1432. int j;
  1433. int lcd = -1;
  1434. char *s1, *s2;
  1435. char c1, c2;
  1436. if (matched[0] == NULL || matched[1] == NULL)
  1437. return 0;
  1438. for (i = 1; matched[i] != NULL; i++)
  1439. {
  1440. s1 = matched[i - 1];
  1441. s2 = matched[i];
  1442. for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
  1443. if (c1 != c2)
  1444. break;
  1445. if (lcd < 0)
  1446. lcd = j;
  1447. else
  1448. {
  1449. if (lcd > j)
  1450. lcd = j;
  1451. }
  1452. }
  1453. return lcd;
  1454. }
  1455. /* Command line completion support. */
  1456. static char **
  1457. cmd_complete_command_real (vector vline, struct vty *vty, int *status)
  1458. {
  1459. unsigned int i;
  1460. vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1461. #define INIT_MATCHVEC_SIZE 10
  1462. vector matchvec;
  1463. struct cmd_element *cmd_element;
  1464. unsigned int index;
  1465. char **match_str;
  1466. struct desc *desc;
  1467. vector descvec;
  1468. char *command;
  1469. int lcd;
  1470. if (vector_active (vline) == 0)
  1471. {
  1472. vector_free (cmd_vector);
  1473. *status = CMD_ERR_NO_MATCH;
  1474. return NULL;
  1475. }
  1476. else
  1477. index = vector_active (vline) - 1;
  1478. /* First, filter by preceeding command string */
  1479. for (i = 0; i < index; i++)
  1480. if ((command = vector_slot (vline, i)))
  1481. {
  1482. enum match_type match;
  1483. int ret;
  1484. /* First try completion match, if there is exactly match return 1 */
  1485. match = cmd_filter_by_completion (command, cmd_vector, i);
  1486. /* If there is exact match then filter ambiguous match else check
  1487. ambiguousness. */
  1488. if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1)
  1489. {
  1490. vector_free (cmd_vector);
  1491. *status = CMD_ERR_AMBIGUOUS;
  1492. return NULL;
  1493. }
  1494. /*
  1495. else if (ret == 2)
  1496. {
  1497. vector_free (cmd_vector);
  1498. *status = CMD_ERR_NO_MATCH;
  1499. return NULL;
  1500. }
  1501. */
  1502. }
  1503. /* Prepare match vector. */
  1504. matchvec = vector_init (INIT_MATCHVEC_SIZE);
  1505. /* Now we got into completion */
  1506. for (i = 0; i < vector_active (cmd_vector); i++)
  1507. if ((cmd_element = vector_slot (cmd_vector, i)))
  1508. {
  1509. const char *string;
  1510. vector strvec = cmd_element->strvec;
  1511. /* Check field length */
  1512. if (index >= vector_active (strvec))
  1513. vector_slot (cmd_vector, i) = NULL;
  1514. else
  1515. {
  1516. unsigned int j;
  1517. descvec = vector_slot (strvec, index);
  1518. for (j = 0; j < vector_active (descvec); j++)
  1519. if ((desc = vector_slot (descvec, j)))
  1520. {
  1521. if ((string =
  1522. cmd_entry_function (vector_slot (vline, index),
  1523. desc->cmd)))
  1524. if (cmd_unique_string (matchvec, string))
  1525. vector_set (matchvec, XSTRDUP (MTYPE_TMP, string));
  1526. }
  1527. }
  1528. }
  1529. /* We don't need cmd_vector any more. */
  1530. vector_free (cmd_vector);
  1531. /* No matched command */
  1532. if (vector_slot (matchvec, 0) == NULL)
  1533. {
  1534. vector_free (matchvec);
  1535. /* In case of 'command \t' pattern. Do you need '?' command at
  1536. the end of the line. */
  1537. if (vector_slot (vline, index) == '\0')
  1538. *status = CMD_ERR_NOTHING_TODO;
  1539. else
  1540. *status = CMD_ERR_NO_MATCH;
  1541. return NULL;
  1542. }
  1543. /* Only one matched */
  1544. if (vector_slot (matchvec, 1) == NULL)
  1545. {
  1546. match_str = (char **) matchvec->index;
  1547. vector_only_wrapper_free (matchvec);
  1548. *status = CMD_COMPLETE_FULL_MATCH;
  1549. return match_str;
  1550. }
  1551. /* Make it sure last element is NULL. */
  1552. vector_set (matchvec, NULL);
  1553. /* Check LCD of matched strings. */
  1554. if (vector_slot (vline, index) != NULL)
  1555. {
  1556. lcd = cmd_lcd ((char **) matchvec->index);
  1557. if (lcd)
  1558. {
  1559. int len = strlen (vector_slot (vline, index));
  1560. if (len < lcd)
  1561. {
  1562. char *lcdstr;
  1563. lcdstr = XMALLOC (MTYPE_STRVEC, lcd + 1);
  1564. memcpy (lcdstr, matchvec->index[0], lcd);
  1565. lcdstr[lcd] = '\0';
  1566. /* match_str = (char **) &lcdstr; */
  1567. /* Free matchvec. */
  1568. for (i = 0; i < vector_active (matchvec); i++)
  1569. {
  1570. if (vector_slot (matchvec, i))
  1571. XFREE (MTYPE_STRVEC, vector_slot (matchvec, i));
  1572. }
  1573. vector_free (matchvec);
  1574. /* Make new matchvec. */
  1575. matchvec = vector_init (INIT_MATCHVEC_SIZE);
  1576. vector_set (matchvec, lcdstr);
  1577. match_str = (char **) matchvec->index;
  1578. vector_only_wrapper_free (matchvec);
  1579. *status = CMD_COMPLETE_MATCH;
  1580. return match_str;
  1581. }
  1582. }
  1583. }
  1584. match_str = (char **) matchvec->index;
  1585. vector_only_wrapper_free (matchvec);
  1586. *status = CMD_COMPLETE_LIST_MATCH;
  1587. return match_str;
  1588. }
  1589. char **
  1590. cmd_complete_command (vector vline, struct vty *vty, int *status)
  1591. {
  1592. char **ret;
  1593. if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
  1594. {
  1595. enum node_type onode;
  1596. vector shifted_vline;
  1597. unsigned int index;
  1598. onode = vty->node;
  1599. vty->node = ENABLE_NODE;
  1600. /* We can try it on enable node, cos' the vty is authenticated */
  1601. shifted_vline = vector_init (vector_count(vline));
  1602. /* use memcpy? */
  1603. for (index = 1; index < vector_active (vline); index++)
  1604. {
  1605. vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
  1606. }
  1607. ret = cmd_complete_command_real (shifted_vline, vty, status);
  1608. vector_free(shifted_vline);
  1609. vty->node = onode;
  1610. return ret;
  1611. }
  1612. return cmd_complete_command_real (vline, vty, status);
  1613. }
  1614. /* return parent node */
  1615. /* MUST eventually converge on CONFIG_NODE */
  1616. enum node_type
  1617. node_parent ( enum node_type node )
  1618. {
  1619. enum node_type ret;
  1620. assert (node > CONFIG_NODE);
  1621. switch (node)
  1622. {
  1623. case BGP_VPNV4_NODE:
  1624. case BGP_IPV4_NODE:
  1625. case BGP_IPV4M_NODE:
  1626. case BGP_IPV6_NODE:
  1627. case BGP_IPV6M_NODE:
  1628. ret = BGP_NODE;
  1629. break;
  1630. case KEYCHAIN_KEY_NODE:
  1631. ret = KEYCHAIN_NODE;
  1632. break;
  1633. default:
  1634. ret = CONFIG_NODE;
  1635. }
  1636. return ret;
  1637. }
  1638. /* Execute command by argument vline vector. */
  1639. static int
  1640. cmd_execute_command_real (vector vline, struct vty *vty,
  1641. struct cmd_element **cmd)
  1642. {
  1643. unsigned int i;
  1644. unsigned int index;
  1645. vector cmd_vector;
  1646. struct cmd_element *cmd_element;
  1647. struct cmd_element *matched_element;
  1648. unsigned int matched_count, incomplete_count;
  1649. int argc;
  1650. const char *argv[CMD_ARGC_MAX];
  1651. enum match_type match = 0;
  1652. int varflag;
  1653. char *command;
  1654. /* Make copy of command elements. */
  1655. cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1656. for (index = 0; index < vector_active (vline); index++)
  1657. if ((command = vector_slot (vline, index)))
  1658. {
  1659. int ret;
  1660. match = cmd_filter_by_completion (command, cmd_vector, index);
  1661. if (match == vararg_match)
  1662. break;
  1663. ret = is_cmd_ambiguous (command, cmd_vector, index, match);
  1664. if (ret == 1)
  1665. {
  1666. vector_free (cmd_vector);
  1667. return CMD_ERR_AMBIGUOUS;
  1668. }
  1669. else if (ret == 2)
  1670. {
  1671. vector_free (cmd_vector);
  1672. return CMD_ERR_NO_MATCH;
  1673. }
  1674. }
  1675. /* Check matched count. */
  1676. matched_element = NULL;
  1677. matched_count = 0;
  1678. incomplete_count = 0;
  1679. for (i = 0; i < vector_active (cmd_vector); i++)
  1680. if ((cmd_element = vector_slot (cmd_vector, i)))
  1681. {
  1682. if (match == vararg_match || index >= cmd_element->cmdsize)
  1683. {
  1684. matched_element = cmd_element;
  1685. #if 0
  1686. printf ("DEBUG: %s\n", cmd_element->string);
  1687. #endif
  1688. matched_count++;
  1689. }
  1690. else
  1691. {
  1692. incomplete_count++;
  1693. }
  1694. }
  1695. /* Finish of using cmd_vector. */
  1696. vector_free (cmd_vector);
  1697. /* To execute command, matched_count must be 1. */
  1698. if (matched_count == 0)
  1699. {
  1700. if (incomplete_count)
  1701. return CMD_ERR_INCOMPLETE;
  1702. else
  1703. return CMD_ERR_NO_MATCH;
  1704. }
  1705. if (matched_count > 1)
  1706. return CMD_ERR_AMBIGUOUS;
  1707. /* Argument treatment */
  1708. varflag = 0;
  1709. argc = 0;
  1710. for (i = 0; i < vector_active (vline); i++)
  1711. {
  1712. if (varflag)
  1713. argv[argc++] = vector_slot (vline, i);
  1714. else
  1715. {
  1716. vector descvec = vector_slot (matched_element->strvec, i);
  1717. if (vector_active (descvec) == 1)
  1718. {
  1719. struct desc *desc = vector_slot (descvec, 0);
  1720. if (CMD_VARARG (desc->cmd))
  1721. varflag = 1;
  1722. if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd))
  1723. argv[argc++] = vector_slot (vline, i);
  1724. }
  1725. else
  1726. argv[argc++] = vector_slot (vline, i);
  1727. }
  1728. if (argc >= CMD_ARGC_MAX)
  1729. return CMD_ERR_EXEED_ARGC_MAX;
  1730. }
  1731. /* For vtysh execution. */
  1732. if (cmd)
  1733. *cmd = matched_element;
  1734. if (matched_element->daemon)
  1735. return CMD_SUCCESS_DAEMON;
  1736. /* Execute matched command. */
  1737. return (*matched_element->func) (matched_element, vty, argc, argv);
  1738. }
  1739. int
  1740. cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
  1741. int vtysh) {
  1742. int ret, saved_ret, tried = 0;
  1743. enum node_type onode, try_node;
  1744. onode = try_node = vty->node;
  1745. if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
  1746. {
  1747. vector shifted_vline;
  1748. unsigned int index;
  1749. vty->node = ENABLE_NODE;
  1750. /* We can try it on enable node, cos' the vty is authenticated */
  1751. shifted_vline = vector_init (vector_count(vline));
  1752. /* use memcpy? */
  1753. for (index = 1; index < vector_active (vline); index++)
  1754. {
  1755. vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
  1756. }
  1757. ret = cmd_execute_command_real (shifted_vline, vty, cmd);
  1758. vector_free(shifted_vline);
  1759. vty->node = onode;
  1760. return ret;
  1761. }
  1762. saved_ret = ret = cmd_execute_command_real (vline, vty, cmd);
  1763. if (vtysh)
  1764. return saved_ret;
  1765. /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
  1766. while ( ret != CMD_SUCCESS && ret != CMD_WARNING
  1767. && vty->node > CONFIG_NODE )
  1768. {
  1769. try_node = node_parent(try_node);
  1770. vty->node = try_node;
  1771. ret = cmd_execute_command_real (vline, vty, cmd);
  1772. tried = 1;
  1773. if (ret == CMD_SUCCESS || ret == CMD_WARNING)
  1774. {
  1775. /* succesfull command, leave the node as is */
  1776. return ret;
  1777. }
  1778. }
  1779. /* no command succeeded, reset the vty to the original node and
  1780. return the error for this node */
  1781. if ( tried )
  1782. vty->node = onode;
  1783. return saved_ret;
  1784. }
  1785. /* Execute command by argument readline. */
  1786. int
  1787. cmd_execute_command_strict (vector vline, struct vty *vty,
  1788. struct cmd_element **cmd)
  1789. {
  1790. unsigned int i;
  1791. unsigned int index;
  1792. vector cmd_vector;
  1793. struct cmd_element *cmd_element;
  1794. struct cmd_element *matched_element;
  1795. unsigned int matched_count, incomplete_count;
  1796. int argc;
  1797. const char *argv[CMD_ARGC_MAX];
  1798. int varflag;
  1799. enum match_type match = 0;
  1800. char *command;
  1801. /* Make copy of command element */
  1802. cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
  1803. for (index = 0; index < vector_active (vline); index++)
  1804. if ((command = vector_slot (vline, index)))
  1805. {
  1806. int ret;
  1807. match = cmd_filter_by_string (vector_slot (vline, index),
  1808. cmd_vector, index);
  1809. /* If command meets '.VARARG' then finish matching. */
  1810. if (match == vararg_match)
  1811. break;
  1812. ret = is_cmd_ambiguous (command, cmd_vector, index, match);
  1813. if (ret == 1)
  1814. {
  1815. vector_free (cmd_vector);
  1816. return CMD_ERR_AMBIGUOUS;
  1817. }
  1818. if (ret == 2)
  1819. {
  1820. vector_free (cmd_vector);
  1821. return CMD_ERR_NO_MATCH;
  1822. }
  1823. }
  1824. /* Check matched count. */
  1825. matched_element = NULL;
  1826. matched_count = 0;
  1827. incomplete_count = 0;
  1828. for (i = 0; i < vector_active (cmd_vector); i++)
  1829. if (vector_slot (cmd_vector, i) != NULL)
  1830. {
  1831. cmd_element = vector_slot (cmd_vector, i);
  1832. if (match == vararg_match || index >= cmd_element->cmdsize)
  1833. {
  1834. matched_element = cmd_element;
  1835. matched_count++;
  1836. }
  1837. else
  1838. incomplete_count++;
  1839. }
  1840. /* Finish of using cmd_vector. */
  1841. vector_free (cmd_vector);
  1842. /* To execute command, matched_count must be 1. */
  1843. if (matched_count == 0)
  1844. {
  1845. if (incomplete_count)
  1846. return CMD_ERR_INCOMPLETE;
  1847. else
  1848. return CMD_ERR_NO_MATCH;
  1849. }
  1850. if (matched_count > 1)
  1851. return CMD_ERR_AMBIGUOUS;
  1852. /* Argument treatment */
  1853. varflag = 0;
  1854. argc = 0;
  1855. for (i = 0; i < vector_active (vline); i++)
  1856. {
  1857. if (varflag)
  1858. argv[argc++] = vector_slot (vline, i);
  1859. else
  1860. {
  1861. vector descvec = vector_slot (matched_element->strvec, i);
  1862. if (vector_active (descvec) == 1)
  1863. {
  1864. struct desc *desc = vector_slot (descvec, 0);
  1865. if (CMD_VARARG (desc->cmd))
  1866. varflag = 1;
  1867. if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd))
  1868. argv[argc++] = vector_slot (vline, i);
  1869. }
  1870. else
  1871. argv[argc++] = vector_slot (vline, i);
  1872. }
  1873. if (argc >= CMD_ARGC_MAX)
  1874. return CMD_ERR_EXEED_ARGC_MAX;
  1875. }
  1876. /* For vtysh execution. */
  1877. if (cmd)
  1878. *cmd = matched_element;
  1879. if (matched_element->daemon)
  1880. return CMD_SUCCESS_DAEMON;
  1881. /* Now execute matched command */
  1882. return (*matched_element->func) (matched_element, vty, argc, argv);
  1883. }
  1884. /* Configration make from file. */
  1885. int
  1886. config_from_file (struct vty *vty, FILE *fp)
  1887. {
  1888. int ret;
  1889. vector vline;
  1890. while (fgets (vty->buf, VTY_BUFSIZ, fp))
  1891. {
  1892. vline = cmd_make_strvec (vty->buf);
  1893. /* In case of comment line */
  1894. if (vline == NULL)
  1895. continue;
  1896. /* Execute configuration command : this is strict match */
  1897. ret = cmd_execute_command_strict (vline, vty, NULL);
  1898. /* Try again with setting node to CONFIG_NODE */
  1899. while (ret != CMD_SUCCESS && ret != CMD_WARNING
  1900. && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE)
  1901. {
  1902. vty->node = node_parent(vty->node);
  1903. ret = cmd_execute_command_strict (vline, vty, NULL);
  1904. }
  1905. cmd_free_strvec (vline);
  1906. if (ret != CMD_SUCCESS && ret != CMD_WARNING
  1907. && ret != CMD_ERR_NOTHING_TODO)
  1908. return ret;
  1909. }
  1910. return CMD_SUCCESS;
  1911. }
  1912. /* Configration from terminal */
  1913. DEFUN (config_terminal,
  1914. config_terminal_cmd,
  1915. "configure terminal",
  1916. "Configuration from vty interface\n"
  1917. "Configuration terminal\n")
  1918. {
  1919. if (vty_config_lock (vty))
  1920. vty->node = CONFIG_NODE;
  1921. else
  1922. {
  1923. vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);
  1924. return CMD_WARNING;
  1925. }
  1926. return CMD_SUCCESS;
  1927. }
  1928. /* Enable command */
  1929. DEFUN (enable,
  1930. config_enable_cmd,
  1931. "enable",
  1932. "Turn on privileged mode command\n")
  1933. {
  1934. /* If enable password is NULL, change to ENABLE_NODE */
  1935. if ((host.enable == NULL && host.enable_encrypt == NULL) ||
  1936. vty->type == VTY_SHELL_SERV)
  1937. vty->node = ENABLE_NODE;
  1938. else
  1939. vty->node = AUTH_ENABLE_NODE;
  1940. return CMD_SUCCESS;
  1941. }
  1942. /* Disable command */
  1943. DEFUN (disable,
  1944. config_disable_cmd,
  1945. "disable",
  1946. "Turn off privileged mode command\n")
  1947. {
  1948. if (vty->node == ENABLE_NODE)
  1949. vty->node = VIEW_NODE;
  1950. return CMD_SUCCESS;
  1951. }
  1952. /* Down vty node level. */
  1953. DEFUN (config_exit,
  1954. config_exit_cmd,
  1955. "exit",
  1956. "Exit current mode and down to previous mode\n")
  1957. {
  1958. switch (vty->node)
  1959. {
  1960. case VIEW_NODE:
  1961. case ENABLE_NODE:
  1962. case RESTRICTED_NODE:
  1963. if (vty_shell (vty))
  1964. exit (0);
  1965. else
  1966. vty->status = VTY_CLOSE;
  1967. break;
  1968. case CONFIG_NODE:
  1969. vty->node = ENABLE_NODE;
  1970. vty_config_unlock (vty);
  1971. break;
  1972. case INTERFACE_NODE:
  1973. case ZEBRA_NODE:
  1974. case BGP_NODE:
  1975. case RIP_NODE:
  1976. case RIPNG_NODE:
  1977. case BABEL_NODE:
  1978. case OSPF_NODE:
  1979. case OSPF6_NODE:
  1980. case ISIS_NODE:
  1981. case KEYCHAIN_NODE:
  1982. case MASC_NODE:
  1983. case RMAP_NODE:
  1984. case VTY_NODE:
  1985. vty->node = CONFIG_NODE;
  1986. break;
  1987. case BGP_VPNV4_NODE:
  1988. case BGP_IPV4_NODE:
  1989. case BGP_IPV4M_NODE:
  1990. case BGP_IPV6_NODE:
  1991. case BGP_IPV6M_NODE:
  1992. vty->node = BGP_NODE;
  1993. break;
  1994. case KEYCHAIN_KEY_NODE:
  1995. vty->node = KEYCHAIN_NODE;
  1996. break;
  1997. default:
  1998. break;
  1999. }
  2000. return CMD_SUCCESS;
  2001. }
  2002. /* quit is alias of exit. */
  2003. ALIAS (config_exit,
  2004. config_quit_cmd,
  2005. "quit",
  2006. "Exit current mode and down to previous mode\n")
  2007. /* End of configuration. */
  2008. DEFUN (config_end,
  2009. config_end_cmd,
  2010. "end",
  2011. "End current mode and change to enable mode.")
  2012. {
  2013. switch (vty->node)
  2014. {
  2015. case VIEW_NODE:
  2016. case ENABLE_NODE:
  2017. case RESTRICTED_NODE:
  2018. /* Nothing to do. */
  2019. break;
  2020. case CONFIG_NODE:
  2021. case INTERFACE_NODE:
  2022. case ZEBRA_NODE:
  2023. case RIP_NODE:
  2024. case RIPNG_NODE:
  2025. case BABEL_NODE:
  2026. case BGP_NODE:
  2027. case BGP_VPNV4_NODE:
  2028. case BGP_IPV4_NODE:
  2029. case BGP_IPV4M_NODE:
  2030. case BGP_IPV6_NODE:
  2031. case BGP_IPV6M_NODE:
  2032. case RMAP_NODE:
  2033. case OSPF_NODE:
  2034. case OSPF6_NODE:
  2035. case ISIS_NODE:
  2036. case KEYCHAIN_NODE:
  2037. case KEYCHAIN_KEY_NODE:
  2038. case MASC_NODE:
  2039. case VTY_NODE:
  2040. vty_config_unlock (vty);
  2041. vty->node = ENABLE_NODE;
  2042. break;
  2043. default:
  2044. break;
  2045. }
  2046. return CMD_SUCCESS;
  2047. }
  2048. /* Show version. */
  2049. DEFUN (show_version,
  2050. show_version_cmd,
  2051. "show version",
  2052. SHOW_STR
  2053. "Displays zebra version\n")
  2054. {
  2055. vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"",
  2056. VTY_NEWLINE);
  2057. vty_out (vty, "%s%s%s", QUAGGA_COPYRIGHT, GIT_INFO, VTY_NEWLINE);
  2058. return CMD_SUCCESS;
  2059. }
  2060. /* Help display function for all node. */
  2061. DEFUN (config_help,
  2062. config_help_cmd,
  2063. "help",
  2064. "Description of the interactive help system\n")
  2065. {
  2066. vty_out (vty,
  2067. "Quagga VTY provides advanced help feature. When you need help,%s\
  2068. anytime at the command line please press '?'.%s\
  2069. %s\
  2070. If nothing matches, the help list will be empty and you must backup%s\
  2071. until entering a '?' shows the available options.%s\
  2072. Two styles of help are provided:%s\
  2073. 1. Full help is available when you are ready to enter a%s\
  2074. command argument (e.g. 'show ?') and describes each possible%s\
  2075. argument.%s\
  2076. 2. Partial help is provided when an abbreviated argument is entered%s\
  2077. and you want to know what arguments match the input%s\
  2078. (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
  2079. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
  2080. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
  2081. return CMD_SUCCESS;
  2082. }
  2083. /* Help display function for all node. */
  2084. DEFUN (config_list,
  2085. config_list_cmd,
  2086. "list",
  2087. "Print command list\n")
  2088. {
  2089. unsigned int i;
  2090. struct cmd_node *cnode = vector_slot (cmdvec, vty->node);
  2091. struct cmd_element *cmd;
  2092. for (i = 0; i < vector_active (cnode->cmd_vector); i++)
  2093. if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL
  2094. && !(cmd->attr == CMD_ATTR_DEPRECATED
  2095. || cmd->attr == CMD_ATTR_HIDDEN))
  2096. vty_out (vty, " %s%s", cmd->string,
  2097. VTY_NEWLINE);
  2098. return CMD_SUCCESS;
  2099. }
  2100. /* Write current configuration into file. */
  2101. DEFUN (config_write_file,
  2102. config_write_file_cmd,
  2103. "write file",
  2104. "Write running configuration to memory, network, or terminal\n"
  2105. "Write to configuration file\n")
  2106. {
  2107. unsigned int i;
  2108. int fd;
  2109. struct cmd_node *node;
  2110. char *config_file;
  2111. char *config_file_tmp = NULL;
  2112. char *config_file_sav = NULL;
  2113. int ret = CMD_WARNING;
  2114. struct vty *file_vty;
  2115. /* Check and see if we are operating under vtysh configuration */
  2116. if (host.config == NULL)
  2117. {
  2118. vty_out (vty, "Can't save to configuration file, using vtysh.%s",
  2119. VTY_NEWLINE);
  2120. return CMD_WARNING;
  2121. }
  2122. /* Get filename. */
  2123. config_file = host.config;
  2124. config_file_sav =
  2125. XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
  2126. strcpy (config_file_sav, config_file);
  2127. strcat (config_file_sav, CONF_BACKUP_EXT);
  2128. config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
  2129. sprintf (config_file_tmp, "%s.XXXXXX", config_file);
  2130. /* Open file to configuration write. */
  2131. fd = mkstemp (config_file_tmp);
  2132. if (fd < 0)
  2133. {
  2134. vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,
  2135. VTY_NEWLINE);
  2136. goto finished;
  2137. }
  2138. /* Make vty for configuration file. */
  2139. file_vty = vty_new ();
  2140. file_vty->fd = fd;
  2141. file_vty->type = VTY_FILE;
  2142. /* Config file header print. */
  2143. vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! ");
  2144. vty_time_print (file_vty, 1);
  2145. vty_out (file_vty, "!\n");
  2146. for (i = 0; i < vector_active (cmdvec); i++)
  2147. if ((node = vector_slot (cmdvec, i)) && node->func)
  2148. {
  2149. if ((*node->func) (file_vty))
  2150. vty_out (file_vty, "!\n");
  2151. }
  2152. vty_close (file_vty);
  2153. if (unlink (config_file_sav) != 0)
  2154. if (errno != ENOENT)
  2155. {
  2156. vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,
  2157. VTY_NEWLINE);
  2158. goto finished;
  2159. }
  2160. if (link (config_file, config_file_sav) != 0)
  2161. {
  2162. vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,
  2163. VTY_NEWLINE);
  2164. goto finished;
  2165. }
  2166. sync ();
  2167. if (unlink (config_file) != 0)
  2168. {
  2169. vty_out (vty, "Can't unlink configuration file %s.%s", config_file,
  2170. VTY_NEWLINE);
  2171. goto finished;
  2172. }
  2173. if (link (config_file_tmp, config_file) != 0)
  2174. {
  2175. vty_out (vty, "Can't save configuration file %s.%s", config_file,
  2176. VTY_NEWLINE);
  2177. goto finished;
  2178. }
  2179. sync ();
  2180. if (chmod (config_file, CONFIGFILE_MASK) != 0)
  2181. {
  2182. vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
  2183. config_file, safe_strerror(errno), errno, VTY_NEWLINE);
  2184. goto finished;
  2185. }
  2186. vty_out (vty, "Configuration saved to %s%s", config_file,
  2187. VTY_NEWLINE);
  2188. ret = CMD_SUCCESS;
  2189. finished:
  2190. unlink (config_file_tmp);
  2191. XFREE (MTYPE_TMP, config_file_tmp);
  2192. XFREE (MTYPE_TMP, config_file_sav);
  2193. return ret;
  2194. }
  2195. ALIAS (config_write_file,
  2196. config_write_cmd,
  2197. "write",
  2198. "Write running configuration to memory, network, or terminal\n")
  2199. ALIAS (config_write_file,
  2200. config_write_memory_cmd,
  2201. "write memory",
  2202. "Write running configuration to memory, network, or terminal\n"
  2203. "Write configuration to the file (same as write file)\n")
  2204. ALIAS (config_write_file,
  2205. copy_runningconfig_startupconfig_cmd,
  2206. "copy running-config startup-config",
  2207. "Copy configuration\n"
  2208. "Copy running config to... \n"
  2209. "Copy running config to startup config (same as write file)\n")
  2210. /* Write current configuration into the terminal. */
  2211. DEFUN (config_write_terminal,
  2212. config_write_terminal_cmd,
  2213. "write terminal",
  2214. "Write running configuration to memory, network, or terminal\n"
  2215. "Write to terminal\n")
  2216. {
  2217. unsigned int i;
  2218. struct cmd_node *node;
  2219. if (vty->type == VTY_SHELL_SERV)
  2220. {
  2221. for (i = 0; i < vector_active (cmdvec); i++)
  2222. if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)
  2223. {
  2224. if ((*node->func) (vty))
  2225. vty_out (vty, "!%s", VTY_NEWLINE);
  2226. }
  2227. }
  2228. else
  2229. {
  2230. vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
  2231. VTY_NEWLINE);
  2232. vty_out (vty, "!%s", VTY_NEWLINE);
  2233. for (i = 0; i < vector_active (cmdvec); i++)
  2234. if ((node = vector_slot (cmdvec, i)) && node->func)
  2235. {
  2236. if ((*node->func) (vty))
  2237. vty_out (vty, "!%s", VTY_NEWLINE);
  2238. }
  2239. vty_out (vty, "end%s",VTY_NEWLINE);
  2240. }
  2241. return CMD_SUCCESS;
  2242. }
  2243. /* Write current configuration into the terminal. */
  2244. ALIAS (config_write_terminal,
  2245. show_running_config_cmd,
  2246. "show running-config",
  2247. SHOW_STR
  2248. "running configuration\n")
  2249. /* Write startup configuration into the terminal. */
  2250. DEFUN (show_startup_config,
  2251. show_startup_config_cmd,
  2252. "show startup-config",
  2253. SHOW_STR
  2254. "Contentes of startup configuration\n")
  2255. {
  2256. char buf[BUFSIZ];
  2257. FILE *confp;
  2258. confp = fopen (host.config, "r");
  2259. if (confp == NULL)
  2260. {
  2261. vty_out (vty, "Can't open configuration file [%s]%s",
  2262. host.config, VTY_NEWLINE);
  2263. return CMD_WARNING;
  2264. }
  2265. while (fgets (buf, BUFSIZ, confp))
  2266. {
  2267. char *cp = buf;
  2268. while (*cp != '\r' && *cp != '\n' && *cp != '\0')
  2269. cp++;
  2270. *cp = '\0';
  2271. vty_out (vty, "%s%s", buf, VTY_NEWLINE);
  2272. }
  2273. fclose (confp);
  2274. return CMD_SUCCESS;
  2275. }
  2276. /* Hostname configuration */
  2277. DEFUN (config_hostname,
  2278. hostname_cmd,
  2279. "hostname WORD",
  2280. "Set system's network name\n"
  2281. "This system's network name\n")
  2282. {
  2283. if (!isalpha((int) *argv[0]))
  2284. {
  2285. vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE);
  2286. return CMD_WARNING;
  2287. }
  2288. if (host.name)
  2289. XFREE (MTYPE_HOST, host.name);
  2290. host.name = XSTRDUP (MTYPE_HOST, argv[0]);
  2291. return CMD_SUCCESS;
  2292. }
  2293. DEFUN (config_no_hostname,
  2294. no_hostname_cmd,
  2295. "no hostname [HOSTNAME]",
  2296. NO_STR
  2297. "Reset system's network name\n"
  2298. "Host name of this router\n")
  2299. {
  2300. if (host.name)
  2301. XFREE (MTYPE_HOST, host.name);
  2302. host.name = NULL;
  2303. return CMD_SUCCESS;
  2304. }
  2305. /* VTY interface password set. */
  2306. DEFUN (config_password, password_cmd,
  2307. "password (8|) WORD",
  2308. "Assign the terminal connection password\n"
  2309. "Specifies a HIDDEN password will follow\n"
  2310. "dummy string \n"
  2311. "The HIDDEN line password string\n")
  2312. {
  2313. /* Argument check. */
  2314. if (argc == 0)
  2315. {
  2316. vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
  2317. return CMD_WARNING;
  2318. }
  2319. if (argc == 2)
  2320. {
  2321. if (*argv[0] == '8')
  2322. {
  2323. if (host.password)
  2324. XFREE (MTYPE_HOST, host.password);
  2325. host.password = NULL;
  2326. if (host.password_encrypt)
  2327. XFREE (MTYPE_HOST, host.password_encrypt);
  2328. host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
  2329. return CMD_SUCCESS;
  2330. }
  2331. else
  2332. {
  2333. vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
  2334. return CMD_WARNING;
  2335. }
  2336. }
  2337. if (!isalnum ((int) *argv[0]))
  2338. {
  2339. vty_out (vty,
  2340. "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
  2341. return CMD_WARNING;
  2342. }
  2343. if (host.password)
  2344. XFREE (MTYPE_HOST, host.password);
  2345. host.password = NULL;
  2346. if (host.encrypt)
  2347. {
  2348. if (host.password_encrypt)
  2349. XFREE (MTYPE_HOST, host.password_encrypt);
  2350. host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
  2351. }
  2352. else
  2353. host.password = XSTRDUP (MTYPE_HOST, argv[0]);
  2354. return CMD_SUCCESS;
  2355. }
  2356. ALIAS (config_password, password_text_cmd,
  2357. "password LINE",
  2358. "Assign the terminal connection password\n"
  2359. "The UNENCRYPTED (cleartext) line password\n")
  2360. /* VTY enable password set. */
  2361. DEFUN (config_enable_password, enable_password_cmd,
  2362. "enable password (8|) WORD",
  2363. "Modify enable password parameters\n"
  2364. "Assign the privileged level password\n"
  2365. "Specifies a HIDDEN password will follow\n"
  2366. "dummy string \n"
  2367. "The HIDDEN 'enable' password string\n")
  2368. {
  2369. /* Argument check. */
  2370. if (argc == 0)
  2371. {
  2372. vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
  2373. return CMD_WARNING;
  2374. }
  2375. /* Crypt type is specified. */
  2376. if (argc == 2)
  2377. {
  2378. if (*argv[0] == '8')
  2379. {
  2380. if (host.enable)
  2381. XFREE (MTYPE_HOST, host.enable);
  2382. host.enable = NULL;
  2383. if (host.enable_encrypt)
  2384. XFREE (MTYPE_HOST, host.enable_encrypt);
  2385. host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
  2386. return CMD_SUCCESS;
  2387. }
  2388. else
  2389. {
  2390. vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
  2391. return CMD_WARNING;
  2392. }
  2393. }
  2394. if (!isalnum ((int) *argv[0]))
  2395. {
  2396. vty_out (vty,
  2397. "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
  2398. return CMD_WARNING;
  2399. }
  2400. if (host.enable)
  2401. XFREE (MTYPE_HOST, host.enable);
  2402. host.enable = NULL;
  2403. /* Plain password input. */
  2404. if (host.encrypt)
  2405. {
  2406. if (host.enable_encrypt)
  2407. XFREE (MTYPE_HOST, host.enable_encrypt);
  2408. host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
  2409. }
  2410. else
  2411. host.enable = XSTRDUP (MTYPE_HOST, argv[0]);
  2412. return CMD_SUCCESS;
  2413. }
  2414. ALIAS (config_enable_password,
  2415. enable_password_text_cmd,
  2416. "enable password LINE",
  2417. "Modify enable password parameters\n"
  2418. "Assign the privileged level password\n"
  2419. "The UNENCRYPTED (cleartext) 'enable' password\n")
  2420. /* VTY enable password delete. */
  2421. DEFUN (no_config_enable_password, no_enable_password_cmd,
  2422. "no enable password",
  2423. NO_STR
  2424. "Modify enable password parameters\n"
  2425. "Assign the privileged level password\n")
  2426. {
  2427. if (host.enable)
  2428. XFREE (MTYPE_HOST, host.enable);
  2429. host.enable = NULL;
  2430. if (host.enable_encrypt)
  2431. XFREE (MTYPE_HOST, host.enable_encrypt);
  2432. host.enable_encrypt = NULL;
  2433. return CMD_SUCCESS;
  2434. }
  2435. DEFUN (service_password_encrypt,
  2436. service_password_encrypt_cmd,
  2437. "service password-encryption",
  2438. "Set up miscellaneous service\n"
  2439. "Enable encrypted passwords\n")
  2440. {
  2441. if (host.encrypt)
  2442. return CMD_SUCCESS;
  2443. host.encrypt = 1;
  2444. if (host.password)
  2445. {
  2446. if (host.password_encrypt)
  2447. XFREE (MTYPE_HOST, host.password_encrypt);
  2448. host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password));
  2449. }
  2450. if (host.enable)
  2451. {
  2452. if (host.enable_encrypt)
  2453. XFREE (MTYPE_HOST, host.enable_encrypt);
  2454. host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable));
  2455. }
  2456. return CMD_SUCCESS;
  2457. }
  2458. DEFUN (no_service_password_encrypt,
  2459. no_service_password_encrypt_cmd,
  2460. "no service password-encryption",
  2461. NO_STR
  2462. "Set up miscellaneous service\n"
  2463. "Enable encrypted passwords\n")
  2464. {
  2465. if (! host.encrypt)
  2466. return CMD_SUCCESS;
  2467. host.encrypt = 0;
  2468. if (host.password_encrypt)
  2469. XFREE (MTYPE_HOST, host.password_encrypt);
  2470. host.password_encrypt = NULL;
  2471. if (host.enable_encrypt)
  2472. XFREE (MTYPE_HOST, host.enable_encrypt);
  2473. host.enable_encrypt = NULL;
  2474. return CMD_SUCCESS;
  2475. }
  2476. DEFUN (config_terminal_length, config_terminal_length_cmd,
  2477. "terminal length <0-512>",
  2478. "Set terminal line parameters\n"
  2479. "Set number of lines on a screen\n"
  2480. "Number of lines on screen (0 for no pausing)\n")
  2481. {
  2482. int lines;
  2483. char *endptr = NULL;
  2484. lines = strtol (argv[0], &endptr, 10);
  2485. if (lines < 0 || lines > 512 || *endptr != '\0')
  2486. {
  2487. vty_out (vty, "length is malformed%s", VTY_NEWLINE);
  2488. return CMD_WARNING;
  2489. }
  2490. vty->lines = lines;
  2491. return CMD_SUCCESS;
  2492. }
  2493. DEFUN (config_terminal_no_length, config_terminal_no_length_cmd,
  2494. "terminal no length",
  2495. "Set terminal line parameters\n"
  2496. NO_STR
  2497. "Set number of lines on a screen\n")
  2498. {
  2499. vty->lines = -1;
  2500. return CMD_SUCCESS;
  2501. }
  2502. DEFUN (service_terminal_length, service_terminal_length_cmd,
  2503. "service terminal-length <0-512>",
  2504. "Set up miscellaneous service\n"
  2505. "System wide terminal length configuration\n"
  2506. "Number of lines of VTY (0 means no line control)\n")
  2507. {
  2508. int lines;
  2509. char *endptr = NULL;
  2510. lines = strtol (argv[0], &endptr, 10);
  2511. if (lines < 0 || lines > 512 || *endptr != '\0')
  2512. {
  2513. vty_out (vty, "length is malformed%s", VTY_NEWLINE);
  2514. return CMD_WARNING;
  2515. }
  2516. host.lines = lines;
  2517. return CMD_SUCCESS;
  2518. }
  2519. DEFUN (no_service_terminal_length, no_service_terminal_length_cmd,
  2520. "no service terminal-length [<0-512>]",
  2521. NO_STR
  2522. "Set up miscellaneous service\n"
  2523. "System wide terminal length configuration\n"
  2524. "Number of lines of VTY (0 means no line control)\n")
  2525. {
  2526. host.lines = -1;
  2527. return CMD_SUCCESS;
  2528. }
  2529. DEFUN_HIDDEN (do_echo,
  2530. echo_cmd,
  2531. "echo .MESSAGE",
  2532. "Echo a message back to the vty\n"
  2533. "The message to echo\n")
  2534. {
  2535. char *message;
  2536. vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
  2537. VTY_NEWLINE);
  2538. if (message)
  2539. XFREE(MTYPE_TMP, message);
  2540. return CMD_SUCCESS;
  2541. }
  2542. DEFUN (config_logmsg,
  2543. config_logmsg_cmd,
  2544. "logmsg "LOG_LEVELS" .MESSAGE",
  2545. "Send a message to enabled logging destinations\n"
  2546. LOG_LEVEL_DESC
  2547. "The message to send\n")
  2548. {
  2549. int level;
  2550. char *message;
  2551. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2552. return CMD_ERR_NO_MATCH;
  2553. zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, 1)) ? message : ""));
  2554. if (message)
  2555. XFREE(MTYPE_TMP, message);
  2556. return CMD_SUCCESS;
  2557. }
  2558. DEFUN (show_logging,
  2559. show_logging_cmd,
  2560. "show logging",
  2561. SHOW_STR
  2562. "Show current logging configuration\n")
  2563. {
  2564. struct zlog *zl = zlog_default;
  2565. vty_out (vty, "Syslog logging: ");
  2566. if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
  2567. vty_out (vty, "disabled");
  2568. else
  2569. vty_out (vty, "level %s, facility %s, ident %s",
  2570. zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
  2571. facility_name(zl->facility), zl->ident);
  2572. vty_out (vty, "%s", VTY_NEWLINE);
  2573. vty_out (vty, "Stdout logging: ");
  2574. if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
  2575. vty_out (vty, "disabled");
  2576. else
  2577. vty_out (vty, "level %s",
  2578. zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
  2579. vty_out (vty, "%s", VTY_NEWLINE);
  2580. vty_out (vty, "Monitor logging: ");
  2581. if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
  2582. vty_out (vty, "disabled");
  2583. else
  2584. vty_out (vty, "level %s",
  2585. zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
  2586. vty_out (vty, "%s", VTY_NEWLINE);
  2587. vty_out (vty, "File logging: ");
  2588. if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) ||
  2589. !zl->fp)
  2590. vty_out (vty, "disabled");
  2591. else
  2592. vty_out (vty, "level %s, filename %s",
  2593. zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
  2594. zl->filename);
  2595. vty_out (vty, "%s", VTY_NEWLINE);
  2596. vty_out (vty, "Protocol name: %s%s",
  2597. zlog_proto_names[zl->protocol], VTY_NEWLINE);
  2598. vty_out (vty, "Record priority: %s%s",
  2599. (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
  2600. vty_out (vty, "Timestamp precision: %d%s",
  2601. zl->timestamp_precision, VTY_NEWLINE);
  2602. return CMD_SUCCESS;
  2603. }
  2604. DEFUN (config_log_stdout,
  2605. config_log_stdout_cmd,
  2606. "log stdout",
  2607. "Logging control\n"
  2608. "Set stdout logging level\n")
  2609. {
  2610. zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
  2611. return CMD_SUCCESS;
  2612. }
  2613. DEFUN (config_log_stdout_level,
  2614. config_log_stdout_level_cmd,
  2615. "log stdout "LOG_LEVELS,
  2616. "Logging control\n"
  2617. "Set stdout logging level\n"
  2618. LOG_LEVEL_DESC)
  2619. {
  2620. int level;
  2621. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2622. return CMD_ERR_NO_MATCH;
  2623. zlog_set_level (NULL, ZLOG_DEST_STDOUT, level);
  2624. return CMD_SUCCESS;
  2625. }
  2626. DEFUN (no_config_log_stdout,
  2627. no_config_log_stdout_cmd,
  2628. "no log stdout [LEVEL]",
  2629. NO_STR
  2630. "Logging control\n"
  2631. "Cancel logging to stdout\n"
  2632. "Logging level\n")
  2633. {
  2634. zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
  2635. return CMD_SUCCESS;
  2636. }
  2637. DEFUN (config_log_monitor,
  2638. config_log_monitor_cmd,
  2639. "log monitor",
  2640. "Logging control\n"
  2641. "Set terminal line (monitor) logging level\n")
  2642. {
  2643. zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
  2644. return CMD_SUCCESS;
  2645. }
  2646. DEFUN (config_log_monitor_level,
  2647. config_log_monitor_level_cmd,
  2648. "log monitor "LOG_LEVELS,
  2649. "Logging control\n"
  2650. "Set terminal line (monitor) logging level\n"
  2651. LOG_LEVEL_DESC)
  2652. {
  2653. int level;
  2654. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2655. return CMD_ERR_NO_MATCH;
  2656. zlog_set_level (NULL, ZLOG_DEST_MONITOR, level);
  2657. return CMD_SUCCESS;
  2658. }
  2659. DEFUN (no_config_log_monitor,
  2660. no_config_log_monitor_cmd,
  2661. "no log monitor [LEVEL]",
  2662. NO_STR
  2663. "Logging control\n"
  2664. "Disable terminal line (monitor) logging\n"
  2665. "Logging level\n")
  2666. {
  2667. zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
  2668. return CMD_SUCCESS;
  2669. }
  2670. static int
  2671. set_log_file(struct vty *vty, const char *fname, int loglevel)
  2672. {
  2673. int ret;
  2674. char *p = NULL;
  2675. const char *fullpath;
  2676. /* Path detection. */
  2677. if (! IS_DIRECTORY_SEP (*fname))
  2678. {
  2679. char cwd[MAXPATHLEN+1];
  2680. cwd[MAXPATHLEN] = '\0';
  2681. if (getcwd (cwd, MAXPATHLEN) == NULL)
  2682. {
  2683. zlog_err ("config_log_file: Unable to alloc mem!");
  2684. return CMD_WARNING;
  2685. }
  2686. if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2))
  2687. == NULL)
  2688. {
  2689. zlog_err ("config_log_file: Unable to alloc mem!");
  2690. return CMD_WARNING;
  2691. }
  2692. sprintf (p, "%s/%s", cwd, fname);
  2693. fullpath = p;
  2694. }
  2695. else
  2696. fullpath = fname;
  2697. ret = zlog_set_file (NULL, fullpath, loglevel);
  2698. if (p)
  2699. XFREE (MTYPE_TMP, p);
  2700. if (!ret)
  2701. {
  2702. vty_out (vty, "can't open logfile %s\n", fname);
  2703. return CMD_WARNING;
  2704. }
  2705. if (host.logfile)
  2706. XFREE (MTYPE_HOST, host.logfile);
  2707. host.logfile = XSTRDUP (MTYPE_HOST, fname);
  2708. return CMD_SUCCESS;
  2709. }
  2710. DEFUN (config_log_file,
  2711. config_log_file_cmd,
  2712. "log file FILENAME",
  2713. "Logging control\n"
  2714. "Logging to file\n"
  2715. "Logging filename\n")
  2716. {
  2717. return set_log_file(vty, argv[0], zlog_default->default_lvl);
  2718. }
  2719. DEFUN (config_log_file_level,
  2720. config_log_file_level_cmd,
  2721. "log file FILENAME "LOG_LEVELS,
  2722. "Logging control\n"
  2723. "Logging to file\n"
  2724. "Logging filename\n"
  2725. LOG_LEVEL_DESC)
  2726. {
  2727. int level;
  2728. if ((level = level_match(argv[1])) == ZLOG_DISABLED)
  2729. return CMD_ERR_NO_MATCH;
  2730. return set_log_file(vty, argv[0], level);
  2731. }
  2732. DEFUN (no_config_log_file,
  2733. no_config_log_file_cmd,
  2734. "no log file [FILENAME]",
  2735. NO_STR
  2736. "Logging control\n"
  2737. "Cancel logging to file\n"
  2738. "Logging file name\n")
  2739. {
  2740. zlog_reset_file (NULL);
  2741. if (host.logfile)
  2742. XFREE (MTYPE_HOST, host.logfile);
  2743. host.logfile = NULL;
  2744. return CMD_SUCCESS;
  2745. }
  2746. ALIAS (no_config_log_file,
  2747. no_config_log_file_level_cmd,
  2748. "no log file FILENAME LEVEL",
  2749. NO_STR
  2750. "Logging control\n"
  2751. "Cancel logging to file\n"
  2752. "Logging file name\n"
  2753. "Logging level\n")
  2754. DEFUN (config_log_syslog,
  2755. config_log_syslog_cmd,
  2756. "log syslog",
  2757. "Logging control\n"
  2758. "Set syslog logging level\n")
  2759. {
  2760. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
  2761. return CMD_SUCCESS;
  2762. }
  2763. DEFUN (config_log_syslog_level,
  2764. config_log_syslog_level_cmd,
  2765. "log syslog "LOG_LEVELS,
  2766. "Logging control\n"
  2767. "Set syslog logging level\n"
  2768. LOG_LEVEL_DESC)
  2769. {
  2770. int level;
  2771. if ((level = level_match(argv[0])) == ZLOG_DISABLED)
  2772. return CMD_ERR_NO_MATCH;
  2773. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level);
  2774. return CMD_SUCCESS;
  2775. }
  2776. DEFUN_DEPRECATED (config_log_syslog_facility,
  2777. config_log_syslog_facility_cmd,
  2778. "log syslog facility "LOG_FACILITIES,
  2779. "Logging control\n"
  2780. "Logging goes to syslog\n"
  2781. "(Deprecated) Facility parameter for syslog messages\n"
  2782. LOG_FACILITY_DESC)
  2783. {
  2784. int facility;
  2785. if ((facility = facility_match(argv[0])) < 0)
  2786. return CMD_ERR_NO_MATCH;
  2787. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
  2788. zlog_default->facility = facility;
  2789. return CMD_SUCCESS;
  2790. }
  2791. DEFUN (no_config_log_syslog,
  2792. no_config_log_syslog_cmd,
  2793. "no log syslog [LEVEL]",
  2794. NO_STR
  2795. "Logging control\n"
  2796. "Cancel logging to syslog\n"
  2797. "Logging level\n")
  2798. {
  2799. zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
  2800. return CMD_SUCCESS;
  2801. }
  2802. ALIAS (no_config_log_syslog,
  2803. no_config_log_syslog_facility_cmd,
  2804. "no log syslog facility "LOG_FACILITIES,
  2805. NO_STR
  2806. "Logging control\n"
  2807. "Logging goes to syslog\n"
  2808. "Facility parameter for syslog messages\n"
  2809. LOG_FACILITY_DESC)
  2810. DEFUN (config_log_facility,
  2811. config_log_facility_cmd,
  2812. "log facility "LOG_FACILITIES,
  2813. "Logging control\n"
  2814. "Facility parameter for syslog messages\n"
  2815. LOG_FACILITY_DESC)
  2816. {
  2817. int facility;
  2818. if ((facility = facility_match(argv[0])) < 0)
  2819. return CMD_ERR_NO_MATCH;
  2820. zlog_default->facility = facility;
  2821. return CMD_SUCCESS;
  2822. }
  2823. DEFUN (no_config_log_facility,
  2824. no_config_log_facility_cmd,
  2825. "no log facility [FACILITY]",
  2826. NO_STR
  2827. "Logging control\n"
  2828. "Reset syslog facility to default (daemon)\n"
  2829. "Syslog facility\n")
  2830. {
  2831. zlog_default->facility = LOG_DAEMON;
  2832. return CMD_SUCCESS;
  2833. }
  2834. DEFUN_DEPRECATED (config_log_trap,
  2835. config_log_trap_cmd,
  2836. "log trap "LOG_LEVELS,
  2837. "Logging control\n"
  2838. "(Deprecated) Set logging level and default for all destinations\n"
  2839. LOG_LEVEL_DESC)
  2840. {
  2841. int new_level ;
  2842. int i;
  2843. if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
  2844. return CMD_ERR_NO_MATCH;
  2845. zlog_default->default_lvl = new_level;
  2846. for (i = 0; i < ZLOG_NUM_DESTS; i++)
  2847. if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
  2848. zlog_default->maxlvl[i] = new_level;
  2849. return CMD_SUCCESS;
  2850. }
  2851. DEFUN_DEPRECATED (no_config_log_trap,
  2852. no_config_log_trap_cmd,
  2853. "no log trap [LEVEL]",
  2854. NO_STR
  2855. "Logging control\n"
  2856. "Permit all logging information\n"
  2857. "Logging level\n")
  2858. {
  2859. zlog_default->default_lvl = LOG_DEBUG;
  2860. return CMD_SUCCESS;
  2861. }
  2862. DEFUN (config_log_record_priority,
  2863. config_log_record_priority_cmd,
  2864. "log record-priority",
  2865. "Logging control\n"
  2866. "Log the priority of the message within the message\n")
  2867. {
  2868. zlog_default->record_priority = 1 ;
  2869. return CMD_SUCCESS;
  2870. }
  2871. DEFUN (no_config_log_record_priority,
  2872. no_config_log_record_priority_cmd,
  2873. "no log record-priority",
  2874. NO_STR
  2875. "Logging control\n"
  2876. "Do not log the priority of the message within the message\n")
  2877. {
  2878. zlog_default->record_priority = 0 ;
  2879. return CMD_SUCCESS;
  2880. }
  2881. DEFUN (config_log_timestamp_precision,
  2882. config_log_timestamp_precision_cmd,
  2883. "log timestamp precision <0-6>",
  2884. "Logging control\n"
  2885. "Timestamp configuration\n"
  2886. "Set the timestamp precision\n"
  2887. "Number of subsecond digits\n")
  2888. {
  2889. if (argc != 1)
  2890. {
  2891. vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
  2892. return CMD_WARNING;
  2893. }
  2894. VTY_GET_INTEGER_RANGE("Timestamp Precision",
  2895. zlog_default->timestamp_precision, argv[0], 0, 6);
  2896. return CMD_SUCCESS;
  2897. }
  2898. DEFUN (no_config_log_timestamp_precision,
  2899. no_config_log_timestamp_precision_cmd,
  2900. "no log timestamp precision",
  2901. NO_STR
  2902. "Logging control\n"
  2903. "Timestamp configuration\n"
  2904. "Reset the timestamp precision to the default value of 0\n")
  2905. {
  2906. zlog_default->timestamp_precision = 0 ;
  2907. return CMD_SUCCESS;
  2908. }
  2909. DEFUN (banner_motd_file,
  2910. banner_motd_file_cmd,
  2911. "banner motd file [FILE]",
  2912. "Set banner\n"
  2913. "Banner for motd\n"
  2914. "Banner from a file\n"
  2915. "Filename\n")
  2916. {
  2917. if (host.motdfile)
  2918. XFREE (MTYPE_HOST, host.motdfile);
  2919. host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]);
  2920. return CMD_SUCCESS;
  2921. }
  2922. DEFUN (banner_motd_default,
  2923. banner_motd_default_cmd,
  2924. "banner motd default",
  2925. "Set banner string\n"
  2926. "Strings for motd\n"
  2927. "Default string\n")
  2928. {
  2929. host.motd = default_motd;
  2930. return CMD_SUCCESS;
  2931. }
  2932. DEFUN (no_banner_motd,
  2933. no_banner_motd_cmd,
  2934. "no banner motd",
  2935. NO_STR
  2936. "Set banner string\n"
  2937. "Strings for motd\n")
  2938. {
  2939. host.motd = NULL;
  2940. if (host.motdfile)
  2941. XFREE (MTYPE_HOST, host.motdfile);
  2942. host.motdfile = NULL;
  2943. return CMD_SUCCESS;
  2944. }
  2945. /* Set config filename. Called from vty.c */
  2946. void
  2947. host_config_set (char *filename)
  2948. {
  2949. if (host.config)
  2950. XFREE (MTYPE_HOST, host.config);
  2951. host.config = XSTRDUP (MTYPE_HOST, filename);
  2952. }
  2953. void
  2954. install_default (enum node_type node)
  2955. {
  2956. install_element (node, &config_exit_cmd);
  2957. install_element (node, &config_quit_cmd);
  2958. install_element (node, &config_end_cmd);
  2959. install_element (node, &config_help_cmd);
  2960. install_element (node, &config_list_cmd);
  2961. install_element (node, &config_write_terminal_cmd);
  2962. install_element (node, &config_write_file_cmd);
  2963. install_element (node, &config_write_memory_cmd);
  2964. install_element (node, &config_write_cmd);
  2965. install_element (node, &show_running_config_cmd);
  2966. }
  2967. /* Initialize command interface. Install basic nodes and commands. */
  2968. void
  2969. cmd_init (int terminal)
  2970. {
  2971. command_cr = XSTRDUP(MTYPE_STRVEC, "<cr>");
  2972. desc_cr.cmd = command_cr;
  2973. desc_cr.str = XSTRDUP(MTYPE_STRVEC, "");
  2974. /* Allocate initial top vector of commands. */
  2975. cmdvec = vector_init (VECTOR_MIN_SIZE);
  2976. /* Default host value settings. */
  2977. host.name = NULL;
  2978. host.password = NULL;
  2979. host.enable = NULL;
  2980. host.logfile = NULL;
  2981. host.config = NULL;
  2982. host.lines = -1;
  2983. host.motd = default_motd;
  2984. host.motdfile = NULL;
  2985. /* Install top nodes. */
  2986. install_node (&view_node, NULL);
  2987. install_node (&enable_node, NULL);
  2988. install_node (&auth_node, NULL);
  2989. install_node (&auth_enable_node, NULL);
  2990. install_node (&restricted_node, NULL);
  2991. install_node (&config_node, config_write_host);
  2992. /* Each node's basic commands. */
  2993. install_element (VIEW_NODE, &show_version_cmd);
  2994. if (terminal)
  2995. {
  2996. install_element (VIEW_NODE, &config_list_cmd);
  2997. install_element (VIEW_NODE, &config_exit_cmd);
  2998. install_element (VIEW_NODE, &config_quit_cmd);
  2999. install_element (VIEW_NODE, &config_help_cmd);
  3000. install_element (VIEW_NODE, &config_enable_cmd);
  3001. install_element (VIEW_NODE, &config_terminal_length_cmd);
  3002. install_element (VIEW_NODE, &config_terminal_no_length_cmd);
  3003. install_element (VIEW_NODE, &show_logging_cmd);
  3004. install_element (VIEW_NODE, &echo_cmd);
  3005. install_element (RESTRICTED_NODE, &config_list_cmd);
  3006. install_element (RESTRICTED_NODE, &config_exit_cmd);
  3007. install_element (RESTRICTED_NODE, &config_quit_cmd);
  3008. install_element (RESTRICTED_NODE, &config_help_cmd);
  3009. install_element (RESTRICTED_NODE, &config_enable_cmd);
  3010. install_element (RESTRICTED_NODE, &config_terminal_length_cmd);
  3011. install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd);
  3012. install_element (RESTRICTED_NODE, &echo_cmd);
  3013. }
  3014. if (terminal)
  3015. {
  3016. install_default (ENABLE_NODE);
  3017. install_element (ENABLE_NODE, &config_disable_cmd);
  3018. install_element (ENABLE_NODE, &config_terminal_cmd);
  3019. install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
  3020. }
  3021. install_element (ENABLE_NODE, &show_startup_config_cmd);
  3022. install_element (ENABLE_NODE, &show_version_cmd);
  3023. if (terminal)
  3024. {
  3025. install_element (ENABLE_NODE, &config_terminal_length_cmd);
  3026. install_element (ENABLE_NODE, &config_terminal_no_length_cmd);
  3027. install_element (ENABLE_NODE, &show_logging_cmd);
  3028. install_element (ENABLE_NODE, &echo_cmd);
  3029. install_element (ENABLE_NODE, &config_logmsg_cmd);
  3030. install_default (CONFIG_NODE);
  3031. }
  3032. install_element (CONFIG_NODE, &hostname_cmd);
  3033. install_element (CONFIG_NODE, &no_hostname_cmd);
  3034. if (terminal)
  3035. {
  3036. install_element (CONFIG_NODE, &password_cmd);
  3037. install_element (CONFIG_NODE, &password_text_cmd);
  3038. install_element (CONFIG_NODE, &enable_password_cmd);
  3039. install_element (CONFIG_NODE, &enable_password_text_cmd);
  3040. install_element (CONFIG_NODE, &no_enable_password_cmd);
  3041. install_element (CONFIG_NODE, &config_log_stdout_cmd);
  3042. install_element (CONFIG_NODE, &config_log_stdout_level_cmd);
  3043. install_element (CONFIG_NODE, &no_config_log_stdout_cmd);
  3044. install_element (CONFIG_NODE, &config_log_monitor_cmd);
  3045. install_element (CONFIG_NODE, &config_log_monitor_level_cmd);
  3046. install_element (CONFIG_NODE, &no_config_log_monitor_cmd);
  3047. install_element (CONFIG_NODE, &config_log_file_cmd);
  3048. install_element (CONFIG_NODE, &config_log_file_level_cmd);
  3049. install_element (CONFIG_NODE, &no_config_log_file_cmd);
  3050. install_element (CONFIG_NODE, &no_config_log_file_level_cmd);
  3051. install_element (CONFIG_NODE, &config_log_syslog_cmd);
  3052. install_element (CONFIG_NODE, &config_log_syslog_level_cmd);
  3053. install_element (CONFIG_NODE, &config_log_syslog_facility_cmd);
  3054. install_element (CONFIG_NODE, &no_config_log_syslog_cmd);
  3055. install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd);
  3056. install_element (CONFIG_NODE, &config_log_facility_cmd);
  3057. install_element (CONFIG_NODE, &no_config_log_facility_cmd);
  3058. install_element (CONFIG_NODE, &config_log_trap_cmd);
  3059. install_element (CONFIG_NODE, &no_config_log_trap_cmd);
  3060. install_element (CONFIG_NODE, &config_log_record_priority_cmd);
  3061. install_element (CONFIG_NODE, &no_config_log_record_priority_cmd);
  3062. install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd);
  3063. install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd);
  3064. install_element (CONFIG_NODE, &service_password_encrypt_cmd);
  3065. install_element (CONFIG_NODE, &no_service_password_encrypt_cmd);
  3066. install_element (CONFIG_NODE, &banner_motd_default_cmd);
  3067. install_element (CONFIG_NODE, &banner_motd_file_cmd);
  3068. install_element (CONFIG_NODE, &no_banner_motd_cmd);
  3069. install_element (CONFIG_NODE, &service_terminal_length_cmd);
  3070. install_element (CONFIG_NODE, &no_service_terminal_length_cmd);
  3071. install_element (VIEW_NODE, &show_thread_cpu_cmd);
  3072. install_element (ENABLE_NODE, &show_thread_cpu_cmd);
  3073. install_element (RESTRICTED_NODE, &show_thread_cpu_cmd);
  3074. install_element (ENABLE_NODE, &clear_thread_cpu_cmd);
  3075. install_element (VIEW_NODE, &show_work_queues_cmd);
  3076. install_element (ENABLE_NODE, &show_work_queues_cmd);
  3077. }
  3078. srand(time(NULL));
  3079. }
  3080. void
  3081. cmd_terminate ()
  3082. {
  3083. unsigned int i, j, k, l;
  3084. struct cmd_node *cmd_node;
  3085. struct cmd_element *cmd_element;
  3086. struct desc *desc;
  3087. vector cmd_node_v, cmd_element_v, desc_v;
  3088. if (cmdvec)
  3089. {
  3090. for (i = 0; i < vector_active (cmdvec); i++)
  3091. if ((cmd_node = vector_slot (cmdvec, i)) != NULL)
  3092. {
  3093. cmd_node_v = cmd_node->cmd_vector;
  3094. for (j = 0; j < vector_active (cmd_node_v); j++)
  3095. if ((cmd_element = vector_slot (cmd_node_v, j)) != NULL &&
  3096. cmd_element->strvec != NULL)
  3097. {
  3098. cmd_element_v = cmd_element->strvec;
  3099. for (k = 0; k < vector_active (cmd_element_v); k++)
  3100. if ((desc_v = vector_slot (cmd_element_v, k)) != NULL)
  3101. {
  3102. for (l = 0; l < vector_active (desc_v); l++)
  3103. if ((desc = vector_slot (desc_v, l)) != NULL)
  3104. {
  3105. if (desc->cmd)
  3106. XFREE (MTYPE_STRVEC, desc->cmd);
  3107. if (desc->str)
  3108. XFREE (MTYPE_STRVEC, desc->str);
  3109. XFREE (MTYPE_DESC, desc);
  3110. }
  3111. vector_free (desc_v);
  3112. }
  3113. cmd_element->strvec = NULL;
  3114. vector_free (cmd_element_v);
  3115. }
  3116. vector_free (cmd_node_v);
  3117. }
  3118. vector_free (cmdvec);
  3119. cmdvec = NULL;
  3120. }
  3121. if (command_cr)
  3122. XFREE(MTYPE_STRVEC, command_cr);
  3123. if (desc_cr.str)
  3124. XFREE(MTYPE_STRVEC, desc_cr.str);
  3125. if (host.name)
  3126. XFREE (MTYPE_HOST, host.name);
  3127. if (host.password)
  3128. XFREE (MTYPE_HOST, host.password);
  3129. if (host.password_encrypt)
  3130. XFREE (MTYPE_HOST, host.password_encrypt);
  3131. if (host.enable)
  3132. XFREE (MTYPE_HOST, host.enable);
  3133. if (host.enable_encrypt)
  3134. XFREE (MTYPE_HOST, host.enable_encrypt);
  3135. if (host.logfile)
  3136. XFREE (MTYPE_HOST, host.logfile);
  3137. if (host.motdfile)
  3138. XFREE (MTYPE_HOST, host.motdfile);
  3139. if (host.config)
  3140. XFREE (MTYPE_HOST, host.config);
  3141. }