plugin.c 133 KB

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