command.c 107 KB

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