command.c 104 KB

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