plugin.c 91 KB

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