plugin.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* plugin.c - routines for setting up and calling plugins */
  42. #include <stddef.h>
  43. #include <stdio.h>
  44. #include <plhash.h>
  45. #include "slap.h"
  46. /* this defines are used for plugin configuration */
  47. #define LOCAL_DATA "local data"
  48. #define REMOTE_DATA "remote data"
  49. #define ALL_DATA "*"
  50. #define ROOT_BIND "directory manager"
  51. #define ANONYMOUS_BIND "anonymous"
  52. /* dependency checking flags */
  53. #define CHECK_ALL 0
  54. #define CHECK_TYPE 1
  55. /* plugin removal flags */
  56. #define PLUGIN_NOT_FOUND 0
  57. #define PLUGIN_REMOVED 1
  58. #define PLUGIN_BUSY 2
  59. static char *critical_plugins[] = { "cn=ldbm database,cn=plugins,cn=config",
  60. "cn=ACL Plugin,cn=plugins,cn=config",
  61. "cn=ACL preoperation,cn=plugins,cn=config",
  62. "cn=chaining database,cn=plugins,cn=config",
  63. "cn=Multimaster Replication Plugin,cn=plugins,cn=config",
  64. NULL };
  65. /* Forward Declarations */
  66. static int plugin_call_list (struct slapdplugin *list, int operation, Slapi_PBlock *pb);
  67. static int plugin_call_one (struct slapdplugin *list, int operation, Slapi_PBlock *pb);
  68. static int plugin_call_func (struct slapdplugin *list, int operation, Slapi_PBlock *pb, int call_one);
  69. static PRBool plugin_invoke_plugin_pb (struct slapdplugin *plugin, int operation, Slapi_PBlock *pb);
  70. static PRBool plugin_matches_operation (Slapi_DN *target_spec, PluginTargetData *ptd,
  71. PRBool bindop, PRBool isroot, PRBool islocal, ber_tag_t method);
  72. static void plugin_config_init (struct pluginconfig *config);
  73. static void plugin_config_cleanup (struct pluginconfig *config);
  74. static int plugin_config_set_action (int *action, char *value);
  75. static struct pluginconfig* plugin_get_config (struct slapdplugin *plugin);
  76. static void default_plugin_init();
  77. static void ptd_init (PluginTargetData *ptd);
  78. static void ptd_cleanup (PluginTargetData *ptd);
  79. static void ptd_add_subtree (PluginTargetData *ptd, Slapi_DN *subtree);
  80. static void ptd_set_special_data (PluginTargetData *ptd, int type);
  81. static Slapi_DN *ptd_get_first_subtree (const PluginTargetData *ptd, int *cookie);
  82. static Slapi_DN *ptd_get_next_subtree (const PluginTargetData *ptd, int *cookie);
  83. static PRBool ptd_is_special_data_set (const PluginTargetData *ptd, int type);
  84. int ptd_get_subtree_count (const PluginTargetData *ptd);
  85. static void plugin_set_global (PluginTargetData *ptd);
  86. static PRBool plugin_is_global (const PluginTargetData *ptd);
  87. static void plugin_set_default_access (struct pluginconfig *config);
  88. static int plugin_delete_check_dependency(struct slapdplugin *plugin_entry, int flag, char *returntext);
  89. static char *plugin_get_type_str( int type );
  90. static void plugin_cleanup_list();
  91. static int plugin_remove_plugins(struct slapdplugin *plugin_entry, char *plugin_type);
  92. static void plugin_remove_from_shutdown(struct slapdplugin *plugin_entry);
  93. static PLHashTable *global_plugin_dns = NULL;
  94. /* The global plugin list is indexed by the PLUGIN_LIST_* constants defined in slap.h */
  95. static struct slapdplugin *global_plugin_list[PLUGIN_LIST_GLOBAL_MAX];
  96. /* plugin structure used to configure internal operation issued by the core server */
  97. static int global_server_plg_initialised = 0;
  98. struct slapdplugin global_server_plg;
  99. /* plugin structure used to configure internal operation issued by the core server */
  100. static int global_server_plg_id_initialised = 0;
  101. struct slapi_componentid global_server_id_plg;
  102. /* plugin structure used to configure operations issued by the old plugins that
  103. do not pass their identity in the operation */
  104. static struct slapdplugin global_default_plg;
  105. /* Enable/disable plugin callbacks for clean startup */
  106. static int global_plugin_callbacks_enabled = 0;
  107. static Slapi_RWLock *global_rwlock = NULL;
  108. void
  109. global_plugin_init()
  110. {
  111. if((global_rwlock = slapi_new_rwlock()) == NULL){
  112. slapi_log_error( SLAPI_LOG_FATAL, "startup", "Failed to create global plugin rwlock.\n" );
  113. exit (1);
  114. }
  115. }
  116. static void
  117. add_plugin_to_list(struct slapdplugin **list, struct slapdplugin *plugin)
  118. {
  119. struct slapdplugin **tmp;
  120. struct slapdplugin *last = NULL;
  121. int plugin_added = 0;
  122. /* Insert the plugin into list based off of precedence. */
  123. for ( tmp = list; *tmp; tmp = &(*tmp)->plg_next )
  124. {
  125. if (plugin->plg_precedence < (*tmp)->plg_precedence)
  126. {
  127. if (last)
  128. {
  129. /* Insert item between last and tmp. */
  130. plugin->plg_next = *tmp;
  131. last->plg_next = plugin;
  132. } else {
  133. /* Add as the first list item. */
  134. plugin->plg_next = *tmp;
  135. *list = plugin;
  136. }
  137. plugin_added = 1;
  138. /* We've added the plug-in to the
  139. * list, so bail from the loop. */
  140. break;
  141. }
  142. /* Save a pointer to this plugin so we can
  143. * refer to it on the next loop iteration. */
  144. last = *tmp;
  145. }
  146. /* If we didn't add the plug-in to the list yet,
  147. * it needs to be added to the end of the list. */
  148. if (!plugin_added)
  149. {
  150. *tmp = plugin;
  151. }
  152. }
  153. struct slapdplugin *
  154. get_plugin_list(int plugin_list_index)
  155. {
  156. return global_plugin_list[plugin_list_index];
  157. }
  158. /*
  159. * As the plugin configuration information is read an array of
  160. * entries is built which reflect the plugins. The entries
  161. * are added after the syntax plugins are started so that the
  162. * nodes in the attribute tree are initialised correctly.
  163. */
  164. typedef struct entry_and_plugin {
  165. Slapi_Entry *e;
  166. struct slapdplugin *plugin;
  167. struct entry_and_plugin *next;
  168. } entry_and_plugin_t;
  169. static entry_and_plugin_t *dep_plugin_entries = NULL; /* for dependencies */
  170. #if 0
  171. static entry_and_plugin_t *plugin_entries = NULL;
  172. static void
  173. add_plugin_entries()
  174. {
  175. entry_and_plugin_t *ep = plugin_entries;
  176. entry_and_plugin_t *deleteep = 0;
  177. while (ep)
  178. {
  179. int plugin_actions = 0;
  180. Slapi_PBlock newpb;
  181. pblock_init(&newpb);
  182. slapi_add_entry_internal_set_pb(&newpb, ep->e, NULL,
  183. ep->plugin, plugin_actions);
  184. slapi_pblock_set(&newpb, SLAPI_TARGET_DN, (void*)slapi_entry_get_dn_const(ep->e));
  185. slapi_pblock_set(&newpb, SLAPI_TARGET_SDN, (void*)slapi_entry_get_sdn_const(ep->e));
  186. slapi_add_internal_pb(&newpb);
  187. deleteep = ep;
  188. ep = ep->next;
  189. slapi_ch_free((void**)&deleteep);
  190. pblock_done(&newpb);
  191. }
  192. plugin_entries = NULL;
  193. }
  194. #endif
  195. static int
  196. plugin_is_critical(Slapi_Entry *plugin_entry)
  197. {
  198. char *plugin_dn = NULL;
  199. int i;
  200. plugin_dn = slapi_entry_get_ndn(plugin_entry);
  201. for( i = 0; critical_plugins[i]; i++){
  202. if(strcasecmp(plugin_dn, critical_plugins[i]) == 0){
  203. return 1;
  204. }
  205. }
  206. return 0;
  207. }
  208. static void
  209. new_plugin_entry(entry_and_plugin_t **ep, Slapi_Entry *e, struct slapdplugin *plugin)
  210. {
  211. entry_and_plugin_t *oldep = 0;
  212. entry_and_plugin_t *iterep = *ep;
  213. entry_and_plugin_t *newep =
  214. (entry_and_plugin_t*)slapi_ch_calloc(1,sizeof(entry_and_plugin_t));
  215. newep->e = e;
  216. newep->plugin = plugin;
  217. while(iterep)
  218. {
  219. oldep = iterep;
  220. iterep = iterep->next;
  221. }
  222. newep->next = 0;
  223. if(oldep)
  224. oldep->next = newep;
  225. else
  226. *ep = newep;
  227. }
  228. static void
  229. add_plugin_entry_dn(const Slapi_DN *plugin_dn)
  230. {
  231. if (!global_plugin_dns)
  232. {
  233. global_plugin_dns = PL_NewHashTable(20, PL_HashString,
  234. PL_CompareStrings,
  235. PL_CompareValues, 0, 0);
  236. }
  237. PL_HashTableAdd(global_plugin_dns,
  238. slapi_sdn_get_ndn(plugin_dn),
  239. (void*)plugin_dn);
  240. }
  241. #define SLAPI_PLUGIN_NONE_IF_NULL( s ) ((s) == NULL ? "none" : (s))
  242. /*
  243. * Allows a plugin to register a plugin.
  244. * This was added so that 'object' plugins could register all
  245. * the plugin interfaces that it supports.
  246. */
  247. int
  248. slapi_register_plugin(
  249. const char *plugintype,
  250. int enabled,
  251. const char *initsymbol,
  252. slapi_plugin_init_fnptr initfunc,
  253. const char *name,
  254. char **argv,
  255. void *group_identity
  256. )
  257. {
  258. return slapi_register_plugin_ext(plugintype, enabled, initsymbol,
  259. initfunc, name, argv, group_identity, PLUGIN_DEFAULT_PRECEDENCE);
  260. }
  261. int
  262. slapi_register_plugin_ext(
  263. const char *plugintype,
  264. int enabled,
  265. const char *initsymbol,
  266. slapi_plugin_init_fnptr initfunc,
  267. const char *name,
  268. char **argv,
  269. void *group_identity,
  270. int precedence
  271. )
  272. {
  273. Slapi_Entry *e = NULL;
  274. char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
  275. char *dn = slapi_ch_smprintf("cn=%s,%s", name, PLUGIN_BASE_DN);
  276. Slapi_DN *sdn = slapi_sdn_new_normdn_passin(dn);
  277. int found_precedence;
  278. int ii = 0;
  279. int rc = 0;
  280. e = slapi_entry_alloc();
  281. /* this function consumes dn */
  282. slapi_entry_init_ext(e, sdn, NULL);
  283. slapi_sdn_free(&sdn);
  284. slapi_entry_attr_set_charptr(e, "cn", name);
  285. slapi_entry_attr_set_charptr(e, ATTR_PLUGIN_TYPE, plugintype);
  286. if (!enabled)
  287. slapi_entry_attr_set_charptr(e, ATTR_PLUGIN_ENABLED, "off");
  288. slapi_entry_attr_set_charptr(e, ATTR_PLUGIN_INITFN, initsymbol);
  289. /* If the plugin belong to a group, get the precedence from the group */
  290. found_precedence = precedence;
  291. if ((found_precedence == PLUGIN_DEFAULT_PRECEDENCE) && group_identity) {
  292. struct slapi_componentid * cid = (struct slapi_componentid *) group_identity;
  293. if (cid->sci_plugin && (cid->sci_plugin->plg_precedence != PLUGIN_DEFAULT_PRECEDENCE)) {
  294. slapi_log_error(SLAPI_LOG_PLUGIN, NULL,
  295. "Plugin precedence (%s) reset to group precedence (%s): %d \n",
  296. name ? name : "",
  297. cid->sci_plugin->plg_name ? cid->sci_plugin->plg_name : "",
  298. cid->sci_plugin->plg_precedence);
  299. found_precedence = cid->sci_plugin->plg_precedence;
  300. }
  301. }
  302. slapi_entry_attr_set_int(e, ATTR_PLUGIN_PRECEDENCE, found_precedence);
  303. for (ii = 0; argv && argv[ii]; ++ii) {
  304. char argname[64];
  305. PR_snprintf(argname, sizeof(argname), "%s%d", ATTR_PLUGIN_ARG, ii);
  306. slapi_entry_attr_set_charptr(e, argname, argv[ii]);
  307. }
  308. /* plugin_setup copies the given entry */
  309. rc = plugin_setup(e, group_identity, initfunc, 0, returntext);
  310. slapi_entry_free(e);
  311. return rc;
  312. }
  313. int
  314. plugin_call_plugins( Slapi_PBlock *pb, int whichfunction )
  315. {
  316. int plugin_list_number= -1;
  317. int rc= 0;
  318. int do_op = global_plugin_callbacks_enabled;
  319. if ( pb == NULL )
  320. {
  321. return( 0 );
  322. }
  323. switch ( whichfunction ) {
  324. case SLAPI_PLUGIN_PRE_BIND_FN:
  325. case SLAPI_PLUGIN_PRE_UNBIND_FN:
  326. case SLAPI_PLUGIN_PRE_SEARCH_FN:
  327. case SLAPI_PLUGIN_PRE_COMPARE_FN:
  328. case SLAPI_PLUGIN_PRE_MODIFY_FN:
  329. case SLAPI_PLUGIN_PRE_MODRDN_FN:
  330. case SLAPI_PLUGIN_PRE_ADD_FN:
  331. case SLAPI_PLUGIN_PRE_DELETE_FN:
  332. case SLAPI_PLUGIN_PRE_ABANDON_FN:
  333. case SLAPI_PLUGIN_PRE_ENTRY_FN:
  334. case SLAPI_PLUGIN_PRE_REFERRAL_FN:
  335. case SLAPI_PLUGIN_PRE_RESULT_FN:
  336. plugin_list_number= PLUGIN_LIST_PREOPERATION;
  337. break;
  338. case SLAPI_PLUGIN_POST_BIND_FN:
  339. case SLAPI_PLUGIN_POST_UNBIND_FN:
  340. case SLAPI_PLUGIN_POST_SEARCH_FN:
  341. case SLAPI_PLUGIN_POST_SEARCH_FAIL_FN:
  342. case SLAPI_PLUGIN_POST_COMPARE_FN:
  343. case SLAPI_PLUGIN_POST_MODIFY_FN:
  344. case SLAPI_PLUGIN_POST_MODRDN_FN:
  345. case SLAPI_PLUGIN_POST_ADD_FN:
  346. case SLAPI_PLUGIN_POST_DELETE_FN:
  347. case SLAPI_PLUGIN_POST_ABANDON_FN:
  348. case SLAPI_PLUGIN_POST_ENTRY_FN:
  349. case SLAPI_PLUGIN_POST_REFERRAL_FN:
  350. case SLAPI_PLUGIN_POST_RESULT_FN:
  351. plugin_list_number= PLUGIN_LIST_POSTOPERATION;
  352. break;
  353. case SLAPI_PLUGIN_BE_PRE_MODIFY_FN:
  354. case SLAPI_PLUGIN_BE_PRE_MODRDN_FN:
  355. case SLAPI_PLUGIN_BE_PRE_ADD_FN:
  356. case SLAPI_PLUGIN_BE_PRE_DELETE_FN:
  357. case SLAPI_PLUGIN_BE_PRE_CLOSE_FN:
  358. case SLAPI_PLUGIN_BE_PRE_BACKUP_FN:
  359. plugin_list_number= PLUGIN_LIST_BEPREOPERATION;
  360. do_op = 1; /* always allow backend callbacks (even during startup) */
  361. break;
  362. case SLAPI_PLUGIN_BE_POST_MODIFY_FN:
  363. case SLAPI_PLUGIN_BE_POST_MODRDN_FN:
  364. case SLAPI_PLUGIN_BE_POST_ADD_FN:
  365. case SLAPI_PLUGIN_BE_POST_DELETE_FN:
  366. case SLAPI_PLUGIN_BE_POST_OPEN_FN:
  367. case SLAPI_PLUGIN_BE_POST_BACKUP_FN:
  368. plugin_list_number= PLUGIN_LIST_BEPOSTOPERATION;
  369. do_op = 1; /* always allow backend callbacks (even during startup) */
  370. break;
  371. case SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN:
  372. case SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN:
  373. case SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN:
  374. case SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN:
  375. case SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN:
  376. plugin_list_number= PLUGIN_LIST_INTERNAL_PREOPERATION;
  377. break;
  378. case SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN:
  379. case SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN:
  380. case SLAPI_PLUGIN_INTERNAL_POST_ADD_FN:
  381. case SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN:
  382. plugin_list_number= PLUGIN_LIST_INTERNAL_POSTOPERATION;
  383. break;
  384. case SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN:
  385. case SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN:
  386. case SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN:
  387. case SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN:
  388. case SLAPI_PLUGIN_BE_TXN_PRE_DELETE_TOMBSTONE_FN:
  389. plugin_list_number= PLUGIN_LIST_BETXNPREOPERATION;
  390. do_op = 1; /* always allow backend callbacks (even during startup) */
  391. break;
  392. case SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN:
  393. case SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN:
  394. case SLAPI_PLUGIN_BE_TXN_POST_ADD_FN:
  395. case SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN:
  396. plugin_list_number= PLUGIN_LIST_BETXNPOSTOPERATION;
  397. do_op = 1; /* always allow backend callbacks (even during startup) */
  398. break;
  399. }
  400. if(plugin_list_number!=-1 && do_op)
  401. {
  402. /* We stash the pblock plugin pointer to preserve the callers context */
  403. struct slapdplugin *p;
  404. int locked = 0;
  405. locked = slapi_td_get_plugin_locked();
  406. if (!locked) {
  407. slapi_rwlock_rdlock(global_rwlock);
  408. }
  409. slapi_pblock_get(pb, SLAPI_PLUGIN, &p);
  410. /* Call the operation on the Global Plugins */
  411. rc = plugin_call_list(global_plugin_list[plugin_list_number], whichfunction, pb);
  412. slapi_pblock_set(pb, SLAPI_PLUGIN, p);
  413. if (!locked) {
  414. slapi_rwlock_unlock(global_rwlock);
  415. }
  416. }
  417. else
  418. {
  419. /* Programmer error! or the callback is denied during startup */
  420. }
  421. return rc;
  422. }
  423. void
  424. plugin_call_entrystore_plugins(char **entrystr, uint *size)
  425. {
  426. struct slapdplugin *p;
  427. for (p = global_plugin_list[PLUGIN_LIST_LDBM_ENTRY_FETCH_STORE];
  428. p != NULL; p = p->plg_next )
  429. {
  430. if (p->plg_entrystorefunc)
  431. (*p->plg_entrystorefunc)(entrystr, size);
  432. }
  433. }
  434. void
  435. plugin_call_entryfetch_plugins(char **entrystr, uint *size)
  436. {
  437. struct slapdplugin *p;
  438. for (p = global_plugin_list[PLUGIN_LIST_LDBM_ENTRY_FETCH_STORE];
  439. p != NULL; p = p->plg_next )
  440. {
  441. if (p->plg_entryfetchfunc)
  442. (*p->plg_entryfetchfunc)(entrystr, size);
  443. }
  444. }
  445. /*
  446. * call extended operation plugins
  447. *
  448. * return SLAPI_PLUGIN_EXTENDED_SENT_RESULT if one of the extended operation
  449. * plugins sent a result.
  450. * return SLAPI_PLUGIN_EXTENDED_NOT_HANDLED if no extended operation plugins
  451. * handled the operation.
  452. * otherwise, return an LDAP error code (possibly a merge of the errors
  453. * returned by the plugins we called).
  454. */
  455. int
  456. plugin_call_exop_plugins( Slapi_PBlock *pb, char *oid )
  457. {
  458. struct slapdplugin *p;
  459. int i, rc;
  460. int lderr = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  461. for ( p = global_plugin_list[PLUGIN_LIST_EXTENDED_OPERATION]; p != NULL; p = p->plg_next ) {
  462. if ( p->plg_exhandler != NULL ) {
  463. if ( p->plg_exoids != NULL ) {
  464. for ( i = 0; p->plg_exoids[i] != NULL; i++ ) {
  465. if ( strcasecmp( oid, p->plg_exoids[i] )
  466. == 0 ) {
  467. break;
  468. }
  469. }
  470. if ( p->plg_exoids[i] == NULL ) {
  471. continue;
  472. }
  473. }
  474. slapi_pblock_set( pb, SLAPI_PLUGIN, p );
  475. set_db_default_result_handlers( pb );
  476. if ( (rc = (*p->plg_exhandler)( pb ))
  477. == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
  478. return( rc ); /* result sent */
  479. } else if ( rc != SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
  480. /*
  481. * simple merge: report last real error
  482. */
  483. if ( lderr == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED
  484. || rc != LDAP_SUCCESS ) {
  485. lderr = rc;
  486. }
  487. }
  488. }
  489. }
  490. return( lderr );
  491. }
  492. /*
  493. * Attempt to convert the extended operation 'oid' to a string by
  494. * examining the registered plugins. Returns NULL if no plugin is
  495. * registered for this OID.
  496. *
  497. * Our first choice is to use an OID-specific name that has been
  498. * registered by a plugin via the SLAPI_PLUGIN_EXT_OP_NAMELIST pblock setting.
  499. * Our second choice is to use the plugin's ID (short name).
  500. * Our third choice is to use the plugin's RDN (under cn=config).
  501. */
  502. const char *
  503. plugin_extended_op_oid2string( const char *oid )
  504. {
  505. struct slapdplugin *p;
  506. int i, j;
  507. const char *rval = NULL;
  508. for ( p = global_plugin_list[PLUGIN_LIST_EXTENDED_OPERATION]; p != NULL;
  509. p = p->plg_next ) {
  510. if ( p->plg_exhandler != NULL && p->plg_exoids != NULL ) {
  511. for ( i = 0; p->plg_exoids[i] != NULL; i++ ) {
  512. if ( strcasecmp( oid, p->plg_exoids[i] ) == 0 ) {
  513. if ( NULL != p->plg_exnames ) {
  514. for ( j = 0; j < i && p->plg_exnames[j] != NULL; ++j ) {
  515. ;
  516. }
  517. rval = p->plg_exnames[j]; /* OID-related name */
  518. }
  519. if ( NULL == rval ) {
  520. if ( NULL != p->plg_desc.spd_id ) {
  521. rval = p->plg_desc.spd_id; /* short name */
  522. } else {
  523. rval = p->plg_name; /* RDN */
  524. }
  525. }
  526. break;
  527. }
  528. }
  529. }
  530. }
  531. return( rval );
  532. }
  533. static int
  534. plugin_cmp_plugins(struct slapdplugin *p1, struct slapdplugin *p2)
  535. {
  536. int rc = 0;
  537. if( p1->plg_dn ){
  538. if(p2->plg_dn && strcasecmp(p1->plg_dn, p2->plg_dn) == 0){
  539. rc = 1;
  540. } else if(p2->plg_id && strcasecmp(p1->plg_dn, p2->plg_id) == 0){
  541. rc = 1;
  542. }
  543. } else if(p1->plg_id){
  544. if(p2->plg_id && strcasecmp(p1->plg_id, p2->plg_id) == 0){
  545. rc = 1;
  546. } else if(p2->plg_dn && strcasecmp(p2->plg_dn, p1->plg_id) == 0){
  547. rc = 1;
  548. }
  549. }
  550. return rc;
  551. }
  552. /*
  553. * kexcoff: return the slapdplugin structure
  554. */
  555. struct slapdplugin *
  556. plugin_get_pwd_storage_scheme(char *name, int len, int index)
  557. {
  558. /* index could be PLUGIN_LIST_PWD_STORAGE_SCHEME or PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME */
  559. struct slapdplugin *p;
  560. for ( p = global_plugin_list[index]; p != NULL; p = p->plg_next ) {
  561. if (strlen(p->plg_pwdstorageschemename) == len) {
  562. if (strncasecmp(p->plg_pwdstorageschemename, name, len) == 0) {
  563. return( p );
  564. }
  565. }
  566. }
  567. return( NULL );
  568. }
  569. char *
  570. plugin_get_pwd_storage_scheme_list(int index)
  571. {
  572. /* index could be PLUGIN_LIST_PWD_STORAGE_SCHEME or PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME */
  573. struct slapdplugin *p = NULL;
  574. char *names_list = NULL;
  575. int len = 0;
  576. /* first pass - calculate space needed for comma delimited list */
  577. for ( p = global_plugin_list[index]; p != NULL; p = p->plg_next ) {
  578. if ( p->plg_pwdstorageschemeenc != NULL )
  579. {
  580. /* + 1 for comma, 1 for space, 1 for null */
  581. len += strlen(p->plg_pwdstorageschemename) + 3;
  582. }
  583. }
  584. /* no plugins? */
  585. if (!len)
  586. return NULL;
  587. /* next, allocate the space */
  588. names_list = (char *)slapi_ch_malloc(len+1);
  589. *names_list = 0;
  590. /* second pass - write the string */
  591. for ( p = global_plugin_list[index]; p != NULL; p = p->plg_next ) {
  592. if ( p->plg_pwdstorageschemeenc != NULL )
  593. {
  594. strcat(names_list, p->plg_pwdstorageschemename);
  595. if (p->plg_next != NULL)
  596. strcat(names_list, ", ");
  597. }
  598. }
  599. return( names_list );
  600. }
  601. int slapi_send_ldap_intermediate( Slapi_PBlock *pb, LDAPControl **ectrls,
  602. char *responseName, struct berval *responseValue)
  603. {
  604. /* no SLAPI_PLUGIN_DB_INTERMEDIATE_FN defined
  605. * always directly call slapd_ function
  606. */
  607. return send_ldap_intermediate(pb, ectrls, responseName, responseValue);
  608. }
  609. int
  610. slapi_send_ldap_search_entry( Slapi_PBlock *pb, Slapi_Entry *e, LDAPControl **ectrls,
  611. char **attrs, int attrsonly )
  612. {
  613. IFP fn = NULL;
  614. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_ENTRY_FN,(void*)&fn);
  615. if (NULL == fn)
  616. {
  617. return -1;
  618. }
  619. return (*fn)(pb,e,ectrls,attrs,attrsonly);
  620. }
  621. void
  622. slapi_set_ldap_result( Slapi_PBlock *pb, int err, char *matched, char *text,
  623. int nentries, struct berval **urls )
  624. {
  625. char * old_matched = NULL;
  626. char * old_text = NULL;
  627. char * matched_copy = slapi_ch_strdup(matched);
  628. char * text_copy = slapi_ch_strdup(text);
  629. /* free the old matched and text, if any */
  630. slapi_pblock_get(pb, SLAPI_RESULT_MATCHED, &old_matched);
  631. slapi_ch_free_string(&old_matched);
  632. slapi_pblock_get(pb, SLAPI_RESULT_TEXT, &old_text);
  633. slapi_ch_free_string(&old_text);
  634. /* set the new stuff */
  635. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &err);
  636. slapi_pblock_set(pb, SLAPI_RESULT_MATCHED, matched_copy);
  637. slapi_pblock_set(pb, SLAPI_RESULT_TEXT, text_copy);
  638. }
  639. void
  640. slapi_send_ldap_result_from_pb( Slapi_PBlock *pb)
  641. {
  642. int err;
  643. char *matched;
  644. char *text;
  645. IFP fn = NULL;
  646. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &err);
  647. slapi_pblock_get(pb, SLAPI_RESULT_TEXT, &text);
  648. slapi_pblock_get(pb, SLAPI_RESULT_MATCHED, &matched);
  649. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_RESULT_FN,(void*)&fn);
  650. if (NULL != fn)
  651. {
  652. (*fn)(pb,err,matched,text,0,NULL);
  653. }
  654. slapi_pblock_set(pb, SLAPI_RESULT_TEXT, NULL);
  655. slapi_pblock_set(pb, SLAPI_RESULT_MATCHED, NULL);
  656. slapi_ch_free((void **)&matched);
  657. slapi_ch_free((void **)&text);
  658. }
  659. void
  660. slapi_send_ldap_result( Slapi_PBlock *pb, int err, char *matched, char *text,
  661. int nentries, struct berval **urls )
  662. {
  663. IFP fn = NULL;
  664. Slapi_Operation *operation;
  665. long op_type;
  666. /* GB : for spanning requests over multiple backends */
  667. if (err == LDAP_NO_SUCH_OBJECT)
  668. {
  669. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  670. op_type = operation_get_type(operation);
  671. if (op_type == SLAPI_OPERATION_SEARCH)
  672. {
  673. if (urls || nentries)
  674. {
  675. LDAPDebug( LDAP_DEBUG_ANY, "ERROR : urls or nentries set"
  676. "in sendldap_result while NO_SUCH_OBJECT returned\n",0,0,0);
  677. }
  678. slapi_set_ldap_result(pb, err, matched, text, 0, NULL);
  679. return;
  680. }
  681. }
  682. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &err);
  683. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_RESULT_FN,(void*)&fn);
  684. if (NULL == fn)
  685. {
  686. return ;
  687. }
  688. /*
  689. * Call the result function. It should set pb->pb_op->o_status to
  690. * SLAPI_OP_STATUS_RESULT_SENT right after sending the result to
  691. * the client or otherwise consuming it.
  692. */
  693. (*fn)(pb,err,matched,text,nentries,urls);
  694. }
  695. int
  696. slapi_send_ldap_referral( Slapi_PBlock *pb, Slapi_Entry *e, struct berval **refs,
  697. struct berval ***urls )
  698. {
  699. IFP fn = NULL;
  700. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_REFERRAL_FN,(void*)&fn);
  701. if (NULL == fn)
  702. {
  703. return -1;
  704. }
  705. return (*fn)(pb,e,refs,urls);
  706. }
  707. /***********************************************************
  708. start of plugin dependency code
  709. ************************************************************/
  710. /* struct _plugin_dep_type
  711. * we shall not presume to know all plugin types
  712. * so as to allow new types to be added without
  713. * requiring changes to this code (hopefully)
  714. * so we need to dynamically keep track of them
  715. */
  716. typedef struct _plugin_dep_type{
  717. char *type; /* the string descriptor */
  718. int num_not_started; /* the count of plugins which have yet to be started for this type */
  719. struct _plugin_dep_type *next;
  720. } *plugin_dep_type;
  721. /* _plugin_dep_config
  722. * we need somewhere to collect the plugin configurations
  723. * prior to attempting to resolve dependencies
  724. */
  725. typedef struct _plugin_dep_config {
  726. char *name;
  727. char *type;
  728. Slapi_PBlock pb;
  729. struct slapdplugin *plugin;
  730. Slapi_Entry *e;
  731. int entry_created;
  732. int op_done;
  733. char **depends_type_list;
  734. int total_type;
  735. char **depends_named_list;
  736. int total_named;
  737. char *config_area;
  738. int removed;
  739. } plugin_dep_config;
  740. static void plugin_free_plugin_dep_config(plugin_dep_config **config);
  741. /* list of plugins which should be shutdown in reverse order */
  742. static plugin_dep_config *global_plugin_shutdown_order = 0;
  743. static int global_plugins_started = 0;
  744. /*
  745. * find_plugin_type
  746. *
  747. * searches the list for the plugin type
  748. * and returns the plugin_dep_type if found
  749. */
  750. static plugin_dep_type
  751. find_plugin_type(plugin_dep_type head, char *type)
  752. {
  753. plugin_dep_type ret = 0;
  754. plugin_dep_type iter = head;
  755. while(iter)
  756. {
  757. if(!slapi_UTF8CASECMP(iter->type, type))
  758. {
  759. ret = iter;
  760. break;
  761. }
  762. iter = iter->next;
  763. }
  764. return ret;
  765. }
  766. /*
  767. * increment_plugin_type
  768. *
  769. * searches the list for the plugin type
  770. * and increments its not started value
  771. * returns the current type count on success -1 on failure
  772. * to find the type
  773. */
  774. static int
  775. increment_plugin_type(plugin_dep_type head, char *type)
  776. {
  777. int ret = -1;
  778. plugin_dep_type the_type;
  779. if ((the_type = find_plugin_type(head, type)) != NULL)
  780. ret = ++the_type->num_not_started;
  781. return ret;
  782. }
  783. /*
  784. * decrement_plugin_type
  785. *
  786. * searches the list for the plugin type
  787. * and decrements its not started value
  788. * returns the current type count on success -1 on failure
  789. * to find the type
  790. */
  791. static int
  792. decrement_plugin_type(plugin_dep_type head, char *type)
  793. {
  794. int ret = -1;
  795. plugin_dep_type the_type;
  796. if ((the_type = find_plugin_type(head, type)) != NULL)
  797. ret = --the_type->num_not_started;
  798. return ret;
  799. }
  800. /*
  801. * add_plugin_type
  802. *
  803. * Either increments the count of the plugin type
  804. * or when it does not exist, adds it to the list
  805. */
  806. static int
  807. add_plugin_type(plugin_dep_type *head, char *type)
  808. {
  809. int ret = -1;
  810. if(*head)
  811. {
  812. if(0 < increment_plugin_type(*head, type))
  813. {
  814. ret = 0;
  815. }
  816. }
  817. if(ret)
  818. {
  819. /* create new head */
  820. plugin_dep_type tmp_head;
  821. tmp_head = (plugin_dep_type)slapi_ch_malloc(sizeof(struct _plugin_dep_type));
  822. tmp_head->num_not_started = 1;
  823. tmp_head->type = slapi_ch_strdup(type);
  824. ret = 0;
  825. tmp_head->next = *head;
  826. (*head) = tmp_head;
  827. }
  828. return ret;
  829. }
  830. /*
  831. * plugin_create_stringlist
  832. *
  833. * Creates a string list from values of the entries
  834. * attribute passed in as args - used to track dependencies
  835. *
  836. */
  837. int
  838. plugin_create_stringlist( Slapi_Entry *plugin_entry, char *attr_name,
  839. int *total_strings, char ***list)
  840. {
  841. Slapi_Attr *attr = 0;
  842. int hint = 0;
  843. int num_vals = 0;
  844. int val_index = 0;
  845. Slapi_Value *val = NULL;
  846. if(0 == slapi_entry_attr_find( plugin_entry, attr_name, &attr ))
  847. {
  848. /* allocate memory for the string array */
  849. slapi_attr_get_numvalues( attr, &num_vals);
  850. if(num_vals)
  851. {
  852. *total_strings = num_vals;
  853. *list = (char **)slapi_ch_calloc(num_vals + 1, sizeof(char*));
  854. }
  855. else
  856. goto bail; /* if this ever happens, then they are running on a TSR-80 */
  857. val_index = 0;
  858. hint = slapi_attr_first_value( attr, &val );
  859. while(val_index < num_vals)
  860. {
  861. /* add the value to the array */
  862. (*list)[val_index] = (char*)slapi_ch_strdup(slapi_value_get_string(val));
  863. hint = slapi_attr_next_value( attr, hint, &val );
  864. val_index++;
  865. }
  866. }
  867. else
  868. *total_strings = num_vals;
  869. bail:
  870. return num_vals;
  871. }
  872. /*
  873. * Remove the plugin from the DN hashtable
  874. */
  875. static void
  876. plugin_remove_from_list(char *plugin_dn)
  877. {
  878. Slapi_DN *hash_sdn;
  879. hash_sdn = (Slapi_DN *)PL_HashTableLookup(global_plugin_dns, plugin_dn);
  880. if(hash_sdn){
  881. PL_HashTableRemove(global_plugin_dns, plugin_dn);
  882. slapi_sdn_free(&hash_sdn);
  883. }
  884. }
  885. /*
  886. * The main plugin was successfully started, set it and its
  887. * registered plugin functions as started.
  888. */
  889. static void
  890. plugin_set_plugins_started(struct slapdplugin *plugin_entry)
  891. {
  892. struct slapdplugin *plugin = NULL;
  893. int type = 0;
  894. /* look everywhere for other plugin functions with the plugin id */
  895. for(type = 0; type < PLUGIN_LIST_GLOBAL_MAX; type++){
  896. plugin = global_plugin_list[type];
  897. while(plugin){
  898. if(plugin_cmp_plugins(plugin_entry, plugin)){
  899. plugin->plg_started = 1;
  900. }
  901. plugin = plugin->plg_next;
  902. }
  903. }
  904. }
  905. /*
  906. * A plugin dependency changed, update the dependency list
  907. */
  908. void
  909. plugin_update_dep_entries(Slapi_Entry *plugin_entry)
  910. {
  911. entry_and_plugin_t *ep;
  912. slapi_rwlock_wrlock(global_rwlock);
  913. ep = dep_plugin_entries;
  914. while(ep){
  915. if(ep->plugin && ep->e){
  916. if(slapi_sdn_compare(slapi_entry_get_sdn(ep->e), slapi_entry_get_sdn(plugin_entry)) == 0){
  917. slapi_entry_free(ep->e);
  918. ep->e = slapi_entry_dup(plugin_entry);
  919. break;
  920. }
  921. }
  922. ep = ep->next;
  923. }
  924. slapi_rwlock_unlock(global_rwlock);
  925. }
  926. /*
  927. * Attempt to start a plugin that was either just added or just enabled.
  928. */
  929. int
  930. plugin_start(Slapi_Entry *entry, char *returntext)
  931. {
  932. entry_and_plugin_t *ep = dep_plugin_entries;
  933. plugin_dep_config *global_tmp_list = NULL;
  934. plugin_dep_config *config = NULL;
  935. plugin_dep_type the_plugin_type;
  936. plugin_dep_type plugin_head = 0;
  937. Slapi_PBlock pb;
  938. Slapi_Entry *plugin_entry;
  939. struct slapdplugin *plugin;
  940. char *value;
  941. int plugins_started = 1;
  942. int num_plg_started = 0;
  943. int shutdown_index = 0;
  944. int total_plugins = 0;
  945. int plugin_index = 0;
  946. int plugin_idx = -1;
  947. int index = 0;
  948. int ret = 0;
  949. int i = 0;
  950. global_plugins_started = 0;
  951. /* Count the plugins so we can allocate memory for the config array */
  952. while(ep){
  953. if(slapi_sdn_compare(slapi_entry_get_sdn(ep->e), slapi_entry_get_sdn(entry)) == 0){
  954. plugin_idx = total_plugins;
  955. }
  956. total_plugins++;
  957. ep = ep->next;
  958. }
  959. /* allocate the config array */
  960. config = (plugin_dep_config*)slapi_ch_calloc(total_plugins + 1, sizeof(plugin_dep_config));
  961. ep = dep_plugin_entries;
  962. /* Collect relevant config */
  963. while(ep){
  964. plugin = ep->plugin;
  965. if(plugin == 0){
  966. continue;
  967. }
  968. pblock_init(&pb);
  969. slapi_pblock_set( &pb, SLAPI_ARGC, &plugin->plg_argc);
  970. slapi_pblock_set( &pb, SLAPI_ARGV, &plugin->plg_argv);
  971. config[plugin_index].pb = pb;
  972. config[plugin_index].e = ep->e;
  973. /* add type */
  974. plugin_entry = ep->e;
  975. if(plugin_entry){
  976. /*
  977. * Pass the plugin DN in SLAPI_TARGET_SDN and the plugin entry
  978. * in SLAPI_ADD_ENTRY. For this to actually work, we need to
  979. * create an operation and include that in the pblock as well,
  980. * because these two items are stored in the operation parameters.
  981. */
  982. Operation *op = internal_operation_new(SLAPI_OPERATION_ADD, 0);
  983. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_OPERATION, op);
  984. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_TARGET_SDN,
  985. (void*)(slapi_entry_get_sdn_const(plugin_entry)));
  986. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_ADD_ENTRY,
  987. plugin_entry );
  988. /* Pass the plugin alternate config area DN in SLAPI_PLUGIN_CONFIG_AREA. */
  989. value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_CONFIG_AREA);
  990. if(value){
  991. config[plugin_index].config_area = value;
  992. value = NULL;
  993. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_PLUGIN_CONFIG_AREA,
  994. config[plugin_index].config_area);
  995. }
  996. value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype");
  997. if(value){
  998. add_plugin_type( &plugin_head, value);
  999. config[plugin_index].type = value;
  1000. value = NULL;
  1001. }
  1002. /* now the name */
  1003. value = slapi_entry_attr_get_charptr(plugin_entry, "cn");
  1004. if(value){
  1005. config[plugin_index].name = value;
  1006. value = NULL;
  1007. }
  1008. config[plugin_index].plugin = plugin;
  1009. /* now add dependencies */
  1010. plugin_create_stringlist( plugin_entry, ATTR_PLUGIN_DEPENDS_ON_NAMED,
  1011. &(config[plugin_index].total_named), &(config[plugin_index].depends_named_list));
  1012. plugin_create_stringlist( plugin_entry, ATTR_PLUGIN_DEPENDS_ON_TYPE,
  1013. &(config[plugin_index].total_type), &(config[plugin_index].depends_type_list));
  1014. }
  1015. plugin_index++;
  1016. ep = ep->next;
  1017. }
  1018. /*
  1019. * Prepare list of shutdown order (we need nothing fancier right now
  1020. * than the reverse startup order) The list may include NULL entries,
  1021. * these will be plugins which were never started
  1022. */
  1023. shutdown_index = total_plugins - 1;
  1024. global_tmp_list = (plugin_dep_config*)slapi_ch_calloc(total_plugins, sizeof(plugin_dep_config));
  1025. /* now resolve dependencies
  1026. * cycle through list, if a plugin has no dependencies then start it
  1027. * then remove it from the dependency lists of all other plugins
  1028. * and decrement the corresponding element of the plugin types array
  1029. * for depends_type we will need to check the array of plugin types
  1030. * to see if all type dependencies are at zero prior to start
  1031. * if one cycle fails to load any plugins we have failed, however
  1032. * we shall continue loading plugins in case a configuration error
  1033. * can correct itself
  1034. */
  1035. while(plugins_started && num_plg_started < total_plugins){
  1036. for(plugin_index = 0, plugins_started = 0; plugin_index < total_plugins; plugin_index++){
  1037. /* perform op on plugins only once */
  1038. if(config[plugin_index].op_done == 0){
  1039. int enabled = 0;
  1040. int satisfied = 0;
  1041. int break_out = 0;
  1042. /*
  1043. * determine if plugin is enabled
  1044. * some processing is necessary even
  1045. * if it is not
  1046. */
  1047. if ( NULL != config[plugin_index].e && (value = slapi_entry_attr_get_charptr(config[plugin_index].e,
  1048. ATTR_PLUGIN_ENABLED)) &&
  1049. !strcasecmp(value, "on"))
  1050. {
  1051. enabled = 1;
  1052. } else {
  1053. enabled = 0;
  1054. }
  1055. slapi_ch_free_string(&value);
  1056. /*
  1057. * make sure named dependencies have been satisfied
  1058. * that means that the list of names should contain all
  1059. * null entries
  1060. */
  1061. if(enabled && config[plugin_index].total_named){
  1062. i = 0;
  1063. while(break_out == 0 && i < config[plugin_index].total_named){
  1064. satisfied = 1;
  1065. if((config[plugin_index].depends_named_list)[i] != 0){
  1066. satisfied = 0;
  1067. break_out = 1;
  1068. }
  1069. i++;
  1070. }
  1071. if(!satisfied)
  1072. continue;
  1073. }
  1074. /*
  1075. * make sure the type dependencies have been satisfied
  1076. * that means for each type in the list, it's number of
  1077. * plugins left not started is zero
  1078. */
  1079. satisfied = 0;
  1080. break_out = 0;
  1081. if(enabled && config[plugin_index].total_type){
  1082. i = 0;
  1083. while(break_out == 0 && i < config[plugin_index].total_type){
  1084. satisfied = 1;
  1085. the_plugin_type = find_plugin_type(plugin_head, (config[plugin_index].depends_type_list)[i]);
  1086. if(the_plugin_type && the_plugin_type->num_not_started != 0){
  1087. satisfied = 0;
  1088. break_out = 1;
  1089. }
  1090. i++;
  1091. }
  1092. if(!satisfied)
  1093. continue;
  1094. }
  1095. /**** This plugins dependencies have now been satisfied ****/
  1096. satisfied = 1; /* symbolic only */
  1097. config[plugin_index].entry_created = 1;
  1098. if (enabled){
  1099. if(plugin_index == plugin_idx){
  1100. /* finally, perform the op on the plugin */
  1101. LDAPDebug( LDAP_DEBUG_PLUGIN, "Starting %s plugin %s\n" ,
  1102. config[plugin_index].type, config[plugin_index].name, 0 );
  1103. /*
  1104. * Put the plugin into the temporary pblock so the startup functions have
  1105. * access to the real plugin for registering callbacks, task, etc.
  1106. */
  1107. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_PLUGIN, config[plugin_index].plugin);
  1108. ret = plugin_call_one( config[plugin_index].plugin, SLAPI_PLUGIN_START_FN,
  1109. &(config[plugin_index].pb));
  1110. pblock_done(&(config[plugin_index].pb));
  1111. if(ret){
  1112. /*
  1113. * Delete the plugin(undo everything), as we don't know how far the start
  1114. * function got.
  1115. */
  1116. LDAPDebug( LDAP_DEBUG_ANY, "Failed to start %s plugin %s\n" ,
  1117. config[plugin_index].type, config[plugin_index].name, 0 );
  1118. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"Failed to start plugin \"%s\". See errors log.",
  1119. config[plugin_index].name);
  1120. plugin_delete(entry, returntext, 1);
  1121. goto done;
  1122. } else {
  1123. /*
  1124. * Set the plugin and its registered functions as started.
  1125. */
  1126. plugin_set_plugins_started(config[plugin_index].plugin);
  1127. }
  1128. }
  1129. /* Add this plugin to the shutdown list */
  1130. global_tmp_list[shutdown_index] = config[plugin_index];
  1131. shutdown_index--;
  1132. global_plugins_started++;
  1133. /* remove this named plugin from other plugins lists */
  1134. for(i = 0; i < total_plugins; i++){
  1135. index = 0;
  1136. while(index < config[i].total_named){
  1137. if((config[i].depends_named_list)[index] != 0 &&
  1138. !slapi_UTF8CASECMP((config[i].depends_named_list)[index], config[plugin_index].name))
  1139. {
  1140. slapi_ch_free((void**)&((config[i].depends_named_list)[index]));
  1141. }
  1142. index++;
  1143. }
  1144. }
  1145. } else {
  1146. pblock_done(&(config[plugin_index].pb));
  1147. }
  1148. /* decrement the type counter for this plugin type */
  1149. decrement_plugin_type(plugin_head, config[plugin_index].type);
  1150. config[plugin_index].op_done = 1;
  1151. num_plg_started++;
  1152. plugins_started = 1;
  1153. } /* !op_done */
  1154. } /* plugin loop */
  1155. } /* while plugins not started */
  1156. if(plugins_started == 0){
  1157. /* a dependency was not resolved - error */
  1158. LDAPDebug( LDAP_DEBUG_ANY, "Error: Failed to resolve plugin dependencies\n" , 0, 0, 0 );
  1159. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"Failed to resolve plugin dependencies.");
  1160. /* list the plugins yet to perform op */
  1161. i = 0;
  1162. while(i < total_plugins){
  1163. if(config[i].op_done == 0){
  1164. LDAPDebug( LDAP_DEBUG_ANY, "Error: %s plugin %s is not started\n" , config[i].type, config[i].name, 0 );
  1165. plugin_remove_plugins(config[i].plugin, config[i].type);
  1166. }
  1167. i++;
  1168. }
  1169. ret = -1;
  1170. }
  1171. done:
  1172. /*
  1173. * Free the plugin list, then rebuild it
  1174. */
  1175. if(ret == LDAP_SUCCESS){
  1176. /* Ok, everything went well, use the new global_shutdown list */
  1177. plugin_free_plugin_dep_config(&global_plugin_shutdown_order);
  1178. global_plugin_shutdown_order = global_tmp_list;
  1179. } else {
  1180. /*
  1181. * problem, undo what we've done in plugin_setup.
  1182. */
  1183. plugin_free_plugin_dep_config(&global_tmp_list);
  1184. }
  1185. /*
  1186. * need the details in config to hang around for shutdown
  1187. * config itself may be deleted since its contents have been
  1188. * copied by value to the shutdown list
  1189. */
  1190. plugin_free_plugin_dep_config(&config);
  1191. if(plugin_head){
  1192. plugin_dep_type next;
  1193. while(plugin_head){
  1194. next = plugin_head->next;
  1195. slapi_ch_free_string(&plugin_head->type);
  1196. slapi_ch_free((void *)&plugin_head);
  1197. plugin_head = next;
  1198. }
  1199. }
  1200. /* Finally enable registered plugin functions */
  1201. global_plugin_callbacks_enabled = 1;
  1202. return ret;
  1203. }
  1204. static void
  1205. plugin_free_plugin_dep_config(plugin_dep_config **cfg)
  1206. {
  1207. plugin_dep_config *config = *cfg;
  1208. int index = 0;
  1209. int i = 0;
  1210. if(config){
  1211. while( config[index].plugin ){
  1212. if(config[index].depends_named_list){
  1213. for(i = 0; i < config[index].total_named; i++){
  1214. slapi_ch_free((void**)&(config[index].depends_named_list)[i]);
  1215. }
  1216. slapi_ch_free((void**)&(config[index].depends_named_list));
  1217. }
  1218. if(config[index].depends_type_list){
  1219. for(i = 0; i < config[index].total_type; i++){
  1220. slapi_ch_free((void**)&(config[index].depends_type_list)[i]);
  1221. }
  1222. slapi_ch_free((void**)&(config[index].depends_type_list));
  1223. }
  1224. slapi_ch_free_string(&config[index].type);
  1225. slapi_ch_free_string(&config[index].name);
  1226. slapi_ch_free_string(&config[index].config_area);
  1227. pblock_done(&config[index].pb);
  1228. index++;
  1229. }
  1230. slapi_ch_free((void**)&config);
  1231. *cfg = NULL;
  1232. }
  1233. }
  1234. /*
  1235. * plugin_dependency_startall()
  1236. *
  1237. * Starts all plugins (apart from syntax and matching rule) in order
  1238. * of dependency.
  1239. *
  1240. * Dependencies will be determined by these multi-valued attributes:
  1241. *
  1242. * nsslapd-plugin-depends-on-type : all plugins whose type value matches one of these values must
  1243. * be started prior to this plugin
  1244. *
  1245. * nsslapd-plugin-depends-on-named : the plugin whose cn value matches one of these values must
  1246. * be started prior to this plugin
  1247. */
  1248. static int
  1249. plugin_dependency_startall(int argc, char** argv, char *errmsg, int operation)
  1250. {
  1251. int ret = 0;
  1252. Slapi_PBlock pb;
  1253. int total_plugins = 0;
  1254. plugin_dep_config *config = 0;
  1255. plugin_dep_type plugin_head = 0;
  1256. int plugin_index = 0;
  1257. Slapi_Entry *plugin_entry;
  1258. int i = 0; /* general index iterator */
  1259. plugin_dep_type the_plugin_type;
  1260. int index = 0;
  1261. char * value;
  1262. int plugins_started;
  1263. int num_plg_started;
  1264. struct slapdplugin *plugin;
  1265. entry_and_plugin_t *ep = dep_plugin_entries;
  1266. int shutdown_index = 0;
  1267. /*
  1268. * Disable registered plugin functions so preops/postops/etc
  1269. * dont get called prior to the plugin being started (due to
  1270. * plugins performing ops on the DIT)
  1271. */
  1272. global_plugin_callbacks_enabled = 0;
  1273. /* Count the plugins so we can allocate memory for the config array */
  1274. while(ep)
  1275. {
  1276. total_plugins++;
  1277. ep = ep->next;
  1278. }
  1279. /* allocate the config array */
  1280. config = (plugin_dep_config*)slapi_ch_calloc(total_plugins + 1, sizeof(plugin_dep_config));
  1281. ep = dep_plugin_entries;
  1282. /* Collect relevant config */
  1283. while(ep)
  1284. {
  1285. plugin = ep->plugin;
  1286. if(plugin == 0)
  1287. continue;
  1288. pblock_init(&pb);
  1289. slapi_pblock_set( &pb, SLAPI_ARGC, &argc);
  1290. slapi_pblock_set( &pb, SLAPI_ARGV, &argv);
  1291. config[plugin_index].pb = pb;
  1292. config[plugin_index].e = ep->e;
  1293. /* add type */
  1294. plugin_entry = ep->e;
  1295. if(plugin_entry)
  1296. {
  1297. /*
  1298. * Pass the plugin DN in SLAPI_TARGET_SDN and the plugin entry
  1299. * in SLAPI_ADD_ENTRY. For this to actually work, we need to
  1300. * create an operation and include that in the pblock as well,
  1301. * because these two items are stored in the operation parameters.
  1302. */
  1303. /* WARNING: memory leak here - op is only freed by a pblock_done,
  1304. and this only happens below if the plugin is enabled - a short
  1305. circuit goto bail may also cause a leak - however, since this
  1306. only happens a few times at startup, this is not a very serious
  1307. leak - just after the call to plugin_call_one */
  1308. Operation *op = internal_operation_new(SLAPI_OPERATION_ADD, 0);
  1309. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_OPERATION, op);
  1310. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_TARGET_SDN,
  1311. (void*)(slapi_entry_get_sdn_const(plugin_entry)));
  1312. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_ADD_ENTRY,
  1313. plugin_entry );
  1314. /* Pass the plugin alternate config area DN in SLAPI_PLUGIN_CONFIG_AREA. */
  1315. value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_CONFIG_AREA);
  1316. if(value)
  1317. {
  1318. config[plugin_index].config_area = value;
  1319. value = NULL;
  1320. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_PLUGIN_CONFIG_AREA,
  1321. config[plugin_index].config_area);
  1322. }
  1323. value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype");
  1324. if(value)
  1325. {
  1326. add_plugin_type( &plugin_head, value);
  1327. config[plugin_index].type = value;
  1328. value = NULL;
  1329. }
  1330. /* now the name */
  1331. value = slapi_entry_attr_get_charptr(plugin_entry, "cn");
  1332. if(value)
  1333. {
  1334. config[plugin_index].name = value;
  1335. value = NULL;
  1336. }
  1337. config[plugin_index].plugin = plugin;
  1338. /* now add dependencies */
  1339. plugin_create_stringlist( plugin_entry, "nsslapd-plugin-depends-on-named",
  1340. &(config[plugin_index].total_named), &(config[plugin_index].depends_named_list));
  1341. plugin_create_stringlist( plugin_entry, "nsslapd-plugin-depends-on-type",
  1342. &(config[plugin_index].total_type), &(config[plugin_index].depends_type_list));
  1343. }
  1344. plugin_index++;
  1345. ep = ep->next;
  1346. }
  1347. /* prepare list of shutdown order (we need nothing fancier right now
  1348. * than the reverse startup order) The list may include NULL entries,
  1349. * these will be plugins which were never started
  1350. */
  1351. shutdown_index = total_plugins - 1;
  1352. global_plugin_shutdown_order = (plugin_dep_config*)slapi_ch_calloc(total_plugins + 1, sizeof(plugin_dep_config));
  1353. /* now resolve dependencies
  1354. * cycle through list, if a plugin has no dependencies then start it
  1355. * then remove it from the dependency lists of all other plugins
  1356. * and decrement the corresponding element of the plugin types array
  1357. * for depends_type we will need to check the array of plugin types
  1358. * to see if all type dependencies are at zero prior to start
  1359. * if one cycle fails to load any plugins we have failed, however
  1360. * we shall continue loading plugins in case a configuration error
  1361. * can correct itself
  1362. */
  1363. plugins_started = 1;
  1364. num_plg_started = 0;
  1365. while(plugins_started && num_plg_started < total_plugins)
  1366. {
  1367. plugins_started = 0;
  1368. for(plugin_index=0; plugin_index < total_plugins; plugin_index++)
  1369. {
  1370. /* perform op on plugins only once */
  1371. if(config[plugin_index].op_done == 0)
  1372. {
  1373. int enabled = 0;
  1374. int satisfied = 0;
  1375. int break_out = 0;
  1376. /*
  1377. * determine if plugin is enabled
  1378. * some processing is necessary even
  1379. * if it is not
  1380. */
  1381. if ( NULL != config[plugin_index].e && (value = slapi_entry_attr_get_charptr(config[plugin_index].e,
  1382. ATTR_PLUGIN_ENABLED)) &&
  1383. !strcasecmp(value, "on"))
  1384. {
  1385. enabled = 1;
  1386. }
  1387. else
  1388. enabled = 0;
  1389. slapi_ch_free((void**)&value);
  1390. /*
  1391. * make sure named dependencies have been satisfied
  1392. * that means that the list of names should contain all
  1393. * null entries
  1394. */
  1395. if(enabled && config[plugin_index].total_named)
  1396. {
  1397. i = 0;
  1398. while(break_out == 0 && i < config[plugin_index].total_named)
  1399. {
  1400. satisfied = 1;
  1401. if((config[plugin_index].depends_named_list)[i] != 0)
  1402. {
  1403. satisfied = 0;
  1404. break_out = 1;
  1405. }
  1406. i++;
  1407. }
  1408. if(!satisfied)
  1409. continue;
  1410. }
  1411. /*
  1412. * make sure the type dependencies have been satisfied
  1413. * that means for each type in the list, it's number of
  1414. * plugins left not started is zero
  1415. *
  1416. */
  1417. satisfied = 0;
  1418. break_out = 0;
  1419. if(enabled && config[plugin_index].total_type)
  1420. {
  1421. i = 0;
  1422. while(break_out == 0 && i < config[plugin_index].total_type)
  1423. {
  1424. satisfied = 1;
  1425. the_plugin_type = find_plugin_type(plugin_head, (config[plugin_index].depends_type_list)[i]);
  1426. if(the_plugin_type && the_plugin_type->num_not_started != 0)
  1427. {
  1428. satisfied = 0;
  1429. break_out = 1;
  1430. }
  1431. i++;
  1432. }
  1433. if(!satisfied)
  1434. continue;
  1435. }
  1436. /**** This plugins dependencies have now been satisfied ****/
  1437. satisfied = 1; /* symbolic only */
  1438. /*
  1439. * Add the plugins entry to the DSE so the plugin can get
  1440. * its config (both enabled and disabled have entries
  1441. */
  1442. if(!config[plugin_index].entry_created)
  1443. {
  1444. int plugin_actions = 0;
  1445. Slapi_PBlock newpb;
  1446. Slapi_Entry *newe;
  1447. pblock_init(&newpb);
  1448. newe = slapi_entry_dup( config[plugin_index].e );
  1449. slapi_add_entry_internal_set_pb(&newpb, newe, NULL,
  1450. plugin_get_default_component_id(), plugin_actions);
  1451. slapi_pblock_set(&newpb, SLAPI_TARGET_SDN, (void*)slapi_entry_get_sdn_const(newe));
  1452. slapi_add_internal_pb(&newpb);
  1453. pblock_done(&newpb);
  1454. config[plugin_index].entry_created = 1;
  1455. }
  1456. /*
  1457. * only actually start plugin and remove from lists if its enabled
  1458. * we do remove from plugin type list however - rule is dependency on
  1459. * zero or more for type
  1460. */
  1461. if (enabled)
  1462. {
  1463. /* finally, perform the op on the plugin */
  1464. LDAPDebug( LDAP_DEBUG_PLUGIN, "Starting %s plugin %s\n" , config[plugin_index].type, config[plugin_index].name, 0 );
  1465. /*
  1466. * Put the plugin into the temporary pblock so the startup functions have
  1467. * access to the real plugin for registering callbacks, tasks, etc.
  1468. */
  1469. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_PLUGIN, config[plugin_index].plugin);
  1470. ret = plugin_call_one( config[plugin_index].plugin, operation, &(config[plugin_index].pb));
  1471. pblock_done(&(config[plugin_index].pb));
  1472. if(ret)
  1473. {
  1474. /*
  1475. * We will not exit here. If we allow plugins to load normally it is
  1476. * possible that a configuration error (dependencies which were not
  1477. * configured properly) can be recovered from. If there really is a
  1478. * problem then the plugin will never start and eventually it will
  1479. * trigger an exit anyway.
  1480. */
  1481. LDAPDebug( LDAP_DEBUG_ANY, "Failed to start %s plugin %s\n" , config[plugin_index].type, config[plugin_index].name, 0 );
  1482. continue;
  1483. } else {
  1484. /* now set the plugin and all its registered plugin functions as started */
  1485. plugin_set_plugins_started(config[plugin_index].plugin);
  1486. }
  1487. /* Add this plugin to the shutdown list */
  1488. global_plugin_shutdown_order[shutdown_index] = config[plugin_index];
  1489. shutdown_index--;
  1490. global_plugins_started++;
  1491. /* remove this named plugin from other plugins lists */
  1492. for(i=0; i<total_plugins; i++)
  1493. {
  1494. index = 0;
  1495. while(index < config[i].total_named)
  1496. {
  1497. if((config[i].depends_named_list)[index] != 0 && !slapi_UTF8CASECMP((config[i].depends_named_list)[index], config[plugin_index].name))
  1498. {
  1499. slapi_ch_free((void**)&((config[i].depends_named_list)[index]));
  1500. (config[i].depends_named_list)[index] = 0;
  1501. }
  1502. index++;
  1503. }
  1504. }
  1505. } else {
  1506. pblock_done(&(config[plugin_index].pb));
  1507. }
  1508. /* decrement the type counter for this plugin type */
  1509. decrement_plugin_type(plugin_head, config[plugin_index].type);
  1510. config[plugin_index].op_done = 1;
  1511. num_plg_started++;
  1512. plugins_started = 1;
  1513. }
  1514. }
  1515. }
  1516. if(plugins_started == 0)
  1517. {
  1518. /* a dependency was not resolved - error */
  1519. LDAPDebug( LDAP_DEBUG_ANY, "Error: Failed to resolve plugin dependencies\n" , 0, 0, 0 );
  1520. /* list the plugins yet to perform op */
  1521. i = 0;
  1522. while(i < total_plugins)
  1523. {
  1524. if(config[i].op_done == 0)
  1525. {
  1526. LDAPDebug( LDAP_DEBUG_ANY, "Error: %s plugin %s is not started\n" , config[i].type, config[i].name, 0 );
  1527. }
  1528. i++;
  1529. }
  1530. exit(1);
  1531. }
  1532. /*
  1533. * need the details in config to hang around for shutdown
  1534. * config itself may be deleted since its contents have been
  1535. * copied by value to the shutdown list
  1536. */
  1537. plugin_free_plugin_dep_config(&config);
  1538. /* Finally enable registered plugin functions */
  1539. global_plugin_callbacks_enabled = 1;
  1540. return ret;
  1541. }
  1542. /*
  1543. * plugin_dependency_closeall
  1544. *
  1545. * uses the shutdown list created at startup to close
  1546. * plugins in the correct order
  1547. *
  1548. * For now this leaks the list and contents, but since
  1549. * it hangs around until shutdown anyway, we don't care
  1550. *
  1551. */
  1552. void
  1553. plugin_dependency_closeall()
  1554. {
  1555. Slapi_PBlock pb;
  1556. int plugins_closed = 0;
  1557. int index = 0;
  1558. while(plugins_closed<global_plugins_started)
  1559. {
  1560. /*
  1561. * the first few entries may not be valid
  1562. * since the list was created in the reverse
  1563. * order and some plugins may have been counted
  1564. * for the purpose of list allocation but are
  1565. * disabled and so were never started
  1566. *
  1567. * we check that here
  1568. */
  1569. if(global_plugin_shutdown_order[index].name)
  1570. {
  1571. if(!global_plugin_shutdown_order[index].removed){
  1572. pblock_init(&pb);
  1573. plugin_set_stopped(global_plugin_shutdown_order[index].plugin);
  1574. plugin_op_all_finished(global_plugin_shutdown_order[index].plugin);
  1575. plugin_call_one( global_plugin_shutdown_order[index].plugin, SLAPI_PLUGIN_CLOSE_FN, &pb);
  1576. /* set plg_closed to 1 to prevent any further plugin pre/post op function calls */
  1577. global_plugin_shutdown_order[index].plugin->plg_closed = 1;
  1578. }
  1579. plugins_closed++;
  1580. }
  1581. index++;
  1582. }
  1583. }
  1584. /***********************************************************
  1585. end of plugin dependency code
  1586. ************************************************************/
  1587. /*
  1588. * Function: plugin_startall
  1589. *
  1590. * Returns: squat
  1591. *
  1592. * Description: Some plugins may need to do some stuff after all the config
  1593. * stuff is done with. So this function goes through and starts all plugins
  1594. */
  1595. void
  1596. plugin_startall(int argc, char** argv, int start_backends, int start_global)
  1597. {
  1598. /* initialize special plugin structures */
  1599. default_plugin_init ();
  1600. plugin_dependency_startall(argc, argv, "plugin startup failed\n", SLAPI_PLUGIN_START_FN);
  1601. }
  1602. /*
  1603. * Function: plugin_close_all
  1604. *
  1605. * Returns: squat
  1606. *
  1607. * Description: cleanup routine, allows plugins to kill threads, free memory started in start fn
  1608. *
  1609. */
  1610. void
  1611. plugin_closeall(int close_backends, int close_globals)
  1612. {
  1613. entry_and_plugin_t *iterp, *nextp;
  1614. plugin_dependency_closeall();
  1615. /* free the plugin dependency entry list */
  1616. iterp = dep_plugin_entries;
  1617. while (iterp) {
  1618. nextp = iterp->next;
  1619. slapi_entry_free(iterp->e);
  1620. slapi_ch_free((void **)&iterp);
  1621. iterp = nextp;
  1622. }
  1623. dep_plugin_entries = NULL;
  1624. }
  1625. static int
  1626. plugin_call_list (struct slapdplugin *list, int operation, Slapi_PBlock *pb)
  1627. {
  1628. return plugin_call_func(list, operation, pb, 0);
  1629. }
  1630. static int
  1631. plugin_call_one (struct slapdplugin *list, int operation, Slapi_PBlock *pb)
  1632. {
  1633. return plugin_call_func(list, operation, pb, 1);
  1634. }
  1635. /*
  1636. * Return codes:
  1637. * - For preoperation plugins, returns the return code passed back from the first
  1638. * plugin that fails, or zero if all plugins succeed.
  1639. * - For bepreop and bepostop plugins, returns a bitwise OR of the return codes
  1640. * returned by all the plugins called (there's only one bepreop and one bepostop
  1641. * in DS 5.0 anyway).
  1642. * - For postoperation plugins, returns 0.
  1643. */
  1644. static int
  1645. plugin_call_func (struct slapdplugin *list, int operation, Slapi_PBlock *pb, int call_one)
  1646. {
  1647. /* Invoke the operation on the plugins that are registered for the subtree effected by the operation. */
  1648. int rc;
  1649. int return_value = 0;
  1650. int count = 0;
  1651. for (; list != NULL; list = list->plg_next)
  1652. {
  1653. IFP func = NULL;
  1654. slapi_pblock_set (pb, SLAPI_PLUGIN, list);
  1655. set_db_default_result_handlers (pb); /* JCM: What's this do? Is it needed here? */
  1656. if (slapi_pblock_get (pb, operation, &func) == 0 &&
  1657. func != NULL &&
  1658. plugin_invoke_plugin_pb (list, operation, pb) &&
  1659. list->plg_closed == 0)
  1660. {
  1661. char *n = list->plg_name;
  1662. LDAPDebug( LDAP_DEBUG_TRACE , "Calling plugin '%s' #%d type %d\n", (n==NULL?"noname":n), count, operation );
  1663. /* counters_to_errors_log("before plugin call"); */
  1664. /*
  1665. * Only call the plugin function if:
  1666. *
  1667. * [1] The plugin is started, and we are NOT trying to restart it.
  1668. * [2] The plugin is started, and we are stopping it.
  1669. * [3] The plugin is stopped, and we are trying to start it.
  1670. *
  1671. * This frees up the plugins from having to check if the plugin is already started when
  1672. * calling the START and CLOSE functions - prevents double starts and stops.
  1673. */
  1674. slapi_plugin_op_started(list);
  1675. if (((SLAPI_PLUGIN_START_FN == operation && !list->plg_started) || /* Starting it up for the first time */
  1676. (SLAPI_PLUGIN_CLOSE_FN == operation && !list->plg_stopped) || /* Shutting down, plugin has been stopped */
  1677. (SLAPI_PLUGIN_START_FN != operation && list->plg_started)) && /* Started, and not trying to start again */
  1678. ( rc = func (pb)) != 0 )
  1679. {
  1680. slapi_plugin_op_finished(list);
  1681. if (SLAPI_PLUGIN_PREOPERATION == list->plg_type ||
  1682. SLAPI_PLUGIN_INTERNAL_PREOPERATION == list->plg_type ||
  1683. SLAPI_PLUGIN_START_FN == operation )
  1684. {
  1685. /*
  1686. * We bail out of plugin processing for preop plugins
  1687. * that return a non-zero return code. This allows preop
  1688. * plugins to cause further preop processing to terminate, and
  1689. * causes the operation to be vetoed.
  1690. */
  1691. return_value = rc;
  1692. break;
  1693. }
  1694. else if (SLAPI_PLUGIN_BEPREOPERATION == list->plg_type ||
  1695. SLAPI_PLUGIN_BETXNPREOPERATION == list->plg_type ||
  1696. SLAPI_PLUGIN_BEPOSTOPERATION == list->plg_type ||
  1697. SLAPI_PLUGIN_BETXNPOSTOPERATION == list->plg_type )
  1698. {
  1699. /*
  1700. * respect fatal error SLAPI_PLUGIN_FAILURE (-1);
  1701. * should not OR it.
  1702. */
  1703. if (SLAPI_PLUGIN_FAILURE == rc) {
  1704. return_value = rc;
  1705. } else if (SLAPI_PLUGIN_FAILURE != return_value) {
  1706. /* OR the result into the return value
  1707. * for be pre/postops */
  1708. return_value |= rc;
  1709. }
  1710. }
  1711. } else {
  1712. if(SLAPI_PLUGIN_CLOSE_FN == operation){
  1713. /* successfully stopped the plugin */
  1714. list->plg_stopped = 1;
  1715. }
  1716. slapi_plugin_op_finished(list);
  1717. }
  1718. /* counters_to_errors_log("after plugin call"); */
  1719. }
  1720. count++;
  1721. if(call_one)
  1722. break;
  1723. }
  1724. return( return_value );
  1725. }
  1726. int
  1727. slapi_berval_cmp (const struct berval* L, const struct berval* R) /* JCM - This does not belong here. But, where should it go? */
  1728. {
  1729. int result = 0;
  1730. if(L == NULL && R != NULL){
  1731. return 1;
  1732. } else if(L != NULL && R == NULL){
  1733. return -1;
  1734. } else if(L == NULL && R == NULL){
  1735. return 0;
  1736. }
  1737. if (L->bv_len < R->bv_len) {
  1738. result = memcmp (L->bv_val, R->bv_val, L->bv_len);
  1739. if (result == 0)
  1740. result = -1;
  1741. } else {
  1742. result = memcmp (L->bv_val, R->bv_val, R->bv_len);
  1743. if (result == 0 && (L->bv_len > R->bv_len))
  1744. result = 1;
  1745. }
  1746. return result;
  1747. }
  1748. static char **supported_saslmechanisms = NULL;
  1749. static Slapi_RWLock *supported_saslmechanisms_lock = NULL;
  1750. /*
  1751. * register a supported SASL mechanism so it will be returned as part of the
  1752. * root DSE.
  1753. */
  1754. void
  1755. slapi_register_supported_saslmechanism( char *mechanism )
  1756. {
  1757. if ( mechanism != NULL ) {
  1758. if (NULL == supported_saslmechanisms_lock) {
  1759. /* This is thread safe, as it gets executed by
  1760. * a single thread at init time (main->init_saslmechanisms) */
  1761. supported_saslmechanisms_lock = slapi_new_rwlock();
  1762. if (NULL == supported_saslmechanisms_lock) {
  1763. /* Out of resources */
  1764. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  1765. "slapi_register_supported_saslmechanism: failed to create lock.\n");
  1766. exit (1);
  1767. }
  1768. }
  1769. slapi_rwlock_wrlock(supported_saslmechanisms_lock);
  1770. charray_add( &supported_saslmechanisms, slapi_ch_strdup( mechanism ));
  1771. slapi_rwlock_unlock(supported_saslmechanisms_lock);
  1772. }
  1773. }
  1774. /*
  1775. * return pointer to NULL-terminated array of supported SASL mechanisms.
  1776. * This function is not MTSafe and should be deprecated.
  1777. * slapi_get_supported_saslmechanisms_copy should be used instead.
  1778. */
  1779. char **
  1780. slapi_get_supported_saslmechanisms( void )
  1781. {
  1782. return( supported_saslmechanisms );
  1783. }
  1784. /*
  1785. * return pointer to NULL-terminated array of supported SASL mechanisms.
  1786. */
  1787. char **
  1788. slapi_get_supported_saslmechanisms_copy( void )
  1789. {
  1790. char ** ret = NULL;
  1791. slapi_rwlock_rdlock(supported_saslmechanisms_lock);
  1792. ret = charray_dup(supported_saslmechanisms);
  1793. slapi_rwlock_unlock(supported_saslmechanisms_lock);
  1794. return( ret );
  1795. }
  1796. static char **supported_extended_ops = NULL;
  1797. static Slapi_RWLock *extended_ops_lock = NULL;
  1798. /*
  1799. * register all of the LDAPv3 extended operations we know about.
  1800. */
  1801. void
  1802. ldapi_init_extended_ops( void )
  1803. {
  1804. extended_ops_lock = slapi_new_rwlock();
  1805. if (NULL == extended_ops_lock) {
  1806. /* Out of resources */
  1807. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  1808. "ldapi_init_extended_ops: failed to create lock.\n");
  1809. exit (1);
  1810. }
  1811. slapi_rwlock_wrlock(extended_ops_lock);
  1812. charray_add(&supported_extended_ops,
  1813. slapi_ch_strdup(EXTOP_BULK_IMPORT_START_OID));
  1814. charray_add(&supported_extended_ops,
  1815. slapi_ch_strdup(EXTOP_BULK_IMPORT_DONE_OID));
  1816. /* add future supported extops here... */
  1817. slapi_rwlock_unlock(extended_ops_lock);
  1818. }
  1819. /*
  1820. * register an extended op. so it can be returned as part of the root DSE.
  1821. */
  1822. void
  1823. ldapi_register_extended_op( char **opoids )
  1824. {
  1825. int i;
  1826. slapi_rwlock_wrlock(extended_ops_lock);
  1827. for ( i = 0; opoids != NULL && opoids[i] != NULL; ++i ) {
  1828. if ( !charray_inlist( supported_extended_ops, opoids[i] )) {
  1829. charray_add( &supported_extended_ops, slapi_ch_strdup( opoids[i] ));
  1830. }
  1831. }
  1832. slapi_rwlock_unlock(extended_ops_lock);
  1833. }
  1834. /*
  1835. * retrieve supported extended operation OIDs
  1836. * return 0 if successful and -1 if not.
  1837. * This function is not MTSafe and should be deprecated.
  1838. * slapi_get_supported_extended_ops_copy should be used instead.
  1839. */
  1840. char **
  1841. slapi_get_supported_extended_ops( void )
  1842. {
  1843. return( supported_extended_ops );
  1844. }
  1845. /*
  1846. * retrieve supported extended operation OIDs
  1847. * return 0 if successful and -1 if not.
  1848. */
  1849. char **
  1850. slapi_get_supported_extended_ops_copy( void )
  1851. {
  1852. char ** ret = NULL;
  1853. slapi_rwlock_rdlock(extended_ops_lock);
  1854. ret = charray_dup(supported_extended_ops);
  1855. slapi_rwlock_unlock(extended_ops_lock);
  1856. return( ret );
  1857. }
  1858. /*
  1859. looks up the given string type to convert to the internal integral type; also
  1860. returns the plugin list associated with the plugin type
  1861. returns 0 upon success and non-zero upon failure
  1862. */
  1863. static int
  1864. plugin_get_type_and_list(
  1865. const char *plugintype,
  1866. int *type,
  1867. struct slapdplugin ***plugin_list
  1868. )
  1869. {
  1870. int plugin_list_index = -1;
  1871. if ( strcasecmp( plugintype, "database" ) == 0 ) {
  1872. *type = SLAPI_PLUGIN_DATABASE;
  1873. plugin_list_index= PLUGIN_LIST_DATABASE;
  1874. } else if ( strcasecmp( plugintype, "extendedop" ) == 0 ) {
  1875. *type = SLAPI_PLUGIN_EXTENDEDOP;
  1876. plugin_list_index= PLUGIN_LIST_EXTENDED_OPERATION;
  1877. } else if ( strcasecmp( plugintype, "preoperation" ) == 0 ) {
  1878. *type = SLAPI_PLUGIN_PREOPERATION;
  1879. plugin_list_index= PLUGIN_LIST_PREOPERATION;
  1880. } else if ( strcasecmp( plugintype, "postoperation" ) == 0 ) {
  1881. *type = SLAPI_PLUGIN_POSTOPERATION;
  1882. plugin_list_index= PLUGIN_LIST_POSTOPERATION;
  1883. } else if ( strcasecmp( plugintype, "matchingrule" ) == 0 ) {
  1884. *type = SLAPI_PLUGIN_MATCHINGRULE;
  1885. plugin_list_index= PLUGIN_LIST_MATCHINGRULE;
  1886. } else if ( strcasecmp( plugintype, "syntax" ) == 0 ) {
  1887. *type = SLAPI_PLUGIN_SYNTAX;
  1888. plugin_list_index= PLUGIN_LIST_SYNTAX;
  1889. } else if ( strcasecmp( plugintype, "accesscontrol" ) == 0 ) {
  1890. *type = SLAPI_PLUGIN_ACL;
  1891. plugin_list_index= PLUGIN_LIST_ACL;
  1892. } else if ( strcasecmp( plugintype, "bepreoperation" ) == 0 ) {
  1893. *type = SLAPI_PLUGIN_BEPREOPERATION;
  1894. plugin_list_index= PLUGIN_LIST_BEPREOPERATION;
  1895. } else if ( strcasecmp( plugintype, "bepostoperation" ) == 0 ) {
  1896. *type = SLAPI_PLUGIN_BEPOSTOPERATION;
  1897. plugin_list_index= PLUGIN_LIST_BEPOSTOPERATION;
  1898. } else if ( strcasecmp( plugintype, "betxnpreoperation" ) == 0 ) {
  1899. *type = SLAPI_PLUGIN_BETXNPREOPERATION;
  1900. plugin_list_index= PLUGIN_LIST_BETXNPREOPERATION;
  1901. } else if ( strcasecmp( plugintype, "betxnpostoperation" ) == 0 ) {
  1902. *type = SLAPI_PLUGIN_BETXNPOSTOPERATION;
  1903. plugin_list_index= PLUGIN_LIST_BETXNPOSTOPERATION;
  1904. } else if ( strcasecmp( plugintype, "internalpreoperation" ) == 0 ) {
  1905. *type = SLAPI_PLUGIN_INTERNAL_PREOPERATION;
  1906. plugin_list_index= PLUGIN_LIST_INTERNAL_PREOPERATION;
  1907. } else if ( strcasecmp( plugintype, "internalpostoperation" ) == 0 ) {
  1908. *type = SLAPI_PLUGIN_INTERNAL_POSTOPERATION;
  1909. plugin_list_index= PLUGIN_LIST_INTERNAL_POSTOPERATION;
  1910. } else if ( strcasecmp( plugintype, "entry" ) == 0 ) {
  1911. *type = SLAPI_PLUGIN_ENTRY;
  1912. plugin_list_index= PLUGIN_LIST_ENTRY;
  1913. } else if ( strcasecmp( plugintype, "object" ) == 0 ) {
  1914. *type = SLAPI_PLUGIN_TYPE_OBJECT;
  1915. plugin_list_index= PLUGIN_LIST_OBJECT;
  1916. } else if ( strcasecmp( plugintype, "pwdstoragescheme" ) == 0 ) {
  1917. *type = SLAPI_PLUGIN_PWD_STORAGE_SCHEME;
  1918. plugin_list_index= PLUGIN_LIST_PWD_STORAGE_SCHEME;
  1919. } else if ( strcasecmp( plugintype, "reverpwdstoragescheme" ) == 0 ) {
  1920. *type = SLAPI_PLUGIN_REVER_PWD_STORAGE_SCHEME;
  1921. plugin_list_index= PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME;
  1922. } else if ( strcasecmp( plugintype, "vattrsp" ) == 0 ) {
  1923. *type = SLAPI_PLUGIN_VATTR_SP;
  1924. plugin_list_index= PLUGIN_LIST_VATTR_SP;
  1925. } else if ( strcasecmp( plugintype, "ldbmentryfetchstore" ) == 0 ) {
  1926. *type = SLAPI_PLUGIN_LDBM_ENTRY_FETCH_STORE;
  1927. plugin_list_index= PLUGIN_LIST_LDBM_ENTRY_FETCH_STORE;
  1928. } else if ( strcasecmp( plugintype, "index" ) == 0 ) {
  1929. *type = SLAPI_PLUGIN_INDEX;
  1930. plugin_list_index= PLUGIN_LIST_INDEX;
  1931. } else {
  1932. return( 1 ); /* unknown plugin type - pass to backend */
  1933. }
  1934. if (plugin_list_index >= 0)
  1935. *plugin_list = &global_plugin_list[plugin_list_index];
  1936. return 0;
  1937. }
  1938. static char *
  1939. plugin_get_type_str( int type )
  1940. {
  1941. if ( type == SLAPI_PLUGIN_DATABASE){
  1942. return "database";
  1943. } else if ( type == SLAPI_PLUGIN_EXTENDEDOP){
  1944. return "extendedop";
  1945. } else if ( type == SLAPI_PLUGIN_PREOPERATION){
  1946. return "preoperation";
  1947. } else if ( type == SLAPI_PLUGIN_POSTOPERATION){
  1948. return "postoperation";
  1949. } else if ( type == SLAPI_PLUGIN_MATCHINGRULE){
  1950. return "matchingrule";
  1951. } else if ( type == SLAPI_PLUGIN_SYNTAX){
  1952. return "syntax";
  1953. } else if ( type == SLAPI_PLUGIN_ACL){
  1954. return "accesscontrol";
  1955. } else if ( type == SLAPI_PLUGIN_BEPREOPERATION){
  1956. return "bepreoperation";
  1957. } else if ( type == SLAPI_PLUGIN_BEPOSTOPERATION){
  1958. return "bepostoperation";
  1959. } else if ( type == SLAPI_PLUGIN_BETXNPREOPERATION){
  1960. return "betxnpreoperation";
  1961. } else if ( type == SLAPI_PLUGIN_BETXNPOSTOPERATION){
  1962. return "betxnpostoperation";
  1963. } else if ( type == SLAPI_PLUGIN_INTERNAL_PREOPERATION){
  1964. return "internalpreoperation";
  1965. } else if ( type == SLAPI_PLUGIN_INTERNAL_POSTOPERATION){
  1966. return "internalpostoperation";
  1967. } else if ( type == SLAPI_PLUGIN_ENTRY){
  1968. return "entry";
  1969. } else if ( type == SLAPI_PLUGIN_TYPE_OBJECT){
  1970. return "object";
  1971. } else if ( type == SLAPI_PLUGIN_PWD_STORAGE_SCHEME){
  1972. return "pwdstoragescheme";
  1973. } else if ( type == SLAPI_PLUGIN_REVER_PWD_STORAGE_SCHEME){
  1974. return "reverpwdstoragescheme";
  1975. } else if ( type == SLAPI_PLUGIN_VATTR_SP){
  1976. return "vattrsp";
  1977. } else if ( type == SLAPI_PLUGIN_LDBM_ENTRY_FETCH_STORE){
  1978. return "ldbmentryfetchstore";
  1979. } else if ( type == SLAPI_PLUGIN_INDEX){
  1980. return "index";
  1981. } else {
  1982. return NULL; /* unknown plugin type - pass to backend */
  1983. }
  1984. }
  1985. static const char *
  1986. plugin_exists(const Slapi_DN *plugin_dn)
  1987. {
  1988. /* check to see if the plugin name is unique */
  1989. const char *retval = 0;
  1990. if (global_plugin_dns && PL_HashTableLookup(global_plugin_dns,
  1991. slapi_sdn_get_ndn(plugin_dn)))
  1992. {
  1993. retval = slapi_sdn_get_dn(plugin_dn);
  1994. }
  1995. return retval;
  1996. }
  1997. /*
  1998. * A plugin config change has occurred, restart the plugin (delete/add).
  1999. * If something goes wrong, revert to the original plugin entry.
  2000. */
  2001. int
  2002. plugin_restart(Slapi_Entry *pentryBefore, Slapi_Entry *pentryAfter)
  2003. {
  2004. char returntext[SLAPI_DSE_RETURNTEXT_SIZE];
  2005. int rc = LDAP_SUCCESS;
  2006. /*
  2007. * We can not restart a critical plugin, but the operation should still
  2008. * be allowed
  2009. */
  2010. if(plugin_is_critical(pentryBefore)){
  2011. LDAPDebug(LDAP_DEBUG_ANY, "plugin_restart: Plugin (%s) is critical "
  2012. "to server operation. Server requires restart for changes to "
  2013. "take effect.\n", slapi_entry_get_dn(pentryBefore),0,0);
  2014. return 0;
  2015. }
  2016. slapi_rwlock_wrlock(global_rwlock);
  2017. slapi_td_set_plugin_locked();
  2018. if(plugin_delete(pentryBefore, returntext, 1) == LDAP_SUCCESS){
  2019. if(plugin_add(pentryAfter, returntext, 1) == LDAP_SUCCESS){
  2020. LDAPDebug(LDAP_DEBUG_PLUGIN, "plugin_restart: Plugin (%s) has been successfully "
  2021. "restarted after configuration change.\n",
  2022. slapi_entry_get_dn(pentryAfter),0,0);
  2023. } else {
  2024. LDAPDebug(LDAP_DEBUG_ANY, "plugin_restart: Plugin (%s) failed to restart after "
  2025. "configuration change (%s). Reverting to original plugin entry.\n",
  2026. slapi_entry_get_dn(pentryAfter), returntext, 0);
  2027. if(plugin_add(pentryBefore, returntext, 1) != LDAP_SUCCESS){
  2028. LDAPDebug(LDAP_DEBUG_ANY, "plugin_restart: Plugin (%s) failed to reload "
  2029. "original plugin (%s)\n",slapi_entry_get_dn(pentryBefore), returntext, 0);
  2030. }
  2031. rc = 1;
  2032. }
  2033. } else {
  2034. LDAPDebug(LDAP_DEBUG_ANY,"plugin_restart: failed to disable/stop the plugin (%s): %s\n",
  2035. slapi_entry_get_dn(pentryBefore), returntext, 0);
  2036. rc = 1;
  2037. }
  2038. slapi_rwlock_unlock(global_rwlock);
  2039. slapi_td_set_plugin_unlocked();
  2040. return rc;
  2041. }
  2042. static int
  2043. plugin_set_subtree_config(PluginTargetData *subtree_config, const char *val)
  2044. {
  2045. int status = 0;
  2046. if (strcasecmp (val, ALL_DATA) == 0) /* allow access to both local and remote data */
  2047. {
  2048. plugin_set_global (subtree_config);
  2049. }
  2050. else if (strcasecmp (val, LOCAL_DATA) == 0) /* allow access to all locally hosted data */
  2051. {
  2052. ptd_set_special_data (subtree_config, PLGC_DATA_LOCAL);
  2053. }
  2054. else if (strcasecmp (val, REMOTE_DATA) == 0)/* allow access to requests for remote data */
  2055. {
  2056. ptd_set_special_data (subtree_config, PLGC_DATA_REMOTE);
  2057. }
  2058. else /* dn */
  2059. {
  2060. ptd_add_subtree (subtree_config, slapi_sdn_new_dn_byval(val));
  2061. }
  2062. /* I suppose we could check the val at this point to make sure
  2063. its a valid DN . . . */
  2064. return status;
  2065. }
  2066. static int
  2067. set_plugin_config_from_entry(
  2068. const Slapi_Entry *plugin_entry,
  2069. struct slapdplugin *plugin
  2070. )
  2071. {
  2072. struct pluginconfig *config = &plugin->plg_conf;
  2073. char *value = 0;
  2074. char **values = 0;
  2075. int i = 0;
  2076. int status = 0;
  2077. PRBool target_seen = PR_FALSE;
  2078. PRBool bind_seen = PR_FALSE;
  2079. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  2080. ATTR_PLUGIN_SCHEMA_CHECK)) != NULL)
  2081. {
  2082. if (plugin_config_set_action(&config->plgc_schema_check, value))
  2083. {
  2084. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2085. "from entry %s\n", value, ATTR_PLUGIN_SCHEMA_CHECK,
  2086. slapi_entry_get_dn_const(plugin_entry));
  2087. status = 1;
  2088. }
  2089. slapi_ch_free((void**)&value);
  2090. }
  2091. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  2092. ATTR_PLUGIN_LOG_ACCESS)) != NULL)
  2093. {
  2094. if (plugin_config_set_action(&config->plgc_log_access, value))
  2095. {
  2096. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2097. "from entry %s\n", value, ATTR_PLUGIN_LOG_ACCESS,
  2098. slapi_entry_get_dn_const(plugin_entry));
  2099. status = 1;
  2100. }
  2101. slapi_ch_free((void**)&value);
  2102. }
  2103. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  2104. ATTR_PLUGIN_LOG_AUDIT)) != NULL)
  2105. {
  2106. if (plugin_config_set_action(&config->plgc_log_audit, value))
  2107. {
  2108. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2109. "from entry %s\n", value, ATTR_PLUGIN_LOG_AUDIT,
  2110. slapi_entry_get_dn_const(plugin_entry));
  2111. status = 1;
  2112. }
  2113. slapi_ch_free((void**)&value);
  2114. }
  2115. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  2116. ATTR_PLUGIN_INVOKE_FOR_REPLOP)) != NULL)
  2117. {
  2118. if (plugin_config_set_action(&config->plgc_invoke_for_replop, value))
  2119. {
  2120. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2121. "from entry %s\n", value, ATTR_PLUGIN_INVOKE_FOR_REPLOP,
  2122. slapi_entry_get_dn_const(plugin_entry));
  2123. status = 1;
  2124. }
  2125. slapi_ch_free((void**)&value);
  2126. }
  2127. values = slapi_entry_attr_get_charray(plugin_entry,
  2128. ATTR_PLUGIN_TARGET_SUBTREE);
  2129. for (i=0; values && values[i]; i++)
  2130. {
  2131. if (plugin_set_subtree_config(&(config->plgc_target_subtrees), values[i]))
  2132. {
  2133. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2134. "from entry %s\n", values[i], ATTR_PLUGIN_TARGET_SUBTREE,
  2135. slapi_entry_get_dn_const(plugin_entry));
  2136. status = 1;
  2137. break;
  2138. }
  2139. else
  2140. {
  2141. target_seen = PR_TRUE;
  2142. }
  2143. }
  2144. slapi_ch_array_free(values);
  2145. values = slapi_entry_attr_get_charray(plugin_entry,
  2146. ATTR_PLUGIN_EXCLUDE_TARGET_SUBTREE);
  2147. for (i=0; values && values[i]; i++)
  2148. {
  2149. if (plugin_set_subtree_config(&(config->plgc_excluded_target_subtrees), values[i]))
  2150. {
  2151. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2152. "from entry %s\n", values[i], ATTR_PLUGIN_EXCLUDE_TARGET_SUBTREE,
  2153. slapi_entry_get_dn_const(plugin_entry));
  2154. status = 1;
  2155. break;
  2156. }
  2157. }
  2158. slapi_ch_array_free(values);
  2159. values = slapi_entry_attr_get_charray(plugin_entry,
  2160. ATTR_PLUGIN_BIND_SUBTREE);
  2161. for (i=0; values && values[i]; i++)
  2162. {
  2163. if (plugin_set_subtree_config(&(config->plgc_bind_subtrees), values[i]))
  2164. {
  2165. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2166. "from entry %s\n", values[i], ATTR_PLUGIN_BIND_SUBTREE,
  2167. slapi_entry_get_dn_const(plugin_entry));
  2168. status = 1;
  2169. break;
  2170. }
  2171. else
  2172. {
  2173. bind_seen = PR_TRUE;
  2174. }
  2175. }
  2176. slapi_ch_array_free(values);
  2177. values = slapi_entry_attr_get_charray(plugin_entry,
  2178. ATTR_PLUGIN_EXCLUDE_BIND_SUBTREE);
  2179. for (i=0; values && values[i]; i++)
  2180. {
  2181. if (plugin_set_subtree_config(&(config->plgc_excluded_bind_subtrees), values[i]))
  2182. {
  2183. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  2184. "from entry %s\n", values[i], ATTR_PLUGIN_EXCLUDE_BIND_SUBTREE,
  2185. slapi_entry_get_dn_const(plugin_entry));
  2186. status = 1;
  2187. break;
  2188. }
  2189. }
  2190. slapi_ch_array_free(values);
  2191. /* set target subtree default - allow access to all data */
  2192. if (!target_seen)
  2193. {
  2194. plugin_set_global(&(config->plgc_target_subtrees));
  2195. }
  2196. /* set bind subtree default - allow access to local data only */
  2197. if (!bind_seen)
  2198. {
  2199. ptd_set_special_data(&(config->plgc_bind_subtrees), PLGC_DATA_LOCAL);
  2200. ptd_set_special_data(&(config->plgc_bind_subtrees), PLGC_DATA_REMOTE);
  2201. }
  2202. return status;
  2203. }
  2204. /* This function is called after the plugin init function has been called
  2205. which fills in the desc part of the plugin
  2206. */
  2207. static int
  2208. add_plugin_description(Slapi_Entry *e, const char *attrname, char *val)
  2209. {
  2210. struct berval desc;
  2211. struct berval *newval[2] = {0, 0};
  2212. int status = 0;
  2213. desc.bv_val = SLAPI_PLUGIN_NONE_IF_NULL( val );
  2214. desc.bv_len = strlen(desc.bv_val);
  2215. newval[0] = &desc;
  2216. if ((status = entry_replace_values(e, attrname, newval)) != 0)
  2217. {
  2218. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: failed to add value %s to "
  2219. "attribute %s of entry %s\n", val, attrname,
  2220. slapi_entry_get_dn_const(e));
  2221. status = 1;
  2222. }
  2223. return status;
  2224. }
  2225. /*
  2226. * The plugin initfunc sets some vendor and version information in the plugin.
  2227. * This function extracts that and adds it as attributes to `e'. If
  2228. * `plugin' is NULL, the plugin is located based on the DN in `e'.
  2229. *
  2230. * Returns 0 if all goes well and 1 if not.
  2231. */
  2232. int
  2233. plugin_add_descriptive_attributes( Slapi_Entry *e, struct slapdplugin *plugin )
  2234. {
  2235. int status = 0;
  2236. if ( NULL == plugin ) {
  2237. int i;
  2238. const Slapi_DN *ednp = slapi_entry_get_sdn_const( e );
  2239. Slapi_DN pdn;
  2240. struct slapdplugin *plugtmp;
  2241. for( i = 0; NULL == plugin && i < PLUGIN_LIST_GLOBAL_MAX; ++i )
  2242. {
  2243. for ( plugtmp = global_plugin_list[i]; NULL == plugin && plugtmp;
  2244. plugtmp = plugtmp->plg_next)
  2245. {
  2246. slapi_sdn_init_normdn_byref( &pdn, plugtmp->plg_dn );
  2247. if ( 0 == slapi_sdn_compare( &pdn, ednp ))
  2248. {
  2249. plugin = plugtmp;
  2250. }
  2251. slapi_sdn_done( &pdn );
  2252. }
  2253. }
  2254. if ( NULL == plugin )
  2255. {
  2256. /* This can happen for things such as disabled syntax plug-ins. We
  2257. * just treat this as a warning to allow the description attributes
  2258. * to be set to a default value to avoid an objectclass violation. */
  2259. LDAPDebug(LDAP_DEBUG_PLUGIN,
  2260. "Warning: couldn't find plugin %s in global list. "
  2261. "Adding default descriptive values.\n",
  2262. slapi_entry_get_dn_const(e), 0, 0 );
  2263. }
  2264. }
  2265. if (add_plugin_description(e, ATTR_PLUGIN_PLUGINID,
  2266. plugin ? plugin->plg_desc.spd_id : NULL))
  2267. {
  2268. status = 1;
  2269. }
  2270. if (add_plugin_description(e, ATTR_PLUGIN_VERSION,
  2271. plugin ? plugin->plg_desc.spd_version : NULL))
  2272. {
  2273. status = 1;
  2274. }
  2275. if (add_plugin_description(e, ATTR_PLUGIN_VENDOR,
  2276. plugin ? plugin->plg_desc.spd_vendor: NULL))
  2277. {
  2278. status = 1;
  2279. }
  2280. if (add_plugin_description(e, ATTR_PLUGIN_DESC,
  2281. plugin ? plugin->plg_desc.spd_description : NULL))
  2282. {
  2283. status = 1;
  2284. }
  2285. return status;
  2286. }
  2287. /*
  2288. clean up the memory associated with the plugin
  2289. */
  2290. static void
  2291. plugin_free(struct slapdplugin *plugin)
  2292. {
  2293. charray_free(plugin->plg_argv);
  2294. slapi_ch_free_string(&plugin->plg_libpath);
  2295. slapi_ch_free_string(&plugin->plg_initfunc);
  2296. slapi_ch_free_string(&plugin->plg_name);
  2297. slapi_ch_free_string(&plugin->plg_dn);
  2298. slapi_counter_destroy(&plugin->plg_op_counter);
  2299. if (!plugin->plg_group)
  2300. plugin_config_cleanup(&plugin->plg_conf);
  2301. slapi_ch_free((void**)&plugin);
  2302. }
  2303. /***********************************
  2304. This is the main entry point for plugin configuration. The plugin_entry argument
  2305. should already contain the necessary fields required to initialize the plugin and
  2306. to give it a proper name in the plugin configuration DIT.
  2307. Argument:
  2308. Slapi_Entry *plugin_entry - the required attributes are
  2309. dn: the dn of the plugin entry
  2310. cn: the unique name of the plugin
  2311. nsslapd-pluginType: one of the several recognized plugin types e.g. "postoperation"
  2312. if p_initfunc is given, pluginPath and pluginInitFunc are optional
  2313. nsslapd-pluginPath: full path and file name of the dll implementing the plugin
  2314. nsslapd-pluginInitFunc: the name of the plugin initialization function
  2315. the optional attributes are:
  2316. nsslapd-pluginArg0
  2317. ...
  2318. nsslapd-pluginArg[N-1] - the (old style) arguments to the plugin, where N varies
  2319. from 0 to the number of arguments. The numbers must be consecutive i.e. no
  2320. skipping
  2321. Instead of using nsslapd-pluginArgN, it is encouraged for you to use named
  2322. parameters e.g.
  2323. nsslapd-tweakThis: 1
  2324. nsslapd-tweakThat: 2
  2325. etc.
  2326. nsslapd-pluginEnabled: "on"|"off" - by default, the plugin will be enabled unless
  2327. this attribute is present and has the value "off"
  2328. for other known attributes, see set_plugin_config_from_entry() above
  2329. all other attributes will be ignored
  2330. The reason this parameter is not const is because it may be modified. This
  2331. function will modify it if the plugin init function is called successfully
  2332. to add the description attributes, and the plugin init function may modify
  2333. it as well.
  2334. Argument:
  2335. group - the group to which this plugin will belong - each member of a plugin group
  2336. shares the pluginconfig of the group leader; refer to the function plugin_get_config
  2337. for more information
  2338. Argument:
  2339. add_entry - if true, the entry will be added to the DIT using the given
  2340. DN in the plugin_entry - this is the default behavior; if false, the
  2341. plugin entry will not show up in the DIT
  2342. ************************************/
  2343. int
  2344. plugin_setup(Slapi_Entry *plugin_entry, struct slapi_componentid *group,
  2345. slapi_plugin_init_fnptr p_initfunc, int add_entry, char *returntext)
  2346. {
  2347. int ii = 0;
  2348. char attrname[SLAPD_TYPICAL_ATTRIBUTE_NAME_MAX_LENGTH];
  2349. char *value = NULL;
  2350. char **values = NULL;
  2351. struct slapdplugin *plugin = NULL;
  2352. struct slapdplugin **plugin_list = NULL;
  2353. struct slapi_componentid *cid = NULL;
  2354. const char *existname = 0;
  2355. slapi_plugin_init_fnptr initfunc = p_initfunc;
  2356. Slapi_PBlock pb;
  2357. int status = 0;
  2358. int enabled = 1;
  2359. char *configdir = 0;
  2360. int skipped;
  2361. attrname[0] = '\0';
  2362. if (!slapi_entry_get_sdn_const(plugin_entry))
  2363. {
  2364. LDAPDebug0Args(LDAP_DEBUG_ANY, "plugin_setup: DN is missing from the plugin.\n");
  2365. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"Plugin is missing dn.");
  2366. return -1;
  2367. }
  2368. if ((existname = plugin_exists(slapi_entry_get_sdn_const(plugin_entry))) != NULL)
  2369. {
  2370. LDAPDebug(LDAP_DEBUG_ANY, "plugin_setup: the plugin named %s already exists, "
  2371. "or is already setup.\n", existname, 0, 0);
  2372. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2373. "the plugin named %s already exists, or is already setup.", existname);
  2374. return -1;
  2375. }
  2376. /*
  2377. * create a new plugin structure, fill it in, and prepare to
  2378. * call the plugin's init function. the init function will
  2379. * set the plugin function pointers.
  2380. */
  2381. plugin = (struct slapdplugin *)slapi_ch_calloc(1, sizeof(struct slapdplugin));
  2382. plugin->plg_dn = slapi_ch_strdup(slapi_entry_get_ndn(plugin_entry));
  2383. plugin->plg_closed = 0;
  2384. if (!(value = slapi_entry_attr_get_charptr(plugin_entry,
  2385. ATTR_PLUGIN_TYPE)))
  2386. {
  2387. /* error: required attribute %s missing */
  2388. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing from entry \"%s\"\n",
  2389. ATTR_PLUGIN_TYPE, slapi_entry_get_dn_const(plugin_entry), 0);
  2390. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,"required attribute %s is missing from entry",
  2391. ATTR_PLUGIN_TYPE);
  2392. status = -1;
  2393. goto PLUGIN_CLEANUP;
  2394. }
  2395. else
  2396. {
  2397. status = plugin_get_type_and_list(value, &plugin->plg_type,
  2398. &plugin_list);
  2399. if ( status != 0 ) {
  2400. /* error: unknown plugin type */
  2401. LDAPDebug(LDAP_DEBUG_ANY, "Error: unknown plugin type \"%s\" in entry \"%s\"\n",
  2402. value, slapi_entry_get_dn_const(plugin_entry), 0);
  2403. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unknown plugin type \"%s\" in entry", value);
  2404. slapi_ch_free_string(&value);
  2405. status = -1;
  2406. goto PLUGIN_CLEANUP;
  2407. }
  2408. slapi_ch_free_string(&value);
  2409. }
  2410. if (!status &&
  2411. !(value = slapi_entry_attr_get_charptr(plugin_entry, "cn")))
  2412. {
  2413. /* error: required attribute %s missing */
  2414. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing from entry \"%s\"\n",
  2415. "cn", slapi_entry_get_dn_const(plugin_entry), 0);
  2416. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "required attribute \"cn\" is missing from entry");
  2417. status = -1;
  2418. goto PLUGIN_CLEANUP;
  2419. }
  2420. else
  2421. {
  2422. /* plg_name is normalized once here */
  2423. plugin->plg_name = slapi_create_rdn_value("%s", value);
  2424. slapi_ch_free((void**)&value);
  2425. }
  2426. if (!(value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_PRECEDENCE)))
  2427. {
  2428. /* A precedence isn't set, so just use the default. */
  2429. plugin->plg_precedence = PLUGIN_DEFAULT_PRECEDENCE;
  2430. }
  2431. else
  2432. {
  2433. /* A precedence was set, so let's make sure it's valid. */
  2434. int precedence = 0;
  2435. char *endptr = NULL;
  2436. /* Convert the value. */
  2437. precedence = strtol(value, &endptr, 10);
  2438. /* Make sure the precedence is within our valid
  2439. * range and that we had no conversion errors. */
  2440. if ((*value == '\0') || (*endptr != '\0') ||
  2441. (precedence < PLUGIN_MIN_PRECEDENCE) || (precedence > PLUGIN_MAX_PRECEDENCE))
  2442. {
  2443. LDAPDebug(LDAP_DEBUG_ANY, "Error: value for attribute %s must be "
  2444. "an integer between %d and %d\n", ATTR_PLUGIN_PRECEDENCE,
  2445. PLUGIN_MIN_PRECEDENCE, PLUGIN_MAX_PRECEDENCE);
  2446. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "value for attribute %s must be "
  2447. "an integer between %d and %d.", ATTR_PLUGIN_PRECEDENCE,
  2448. PLUGIN_MIN_PRECEDENCE, PLUGIN_MAX_PRECEDENCE);
  2449. status = -1;
  2450. slapi_ch_free((void**)&value);
  2451. goto PLUGIN_CLEANUP;
  2452. }
  2453. else
  2454. {
  2455. plugin->plg_precedence = precedence;
  2456. }
  2457. slapi_ch_free((void**)&value);
  2458. }
  2459. if (!(value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_INITFN)))
  2460. {
  2461. if (!initfunc)
  2462. {
  2463. /* error: required attribute %s missing */
  2464. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing from entry \"%s\"\n",
  2465. ATTR_PLUGIN_INITFN, slapi_entry_get_dn_const(plugin_entry), 0);
  2466. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"required attribute %s is missing from entry.",
  2467. ATTR_PLUGIN_INITFN);
  2468. status = -1;
  2469. goto PLUGIN_CLEANUP;
  2470. }
  2471. }
  2472. else
  2473. {
  2474. plugin->plg_initfunc = value; /* plugin owns value's memory now, don't free */
  2475. }
  2476. if (!initfunc)
  2477. {
  2478. PRBool loadNow = PR_FALSE;
  2479. PRBool loadGlobal = PR_FALSE;
  2480. if (!(value = slapi_entry_attr_get_charptr(plugin_entry,
  2481. ATTR_PLUGIN_PATH)))
  2482. {
  2483. /* error: required attribute %s missing */
  2484. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing from entry \"%s\"\n",
  2485. ATTR_PLUGIN_PATH, slapi_entry_get_dn_const(plugin_entry), 0);
  2486. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "required attribute %s is missing from entry.",
  2487. ATTR_PLUGIN_PATH);
  2488. status = -1;
  2489. goto PLUGIN_CLEANUP;
  2490. }
  2491. else
  2492. {
  2493. plugin->plg_libpath = value; /* plugin owns value's memory now, don't free */
  2494. }
  2495. loadNow = slapi_entry_attr_get_bool(plugin_entry, ATTR_PLUGIN_LOAD_NOW);
  2496. loadGlobal = slapi_entry_attr_get_bool(plugin_entry, ATTR_PLUGIN_LOAD_GLOBAL);
  2497. /*
  2498. * load the plugin's init function
  2499. */
  2500. if ((initfunc = (slapi_plugin_init_fnptr)sym_load_with_flags(plugin->plg_libpath,
  2501. plugin->plg_initfunc, plugin->plg_name, 1 /* report errors */,
  2502. loadNow, loadGlobal)) == NULL)
  2503. {
  2504. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"Failed to load plugin's init function.");
  2505. status = -1;
  2506. goto PLUGIN_CLEANUP;
  2507. }
  2508. #ifdef _WIN32
  2509. {
  2510. set_debug_level_fn_t fn;
  2511. /* for Win32 only, attempt to get its debug level init function */
  2512. if ((fn = (set_debug_level_fn_t)sym_load(plugin->plg_libpath,
  2513. "plugin_init_debug_level", plugin->plg_name,
  2514. 0 /* do not report errors */ )) != NULL) {
  2515. /* we hooked the function, so call it */
  2516. (*fn)(module_ldap_debug);
  2517. }
  2518. }
  2519. #endif
  2520. }
  2521. if (!status && group) /* uses group's config; see plugin_get_config */
  2522. {
  2523. struct slapi_componentid * cid = (struct slapi_componentid *) group;
  2524. plugin->plg_group = (struct slapdplugin *) cid->sci_plugin;
  2525. }
  2526. else if (!status) /* using own config */
  2527. {
  2528. plugin_config_init(&(plugin->plg_conf));
  2529. set_plugin_config_from_entry(plugin_entry, plugin);
  2530. }
  2531. /*
  2532. * If this is a registered plugin function, then set the plugin id so we can remove
  2533. * this plugin later if needed.
  2534. */
  2535. if(group){
  2536. plugin->plg_id = group->sci_component_name;
  2537. }
  2538. /* add the plugin arguments */
  2539. value = 0;
  2540. ii = 0;
  2541. PR_snprintf(attrname, sizeof(attrname), "%s%d", ATTR_PLUGIN_ARG, ii);
  2542. skipped = 0;
  2543. #define MAXSKIPPED 10 /* Max allowed missing args */
  2544. do {
  2545. /*
  2546. * nsslapd-pluginarg0: val0
  2547. * nsslapd-pluginarg0: val00
  2548. * nsslapd-pluginarg2: val2
  2549. * nsslapd-pluginarg5: val5
  2550. * ==>
  2551. * treated as
  2552. * nsslapd-pluginarg0: val0
  2553. * nsslapd-pluginarg1: val00
  2554. * nsslapd-pluginarg2: val2
  2555. * nsslapd-pluginarg3: val5
  2556. */
  2557. char **vp = values = slapi_entry_attr_get_charray(plugin_entry, attrname);
  2558. if (values) {
  2559. charray_add(&plugin->plg_argv, slapi_ch_strdup(*vp));
  2560. plugin->plg_argc++;
  2561. for (vp++; vp && *vp; vp++) {
  2562. charray_add(&plugin->plg_argv, slapi_ch_strdup(*vp));
  2563. plugin->plg_argc++;
  2564. }
  2565. charray_free(values);
  2566. } else {
  2567. skipped++;
  2568. }
  2569. PR_snprintf(attrname, sizeof(attrname), "%s%d", ATTR_PLUGIN_ARG, ++ii);
  2570. } while (skipped < MAXSKIPPED);
  2571. memset((char *)&pb, '\0', sizeof(pb));
  2572. slapi_pblock_set(&pb, SLAPI_PLUGIN, plugin);
  2573. slapi_pblock_set(&pb, SLAPI_PLUGIN_VERSION, (void *)SLAPI_PLUGIN_CURRENT_VERSION);
  2574. cid = generate_componentid (plugin,NULL);
  2575. slapi_pblock_set(&pb, SLAPI_PLUGIN_IDENTITY, (void*)cid);
  2576. configdir = config_get_configdir();
  2577. slapi_pblock_set(&pb, SLAPI_CONFIG_DIRECTORY, configdir);
  2578. /* see if the plugin is enabled or not */
  2579. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  2580. ATTR_PLUGIN_ENABLED)) &&
  2581. !strcasecmp(value, "off"))
  2582. {
  2583. enabled = 0;
  2584. }
  2585. else
  2586. {
  2587. enabled = 1;
  2588. }
  2589. slapi_pblock_set(&pb, SLAPI_PLUGIN_ENABLED, &enabled);
  2590. slapi_pblock_set(&pb, SLAPI_PLUGIN_CONFIG_ENTRY, plugin_entry);
  2591. plugin->plg_op_counter = slapi_counter_new();
  2592. if (enabled && (*initfunc)(&pb) != 0)
  2593. {
  2594. LDAPDebug(LDAP_DEBUG_ANY, "Init function \"%s\" for \"%s\" plugin in library \"%s\" failed\n",
  2595. plugin->plg_initfunc, plugin->plg_name, plugin->plg_libpath);
  2596. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"Init function \"%s\" for \"%s\" plugin in "
  2597. "library \"%s\" failed.",plugin->plg_initfunc, plugin->plg_name, plugin->plg_libpath);
  2598. status = -1;
  2599. /*
  2600. * The init function might have added the plugin to the global list before
  2601. * it failed - attempt to remove it just in case it was added.
  2602. */
  2603. plugin_remove_plugins(plugin, value);
  2604. slapi_ch_free((void**)&value);
  2605. goto PLUGIN_CLEANUP;
  2606. }
  2607. if ( !status ) {
  2608. status = plugin_add_descriptive_attributes( plugin_entry, plugin );
  2609. }
  2610. slapi_ch_free((void**)&value);
  2611. if(enabled)
  2612. {
  2613. /* don't use raw pointer from plugin_entry because it
  2614. will be freed later by the caller */
  2615. Slapi_DN *dn_copy = slapi_sdn_dup(slapi_entry_get_sdn_const(plugin_entry));
  2616. add_plugin_to_list(plugin_list, plugin);
  2617. add_plugin_entry_dn(dn_copy);
  2618. }
  2619. if (add_entry)
  2620. {
  2621. /* make a copy of the plugin entry for our own use because it will
  2622. be freed later by the caller */
  2623. Slapi_Entry *e_copy = slapi_entry_dup(plugin_entry);
  2624. /* new_plugin_entry(&plugin_entries, plugin_entry, plugin); */
  2625. new_plugin_entry(&dep_plugin_entries, e_copy, plugin);
  2626. }
  2627. PLUGIN_CLEANUP:
  2628. if (status)
  2629. plugin_free(plugin);
  2630. slapi_ch_free((void **)&configdir);
  2631. return status;
  2632. }
  2633. /*
  2634. * We added a plugin, do our setup and then start the plugin. This is the same as adding a plugin
  2635. * or enabling a disabled plugin.
  2636. */
  2637. int
  2638. plugin_add(Slapi_Entry *entry, char *returntext, int locked)
  2639. {
  2640. int rc = LDAP_SUCCESS;
  2641. if(!locked){
  2642. slapi_rwlock_wrlock(global_rwlock);
  2643. slapi_td_set_plugin_locked();
  2644. }
  2645. if((rc = plugin_setup(entry, 0, 0, 1, returntext)) != LDAP_SUCCESS){
  2646. LDAPDebug(LDAP_DEBUG_PLUGIN, "plugin_add: plugin_setup failed for (%s)\n",slapi_entry_get_dn(entry), rc, 0);
  2647. goto done;
  2648. }
  2649. if((rc = plugin_start(entry, returntext)) != LDAP_SUCCESS){
  2650. LDAPDebug(LDAP_DEBUG_PLUGIN, "plugin_add: plugin_start failed for (%s)\n",slapi_entry_get_dn(entry), rc, 0);
  2651. goto done;
  2652. }
  2653. done:
  2654. if(!locked){
  2655. slapi_rwlock_unlock(global_rwlock);
  2656. slapi_td_set_plugin_unlocked();
  2657. }
  2658. return rc;
  2659. }
  2660. static char *
  2661. get_dep_plugin_list(char **plugins)
  2662. {
  2663. char output[1024];
  2664. int first_plugin = 1;
  2665. int len = 0;
  2666. int i ;
  2667. for(i = 0; plugins && plugins[i]; i++){
  2668. if(first_plugin){
  2669. PL_strncpyz(output,plugins[i], sizeof(output) - 3);
  2670. len += strlen(plugins[i]);
  2671. first_plugin = 0;
  2672. } else {
  2673. PL_strcatn(output ,sizeof(output) -3, ", ");
  2674. PL_strcatn(output, sizeof(output) -3, plugins[i]);
  2675. len += strlen(plugins[i]);
  2676. }
  2677. if(len > (sizeof(output) - 3)){
  2678. /*
  2679. * We could not print all the plugins, show that we truncated
  2680. * the list by adding "..."
  2681. */
  2682. PL_strcatn(output ,sizeof(output) , "...");
  2683. }
  2684. }
  2685. return slapi_ch_strdup(output);
  2686. }
  2687. /*
  2688. * Make sure the removal of this plugin does not breaking any existing dependencies
  2689. */
  2690. static int
  2691. plugin_delete_check_dependency(struct slapdplugin *plugin_entry, int flag, char *returntext)
  2692. {
  2693. entry_and_plugin_t *ep = dep_plugin_entries;
  2694. plugin_dep_config *config = NULL;
  2695. struct slapdplugin *plugin = NULL;
  2696. Slapi_Entry *pentry = NULL;
  2697. char *plugin_name = plugin_entry->plg_name;
  2698. char *plugin_type = plugin_get_type_str(plugin_entry->plg_type);
  2699. char **dep_plugins = NULL;
  2700. char *value = NULL;
  2701. char **list = NULL;
  2702. int dep_type_count = 0;
  2703. int type_count = 0;
  2704. int total_plugins = 0;
  2705. int plugin_index = 0;
  2706. int index = 0;
  2707. int rc = LDAP_SUCCESS;
  2708. int i;
  2709. while(ep){
  2710. total_plugins++;
  2711. ep = ep->next;
  2712. }
  2713. /* allocate the config array */
  2714. config = (plugin_dep_config*)slapi_ch_calloc(total_plugins + 1, sizeof(plugin_dep_config));
  2715. ep = dep_plugin_entries;
  2716. /*
  2717. * Collect relevant config
  2718. */
  2719. while(ep){
  2720. plugin = ep->plugin;
  2721. if(plugin == 0){
  2722. goto next;
  2723. }
  2724. /*
  2725. * We are not concerned with disabled plugins
  2726. */
  2727. value = slapi_entry_attr_get_charptr(ep->e, ATTR_PLUGIN_ENABLED);
  2728. if(value){
  2729. if(strcasecmp(value, "off") == 0){
  2730. slapi_ch_free_string(&value);
  2731. goto next;
  2732. }
  2733. slapi_ch_free_string(&value);
  2734. } else {
  2735. goto next;
  2736. }
  2737. if(!ep->plugin){
  2738. goto next;
  2739. }
  2740. config[plugin_index].e = ep->e;
  2741. pentry = ep->e;
  2742. if(pentry){
  2743. config[plugin_index].plugin = plugin;
  2744. plugin_create_stringlist( pentry, "nsslapd-plugin-depends-on-named",
  2745. &(config[plugin_index].total_named), &(config[plugin_index].depends_named_list));
  2746. plugin_create_stringlist( pentry, "nsslapd-plugin-depends-on-type",
  2747. &(config[plugin_index].total_type), &(config[plugin_index].depends_type_list));
  2748. }
  2749. plugin_index++;
  2750. next:
  2751. ep = ep->next;
  2752. }
  2753. /*
  2754. * Start checking every plugin for dependency issues
  2755. */
  2756. for(index = 0; index < plugin_index; index++){
  2757. if((plugin = config[index].plugin)){
  2758. /*
  2759. * We can skip ourselves, and our registered plugins.
  2760. */
  2761. if( plugin_cmp_plugins(plugin, plugin_entry))
  2762. {
  2763. continue;
  2764. }
  2765. /*
  2766. * Check all the plugins to see if one is depending on this plugin(name)
  2767. */
  2768. if(flag == CHECK_ALL && config[index].depends_named_list){
  2769. list = config[index].depends_named_list;
  2770. for(i = 0; list && list[i]; i++){
  2771. if(strcasecmp(list[i], plugin_name) == 0){
  2772. /* We have a dependency, we can not disable this pluign */
  2773. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete_check_dependency: can not disable plugin(%s) due "
  2774. "to dependency name issues with plugin (%s)\n",
  2775. plugin_name, config[index].plugin->plg_name, 0);
  2776. rc = -1;
  2777. goto free_and_return;
  2778. }
  2779. }
  2780. }
  2781. /*
  2782. * Check all the plugins to see if one is depending on this plugin(type).
  2783. */
  2784. if(config[index].depends_type_list){
  2785. list = config[index].depends_type_list;
  2786. for(i = 0; list && list[i]; i++){
  2787. if(strcasecmp(list[i], plugin_type) == 0){
  2788. charray_add(&dep_plugins, slapi_ch_strdup(plugin->plg_name));
  2789. dep_type_count++;
  2790. break;
  2791. }
  2792. }
  2793. }
  2794. }
  2795. }
  2796. /*
  2797. * Now check the dependency type count.
  2798. */
  2799. if(dep_type_count > 0){
  2800. /*
  2801. * There are plugins that depend on this plugin type. Now, get a plugin count of this
  2802. * type of plugin. If we are the only plugin of this type, we can not disable it.
  2803. */
  2804. for(index = 0; index < plugin_index; index++){
  2805. if((plugin = config[index].plugin)){
  2806. /* Skip ourselves, and our registered plugins. */
  2807. if( plugin_cmp_plugins(plugin, plugin_entry))
  2808. {
  2809. continue;
  2810. }
  2811. if(plugin->plg_type == plugin_entry->plg_type){
  2812. /* there is at least one other plugin of this type, its ok to disable */
  2813. type_count = 1;
  2814. break;
  2815. }
  2816. }
  2817. }
  2818. if(type_count == 0){ /* this is the only plugin of this type - return an error */
  2819. char *plugins = get_dep_plugin_list(dep_plugins);
  2820. /*
  2821. * The plugin type was changed, but since other plugins currently have dependencies,
  2822. * we can not dynamically apply the change. This is will most likely cause issues
  2823. * at the next server startup.
  2824. */
  2825. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Plugin (%s) type (%s) is needed "
  2826. "by other plugins(%s), it can not be dynamically disabled/removed at this time.",
  2827. plugin_name, plugin_type, plugins);
  2828. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete_check_dependency: %s\n",
  2829. returntext, 0, 0);
  2830. slapi_ch_free_string(&plugins);
  2831. rc = -1;
  2832. }
  2833. }
  2834. free_and_return:
  2835. /*
  2836. * Free the config list
  2837. */
  2838. charray_free(dep_plugins);
  2839. if(config){
  2840. index = 0;
  2841. while( config[index].plugin){
  2842. charray_free(config[index].depends_type_list);
  2843. charray_free(config[index].depends_named_list);
  2844. index++;
  2845. }
  2846. slapi_ch_free((void**)&config);
  2847. }
  2848. return rc;
  2849. }
  2850. /*
  2851. * Mark the plugin in the shutdown list a removed
  2852. */
  2853. static void
  2854. plugin_remove_from_shutdown(struct slapdplugin *plugin_entry)
  2855. {
  2856. struct slapdplugin *plugin = NULL;
  2857. int index = 0;
  2858. for(;index < global_plugins_started; index++){
  2859. if((plugin = global_plugin_shutdown_order[index].plugin)){
  2860. if(global_plugin_shutdown_order[index].removed){
  2861. continue;
  2862. }
  2863. /* "plugin_entry" can be the main plugin for registered function */
  2864. if(plugin_cmp_plugins(plugin, plugin_entry))
  2865. {
  2866. /*
  2867. * We have our index, just mark it as removed. The global list gets rewritten
  2868. * the next time we add or enable a plugin.
  2869. */
  2870. global_plugin_shutdown_order[index].removed = 1;
  2871. return;
  2872. }
  2873. }
  2874. }
  2875. }
  2876. /*
  2877. * Free the plugins that have been set to be removed.
  2878. */
  2879. static void
  2880. plugin_cleanup_list()
  2881. {
  2882. struct slapdplugin *plugin = NULL;
  2883. entry_and_plugin_t *ep = dep_plugin_entries;
  2884. entry_and_plugin_t *ep_prev = NULL, *ep_next = NULL;
  2885. while(ep){
  2886. plugin = ep->plugin;
  2887. ep_next = ep->next;
  2888. if(plugin && plugin->plg_removed){
  2889. if(ep_prev){
  2890. ep_prev->next = ep->next;
  2891. } else {
  2892. dep_plugin_entries = ep->next;
  2893. }
  2894. slapi_entry_free(ep->e);
  2895. if(ep->plugin){
  2896. plugin_free(ep->plugin);
  2897. }
  2898. slapi_ch_free((void **)&ep);
  2899. } else {
  2900. ep_prev = ep;
  2901. }
  2902. ep = ep_next;
  2903. }
  2904. }
  2905. /*
  2906. * Look at all the plugins for any matches. Then mark the ones we need
  2907. * to delete. After checking all the plugins, then we free the ones that
  2908. * were marked to be removed.
  2909. */
  2910. static int
  2911. plugin_remove_plugins(struct slapdplugin *plugin_entry, char *plugin_type)
  2912. {
  2913. struct slapdplugin *plugin = NULL;
  2914. struct slapdplugin *plugin_next = NULL;
  2915. struct slapdplugin *plugin_prev = NULL;
  2916. int removed = PLUGIN_NOT_FOUND;
  2917. int type;
  2918. /* look everywhere for other plugin functions with the plugin id */
  2919. for(type = 0; type < PLUGIN_LIST_GLOBAL_MAX; type++){
  2920. plugin = global_plugin_list[type];
  2921. plugin_prev = NULL;
  2922. while(plugin){
  2923. plugin_next = plugin->plg_next;
  2924. /*
  2925. * Check for the two types of plugins:
  2926. * the main plugin, and its registered plugin functions.
  2927. */
  2928. if(plugin_cmp_plugins(plugin_entry, plugin)){
  2929. /*
  2930. * Call the close function, cleanup the hashtable & the global shutdown list
  2931. */
  2932. Slapi_PBlock pb;
  2933. pblock_init(&pb);
  2934. plugin_set_stopped(plugin);
  2935. if (slapi_counter_get_value(plugin->plg_op_counter) > 0){
  2936. /*
  2937. * Plugin is still busy, and we might be blocking it
  2938. * by holding global plugin lock so return for now.
  2939. */
  2940. return PLUGIN_BUSY;
  2941. }
  2942. plugin_call_one( plugin, SLAPI_PLUGIN_CLOSE_FN, &pb);
  2943. if(plugin_prev){
  2944. plugin_prev->plg_next = plugin_next;
  2945. } else {
  2946. global_plugin_list[type] = plugin_next;
  2947. }
  2948. /*
  2949. * Remove plugin the DN hashtable, update the shutdown list,
  2950. * and mark the plugin for deletion
  2951. */
  2952. plugin_remove_from_list(plugin->plg_dn);
  2953. plugin_remove_from_shutdown(plugin);
  2954. plugin->plg_removed = 1;
  2955. plugin->plg_started = 0;
  2956. removed = PLUGIN_REMOVED;
  2957. } else {
  2958. plugin_prev = plugin;
  2959. }
  2960. plugin = plugin_next;
  2961. }
  2962. }
  2963. if(removed){
  2964. /*
  2965. * Now free the marked plugins, we could not do this earlier because
  2966. * we also needed to check for plugins registered functions. As both
  2967. * plugin types share the same slapi_plugin entry.
  2968. */
  2969. plugin_cleanup_list();
  2970. }
  2971. return removed;
  2972. }
  2973. /*
  2974. * We are removing a plugin from the global list. This happens when we delete a plugin, or disable it.
  2975. */
  2976. int
  2977. plugin_delete(Slapi_Entry *plugin_entry, char *returntext, int locked)
  2978. {
  2979. struct slapdplugin **plugin_list = NULL;
  2980. struct slapdplugin *plugin = NULL;
  2981. const char *plugin_dn = slapi_entry_get_dn_const(plugin_entry);
  2982. char *value = NULL;
  2983. int removed = PLUGIN_BUSY;
  2984. int type = 0;
  2985. int rc = LDAP_SUCCESS;
  2986. /* Critical server plugins can not be disabled */
  2987. if(plugin_is_critical(plugin_entry)){
  2988. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete: plugin \"%s\" is critical to server operations, and can not be disabled\n",
  2989. slapi_entry_get_dn_const(plugin_entry), 0, 0);
  2990. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Plugin \"%s\" is critical to server operations, and can not "
  2991. "be disabled.\n",slapi_entry_get_dn_const(plugin_entry));
  2992. rc = -1;
  2993. goto done;
  2994. }
  2995. if (!(value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_TYPE))){
  2996. /* error: required attribute %s missing */
  2997. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete: required attribute %s is missing from entry \"%s\"\n",
  2998. ATTR_PLUGIN_TYPE, slapi_entry_get_dn_const(plugin_entry), 0);
  2999. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Plugin delete failed: required attribute %s "
  3000. "is missing from entry.",ATTR_PLUGIN_TYPE);
  3001. rc = -1;
  3002. goto done;
  3003. } else {
  3004. while(removed == PLUGIN_BUSY){
  3005. removed = PLUGIN_NOT_FOUND;
  3006. if(!locked){
  3007. slapi_rwlock_wrlock(global_rwlock);
  3008. slapi_td_set_plugin_locked();
  3009. }
  3010. rc = plugin_get_type_and_list(value, &type, &plugin_list);
  3011. if ( rc != 0 ) {
  3012. /* error: unknown plugin type */
  3013. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete: unknown plugin type \"%s\" in entry \"%s\"\n",
  3014. value, slapi_entry_get_dn_const(plugin_entry), 0);
  3015. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Plugin delete failed: unknown plugin type "
  3016. "\"%s\" in entry.",value);
  3017. rc = -1;
  3018. goto unlock;
  3019. }
  3020. /*
  3021. * Skip syntax/matching rule/database plugins - these can not be disabled as it
  3022. * could break existing schema. We allow the update to occur, but it will
  3023. * not take effect until the next server restart.
  3024. */
  3025. if(type == SLAPI_PLUGIN_SYNTAX || type == SLAPI_PLUGIN_MATCHINGRULE || type == SLAPI_PLUGIN_DATABASE){
  3026. removed = PLUGIN_REMOVED; /* avoids error check below */
  3027. goto unlock;
  3028. }
  3029. /*
  3030. * Now remove the plugin from the list and the hashtable
  3031. */
  3032. for(plugin = *plugin_list; plugin ; plugin = plugin->plg_next){
  3033. if(strcasecmp(plugin->plg_dn, plugin_dn) == 0){
  3034. /*
  3035. * Make sure there are no other plugins that depend on this one before removing it
  3036. */
  3037. if(plugin_delete_check_dependency(plugin, CHECK_ALL, returntext) != LDAP_SUCCESS){
  3038. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete: failed to disable/delete plugin (%s)\n",
  3039. plugin->plg_dn,0,0);
  3040. rc = -1;
  3041. break;
  3042. }
  3043. removed = plugin_remove_plugins(plugin, value);
  3044. break;
  3045. }
  3046. }
  3047. unlock:
  3048. if(!locked){
  3049. slapi_rwlock_unlock(global_rwlock);
  3050. slapi_td_set_plugin_unlocked();
  3051. }
  3052. }
  3053. }
  3054. done:
  3055. slapi_ch_free_string(&value);
  3056. if(!removed && rc == 0){
  3057. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,"Plugin delete failed: could not find plugin "
  3058. "in the global list.");
  3059. LDAPDebug(LDAP_DEBUG_ANY, "plugin_delete: did not find plugin(%s) in the global list.\n",
  3060. slapi_entry_get_dn_const(plugin_entry),0,0);
  3061. rc = -1;
  3062. }
  3063. return rc;
  3064. }
  3065. /* set default configuration parameters */
  3066. static void
  3067. plugin_config_init (struct pluginconfig *config)
  3068. {
  3069. PR_ASSERT (config);
  3070. ptd_init (&config->plgc_target_subtrees);
  3071. ptd_init (&config->plgc_excluded_target_subtrees);
  3072. ptd_init (&config->plgc_bind_subtrees);
  3073. ptd_init (&config->plgc_excluded_bind_subtrees);
  3074. config->plgc_schema_check = PLGC_ON;
  3075. config->plgc_invoke_for_replop = PLGC_ON;
  3076. /* currently, we leave it up to plugin, but don't actually tell plugins that they can choose.
  3077. We want changes to always be logged by regular plugins to avoid data inconsistency, but we
  3078. want to allow internal plugins like replication to make the decision.*/
  3079. config->plgc_log_change = PLGC_UPTOPLUGIN;
  3080. config->plgc_log_access = PLGC_OFF;
  3081. config->plgc_log_audit = PLGC_OFF;
  3082. }
  3083. static int
  3084. plugin_config_set_action (int *action, char *value)
  3085. {
  3086. PR_ASSERT (action);
  3087. PR_ASSERT (value);
  3088. if (strcasecmp (value, "on") == 0)
  3089. {
  3090. *action = PLGC_ON;
  3091. }
  3092. else if (strcasecmp (value, "off") == 0)
  3093. {
  3094. *action = PLGC_OFF;
  3095. }
  3096. else if (strcasecmp (value, "uptoplugin") == 0)
  3097. {
  3098. *action = PLGC_UPTOPLUGIN;
  3099. }
  3100. else
  3101. {
  3102. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  3103. "plugin_config_set_action: invalid action %s\n", value);
  3104. return -1;
  3105. }
  3106. return 0;
  3107. }
  3108. static void
  3109. plugin_config_cleanup (struct pluginconfig *config)
  3110. {
  3111. PR_ASSERT (config);
  3112. ptd_cleanup (&config->plgc_target_subtrees);
  3113. ptd_cleanup (&config->plgc_excluded_target_subtrees);
  3114. ptd_cleanup (&config->plgc_bind_subtrees);
  3115. ptd_cleanup (&config->plgc_excluded_bind_subtrees);
  3116. }
  3117. #if 0
  3118. static char*
  3119. plugin_config_action_to_string (int action)
  3120. {
  3121. switch (action)
  3122. {
  3123. case PLGC_ON: return "on";
  3124. case PLGC_OFF: return "off";
  3125. case PLGC_UPTOPLUGIN: return "uptoplugin";
  3126. default: return NULL;
  3127. }
  3128. }
  3129. #endif
  3130. static struct pluginconfig*
  3131. plugin_get_config (struct slapdplugin *plugin)
  3132. {
  3133. struct slapdplugin *temp = plugin;
  3134. PR_ASSERT (plugin);
  3135. while (temp->plg_group)
  3136. {
  3137. temp = temp->plg_group;
  3138. }
  3139. return &(temp->plg_conf);
  3140. }
  3141. static PRBool
  3142. plugin_invoke_plugin_pb (struct slapdplugin *plugin, int operation, Slapi_PBlock *pb)
  3143. {
  3144. Slapi_DN *target_spec;
  3145. PRBool rc;
  3146. PR_ASSERT (plugin);
  3147. PR_ASSERT (pb);
  3148. /* we always allow initialization and cleanup operations */
  3149. if (operation == SLAPI_PLUGIN_START_FN ||
  3150. operation == SLAPI_PLUGIN_POSTSTART_FN ||
  3151. operation == SLAPI_PLUGIN_CLOSE_FN ||
  3152. operation == SLAPI_PLUGIN_CLEANUP_FN ||
  3153. operation == SLAPI_PLUGIN_BE_PRE_CLOSE_FN ||
  3154. operation == SLAPI_PLUGIN_BE_POST_OPEN_FN ||
  3155. operation == SLAPI_PLUGIN_BE_PRE_BACKUP_FN ||
  3156. operation == SLAPI_PLUGIN_BE_POST_BACKUP_FN)
  3157. return PR_TRUE;
  3158. PR_ASSERT (pb->pb_op);
  3159. target_spec = operation_get_target_spec (pb->pb_op);
  3160. PR_ASSERT (target_spec);
  3161. rc = plugin_invoke_plugin_sdn (plugin, operation, pb, target_spec);
  3162. return rc;
  3163. }
  3164. PRBool
  3165. plugin_invoke_plugin_sdn (struct slapdplugin *plugin, int operation, Slapi_PBlock *pb, Slapi_DN *target_spec)
  3166. {
  3167. PluginTargetData *ptd;
  3168. PluginTargetData *excludedPtd;
  3169. struct pluginconfig *config;
  3170. Slapi_Backend *be;
  3171. int isroot;
  3172. PRBool islocal;
  3173. PRBool bindop;
  3174. unsigned long op;
  3175. ber_tag_t method = LBER_ERROR;
  3176. PR_ASSERT (plugin);
  3177. if (!pb) {
  3178. LDAPDebug(LDAP_DEBUG_ANY, "plugin_invoke_plugin_sdn: NULL pblock.\n", 0, 0, 0);
  3179. return PR_FALSE;
  3180. }
  3181. /* get configuration from the group plugin if necessary */
  3182. config = plugin_get_config (plugin);
  3183. slapi_pblock_get(pb, SLAPI_BIND_METHOD, &method);
  3184. /* check if plugin is configured to service replicated operations */
  3185. if (!config->plgc_invoke_for_replop)
  3186. {
  3187. int repl_op;
  3188. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  3189. if (repl_op)
  3190. return PR_FALSE;
  3191. }
  3192. if (pb->pb_op)
  3193. {
  3194. op = operation_get_type(pb->pb_op);
  3195. if (op == SLAPI_OPERATION_BIND || op == SLAPI_OPERATION_UNBIND)
  3196. {
  3197. bindop = PR_TRUE;
  3198. }
  3199. else
  3200. {
  3201. bindop = PR_FALSE;
  3202. }
  3203. slapi_pblock_get (pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  3204. }
  3205. else
  3206. {
  3207. bindop = PR_FALSE;
  3208. isroot = 1;
  3209. }
  3210. slapi_pblock_get (pb, SLAPI_BACKEND, &be);
  3211. /* determine whether data are local or remote */
  3212. /* remote if chaining backend or default backend */
  3213. if ( be!=NULL ) {
  3214. islocal=!(slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA));
  3215. } else {
  3216. islocal = be != defbackend_get_backend();
  3217. }
  3218. if (bindop)
  3219. {
  3220. ptd = &(config->plgc_bind_subtrees);
  3221. excludedPtd = &(config->plgc_excluded_bind_subtrees);
  3222. }
  3223. else
  3224. {
  3225. ptd = &(config->plgc_target_subtrees);
  3226. excludedPtd = &(config->plgc_excluded_target_subtrees);
  3227. }
  3228. if (plugin_matches_operation (target_spec, excludedPtd, bindop, isroot, islocal, method) == PR_TRUE) {
  3229. return PR_FALSE;
  3230. }
  3231. return plugin_matches_operation (target_spec, ptd, bindop, isroot, islocal, method);
  3232. }
  3233. /* this interface is exposed to be used by internal operations.
  3234. */
  3235. char* plugin_get_dn (const struct slapdplugin *plugin)
  3236. {
  3237. char *plugindn = NULL;
  3238. char *pattern = "cn=%s," PLUGIN_BASE_DN; /* cn=plugins,cn=config */
  3239. if (plugin == NULL) /* old plugin that does not pass identity - use default */
  3240. plugin = &global_default_plg;
  3241. if (plugin->plg_name == NULL)
  3242. return NULL;
  3243. /* plg_name is normalized in plugin_setup. So, we can use smprintf */
  3244. plugindn = slapi_ch_smprintf(pattern, plugin->plg_name);
  3245. if (NULL == plugindn) {
  3246. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  3247. "plugin_get_dn: failed to create plugin dn "
  3248. "(plugin name: %s)\n", plugin->plg_name);
  3249. return NULL;
  3250. }
  3251. return plugindn;
  3252. }
  3253. static PRBool plugin_is_global (const PluginTargetData *ptd)
  3254. {
  3255. /* plugin is considered to be global if it is invoked for
  3256. global data, local data and anonymous bind (bind target
  3257. data only). We don't include directory manager here
  3258. as it is considered to be part of local data */
  3259. return (ptd_is_special_data_set (ptd, PLGC_DATA_LOCAL) &&
  3260. ptd_is_special_data_set (ptd, PLGC_DATA_REMOTE) &&
  3261. ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ANONYMOUS) &&
  3262. ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ROOT));
  3263. }
  3264. static void plugin_set_global (PluginTargetData *ptd)
  3265. {
  3266. PR_ASSERT (ptd);
  3267. /* plugin is global if it is allowed access to all data */
  3268. ptd_set_special_data (ptd, PLGC_DATA_LOCAL);
  3269. ptd_set_special_data (ptd, PLGC_DATA_REMOTE);
  3270. ptd_set_special_data (ptd, PLGC_DATA_BIND_ANONYMOUS);
  3271. ptd_set_special_data (ptd, PLGC_DATA_BIND_ROOT);
  3272. }
  3273. static void plugin_set_default_access (struct pluginconfig *config)
  3274. {
  3275. /* by default, plugins are invoked if dn is local for bind operations,
  3276. and for all requests for all other operations */
  3277. PR_ASSERT (config);
  3278. plugin_set_global (&config->plgc_target_subtrees);
  3279. ptd_set_special_data (&config->plgc_bind_subtrees, PLGC_DATA_LOCAL);
  3280. ptd_set_special_data (&config->plgc_bind_subtrees, PLGC_DATA_REMOTE);
  3281. }
  3282. /* determine whether operation should be allowed based on plugin configuration */
  3283. PRBool plugin_allow_internal_op (Slapi_DN *target_spec, struct slapdplugin *plugin)
  3284. {
  3285. struct pluginconfig *config = plugin_get_config (plugin);
  3286. Slapi_Backend *be;
  3287. int islocal;
  3288. if (plugin_is_global (&config->plgc_excluded_target_subtrees))
  3289. return PR_FALSE;
  3290. if (plugin_is_global (&config->plgc_target_subtrees))
  3291. return PR_TRUE;
  3292. /* ONREPL - we do be_select to decide whether the request is for local
  3293. or remote data. We might need to reconsider how to do this
  3294. for performance reasons since be_select will be done again
  3295. once the operation goes through */
  3296. be = slapi_be_select(target_spec);
  3297. /* determine whether data are local or remote */
  3298. /* remote if chaining backend or default backend */
  3299. if ( be!=NULL ) {
  3300. islocal=!(slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA));
  3301. } else {
  3302. islocal = be != defbackend_get_backend();
  3303. }
  3304. /* SIMPLE auth method sends us through original code path in plugin_mathches_operation */
  3305. if (plugin_matches_operation (target_spec, &config->plgc_excluded_target_subtrees,
  3306. PR_FALSE, PR_FALSE, islocal, LDAP_AUTH_SIMPLE) == PR_TRUE) {
  3307. return PR_FALSE;
  3308. }
  3309. return plugin_matches_operation (target_spec, &config->plgc_target_subtrees,
  3310. PR_FALSE, PR_FALSE, islocal, LDAP_AUTH_SIMPLE);
  3311. }
  3312. static PRBool plugin_matches_operation (Slapi_DN *target_spec, PluginTargetData *ptd,
  3313. PRBool bindop, PRBool isroot, PRBool islocal, ber_tag_t method)
  3314. {
  3315. int cookie;
  3316. Slapi_DN *subtree;
  3317. /* check for special cases */
  3318. if (plugin_is_global (ptd))
  3319. return PR_TRUE;
  3320. /* if method is SASL we can have a null DN so bypass this check*/
  3321. if(method != LDAP_AUTH_SASL) {
  3322. if (bindop && target_spec && (slapi_sdn_get_dn (target_spec) == NULL ||
  3323. slapi_sdn_get_dn (target_spec)[0] == '\0'))
  3324. {
  3325. return (ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ANONYMOUS));
  3326. }
  3327. }
  3328. /* check for root bind */
  3329. if (bindop && isroot)
  3330. {
  3331. return (ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ROOT));
  3332. }
  3333. /* check for local data */
  3334. if (ptd_is_special_data_set (ptd, PLGC_DATA_LOCAL) && islocal)
  3335. {
  3336. return PR_TRUE;
  3337. }
  3338. /* check for remote data */
  3339. if (ptd_is_special_data_set (ptd, PLGC_DATA_REMOTE) && !islocal)
  3340. {
  3341. return (PR_TRUE);
  3342. }
  3343. subtree = ptd_get_first_subtree (ptd, &cookie);
  3344. while (subtree)
  3345. {
  3346. if (slapi_sdn_issuffix (target_spec, subtree))
  3347. return (PR_TRUE);
  3348. subtree = ptd_get_next_subtree (ptd, &cookie);
  3349. }
  3350. return PR_FALSE;
  3351. }
  3352. /* build operation action bitmap based on plugin configuration and actions specified for the operation */
  3353. int plugin_build_operation_action_bitmap (int input_actions, const struct slapdplugin *plugin)
  3354. {
  3355. int result_actions = 0;
  3356. /* old plugin that does not pass its identity to the operation */
  3357. if (plugin == NULL)
  3358. plugin = &global_default_plg;
  3359. if (plugin->plg_conf.plgc_log_access || config_get_plugin_logging())
  3360. result_actions |= OP_FLAG_ACTION_LOG_ACCESS;
  3361. if (plugin->plg_conf.plgc_log_audit)
  3362. result_actions |= OP_FLAG_ACTION_LOG_AUDIT;
  3363. /*
  3364. * OP_FLAG_ACTION_INVOKE_FOR_REPLOP is now used only by URP code.
  3365. * If someday this code needs to reclaim the flag, it has to use
  3366. * another flag to avoid the conflict with URP code.
  3367. *
  3368. * if (plugin->plg_conf.plgc_invoke_for_replop)
  3369. * result_actions |= OP_FLAG_ACTION_INVOKE_FOR_REPLOP;
  3370. */
  3371. switch (plugin->plg_conf.plgc_schema_check)
  3372. {
  3373. case PLGC_OFF: result_actions &= ~OP_FLAG_ACTION_SCHEMA_CHECK;
  3374. break;
  3375. case PLGC_ON: result_actions |= OP_FLAG_ACTION_SCHEMA_CHECK;
  3376. break;
  3377. case PLGC_UPTOPLUGIN: break;
  3378. default: PR_ASSERT (PR_FALSE);
  3379. }
  3380. switch (plugin->plg_conf.plgc_log_change)
  3381. {
  3382. case PLGC_OFF: result_actions &= ~OP_FLAG_ACTION_LOG_CHANGES;
  3383. break;
  3384. case PLGC_ON: result_actions |= OP_FLAG_ACTION_LOG_CHANGES;
  3385. break;
  3386. case PLGC_UPTOPLUGIN: break;
  3387. default: PR_ASSERT (PR_FALSE);
  3388. }
  3389. return result_actions;
  3390. }
  3391. const struct slapdplugin*
  3392. plugin_get_server_plg()
  3393. {
  3394. if(!global_server_plg_initialised)
  3395. {
  3396. global_server_plg.plg_name = "server";
  3397. plugin_set_global (&global_server_plg.plg_conf.plgc_target_subtrees);
  3398. global_server_plg.plg_conf.plgc_log_access = 1;
  3399. global_server_plg.plg_conf.plgc_log_audit = 1;
  3400. global_server_plg.plg_conf.plgc_schema_check = 1;
  3401. global_server_plg.plg_conf.plgc_log_change = 1;
  3402. global_server_plg_initialised= 1;
  3403. global_server_plg_initialised= 1;
  3404. }
  3405. return &global_server_plg;
  3406. }
  3407. struct slapi_componentid * plugin_get_default_component_id() {
  3408. if(!global_server_plg_id_initialised) {
  3409. global_server_id_plg.sci_plugin=plugin_get_server_plg();
  3410. global_server_id_plg.sci_component_name=
  3411. plugin_get_dn(global_server_id_plg.sci_plugin);
  3412. global_server_plg_id_initialised=1;
  3413. }
  3414. return &global_server_id_plg;
  3415. }
  3416. static void
  3417. default_plugin_init()
  3418. {
  3419. global_default_plg.plg_name = "old plugin";
  3420. plugin_config_init (&global_default_plg.plg_conf);
  3421. plugin_set_default_access (&global_default_plg.plg_conf);
  3422. }
  3423. #if 0
  3424. static void trace_plugin_invocation (Slapi_DN *target_spec, PluginTargetData *ptd,
  3425. PRBool bindop, PRBool isroot, PRBool islocal, int invoked)
  3426. {
  3427. int cookie, i = 0;
  3428. Slapi_DN *sdn;
  3429. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  3430. "Invocation parameters: target_spec = %s, bindop = %d, isroot=%d, islocal=%d\n"
  3431. "Plugin configuration: local_data=%d, remote_data=%d, anonymous_bind=%d, root_bind=%d\n",
  3432. slapi_sdn_get_ndn (target_spec), bindop, isroot, islocal, ptd->special_data[0],
  3433. ptd->special_data[1], ptd->special_data[2], ptd->special_data[3]);
  3434. sdn = ptd_get_first_subtree (ptd, &cookie);
  3435. while (sdn)
  3436. {
  3437. slapi_log_error (SLAPI_LOG_FATAL, NULL, "target_subtree%d: %s\n", i, slapi_sdn_get_ndn (sdn));
  3438. sdn = ptd_get_next_subtree (ptd, &cookie);
  3439. }
  3440. slapi_log_error (SLAPI_LOG_FATAL, NULL, invoked ? "Plugin is invoked\n" : "Plugin is not invoked\n");
  3441. }
  3442. #endif
  3443. /* functions to manipulate PluginTargetData type */
  3444. static void ptd_init (PluginTargetData *ptd)
  3445. {
  3446. PR_ASSERT (ptd);
  3447. dl_init (&ptd->subtrees, 0 /* initial count */);
  3448. memset (&ptd->special_data, 0, sizeof (ptd->special_data));
  3449. }
  3450. static void ptd_cleanup (PluginTargetData *ptd)
  3451. {
  3452. PR_ASSERT (ptd);
  3453. dl_cleanup (&ptd->subtrees, (FREEFN)slapi_sdn_free);
  3454. memset (&ptd->special_data, 0, sizeof (ptd->special_data));
  3455. }
  3456. static void ptd_add_subtree (PluginTargetData *ptd, Slapi_DN *subtree)
  3457. {
  3458. PR_ASSERT (ptd);
  3459. PR_ASSERT (subtree);
  3460. dl_add (&ptd->subtrees, subtree);
  3461. }
  3462. static void ptd_set_special_data (PluginTargetData *ptd, int type)
  3463. {
  3464. PR_ASSERT (ptd);
  3465. PR_ASSERT (type >= 0 && type < PLGC_DATA_MAX);
  3466. ptd->special_data [type] = PR_TRUE;
  3467. }
  3468. #if 0
  3469. static void ptd_clear_special_data (PluginTargetData *ptd, int type)
  3470. {
  3471. PR_ASSERT (ptd);
  3472. PR_ASSERT (type >= 0 && type < PLGC_DATA_MAX);
  3473. ptd->special_data [type] = PR_FALSE;
  3474. }
  3475. #endif
  3476. static Slapi_DN *ptd_get_first_subtree (const PluginTargetData *ptd, int *cookie)
  3477. {
  3478. PR_ASSERT (ptd);
  3479. return dl_get_first (&ptd->subtrees, cookie);
  3480. }
  3481. static Slapi_DN *ptd_get_next_subtree (const PluginTargetData *ptd, int *cookie)
  3482. {
  3483. PR_ASSERT (ptd);
  3484. return dl_get_next (&ptd->subtrees, cookie);
  3485. }
  3486. static PRBool ptd_is_special_data_set (const PluginTargetData *ptd, int type)
  3487. {
  3488. PR_ASSERT (ptd);
  3489. PR_ASSERT (type >= 0 && type < PLGC_DATA_MAX);
  3490. return ptd->special_data [type];
  3491. }
  3492. #if 0
  3493. static Slapi_DN* ptd_delete_subtree (PluginTargetData *ptd, Slapi_DN *subtree)
  3494. {
  3495. PR_ASSERT (ptd);
  3496. PR_ASSERT (subtree);
  3497. return (Slapi_DN*)dl_delete (&ptd->subtrees, subtree, (CMPFN)slapi_sdn_compare, NULL);
  3498. }
  3499. #endif
  3500. int ptd_get_subtree_count (const PluginTargetData *ptd)
  3501. {
  3502. PR_ASSERT (ptd);
  3503. return dl_get_count (&ptd->subtrees);
  3504. }
  3505. /* needed by command-line tasks to find an instance's plugin */
  3506. struct slapdplugin *plugin_get_by_name(char *name)
  3507. {
  3508. int x;
  3509. struct slapdplugin *plugin;
  3510. for(x = 0; x < PLUGIN_LIST_GLOBAL_MAX; x++) {
  3511. for(plugin = global_plugin_list[x]; plugin; plugin = plugin->plg_next) {
  3512. if (!strcmp(name, plugin->plg_name)) {
  3513. return plugin;
  3514. }
  3515. }
  3516. }
  3517. return NULL;
  3518. }
  3519. struct slapi_componentid *
  3520. generate_componentid ( struct slapdplugin * pp , char * name )
  3521. {
  3522. struct slapi_componentid * idp;
  3523. idp = (struct slapi_componentid *) slapi_ch_calloc(1, sizeof( *idp ));
  3524. if ( pp )
  3525. idp->sci_plugin=pp;
  3526. else
  3527. idp->sci_plugin=(struct slapdplugin *) plugin_get_server_plg();
  3528. if ( name )
  3529. idp->sci_component_name = slapi_ch_strdup(name);
  3530. else
  3531. /* Use plugin dn */
  3532. idp->sci_component_name = plugin_get_dn( idp->sci_plugin );
  3533. if (idp->sci_component_name)
  3534. slapi_dn_normalize(idp->sci_component_name);
  3535. return idp;
  3536. }
  3537. void release_componentid ( struct slapi_componentid * id )
  3538. {
  3539. if ( id ) {
  3540. if ( id->sci_component_name ) {
  3541. slapi_ch_free((void **)&id->sci_component_name);
  3542. id->sci_component_name=NULL;
  3543. }
  3544. slapi_ch_free((void **)&id);
  3545. }
  3546. }
  3547. /* used in main.c if -V flag is given */
  3548. static void slapd_print_plugin_version (
  3549. struct slapdplugin *plg,
  3550. struct slapdplugin *prev
  3551. )
  3552. {
  3553. if (plg == NULL || plg->plg_libpath == NULL) return;
  3554. /* same library as previous - don't print twice */
  3555. if (prev != NULL && prev->plg_libpath != NULL) {
  3556. if (strcmp(prev->plg_libpath,plg->plg_libpath) == 0) {
  3557. return;
  3558. }
  3559. }
  3560. printf("%s: %s\n",
  3561. plg->plg_libpath,
  3562. plg->plg_desc.spd_version ? plg->plg_desc.spd_version : "");
  3563. }
  3564. static void slapd_print_pluginlist_versions(struct slapdplugin *plg)
  3565. {
  3566. struct slapdplugin *p,*prev = NULL;
  3567. for (p = plg; p != NULL; p = p->plg_next) {
  3568. slapd_print_plugin_version(p,prev);
  3569. prev = p;
  3570. }
  3571. }
  3572. void plugin_print_versions(void)
  3573. {
  3574. int i;
  3575. for (i = 0; i < PLUGIN_LIST_GLOBAL_MAX; i++) {
  3576. slapd_print_pluginlist_versions(get_plugin_list(i));
  3577. }
  3578. }
  3579. /*
  3580. * Prints a list of plugins in execution order for each
  3581. * plug-in type. This will only be printed at the
  3582. * SLAPI_LOG_PLUGIN log level.
  3583. */
  3584. void plugin_print_lists(void)
  3585. {
  3586. int i;
  3587. struct slapdplugin *list = NULL;
  3588. struct slapdplugin *tmp = NULL;
  3589. for (i = 0; i < PLUGIN_LIST_GLOBAL_MAX; i++) {
  3590. if ((list = get_plugin_list(i)))
  3591. {
  3592. slapi_log_error(SLAPI_LOG_PLUGIN, NULL,
  3593. "---- Plugin List (type %d) ----\n", i);
  3594. for ( tmp = list; tmp; tmp = tmp->plg_next )
  3595. {
  3596. slapi_log_error(SLAPI_LOG_PLUGIN, NULL, " %s (precedence: %d)\n",
  3597. tmp->plg_name, tmp->plg_precedence);
  3598. }
  3599. }
  3600. }
  3601. }
  3602. /*
  3603. * check the spedified plugin entry and its nssladp-pluginEnabled value
  3604. * Return Value: 1 if the plugin is on.
  3605. * : 0 otherwise.
  3606. */
  3607. int
  3608. plugin_enabled(const char *plugin_name, void *identity)
  3609. {
  3610. Slapi_PBlock *search_pb = NULL;
  3611. Slapi_Entry **entries = NULL, **ep = NULL;
  3612. Slapi_Value *on_off = slapi_value_new_string("on");
  3613. char *filter = NULL;
  3614. int rc = 0; /* disabled, by default */
  3615. filter = slapi_filter_sprintf("cn=%s%s", ESC_NEXT_VAL, plugin_name);
  3616. search_pb = slapi_pblock_new();
  3617. slapi_search_internal_set_pb(search_pb, PLUGIN_BASE_DN, LDAP_SCOPE_ONELEVEL,
  3618. filter, NULL, 0, NULL, NULL, identity, 0);
  3619. slapi_search_internal_pb(search_pb);
  3620. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3621. if (LDAP_SUCCESS != rc) { /* plugin is not available */
  3622. rc = 0; /* disabled, by default */
  3623. goto bail;
  3624. }
  3625. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3626. for (ep = entries; ep && *ep; ep++) {
  3627. if (slapi_entry_attr_has_syntax_value(*ep, "nsslapd-pluginEnabled", on_off)) {
  3628. rc = 1; /* plugin is on */
  3629. goto bail;
  3630. }
  3631. }
  3632. bail:
  3633. slapi_value_free(&on_off);
  3634. slapi_free_search_results_internal(search_pb);
  3635. slapi_pblock_destroy(search_pb);
  3636. slapi_ch_free_string(&filter);
  3637. return rc;
  3638. }
  3639. /*
  3640. * Set given "type: attr" to the plugin default config entry
  3641. * (cn=plugin default config,cn=config) unless the same "type: attr" pair
  3642. * already exists in the entry.
  3643. */
  3644. int
  3645. slapi_set_plugin_default_config(const char *type, Slapi_Value *value)
  3646. {
  3647. Slapi_PBlock pb;
  3648. Slapi_Entry **entries = NULL;
  3649. int rc = LDAP_SUCCESS;
  3650. char **search_attrs = NULL; /* used by search */
  3651. if (NULL == type || '\0' == *type || NULL == value ) { /* nothing to do */
  3652. return rc;
  3653. }
  3654. charray_add(&search_attrs, slapi_ch_strdup(type));
  3655. /* cn=plugin default config,cn=config */
  3656. pblock_init(&pb);
  3657. slapi_search_internal_set_pb(&pb,
  3658. SLAPI_PLUGIN_DEFAULT_CONFIG, /* Base DN (normalized) */
  3659. LDAP_SCOPE_BASE,
  3660. "(objectclass=*)",
  3661. search_attrs, /* Attrs */
  3662. 0, /* AttrOnly */
  3663. NULL, /* Controls */
  3664. NULL, /* UniqueID */
  3665. (void *)plugin_get_default_component_id(),
  3666. 0);
  3667. slapi_search_internal_pb(&pb);
  3668. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3669. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3670. if (LDAP_SUCCESS == rc && entries && *entries) {
  3671. /* plugin default config entry exists */
  3672. int exists = 0;
  3673. Slapi_Attr *attr = NULL;
  3674. rc = slapi_entry_attr_find(*entries, type, &attr);
  3675. if (0 == rc) { /* type exists in the entry */
  3676. if (0 ==
  3677. slapi_attr_value_find(attr, slapi_value_get_berval(value))) {
  3678. /* value exists in the entry; we don't have to do anything. */
  3679. exists = 1;
  3680. }
  3681. }
  3682. slapi_free_search_results_internal(&pb);
  3683. pblock_done(&pb);
  3684. if (!exists) {
  3685. /* The argument attr is not in the plugin default config.
  3686. * Let's add it. */
  3687. Slapi_Mods smods;
  3688. Slapi_Value *va[2];
  3689. va[0] = value;
  3690. va[1] = NULL;
  3691. slapi_mods_init(&smods, 1);
  3692. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, type, va);
  3693. pblock_init(&pb);
  3694. slapi_modify_internal_set_pb(&pb, SLAPI_PLUGIN_DEFAULT_CONFIG,
  3695. slapi_mods_get_ldapmods_byref(&smods),
  3696. NULL, NULL, /* UniqueID */
  3697. (void *)plugin_get_default_component_id(),
  3698. 0 /* Flags */ );
  3699. slapi_modify_internal_pb(&pb);
  3700. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3701. slapi_mods_done(&smods);
  3702. pblock_done(&pb);
  3703. }
  3704. } else { /* cn=plugin default config does not exist. Let's add it. */
  3705. Slapi_Mods smods;
  3706. Slapi_Value *va[2];
  3707. slapi_free_search_results_internal(&pb);
  3708. pblock_done(&pb);
  3709. va[0] = value;
  3710. va[1] = NULL;
  3711. slapi_mods_init(&smods, 1);
  3712. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectClass", "top");
  3713. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectClass",
  3714. "extensibleObject");
  3715. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, type, va);
  3716. pblock_init(&pb);
  3717. slapi_add_internal_set_pb(&pb, SLAPI_PLUGIN_DEFAULT_CONFIG,
  3718. slapi_mods_get_ldapmods_byref(&smods), NULL,
  3719. (void *)plugin_get_default_component_id(),
  3720. 0 /* Flags */ );
  3721. slapi_add_internal_pb(&pb);
  3722. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3723. slapi_mods_done(&smods);
  3724. pblock_done(&pb);
  3725. }
  3726. charray_free(search_attrs);
  3727. return rc;
  3728. }
  3729. /*
  3730. * Get attribute values of given type from the plugin default config entry
  3731. * (cn=plugin default config,cn=config).
  3732. *
  3733. * Caller is responsible to free attrs by slapi_valueset_free.
  3734. */
  3735. int
  3736. slapi_get_plugin_default_config(char *type, Slapi_ValueSet **valueset)
  3737. {
  3738. Slapi_PBlock pb;
  3739. Slapi_Entry **entries = NULL;
  3740. int rc = LDAP_PARAM_ERROR;
  3741. char **search_attrs = NULL; /* used by search */
  3742. if (NULL == type || '\0' == *type || NULL == valueset) { /* nothing to do */
  3743. return rc;
  3744. }
  3745. charray_add(&search_attrs, slapi_ch_strdup(type));
  3746. /* cn=plugin default config,cn=config */
  3747. pblock_init(&pb);
  3748. slapi_search_internal_set_pb(&pb,
  3749. SLAPI_PLUGIN_DEFAULT_CONFIG, /* Base DN (normalized) */
  3750. LDAP_SCOPE_BASE,
  3751. "(objectclass=*)",
  3752. search_attrs, /* Attrs */
  3753. 0, /* AttrOnly */
  3754. NULL, /* Controls */
  3755. NULL, /* UniqueID */
  3756. (void *)plugin_get_default_component_id(),
  3757. 0);
  3758. slapi_search_internal_pb(&pb);
  3759. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3760. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3761. if (LDAP_SUCCESS == rc && entries && *entries) {
  3762. /* default config entry exists */
  3763. /* retrieve attribute values from the entry */
  3764. Slapi_Attr *attr = NULL;
  3765. rc = slapi_entry_attr_find(*entries, type, &attr);
  3766. if (0 == rc) { /* type value exists */
  3767. rc = slapi_attr_get_valueset(attr, valueset);
  3768. } else {
  3769. rc = LDAP_NO_SUCH_ATTRIBUTE;
  3770. }
  3771. }
  3772. slapi_free_search_results_internal(&pb);
  3773. pblock_done(&pb);
  3774. charray_free(search_attrs);
  3775. return rc;
  3776. }
  3777. void
  3778. slapi_set_plugin_open_rootdn_bind(Slapi_PBlock *pb){
  3779. struct pluginconfig *config = &pb->pb_plugin->plg_conf;
  3780. ptd_set_special_data(&(config->plgc_bind_subtrees), PLGC_DATA_BIND_ROOT);
  3781. }
  3782. PRBool
  3783. slapi_disordely_shutdown(PRBool set)
  3784. {
  3785. static PRBool is_disordely_shutdown = PR_FALSE;
  3786. if (set) {
  3787. is_disordely_shutdown = PR_TRUE;
  3788. }
  3789. return (is_disordely_shutdown);
  3790. }
  3791. /*
  3792. * Plugin operation counters
  3793. *
  3794. * Since most plugins can now be stopped and started dynamically we need
  3795. * to take special care when calling a close function. Since many plugins
  3796. * use global locks and data structures, these can not be freed/destroyed
  3797. * while there are active operations using them.
  3798. */
  3799. void
  3800. slapi_plugin_op_started(void *p)
  3801. {
  3802. struct slapdplugin *plugin = (struct slapdplugin *)p;
  3803. if(plugin){
  3804. slapi_counter_increment(plugin->plg_op_counter);
  3805. }
  3806. }
  3807. void
  3808. slapi_plugin_op_finished(void *p)
  3809. {
  3810. struct slapdplugin *plugin = (struct slapdplugin *)p;
  3811. if(plugin){
  3812. slapi_counter_decrement(plugin->plg_op_counter);
  3813. }
  3814. }
  3815. /*
  3816. * Waits for the operation counter to hit zero
  3817. */
  3818. void
  3819. plugin_op_all_finished(struct slapdplugin *p)
  3820. {
  3821. while(p && slapi_counter_get_value(p->plg_op_counter) > 0){
  3822. DS_Sleep(PR_MillisecondsToInterval(100));
  3823. }
  3824. }
  3825. void
  3826. plugin_set_started(struct slapdplugin *p)
  3827. {
  3828. p->plg_started = 1;
  3829. p->plg_stopped = 0;
  3830. }
  3831. void
  3832. plugin_set_stopped(struct slapdplugin *p)
  3833. {
  3834. /*
  3835. * We do not set "plg_stopped" here, because that is only used
  3836. * once the plugin has called its CLOSE function. Setting
  3837. * "plg_started" to 0 will prevent new operations from calling
  3838. * the plugin.
  3839. */
  3840. p->plg_started = 0;
  3841. }
  3842. int
  3843. slapi_plugin_running(Slapi_PBlock *pb)
  3844. {
  3845. int rc = 0;
  3846. if(pb->pb_plugin){
  3847. rc = pb->pb_plugin->plg_started;
  3848. }
  3849. return rc;
  3850. }
  3851. /*
  3852. * Allow "database" plugins to call the backend/backend txn plugins.
  3853. */
  3854. int
  3855. slapi_plugin_call_preop_be_plugins(Slapi_PBlock *pb, int function)
  3856. {
  3857. int be_func, betxn_func;
  3858. int rc = 0;
  3859. switch(function){
  3860. case SLAPI_PLUGIN_ADD_OP:
  3861. be_func = SLAPI_PLUGIN_BE_PRE_ADD_FN;
  3862. betxn_func = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  3863. break;
  3864. case SLAPI_PLUGIN_MOD_OP:
  3865. be_func = SLAPI_PLUGIN_BE_PRE_MODIFY_FN;
  3866. betxn_func = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  3867. break;
  3868. case SLAPI_PLUGIN_MODRDN_OP:
  3869. be_func = SLAPI_PLUGIN_BE_PRE_MODRDN_FN;
  3870. betxn_func = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  3871. break;
  3872. case SLAPI_PLUGIN_DEL_OP:
  3873. be_func = SLAPI_PLUGIN_BE_PRE_DELETE_FN;
  3874. betxn_func = SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN;
  3875. break;
  3876. default:
  3877. /* invalid function */
  3878. slapi_log_error(SLAPI_LOG_FATAL, "slapi_plugin_call_preop_betxn_plugins",
  3879. "Invalid function specified - backend plugins will not be called.\n");
  3880. return 0;
  3881. }
  3882. /*
  3883. * Call the be preop plugins.
  3884. */
  3885. plugin_call_plugins(pb, be_func);
  3886. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &rc);
  3887. /*
  3888. * Call the betxn preop plugins.
  3889. */
  3890. if (rc == LDAP_SUCCESS) {
  3891. plugin_call_plugins(pb, betxn_func);
  3892. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &rc);
  3893. }
  3894. return rc;
  3895. }
  3896. int
  3897. slapi_plugin_call_postop_be_plugins(Slapi_PBlock *pb, int function)
  3898. {
  3899. int be_func, betxn_func;
  3900. int rc = 0;
  3901. switch(function){
  3902. case SLAPI_PLUGIN_ADD_OP:
  3903. be_func = SLAPI_PLUGIN_BE_POST_ADD_FN;
  3904. betxn_func = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  3905. break;
  3906. case SLAPI_PLUGIN_MOD_OP:
  3907. be_func = SLAPI_PLUGIN_BE_POST_MODIFY_FN;
  3908. betxn_func = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  3909. break;
  3910. case SLAPI_PLUGIN_MODRDN_OP:
  3911. be_func = SLAPI_PLUGIN_BE_POST_MODRDN_FN;
  3912. betxn_func = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  3913. break;
  3914. case SLAPI_PLUGIN_DEL_OP:
  3915. be_func = SLAPI_PLUGIN_BE_POST_DELETE_FN;
  3916. betxn_func = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  3917. break;
  3918. default:
  3919. /* invalid function */
  3920. slapi_log_error(SLAPI_LOG_FATAL, "slapi_plugin_call_postop_betxn_plugins",
  3921. "Invalid function specified - backend plugins will not be called.\n");
  3922. return 0;
  3923. }
  3924. /* next, give the be txn plugins a crack at it */;
  3925. plugin_call_plugins(pb, betxn_func);
  3926. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &rc);
  3927. /* finally, give the be plugins a crack at it */
  3928. plugin_call_plugins(pb, be_func);
  3929. if (rc == LDAP_SUCCESS) {
  3930. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &rc);
  3931. }
  3932. return rc;
  3933. }