plugin.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* plugin.c - routines for setting up and calling plugins */
  42. #include <stddef.h>
  43. #include <stdio.h>
  44. #include <plhash.h>
  45. #include "slap.h"
  46. /* this defines are used for plugin configuration */
  47. #define LOCAL_DATA "local data"
  48. #define REMOTE_DATA "remote data"
  49. #define ALL_DATA "*"
  50. #define ROOT_BIND "directory manager"
  51. #define ANONYMOUS_BIND "anonymous"
  52. /* Forward Declarations */
  53. static int plugin_call_list (struct slapdplugin *list, int operation, Slapi_PBlock *pb);
  54. static int plugin_call_one (struct slapdplugin *list, int operation, Slapi_PBlock *pb);
  55. static int plugin_call_func (struct slapdplugin *list, int operation, Slapi_PBlock *pb, int call_one);
  56. static PRBool plugin_invoke_plugin_pb (struct slapdplugin *plugin, int operation, Slapi_PBlock *pb);
  57. static PRBool plugin_matches_operation (Slapi_DN *target_spec, PluginTargetData *ptd,
  58. PRBool bindop, PRBool isroot, PRBool islocal, int method);
  59. static void plugin_config_init (struct pluginconfig *config);
  60. static void plugin_config_cleanup (struct pluginconfig *config);
  61. static int plugin_config_set_action (int *action, char *value);
  62. static struct pluginconfig* plugin_get_config (struct slapdplugin *plugin);
  63. static void default_plugin_init();
  64. static void ptd_init (PluginTargetData *ptd);
  65. static void ptd_cleanup (PluginTargetData *ptd);
  66. static void ptd_add_subtree (PluginTargetData *ptd, Slapi_DN *subtree);
  67. static void ptd_set_special_data (PluginTargetData *ptd, int type);
  68. static Slapi_DN *ptd_get_first_subtree (const PluginTargetData *ptd, int *cookie);
  69. static Slapi_DN *ptd_get_next_subtree (const PluginTargetData *ptd, int *cookie);
  70. static PRBool ptd_is_special_data_set (const PluginTargetData *ptd, int type);
  71. int ptd_get_subtree_count (const PluginTargetData *ptd);
  72. static void plugin_set_global (PluginTargetData *ptd);
  73. static PRBool plugin_is_global (const PluginTargetData *ptd);
  74. static void plugin_set_default_access (struct pluginconfig *config);
  75. static PLHashTable *global_plugin_dns = NULL;
  76. /* The global plugin list is indexed by the PLUGIN_LIST_* constants defined in slap.h */
  77. static struct slapdplugin *global_plugin_list[PLUGIN_LIST_GLOBAL_MAX];
  78. /* plugin structure used to configure internal operation issued by the core server */
  79. static int global_server_plg_initialised= 0;
  80. struct slapdplugin global_server_plg;
  81. /* plugin structure used to configure internal operation issued by the core server */
  82. static int global_server_plg_id_initialised= 0;
  83. struct slapi_componentid global_server_id_plg;
  84. /* plugin structure used to configure operations issued by the old plugins that
  85. do not pass their identity in the operation */
  86. static struct slapdplugin global_default_plg;
  87. /* Enable/disable plugin callbacks for clean startup */
  88. static int global_plugin_callbacks_enabled = 0;
  89. static void
  90. add_plugin_to_list(struct slapdplugin **list, struct slapdplugin *plugin)
  91. {
  92. struct slapdplugin **tmp;
  93. struct slapdplugin *last = NULL;
  94. int plugin_added = 0;
  95. /* Insert the plugin into list based off of precedence. */
  96. for ( tmp = list; *tmp; tmp = &(*tmp)->plg_next )
  97. {
  98. if (plugin->plg_precedence < (*tmp)->plg_precedence)
  99. {
  100. if (last)
  101. {
  102. /* Insert item between last and tmp. */
  103. plugin->plg_next = *tmp;
  104. last->plg_next = plugin;
  105. } else {
  106. /* Add as the first list item. */
  107. plugin->plg_next = *tmp;
  108. *list = plugin;
  109. }
  110. plugin_added = 1;
  111. /* We've added the plug-in to the
  112. * list, so bail from the loop. */
  113. break;
  114. }
  115. /* Save a pointer to this plugin so we can
  116. * refer to it on the next loop iteration. */
  117. last = *tmp;
  118. }
  119. /* If we didn't add the plug-in to the list yet,
  120. * it needs to be added to the end of the list. */
  121. if (!plugin_added)
  122. {
  123. *tmp = plugin;
  124. }
  125. }
  126. struct slapdplugin *
  127. get_plugin_list(int plugin_list_index)
  128. {
  129. return global_plugin_list[plugin_list_index];
  130. }
  131. /*
  132. * As the plugin configuration information is read an array of
  133. * entries is built which reflect the plugins. The entries
  134. * are added after the syntax plugins are started so that the
  135. * nodes in the attribute tree are initialised correctly.
  136. */
  137. typedef struct entry_and_plugin {
  138. Slapi_Entry *e;
  139. struct slapdplugin *plugin;
  140. struct entry_and_plugin *next;
  141. } entry_and_plugin_t;
  142. static entry_and_plugin_t *dep_plugin_entries = NULL; /* for dependencies */
  143. #if 0
  144. static entry_and_plugin_t *plugin_entries = NULL;
  145. static void
  146. add_plugin_entries()
  147. {
  148. entry_and_plugin_t *ep = plugin_entries;
  149. entry_and_plugin_t *deleteep = 0;
  150. while (ep)
  151. {
  152. int plugin_actions = 0;
  153. Slapi_PBlock newpb;
  154. pblock_init(&newpb);
  155. slapi_add_entry_internal_set_pb(&newpb, ep->e, NULL,
  156. ep->plugin, plugin_actions);
  157. slapi_pblock_set(&newpb, SLAPI_TARGET_DN, (void*)slapi_entry_get_dn_const(ep->e));
  158. slapi_pblock_set(&newpb, SLAPI_TARGET_SDN, (void*)slapi_entry_get_sdn_const(ep->e));
  159. slapi_add_internal_pb(&newpb);
  160. deleteep = ep;
  161. ep = ep->next;
  162. slapi_ch_free((void**)&deleteep);
  163. pblock_done(&newpb);
  164. }
  165. plugin_entries = NULL;
  166. }
  167. #endif
  168. static void
  169. new_plugin_entry(entry_and_plugin_t **ep, Slapi_Entry *e, struct slapdplugin *plugin)
  170. {
  171. entry_and_plugin_t *oldep = 0;
  172. entry_and_plugin_t *iterep = *ep;
  173. entry_and_plugin_t *newep =
  174. (entry_and_plugin_t*)slapi_ch_calloc(1,sizeof(entry_and_plugin_t));
  175. newep->e = e;
  176. newep->plugin = plugin;
  177. while(iterep)
  178. {
  179. oldep = iterep;
  180. iterep = iterep->next;
  181. }
  182. newep->next = 0;
  183. if(oldep)
  184. oldep->next = newep;
  185. else
  186. *ep = newep;
  187. }
  188. static void
  189. add_plugin_entry_dn(const Slapi_DN *plugin_dn)
  190. {
  191. if (!global_plugin_dns)
  192. {
  193. global_plugin_dns = PL_NewHashTable(20, PL_HashString,
  194. PL_CompareStrings,
  195. PL_CompareValues, 0, 0);
  196. }
  197. PL_HashTableAdd(global_plugin_dns,
  198. slapi_sdn_get_ndn(plugin_dn),
  199. (void*)plugin_dn);
  200. }
  201. #define SLAPI_PLUGIN_NONE_IF_NULL( s ) ((s) == NULL ? "none" : (s))
  202. /*
  203. * Allows a plugin to register a plugin.
  204. * This was added so that 'object' plugins could register all
  205. * the plugin interfaces that it supports.
  206. */
  207. int
  208. slapi_register_plugin(
  209. const char *plugintype,
  210. int enabled,
  211. const char *initsymbol,
  212. slapi_plugin_init_fnptr initfunc,
  213. const char *name,
  214. char **argv,
  215. void *group_identity
  216. )
  217. {
  218. return slapi_register_plugin_ext(plugintype, enabled, initsymbol,
  219. initfunc, name, argv, group_identity, PLUGIN_DEFAULT_PRECEDENCE);
  220. }
  221. int
  222. slapi_register_plugin_ext(
  223. const char *plugintype,
  224. int enabled,
  225. const char *initsymbol,
  226. slapi_plugin_init_fnptr initfunc,
  227. const char *name,
  228. char **argv,
  229. void *group_identity,
  230. int precedence
  231. )
  232. {
  233. int ii = 0;
  234. int rc = 0;
  235. Slapi_Entry *e = NULL;
  236. char *dn = slapi_ch_smprintf("cn=%s,%s", name, PLUGIN_BASE_DN);
  237. Slapi_DN *sdn = slapi_sdn_new_normdn_passin(dn);
  238. e = slapi_entry_alloc();
  239. /* this function consumes dn */
  240. slapi_entry_init_ext(e, sdn, NULL);
  241. slapi_sdn_free(&sdn);
  242. slapi_entry_attr_set_charptr(e, "cn", name);
  243. slapi_entry_attr_set_charptr(e, ATTR_PLUGIN_TYPE, plugintype);
  244. if (!enabled)
  245. slapi_entry_attr_set_charptr(e, ATTR_PLUGIN_ENABLED, "off");
  246. slapi_entry_attr_set_charptr(e, ATTR_PLUGIN_INITFN, initsymbol);
  247. slapi_entry_attr_set_int(e, ATTR_PLUGIN_PRECEDENCE, precedence);
  248. for (ii = 0; argv && argv[ii]; ++ii) {
  249. char argname[64];
  250. PR_snprintf(argname, sizeof(argname), "%s%d", ATTR_PLUGIN_ARG, ii);
  251. slapi_entry_attr_set_charptr(e, argname, argv[ii]);
  252. }
  253. /* plugin_setup copies the given entry */
  254. plugin_setup(e, group_identity, initfunc, 0);
  255. slapi_entry_free(e);
  256. return rc;
  257. }
  258. int
  259. plugin_call_plugins( Slapi_PBlock *pb, int whichfunction )
  260. {
  261. int plugin_list_number= -1;
  262. int rc= 0;
  263. int do_op = global_plugin_callbacks_enabled;
  264. if ( pb == NULL )
  265. {
  266. return( 0 );
  267. }
  268. switch ( whichfunction ) {
  269. case SLAPI_PLUGIN_PRE_BIND_FN:
  270. case SLAPI_PLUGIN_PRE_UNBIND_FN:
  271. case SLAPI_PLUGIN_PRE_SEARCH_FN:
  272. case SLAPI_PLUGIN_PRE_COMPARE_FN:
  273. case SLAPI_PLUGIN_PRE_MODIFY_FN:
  274. case SLAPI_PLUGIN_PRE_MODRDN_FN:
  275. case SLAPI_PLUGIN_PRE_ADD_FN:
  276. case SLAPI_PLUGIN_PRE_DELETE_FN:
  277. case SLAPI_PLUGIN_PRE_ABANDON_FN:
  278. case SLAPI_PLUGIN_PRE_ENTRY_FN:
  279. case SLAPI_PLUGIN_PRE_REFERRAL_FN:
  280. case SLAPI_PLUGIN_PRE_RESULT_FN:
  281. plugin_list_number= PLUGIN_LIST_PREOPERATION;
  282. break;
  283. case SLAPI_PLUGIN_POST_BIND_FN:
  284. case SLAPI_PLUGIN_POST_UNBIND_FN:
  285. case SLAPI_PLUGIN_POST_SEARCH_FN:
  286. case SLAPI_PLUGIN_POST_SEARCH_FAIL_FN:
  287. case SLAPI_PLUGIN_POST_COMPARE_FN:
  288. case SLAPI_PLUGIN_POST_MODIFY_FN:
  289. case SLAPI_PLUGIN_POST_MODRDN_FN:
  290. case SLAPI_PLUGIN_POST_ADD_FN:
  291. case SLAPI_PLUGIN_POST_DELETE_FN:
  292. case SLAPI_PLUGIN_POST_ABANDON_FN:
  293. case SLAPI_PLUGIN_POST_ENTRY_FN:
  294. case SLAPI_PLUGIN_POST_REFERRAL_FN:
  295. case SLAPI_PLUGIN_POST_RESULT_FN:
  296. plugin_list_number= PLUGIN_LIST_POSTOPERATION;
  297. break;
  298. case SLAPI_PLUGIN_BE_PRE_MODIFY_FN:
  299. case SLAPI_PLUGIN_BE_PRE_MODRDN_FN:
  300. case SLAPI_PLUGIN_BE_PRE_ADD_FN:
  301. case SLAPI_PLUGIN_BE_PRE_DELETE_FN:
  302. case SLAPI_PLUGIN_BE_PRE_CLOSE_FN:
  303. case SLAPI_PLUGIN_BE_PRE_BACKUP_FN:
  304. plugin_list_number= PLUGIN_LIST_BEPREOPERATION;
  305. do_op = 1; /* always allow backend callbacks (even during startup) */
  306. break;
  307. case SLAPI_PLUGIN_BE_POST_MODIFY_FN:
  308. case SLAPI_PLUGIN_BE_POST_MODRDN_FN:
  309. case SLAPI_PLUGIN_BE_POST_ADD_FN:
  310. case SLAPI_PLUGIN_BE_POST_DELETE_FN:
  311. case SLAPI_PLUGIN_BE_POST_OPEN_FN:
  312. case SLAPI_PLUGIN_BE_POST_BACKUP_FN:
  313. plugin_list_number= PLUGIN_LIST_BEPOSTOPERATION;
  314. do_op = 1; /* always allow backend callbacks (even during startup) */
  315. break;
  316. case SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN:
  317. case SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN:
  318. case SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN:
  319. case SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN:
  320. case SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN:
  321. plugin_list_number= PLUGIN_LIST_INTERNAL_PREOPERATION;
  322. break;
  323. case SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN:
  324. case SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN:
  325. case SLAPI_PLUGIN_INTERNAL_POST_ADD_FN:
  326. case SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN:
  327. plugin_list_number= PLUGIN_LIST_INTERNAL_POSTOPERATION;
  328. break;
  329. case SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN:
  330. case SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN:
  331. case SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN:
  332. case SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN:
  333. case SLAPI_PLUGIN_BE_TXN_PRE_DELETE_TOMBSTONE_FN:
  334. plugin_list_number= PLUGIN_LIST_BETXNPREOPERATION;
  335. do_op = 1; /* always allow backend callbacks (even during startup) */
  336. break;
  337. case SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN:
  338. case SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN:
  339. case SLAPI_PLUGIN_BE_TXN_POST_ADD_FN:
  340. case SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN:
  341. plugin_list_number= PLUGIN_LIST_BETXNPOSTOPERATION;
  342. do_op = 1; /* always allow backend callbacks (even during startup) */
  343. break;
  344. }
  345. if(plugin_list_number!=-1 && do_op)
  346. {
  347. /* We stash the pblock plugin pointer to preserve the callers context */
  348. struct slapdplugin *p;
  349. slapi_pblock_get(pb, SLAPI_PLUGIN, &p);
  350. /* Call the operation on the Global Plugins */
  351. rc= plugin_call_list(global_plugin_list[plugin_list_number], whichfunction, pb);
  352. slapi_pblock_set(pb, SLAPI_PLUGIN, p);
  353. }
  354. else
  355. {
  356. /* Programmer error! or the callback is denied during startup */
  357. }
  358. return rc;
  359. }
  360. void
  361. plugin_call_entrystore_plugins(char **entrystr, uint *size)
  362. {
  363. struct slapdplugin *p;
  364. for (p = global_plugin_list[PLUGIN_LIST_LDBM_ENTRY_FETCH_STORE];
  365. p != NULL; p = p->plg_next )
  366. {
  367. if (p->plg_entrystorefunc)
  368. (*p->plg_entrystorefunc)(entrystr, size);
  369. }
  370. }
  371. void
  372. plugin_call_entryfetch_plugins(char **entrystr, uint *size)
  373. {
  374. struct slapdplugin *p;
  375. for (p = global_plugin_list[PLUGIN_LIST_LDBM_ENTRY_FETCH_STORE];
  376. p != NULL; p = p->plg_next )
  377. {
  378. if (p->plg_entryfetchfunc)
  379. (*p->plg_entryfetchfunc)(entrystr, size);
  380. }
  381. }
  382. /*
  383. * call extended operation plugins
  384. *
  385. * return SLAPI_PLUGIN_EXTENDED_SENT_RESULT if one of the extended operation
  386. * plugins sent a result.
  387. * return SLAPI_PLUGIN_EXTENDED_NOT_HANDLED if no extended operation plugins
  388. * handled the operation.
  389. * otherwise, return an LDAP error code (possibly a merge of the errors
  390. * returned by the plugins we called).
  391. */
  392. int
  393. plugin_call_exop_plugins( Slapi_PBlock *pb, char *oid )
  394. {
  395. struct slapdplugin *p;
  396. int i, rc;
  397. int lderr = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  398. for ( p = global_plugin_list[PLUGIN_LIST_EXTENDED_OPERATION]; p != NULL; p = p->plg_next ) {
  399. if ( p->plg_exhandler != NULL ) {
  400. if ( p->plg_exoids != NULL ) {
  401. for ( i = 0; p->plg_exoids[i] != NULL; i++ ) {
  402. if ( strcasecmp( oid, p->plg_exoids[i] )
  403. == 0 ) {
  404. break;
  405. }
  406. }
  407. if ( p->plg_exoids[i] == NULL ) {
  408. continue;
  409. }
  410. }
  411. slapi_pblock_set( pb, SLAPI_PLUGIN, p );
  412. set_db_default_result_handlers( pb );
  413. if ( (rc = (*p->plg_exhandler)( pb ))
  414. == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
  415. return( rc ); /* result sent */
  416. } else if ( rc != SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
  417. /*
  418. * simple merge: report last real error
  419. */
  420. if ( lderr == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED
  421. || rc != LDAP_SUCCESS ) {
  422. lderr = rc;
  423. }
  424. }
  425. }
  426. }
  427. return( lderr );
  428. }
  429. /*
  430. * Attempt to convert the extended operation 'oid' to a string by
  431. * examining the registered plugins. Returns NULL if no plugin is
  432. * registered for this OID.
  433. *
  434. * Our first choice is to use an OID-specific name that has been
  435. * registered by a plugin via the SLAPI_PLUGIN_EXT_OP_NAMELIST pblock setting.
  436. * Our second choice is to use the plugin's ID (short name).
  437. * Our third choice is to use the plugin's RDN (under cn=config).
  438. */
  439. const char *
  440. plugin_extended_op_oid2string( const char *oid )
  441. {
  442. struct slapdplugin *p;
  443. int i, j;
  444. const char *rval = NULL;
  445. for ( p = global_plugin_list[PLUGIN_LIST_EXTENDED_OPERATION]; p != NULL;
  446. p = p->plg_next ) {
  447. if ( p->plg_exhandler != NULL && p->plg_exoids != NULL ) {
  448. for ( i = 0; p->plg_exoids[i] != NULL; i++ ) {
  449. if ( strcasecmp( oid, p->plg_exoids[i] ) == 0 ) {
  450. if ( NULL != p->plg_exnames ) {
  451. for ( j = 0; j < i && p->plg_exnames[j] != NULL; ++j ) {
  452. ;
  453. }
  454. rval = p->plg_exnames[j]; /* OID-related name */
  455. }
  456. if ( NULL == rval ) {
  457. if ( NULL != p->plg_desc.spd_id ) {
  458. rval = p->plg_desc.spd_id; /* short name */
  459. } else {
  460. rval = p->plg_name; /* RDN */
  461. }
  462. }
  463. break;
  464. }
  465. }
  466. }
  467. }
  468. return( rval );
  469. }
  470. /*
  471. * kexcoff: return the slapdplugin structure
  472. */
  473. struct slapdplugin *
  474. plugin_get_pwd_storage_scheme(char *name, int len, int index)
  475. {
  476. /* index could be PLUGIN_LIST_PWD_STORAGE_SCHEME or PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME */
  477. struct slapdplugin *p;
  478. for ( p = global_plugin_list[index]; p != NULL; p = p->plg_next ) {
  479. if (strlen(p->plg_pwdstorageschemename) == len) {
  480. if (strncasecmp(p->plg_pwdstorageschemename, name, len) == 0) {
  481. return( p );
  482. }
  483. }
  484. }
  485. return( NULL );
  486. }
  487. char *
  488. plugin_get_pwd_storage_scheme_list(int index)
  489. {
  490. /* index could be PLUGIN_LIST_PWD_STORAGE_SCHEME or PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME */
  491. struct slapdplugin *p = NULL;
  492. char *names_list = NULL;
  493. int len = 0;
  494. /* first pass - calculate space needed for comma delimited list */
  495. for ( p = global_plugin_list[index]; p != NULL; p = p->plg_next ) {
  496. if ( p->plg_pwdstorageschemeenc != NULL )
  497. {
  498. /* + 1 for comma, 1 for space, 1 for null */
  499. len += strlen(p->plg_pwdstorageschemename) + 3;
  500. }
  501. }
  502. /* no plugins? */
  503. if (!len)
  504. return NULL;
  505. /* next, allocate the space */
  506. names_list = (char *)slapi_ch_malloc(len+1);
  507. *names_list = 0;
  508. /* second pass - write the string */
  509. for ( p = global_plugin_list[index]; p != NULL; p = p->plg_next ) {
  510. if ( p->plg_pwdstorageschemeenc != NULL )
  511. {
  512. strcat(names_list, p->plg_pwdstorageschemename);
  513. if (p->plg_next != NULL)
  514. strcat(names_list, ", ");
  515. }
  516. }
  517. return( names_list );
  518. }
  519. int slapi_send_ldap_intermediate( Slapi_PBlock *pb, LDAPControl **ectrls,
  520. char *responseName, struct berval *responseValue)
  521. {
  522. /* no SLAPI_PLUGIN_DB_INTERMEDIATE_FN defined
  523. * always directly call slapd_ function
  524. */
  525. return send_ldap_intermediate(pb, ectrls, responseName, responseValue);
  526. }
  527. int
  528. slapi_send_ldap_search_entry( Slapi_PBlock *pb, Slapi_Entry *e, LDAPControl **ectrls,
  529. char **attrs, int attrsonly )
  530. {
  531. IFP fn = NULL;
  532. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_ENTRY_FN,(void*)&fn);
  533. if (NULL == fn)
  534. {
  535. return -1;
  536. }
  537. return (*fn)(pb,e,ectrls,attrs,attrsonly);
  538. }
  539. void
  540. slapi_set_ldap_result( Slapi_PBlock *pb, int err, char *matched, char *text,
  541. int nentries, struct berval **urls )
  542. {
  543. char * old_matched = NULL;
  544. char * old_text = NULL;
  545. char * matched_copy = slapi_ch_strdup(matched);
  546. char * text_copy = slapi_ch_strdup(text);
  547. /* free the old matched and text, if any */
  548. slapi_pblock_get(pb, SLAPI_RESULT_MATCHED, &old_matched);
  549. slapi_ch_free_string(&old_matched);
  550. slapi_pblock_get(pb, SLAPI_RESULT_TEXT, &old_text);
  551. slapi_ch_free_string(&old_text);
  552. /* set the new stuff */
  553. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &err);
  554. slapi_pblock_set(pb, SLAPI_RESULT_MATCHED, matched_copy);
  555. slapi_pblock_set(pb, SLAPI_RESULT_TEXT, text_copy);
  556. }
  557. void
  558. slapi_send_ldap_result_from_pb( Slapi_PBlock *pb)
  559. {
  560. int err;
  561. char *matched;
  562. char *text;
  563. IFP fn = NULL;
  564. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &err);
  565. slapi_pblock_get(pb, SLAPI_RESULT_TEXT, &text);
  566. slapi_pblock_get(pb, SLAPI_RESULT_MATCHED, &matched);
  567. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_RESULT_FN,(void*)&fn);
  568. if (NULL != fn)
  569. {
  570. (*fn)(pb,err,matched,text,0,NULL);
  571. }
  572. slapi_pblock_set(pb, SLAPI_RESULT_TEXT, NULL);
  573. slapi_pblock_set(pb, SLAPI_RESULT_MATCHED, NULL);
  574. slapi_ch_free((void **)&matched);
  575. slapi_ch_free((void **)&text);
  576. }
  577. void
  578. slapi_send_ldap_result( Slapi_PBlock *pb, int err, char *matched, char *text,
  579. int nentries, struct berval **urls )
  580. {
  581. IFP fn = NULL;
  582. Slapi_Operation *operation;
  583. long op_type;
  584. /* GB : for spanning requests over multiple backends */
  585. if (err == LDAP_NO_SUCH_OBJECT)
  586. {
  587. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  588. op_type = operation_get_type(operation);
  589. if (op_type == SLAPI_OPERATION_SEARCH)
  590. {
  591. if (urls || nentries)
  592. {
  593. LDAPDebug( LDAP_DEBUG_ANY, "ERROR : urls or nentries set"
  594. "in sendldap_result while NO_SUCH_OBJECT returned\n",0,0,0);
  595. }
  596. slapi_set_ldap_result(pb, err, matched, text, 0, NULL);
  597. return;
  598. }
  599. }
  600. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &err);
  601. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_RESULT_FN,(void*)&fn);
  602. if (NULL == fn)
  603. {
  604. return ;
  605. }
  606. /*
  607. * Call the result function. It should set pb->pb_op->o_status to
  608. * SLAPI_OP_STATUS_RESULT_SENT right after sending the result to
  609. * the client or otherwise consuming it.
  610. */
  611. (*fn)(pb,err,matched,text,nentries,urls);
  612. }
  613. int
  614. slapi_send_ldap_referral( Slapi_PBlock *pb, Slapi_Entry *e, struct berval **refs,
  615. struct berval ***urls )
  616. {
  617. IFP fn = NULL;
  618. slapi_pblock_get(pb,SLAPI_PLUGIN_DB_REFERRAL_FN,(void*)&fn);
  619. if (NULL == fn)
  620. {
  621. return -1;
  622. }
  623. return (*fn)(pb,e,refs,urls);
  624. }
  625. /***********************************************************
  626. start of plugin dependency code
  627. ************************************************************/
  628. /* struct _plugin_dep_type
  629. * we shall not presume to know all plugin types
  630. * so as to allow new types to be added without
  631. * requiring changes to this code (hopefully)
  632. * so we need to dynamically keep track of them
  633. */
  634. typedef struct _plugin_dep_type{
  635. char *type; /* the string descriptor */
  636. int num_not_started; /* the count of plugins which have yet to be started for this type */
  637. struct _plugin_dep_type *next;
  638. } *plugin_dep_type;
  639. /* _plugin_dep_config
  640. * we need somewhere to collect the plugin configurations
  641. * prior to attempting to resolve dependencies
  642. */
  643. typedef struct _plugin_dep_config {
  644. char *name;
  645. char *type;
  646. Slapi_PBlock pb;
  647. struct slapdplugin *plugin;
  648. Slapi_Entry *e;
  649. int entry_created;
  650. int op_done;
  651. char **depends_type_list;
  652. int total_type;
  653. char **depends_named_list;
  654. int total_named;
  655. char *config_area;
  656. } plugin_dep_config;
  657. /* list of plugins which should be shutdown in reverse order */
  658. static plugin_dep_config *global_plugin_shutdown_order = 0;
  659. static int global_plugins_started = 0;
  660. /*
  661. * find_plugin_type
  662. *
  663. * searches the list for the plugin type
  664. * and returns the plugin_dep_type if found
  665. */
  666. static plugin_dep_type
  667. find_plugin_type(plugin_dep_type head, char *type)
  668. {
  669. plugin_dep_type ret = 0;
  670. plugin_dep_type iter = head;
  671. while(iter)
  672. {
  673. if(!slapi_UTF8CASECMP(iter->type, type))
  674. {
  675. ret = iter;
  676. break;
  677. }
  678. iter = iter->next;
  679. }
  680. return ret;
  681. }
  682. /*
  683. * increment_plugin_type
  684. *
  685. * searches the list for the plugin type
  686. * and increments its not started value
  687. * returns the current type count on success -1 on failure
  688. * to find the type
  689. */
  690. static int
  691. increment_plugin_type(plugin_dep_type head, char *type)
  692. {
  693. int ret = -1;
  694. plugin_dep_type the_type;
  695. if ((the_type = find_plugin_type(head, type)) != NULL)
  696. ret = ++the_type->num_not_started;
  697. return ret;
  698. }
  699. /*
  700. * decrement_plugin_type
  701. *
  702. * searches the list for the plugin type
  703. * and decrements its not started value
  704. * returns the current type count on success -1 on failure
  705. * to find the type
  706. */
  707. static int
  708. decrement_plugin_type(plugin_dep_type head, char *type)
  709. {
  710. int ret = -1;
  711. plugin_dep_type the_type;
  712. if ((the_type = find_plugin_type(head, type)) != NULL)
  713. ret = --the_type->num_not_started;
  714. return ret;
  715. }
  716. /*
  717. * add_plugin_type
  718. *
  719. * Either increments the count of the plugin type
  720. * or when it does not exist, adds it to the list
  721. */
  722. static int
  723. add_plugin_type(plugin_dep_type *head, char *type)
  724. {
  725. int ret = -1;
  726. if(*head)
  727. {
  728. if(0 < increment_plugin_type(*head, type))
  729. {
  730. ret = 0;
  731. }
  732. }
  733. if(ret)
  734. {
  735. /* create new head */
  736. plugin_dep_type tmp_head;
  737. tmp_head = (plugin_dep_type)slapi_ch_malloc(sizeof(struct _plugin_dep_type));
  738. tmp_head->num_not_started = 1;
  739. tmp_head->type = slapi_ch_strdup(type);
  740. ret = 0;
  741. tmp_head->next = *head;
  742. (*head) = tmp_head;
  743. }
  744. return ret;
  745. }
  746. /*
  747. * plugin_create_stringlist
  748. *
  749. * Creates a string list from values of the entries
  750. * attribute passed in as args - used to track dependencies
  751. *
  752. */
  753. int
  754. plugin_create_stringlist( Slapi_Entry *plugin_entry, char *attr_name,
  755. int *total_strings, char ***list)
  756. {
  757. Slapi_Attr *attr = 0;
  758. int hint =0;
  759. int num_vals = 0;
  760. int val_index = 0;
  761. Slapi_Value *val;
  762. if(0 == slapi_entry_attr_find( plugin_entry, attr_name, &attr ))
  763. {
  764. /* allocate memory for the string array */
  765. slapi_attr_get_numvalues( attr, &num_vals);
  766. if(num_vals)
  767. {
  768. *total_strings = num_vals;
  769. *list = (char **)slapi_ch_malloc(sizeof(char*) * num_vals);
  770. }
  771. else
  772. goto bail; /* if this ever happens, then they are running on a TSR-80 */
  773. val_index = 0;
  774. hint = slapi_attr_first_value( attr, &val );
  775. while(val_index < num_vals)
  776. {
  777. /* add the value to the array */
  778. (*list)[val_index] = (char*)slapi_ch_strdup(slapi_value_get_string(val));
  779. hint = slapi_attr_next_value( attr, hint, &val );
  780. val_index++;
  781. }
  782. }
  783. else
  784. *total_strings = num_vals;
  785. bail:
  786. return num_vals;
  787. }
  788. /*
  789. * plugin_dependency_startall()
  790. *
  791. * Starts all plugins (apart from syntax and matching rule) in order
  792. * of dependency.
  793. *
  794. * Dependencies will be determined by these multi-valued attributes:
  795. *
  796. * nsslapd-plugin-depends-on-type : all plugins whose type value matches one of these values must
  797. * be started prior to this plugin
  798. *
  799. * nsslapd-plugin-depends-on-named : the plugin whose cn value matches one of these values must
  800. * be started prior to this plugin
  801. */
  802. static int
  803. plugin_dependency_startall(int argc, char** argv, char *errmsg, int operation)
  804. {
  805. int ret = 0;
  806. Slapi_PBlock pb;
  807. int total_plugins = 0;
  808. plugin_dep_config *config = 0;
  809. plugin_dep_type plugin_head = 0;
  810. int plugin_index = 0;
  811. Slapi_Entry *plugin_entry;
  812. int i = 0; /* general index iterator */
  813. plugin_dep_type the_plugin_type;
  814. int index = 0;
  815. char * value;
  816. int plugins_started;
  817. int num_plg_started;
  818. struct slapdplugin *plugin;
  819. entry_and_plugin_t *ep = dep_plugin_entries;
  820. int shutdown_index = 0;
  821. /*
  822. * Disable registered plugin functions so preops/postops/etc
  823. * dont get called prior to the plugin being started (due to
  824. * plugins performing ops on the DIT)
  825. */
  826. global_plugin_callbacks_enabled = 0;
  827. /* Count the plugins so we can allocate memory for the config array */
  828. while(ep)
  829. {
  830. total_plugins++;
  831. ep = ep->next;
  832. }
  833. /* allocate the config array */
  834. config = (plugin_dep_config*)slapi_ch_malloc(sizeof(plugin_dep_config) * total_plugins);
  835. if(config)
  836. memset(config, 0, sizeof(plugin_dep_config) * total_plugins);
  837. else
  838. {
  839. ret = -1;
  840. goto bail;
  841. }
  842. ep = dep_plugin_entries;
  843. /* Collect relevant config */
  844. while(ep)
  845. {
  846. plugin = ep->plugin;
  847. if(plugin == 0)
  848. continue;
  849. pblock_init(&pb);
  850. slapi_pblock_set( &pb, SLAPI_ARGC, &argc);
  851. slapi_pblock_set( &pb, SLAPI_ARGV, &argv);
  852. config[plugin_index].pb = pb;
  853. config[plugin_index].e = ep->e;
  854. /* add type */
  855. plugin_entry = ep->e;
  856. ep->e = NULL; /* consumed by the operation above, and eventually by the
  857. slapi_internal_add operation below */
  858. if(plugin_entry)
  859. {
  860. /*
  861. * Pass the plugin DN in SLAPI_TARGET_SDN and the plugin entry
  862. * in SLAPI_ADD_ENTRY. For this to actually work, we need to
  863. * create an operation and include that in the pblock as well,
  864. * because these two items are stored in the operation parameters.
  865. */
  866. /* WARNING: memory leak here - op is only freed by a pblock_done,
  867. and this only happens below if the plugin is enabled - a short
  868. circuit goto bail may also cause a leak - however, since this
  869. only happens a few times at startup, this is not a very serious
  870. leak - just after the call to plugin_call_one */
  871. Operation *op = internal_operation_new(SLAPI_OPERATION_ADD, 0);
  872. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_OPERATION, op);
  873. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_TARGET_SDN,
  874. (void*)(slapi_entry_get_sdn_const(plugin_entry)));
  875. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_ADD_ENTRY,
  876. plugin_entry );
  877. /* Pass the plugin alternate config area DN in SLAPI_PLUGIN_CONFIG_AREA. */
  878. value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_CONFIG_AREA);
  879. if(value)
  880. {
  881. config[plugin_index].config_area = value;
  882. value = NULL;
  883. slapi_pblock_set(&(config[plugin_index].pb), SLAPI_PLUGIN_CONFIG_AREA,
  884. config[plugin_index].config_area);
  885. }
  886. value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype");
  887. if(value)
  888. {
  889. add_plugin_type( &plugin_head, value);
  890. config[plugin_index].type = value;
  891. value = NULL;
  892. }
  893. /* now the name */
  894. value = slapi_entry_attr_get_charptr(plugin_entry, "cn");
  895. if(value)
  896. {
  897. config[plugin_index].name = value;
  898. value = NULL;
  899. }
  900. config[plugin_index].plugin = plugin;
  901. /* now add dependencies */
  902. plugin_create_stringlist( plugin_entry, "nsslapd-plugin-depends-on-named",
  903. &(config[plugin_index].total_named), &(config[plugin_index].depends_named_list));
  904. plugin_create_stringlist( plugin_entry, "nsslapd-plugin-depends-on-type",
  905. &(config[plugin_index].total_type), &(config[plugin_index].depends_type_list));
  906. }
  907. plugin_index++;
  908. ep = ep->next;
  909. }
  910. /* prepare list of shutdown order (we need nothing fancier right now
  911. * than the reverse startup order) The list may include NULL entries,
  912. * these will be plugins which were never started
  913. */
  914. shutdown_index = total_plugins - 1;
  915. global_plugin_shutdown_order = (plugin_dep_config*)slapi_ch_malloc(sizeof(plugin_dep_config) * total_plugins);
  916. if(global_plugin_shutdown_order)
  917. memset(global_plugin_shutdown_order, 0, sizeof(plugin_dep_config) * total_plugins);
  918. else
  919. {
  920. ret = -1;
  921. goto bail;
  922. }
  923. /* now resolve dependencies
  924. * cycle through list, if a plugin has no dependencies then start it
  925. * then remove it from the dependency lists of all other plugins
  926. * and decrement the corresponding element of the plugin types array
  927. * for depends_type we will need to check the array of plugin types
  928. * to see if all type dependencies are at zero prior to start
  929. * if one cycle fails to load any plugins we have failed, however
  930. * we shall continue loading plugins in case a configuration error
  931. * can correct itself
  932. */
  933. plugins_started = 1;
  934. num_plg_started = 0;
  935. while(plugins_started && num_plg_started < total_plugins)
  936. {
  937. plugins_started = 0;
  938. for(plugin_index=0; plugin_index < total_plugins; plugin_index++)
  939. {
  940. /* perform op on plugins only once */
  941. if(config[plugin_index].op_done == 0)
  942. {
  943. int enabled = 0;
  944. int satisfied = 0;
  945. int break_out = 0;
  946. /*
  947. * determine if plugin is enabled
  948. * some processing is necessary even
  949. * if it is not
  950. */
  951. if ( NULL != config[plugin_index].e && (value = slapi_entry_attr_get_charptr(config[plugin_index].e,
  952. ATTR_PLUGIN_ENABLED)) &&
  953. !strcasecmp(value, "on"))
  954. {
  955. enabled = 1;
  956. }
  957. else
  958. enabled = 0;
  959. slapi_ch_free((void**)&value);
  960. /*
  961. * make sure named dependencies have been satisfied
  962. * that means that the list of names should contain all
  963. * null entries
  964. */
  965. if(enabled && config[plugin_index].total_named)
  966. {
  967. i = 0;
  968. while(break_out == 0 && i < config[plugin_index].total_named)
  969. {
  970. satisfied = 1;
  971. if((config[plugin_index].depends_named_list)[i] != 0)
  972. {
  973. satisfied = 0;
  974. break_out = 1;
  975. }
  976. i++;
  977. }
  978. if(!satisfied)
  979. continue;
  980. }
  981. /*
  982. * make sure the type dependencies have been satisfied
  983. * that means for each type in the list, it's number of
  984. * plugins left not started is zero
  985. *
  986. */
  987. satisfied = 0;
  988. break_out = 0;
  989. if(enabled && config[plugin_index].total_type)
  990. {
  991. i = 0;
  992. while(break_out == 0 && i < config[plugin_index].total_type)
  993. {
  994. satisfied = 1;
  995. the_plugin_type = find_plugin_type(plugin_head, (config[plugin_index].depends_type_list)[i]);
  996. if(the_plugin_type && the_plugin_type->num_not_started != 0)
  997. {
  998. satisfied = 0;
  999. break_out = 1;
  1000. }
  1001. i++;
  1002. }
  1003. if(!satisfied)
  1004. continue;
  1005. }
  1006. /**** This plugins dependencies have now been satisfied ****/
  1007. satisfied = 1; /* symbolic only */
  1008. /*
  1009. * Add the plugins entry to the DSE so the plugin can get
  1010. * its config (both enabled and disabled have entries
  1011. */
  1012. if(!config[plugin_index].entry_created)
  1013. {
  1014. int plugin_actions = 0;
  1015. Slapi_PBlock newpb;
  1016. Slapi_Entry *newe;
  1017. pblock_init(&newpb);
  1018. /*
  1019. * config[plugin_index].e is freed up by
  1020. * below function calls, but we may need
  1021. * it later, so create a copy
  1022. */
  1023. newe = slapi_entry_dup( config[plugin_index].e );
  1024. slapi_add_entry_internal_set_pb(&newpb, newe, NULL,
  1025. plugin_get_default_component_id(), plugin_actions);
  1026. slapi_pblock_set(&newpb, SLAPI_TARGET_SDN, (void*)slapi_entry_get_sdn_const(newe));
  1027. slapi_add_internal_pb(&newpb);
  1028. pblock_done(&newpb);
  1029. config[plugin_index].entry_created = 1;
  1030. }
  1031. /*
  1032. * only actually start plugin and remove from lists if its enabled
  1033. * we do remove from plugin type list however - rule is dependency on
  1034. * zero or more for type
  1035. */
  1036. if (enabled)
  1037. {
  1038. /* finally, perform the op on the plugin */
  1039. LDAPDebug( LDAP_DEBUG_PLUGIN, "Starting %s plugin %s\n" , config[plugin_index].type, config[plugin_index].name, 0 );
  1040. ret = plugin_call_one( config[plugin_index].plugin, operation, &(config[plugin_index].pb));
  1041. pblock_done(&(config[plugin_index].pb));
  1042. if(ret)
  1043. {
  1044. /*
  1045. * We will not exit here. If we allow plugins to load normally it is
  1046. * possible that a configuration error (dependedncies which were not
  1047. * configured properly) can be recovered from. If there really is a
  1048. * problem then the plugin will never start and eventually it will
  1049. * trigger an exit anyway.
  1050. */
  1051. LDAPDebug( LDAP_DEBUG_ANY, "Failed to start %s plugin %s\n" , config[plugin_index].type, config[plugin_index].name, 0 );
  1052. continue;
  1053. }
  1054. /* Add this plugin to the shutdown list */
  1055. global_plugin_shutdown_order[shutdown_index] = config[plugin_index];
  1056. shutdown_index--;
  1057. global_plugins_started++;
  1058. /* remove this named plugin from other plugins lists */
  1059. for(i=0; i<total_plugins; i++)
  1060. {
  1061. index = 0;
  1062. while(index < config[i].total_named)
  1063. {
  1064. if((config[i].depends_named_list)[index] != 0 && !slapi_UTF8CASECMP((config[i].depends_named_list)[index], config[plugin_index].name))
  1065. {
  1066. slapi_ch_free((void**)&((config[i].depends_named_list)[index]));
  1067. (config[i].depends_named_list)[index] = 0;
  1068. }
  1069. index++;
  1070. }
  1071. }
  1072. }
  1073. /* decrement the type counter for this plugin type */
  1074. decrement_plugin_type(plugin_head, config[plugin_index].type);
  1075. config[plugin_index].op_done = 1;
  1076. num_plg_started++;
  1077. plugins_started = 1;
  1078. }
  1079. }
  1080. }
  1081. if(plugins_started == 0)
  1082. {
  1083. /* a dependency was not resolved - error */
  1084. LDAPDebug( LDAP_DEBUG_ANY, "Error: Failed to resolve plugin dependencies\n" , 0, 0, 0 );
  1085. /* list the plugins yet to perform op */
  1086. index = 0;
  1087. while(i < total_plugins)
  1088. {
  1089. if(config[i].op_done == 0)
  1090. {
  1091. LDAPDebug( LDAP_DEBUG_ANY, "Error: %s plugin %s is not started\n" , config[i].type, config[i].name, 0 );
  1092. }
  1093. i++;
  1094. }
  1095. exit(1);
  1096. }
  1097. bail:
  1098. /*
  1099. * need the details in config to hang around for shutdown
  1100. * config itself may be deleted since its contents have been
  1101. * copied by value to the shutdown list
  1102. */
  1103. if(config)
  1104. {
  1105. index = 0;
  1106. while(index < total_plugins)
  1107. {
  1108. /*
  1109. if(config[index].depends_named_list)
  1110. {
  1111. slapi_ch_free((void**)&(config[index].depends_named_list));
  1112. }
  1113. */
  1114. if(config[index].depends_type_list)
  1115. {
  1116. i = 0;
  1117. while(i < config[index].total_type)
  1118. {
  1119. slapi_ch_free((void**)&(config[index].depends_type_list)[i]);
  1120. i++;
  1121. }
  1122. slapi_ch_free((void**)&(config[index].depends_type_list));
  1123. }
  1124. /*
  1125. slapi_ch_free((void**)&(config[index].name));
  1126. slapi_ch_free((void**)&(config[index].type));
  1127. */
  1128. index++;
  1129. }
  1130. slapi_ch_free((void**)&config);
  1131. }
  1132. /* Finally enable registered plugin functions */
  1133. global_plugin_callbacks_enabled = 1;
  1134. return ret;
  1135. }
  1136. /*
  1137. * plugin_dependency_closeall
  1138. *
  1139. * uses the shutdown list created at startup to close
  1140. * plugins in the correct order
  1141. *
  1142. * For now this leaks the list and contents, but since
  1143. * it hangs around until shutdown anyway, we don't care
  1144. *
  1145. */
  1146. void
  1147. plugin_dependency_closeall()
  1148. {
  1149. Slapi_PBlock pb;
  1150. int plugins_closed = 0;
  1151. int index = 0;
  1152. while(plugins_closed<global_plugins_started)
  1153. {
  1154. /*
  1155. * the first few entries may not be valid
  1156. * since the list was created in the reverse
  1157. * order and some plugins may have been counted
  1158. * for the purpose of list allocation but are
  1159. * disabled and so were never started
  1160. *
  1161. * we check that here
  1162. */
  1163. if(global_plugin_shutdown_order[index].name)
  1164. {
  1165. pblock_init(&pb);
  1166. plugin_call_one( global_plugin_shutdown_order[index].plugin, SLAPI_PLUGIN_CLOSE_FN, &pb );
  1167. /* set plg_closed to 1 to prevent any further plugin pre/post op function calls */
  1168. global_plugin_shutdown_order[index].plugin->plg_closed = 1;
  1169. plugins_closed++;
  1170. }
  1171. index++;
  1172. }
  1173. }
  1174. /***********************************************************
  1175. end of plugin dependency code
  1176. ************************************************************/
  1177. /*
  1178. * Function: plugin_startall
  1179. *
  1180. * Returns: squat
  1181. *
  1182. * Description: Some plugins may need to do some stuff after all the config
  1183. * stuff is done with. So this function goes through and starts all plugins
  1184. */
  1185. void
  1186. plugin_startall(int argc, char** argv, int start_backends, int start_global)
  1187. {
  1188. /* initialize special plugin structures */
  1189. default_plugin_init ();
  1190. plugin_dependency_startall(argc, argv, "plugin startup failed\n", SLAPI_PLUGIN_START_FN);
  1191. }
  1192. /*
  1193. * Function: plugin_close_all
  1194. *
  1195. * Returns: squat
  1196. *
  1197. * Description: cleanup routine, allows plugins to kill threads, free memory started in start fn
  1198. *
  1199. */
  1200. void
  1201. plugin_closeall(int close_backends, int close_globals)
  1202. {
  1203. plugin_dependency_closeall();
  1204. }
  1205. static int
  1206. plugin_call_list (struct slapdplugin *list, int operation, Slapi_PBlock *pb)
  1207. {
  1208. return plugin_call_func(list, operation, pb, 0);
  1209. }
  1210. static int
  1211. plugin_call_one (struct slapdplugin *list, int operation, Slapi_PBlock *pb)
  1212. {
  1213. return plugin_call_func(list, operation, pb, 1);
  1214. }
  1215. /*
  1216. * Return codes:
  1217. * - For preoperation plugins, returns the return code passed back from the first
  1218. * plugin that fails, or zero if all plugins succeed.
  1219. * - For bepreop and bepostop plugins, returns a bitwise OR of the return codes
  1220. * returned by all the plugins called (there's only one bepreop and one bepostop
  1221. * in DS 5.0 anyway).
  1222. * - For postoperation plugins, returns 0.
  1223. */
  1224. static int
  1225. plugin_call_func (struct slapdplugin *list, int operation, Slapi_PBlock *pb, int call_one)
  1226. {
  1227. /* Invoke the operation on the plugins that are registered for the subtree effected by the operation. */
  1228. int rc;
  1229. int return_value = 0;
  1230. int count= 0;
  1231. for (; list != NULL; list = list->plg_next)
  1232. {
  1233. IFP func = NULL;
  1234. slapi_pblock_set (pb, SLAPI_PLUGIN, list);
  1235. set_db_default_result_handlers (pb); /* JCM: What's this do? Is it needed here? */
  1236. if (slapi_pblock_get (pb, operation, &func) == 0 && func != NULL &&
  1237. plugin_invoke_plugin_pb (list, operation, pb) && list->plg_closed == 0)
  1238. {
  1239. char *n= list->plg_name;
  1240. LDAPDebug( LDAP_DEBUG_TRACE, "Calling plugin '%s' #%d type %d\n", (n==NULL?"noname":n), count, operation );
  1241. /* counters_to_errors_log("before plugin call"); */
  1242. if (( rc = func (pb)) != 0 )
  1243. {
  1244. if (SLAPI_PLUGIN_PREOPERATION == list->plg_type ||
  1245. SLAPI_PLUGIN_INTERNAL_PREOPERATION == list->plg_type ||
  1246. SLAPI_PLUGIN_START_FN == operation )
  1247. {
  1248. /*
  1249. * We bail out of plugin processing for preop plugins
  1250. * that return a non-zero return code. This allows preop
  1251. * plugins to cause further preop processing to terminate, and
  1252. * causes the operation to be vetoed.
  1253. */
  1254. return_value = rc;
  1255. break;
  1256. }
  1257. else if (SLAPI_PLUGIN_BEPREOPERATION == list->plg_type ||
  1258. SLAPI_PLUGIN_BETXNPREOPERATION == list->plg_type ||
  1259. SLAPI_PLUGIN_BEPOSTOPERATION == list->plg_type ||
  1260. SLAPI_PLUGIN_BETXNPOSTOPERATION == list->plg_type )
  1261. {
  1262. /*
  1263. * respect fatal error SLAPI_PLUGIN_FAILURE (-1);
  1264. * should not OR it.
  1265. */
  1266. if (SLAPI_PLUGIN_FAILURE == rc) {
  1267. return_value = rc;
  1268. } else if (SLAPI_PLUGIN_FAILURE != return_value) {
  1269. /* OR the result into the return value
  1270. * for be pre/postops */
  1271. return_value |= rc;
  1272. }
  1273. }
  1274. }
  1275. /* counters_to_errors_log("after plugin call"); */
  1276. }
  1277. count++;
  1278. if(call_one)
  1279. break;
  1280. }
  1281. return( return_value );
  1282. }
  1283. int
  1284. slapi_berval_cmp (const struct berval* L, const struct berval* R) /* JCM - This does not belong here. But, where should it go? */
  1285. {
  1286. int result = 0;
  1287. if(L == NULL && R != NULL){
  1288. return 1;
  1289. } else if(L != NULL && R == NULL){
  1290. return -1;
  1291. } else if(L == NULL && R == NULL){
  1292. return 0;
  1293. }
  1294. if (L->bv_len < R->bv_len) {
  1295. result = memcmp (L->bv_val, R->bv_val, L->bv_len);
  1296. if (result == 0)
  1297. result = -1;
  1298. } else {
  1299. result = memcmp (L->bv_val, R->bv_val, R->bv_len);
  1300. if (result == 0 && (L->bv_len > R->bv_len))
  1301. result = 1;
  1302. }
  1303. return result;
  1304. }
  1305. static char **supported_saslmechanisms = NULL;
  1306. static Slapi_RWLock *supported_saslmechanisms_lock = NULL;
  1307. /*
  1308. * register a supported SASL mechanism so it will be returned as part of the
  1309. * root DSE.
  1310. */
  1311. void
  1312. slapi_register_supported_saslmechanism( char *mechanism )
  1313. {
  1314. if ( mechanism != NULL ) {
  1315. if (NULL == supported_saslmechanisms_lock) {
  1316. /* This is thread safe, as it gets executed by
  1317. * a single thread at init time (main->init_saslmechanisms) */
  1318. supported_saslmechanisms_lock = slapi_new_rwlock();
  1319. if (NULL == supported_saslmechanisms_lock) {
  1320. /* Out of resources */
  1321. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  1322. "slapi_register_supported_saslmechanism: failed to create lock.\n");
  1323. exit (1);
  1324. }
  1325. }
  1326. slapi_rwlock_wrlock(supported_saslmechanisms_lock);
  1327. charray_add( &supported_saslmechanisms, slapi_ch_strdup( mechanism ));
  1328. slapi_rwlock_unlock(supported_saslmechanisms_lock);
  1329. }
  1330. }
  1331. /*
  1332. * return pointer to NULL-terminated array of supported SASL mechanisms.
  1333. * This function is not MTSafe and should be deprecated.
  1334. * slapi_get_supported_saslmechanisms_copy should be used instead.
  1335. */
  1336. char **
  1337. slapi_get_supported_saslmechanisms( void )
  1338. {
  1339. return( supported_saslmechanisms );
  1340. }
  1341. /*
  1342. * return pointer to NULL-terminated array of supported SASL mechanisms.
  1343. */
  1344. char **
  1345. slapi_get_supported_saslmechanisms_copy( void )
  1346. {
  1347. char ** ret = NULL;
  1348. slapi_rwlock_rdlock(supported_saslmechanisms_lock);
  1349. ret = charray_dup(supported_saslmechanisms);
  1350. slapi_rwlock_unlock(supported_saslmechanisms_lock);
  1351. return( ret );
  1352. }
  1353. static char **supported_extended_ops = NULL;
  1354. static Slapi_RWLock *extended_ops_lock = NULL;
  1355. /*
  1356. * register all of the LDAPv3 extended operations we know about.
  1357. */
  1358. void
  1359. ldapi_init_extended_ops( void )
  1360. {
  1361. extended_ops_lock = slapi_new_rwlock();
  1362. if (NULL == extended_ops_lock) {
  1363. /* Out of resources */
  1364. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  1365. "ldapi_init_extended_ops: failed to create lock.\n");
  1366. exit (1);
  1367. }
  1368. slapi_rwlock_wrlock(extended_ops_lock);
  1369. charray_add(&supported_extended_ops,
  1370. slapi_ch_strdup(EXTOP_BULK_IMPORT_START_OID));
  1371. charray_add(&supported_extended_ops,
  1372. slapi_ch_strdup(EXTOP_BULK_IMPORT_DONE_OID));
  1373. /* add future supported extops here... */
  1374. slapi_rwlock_unlock(extended_ops_lock);
  1375. }
  1376. /*
  1377. * register an extended op. so it can be returned as part of the root DSE.
  1378. */
  1379. void
  1380. ldapi_register_extended_op( char **opoids )
  1381. {
  1382. int i;
  1383. slapi_rwlock_wrlock(extended_ops_lock);
  1384. for ( i = 0; opoids != NULL && opoids[i] != NULL; ++i ) {
  1385. if ( !charray_inlist( supported_extended_ops, opoids[i] )) {
  1386. charray_add( &supported_extended_ops, slapi_ch_strdup( opoids[i] ));
  1387. }
  1388. }
  1389. slapi_rwlock_unlock(extended_ops_lock);
  1390. }
  1391. /*
  1392. * retrieve supported extended operation OIDs
  1393. * return 0 if successful and -1 if not.
  1394. * This function is not MTSafe and should be deprecated.
  1395. * slapi_get_supported_extended_ops_copy should be used instead.
  1396. */
  1397. char **
  1398. slapi_get_supported_extended_ops( void )
  1399. {
  1400. return( supported_extended_ops );
  1401. }
  1402. /*
  1403. * retrieve supported extended operation OIDs
  1404. * return 0 if successful and -1 if not.
  1405. */
  1406. char **
  1407. slapi_get_supported_extended_ops_copy( void )
  1408. {
  1409. char ** ret = NULL;
  1410. slapi_rwlock_rdlock(extended_ops_lock);
  1411. ret = charray_dup(supported_extended_ops);
  1412. slapi_rwlock_unlock(extended_ops_lock);
  1413. return( ret );
  1414. }
  1415. /*
  1416. looks up the given string type to convert to the internal integral type; also
  1417. returns the plugin list associated with the plugin type
  1418. returns 0 upon success and non-zero upon failure
  1419. */
  1420. static int
  1421. plugin_get_type_and_list(
  1422. const char *plugintype,
  1423. int *type,
  1424. struct slapdplugin ***plugin_list
  1425. )
  1426. {
  1427. int plugin_list_index = -1;
  1428. if ( strcasecmp( plugintype, "database" ) == 0 ) {
  1429. *type = SLAPI_PLUGIN_DATABASE;
  1430. plugin_list_index= PLUGIN_LIST_DATABASE;
  1431. } else if ( strcasecmp( plugintype, "extendedop" ) == 0 ) {
  1432. *type = SLAPI_PLUGIN_EXTENDEDOP;
  1433. plugin_list_index= PLUGIN_LIST_EXTENDED_OPERATION;
  1434. } else if ( strcasecmp( plugintype, "preoperation" ) == 0 ) {
  1435. *type = SLAPI_PLUGIN_PREOPERATION;
  1436. plugin_list_index= PLUGIN_LIST_PREOPERATION;
  1437. } else if ( strcasecmp( plugintype, "postoperation" ) == 0 ) {
  1438. *type = SLAPI_PLUGIN_POSTOPERATION;
  1439. plugin_list_index= PLUGIN_LIST_POSTOPERATION;
  1440. } else if ( strcasecmp( plugintype, "matchingrule" ) == 0 ) {
  1441. *type = SLAPI_PLUGIN_MATCHINGRULE;
  1442. plugin_list_index= PLUGIN_LIST_MATCHINGRULE;
  1443. } else if ( strcasecmp( plugintype, "syntax" ) == 0 ) {
  1444. *type = SLAPI_PLUGIN_SYNTAX;
  1445. plugin_list_index= PLUGIN_LIST_SYNTAX;
  1446. } else if ( strcasecmp( plugintype, "accesscontrol" ) == 0 ) {
  1447. *type = SLAPI_PLUGIN_ACL;
  1448. plugin_list_index= PLUGIN_LIST_ACL;
  1449. } else if ( strcasecmp( plugintype, "bepreoperation" ) == 0 ) {
  1450. *type = SLAPI_PLUGIN_BEPREOPERATION;
  1451. plugin_list_index= PLUGIN_LIST_BEPREOPERATION;
  1452. } else if ( strcasecmp( plugintype, "bepostoperation" ) == 0 ) {
  1453. *type = SLAPI_PLUGIN_BEPOSTOPERATION;
  1454. plugin_list_index= PLUGIN_LIST_BEPOSTOPERATION;
  1455. } else if ( strcasecmp( plugintype, "betxnpreoperation" ) == 0 ) {
  1456. *type = SLAPI_PLUGIN_BETXNPREOPERATION;
  1457. plugin_list_index= PLUGIN_LIST_BETXNPREOPERATION;
  1458. } else if ( strcasecmp( plugintype, "betxnpostoperation" ) == 0 ) {
  1459. *type = SLAPI_PLUGIN_BETXNPOSTOPERATION;
  1460. plugin_list_index= PLUGIN_LIST_BETXNPOSTOPERATION;
  1461. } else if ( strcasecmp( plugintype, "internalpreoperation" ) == 0 ) {
  1462. *type = SLAPI_PLUGIN_INTERNAL_PREOPERATION;
  1463. plugin_list_index= PLUGIN_LIST_INTERNAL_PREOPERATION;
  1464. } else if ( strcasecmp( plugintype, "internalpostoperation" ) == 0 ) {
  1465. *type = SLAPI_PLUGIN_INTERNAL_POSTOPERATION;
  1466. plugin_list_index= PLUGIN_LIST_INTERNAL_POSTOPERATION;
  1467. } else if ( strcasecmp( plugintype, "entry" ) == 0 ) {
  1468. *type = SLAPI_PLUGIN_ENTRY;
  1469. plugin_list_index= PLUGIN_LIST_ENTRY;
  1470. } else if ( strcasecmp( plugintype, "object" ) == 0 ) {
  1471. *type = SLAPI_PLUGIN_TYPE_OBJECT;
  1472. plugin_list_index= PLUGIN_LIST_OBJECT;
  1473. } else if ( strcasecmp( plugintype, "pwdstoragescheme" ) == 0 ) {
  1474. *type = SLAPI_PLUGIN_PWD_STORAGE_SCHEME;
  1475. plugin_list_index= PLUGIN_LIST_PWD_STORAGE_SCHEME;
  1476. } else if ( strcasecmp( plugintype, "reverpwdstoragescheme" ) == 0 ) {
  1477. *type = SLAPI_PLUGIN_REVER_PWD_STORAGE_SCHEME;
  1478. plugin_list_index= PLUGIN_LIST_REVER_PWD_STORAGE_SCHEME;
  1479. } else if ( strcasecmp( plugintype, "vattrsp" ) == 0 ) {
  1480. *type = SLAPI_PLUGIN_VATTR_SP;
  1481. plugin_list_index= PLUGIN_LIST_VATTR_SP;
  1482. } else if ( strcasecmp( plugintype, "ldbmentryfetchstore" ) == 0 ) {
  1483. *type = SLAPI_PLUGIN_LDBM_ENTRY_FETCH_STORE;
  1484. plugin_list_index= PLUGIN_LIST_LDBM_ENTRY_FETCH_STORE;
  1485. } else if ( strcasecmp( plugintype, "index" ) == 0 ) {
  1486. *type = SLAPI_PLUGIN_INDEX;
  1487. plugin_list_index= PLUGIN_LIST_INDEX;
  1488. } else {
  1489. return( 1 ); /* unknown plugin type - pass to backend */
  1490. }
  1491. if (plugin_list_index >= 0)
  1492. *plugin_list = &global_plugin_list[plugin_list_index];
  1493. return 0;
  1494. }
  1495. static const char *
  1496. plugin_exists(const Slapi_DN *plugin_dn)
  1497. {
  1498. /* check to see if the plugin name is unique */
  1499. const char *retval = 0;
  1500. if (global_plugin_dns && PL_HashTableLookup(global_plugin_dns,
  1501. slapi_sdn_get_ndn(plugin_dn)))
  1502. {
  1503. retval = slapi_sdn_get_dn(plugin_dn);
  1504. }
  1505. return retval;
  1506. }
  1507. static int
  1508. plugin_set_subtree_config(PluginTargetData *subtree_config, const char *val)
  1509. {
  1510. int status = 0;
  1511. if (strcasecmp (val, ALL_DATA) == 0) /* allow access to both local and remote data */
  1512. {
  1513. plugin_set_global (subtree_config);
  1514. }
  1515. else if (strcasecmp (val, LOCAL_DATA) == 0) /* allow access to all locally hosted data */
  1516. {
  1517. ptd_set_special_data (subtree_config, PLGC_DATA_LOCAL);
  1518. }
  1519. else if (strcasecmp (val, REMOTE_DATA) == 0)/* allow access to requests for remote data */
  1520. {
  1521. ptd_set_special_data (subtree_config, PLGC_DATA_REMOTE);
  1522. }
  1523. else /* dn */
  1524. {
  1525. ptd_add_subtree (subtree_config, slapi_sdn_new_dn_byval(val));
  1526. }
  1527. /* I suppose we could check the val at this point to make sure
  1528. its a valid DN . . . */
  1529. return status;
  1530. }
  1531. static int
  1532. set_plugin_config_from_entry(
  1533. const Slapi_Entry *plugin_entry,
  1534. struct slapdplugin *plugin
  1535. )
  1536. {
  1537. struct pluginconfig *config = &plugin->plg_conf;
  1538. char *value = 0;
  1539. char **values = 0;
  1540. int i = 0;
  1541. int status = 0;
  1542. PRBool target_seen = PR_FALSE;
  1543. PRBool bind_seen = PR_FALSE;
  1544. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  1545. ATTR_PLUGIN_SCHEMA_CHECK)) != NULL)
  1546. {
  1547. if (plugin_config_set_action(&config->plgc_schema_check, value))
  1548. {
  1549. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1550. "from entry %s\n", value, ATTR_PLUGIN_SCHEMA_CHECK,
  1551. slapi_entry_get_dn_const(plugin_entry));
  1552. status = 1;
  1553. }
  1554. slapi_ch_free((void**)&value);
  1555. }
  1556. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  1557. ATTR_PLUGIN_LOG_ACCESS)) != NULL)
  1558. {
  1559. if (plugin_config_set_action(&config->plgc_log_access, value))
  1560. {
  1561. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1562. "from entry %s\n", value, ATTR_PLUGIN_LOG_ACCESS,
  1563. slapi_entry_get_dn_const(plugin_entry));
  1564. status = 1;
  1565. }
  1566. slapi_ch_free((void**)&value);
  1567. }
  1568. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  1569. ATTR_PLUGIN_LOG_AUDIT)) != NULL)
  1570. {
  1571. if (plugin_config_set_action(&config->plgc_log_audit, value))
  1572. {
  1573. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1574. "from entry %s\n", value, ATTR_PLUGIN_LOG_AUDIT,
  1575. slapi_entry_get_dn_const(plugin_entry));
  1576. status = 1;
  1577. }
  1578. slapi_ch_free((void**)&value);
  1579. }
  1580. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  1581. ATTR_PLUGIN_INVOKE_FOR_REPLOP)) != NULL)
  1582. {
  1583. if (plugin_config_set_action(&config->plgc_invoke_for_replop, value))
  1584. {
  1585. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1586. "from entry %s\n", value, ATTR_PLUGIN_INVOKE_FOR_REPLOP,
  1587. slapi_entry_get_dn_const(plugin_entry));
  1588. status = 1;
  1589. }
  1590. slapi_ch_free((void**)&value);
  1591. }
  1592. values = slapi_entry_attr_get_charray(plugin_entry,
  1593. ATTR_PLUGIN_TARGET_SUBTREE);
  1594. for (i=0; values && values[i]; i++)
  1595. {
  1596. if (plugin_set_subtree_config(&(config->plgc_target_subtrees), values[i]))
  1597. {
  1598. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1599. "from entry %s\n", values[i], ATTR_PLUGIN_TARGET_SUBTREE,
  1600. slapi_entry_get_dn_const(plugin_entry));
  1601. status = 1;
  1602. break;
  1603. }
  1604. else
  1605. {
  1606. target_seen = PR_TRUE;
  1607. }
  1608. }
  1609. slapi_ch_array_free(values);
  1610. values = slapi_entry_attr_get_charray(plugin_entry,
  1611. ATTR_PLUGIN_EXCLUDE_TARGET_SUBTREE);
  1612. for (i=0; values && values[i]; i++)
  1613. {
  1614. if (plugin_set_subtree_config(&(config->plgc_excluded_target_subtrees), values[i]))
  1615. {
  1616. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1617. "from entry %s\n", values[i], ATTR_PLUGIN_EXCLUDE_TARGET_SUBTREE,
  1618. slapi_entry_get_dn_const(plugin_entry));
  1619. status = 1;
  1620. break;
  1621. }
  1622. }
  1623. slapi_ch_array_free(values);
  1624. values = slapi_entry_attr_get_charray(plugin_entry,
  1625. ATTR_PLUGIN_BIND_SUBTREE);
  1626. for (i=0; values && values[i]; i++)
  1627. {
  1628. if (plugin_set_subtree_config(&(config->plgc_bind_subtrees), values[i]))
  1629. {
  1630. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1631. "from entry %s\n", values[i], ATTR_PLUGIN_BIND_SUBTREE,
  1632. slapi_entry_get_dn_const(plugin_entry));
  1633. status = 1;
  1634. break;
  1635. }
  1636. else
  1637. {
  1638. bind_seen = PR_TRUE;
  1639. }
  1640. }
  1641. slapi_ch_array_free(values);
  1642. values = slapi_entry_attr_get_charray(plugin_entry,
  1643. ATTR_PLUGIN_EXCLUDE_BIND_SUBTREE);
  1644. for (i=0; values && values[i]; i++)
  1645. {
  1646. if (plugin_set_subtree_config(&(config->plgc_excluded_bind_subtrees), values[i]))
  1647. {
  1648. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: invalid value %s for attribute %s "
  1649. "from entry %s\n", values[i], ATTR_PLUGIN_EXCLUDE_BIND_SUBTREE,
  1650. slapi_entry_get_dn_const(plugin_entry));
  1651. status = 1;
  1652. break;
  1653. }
  1654. }
  1655. slapi_ch_array_free(values);
  1656. /* set target subtree default - allow access to all data */
  1657. if (!target_seen)
  1658. {
  1659. plugin_set_global(&(config->plgc_target_subtrees));
  1660. }
  1661. /* set bind subtree default - allow access to local data only */
  1662. if (!bind_seen)
  1663. {
  1664. ptd_set_special_data(&(config->plgc_bind_subtrees), PLGC_DATA_LOCAL);
  1665. ptd_set_special_data(&(config->plgc_bind_subtrees), PLGC_DATA_REMOTE);
  1666. }
  1667. return status;
  1668. }
  1669. /* This function is called after the plugin init function has been called
  1670. which fills in the desc part of the plugin
  1671. */
  1672. static int
  1673. add_plugin_description(Slapi_Entry *e, const char *attrname, char *val)
  1674. {
  1675. struct berval desc;
  1676. struct berval *newval[2] = {0, 0};
  1677. int status = 0;
  1678. desc.bv_val = SLAPI_PLUGIN_NONE_IF_NULL( val );
  1679. desc.bv_len = strlen(desc.bv_val);
  1680. newval[0] = &desc;
  1681. if ((status = entry_replace_values(e, attrname, newval)) != 0)
  1682. {
  1683. LDAPDebug(LDAP_DEBUG_PLUGIN, "Error: failed to add value %s to "
  1684. "attribute %s of entry %s\n", val, attrname,
  1685. slapi_entry_get_dn_const(e));
  1686. status = 1;
  1687. }
  1688. return status;
  1689. }
  1690. /*
  1691. * The plugin initfunc sets some vendor and version information in the plugin.
  1692. * This function extracts that and adds it as attributes to `e'. If
  1693. * `plugin' is NULL, the plugin is located based on the DN in `e'.
  1694. *
  1695. * Returns 0 if all goes well and 1 if not.
  1696. */
  1697. int
  1698. plugin_add_descriptive_attributes( Slapi_Entry *e, struct slapdplugin *plugin )
  1699. {
  1700. int status = 0;
  1701. if ( NULL == plugin ) {
  1702. int i;
  1703. const Slapi_DN *ednp = slapi_entry_get_sdn_const( e );
  1704. Slapi_DN pdn;
  1705. struct slapdplugin *plugtmp;
  1706. for( i = 0; NULL == plugin && i < PLUGIN_LIST_GLOBAL_MAX; ++i )
  1707. {
  1708. for ( plugtmp = global_plugin_list[i]; NULL == plugin && plugtmp;
  1709. plugtmp = plugtmp->plg_next)
  1710. {
  1711. slapi_sdn_init_normdn_byref( &pdn, plugtmp->plg_dn );
  1712. if ( 0 == slapi_sdn_compare( &pdn, ednp ))
  1713. {
  1714. plugin = plugtmp;
  1715. }
  1716. slapi_sdn_done( &pdn );
  1717. }
  1718. }
  1719. if ( NULL == plugin )
  1720. {
  1721. /* This can happen for things such as disabled syntax plug-ins. We
  1722. * just treat this as a warning to allow the description attributes
  1723. * to be set to a default value to avoid an objectclass violation. */
  1724. LDAPDebug(LDAP_DEBUG_PLUGIN,
  1725. "Warning: couldn't find plugin %s in global list. "
  1726. "Adding default descriptive values.\n",
  1727. slapi_entry_get_dn_const(e), 0, 0 );
  1728. }
  1729. }
  1730. if (add_plugin_description(e, ATTR_PLUGIN_PLUGINID,
  1731. plugin ? plugin->plg_desc.spd_id : NULL))
  1732. {
  1733. status = 1;
  1734. }
  1735. if (add_plugin_description(e, ATTR_PLUGIN_VERSION,
  1736. plugin ? plugin->plg_desc.spd_version : NULL))
  1737. {
  1738. status = 1;
  1739. }
  1740. if (add_plugin_description(e, ATTR_PLUGIN_VENDOR,
  1741. plugin ? plugin->plg_desc.spd_vendor: NULL))
  1742. {
  1743. status = 1;
  1744. }
  1745. if (add_plugin_description(e, ATTR_PLUGIN_DESC,
  1746. plugin ? plugin->plg_desc.spd_description : NULL))
  1747. {
  1748. status = 1;
  1749. }
  1750. return status;
  1751. }
  1752. /*
  1753. clean up the memory associated with the plugin
  1754. */
  1755. static void
  1756. plugin_free(struct slapdplugin *plugin)
  1757. {
  1758. charray_free(plugin->plg_argv);
  1759. slapi_ch_free((void**)&plugin->plg_libpath);
  1760. slapi_ch_free((void**)&plugin->plg_initfunc);
  1761. slapi_ch_free((void**)&plugin->plg_name);
  1762. slapi_ch_free((void**)&plugin->plg_dn);
  1763. if (!plugin->plg_group)
  1764. plugin_config_cleanup(&plugin->plg_conf);
  1765. slapi_ch_free((void**)&plugin);
  1766. }
  1767. /***********************************
  1768. This is the main entry point for plugin configuration. The plugin_entry argument
  1769. should already contain the necessary fields required to initialize the plugin and
  1770. to give it a proper name in the plugin configuration DIT.
  1771. Argument:
  1772. Slapi_Entry *plugin_entry - the required attributes are
  1773. dn: the dn of the plugin entry
  1774. cn: the unique name of the plugin
  1775. nsslapd-pluginType: one of the several recognized plugin types e.g. "postoperation"
  1776. if p_initfunc is given, pluginPath and pluginInitFunc are optional
  1777. nsslapd-pluginPath: full path and file name of the dll implementing the plugin
  1778. nsslapd-pluginInitFunc: the name of the plugin initialization function
  1779. the optional attributes are:
  1780. nsslapd-pluginArg0
  1781. ...
  1782. nsslapd-pluginArg[N-1] - the (old style) arguments to the plugin, where N varies
  1783. from 0 to the number of arguments. The numbers must be consecutive i.e. no
  1784. skipping
  1785. Instead of using nsslapd-pluginArgN, it is encouraged for you to use named
  1786. parameters e.g.
  1787. nsslapd-tweakThis: 1
  1788. nsslapd-tweakThat: 2
  1789. etc.
  1790. nsslapd-pluginEnabled: "on"|"off" - by default, the plugin will be enabled unless
  1791. this attribute is present and has the value "off"
  1792. for other known attributes, see set_plugin_config_from_entry() above
  1793. all other attributes will be ignored
  1794. The reason this parameter is not const is because it may be modified. This
  1795. function will modify it if the plugin init function is called successfully
  1796. to add the description attributes, and the plugin init function may modify
  1797. it as well.
  1798. Argument:
  1799. group - the group to which this plugin will belong - each member of a plugin group
  1800. shares the pluginconfig of the group leader; refer to the function plugin_get_config
  1801. for more information
  1802. Argument:
  1803. add_entry - if true, the entry will be added to the DIT using the given
  1804. DN in the plugin_entry - this is the default behavior; if false, the
  1805. plugin entry will not show up in the DIT
  1806. ************************************/
  1807. int
  1808. plugin_setup(Slapi_Entry *plugin_entry, struct slapi_componentid *group,
  1809. slapi_plugin_init_fnptr p_initfunc, int add_entry)
  1810. {
  1811. int ii = 0;
  1812. char attrname[BUFSIZ];
  1813. char *value = 0;
  1814. struct slapdplugin *plugin = NULL;
  1815. struct slapdplugin **plugin_list = NULL;
  1816. struct slapi_componentid *cid=NULL;
  1817. const char *existname = 0;
  1818. slapi_plugin_init_fnptr initfunc = p_initfunc;
  1819. Slapi_PBlock pb;
  1820. int status = 0;
  1821. int enabled = 1;
  1822. char *configdir = 0;
  1823. attrname[0] = '\0';
  1824. if (!slapi_entry_get_sdn_const(plugin_entry))
  1825. {
  1826. LDAPDebug(LDAP_DEBUG_ANY, "Error: DN is missing from the plugin.\n",
  1827. 0, 0, 0);
  1828. return -1;
  1829. }
  1830. if ((existname = plugin_exists(slapi_entry_get_sdn_const(plugin_entry))) != NULL)
  1831. {
  1832. LDAPDebug(LDAP_DEBUG_ANY, "Error: the plugin named %s "
  1833. "already exists.\n", existname, 0, 0);
  1834. return -1;
  1835. }
  1836. /*
  1837. * create a new plugin structure, fill it in, and prepare to
  1838. * call the plugin's init function. the init function will
  1839. * set the plugin function pointers.
  1840. */
  1841. plugin = (struct slapdplugin *)slapi_ch_calloc(1, sizeof(struct slapdplugin));
  1842. plugin->plg_dn = slapi_ch_strdup(slapi_entry_get_dn_const(plugin_entry));
  1843. plugin->plg_closed = 0;
  1844. if (!(value = slapi_entry_attr_get_charptr(plugin_entry,
  1845. ATTR_PLUGIN_TYPE)))
  1846. {
  1847. /* error: required attribute %s missing */
  1848. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing "
  1849. "from entry \"%s\"\n", ATTR_PLUGIN_TYPE,
  1850. slapi_entry_get_dn_const(plugin_entry), 0);
  1851. status = -1;
  1852. goto PLUGIN_CLEANUP;
  1853. }
  1854. else
  1855. {
  1856. status = plugin_get_type_and_list(value, &plugin->plg_type,
  1857. &plugin_list);
  1858. if ( status != 0 ) {
  1859. /* error: unknown plugin type */
  1860. LDAPDebug(LDAP_DEBUG_ANY, "Error: unknown plugin type \"%s\" "
  1861. "in entry \"%s\"\n",
  1862. value, slapi_entry_get_dn_const(plugin_entry), 0);
  1863. slapi_ch_free((void**)&value);
  1864. status = -1;
  1865. goto PLUGIN_CLEANUP;
  1866. }
  1867. slapi_ch_free((void**)&value);
  1868. }
  1869. if (!status &&
  1870. !(value = slapi_entry_attr_get_charptr(plugin_entry, "cn")))
  1871. {
  1872. /* error: required attribute %s missing */
  1873. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing "
  1874. "from entry \"%s\"\n", "cn",
  1875. slapi_entry_get_dn_const(plugin_entry), 0);
  1876. status = -1;
  1877. goto PLUGIN_CLEANUP;
  1878. }
  1879. else
  1880. {
  1881. /* plg_name is normalized once here */
  1882. plugin->plg_name = slapi_create_rdn_value("%s", value);
  1883. slapi_ch_free((void**)&value);
  1884. }
  1885. if (!(value = slapi_entry_attr_get_charptr(plugin_entry, ATTR_PLUGIN_PRECEDENCE)))
  1886. {
  1887. /* A precedence isn't set, so just use the default. */
  1888. plugin->plg_precedence = PLUGIN_DEFAULT_PRECEDENCE;
  1889. }
  1890. else
  1891. {
  1892. /* A precedence was set, so let's make sure it's valid. */
  1893. int precedence = 0;
  1894. char *endptr = NULL;
  1895. /* Convert the value. */
  1896. precedence = strtol(value, &endptr, 10);
  1897. /* Make sure the precedence is within our valid
  1898. * range and that we had no conversion errors. */
  1899. if ((*value == '\0') || (*endptr != '\0') ||
  1900. (precedence < PLUGIN_MIN_PRECEDENCE) || (precedence > PLUGIN_MAX_PRECEDENCE))
  1901. {
  1902. LDAPDebug(LDAP_DEBUG_ANY, "Error: value for attribute %s must be "
  1903. "an integer between %d and %d\n", ATTR_PLUGIN_PRECEDENCE,
  1904. PLUGIN_MIN_PRECEDENCE, PLUGIN_MAX_PRECEDENCE);
  1905. status = -1;
  1906. slapi_ch_free((void**)&value);
  1907. goto PLUGIN_CLEANUP;
  1908. }
  1909. else
  1910. {
  1911. plugin->plg_precedence = precedence;
  1912. }
  1913. slapi_ch_free((void**)&value);
  1914. }
  1915. if (!(value = slapi_entry_attr_get_charptr(plugin_entry,
  1916. ATTR_PLUGIN_INITFN)))
  1917. {
  1918. if (!initfunc)
  1919. {
  1920. /* error: required attribute %s missing */
  1921. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing "
  1922. "from entry \"%s\"\n", ATTR_PLUGIN_INITFN,
  1923. slapi_entry_get_dn_const(plugin_entry), 0);
  1924. status = -1;
  1925. goto PLUGIN_CLEANUP;
  1926. }
  1927. }
  1928. else
  1929. {
  1930. plugin->plg_initfunc = value; /* plugin owns value's memory now, don't free */
  1931. }
  1932. if (!initfunc)
  1933. {
  1934. PRBool loadNow = PR_FALSE;
  1935. PRBool loadGlobal = PR_FALSE;
  1936. if (!(value = slapi_entry_attr_get_charptr(plugin_entry,
  1937. ATTR_PLUGIN_PATH)))
  1938. {
  1939. /* error: required attribute %s missing */
  1940. LDAPDebug(LDAP_DEBUG_ANY, "Error: required attribute %s is missing "
  1941. "from entry \"%s\"\n", ATTR_PLUGIN_PATH,
  1942. slapi_entry_get_dn_const(plugin_entry), 0);
  1943. status = -1;
  1944. goto PLUGIN_CLEANUP;
  1945. }
  1946. else
  1947. {
  1948. plugin->plg_libpath = value; /* plugin owns value's memory now, don't free */
  1949. }
  1950. loadNow = slapi_entry_attr_get_bool(plugin_entry, ATTR_PLUGIN_LOAD_NOW);
  1951. loadGlobal = slapi_entry_attr_get_bool(plugin_entry, ATTR_PLUGIN_LOAD_GLOBAL);
  1952. /*
  1953. * load the plugin's init function
  1954. */
  1955. if ((initfunc = (slapi_plugin_init_fnptr)sym_load_with_flags(plugin->plg_libpath,
  1956. plugin->plg_initfunc, plugin->plg_name, 1 /* report errors */,
  1957. loadNow, loadGlobal)) == NULL)
  1958. {
  1959. status = -1;
  1960. goto PLUGIN_CLEANUP;
  1961. }
  1962. #ifdef _WIN32
  1963. {
  1964. set_debug_level_fn_t fn;
  1965. /* for Win32 only, attempt to get its debug level init function */
  1966. if ((fn = (set_debug_level_fn_t)sym_load(plugin->plg_libpath,
  1967. "plugin_init_debug_level", plugin->plg_name,
  1968. 0 /* do not report errors */ )) != NULL) {
  1969. /* we hooked the function, so call it */
  1970. (*fn)(module_ldap_debug);
  1971. }
  1972. }
  1973. #endif
  1974. }
  1975. if (!status && group) /* uses group's config; see plugin_get_config */
  1976. {
  1977. struct slapi_componentid * cid = (struct slapi_componentid *) group;
  1978. plugin->plg_group = (struct slapdplugin *) cid->sci_plugin;
  1979. }
  1980. else if (!status) /* using own config */
  1981. {
  1982. plugin_config_init(&(plugin->plg_conf));
  1983. set_plugin_config_from_entry(plugin_entry, plugin);
  1984. }
  1985. /* add the plugin arguments */
  1986. value = 0;
  1987. ii = 0;
  1988. PR_snprintf(attrname, sizeof(attrname), "%s%d", ATTR_PLUGIN_ARG, ii);
  1989. while ((value = slapi_entry_attr_get_charptr(plugin_entry, attrname)) != NULL)
  1990. {
  1991. charray_add(&plugin->plg_argv, value);
  1992. plugin->plg_argc++;
  1993. ++ii;
  1994. PR_snprintf(attrname, sizeof(attrname), "%s%d", ATTR_PLUGIN_ARG, ii);
  1995. }
  1996. memset((char *)&pb, '\0', sizeof(pb));
  1997. slapi_pblock_set(&pb, SLAPI_PLUGIN, plugin);
  1998. slapi_pblock_set(&pb, SLAPI_PLUGIN_VERSION, (void *)SLAPI_PLUGIN_CURRENT_VERSION);
  1999. cid = generate_componentid (plugin,NULL);
  2000. slapi_pblock_set(&pb, SLAPI_PLUGIN_IDENTITY, (void*)cid);
  2001. configdir = config_get_configdir();
  2002. slapi_pblock_set(&pb, SLAPI_CONFIG_DIRECTORY, configdir);
  2003. /* see if the plugin is enabled or not */
  2004. if ((value = slapi_entry_attr_get_charptr(plugin_entry,
  2005. ATTR_PLUGIN_ENABLED)) &&
  2006. !strcasecmp(value, "off"))
  2007. {
  2008. enabled = 0;
  2009. }
  2010. else
  2011. {
  2012. enabled = 1;
  2013. }
  2014. slapi_pblock_set(&pb, SLAPI_PLUGIN_ENABLED, &enabled);
  2015. slapi_pblock_set(&pb, SLAPI_PLUGIN_CONFIG_ENTRY, plugin_entry);
  2016. if (enabled && (*initfunc)(&pb) != 0)
  2017. {
  2018. LDAPDebug(LDAP_DEBUG_ANY, "Init function \"%s\" for \"%s\" plugin"
  2019. " in library \"%s\" failed\n",
  2020. plugin->plg_initfunc, plugin->plg_name,
  2021. plugin->plg_libpath);
  2022. status = -1;
  2023. slapi_ch_free((void**)&value);
  2024. goto PLUGIN_CLEANUP;
  2025. }
  2026. if ( !status ) {
  2027. status = plugin_add_descriptive_attributes( plugin_entry, plugin );
  2028. }
  2029. slapi_ch_free((void**)&value);
  2030. if(enabled)
  2031. {
  2032. /* don't use raw pointer from plugin_entry because it
  2033. will be freed later by the caller */
  2034. Slapi_DN *dn_copy = slapi_sdn_dup(slapi_entry_get_sdn_const(plugin_entry));
  2035. add_plugin_to_list(plugin_list, plugin);
  2036. add_plugin_entry_dn(dn_copy);
  2037. }
  2038. if (add_entry)
  2039. {
  2040. /* make a copy of the plugin entry for our own use because it will
  2041. be freed later by the caller */
  2042. Slapi_Entry *e_copy = slapi_entry_dup(plugin_entry);
  2043. /* new_plugin_entry(&plugin_entries, plugin_entry, plugin); */
  2044. new_plugin_entry(&dep_plugin_entries, e_copy, plugin);
  2045. }
  2046. PLUGIN_CLEANUP:
  2047. if (status)
  2048. plugin_free(plugin);
  2049. slapi_ch_free((void **)&configdir);
  2050. return status;
  2051. }
  2052. /* set default configuration parameters */
  2053. static void
  2054. plugin_config_init (struct pluginconfig *config)
  2055. {
  2056. PR_ASSERT (config);
  2057. ptd_init (&config->plgc_target_subtrees);
  2058. ptd_init (&config->plgc_excluded_target_subtrees);
  2059. ptd_init (&config->plgc_bind_subtrees);
  2060. ptd_init (&config->plgc_excluded_bind_subtrees);
  2061. config->plgc_schema_check = PLGC_ON;
  2062. config->plgc_invoke_for_replop = PLGC_ON;
  2063. /* currently, we leave it up to plugin, but don't actually tell plugins that they can choose.
  2064. We want changes to always be logged by regular plugins to avoid data inconsistency, but we
  2065. want to allow internal plugins like replication to make the decision.*/
  2066. config->plgc_log_change = PLGC_UPTOPLUGIN;
  2067. config->plgc_log_access = PLGC_OFF;
  2068. config->plgc_log_audit = PLGC_OFF;
  2069. }
  2070. static int
  2071. plugin_config_set_action (int *action, char *value)
  2072. {
  2073. PR_ASSERT (action);
  2074. PR_ASSERT (value);
  2075. if (strcasecmp (value, "on") == 0)
  2076. {
  2077. *action = PLGC_ON;
  2078. }
  2079. else if (strcasecmp (value, "off") == 0)
  2080. {
  2081. *action = PLGC_OFF;
  2082. }
  2083. else if (strcasecmp (value, "uptoplugin") == 0)
  2084. {
  2085. *action = PLGC_UPTOPLUGIN;
  2086. }
  2087. else
  2088. {
  2089. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  2090. "plugin_config_set_action: invalid action %s\n", value);
  2091. return -1;
  2092. }
  2093. return 0;
  2094. }
  2095. static void
  2096. plugin_config_cleanup (struct pluginconfig *config)
  2097. {
  2098. PR_ASSERT (config);
  2099. ptd_cleanup (&config->plgc_target_subtrees);
  2100. ptd_cleanup (&config->plgc_excluded_target_subtrees);
  2101. ptd_cleanup (&config->plgc_bind_subtrees);
  2102. ptd_cleanup (&config->plgc_excluded_bind_subtrees);
  2103. }
  2104. #if 0
  2105. static char*
  2106. plugin_config_action_to_string (int action)
  2107. {
  2108. switch (action)
  2109. {
  2110. case PLGC_ON: return "on";
  2111. case PLGC_OFF: return "off";
  2112. case PLGC_UPTOPLUGIN: return "uptoplugin";
  2113. default: return NULL;
  2114. }
  2115. }
  2116. #endif
  2117. static struct pluginconfig*
  2118. plugin_get_config (struct slapdplugin *plugin)
  2119. {
  2120. struct slapdplugin *temp = plugin;
  2121. PR_ASSERT (plugin);
  2122. while (temp->plg_group)
  2123. {
  2124. temp = temp->plg_group;
  2125. }
  2126. return &(temp->plg_conf);
  2127. }
  2128. static PRBool
  2129. plugin_invoke_plugin_pb (struct slapdplugin *plugin, int operation, Slapi_PBlock *pb)
  2130. {
  2131. Slapi_DN *target_spec;
  2132. PRBool rc;
  2133. PR_ASSERT (plugin);
  2134. PR_ASSERT (pb);
  2135. /* we always allow initialization and cleanup operations */
  2136. if (operation == SLAPI_PLUGIN_START_FN ||
  2137. operation == SLAPI_PLUGIN_POSTSTART_FN ||
  2138. operation == SLAPI_PLUGIN_CLOSE_FN ||
  2139. operation == SLAPI_PLUGIN_CLEANUP_FN ||
  2140. operation == SLAPI_PLUGIN_BE_PRE_CLOSE_FN ||
  2141. operation == SLAPI_PLUGIN_BE_POST_OPEN_FN ||
  2142. operation == SLAPI_PLUGIN_BE_PRE_BACKUP_FN ||
  2143. operation == SLAPI_PLUGIN_BE_POST_BACKUP_FN)
  2144. return PR_TRUE;
  2145. PR_ASSERT (pb->pb_op);
  2146. target_spec = operation_get_target_spec (pb->pb_op);
  2147. PR_ASSERT (target_spec);
  2148. rc = plugin_invoke_plugin_sdn (plugin, operation, pb, target_spec);
  2149. return rc;
  2150. }
  2151. PRBool
  2152. plugin_invoke_plugin_sdn (struct slapdplugin *plugin, int operation, Slapi_PBlock *pb, Slapi_DN *target_spec)
  2153. {
  2154. PluginTargetData *ptd;
  2155. PluginTargetData *excludedPtd;
  2156. struct pluginconfig *config;
  2157. Slapi_Backend *be;
  2158. int isroot;
  2159. PRBool islocal;
  2160. PRBool bindop;
  2161. unsigned long op;
  2162. int method = -1;
  2163. PR_ASSERT (plugin);
  2164. if (!pb) {
  2165. LDAPDebug(LDAP_DEBUG_ANY, "plugin_invoke_plugin_sdn: NULL pblock.\n", 0, 0, 0);
  2166. return PR_FALSE;
  2167. }
  2168. /* get configuration from the group plugin if necessary */
  2169. config = plugin_get_config (plugin);
  2170. slapi_pblock_get(pb, SLAPI_BIND_METHOD, &method);
  2171. /* check if plugin is configured to service replicated operations */
  2172. if (!config->plgc_invoke_for_replop)
  2173. {
  2174. int repl_op;
  2175. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  2176. if (repl_op)
  2177. return PR_FALSE;
  2178. }
  2179. if (pb->pb_op)
  2180. {
  2181. op = operation_get_type(pb->pb_op);
  2182. if (op == SLAPI_OPERATION_BIND || op == SLAPI_OPERATION_UNBIND)
  2183. {
  2184. bindop = PR_TRUE;
  2185. }
  2186. else
  2187. {
  2188. bindop = PR_FALSE;
  2189. }
  2190. slapi_pblock_get (pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  2191. }
  2192. else
  2193. {
  2194. bindop = PR_FALSE;
  2195. isroot = 1;
  2196. }
  2197. slapi_pblock_get (pb, SLAPI_BACKEND, &be);
  2198. /* determine whether data are local or remote */
  2199. /* remote if chaining backend or default backend */
  2200. if ( be!=NULL ) {
  2201. islocal=!(slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA));
  2202. } else {
  2203. islocal = be != defbackend_get_backend();
  2204. }
  2205. if (bindop)
  2206. {
  2207. ptd = &(config->plgc_bind_subtrees);
  2208. excludedPtd = &(config->plgc_excluded_bind_subtrees);
  2209. }
  2210. else
  2211. {
  2212. ptd = &(config->plgc_target_subtrees);
  2213. excludedPtd = &(config->plgc_excluded_target_subtrees);
  2214. }
  2215. if (plugin_matches_operation (target_spec, excludedPtd, bindop, isroot, islocal, method) == PR_TRUE) {
  2216. return PR_FALSE;
  2217. }
  2218. return plugin_matches_operation (target_spec, ptd, bindop, isroot, islocal, method);
  2219. }
  2220. /* this interface is exposed to be used by internal operations.
  2221. */
  2222. char* plugin_get_dn (const struct slapdplugin *plugin)
  2223. {
  2224. char *plugindn = NULL;
  2225. char *pattern = "cn=%s," PLUGIN_BASE_DN; /* cn=plugins,cn=config */
  2226. if (plugin == NULL) /* old plugin that does not pass identity - use default */
  2227. plugin = &global_default_plg;
  2228. if (plugin->plg_name == NULL)
  2229. return NULL;
  2230. /* plg_name is normalized in plugin_setup. So, we can use smprintf */
  2231. plugindn = slapi_ch_smprintf(pattern, plugin->plg_name);
  2232. if (NULL == plugindn) {
  2233. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  2234. "plugin_get_dn: failed to create plugin dn "
  2235. "(plugin name: %s)\n", plugin->plg_name);
  2236. return NULL;
  2237. }
  2238. return plugindn;
  2239. }
  2240. static PRBool plugin_is_global (const PluginTargetData *ptd)
  2241. {
  2242. /* plugin is considered to be global if it is invoked for
  2243. global data, local data and anonymous bind (bind target
  2244. data only). We don't include directory manager here
  2245. as it is considered to be part of local data */
  2246. return (ptd_is_special_data_set (ptd, PLGC_DATA_LOCAL) &&
  2247. ptd_is_special_data_set (ptd, PLGC_DATA_REMOTE) &&
  2248. ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ANONYMOUS) &&
  2249. ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ROOT));
  2250. }
  2251. static void plugin_set_global (PluginTargetData *ptd)
  2252. {
  2253. PR_ASSERT (ptd);
  2254. /* plugin is global if it is allowed access to all data */
  2255. ptd_set_special_data (ptd, PLGC_DATA_LOCAL);
  2256. ptd_set_special_data (ptd, PLGC_DATA_REMOTE);
  2257. ptd_set_special_data (ptd, PLGC_DATA_BIND_ANONYMOUS);
  2258. ptd_set_special_data (ptd, PLGC_DATA_BIND_ROOT);
  2259. }
  2260. static void plugin_set_default_access (struct pluginconfig *config)
  2261. {
  2262. /* by default, plugins are invoked if dn is local for bind operations,
  2263. and for all requests for all other operations */
  2264. PR_ASSERT (config);
  2265. plugin_set_global (&config->plgc_target_subtrees);
  2266. ptd_set_special_data (&config->plgc_bind_subtrees, PLGC_DATA_LOCAL);
  2267. ptd_set_special_data (&config->plgc_bind_subtrees, PLGC_DATA_REMOTE);
  2268. }
  2269. /* determine whether operation should be allowed based on plugin configuration */
  2270. PRBool plugin_allow_internal_op (Slapi_DN *target_spec, struct slapdplugin *plugin)
  2271. {
  2272. struct pluginconfig *config = plugin_get_config (plugin);
  2273. Slapi_Backend *be;
  2274. int islocal;
  2275. if (plugin_is_global (&config->plgc_excluded_target_subtrees))
  2276. return PR_FALSE;
  2277. if (plugin_is_global (&config->plgc_target_subtrees))
  2278. return PR_TRUE;
  2279. /* ONREPL - we do be_select to decide whether the request is for local
  2280. or remote data. We might need to reconsider how to do this
  2281. for performance reasons since be_select will be done again
  2282. once the operation goes through */
  2283. be = slapi_be_select(target_spec);
  2284. /* determine whether data are local or remote */
  2285. /* remote if chaining backend or default backend */
  2286. if ( be!=NULL ) {
  2287. islocal=!(slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA));
  2288. } else {
  2289. islocal = be != defbackend_get_backend();
  2290. }
  2291. /* SIMPLE auth method sends us through original code path in plugin_mathches_operation */
  2292. if (plugin_matches_operation (target_spec, &config->plgc_excluded_target_subtrees,
  2293. PR_FALSE, PR_FALSE, islocal, LDAP_AUTH_SIMPLE) == PR_TRUE) {
  2294. return PR_FALSE;
  2295. }
  2296. return plugin_matches_operation (target_spec, &config->plgc_target_subtrees,
  2297. PR_FALSE, PR_FALSE, islocal, LDAP_AUTH_SIMPLE);
  2298. }
  2299. static PRBool plugin_matches_operation (Slapi_DN *target_spec, PluginTargetData *ptd,
  2300. PRBool bindop, PRBool isroot, PRBool islocal, int method)
  2301. {
  2302. int cookie;
  2303. Slapi_DN *subtree;
  2304. /* check for special cases */
  2305. if (plugin_is_global (ptd))
  2306. return PR_TRUE;
  2307. /* if method is SASL we can have a null DN so bypass this check*/
  2308. if(method != LDAP_AUTH_SASL) {
  2309. if (bindop && target_spec && (slapi_sdn_get_dn (target_spec) == NULL ||
  2310. slapi_sdn_get_dn (target_spec)[0] == '\0'))
  2311. {
  2312. return (ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ANONYMOUS));
  2313. }
  2314. }
  2315. /* check for root bind */
  2316. if (bindop && isroot)
  2317. {
  2318. return (ptd_is_special_data_set (ptd, PLGC_DATA_BIND_ROOT));
  2319. }
  2320. /* check for local data */
  2321. if (ptd_is_special_data_set (ptd, PLGC_DATA_LOCAL) && islocal)
  2322. {
  2323. return PR_TRUE;
  2324. }
  2325. /* check for remote data */
  2326. if (ptd_is_special_data_set (ptd, PLGC_DATA_REMOTE) && !islocal)
  2327. {
  2328. return (PR_TRUE);
  2329. }
  2330. subtree = ptd_get_first_subtree (ptd, &cookie);
  2331. while (subtree)
  2332. {
  2333. if (slapi_sdn_issuffix (target_spec, subtree))
  2334. return (PR_TRUE);
  2335. subtree = ptd_get_next_subtree (ptd, &cookie);
  2336. }
  2337. return PR_FALSE;
  2338. }
  2339. /* build operation action bitmap based on plugin configuration and actions specified for the operation */
  2340. int plugin_build_operation_action_bitmap (int input_actions, const struct slapdplugin *plugin)
  2341. {
  2342. int result_actions = 0;
  2343. /* old plugin that does not pass its identity to the operation */
  2344. if (plugin == NULL)
  2345. plugin = &global_default_plg;
  2346. if (plugin->plg_conf.plgc_log_access)
  2347. result_actions |= OP_FLAG_ACTION_LOG_ACCESS;
  2348. if (plugin->plg_conf.plgc_log_audit)
  2349. result_actions |= OP_FLAG_ACTION_LOG_AUDIT;
  2350. /*
  2351. * OP_FLAG_ACTION_INVOKE_FOR_REPLOP is now used only by URP code.
  2352. * If someday this code needs to reclaim the flag, it has to use
  2353. * another flag to avoid the conflict with URP code.
  2354. *
  2355. * if (plugin->plg_conf.plgc_invoke_for_replop)
  2356. * result_actions |= OP_FLAG_ACTION_INVOKE_FOR_REPLOP;
  2357. */
  2358. switch (plugin->plg_conf.plgc_schema_check)
  2359. {
  2360. case PLGC_OFF: result_actions &= ~OP_FLAG_ACTION_SCHEMA_CHECK;
  2361. break;
  2362. case PLGC_ON: result_actions |= OP_FLAG_ACTION_SCHEMA_CHECK;
  2363. break;
  2364. case PLGC_UPTOPLUGIN: break;
  2365. default: PR_ASSERT (PR_FALSE);
  2366. }
  2367. switch (plugin->plg_conf.plgc_log_change)
  2368. {
  2369. case PLGC_OFF: result_actions &= ~OP_FLAG_ACTION_LOG_CHANGES;
  2370. break;
  2371. case PLGC_ON: result_actions |= OP_FLAG_ACTION_LOG_CHANGES;
  2372. break;
  2373. case PLGC_UPTOPLUGIN: break;
  2374. default: PR_ASSERT (PR_FALSE);
  2375. }
  2376. return result_actions;
  2377. }
  2378. const struct slapdplugin*
  2379. plugin_get_server_plg()
  2380. {
  2381. if(!global_server_plg_initialised)
  2382. {
  2383. global_server_plg.plg_name = "server";
  2384. plugin_set_global (&global_server_plg.plg_conf.plgc_target_subtrees);
  2385. global_server_plg.plg_conf.plgc_log_access = 1;
  2386. global_server_plg.plg_conf.plgc_log_audit = 1;
  2387. global_server_plg.plg_conf.plgc_schema_check = 1;
  2388. global_server_plg.plg_conf.plgc_log_change = 1;
  2389. global_server_plg_initialised= 1;
  2390. global_server_plg_initialised= 1;
  2391. }
  2392. return &global_server_plg;
  2393. }
  2394. struct slapi_componentid * plugin_get_default_component_id() {
  2395. if(!global_server_plg_id_initialised) {
  2396. global_server_id_plg.sci_plugin=plugin_get_server_plg();
  2397. global_server_id_plg.sci_component_name=
  2398. plugin_get_dn(global_server_id_plg.sci_plugin);
  2399. global_server_plg_id_initialised=1;
  2400. }
  2401. return &global_server_id_plg;
  2402. }
  2403. static void
  2404. default_plugin_init()
  2405. {
  2406. global_default_plg.plg_name = "old plugin";
  2407. plugin_config_init (&global_default_plg.plg_conf);
  2408. plugin_set_default_access (&global_default_plg.plg_conf);
  2409. }
  2410. #if 0
  2411. static void trace_plugin_invocation (Slapi_DN *target_spec, PluginTargetData *ptd,
  2412. PRBool bindop, PRBool isroot, PRBool islocal, int invoked)
  2413. {
  2414. int cookie, i = 0;
  2415. Slapi_DN *sdn;
  2416. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  2417. "Invocation parameters: target_spec = %s, bindop = %d, isroot=%d, islocal=%d\n"
  2418. "Plugin configuration: local_data=%d, remote_data=%d, anonymous_bind=%d, root_bind=%d\n",
  2419. slapi_sdn_get_ndn (target_spec), bindop, isroot, islocal, ptd->special_data[0],
  2420. ptd->special_data[1], ptd->special_data[2], ptd->special_data[3]);
  2421. sdn = ptd_get_first_subtree (ptd, &cookie);
  2422. while (sdn)
  2423. {
  2424. slapi_log_error (SLAPI_LOG_FATAL, NULL, "target_subtree%d: %s\n", i, slapi_sdn_get_ndn (sdn));
  2425. sdn = ptd_get_next_subtree (ptd, &cookie);
  2426. }
  2427. slapi_log_error (SLAPI_LOG_FATAL, NULL, invoked ? "Plugin is invoked\n" : "Plugin is not invoked\n");
  2428. }
  2429. #endif
  2430. /* functions to manipulate PluginTargetData type */
  2431. static void ptd_init (PluginTargetData *ptd)
  2432. {
  2433. PR_ASSERT (ptd);
  2434. dl_init (&ptd->subtrees, 0 /* initial count */);
  2435. memset (&ptd->special_data, 0, sizeof (ptd->special_data));
  2436. }
  2437. static void ptd_cleanup (PluginTargetData *ptd)
  2438. {
  2439. PR_ASSERT (ptd);
  2440. dl_cleanup (&ptd->subtrees, (FREEFN)slapi_sdn_free);
  2441. memset (&ptd->special_data, 0, sizeof (ptd->special_data));
  2442. }
  2443. static void ptd_add_subtree (PluginTargetData *ptd, Slapi_DN *subtree)
  2444. {
  2445. PR_ASSERT (ptd);
  2446. PR_ASSERT (subtree);
  2447. dl_add (&ptd->subtrees, subtree);
  2448. }
  2449. static void ptd_set_special_data (PluginTargetData *ptd, int type)
  2450. {
  2451. PR_ASSERT (ptd);
  2452. PR_ASSERT (type >= 0 && type < PLGC_DATA_MAX);
  2453. ptd->special_data [type] = PR_TRUE;
  2454. }
  2455. #if 0
  2456. static void ptd_clear_special_data (PluginTargetData *ptd, int type)
  2457. {
  2458. PR_ASSERT (ptd);
  2459. PR_ASSERT (type >= 0 && type < PLGC_DATA_MAX);
  2460. ptd->special_data [type] = PR_FALSE;
  2461. }
  2462. #endif
  2463. static Slapi_DN *ptd_get_first_subtree (const PluginTargetData *ptd, int *cookie)
  2464. {
  2465. PR_ASSERT (ptd);
  2466. return dl_get_first (&ptd->subtrees, cookie);
  2467. }
  2468. static Slapi_DN *ptd_get_next_subtree (const PluginTargetData *ptd, int *cookie)
  2469. {
  2470. PR_ASSERT (ptd);
  2471. return dl_get_next (&ptd->subtrees, cookie);
  2472. }
  2473. static PRBool ptd_is_special_data_set (const PluginTargetData *ptd, int type)
  2474. {
  2475. PR_ASSERT (ptd);
  2476. PR_ASSERT (type >= 0 && type < PLGC_DATA_MAX);
  2477. return ptd->special_data [type];
  2478. }
  2479. #if 0
  2480. static Slapi_DN* ptd_delete_subtree (PluginTargetData *ptd, Slapi_DN *subtree)
  2481. {
  2482. PR_ASSERT (ptd);
  2483. PR_ASSERT (subtree);
  2484. return (Slapi_DN*)dl_delete (&ptd->subtrees, subtree, (CMPFN)slapi_sdn_compare, NULL);
  2485. }
  2486. #endif
  2487. int ptd_get_subtree_count (const PluginTargetData *ptd)
  2488. {
  2489. PR_ASSERT (ptd);
  2490. return dl_get_count (&ptd->subtrees);
  2491. }
  2492. /* needed by command-line tasks to find an instance's plugin */
  2493. struct slapdplugin *plugin_get_by_name(char *name)
  2494. {
  2495. int x;
  2496. struct slapdplugin *plugin;
  2497. for(x = 0; x < PLUGIN_LIST_GLOBAL_MAX; x++) {
  2498. for(plugin = global_plugin_list[x]; plugin; plugin = plugin->plg_next) {
  2499. if (!strcmp(name, plugin->plg_name)) {
  2500. return plugin;
  2501. }
  2502. }
  2503. }
  2504. return NULL;
  2505. }
  2506. struct slapi_componentid *
  2507. generate_componentid ( struct slapdplugin * pp , char * name )
  2508. {
  2509. struct slapi_componentid * idp;
  2510. idp = (struct slapi_componentid *) slapi_ch_calloc(1, sizeof( *idp ));
  2511. if ( pp )
  2512. idp->sci_plugin=pp;
  2513. else
  2514. idp->sci_plugin=(struct slapdplugin *) plugin_get_server_plg();
  2515. if ( name )
  2516. idp->sci_component_name = slapi_ch_strdup(name);
  2517. else
  2518. /* Use plugin dn */
  2519. idp->sci_component_name = plugin_get_dn( idp->sci_plugin );
  2520. if (idp->sci_component_name)
  2521. slapi_dn_normalize(idp->sci_component_name);
  2522. return idp;
  2523. }
  2524. void release_componentid ( struct slapi_componentid * id )
  2525. {
  2526. if ( id ) {
  2527. if ( id->sci_component_name ) {
  2528. slapi_ch_free((void **)&id->sci_component_name);
  2529. id->sci_component_name=NULL;
  2530. }
  2531. slapi_ch_free((void **)&id);
  2532. }
  2533. }
  2534. /* used in main.c if -V flag is given */
  2535. static void slapd_print_plugin_version (
  2536. struct slapdplugin *plg,
  2537. struct slapdplugin *prev
  2538. )
  2539. {
  2540. if (plg == NULL || plg->plg_libpath == NULL) return;
  2541. /* same library as previous - don't print twice */
  2542. if (prev != NULL && prev->plg_libpath != NULL) {
  2543. if (strcmp(prev->plg_libpath,plg->plg_libpath) == 0) {
  2544. return;
  2545. }
  2546. }
  2547. printf("%s: %s\n",
  2548. plg->plg_libpath,
  2549. plg->plg_desc.spd_version ? plg->plg_desc.spd_version : "");
  2550. }
  2551. static void slapd_print_pluginlist_versions(struct slapdplugin *plg)
  2552. {
  2553. struct slapdplugin *p,*prev = NULL;
  2554. for (p = plg; p != NULL; p = p->plg_next) {
  2555. slapd_print_plugin_version(p,prev);
  2556. prev = p;
  2557. }
  2558. }
  2559. void plugin_print_versions(void)
  2560. {
  2561. int i;
  2562. for (i = 0; i < PLUGIN_LIST_GLOBAL_MAX; i++) {
  2563. slapd_print_pluginlist_versions(get_plugin_list(i));
  2564. }
  2565. }
  2566. /*
  2567. * Prints a list of plugins in execution order for each
  2568. * plug-in type. This will only be printed at the
  2569. * SLAPI_LOG_PLUGIN log level.
  2570. */
  2571. void plugin_print_lists(void)
  2572. {
  2573. int i;
  2574. struct slapdplugin *list = NULL;
  2575. struct slapdplugin *tmp = NULL;
  2576. for (i = 0; i < PLUGIN_LIST_GLOBAL_MAX; i++) {
  2577. if ((list = get_plugin_list(i)))
  2578. {
  2579. slapi_log_error(SLAPI_LOG_PLUGIN, NULL,
  2580. "---- Plugin List (type %d) ----\n", i);
  2581. for ( tmp = list; tmp; tmp = tmp->plg_next )
  2582. {
  2583. slapi_log_error(SLAPI_LOG_PLUGIN, NULL, " %s (precedence: %d)\n",
  2584. tmp->plg_name, tmp->plg_precedence);
  2585. }
  2586. }
  2587. }
  2588. }
  2589. /*
  2590. * check the spedified plugin entry and its nssladp-pluginEnabled value
  2591. * Return Value: 1 if the plugin is on.
  2592. * : 0 otherwise.
  2593. */
  2594. int
  2595. plugin_enabled(const char *plugin_name, void *identity)
  2596. {
  2597. Slapi_PBlock *search_pb = NULL;
  2598. Slapi_Entry **entries = NULL, **ep = NULL;
  2599. Slapi_Value *on_off = slapi_value_new_string("on");
  2600. char *filter = NULL;
  2601. int rc = 0; /* disabled, by default */
  2602. filter = slapi_filter_sprintf("cn=%s%s", ESC_NEXT_VAL, plugin_name);
  2603. search_pb = slapi_pblock_new();
  2604. slapi_search_internal_set_pb(search_pb, PLUGIN_BASE_DN, LDAP_SCOPE_ONELEVEL,
  2605. filter, NULL, 0, NULL, NULL, identity, 0);
  2606. slapi_search_internal_pb(search_pb);
  2607. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2608. if (LDAP_SUCCESS != rc) { /* plugin is not available */
  2609. rc = 0; /* disabled, by default */
  2610. goto bail;
  2611. }
  2612. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2613. for (ep = entries; ep && *ep; ep++) {
  2614. if (slapi_entry_attr_has_syntax_value(*ep, "nsslapd-pluginEnabled", on_off)) {
  2615. rc = 1; /* plugin is on */
  2616. goto bail;
  2617. }
  2618. }
  2619. bail:
  2620. slapi_value_free(&on_off);
  2621. slapi_free_search_results_internal(search_pb);
  2622. slapi_pblock_destroy(search_pb);
  2623. slapi_ch_free_string(&filter);
  2624. return rc;
  2625. }
  2626. /*
  2627. * Set given "type: attr" to the plugin default config entry
  2628. * (cn=plugin default config,cn=config) unless the same "type: attr" pair
  2629. * already exists in the entry.
  2630. */
  2631. int
  2632. slapi_set_plugin_default_config(const char *type, Slapi_Value *value)
  2633. {
  2634. Slapi_PBlock pb;
  2635. Slapi_Entry **entries = NULL;
  2636. int rc = LDAP_SUCCESS;
  2637. char **search_attrs = NULL; /* used by search */
  2638. if (NULL == type || '\0' == *type || NULL == value ) { /* nothing to do */
  2639. return rc;
  2640. }
  2641. charray_add(&search_attrs, slapi_ch_strdup(type));
  2642. /* cn=plugin default config,cn=config */
  2643. pblock_init(&pb);
  2644. slapi_search_internal_set_pb(&pb,
  2645. SLAPI_PLUGIN_DEFAULT_CONFIG, /* Base DN (normalized) */
  2646. LDAP_SCOPE_BASE,
  2647. "(objectclass=*)",
  2648. search_attrs, /* Attrs */
  2649. 0, /* AttrOnly */
  2650. NULL, /* Controls */
  2651. NULL, /* UniqueID */
  2652. (void *)plugin_get_default_component_id(),
  2653. 0);
  2654. slapi_search_internal_pb(&pb);
  2655. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2656. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2657. if (LDAP_SUCCESS == rc && entries && *entries) {
  2658. /* plugin default config entry exists */
  2659. int exists = 0;
  2660. Slapi_Attr *attr = NULL;
  2661. rc = slapi_entry_attr_find(*entries, type, &attr);
  2662. if (0 == rc) { /* type exists in the entry */
  2663. if (0 ==
  2664. slapi_attr_value_find(attr, slapi_value_get_berval(value))) {
  2665. /* value exists in the entry; we don't have to do anything. */
  2666. exists = 1;
  2667. }
  2668. }
  2669. slapi_free_search_results_internal(&pb);
  2670. pblock_done(&pb);
  2671. if (!exists) {
  2672. /* The argument attr is not in the plugin default config.
  2673. * Let's add it. */
  2674. Slapi_Mods smods;
  2675. Slapi_Value *va[2];
  2676. va[0] = value;
  2677. va[1] = NULL;
  2678. slapi_mods_init(&smods, 1);
  2679. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, type, va);
  2680. pblock_init(&pb);
  2681. slapi_modify_internal_set_pb(&pb, SLAPI_PLUGIN_DEFAULT_CONFIG,
  2682. slapi_mods_get_ldapmods_byref(&smods),
  2683. NULL, NULL, /* UniqueID */
  2684. (void *)plugin_get_default_component_id(),
  2685. 0 /* Flags */ );
  2686. slapi_modify_internal_pb(&pb);
  2687. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2688. slapi_mods_done(&smods);
  2689. pblock_done(&pb);
  2690. }
  2691. } else { /* cn=plugin default config does not exist. Let's add it. */
  2692. Slapi_Mods smods;
  2693. Slapi_Value *va[2];
  2694. slapi_free_search_results_internal(&pb);
  2695. pblock_done(&pb);
  2696. va[0] = value;
  2697. va[1] = NULL;
  2698. slapi_mods_init(&smods, 1);
  2699. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectClass", "top");
  2700. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectClass",
  2701. "extensibleObject");
  2702. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, type, va);
  2703. pblock_init(&pb);
  2704. slapi_add_internal_set_pb(&pb, SLAPI_PLUGIN_DEFAULT_CONFIG,
  2705. slapi_mods_get_ldapmods_byref(&smods), NULL,
  2706. (void *)plugin_get_default_component_id(),
  2707. 0 /* Flags */ );
  2708. slapi_add_internal_pb(&pb);
  2709. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2710. slapi_mods_done(&smods);
  2711. pblock_done(&pb);
  2712. }
  2713. charray_free(search_attrs);
  2714. return rc;
  2715. }
  2716. /*
  2717. * Get attribute values of given type from the plugin default config entry
  2718. * (cn=plugin default config,cn=config).
  2719. *
  2720. * Caller is responsible to free attrs by slapi_valueset_free.
  2721. */
  2722. int
  2723. slapi_get_plugin_default_config(char *type, Slapi_ValueSet **valueset)
  2724. {
  2725. Slapi_PBlock pb;
  2726. Slapi_Entry **entries = NULL;
  2727. int rc = LDAP_PARAM_ERROR;
  2728. char **search_attrs = NULL; /* used by search */
  2729. if (NULL == type || '\0' == *type || NULL == valueset) { /* nothing to do */
  2730. return rc;
  2731. }
  2732. charray_add(&search_attrs, slapi_ch_strdup(type));
  2733. /* cn=plugin default config,cn=config */
  2734. pblock_init(&pb);
  2735. slapi_search_internal_set_pb(&pb,
  2736. SLAPI_PLUGIN_DEFAULT_CONFIG, /* Base DN (normalized) */
  2737. LDAP_SCOPE_BASE,
  2738. "(objectclass=*)",
  2739. search_attrs, /* Attrs */
  2740. 0, /* AttrOnly */
  2741. NULL, /* Controls */
  2742. NULL, /* UniqueID */
  2743. (void *)plugin_get_default_component_id(),
  2744. 0);
  2745. slapi_search_internal_pb(&pb);
  2746. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2747. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2748. if (LDAP_SUCCESS == rc && entries && *entries) {
  2749. /* default config entry exists */
  2750. /* retrieve attribute values from the entry */
  2751. Slapi_Attr *attr = NULL;
  2752. rc = slapi_entry_attr_find(*entries, type, &attr);
  2753. if (0 == rc) { /* type value exists */
  2754. rc = slapi_attr_get_valueset(attr, valueset);
  2755. } else {
  2756. rc = LDAP_NO_SUCH_ATTRIBUTE;
  2757. }
  2758. }
  2759. slapi_free_search_results_internal(&pb);
  2760. pblock_done(&pb);
  2761. charray_free(search_attrs);
  2762. return rc;
  2763. }
  2764. void
  2765. slapi_set_plugin_open_rootdn_bind(Slapi_PBlock *pb){
  2766. struct pluginconfig *config = &pb->pb_plugin->plg_conf;
  2767. ptd_set_special_data(&(config->plgc_bind_subtrees), PLGC_DATA_BIND_ROOT);
  2768. }
  2769. PRBool
  2770. slapi_disordely_shutdown(PRBool set)
  2771. {
  2772. static PRBool is_disordely_shutdown = PR_FALSE;
  2773. if (set) {
  2774. is_disordely_shutdown = PR_TRUE;
  2775. }
  2776. return (is_disordely_shutdown);
  2777. }