plugin.c 130 KB

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