plugin.c 130 KB

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