plugin.c 86 KB

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