mapping_tree.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586
  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. /* mapping_tree.c - Maps the DIT onto backends and/or referrals. */
  42. #include "slap.h"
  43. /* distribution plugin prototype */
  44. typedef int (* mtn_distrib_fct) (Slapi_PBlock *pb, Slapi_DN * target_dn,
  45. char **mtn_be_names, int be_count, Slapi_DN * mtn_node_dn, int *mtn_be_states);
  46. struct mt_node
  47. {
  48. Slapi_DN *mtn_subtree; /* dn for the node's subtree */
  49. Slapi_Backend **mtn_be; /* backend pointer - the list of backends implementing this
  50. * node usually there is only one back-end here, there can
  51. * be several only when distribution is done on this node
  52. */
  53. int *mtn_be_states; /* states for the backends in table mtn_be */
  54. char **mtn_backend_names; /* list of backend names */
  55. int mtn_be_list_size; /* size of the previous three tables */
  56. int mtn_be_count; /* number of backends implementing this node */
  57. char **mtn_referral; /* referral or list of referrals */
  58. Slapi_Entry *mtn_referral_entry; /* referral or list of referrals */
  59. struct mt_node *mtn_children;
  60. struct mt_node *mtn_parent;
  61. struct mt_node *mtn_brother; /* list of other nodes with the same parent */
  62. int mtn_state;
  63. int mtn_private; /* Never show this node to the user. only used for
  64. * cn=config, cn=schema and root node */
  65. char * mtn_dstr_plg_lib; /* distribution plugin library name */
  66. char * mtn_dstr_plg_name; /* distribution plugin function name */
  67. mtn_distrib_fct mtn_dstr_plg; /* pointer to the actual ditribution function */
  68. void *mtn_extension; /* plugins can extend a mapping tree node */
  69. };
  70. #define BE_LIST_INIT_SIZE 10
  71. #define BE_LIST_INCREMENT 10
  72. /* Mapping tree locking strategy
  73. *
  74. * There are two goals for the mapping tree locking
  75. * - protect the mapping tree structures
  76. * - prevent stop or re-initialisation of a backend which is currently
  77. * used by an LDAP operation
  78. * This must be done without preventing parallelisation of LDAP operations
  79. *
  80. * we use
  81. * - one RW lock will be used to protect access to mapping tree structures
  82. * accessed throuh mtn_lock(), mtn_unlock(), mtn_wlock()
  83. * - one RW lock for each backend. This lock is taken in READ for each
  84. * LDAP operation in progree on the backend
  85. * and is taken in write for administrative operations like stop or
  86. * disable the backend
  87. * accessed through slapi_be_Rlock(), slapi_be_Wlock(), slapi_be_Unlock()
  88. * - a state flag for each backend : mtn_be_states[]
  89. * this state is set to SLAPI_BE_STATE_DELETE or SLAPI_BE_STATE_OFFLINE at the beginning
  90. * of stop/disable operation to ensure that no new operation starts
  91. * while the backend is stopped/disabled
  92. *
  93. * The algorithme for LDAP OPERATIONS is :
  94. *
  95. * lock mapping tree in read mode
  96. * get backend
  97. * check that backend is not in SLAPI_BE_STATE_DELETE or SLAPI_BE_STATE_OFFLINE state
  98. * lock backend in read mode
  99. * unlock mapping tree
  100. * do LDAP operation
  101. * release backend lock
  102. *
  103. * The algorithme for maintenance operation is
  104. * lock mapping tree in write mode
  105. * set state to SLAPI_BE_STATE_DELETE or SLAPI_BE_STATE_OFFLINE
  106. * unlock mapping tree
  107. * get backend lock in write mode
  108. * release backend lock
  109. *
  110. */
  111. static PRRWLock *myLock; /* global lock on the mapping tree structures */
  112. static mapping_tree_node *mapping_tree_root = NULL;
  113. static int mapping_tree_inited = 0;
  114. static int mapping_tree_freed = 0;
  115. static int extension_type = -1; /* type returned from the factory */
  116. /* The different states a mapping tree node can be in. */
  117. #define MTN_DISABLED 0 /* The server acts like the node isn't there. */
  118. #define MTN_BACKEND 1 /* This node represents a backend. */
  119. #define MTN_REFERRAL 2 /* A referral is returned instead of a backend. */
  120. #define MTN_REFERRAL_ON_UPDATE 3 /* A referral is returned for update operations. */
  121. #define MTN_CONTAINER 4 /* This node represents a container for backends. */
  122. /* Need to add a modifier flag to the state - such as round robin */
  123. /* Note: This DN is no need to be normalized. */
  124. #define MAPPING_TREE_BASE_DN "cn=mapping tree,cn=config"
  125. #define MAPPING_TREE_PARENT_ATTRIBUTE "nsslapd-parent-suffix"
  126. void mtn_wlock();
  127. void mtn_lock();
  128. void mtn_unlock();
  129. static mapping_tree_node * mtn_get_mapping_tree_node_by_entry(
  130. mapping_tree_node* node, const Slapi_DN *dn);
  131. static void mtn_remove_node(mapping_tree_node * node);
  132. static void mtn_free_node (mapping_tree_node **node);
  133. static int mtn_get_be_distributed(Slapi_PBlock *pb,
  134. mapping_tree_node * target_node, Slapi_DN *target_sdn, int * flag_stop);
  135. static int mtn_get_be(mapping_tree_node *target_node, Slapi_PBlock *pb,
  136. Slapi_Backend **be, int * index, Slapi_Entry **referral, char *errorbuf);
  137. static mapping_tree_node * mtn_get_next_node(mapping_tree_node * node,
  138. mapping_tree_node * node_list, int scope);
  139. static mapping_tree_node * mtn_get_first_node(mapping_tree_node * node,
  140. int scope);
  141. static mapping_tree_node *
  142. get_mapping_tree_node_by_name(mapping_tree_node * node, char * be_name);
  143. #ifdef DEBUG
  144. static void dump_mapping_tree(mapping_tree_node *parent, int depth);
  145. #endif
  146. /* structure and static local variable used to store the
  147. * list of plugins that have registered to a callback when backend state
  148. * change
  149. */
  150. struct mtn_be_ch_list {
  151. void * handle;
  152. struct mtn_be_ch_list * next;
  153. slapi_backend_state_change_fnptr fnct;
  154. };
  155. static struct mtn_be_ch_list * mtn_plug_list = NULL;
  156. /* API for registering to a callback when backend state change */
  157. void slapi_register_backend_state_change(void * handle, slapi_backend_state_change_fnptr funct)
  158. {
  159. struct mtn_be_ch_list * new_be_ch_plg;
  160. new_be_ch_plg = (struct mtn_be_ch_list *)
  161. slapi_ch_malloc(sizeof(struct mtn_be_ch_list));
  162. new_be_ch_plg->next = mtn_plug_list;
  163. new_be_ch_plg->handle = handle;
  164. new_be_ch_plg->fnct = funct;
  165. mtn_plug_list = new_be_ch_plg;
  166. }
  167. /* To unregister all the state change callbacks registered on the mapping tree */
  168. int slapi_unregister_backend_state_change_all()
  169. {
  170. struct mtn_be_ch_list *cur_be_ch_plg;
  171. while (mtn_plug_list)
  172. {
  173. cur_be_ch_plg = mtn_plug_list;
  174. mtn_plug_list = mtn_plug_list->next;
  175. slapi_ch_free((void **) &cur_be_ch_plg);
  176. }
  177. return 1;
  178. }
  179. int slapi_unregister_backend_state_change(void * handle)
  180. {
  181. struct mtn_be_ch_list * cur_be_ch_plg = mtn_plug_list;
  182. struct mtn_be_ch_list * prev_be_ch_plg = mtn_plug_list;
  183. while (cur_be_ch_plg)
  184. {
  185. if (cur_be_ch_plg->handle == handle)
  186. {
  187. if (cur_be_ch_plg == mtn_plug_list)
  188. {
  189. mtn_plug_list = mtn_plug_list->next;
  190. slapi_ch_free((void **) &cur_be_ch_plg);
  191. return 0;
  192. }
  193. else
  194. {
  195. prev_be_ch_plg->next = cur_be_ch_plg->next;
  196. slapi_ch_free((void **) &cur_be_ch_plg);
  197. return 0;
  198. }
  199. }
  200. prev_be_ch_plg = cur_be_ch_plg;
  201. cur_be_ch_plg = cur_be_ch_plg->next;
  202. }
  203. return 1;
  204. }
  205. void mtn_be_state_change(char * be_name, int old_state, int new_state)
  206. {
  207. struct mtn_be_ch_list * cur_be_ch_plg = mtn_plug_list;
  208. while (cur_be_ch_plg)
  209. {
  210. (* (cur_be_ch_plg->fnct))(cur_be_ch_plg->handle, be_name,
  211. old_state, new_state);
  212. cur_be_ch_plg = cur_be_ch_plg->next;
  213. }
  214. }
  215. Slapi_DN* slapi_mtn_get_dn(mapping_tree_node *node)
  216. {
  217. return (node->mtn_subtree);
  218. }
  219. /* this will turn an array of url into a referral entry */
  220. static Slapi_Entry *
  221. referral2entry(char ** url_array, const char *target)
  222. {
  223. int i;
  224. struct berval bv0,bv1,*bvals[3];
  225. Slapi_Entry *anEntry;
  226. if (url_array == NULL)
  227. return NULL;
  228. anEntry = slapi_entry_alloc();
  229. slapi_entry_set_dn(anEntry,slapi_ch_strdup(target));
  230. bvals[2]=NULL;
  231. bvals[1]=&bv1;
  232. bv1.bv_val="referral";
  233. bv1.bv_len=strlen(bv1.bv_val);
  234. bvals[0]=&bv0;
  235. bv0.bv_val="top";
  236. bv0.bv_len=strlen(bv0.bv_val);
  237. slapi_entry_add_values( anEntry, "objectClass", bvals);
  238. bvals[1]=NULL;
  239. for (i=0; url_array[i]; i++) {
  240. bv0.bv_val=url_array[i];
  241. bv0.bv_len=strlen(bv0.bv_val);
  242. slapi_entry_attr_merge( anEntry, "ref", bvals);
  243. }
  244. return anEntry;
  245. }
  246. /* mapping tree node extension */
  247. int
  248. mapping_tree_get_extension_type ()
  249. {
  250. if(extension_type==-1)
  251. {
  252. /* The factory is given the name of the object type, in
  253. * return for a type handle. Whenever the object is created
  254. * or destroyed the factory is called with the handle so
  255. * that it may call the constructors or destructors registered
  256. * with it.
  257. */
  258. extension_type= factory_register_type(SLAPI_EXT_MTNODE,
  259. offsetof(mapping_tree_node, mtn_extension));
  260. }
  261. return extension_type;
  262. }
  263. static mapping_tree_node *
  264. mapping_tree_node_new(Slapi_DN *dn, Slapi_Backend **be, char **backend_names, int *be_states,
  265. int count, int size,
  266. char **referral, mapping_tree_node *parent,
  267. int state, int private, char *plg_lib, char *plg_fct,
  268. mtn_distrib_fct plg)
  269. {
  270. Slapi_RDN rdn;
  271. mapping_tree_node *node;
  272. node = (mapping_tree_node *) slapi_ch_calloc(1, sizeof(mapping_tree_node));
  273. node->mtn_subtree = dn;
  274. node->mtn_be = be;
  275. node->mtn_be_states = be_states;
  276. node->mtn_backend_names = backend_names;
  277. node->mtn_referral = referral;
  278. node->mtn_referral_entry = referral2entry(referral, slapi_sdn_get_dn(dn)) ;
  279. node->mtn_parent = parent;
  280. node->mtn_children = NULL;
  281. node->mtn_brother = NULL;
  282. node->mtn_state = state;
  283. node->mtn_private = private;
  284. node->mtn_be_list_size = size;
  285. node->mtn_be_count = count;
  286. /* We use this count of the rdn components in the mapping tree to help
  287. * when selecting a mapping tree node for a dn. */
  288. slapi_rdn_init_sdn(&rdn,dn);
  289. slapi_rdn_done(&rdn);
  290. node->mtn_dstr_plg_lib = plg_lib;
  291. node->mtn_dstr_plg_name = plg_fct;
  292. node->mtn_dstr_plg = plg;
  293. slapi_log_error(SLAPI_LOG_TRACE, "mapping_tree",
  294. "Created new mapping tree node for suffix [%s] backend [%s] [%p]\n",
  295. slapi_sdn_get_dn(dn),
  296. backend_names && backend_names[0] ? backend_names[0] : "null",
  297. be ? be[0] : NULL);
  298. return node;
  299. }
  300. /*
  301. * Description:
  302. * Adds a mapping tree node to the child list of another mapping tree node.
  303. *
  304. * Arguments:
  305. * parent and child are pointers to mapping tree nodes. child will be added
  306. * to parent's child list. For now, the child is added to the head of the
  307. * linked list. Later we may come up a way to ordering the entries in the
  308. * list.
  309. *
  310. * Returns:
  311. * nothing
  312. */
  313. static void
  314. mapping_tree_node_add_child(mapping_tree_node *parent, mapping_tree_node* child)
  315. {
  316. /* WARNING:
  317. * As for now the mapping tree is not locked when a child is added
  318. * this is possible only because the child is added into the mapping
  319. * the structure by a single operation after being fully initialized
  320. * should this be changed, the lock policy would have to be checked
  321. * see mapping_tree_entry_add_callback()
  322. */
  323. child->mtn_brother = parent->mtn_children;
  324. parent->mtn_children = child;
  325. /* for debugging: dump_mapping_tree(mapping_tree_root, 0); */
  326. }
  327. static Slapi_DN *
  328. get_parent_from_entry(Slapi_Entry * entry)
  329. {
  330. Slapi_Attr *attr = NULL;
  331. char *origparent = NULL;
  332. char *parent = NULL;
  333. Slapi_Value *val = NULL;
  334. Slapi_DN *parent_sdn = NULL;
  335. if (slapi_entry_attr_find(entry, MAPPING_TREE_PARENT_ATTRIBUTE, &attr))
  336. return NULL;
  337. slapi_attr_first_value(attr, &val);
  338. origparent = parent = slapi_ch_strdup(slapi_value_get_string(val));
  339. if (parent) {
  340. if(*parent == '"') {
  341. char *ptr = NULL;
  342. parent++; /* skipping the starting '"' */
  343. ptr = PL_strnrchr(parent, '"', strlen(parent));
  344. if (ptr) {
  345. *ptr = '\0';
  346. }
  347. }
  348. parent_sdn =
  349. slapi_sdn_new_dn_passin(slapi_create_dn_string("%s", parent));
  350. slapi_ch_free_string(&origparent);
  351. }
  352. return parent_sdn;
  353. }
  354. /* extract the subtree managed by a mapping tree entry from the entry
  355. */
  356. static Slapi_DN *
  357. get_subtree_from_entry(Slapi_Entry * entry)
  358. {
  359. Slapi_Attr *attr = NULL;
  360. char *origcn = NULL;
  361. char *cn = NULL;
  362. Slapi_Value *val = NULL;
  363. Slapi_DN *subtree = NULL;
  364. if (slapi_entry_attr_find(entry, "cn", &attr))
  365. return NULL;
  366. /* should check that there is only one value for cn attribute */
  367. slapi_attr_first_value(attr, &val);
  368. /* The value of cn is the dn of the subtree for this node.
  369. * There is a slight problem though. The cn value is
  370. * quoted. We have to remove the quotes here. I'm sure
  371. * there is a proper way to do this, but for now we'll
  372. * just assume that the first and last chars are ". Later
  373. * we'll have to revisit this because things could be a
  374. * lot more complicated. Especially if there are quotes
  375. * in the dn of the subtree root dn! */
  376. /* JCM - Need to dequote correctly. */
  377. /* GB : I think removing the first and last " in the cn value
  378. * is the right stuff to do
  379. */
  380. origcn = cn = slapi_ch_strdup(slapi_value_get_string(val));
  381. if (cn) {
  382. if(*cn == '"') {
  383. char *ptr = NULL;
  384. cn++; /* skipping the starting '"' */
  385. ptr = PL_strnrchr(cn, '"', strlen(cn));
  386. if (ptr) {
  387. *ptr = '\0';
  388. }
  389. }
  390. subtree = slapi_sdn_new_dn_passin(slapi_create_dn_string("%s", cn));
  391. slapi_ch_free_string(&origcn);
  392. }
  393. return subtree;
  394. }
  395. static int
  396. mtn_state_to_int(const char * state_string, Slapi_Entry *entry)
  397. {
  398. if (!strcasecmp(state_string, "disabled")) {
  399. return MTN_DISABLED;
  400. } else if (!strcasecmp(state_string, "backend")) {
  401. return MTN_BACKEND;
  402. } else if (!strcasecmp(state_string, "referral")) {
  403. return MTN_REFERRAL;
  404. } else if (!strcasecmp(state_string, "referral on update")) {
  405. return MTN_REFERRAL_ON_UPDATE;
  406. } else if (!strcasecmp(state_string, "container")) {
  407. return MTN_CONTAINER;
  408. } else {
  409. LDAPDebug(LDAP_DEBUG_ANY,
  410. "Warning: Unknown state, %s, for mapping tree node %s."
  411. " Defaulting to DISABLED\n",
  412. state_string, slapi_entry_get_dn(entry), 0);
  413. return MTN_DISABLED;
  414. }
  415. }
  416. static char **
  417. mtn_get_referral_from_entry(Slapi_Entry * entry)
  418. {
  419. int nb;
  420. int hint;
  421. char ** referral;
  422. Slapi_Attr * attr;
  423. Slapi_Value *val = NULL;
  424. if (slapi_entry_attr_find(entry, "nsslapd-referral", &attr))
  425. return NULL;
  426. slapi_attr_get_numvalues(attr, &nb);
  427. referral = (char **) slapi_ch_malloc(sizeof(char *) * (nb+1));
  428. hint = slapi_attr_first_value(attr, &val);
  429. if (NULL == val) {
  430. LDAPDebug(LDAP_DEBUG_ANY, "Warning: The nsslapd-referral attribute has no value for the mapping tree node %s\n", slapi_entry_get_dn(entry), 0, 0);
  431. return NULL;
  432. }
  433. nb = 0;
  434. while (val)
  435. {
  436. referral[nb++] = slapi_ch_strdup(slapi_value_get_string(val));
  437. hint = slapi_attr_next_value(attr, hint, &val);
  438. }
  439. referral[nb] = NULL;
  440. return referral;
  441. }
  442. static int
  443. get_backends_from_attr(Slapi_Attr *attr, backend ***be_list, char ***be_names,
  444. int ** be_states, int *be_list_count, int *be_list_size,
  445. mapping_tree_node * node)
  446. {
  447. Slapi_Value *val = NULL;
  448. backend *tmp_be = NULL;
  449. char * tmp_backend_name;
  450. int hint;
  451. mapping_tree_node* new_node;
  452. *be_list_size = BE_LIST_INIT_SIZE;
  453. *be_list_count = 0;
  454. *be_list = (backend **) slapi_ch_malloc(sizeof(backend *) * BE_LIST_INIT_SIZE);
  455. *be_names = (char **) slapi_ch_malloc(sizeof(char *) * BE_LIST_INIT_SIZE);
  456. *be_states = (int *) slapi_ch_malloc(sizeof(int) * BE_LIST_INIT_SIZE);
  457. hint = slapi_attr_first_value(attr, &val);
  458. if (NULL == val) {
  459. slapi_ch_free ((void **)be_list);
  460. *be_list = NULL;
  461. return 0;
  462. }
  463. while (val)
  464. {
  465. tmp_backend_name = (char *) slapi_ch_strdup(slapi_value_get_string(val));
  466. if (*be_list_count >= *be_list_size)
  467. {
  468. (*be_list_size) += BE_LIST_INCREMENT;
  469. *be_names = (char **) slapi_ch_realloc((char *) (*be_names),
  470. sizeof(char *) * (*be_list_size));
  471. *be_list = (backend **) slapi_ch_realloc((char *) (*be_list),
  472. sizeof(backend *) * (*be_list_size));
  473. *be_states = (int *) slapi_ch_realloc((char *) (*be_states),
  474. sizeof(int) * (*be_list_size));
  475. }
  476. (*be_names)[*be_list_count] = tmp_backend_name;
  477. /* set backend as started by default */
  478. (*be_states)[*be_list_count] = SLAPI_BE_STATE_ON;
  479. /* We now need to find the backend with name backend_name. */
  480. tmp_be= slapi_be_select_by_instance_name(tmp_backend_name);
  481. new_node = get_mapping_tree_node_by_name(mapping_tree_root,
  482. tmp_backend_name);
  483. if (new_node && (new_node != node))
  484. {
  485. LDAPDebug(LDAP_DEBUG_ANY,
  486. "ERROR: backend %s is already pointed to by a mapping tree"
  487. " node. Only one mapping tree node can point to a backend\n",
  488. tmp_backend_name, 0, 0);
  489. tmp_be = NULL;
  490. return -1;
  491. }
  492. if(tmp_be!=NULL)
  493. {
  494. tmp_be->be_mapped = 1;
  495. (*be_list)[*be_list_count] = tmp_be;
  496. }
  497. else
  498. {
  499. /* It's just not here yet. That's OK. We'll fix it up at runtime. */
  500. (*be_list)[*be_list_count] = NULL;
  501. }
  502. (*be_list_count) ++;
  503. hint = slapi_attr_next_value(attr, hint, &val);
  504. }
  505. return 0;
  506. }
  507. /*
  508. * Description:
  509. * Free the data allocated for mapping tree node arrays
  510. */
  511. static void
  512. free_mapping_tree_node_arrays(backend ***be_list, char ***be_names,
  513. int ** be_states, int *be_list_count)
  514. {
  515. int i;
  516. /* sanity check */
  517. PR_ASSERT (be_list != NULL && be_names != NULL && be_states != NULL && be_list_count != NULL);
  518. if (*be_names != NULL) for (i = 0; i < *be_list_count; ++i) {
  519. slapi_ch_free ((void **)&((*be_names)[i]));
  520. }
  521. slapi_ch_free ((void **)be_names);
  522. slapi_ch_free ((void **)be_list);
  523. slapi_ch_free ((void **)be_states);
  524. *be_list_count = 0;
  525. }
  526. /*
  527. * Description:
  528. * Takes an entry and creates a mapping tree node from it. Loops through the
  529. * attributes, pulling needed info from them. Right now, each node can only
  530. * have one backend and one referral. Once we move to supporting more than
  531. * one node and more than one referral, this function will need to be
  532. * massaged a little.
  533. *
  534. * We should make a objectclass for a mapping tree node entry. That way
  535. * schema checking would make this function more robust.
  536. *
  537. * Arguments:
  538. * A mapping tree node entry read in from the DIT.
  539. *
  540. * Returns:
  541. * An LDAP result code (LDAP_SUCCESS if all goes well).
  542. * If the return value is LDAP_SUCCESS, *newnodep is set to the new mapping
  543. * tree node (guaranteed to be non-NULL).
  544. */
  545. static int
  546. mapping_tree_entry_add(Slapi_Entry *entry, mapping_tree_node **newnodep )
  547. {
  548. Slapi_DN *subtree = NULL;
  549. const char *tmp_ndn;
  550. int be_list_count = 0;
  551. int be_list_size;
  552. backend **be_list = NULL;
  553. char **be_names = NULL;
  554. int * be_states = NULL;
  555. char * plugin_funct = NULL;
  556. char * plugin_lib = NULL;
  557. mtn_distrib_fct plugin = NULL;
  558. char **referral = NULL;
  559. int state = MTN_DISABLED;
  560. Slapi_Attr *attr = NULL;
  561. mapping_tree_node *node = NULL;
  562. mapping_tree_node *parent_node = mapping_tree_root;
  563. int rc;
  564. int lderr = LDAP_UNWILLING_TO_PERFORM; /* our default result code */
  565. char *tmp_backend_name;
  566. Slapi_Backend *be;
  567. PR_ASSERT(newnodep != NULL);
  568. *newnodep = NULL;
  569. subtree = get_subtree_from_entry(entry);
  570. /* Make sure we know the root dn of the subtree for this node. */
  571. if (NULL == subtree) {
  572. LDAPDebug(LDAP_DEBUG_ANY, "Warning: Unable to determine the subtree represented by the mapping tree node %s\n",
  573. slapi_entry_get_dn(entry), 0, 0);
  574. return lderr;
  575. }
  576. tmp_ndn = slapi_sdn_get_ndn( subtree );
  577. if (tmp_ndn && ( '\0' == *tmp_ndn)) {
  578. /* This entry is associated with the "" subtree. Treat this is
  579. * a special case (no parent; will replace the internal root
  580. * node (mapping_tree_root) with data from this entry).
  581. */
  582. slapi_log_error( SLAPI_LOG_ARGS, "mapping_tree_entry_add", "NULL suffix\n" );
  583. parent_node = NULL;
  584. }
  585. /* Make sure a node does not already exist for this subtree */
  586. if ( parent_node != NULL && NULL != mtn_get_mapping_tree_node_by_entry(mapping_tree_root,
  587. subtree)) {
  588. LDAPDebug(LDAP_DEBUG_ANY, "Warning: a mapping tree node for the"
  589. " subtree %s already exists; unable to add the node %s\n",
  590. slapi_sdn_get_dn(subtree), slapi_entry_get_dn(entry), 0);
  591. slapi_sdn_free(&subtree);
  592. return LDAP_ALREADY_EXISTS;
  593. }
  594. /* Loop through the attributes and handle the ones we care about. */
  595. for (rc = slapi_entry_first_attr(entry, &attr);
  596. !rc && attr;
  597. rc = slapi_entry_next_attr(entry, attr, &attr)) {
  598. char *type = NULL;
  599. Slapi_Value *val = NULL;
  600. slapi_attr_get_type(attr, &type);
  601. if (NULL == type) {
  602. /* strange... I wonder if we should give a warning here? */
  603. continue;
  604. }
  605. if (!strcasecmp(type, "nsslapd-backend")) {
  606. if (get_backends_from_attr(attr, &be_list, &be_names, &be_states,
  607. &be_list_count, &be_list_size, NULL)) {
  608. free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
  609. slapi_sdn_free(&subtree);
  610. return lderr;
  611. }
  612. if (NULL == be_list)
  613. {
  614. LDAPDebug(LDAP_DEBUG_ANY,
  615. "Warning: The nsslapd-backend attribute has no value for the mapping tree node %s\n",
  616. slapi_entry_get_dn(entry), 0, 0);
  617. continue;
  618. }
  619. } else if (!strcasecmp(type, "nsslapd-referral")) {
  620. referral = mtn_get_referral_from_entry(entry);
  621. } else if (!strcasecmp(type, "nsslapd-state")) {
  622. const char *state_string;
  623. slapi_attr_first_value(attr, &val);
  624. if (NULL == val) {
  625. LDAPDebug(LDAP_DEBUG_ANY, "Warning: Can't determine the state of the mapping tree node %s\n",
  626. slapi_entry_get_dn(entry), 0, 0);
  627. continue;
  628. }
  629. /* Convert the string representation for the state to an int */
  630. state_string = slapi_value_get_string(val);
  631. state = mtn_state_to_int(state_string, entry);
  632. } else if (!strcasecmp(type, "nsslapd-distribution-plugin")) {
  633. slapi_attr_first_value(attr, &val);
  634. if (NULL == val) {
  635. LDAPDebug(LDAP_DEBUG_ANY, "Warning: The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
  636. slapi_entry_get_dn(entry), 0, 0);
  637. continue;
  638. }
  639. plugin_lib = slapi_ch_strdup(slapi_value_get_string(val));
  640. } else if (!strcasecmp(type, "nsslapd-distribution-funct")) {
  641. slapi_attr_first_value(attr, &val);
  642. if (NULL == val) {
  643. LDAPDebug(LDAP_DEBUG_ANY, "Warning: The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
  644. slapi_entry_get_dn(entry), 0, 0);
  645. continue;
  646. }
  647. plugin_funct = slapi_ch_strdup(slapi_value_get_string(val));
  648. } else if (!strcasecmp(type, MAPPING_TREE_PARENT_ATTRIBUTE)) {
  649. Slapi_DN *parent_node_dn = get_parent_from_entry(entry);
  650. parent_node = mtn_get_mapping_tree_node_by_entry(
  651. mapping_tree_root, parent_node_dn);
  652. if (parent_node == NULL)
  653. {
  654. parent_node = mapping_tree_root;
  655. LDAPDebug(LDAP_DEBUG_ANY,
  656. "Warning: could not find parent for %s defaulting to root\n",
  657. slapi_entry_get_dn(entry), 0, 0);
  658. }
  659. slapi_sdn_free(&parent_node_dn);
  660. }
  661. }
  662. if (state == MTN_CONTAINER)
  663. {
  664. /* this can be extended later to include the general
  665. null suffix, */
  666. /* The "default" backend is used by the container node */
  667. be = defbackend_get_backend();
  668. if(be == NULL)
  669. {
  670. LDAPDebug(LDAP_DEBUG_ANY,
  671. "ERROR: default container has not been created for the NULL SUFFIX node.\n",
  672. 0, 0, 0);
  673. return -1;
  674. }
  675. be_list_size = 1;
  676. be_list_count = 0;
  677. be_list = (backend **) slapi_ch_calloc(1, sizeof(backend *) );
  678. be_names = (char **) slapi_ch_calloc(1, sizeof(char *) );
  679. be_states = (int *) slapi_ch_calloc(1, sizeof(int) );
  680. tmp_backend_name = (char *) slapi_ch_strdup("default"); /* "NULL_CONTAINER" */
  681. (be_names)[be_list_count] = tmp_backend_name;
  682. /* set backend as started by default */
  683. (be_states)[be_list_count] = SLAPI_BE_STATE_ON;
  684. be->be_mapped = 1;
  685. (be_list)[be_list_count] = be;
  686. be_list_count++;
  687. }
  688. /* check that all required attributes for the givene state are there :
  689. * state backend -> need nsslapd-backend attribute
  690. * state referral or referral on update -> need nsslapd-referral attribute
  691. */
  692. if (((state == MTN_BACKEND) || (state == MTN_REFERRAL_ON_UPDATE))
  693. && (be_names == NULL))
  694. {
  695. LDAPDebug(LDAP_DEBUG_ANY,
  696. "ERROR: node %s must define a backend\n",
  697. slapi_entry_get_dn(entry), 0, 0);
  698. slapi_sdn_free(&subtree);
  699. free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
  700. return lderr;
  701. }
  702. if (((state == MTN_REFERRAL) || (state == MTN_REFERRAL_ON_UPDATE))
  703. && (referral == NULL))
  704. {
  705. LDAPDebug(LDAP_DEBUG_ANY,
  706. "ERROR: node %s must define referrals to be in referral state\n",
  707. slapi_entry_get_dn(entry), 0, 0);
  708. slapi_sdn_free(&subtree);
  709. free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
  710. return lderr;
  711. }
  712. if (plugin_lib && plugin_funct)
  713. {
  714. plugin = (mtn_distrib_fct)sym_load(plugin_lib, plugin_funct,
  715. "Entry Distribution", 1);
  716. if (plugin == NULL)
  717. {
  718. LDAPDebug(LDAP_DEBUG_ANY,
  719. "ERROR: node %s cannot find distribution plugin. "
  720. SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
  721. slapi_entry_get_dn(entry), PR_GetError(), slapd_pr_strerror(PR_GetError()));
  722. slapi_sdn_free(&subtree);
  723. slapi_ch_free((void **) &plugin_funct);
  724. slapi_ch_free((void **) &plugin_lib);
  725. free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
  726. return lderr;
  727. }
  728. }
  729. else if ((plugin_lib == NULL) && (plugin_funct == NULL))
  730. {
  731. /* nothing configured -> OK */
  732. plugin = NULL;
  733. }
  734. else
  735. {
  736. /* only one parameter configured -> ERROR */
  737. LDAPDebug(LDAP_DEBUG_ANY,
  738. "ERROR: node %s must define both lib and funct"
  739. " for distribution plugin\n",
  740. slapi_entry_get_dn(entry), 0, 0);
  741. slapi_sdn_free(&subtree);
  742. slapi_ch_free((void **) &plugin_funct);
  743. slapi_ch_free((void **) &plugin_lib);
  744. free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
  745. return lderr;
  746. }
  747. /* Now we can create the node for this mapping tree entry. */
  748. node= mapping_tree_node_new(subtree, be_list, be_names, be_states, be_list_count,
  749. be_list_size, referral, parent_node, state,
  750. 0 /* Normal node. People can see and change it. */,
  751. plugin_lib, plugin_funct, plugin);
  752. tmp_ndn = slapi_sdn_get_ndn( subtree );
  753. if ( NULL != node && NULL == parent_node && tmp_ndn
  754. && ('\0' == *tmp_ndn )) {
  755. /* The new node is actually the "" node. Replace the root
  756. * node with this new one by copying all information (we can't
  757. * free the root node completely because children of the root
  758. * node hold pointers to it in their mtn_parent field).
  759. */
  760. slapi_log_error( SLAPI_LOG_ARGS, "mapping_tree_entry_add", "fix up NULL suffix\n" );
  761. node->mtn_children = mapping_tree_root->mtn_children;
  762. node->mtn_brother = mapping_tree_root->mtn_brother;
  763. *mapping_tree_root = *node; /* struct copy */
  764. slapi_ch_free( (void **)&node );
  765. node = mapping_tree_root;
  766. }
  767. if ( NULL != node ) {
  768. lderr = LDAP_SUCCESS;
  769. *newnodep = node;
  770. }
  771. return lderr;
  772. }
  773. /*
  774. * Recursive procedure used to create node extensions once the mapping tree
  775. * is fully initialized
  776. * This is done after full init of the mapping tree so that the extensions can do
  777. * searches
  778. */
  779. void mtn_create_extension(mapping_tree_node *node)
  780. {
  781. if (node == NULL)
  782. return;
  783. node->mtn_extension = factory_create_extension(mapping_tree_get_extension_type(),
  784. node, NULL /* parent */);
  785. mtn_create_extension(node->mtn_children);
  786. mtn_create_extension(node->mtn_brother);
  787. }
  788. /*
  789. * Description:
  790. * Does the main work of building the in memory mapping tree form the entries
  791. * in the DIT. This function is called recursively on all the given nodes
  792. * children to build up the tree. Basically it does an internal search for
  793. * all the entries who have the target node as a parent.
  794. *
  795. * Arguments:
  796. * The target node and a flag that tells if it's the root of the tree.
  797. *
  798. * Returns:
  799. * Nothing
  800. */
  801. static int
  802. mapping_tree_node_get_children(mapping_tree_node *target, int is_root)
  803. {
  804. Slapi_PBlock *pb;
  805. char * filter = NULL;
  806. int res;
  807. Slapi_Entry **entries = NULL;
  808. int x;
  809. int result = 0;
  810. pb = slapi_pblock_new();
  811. /* Remember that the root node of the mapping tree is the NULL suffix.
  812. * Since we don't really support it, children of the root node won't
  813. * have a MAPPING_TREE_PARENT_ATTRIBUTE. */
  814. if (is_root) {
  815. filter = slapi_ch_smprintf("(&(objectclass=nsMappingTree)(!(%s=*)))",
  816. MAPPING_TREE_PARENT_ATTRIBUTE);
  817. } else {
  818. filter = slapi_ch_smprintf("(&(objectclass=nsMappingTree)(|(%s=\"%s\")(%s=%s)))",
  819. MAPPING_TREE_PARENT_ATTRIBUTE,
  820. slapi_sdn_get_dn(target->mtn_subtree),
  821. MAPPING_TREE_PARENT_ATTRIBUTE,
  822. slapi_sdn_get_dn(target->mtn_subtree));
  823. }
  824. slapi_search_internal_set_pb(pb, MAPPING_TREE_BASE_DN, LDAP_SCOPE_ONELEVEL,
  825. filter, NULL, 0, NULL, NULL, (void *) plugin_get_default_component_id(), 0);
  826. slapi_search_internal_pb(pb);
  827. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  828. if (res != LDAP_SUCCESS) {
  829. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: Mapping tree unable to read %s: %d\n",
  830. MAPPING_TREE_BASE_DN, res, 0);
  831. result = -1;
  832. goto done;
  833. }
  834. /* We now create the mapping tree node and call this function for each
  835. * of the target's children. */
  836. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  837. if (NULL == entries) {
  838. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: No mapping tree node entries found under %s\n",
  839. MAPPING_TREE_BASE_DN, 0, 0);
  840. result = -1;
  841. goto done;
  842. }
  843. for(x = 0; entries[x] != NULL; x++) {
  844. mapping_tree_node *child = NULL;
  845. if (LDAP_SUCCESS != mapping_tree_entry_add(entries[x], &child)) {
  846. LDAPDebug(LDAP_DEBUG_ANY,
  847. "ERROR: could not add mapping tree node %s\n",
  848. slapi_entry_get_dn(entries[x]), 0, 0);
  849. result = -1;
  850. goto done;
  851. }
  852. if(target == child){
  853. /* the mapping tree root got replaced
  854. * nothing to do
  855. */
  856. } else {
  857. child->mtn_parent = target;
  858. mapping_tree_node_add_child(target, child);
  859. }
  860. if (mapping_tree_node_get_children(child, 0 /* not the root node */))
  861. {
  862. result = -1;
  863. goto done;
  864. }
  865. }
  866. slapi_free_search_results_internal(pb);
  867. done:
  868. slapi_pblock_destroy(pb);
  869. if (filter)
  870. slapi_ch_free((void **) &filter);
  871. return result;
  872. }
  873. /*
  874. * Description:
  875. * A first attempt at walking over the mapping tree and making sure things
  876. * make sense. Right now it just makes sure that each parent node has a
  877. * subtree that is the suffix of its children's subtrees. This function
  878. * is called recursively.
  879. *
  880. * Arguments:
  881. * The root node of the mapping tree.
  882. *
  883. * Returns:
  884. * Nothing - it just prints warnings. This should probably change.
  885. */
  886. static void
  887. mapping_tree_node_validate(mapping_tree_node *node)
  888. {
  889. mapping_tree_node *child_entry;
  890. /* Call this function for all of nodes children */
  891. for (child_entry = node->mtn_children; child_entry; child_entry = child_entry->mtn_brother) {
  892. mapping_tree_node_validate(child_entry);
  893. }
  894. if (node->mtn_parent) {
  895. if (!slapi_sdn_issuffix(node->mtn_subtree, node->mtn_parent->mtn_subtree)) {
  896. LDAPDebug(LDAP_DEBUG_ANY,
  897. "Warning: Invalid mapping tree. %s can not be a child of %s\n",
  898. slapi_sdn_get_ndn(node->mtn_subtree),
  899. slapi_sdn_get_ndn(node->mtn_parent->mtn_subtree), 0);
  900. }
  901. }
  902. }
  903. static void
  904. mtn_free_referral_in_node (mapping_tree_node *node)
  905. {
  906. char ** referral = node->mtn_referral;
  907. if (referral)
  908. {
  909. int i;
  910. for (i=0; referral[i]; i++)
  911. slapi_ch_free((void **) &(referral[i]));
  912. slapi_ch_free((void **) &referral);
  913. }
  914. if (node->mtn_referral_entry)
  915. slapi_entry_free(node->mtn_referral_entry);
  916. node->mtn_referral = NULL;
  917. node->mtn_referral_entry = NULL;
  918. }
  919. int mapping_tree_entry_modify_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  920. {
  921. LDAPMod **mods;
  922. int i;
  923. mapping_tree_node * node;
  924. Slapi_DN * subtree;
  925. Slapi_Attr * attr;
  926. Slapi_Value *val = NULL;
  927. int be_list_count = 0;
  928. int be_list_size = 0;
  929. backend **backends = NULL;
  930. char **be_names = NULL;
  931. int * be_states = NULL;
  932. char * plugin_fct = NULL;
  933. char * plugin_lib = NULL;
  934. int plugin_flag = 0;
  935. mtn_distrib_fct plugin = NULL;
  936. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  937. subtree = get_subtree_from_entry(entryAfter);
  938. node = mtn_get_mapping_tree_node_by_entry(mapping_tree_root, subtree);
  939. if (node == NULL)
  940. {
  941. /* should never happen */
  942. *returncode = LDAP_OPERATIONS_ERROR;
  943. return SLAPI_DSE_CALLBACK_ERROR;
  944. }
  945. for (i = 0; mods[i] != NULL; i++) {
  946. if ( (strcasecmp(mods[i]->mod_type, "cn") == 0) ||
  947. (strcasecmp(mods[i]->mod_type,
  948. MAPPING_TREE_PARENT_ATTRIBUTE) == 0) )
  949. {
  950. mapping_tree_node * parent_node;
  951. /* if we are deleting this attribute the new parent
  952. * node will be mapping_tree_root
  953. */
  954. if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  955. {
  956. parent_node = mapping_tree_root;
  957. }
  958. else
  959. {
  960. /* we have to find the new parent node */
  961. Slapi_DN *parent_node_dn;
  962. parent_node_dn = get_parent_from_entry(entryAfter);
  963. parent_node = mtn_get_mapping_tree_node_by_entry(
  964. mapping_tree_root, parent_node_dn);
  965. if (parent_node == NULL)
  966. {
  967. parent_node = mapping_tree_root;
  968. LDAPDebug(LDAP_DEBUG_ANY,
  969. "Error: could not find parent for %s\n",
  970. slapi_entry_get_dn(entryAfter), 0, 0);
  971. slapi_sdn_free(&subtree);
  972. *returncode = LDAP_UNWILLING_TO_PERFORM;
  973. return SLAPI_DSE_CALLBACK_ERROR;
  974. }
  975. slapi_sdn_free(&parent_node_dn);
  976. }
  977. mtn_wlock();
  978. /* modifying the parent of a node means moving it to an
  979. * other place of the tree
  980. * this can be done simply by removing it from its old place and
  981. * moving it to the new one
  982. */
  983. mtn_remove_node(node);
  984. mapping_tree_node_add_child(parent_node, node);
  985. node->mtn_parent = parent_node;
  986. mtn_unlock();
  987. }
  988. else if (strcasecmp(mods[i]->mod_type, "nsslapd-backend" ) == 0)
  989. {
  990. slapi_entry_attr_find(entryAfter, "nsslapd-backend", &attr);
  991. if (NULL == attr)
  992. {
  993. /* if nsslapd-backend attribute is empty all backends have
  994. * been suppressed, set backend list to NULL
  995. * checks on the state are done a bit later
  996. */
  997. backends = NULL;
  998. be_names = NULL;
  999. be_states = NULL;
  1000. be_list_count = 0;
  1001. be_list_size = 0;
  1002. }
  1003. else if (get_backends_from_attr(attr, &backends, &be_names,
  1004. &be_states, &be_list_count, &be_list_size, node))
  1005. {
  1006. free_mapping_tree_node_arrays(&backends, &be_names, &be_states, &be_list_count);
  1007. slapi_sdn_free(&subtree);
  1008. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1009. return SLAPI_DSE_CALLBACK_ERROR;
  1010. }
  1011. mtn_wlock();
  1012. if ((backends == NULL) && (node->mtn_state == MTN_BACKEND))
  1013. {
  1014. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "mapping tree entry need at least one nsslapd-backend\n");
  1015. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1016. mtn_unlock();
  1017. free_mapping_tree_node_arrays(&backends, &be_names, &be_states, &be_list_count);
  1018. slapi_sdn_free(&subtree);
  1019. return SLAPI_DSE_CALLBACK_ERROR;
  1020. }
  1021. /* free any old data */
  1022. free_mapping_tree_node_arrays(&node->mtn_be, &node->mtn_backend_names, &node->mtn_be_states, &node->mtn_be_count);
  1023. node->mtn_be_states = be_states;
  1024. node->mtn_be = backends;
  1025. node->mtn_backend_names = be_names;
  1026. node->mtn_be_count = be_list_count;
  1027. node->mtn_be_list_size = be_list_size;
  1028. mtn_unlock();
  1029. }
  1030. else if (strcasecmp(mods[i]->mod_type, "nsslapd-state" ) == 0)
  1031. {
  1032. Slapi_Value * val;
  1033. const char * new_state;
  1034. Slapi_Attr * attr;
  1035. /* state change
  1036. * for now only allow replace
  1037. */
  1038. if (!SLAPI_IS_MOD_REPLACE(mods[i]->mod_op))
  1039. {
  1040. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "must use replace operation to change state\n");
  1041. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1042. slapi_sdn_free(&subtree);
  1043. return SLAPI_DSE_CALLBACK_ERROR;
  1044. }
  1045. if ((mods[i]->mod_bvalues == NULL) || (mods[i]->mod_bvalues[0] == NULL))
  1046. {
  1047. slapi_sdn_free(&subtree);
  1048. *returncode = LDAP_OPERATIONS_ERROR;
  1049. return SLAPI_DSE_CALLBACK_ERROR;
  1050. }
  1051. slapi_entry_attr_find(entryAfter, "nsslapd-state", &attr);
  1052. slapi_attr_first_value(attr, &val);
  1053. new_state = slapi_value_get_string(val);
  1054. if (mtn_state_to_int(new_state, entryAfter) == MTN_BACKEND)
  1055. {
  1056. if (slapi_entry_attr_find(entryAfter, "nsslapd-backend", &attr))
  1057. {
  1058. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-backend before moving to backend state\n");
  1059. slapi_sdn_free(&subtree);
  1060. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1061. return SLAPI_DSE_CALLBACK_ERROR;
  1062. }
  1063. }
  1064. if ((mtn_state_to_int(new_state, entryAfter) == MTN_REFERRAL) ||
  1065. (mtn_state_to_int(new_state, entryAfter) == MTN_REFERRAL_ON_UPDATE))
  1066. {
  1067. if (slapi_entry_attr_find(entryAfter, "nsslapd-referral", &attr))
  1068. {
  1069. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-referral before moving to referral state\n");
  1070. slapi_sdn_free(&subtree);
  1071. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1072. return SLAPI_DSE_CALLBACK_ERROR;
  1073. }
  1074. }
  1075. mtn_wlock();
  1076. node->mtn_state = mtn_state_to_int(new_state, entryAfter);
  1077. mtn_unlock();
  1078. }
  1079. else if (strcasecmp(mods[i]->mod_type, "nsslapd-referral" ) == 0)
  1080. {
  1081. char ** referral;
  1082. mtn_wlock();
  1083. if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)
  1084. || SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  1085. {
  1086. /* delete old referrals, set new ones */
  1087. mtn_free_referral_in_node(node);
  1088. referral = mtn_get_referral_from_entry(entryAfter);
  1089. node->mtn_referral = referral;
  1090. node->mtn_referral_entry =
  1091. referral2entry(referral, slapi_sdn_get_dn(subtree));
  1092. } else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  1093. {
  1094. /* it is not OK to delete the referrals if they are still
  1095. * used
  1096. */
  1097. if ((node->mtn_state == MTN_REFERRAL) ||
  1098. (node->mtn_state == MTN_REFERRAL_ON_UPDATE))
  1099. {
  1100. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1101. "cannot delete referrals in this state\n");
  1102. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1103. mtn_unlock();
  1104. slapi_sdn_free(&subtree);
  1105. return SLAPI_DSE_CALLBACK_ERROR;
  1106. }
  1107. mtn_free_referral_in_node(node);
  1108. } else
  1109. {
  1110. *returncode = LDAP_OPERATIONS_ERROR;
  1111. mtn_unlock();
  1112. slapi_sdn_free(&subtree);
  1113. return SLAPI_DSE_CALLBACK_ERROR;
  1114. }
  1115. mtn_unlock();
  1116. slapi_sdn_free(&subtree);
  1117. *returncode = LDAP_SUCCESS;
  1118. return SLAPI_DSE_CALLBACK_OK;
  1119. }
  1120. else if (strcasecmp(mods[i]->mod_type,
  1121. "nsslapd-distribution-funct" ) == 0)
  1122. {
  1123. if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)
  1124. || SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  1125. {
  1126. slapi_entry_attr_find(entryAfter,
  1127. "nsslapd-distribution-funct", &attr);
  1128. slapi_attr_first_value(attr, &val);
  1129. if (NULL == val) {
  1130. LDAPDebug(LDAP_DEBUG_ANY,
  1131. "Warning: The nsslapd-distribution-funct attribute"
  1132. " has no value for the mapping tree node %s\n",
  1133. slapi_entry_get_dn(entryAfter), 0, 0);
  1134. plugin_fct = NULL;
  1135. }
  1136. plugin_fct = slapi_ch_strdup(slapi_value_get_string(val));
  1137. }
  1138. else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  1139. {
  1140. plugin_fct = NULL;
  1141. }
  1142. plugin_flag = 1;
  1143. }
  1144. else if (strcasecmp(mods[i]->mod_type,
  1145. "nsslapd-distribution-plugin" ) == 0)
  1146. {
  1147. if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)
  1148. || SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  1149. {
  1150. slapi_entry_attr_find(entryAfter,
  1151. "nsslapd-distribution-plugin", &attr);
  1152. slapi_attr_first_value(attr, &val);
  1153. if (NULL == val) {
  1154. LDAPDebug(LDAP_DEBUG_ANY,
  1155. "Warning: The nsslapd-distribution-plugin attribute"
  1156. " has no value for the mapping tree node %s\n",
  1157. slapi_entry_get_dn(entryAfter), 0, 0);
  1158. plugin_lib = NULL;
  1159. }
  1160. plugin_lib = slapi_ch_strdup(slapi_value_get_string(val));
  1161. }
  1162. else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  1163. {
  1164. plugin_lib = NULL;
  1165. }
  1166. plugin_flag = 1;
  1167. }
  1168. }
  1169. /* if distribution plugin has been configured or modified
  1170. * check that the library and function exist
  1171. * and if yes apply the modifications
  1172. */
  1173. if (plugin_flag)
  1174. {
  1175. if (plugin_lib && plugin_fct)
  1176. {
  1177. plugin = (mtn_distrib_fct) sym_load(plugin_lib, plugin_fct, "Entry Distribution", 1);
  1178. if (plugin == NULL)
  1179. {
  1180. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "cannot find distribution plugin\n");
  1181. slapi_ch_free((void **) &plugin_fct);
  1182. slapi_ch_free((void **) &plugin_lib);
  1183. slapi_sdn_free(&subtree);
  1184. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1185. return SLAPI_DSE_CALLBACK_ERROR;
  1186. }
  1187. }
  1188. else if ((plugin_lib == NULL) && (plugin_fct == NULL))
  1189. {
  1190. /* nothing configured -> OK */
  1191. plugin = NULL;
  1192. }
  1193. else
  1194. {
  1195. /* only one parameter configured -> ERROR */
  1196. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1197. "must define distribution function and library\n");
  1198. slapi_ch_free((void **) &plugin_fct);
  1199. slapi_ch_free((void **) &plugin_lib);
  1200. slapi_sdn_free(&subtree);
  1201. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1202. return SLAPI_DSE_CALLBACK_ERROR;
  1203. }
  1204. mtn_wlock();
  1205. if (node->mtn_dstr_plg_lib)
  1206. slapi_ch_free((void **) &node->mtn_dstr_plg_lib);
  1207. node->mtn_dstr_plg_lib = plugin_lib;
  1208. if (node->mtn_dstr_plg_name)
  1209. slapi_ch_free((void **) &node->mtn_dstr_plg_name);
  1210. node->mtn_dstr_plg_name = plugin_fct;
  1211. node->mtn_dstr_plg = plugin;
  1212. mtn_unlock();
  1213. }
  1214. slapi_sdn_free(&subtree);
  1215. return SLAPI_DSE_CALLBACK_OK;
  1216. }
  1217. int mapping_tree_entry_add_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  1218. {
  1219. mapping_tree_node *node;
  1220. int i;
  1221. backend * be;
  1222. /* WARNING
  1223. * for adds we don't need to grab the mapping tree global lock,
  1224. * because the add operation in the tree is atomic because
  1225. * only one pointer is updated in the tree.
  1226. * Should the mapping tree stucture change, this would have to
  1227. * be checked again
  1228. */
  1229. *returncode = mapping_tree_entry_add(entryBefore, &node);
  1230. if (LDAP_SUCCESS != *returncode)
  1231. {
  1232. return SLAPI_DSE_CALLBACK_ERROR;
  1233. }
  1234. if(node && node->mtn_parent != NULL && node != mapping_tree_root )
  1235. {
  1236. /* If the node has a parent and the node is not the mapping tree root,
  1237. * then add it as a child node. Note that the special case when the
  1238. * node is the mapping tree root and has no parent is handled inside
  1239. * the mapping_tree_entry_add() function by replacing the contents of
  1240. * the mapping tree root node with information from the add request.
  1241. */
  1242. mapping_tree_node_add_child(node->mtn_parent, node);
  1243. }
  1244. for (i = 0; ((i < node->mtn_be_count) && (node->mtn_backend_names) &&
  1245. (node->mtn_backend_names[i])); i++)
  1246. {
  1247. if ((be = slapi_be_select_by_instance_name(node->mtn_backend_names[i]))
  1248. && (be->be_state == BE_STATE_STARTED))
  1249. {
  1250. mtn_be_state_change(node->mtn_backend_names[i], SLAPI_BE_STATE_DELETE,
  1251. node->mtn_be_states[i]);
  1252. }
  1253. }
  1254. node->mtn_extension = factory_create_extension(mapping_tree_get_extension_type(), node, NULL);
  1255. return SLAPI_DSE_CALLBACK_OK;
  1256. }
  1257. /* utility function to remove a node from the tree of mapping_tree_node
  1258. */
  1259. static void mtn_remove_node(mapping_tree_node * node)
  1260. {
  1261. if (node->mtn_parent->mtn_children == node)
  1262. node->mtn_parent->mtn_children = node->mtn_brother;
  1263. else
  1264. {
  1265. mapping_tree_node * tmp_node = node->mtn_parent->mtn_children;
  1266. while (tmp_node && (tmp_node->mtn_brother != node))
  1267. tmp_node = tmp_node->mtn_brother;
  1268. PR_ASSERT(tmp_node != NULL);
  1269. tmp_node->mtn_brother = node->mtn_brother;
  1270. }
  1271. node->mtn_brother = NULL;
  1272. }
  1273. int mapping_tree_entry_delete_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  1274. {
  1275. int result;
  1276. mapping_tree_node *node = NULL;
  1277. Slapi_DN * subtree;
  1278. int i;
  1279. int removed = 0;
  1280. mtn_wlock();
  1281. subtree = get_subtree_from_entry(entryBefore);
  1282. if (subtree == NULL)
  1283. {
  1284. /* there is no cn attribute in this entry
  1285. * -> this is not a mapping tree node
  1286. * -> nothing to do
  1287. */
  1288. result = SLAPI_DSE_CALLBACK_OK;
  1289. goto done;
  1290. }
  1291. node = slapi_get_mapping_tree_node_by_dn(subtree);
  1292. if (node == NULL)
  1293. {
  1294. /* should never happen */
  1295. *returncode = LDAP_OPERATIONS_ERROR;
  1296. result = SLAPI_DSE_CALLBACK_ERROR;
  1297. goto done;
  1298. }
  1299. if (slapi_sdn_compare(subtree, node->mtn_subtree))
  1300. {
  1301. /* There is no node associated to this entry
  1302. * -> nothing to do
  1303. */
  1304. result = SLAPI_DSE_CALLBACK_OK;
  1305. goto done;
  1306. }
  1307. /* if node has children we must refuse the delete */
  1308. if (node->mtn_children)
  1309. {
  1310. result = SLAPI_DSE_CALLBACK_ERROR;
  1311. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1312. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "this node has some children");
  1313. goto done;
  1314. }
  1315. /* at this point the node should be different from mapping_tree_root
  1316. * and therefore have a parent
  1317. */
  1318. PR_ASSERT(node->mtn_parent != NULL);
  1319. /* lets get the node out of the mapping tree */
  1320. mtn_remove_node(node);
  1321. result = SLAPI_DSE_CALLBACK_OK;
  1322. removed = 1;
  1323. done:
  1324. mtn_unlock();
  1325. slapi_sdn_free(&subtree);
  1326. if (SLAPI_DSE_CALLBACK_OK == result && removed)
  1327. {
  1328. /* Signal the plugins that a new backend-suffix has been deleted
  1329. * rq : we have to unlock the mapping tree in that case because
  1330. * most of the plugins will try to search upon this notification
  1331. * and should we keep the lock we would end with a dead-lock
  1332. */
  1333. for (i = 0; ((i < node->mtn_be_count) && (node->mtn_backend_names) &&
  1334. (node->mtn_backend_names[i])); i++)
  1335. {
  1336. if ((node->mtn_be_states[i] != SLAPI_BE_STATE_DELETE) &&
  1337. (NULL != slapi_be_select_by_instance_name(
  1338. node->mtn_backend_names[i])))
  1339. {
  1340. mtn_be_state_change(node->mtn_backend_names[i],
  1341. node->mtn_be_states[i], SLAPI_BE_STATE_DELETE);
  1342. }
  1343. }
  1344. /* at this point the node is out of the mapping tree,
  1345. * we can now free the structure
  1346. */
  1347. mtn_free_node(&node);
  1348. }
  1349. return result;
  1350. }
  1351. /*
  1352. * Add an internal mapping tree node.
  1353. */
  1354. static mapping_tree_node *
  1355. add_internal_mapping_tree_node(const char *subtree, Slapi_Backend *be, mapping_tree_node *parent)
  1356. {
  1357. Slapi_DN *dn;
  1358. mapping_tree_node *node;
  1359. backend ** be_list = (backend **) slapi_ch_malloc(sizeof(backend **));
  1360. be_list[0] = be;
  1361. dn = slapi_sdn_new_dn_byval(subtree);
  1362. node= mapping_tree_node_new(
  1363. dn,
  1364. be_list,
  1365. NULL, /* backend_name */
  1366. NULL,
  1367. 1, /* number of backends at this node */
  1368. 1, /* size of backend list structure */
  1369. NULL, /* referral */
  1370. parent,
  1371. MTN_BACKEND,
  1372. 1, /* The config node is a private node.
  1373. * People can't see or change it. */
  1374. NULL, NULL, NULL);
  1375. return node;
  1376. }
  1377. /*
  1378. * Description:
  1379. * Inits the mapping tree. The mapping tree is rooted at a node with
  1380. * subtree "". Think of this node as the node for the NULL suffix
  1381. * even though we don't really support it. This function will
  1382. * create the root node and then consult the DIT for the rest of
  1383. * the nodes. It will also add the node for cn=config.
  1384. *
  1385. * One thing to note... Until the mapping tree is inited. We use
  1386. * slapi_be_select for all our selection needs. To read in the mapping
  1387. * tree from the DIT, we need to some internal operations. These
  1388. * operations need to use slapi_be_select.
  1389. *
  1390. * Arguments:
  1391. * Nothing
  1392. *
  1393. * Returns:
  1394. * Right now it always returns 0. This will most likely change. Right
  1395. * now, we just log warnings when ever something goes wrong.
  1396. */
  1397. int
  1398. mapping_tree_init()
  1399. {
  1400. Slapi_Backend *be;
  1401. mapping_tree_node *node;
  1402. /* Create the root of the mapping tree. */
  1403. /* The root of the mapping tree is the NULL suffix. It's always there,
  1404. * but, because we don't really support it, we won't have an entry in
  1405. * the dit for the NULL suffix mapping tree node. */
  1406. /* Once we support the NULL suffix we should do something more clever here.
  1407. * For now will use the current backend we use for "" */
  1408. /* I'm not really sure what the state of the root node should be. The root
  1409. * node will end up being selected if none of the suffices for the backends
  1410. * would work with the target. For now when the root node is selected,
  1411. * the default backend will be returned. (The special case where the
  1412. * target dn is "" is handled differently.) */
  1413. /* we call this function from a single thread, so it should be ok */
  1414. if(mapping_tree_freed){
  1415. /* shutdown has been detected */
  1416. return 0;
  1417. }
  1418. if (mapping_tree_inited)
  1419. return 0;
  1420. /* ONREPL - I have moved this up because otherwise we can endup calling this
  1421. * function recursively */
  1422. mapping_tree_inited = 1;
  1423. slapi_register_supported_control(MTN_CONTROL_USE_ONE_BACKEND_OID,
  1424. SLAPI_OPERATION_SEARCH);
  1425. slapi_register_supported_control(MTN_CONTROL_USE_ONE_BACKEND_EXT_OID,
  1426. SLAPI_OPERATION_SEARCH);
  1427. myLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "mapping tree");
  1428. be= slapi_be_select_by_instance_name(DSE_BACKEND);
  1429. mapping_tree_root= add_internal_mapping_tree_node("", be, NULL);
  1430. /* We also need to add the config and schema backends to the mapping tree.
  1431. * They are special in that users will not know about it's node in the
  1432. * mapping tree. This is to prevent them from disableing it or
  1433. * returning a referral for it. */
  1434. node= add_internal_mapping_tree_node("cn=config", be, mapping_tree_root);
  1435. mapping_tree_node_add_child(mapping_tree_root, node);
  1436. node= add_internal_mapping_tree_node("cn=monitor", be, mapping_tree_root);
  1437. mapping_tree_node_add_child(mapping_tree_root, node);
  1438. be= slapi_be_select_by_instance_name( DSE_SCHEMA );
  1439. node= add_internal_mapping_tree_node("cn=schema", be, mapping_tree_root);
  1440. mapping_tree_node_add_child(mapping_tree_root, node);
  1441. /*
  1442. * Now we need to look under cn=mapping tree, cn=config to find the rest
  1443. * of the mapping tree entries.
  1444. * Builds the mapping tree from entries in the DIT. This function just
  1445. * calls mapping_tree_node_get_children with the special case for the
  1446. * root node.
  1447. */
  1448. if (mapping_tree_node_get_children(mapping_tree_root, 1))
  1449. return -1;
  1450. mtn_create_extension(mapping_tree_root);
  1451. /* setup the dse callback functions for the ldbm instance config entry */
  1452. {
  1453. slapi_config_register_callback(SLAPI_OPERATION_MODIFY,
  1454. DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
  1455. "(objectclass=nsMappingTree)",
  1456. mapping_tree_entry_modify_callback, NULL);
  1457. slapi_config_register_callback(SLAPI_OPERATION_ADD,
  1458. DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
  1459. "(objectclass=nsMappingTree)", mapping_tree_entry_add_callback,
  1460. NULL);
  1461. slapi_config_register_callback(SLAPI_OPERATION_DELETE,
  1462. DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
  1463. "(objectclass=nsMappingTree)", mapping_tree_entry_delete_callback,
  1464. NULL);
  1465. }
  1466. return 0;
  1467. }
  1468. static void
  1469. mtn_free_node (mapping_tree_node **node)
  1470. {
  1471. mapping_tree_node *child = (*node)->mtn_children;
  1472. /* free children first */
  1473. while (child)
  1474. {
  1475. mapping_tree_node * tmp_child = child->mtn_brother;
  1476. mtn_free_node (&child);
  1477. child = tmp_child;
  1478. }
  1479. (*node)->mtn_children = NULL;
  1480. (*node)->mtn_parent = NULL;
  1481. /* free this node */
  1482. /* ONREPL - not quite sure which fields should be freed. For now,
  1483. only freeing fields explicitely allocated in the new_node function */
  1484. factory_destroy_extension (mapping_tree_get_extension_type(), *node, NULL,
  1485. &((*node)->mtn_extension));
  1486. slapi_sdn_free(&((*node)->mtn_subtree));
  1487. mtn_free_referral_in_node(*node);
  1488. if ((*node)->mtn_be_count > 0)
  1489. {
  1490. free_mapping_tree_node_arrays(&((*node)->mtn_be), &((*node)->mtn_backend_names),
  1491. &((*node)->mtn_be_states), &((*node)->mtn_be_count));
  1492. }
  1493. slapi_ch_free_string(&((*node)->mtn_dstr_plg_lib));
  1494. slapi_ch_free_string(&((*node)->mtn_dstr_plg_name));
  1495. slapi_ch_free ((void**) node);
  1496. }
  1497. /* Description: frees the tree; should be called when the server shuts down
  1498. */
  1499. void
  1500. mapping_tree_free ()
  1501. {
  1502. /* unregister dse callbacks */
  1503. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_modify_callback);
  1504. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_add_callback);
  1505. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_delete_callback);
  1506. /* The state change plugins registered on the mapping tree
  1507. * should not get any state change information
  1508. * - unregister all those callbacks
  1509. */
  1510. slapi_unregister_backend_state_change_all();
  1511. /* recursively free tree nodes */
  1512. mtn_free_node (&mapping_tree_root);
  1513. mapping_tree_freed = 1;
  1514. }
  1515. /* This function returns the first node to parse when a search is done
  1516. * on a given node in the mapping tree
  1517. */
  1518. static mapping_tree_node *
  1519. mtn_get_first_node(mapping_tree_node * node, int scope)
  1520. {
  1521. if (node == NULL)
  1522. return NULL;
  1523. /* never climb down the tree from base "" */
  1524. if (node == mapping_tree_root)
  1525. {
  1526. return node;
  1527. }
  1528. if (scope == LDAP_SCOPE_BASE)
  1529. return node;
  1530. if (scope == LDAP_SCOPE_ONELEVEL)
  1531. {
  1532. if (node->mtn_children)
  1533. return node->mtn_children;
  1534. else
  1535. return node;
  1536. }
  1537. while (node->mtn_children)
  1538. node = node->mtn_children;
  1539. return node;
  1540. }
  1541. int slapi_mtn_get_first_be(mapping_tree_node * node_list,
  1542. mapping_tree_node ** node, Slapi_PBlock *pb, Slapi_Backend **be,
  1543. int * be_index, Slapi_Entry **referral, char *errorbuf, int scope)
  1544. {
  1545. *node = mtn_get_first_node(node_list, scope);
  1546. if (scope == LDAP_SCOPE_BASE)
  1547. *be_index = -1;
  1548. else
  1549. *be_index = 0;
  1550. return mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
  1551. }
  1552. int slapi_mtn_get_next_be(mapping_tree_node * node_list,
  1553. mapping_tree_node ** node, Slapi_PBlock *pb, Slapi_Backend **be,
  1554. int * be_index, Slapi_Entry **referral, char *errorbuf, int scope)
  1555. {
  1556. int rc;
  1557. if (((*node)->mtn_parent == NULL) || /* -> node has been deleted */
  1558. (scope == LDAP_SCOPE_BASE))
  1559. {
  1560. *node = NULL;
  1561. *be = NULL;
  1562. *referral = NULL;
  1563. return 0;
  1564. }
  1565. /* never climb down the tree from the rootDSE */
  1566. if (node_list == mapping_tree_root)
  1567. {
  1568. *node = NULL;
  1569. *be = NULL;
  1570. *referral = NULL;
  1571. return 0;
  1572. }
  1573. rc = mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
  1574. if (rc != LDAP_SUCCESS)
  1575. {
  1576. *node = mtn_get_next_node(*node, node_list, scope);
  1577. return rc;
  1578. }
  1579. if ((*be == NULL) && (*referral == NULL))
  1580. {
  1581. *node = mtn_get_next_node(*node, node_list, scope);
  1582. if (*node == NULL)
  1583. {
  1584. *be = NULL;
  1585. return 0;
  1586. }
  1587. *be_index = 0;
  1588. return mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
  1589. }
  1590. return LDAP_SUCCESS;
  1591. }
  1592. /* This function returns the next node to parse when a subtree search is done
  1593. * on a given node in the mapping tree
  1594. */
  1595. static mapping_tree_node *
  1596. mtn_get_next_node(mapping_tree_node * node, mapping_tree_node * node_list, int scope)
  1597. {
  1598. if (scope == LDAP_SCOPE_BASE)
  1599. return NULL;
  1600. /* if we are back to the top of the subtree searched then we have finished */
  1601. if (node == node_list)
  1602. node = NULL;
  1603. else if (node->mtn_brother)
  1604. {
  1605. node = node->mtn_brother;
  1606. if (scope == LDAP_SCOPE_SUBTREE)
  1607. while (node->mtn_children)
  1608. node = node->mtn_children;
  1609. }
  1610. else
  1611. node = node->mtn_parent;
  1612. return node;
  1613. }
  1614. /* Description :
  1615. * return 0 if the given entry does not have any child node in the mapping tree
  1616. * != otherwise
  1617. *
  1618. */
  1619. int
  1620. mtn_sdn_has_child(Slapi_DN *target_sdn)
  1621. {
  1622. mapping_tree_node *node;
  1623. /* algo : get the target node for the given dn
  1624. * then loop through all its child to check if one of them is below
  1625. * the target dn
  1626. */
  1627. node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1628. /* if there is no node for this dn then there is no child either */
  1629. if (node == NULL)
  1630. return 0;
  1631. node = node->mtn_children;
  1632. while (node)
  1633. {
  1634. if (slapi_sdn_issuffix(node->mtn_subtree, target_sdn))
  1635. return 1;
  1636. node = node->mtn_brother;
  1637. }
  1638. return 0;
  1639. }
  1640. /* Description:
  1641. * Find the backend that would be used to store a dn.
  1642. */
  1643. Slapi_Backend *slapi_mapping_tree_find_backend_for_sdn(Slapi_DN *sdn)
  1644. {
  1645. mapping_tree_node *target_node;
  1646. Slapi_Backend *be;
  1647. int flag_stop = 0, index;
  1648. Slapi_PBlock *pb;
  1649. Slapi_Operation *op;
  1650. mtn_lock();
  1651. target_node = slapi_get_mapping_tree_node_by_dn(sdn);
  1652. if ((target_node == mapping_tree_root) &&
  1653. (slapi_sdn_get_ndn_len(sdn) > 0)) {
  1654. /* couldn't find a matching node */
  1655. be = defbackend_get_backend();
  1656. goto done;
  1657. }
  1658. if ((target_node == NULL) || (target_node->mtn_be_count == 0)) {
  1659. /* no backend configured for this node */
  1660. be = NULL;
  1661. goto done;
  1662. }
  1663. if (target_node->mtn_be_count == 1) {
  1664. /* not distributed, so we've already found it */
  1665. if (target_node->mtn_be[0] == NULL) {
  1666. target_node->mtn_be[0] = slapi_be_select_by_instance_name(
  1667. target_node->mtn_backend_names[0]);
  1668. }
  1669. be = target_node->mtn_be[0];
  1670. goto done;
  1671. }
  1672. /* have to call the distribution plugin */
  1673. be = defbackend_get_backend();
  1674. pb = slapi_pblock_new();
  1675. if (!pb) {
  1676. goto done;
  1677. }
  1678. op = internal_operation_new(SLAPI_OPERATION_ADD, 0);
  1679. if (!op) {
  1680. slapi_pblock_destroy(pb);
  1681. goto done;
  1682. }
  1683. operation_set_target_spec(op, sdn);
  1684. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  1685. index = mtn_get_be_distributed(pb, target_node, sdn, &flag_stop);
  1686. slapi_pblock_destroy(pb); /* also frees the operation */
  1687. if (target_node->mtn_be[index] == NULL) {
  1688. target_node->mtn_be[index] = slapi_be_select_by_instance_name(
  1689. target_node->mtn_backend_names[index]);
  1690. }
  1691. be = target_node->mtn_be[index];
  1692. done:
  1693. mtn_unlock();
  1694. return be;
  1695. }
  1696. /* Check if the target dn is '\0' - the null dn */
  1697. static int sdn_is_nulldn(const Slapi_DN *sdn){
  1698. if(sdn){
  1699. const char *dn= slapi_sdn_get_ndn(sdn);
  1700. if(dn && ( '\0' == *dn)){
  1701. return 1;
  1702. }
  1703. }
  1704. return 0;
  1705. }
  1706. /* Checks if a write operation for a particular DN would
  1707. * require a referral to be sent. */
  1708. int slapi_dn_write_needs_referral(Slapi_DN *target_sdn, Slapi_Entry **referral)
  1709. {
  1710. mapping_tree_node *target_node = NULL;
  1711. int ret = 0;
  1712. if(mapping_tree_freed){
  1713. /* shutdown detected */
  1714. goto done;
  1715. }
  1716. if(!mapping_tree_inited) {
  1717. mapping_tree_init();
  1718. }
  1719. if (target_sdn) {
  1720. mtn_lock();
  1721. /* Get the mapping tree node that is the best match for the target dn. */
  1722. target_node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1723. if (target_node == NULL) {
  1724. target_node = mapping_tree_root;
  1725. }
  1726. /* See if we need to return a referral. */
  1727. if ((target_node->mtn_state == MTN_REFERRAL) ||
  1728. (target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) {
  1729. *referral = (target_node->mtn_referral_entry ?
  1730. slapi_entry_dup(target_node->mtn_referral_entry) :
  1731. NULL);
  1732. if (*referral) {
  1733. ret = 1;
  1734. }
  1735. }
  1736. mtn_unlock();
  1737. }
  1738. done:
  1739. return ret;
  1740. }
  1741. /*
  1742. * Description:
  1743. * The reason we have a mapping tree. This function selects a backend or
  1744. * referral to handle a given request. Uses the target of the operation to
  1745. * find a mapping tree node, then based on the operation type, bind dn, state
  1746. * of the node, etc. it selects a backend or referral.
  1747. *
  1748. * In this initial implementation of the mapping tree, each node can only have
  1749. * one backend and one referral. Later we should change this so each node has
  1750. * a list of backends and a list of referrals. Then we should add a modifier
  1751. * to the state of the node. For example, MTN_MODIFIER_ROUND_ROBIN could be a
  1752. * modifer on the way a backend or referral is returned from the lists.
  1753. *
  1754. * Arguments:
  1755. * pb is the pblock being used to service the operation.
  1756. * be is an output param that will be set to the selected backend.
  1757. * referral is an output param that will be set to the selected referral.
  1758. * errorbuf is a pointer to a buffer that an error string will be written to
  1759. * if there is an error. The caller is responsible for passing in a big
  1760. * enough chunk of memory. BUFSIZ should be fine. If errorbuf is NULL,
  1761. * no error string is written to it. The string returned in errorbuf
  1762. * would be a good candidate for sending back to the client to describe the
  1763. * error.
  1764. *
  1765. * Returns:
  1766. * LDAP_SUCCESS on success, other LDAP result codes if there is a problem.
  1767. */
  1768. int slapi_mapping_tree_select(Slapi_PBlock *pb, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
  1769. {
  1770. Slapi_DN *target_sdn = NULL;
  1771. mapping_tree_node *target_node;
  1772. Slapi_Operation *op;
  1773. int index;
  1774. int ret;
  1775. int scope=LDAP_SCOPE_BASE;
  1776. int op_type;
  1777. if(mapping_tree_freed){
  1778. /* shutdown detected */
  1779. return LDAP_OPERATIONS_ERROR;
  1780. }
  1781. if (errorbuf) {
  1782. errorbuf[0] = '\0';
  1783. }
  1784. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1785. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  1786. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  1787. /* Get the target for this op */
  1788. target_sdn = operation_get_target_spec (op);
  1789. if(!mapping_tree_inited) {
  1790. mapping_tree_init();
  1791. }
  1792. be[0] = NULL;
  1793. referral[0] = NULL;
  1794. mtn_lock();
  1795. /* Get the mapping tree node that is the best match for the target dn. */
  1796. target_node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1797. if (target_node == NULL)
  1798. target_node = mapping_tree_root;
  1799. /* The processing of the base scope root DSE search and all other LDAP operations on ""
  1800. * will be transferred to the internal DSE backend
  1801. */
  1802. if( sdn_is_nulldn(target_sdn) &&
  1803. (((op_type == SLAPI_OPERATION_SEARCH) && (scope == LDAP_SCOPE_BASE)) ||
  1804. (op_type != SLAPI_OPERATION_SEARCH)) ) {
  1805. mtn_unlock();
  1806. *be = slapi_be_select_by_instance_name(DSE_BACKEND);
  1807. if(*be != NULL && !be_isdeleted(*be))
  1808. {
  1809. ret = LDAP_SUCCESS;
  1810. slapi_be_Rlock(*be); /* also done inside mtn_get_be() below */
  1811. } else {
  1812. ret = LDAP_OPERATIONS_ERROR;
  1813. }
  1814. return ret;
  1815. }
  1816. /* index == -1 is used to specify that we want only one backend not a list
  1817. * used for BASE search, ADD, DELETE, MODIFY
  1818. */
  1819. index = -1;
  1820. ret = mtn_get_be(target_node, pb, be, &index, referral, errorbuf);
  1821. slapi_pblock_set(pb, SLAPI_BACKEND_COUNT, &index);
  1822. mtn_unlock();
  1823. /* if a backend was returned, make sure that all non-search operations
  1824. * fail if the backend is read-only,
  1825. * or if the whole server is readonly AND backend is public (!private)
  1826. */
  1827. if ((ret == LDAP_SUCCESS) && *be && !be_isdeleted(*be) &&
  1828. ((*be)->be_readonly ||
  1829. (slapi_config_get_readonly() && !slapi_be_private(*be)))) {
  1830. unsigned long op_type = operation_get_type(op);
  1831. if ((op_type != SLAPI_OPERATION_SEARCH) &&
  1832. (op_type != SLAPI_OPERATION_COMPARE) &&
  1833. (op_type != SLAPI_OPERATION_BIND) &&
  1834. (op_type != SLAPI_OPERATION_UNBIND)) {
  1835. ret = LDAP_UNWILLING_TO_PERFORM;
  1836. PL_strncpyz(errorbuf, slapi_config_get_readonly() ?
  1837. "Server is read-only" :
  1838. "database is read-only", BUFSIZ);
  1839. slapi_be_Unlock(*be);
  1840. *be = NULL;
  1841. }
  1842. }
  1843. return ret;
  1844. }
  1845. int slapi_mapping_tree_select_all(Slapi_PBlock *pb, Slapi_Backend **be_list,
  1846. Slapi_Entry **referral_list, char *errorbuf)
  1847. {
  1848. Slapi_DN *target_sdn = NULL;
  1849. mapping_tree_node *node_list;
  1850. mapping_tree_node *node;
  1851. Slapi_Operation *op;
  1852. int index;
  1853. int ret;
  1854. int ret_code = LDAP_SUCCESS;
  1855. int be_index = 0 ;
  1856. int referral_index = 0 ;
  1857. Slapi_Backend * be;
  1858. Slapi_Entry * referral;
  1859. int scope = LDAP_SCOPE_BASE;
  1860. Slapi_DN sdn;
  1861. char *base;
  1862. int flag_partial_result = 0;
  1863. int op_type;
  1864. if(mapping_tree_freed){
  1865. return LDAP_OPERATIONS_ERROR;
  1866. }
  1867. if (errorbuf) {
  1868. errorbuf[0] = '\0';
  1869. }
  1870. /* get the operational parameters */
  1871. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &base);
  1872. slapi_sdn_init_dn_ndn_byref(&sdn, base); /* normalized in opshared.c */
  1873. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1874. target_sdn = operation_get_target_spec (op);
  1875. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  1876. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  1877. if(!mapping_tree_inited){
  1878. mapping_tree_init();
  1879. }
  1880. mtn_lock();
  1881. be_list[0] = NULL;
  1882. referral_list[0] = NULL;
  1883. /* Get the mapping tree node that is the best match for the target dn. */
  1884. node_list = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1885. if (node_list == NULL)
  1886. node_list = mapping_tree_root;
  1887. if( sdn_is_nulldn(target_sdn) && ( op_type == SLAPI_OPERATION_SEARCH)
  1888. && (scope == LDAP_SCOPE_BASE) ) {
  1889. mtn_unlock();
  1890. be = slapi_be_select_by_instance_name(DSE_BACKEND);
  1891. if(be != NULL && !be_isdeleted(be))
  1892. {
  1893. be_list[0]=be;
  1894. be_list[1] = NULL;
  1895. ret_code = LDAP_SUCCESS;
  1896. slapi_be_Rlock(be); /* also done inside mtn_get_be() below */
  1897. } else {
  1898. ret_code = LDAP_OPERATIONS_ERROR;
  1899. }
  1900. return ret_code;
  1901. }
  1902. ret = slapi_mtn_get_first_be(node_list, &node, pb, &be, &index, &referral, errorbuf, scope);
  1903. while ((node) &&(index < BE_LIST_SIZE))
  1904. {
  1905. if (ret != LDAP_SUCCESS)
  1906. {
  1907. /* flag we have problems at least on part of the tree */
  1908. flag_partial_result = 1;
  1909. }
  1910. else if ( ( ((!slapi_sdn_issuffix(&sdn, slapi_mtn_get_dn(node))
  1911. && !slapi_sdn_issuffix(slapi_mtn_get_dn(node), &sdn)))
  1912. || ((node_list == mapping_tree_root) && node->mtn_private
  1913. && (scope != LDAP_SCOPE_BASE)) )
  1914. && (!be || strncmp(be->be_name, "default", 8)))
  1915. {
  1916. if (be && !be_isdeleted(be))
  1917. {
  1918. /* wrong backend or referall, ignore it */
  1919. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  1920. "mapping tree release backend : %s\n",
  1921. slapi_be_get_name(be));
  1922. slapi_be_Unlock(be);
  1923. }
  1924. }
  1925. else
  1926. {
  1927. if (be && !be_isdeleted(be))
  1928. {
  1929. be_list[be_index++]=be;
  1930. }
  1931. if (referral)
  1932. {
  1933. referral_list[referral_index++] = referral;
  1934. /* if we hit a referral at the base of the search
  1935. * we must return a REFERRAL error with only this referral
  1936. * all backend or referral below this node are ignored
  1937. */
  1938. if (slapi_sdn_issuffix(target_sdn, slapi_mtn_get_dn(node)))
  1939. {
  1940. ret_code = LDAP_REFERRAL;
  1941. break; /* get out of the while loop */
  1942. }
  1943. }
  1944. }
  1945. ret = slapi_mtn_get_next_be(node_list, &node, pb, &be, &index,
  1946. &referral, errorbuf, scope);
  1947. }
  1948. mtn_unlock();
  1949. slapi_sdn_done(&sdn);
  1950. be_list[be_index] = NULL;
  1951. referral_list[referral_index] = NULL;
  1952. if (flag_partial_result)
  1953. {
  1954. /* if no node in active has been found -> return LDAP_OPERATIONS_ERROR
  1955. * but if only part of the nodes are disabled
  1956. * do not return an error to allow directory browser to work OK
  1957. * in the console
  1958. * It would be better to return a meaningfull error
  1959. * unfortunately LDAP_PARTIAL_RESULTS is not usable because
  1960. * it is already used for V2 referrals
  1961. * leave no error for now and fix this later
  1962. */
  1963. if ((be_index == 0) && (referral_index == 0))
  1964. return LDAP_OPERATIONS_ERROR;
  1965. else
  1966. return ret_code;
  1967. }
  1968. else
  1969. return ret_code;
  1970. }
  1971. void slapi_mapping_tree_free_all(Slapi_Backend **be_list, Slapi_Entry **referral_list)
  1972. {
  1973. int index = 0;
  1974. /* go through the list of all backends that was used for the operation
  1975. * and unlock them
  1976. * go through the list of referrals and free them
  1977. * free the two tables that were used to store the two lists
  1978. */
  1979. if (be_list[index] != NULL)
  1980. {
  1981. Slapi_Backend * be;
  1982. while ((be = be_list[index++]))
  1983. {
  1984. slapi_log_error(SLAPI_LOG_ARGS, NULL, "mapping tree release backend : %s\n", slapi_be_get_name(be));
  1985. slapi_be_Unlock(be);
  1986. }
  1987. }
  1988. index = 0;
  1989. if (referral_list[index] != NULL)
  1990. {
  1991. Slapi_Entry * referral;
  1992. while ((referral = referral_list[index++]))
  1993. {
  1994. slapi_entry_free(referral);
  1995. }
  1996. }
  1997. }
  1998. /* same as slapi_mapping_tree_select() but will also check that the supplied
  1999. * newdn is in the same backend
  2000. */
  2001. int slapi_mapping_tree_select_and_check(Slapi_PBlock *pb,char *newdn, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
  2002. {
  2003. Slapi_DN *target_sdn = NULL;
  2004. Slapi_DN dn_newdn;
  2005. Slapi_Backend * new_be = NULL;
  2006. Slapi_Entry * new_referral = NULL;
  2007. mapping_tree_node *target_node;
  2008. int index;
  2009. Slapi_Operation *op;
  2010. int ret;
  2011. int need_unlock = 0;
  2012. if(mapping_tree_freed){
  2013. return LDAP_OPERATIONS_ERROR;
  2014. }
  2015. slapi_sdn_init(&dn_newdn);
  2016. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2017. target_sdn = operation_get_target_spec (op);
  2018. * referral = NULL;
  2019. ret = slapi_mapping_tree_select(pb, be, referral, errorbuf);
  2020. if (ret)
  2021. goto unlock_and_return;
  2022. slapi_sdn_init_dn_byref(&dn_newdn,newdn);
  2023. /* acquire lock now, after slapi_mapping_tree_select() which also locks,
  2024. because we are accessing mt internals */
  2025. mtn_lock();
  2026. need_unlock = 1; /* we have now acquired the lock */
  2027. target_node = slapi_get_mapping_tree_node_by_dn(&dn_newdn);
  2028. if (target_node == NULL)
  2029. target_node = mapping_tree_root;
  2030. index = -1;
  2031. ret = mtn_get_be(target_node, pb, &new_be, &index, &new_referral, errorbuf);
  2032. if (ret)
  2033. goto unlock_and_return;
  2034. if (*be)
  2035. {
  2036. /* suffix is a part of mapping tree. We should not free it */
  2037. const Slapi_DN *suffix = slapi_get_suffix_by_dn(target_sdn);
  2038. if (NULL == suffix)
  2039. {
  2040. ret = LDAP_NO_SUCH_OBJECT;
  2041. PR_snprintf(errorbuf, BUFSIZ,
  2042. "Target entry \"%s\" does not exist\n",
  2043. slapi_sdn_get_dn(target_sdn));
  2044. goto unlock_and_return;
  2045. }
  2046. if (0 == slapi_sdn_compare(target_sdn, suffix))
  2047. {
  2048. /* target_sdn is a suffix */
  2049. const Slapi_DN *new_suffix = NULL;
  2050. /* new_suffix is a part of mapping tree. We should not free it */
  2051. new_suffix = slapi_get_suffix_by_dn(&dn_newdn);
  2052. if (!slapi_be_exist((const Slapi_DN *)&dn_newdn))
  2053. {
  2054. /* new_be is an empty backend */
  2055. ret = LDAP_NO_SUCH_OBJECT;
  2056. PR_snprintf(errorbuf, BUFSIZ,
  2057. "Backend for suffix \"%s\" does not exist\n", newdn);
  2058. goto unlock_and_return;
  2059. }
  2060. if (0 == slapi_sdn_compare(&dn_newdn, new_suffix))
  2061. {
  2062. ret = LDAP_ALREADY_EXISTS;
  2063. PR_snprintf(errorbuf, BUFSIZ,
  2064. "Suffix \"%s\" already exists\n", newdn);
  2065. goto unlock_and_return;
  2066. }
  2067. ret = LDAP_NAMING_VIOLATION;
  2068. PR_snprintf(errorbuf, BUFSIZ, "Cannot rename suffix \"%s\"\n",
  2069. slapi_sdn_get_dn(target_sdn));
  2070. goto unlock_and_return;
  2071. }
  2072. else
  2073. {
  2074. if ((*be != new_be) || mtn_sdn_has_child(target_sdn))
  2075. {
  2076. ret = LDAP_AFFECTS_MULTIPLE_DSAS;
  2077. PR_snprintf(errorbuf, BUFSIZ,
  2078. "Cannot move entries accross backends\n");
  2079. goto unlock_and_return;
  2080. }
  2081. }
  2082. }
  2083. unlock_and_return:
  2084. /* if slapi_mapping_tree_select failed, we won't have the lock */
  2085. if (need_unlock) {
  2086. mtn_unlock();
  2087. }
  2088. slapi_sdn_done(&dn_newdn);
  2089. if (new_be)
  2090. slapi_be_Unlock(new_be);
  2091. if (new_referral)
  2092. slapi_entry_free(new_referral);
  2093. if (ret != LDAP_SUCCESS)
  2094. {
  2095. if (be && *be && !be_isdeleted(*be))
  2096. {
  2097. slapi_be_Unlock(*be);
  2098. *be = NULL;
  2099. }
  2100. if (*referral)
  2101. {
  2102. slapi_entry_free(*referral);
  2103. *referral = NULL;
  2104. }
  2105. }
  2106. return ret;
  2107. }
  2108. /*
  2109. * allow to solve the distribution problem when several back-ends are defined
  2110. */
  2111. static int
  2112. mtn_get_be_distributed(Slapi_PBlock *pb, mapping_tree_node * target_node,
  2113. Slapi_DN *target_sdn, int * flag_stop)
  2114. {
  2115. int index;
  2116. *flag_stop = 0;
  2117. if (target_node->mtn_dstr_plg)
  2118. {
  2119. index = (*target_node->mtn_dstr_plg)(pb, target_sdn,
  2120. target_node->mtn_backend_names, target_node->mtn_be_count,
  2121. target_node->mtn_subtree, target_node->mtn_be_states);
  2122. if (index == SLAPI_BE_ALL_BACKENDS)
  2123. {
  2124. /* special value to indicate all backends must be scanned
  2125. * start with first one
  2126. */
  2127. index = 0;
  2128. }
  2129. /* paranoid check, never trust another programmer */
  2130. else if ((index >= target_node->mtn_be_count) || (index < 0))
  2131. {
  2132. LDAPDebug(LDAP_DEBUG_ANY,
  2133. "Warning: distribution plugin returned wrong backend"
  2134. " : %d for entry %s at node %s\n",
  2135. index, slapi_sdn_get_ndn(target_sdn),
  2136. slapi_sdn_get_ndn(target_node->mtn_subtree));
  2137. index = 0;
  2138. }
  2139. else
  2140. {
  2141. /* only one backend to scan
  2142. * set flag_stop to indicate we must stop the search here
  2143. */
  2144. *flag_stop = 1;
  2145. }
  2146. }
  2147. else
  2148. {
  2149. /* there is several backends but no distribution function
  2150. * return the first backend
  2151. */
  2152. LDAPDebug(LDAP_DEBUG_ANY,
  2153. "Warning: distribution plugin not configured at node : %s\n",
  2154. slapi_sdn_get_ndn(target_node->mtn_subtree), 0, 0);
  2155. index = 0;
  2156. }
  2157. return index;
  2158. }
  2159. /*
  2160. * this function is in charge of choosing the right backend for a given
  2161. * mapping tree node
  2162. * In case when several backends are used it is in charge of the spanning the
  2163. * request among all the backend or choosing the only backend to use depending
  2164. * on the type and scope of the LDAP operation
  2165. *
  2166. * index == -1 is used to specify that we want only the one best backend
  2167. * used for BASE search, ADD, DELETE, MODIFY
  2168. * index >0 means we are doing a SUBTREE or ONELEVEL search and that the be in
  2169. * that position must be returned
  2170. */
  2171. static int mtn_get_be(mapping_tree_node *target_node, Slapi_PBlock *pb,
  2172. Slapi_Backend **be, int * index, Slapi_Entry **referral, char *errorbuf)
  2173. {
  2174. Slapi_DN *target_sdn;
  2175. Slapi_Operation *op;
  2176. int result = LDAP_SUCCESS;
  2177. int override_referral = 0;
  2178. unsigned long op_type;
  2179. int flag_stop = 0;
  2180. struct slapi_componentid *cid = NULL;
  2181. if(mapping_tree_freed){
  2182. /* shut down detected */
  2183. return LDAP_OPERATIONS_ERROR;
  2184. }
  2185. /* Get usefull stuff like the type of operation, target dn */
  2186. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2187. op_type = operation_get_type(op);
  2188. target_sdn = operation_get_target_spec (op);
  2189. if (target_node->mtn_state == MTN_DISABLED) {
  2190. if (errorbuf) {
  2191. PR_snprintf(errorbuf, BUFSIZ,
  2192. "Warning: Operation attempted on a disabled node : %s\n",
  2193. slapi_sdn_get_dn(target_node->mtn_subtree));
  2194. }
  2195. result = LDAP_OPERATIONS_ERROR;
  2196. return result;
  2197. }
  2198. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &cid);
  2199. override_referral =
  2200. ((cid != NULL) && (pw_get_componentID() != NULL) && (pw_get_componentID() == cid)) ||
  2201. operation_is_flag_set(op, OP_FLAG_LEGACY_REPLICATION_DN) || /* 4.0 lgacy update */
  2202. operation_is_flag_set(op, OP_FLAG_REPLICATED) || /* 5.0 replication update */
  2203. operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY); /* 5.1 fix to enable tombstone delete on a R-O consumer */
  2204. if ((target_node->mtn_state == MTN_BACKEND) ||
  2205. (target_node->mtn_state == MTN_CONTAINER ) ||
  2206. ((target_node->mtn_state == MTN_REFERRAL_ON_UPDATE) &&
  2207. ((SLAPI_OPERATION_SEARCH == op_type)||(SLAPI_OPERATION_BIND == op_type) ||
  2208. (SLAPI_OPERATION_UNBIND == op_type) || (SLAPI_OPERATION_COMPARE == op_type))) ||
  2209. override_referral) {
  2210. if ((target_node == mapping_tree_root) ){
  2211. /* If we got here, then we couldn't find a matching node
  2212. * for the target. We'll use the default backend. Once
  2213. * we fully support the NULL suffix, we should do something more
  2214. * clever here.
  2215. */
  2216. *be = defbackend_get_backend();
  2217. } else {
  2218. if ((*index == -1) || (*index == 0)) {
  2219. /* In this case, we are doing
  2220. * a READ, ADD, MODIDY or DELETE on a single entry
  2221. * or we are starting a SEARCH
  2222. * if there is several possible backend we want to apply
  2223. * the distribution plugin
  2224. */
  2225. if (target_node->mtn_be_count <= 1) {
  2226. /* there is only one backend no choice possible */
  2227. *index = 0;
  2228. } else {
  2229. *index = mtn_get_be_distributed(pb, target_node,
  2230. target_sdn, &flag_stop);
  2231. }
  2232. }
  2233. if ((*index == -2) || (*index >= target_node->mtn_be_count)) {
  2234. /* we have already returned all backends -> return NULL */
  2235. *be = NULL;
  2236. *referral = NULL;
  2237. } else {
  2238. /* return next backend, increment index */
  2239. *be = target_node->mtn_be[*index];
  2240. if(*be==NULL) {
  2241. if (NULL != target_node->mtn_be_states &&
  2242. target_node->mtn_be_states[*index] == SLAPI_BE_STATE_DELETE) {
  2243. /* This MTN is being deleted */
  2244. *be = defbackend_get_backend();
  2245. } else {
  2246. /* This MTN has not been linked to its backend
  2247. * instance yet. */
  2248. target_node->mtn_be[*index] =
  2249. slapi_be_select_by_instance_name(
  2250. target_node->mtn_backend_names[*index]);
  2251. *be = target_node->mtn_be[*index];
  2252. if(*be==NULL) {
  2253. LDAPDebug(LDAP_DEBUG_ANY,
  2254. "Warning: Mapping tree node entry for %s point to "
  2255. "an unknown backend : %s\n",
  2256. slapi_sdn_get_dn(target_node->mtn_subtree),
  2257. target_node->mtn_backend_names[*index], 0);
  2258. /* Well there's still not backend instance for
  2259. * this MTN, so let's have the default backend
  2260. * deal with this.
  2261. */
  2262. *be = defbackend_get_backend();
  2263. }
  2264. }
  2265. }
  2266. if ((target_node->mtn_be_states) &&
  2267. (target_node->mtn_be_states[*index] == SLAPI_BE_STATE_OFFLINE)) {
  2268. LDAPDebug(LDAP_DEBUG_TRACE,
  2269. "Warning: Operation attempted on backend in OFFLINE "
  2270. "state : %s\n",
  2271. target_node->mtn_backend_names[*index], 0, 0);
  2272. result = LDAP_OPERATIONS_ERROR;
  2273. *be = defbackend_get_backend();
  2274. }
  2275. if (flag_stop)
  2276. *index = -2;
  2277. else
  2278. (*index)++;
  2279. }
  2280. }
  2281. *referral = NULL;
  2282. } else {
  2283. /* otherwise we must return the referral
  2284. * if ((target_node->mtn_state == MTN_REFERRAL) ||
  2285. * (target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) */
  2286. if (*index > 0) {
  2287. /* we have already returned this referral
  2288. * send back NULL to jump to next node
  2289. */
  2290. *be = NULL;
  2291. *referral = NULL;
  2292. result = LDAP_SUCCESS;
  2293. } else {
  2294. /* first time we hit this referral -> return it
  2295. * set the be variable to NULL to indicate we use a referral
  2296. * and increment index to rememeber later that we already
  2297. * returned this referral
  2298. */
  2299. *be = NULL;
  2300. *referral = (target_node->mtn_referral_entry ?
  2301. slapi_entry_dup(target_node->mtn_referral_entry) :
  2302. NULL);
  2303. (*index)++;
  2304. if (NULL == *referral) {
  2305. if (errorbuf) {
  2306. PR_snprintf(errorbuf, BUFSIZ,
  2307. "Mapping tree node for %s is set to return a referral,"
  2308. " but no referral is configured for it",
  2309. slapi_sdn_get_ndn(target_node->mtn_subtree));
  2310. }
  2311. result = LDAP_OPERATIONS_ERROR;
  2312. } else {
  2313. result = LDAP_SUCCESS;
  2314. }
  2315. }
  2316. }
  2317. if (result == LDAP_SUCCESS) {
  2318. if (*be && !be_isdeleted(*be)) {
  2319. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2320. "mapping tree selected backend : %s\n",
  2321. slapi_be_get_name(*be));
  2322. slapi_be_Rlock(*be);
  2323. } else if (*referral) {
  2324. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2325. "mapping tree selected referral at node : %s\n",
  2326. slapi_sdn_get_dn(target_node->mtn_subtree));
  2327. }
  2328. }
  2329. return result;
  2330. }
  2331. /*
  2332. * Description:
  2333. * Finds the best match for the targetdn from the children of parent. Uses
  2334. * slapi_sdn_issuffix and the number of rdns to pick the best node.
  2335. *
  2336. * Arguments:
  2337. * parent is a pointer to a mapping tree node.
  2338. * targetdn is the dn we're trying to find the best match for.
  2339. *
  2340. * Returns:
  2341. * A pointer to the child of parent that best matches the targetdn. NULL
  2342. * if there were no good matches.
  2343. */
  2344. static mapping_tree_node *best_matching_child(mapping_tree_node *parent,
  2345. const Slapi_DN *targetdn)
  2346. {
  2347. mapping_tree_node *highest_match_node = NULL;
  2348. mapping_tree_node *current;
  2349. if(mapping_tree_freed){
  2350. /* shutdown detected */
  2351. return NULL;
  2352. }
  2353. for (current = parent->mtn_children; current;
  2354. current = current->mtn_brother) {
  2355. if (slapi_sdn_issuffix(targetdn, current->mtn_subtree)) {
  2356. if ( (highest_match_node == NULL) ||
  2357. ((slapi_sdn_get_ndn_len(current->mtn_subtree)) >
  2358. slapi_sdn_get_ndn_len(highest_match_node->mtn_subtree)) ) {
  2359. highest_match_node = current;
  2360. }
  2361. }
  2362. }
  2363. return highest_match_node;
  2364. }
  2365. /*
  2366. * look for the exact mapping tree node corresponding to a given entry dn
  2367. */
  2368. static mapping_tree_node *
  2369. mtn_get_mapping_tree_node_by_entry(mapping_tree_node* node, const Slapi_DN *dn)
  2370. {
  2371. mapping_tree_node *found_node = NULL;
  2372. if(mapping_tree_freed){
  2373. /* shutdown detected */
  2374. return NULL;
  2375. }
  2376. if (slapi_sdn_compare(node->mtn_subtree, dn) == 0)
  2377. {
  2378. return node;
  2379. }
  2380. if (node->mtn_children)
  2381. {
  2382. found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_children, dn);
  2383. if (found_node)
  2384. return found_node;
  2385. }
  2386. if (node->mtn_brother)
  2387. {
  2388. found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_brother, dn);
  2389. }
  2390. return found_node;
  2391. }
  2392. /*
  2393. * Description:
  2394. * Gets a mapping tree node that best matches the given dn. If the root
  2395. * node is returned and the target dn is not "", then no match was found.
  2396. *
  2397. * Arguments:
  2398. * dn is the target of the search.
  2399. *
  2400. * Returns:
  2401. * The best matching node for the dn
  2402. * if nothing match, NULL is returned
  2403. */
  2404. mapping_tree_node *
  2405. slapi_get_mapping_tree_node_by_dn(const Slapi_DN *dn)
  2406. {
  2407. mapping_tree_node *current_best_match = mapping_tree_root;
  2408. mapping_tree_node *next_best_match = mapping_tree_root;
  2409. if(mapping_tree_freed){
  2410. /* shutdown detected */
  2411. return NULL;
  2412. }
  2413. /* Handle special case where the dn is "" and the mapping root
  2414. * does not belong to the frontend-internal (DSE_BACKEND);
  2415. * it has been assigned to a different backend.
  2416. * e.g: a container backend
  2417. */
  2418. if ( sdn_is_nulldn(dn) && mapping_tree_root && mapping_tree_root->mtn_be[0] &&
  2419. mapping_tree_root->mtn_be[0] != slapi_be_select_by_instance_name(DSE_BACKEND)) {
  2420. return( mapping_tree_root );
  2421. }
  2422. /* Start at the root and walk down the tree to find the best match. */
  2423. while (next_best_match) {
  2424. current_best_match = next_best_match;
  2425. next_best_match = best_matching_child(current_best_match, dn);
  2426. }
  2427. if (current_best_match == mapping_tree_root)
  2428. return NULL;
  2429. else
  2430. return current_best_match;
  2431. }
  2432. static mapping_tree_node *
  2433. get_mapping_tree_node_by_name(mapping_tree_node * node, char * be_name)
  2434. {
  2435. int i;
  2436. mapping_tree_node *found_node = NULL;
  2437. if(mapping_tree_freed){
  2438. /* shutdown detected */
  2439. return NULL;
  2440. }
  2441. /* now search the backend in this node */
  2442. i = 0;
  2443. while ( ( i < node->mtn_be_count) &&
  2444. (node->mtn_backend_names) &&
  2445. (node->mtn_backend_names[i]) &&
  2446. (strcmp(node->mtn_backend_names[i],be_name)))
  2447. {
  2448. i++;
  2449. }
  2450. if ((i < node->mtn_be_count) &&
  2451. (node->mtn_backend_names != NULL) &&
  2452. (node->mtn_backend_names[i] != NULL))
  2453. {
  2454. return node;
  2455. }
  2456. if (node->mtn_children)
  2457. {
  2458. found_node = get_mapping_tree_node_by_name(node->mtn_children, be_name);
  2459. if (found_node)
  2460. return found_node;
  2461. }
  2462. if (node->mtn_brother)
  2463. {
  2464. found_node = get_mapping_tree_node_by_name(node->mtn_brother, be_name);
  2465. }
  2466. return found_node;
  2467. }
  2468. /*
  2469. * Description: construct the dn of the configuration entry for the
  2470. * node originated at the root. The function just constructs
  2471. * the dn it does not verify that the entry actually exist.
  2472. * The format of the dn is
  2473. * cn="<normalized root>",cn=mapping tree,cn=config
  2474. *
  2475. * Arguments: root - root of the node
  2476. *
  2477. * Returns: dn of the configuration entry if successful and null otherwise.
  2478. */
  2479. char*
  2480. slapi_get_mapping_tree_node_configdn (const Slapi_DN *root)
  2481. {
  2482. char *dn = NULL;
  2483. if(mapping_tree_freed){
  2484. /* shutdown detected */
  2485. return NULL;
  2486. }
  2487. if (root == NULL)
  2488. return NULL;
  2489. /* This function converts the old DN style to the new one. */
  2490. dn = slapi_create_dn_string("cn=\"%s\",%s",
  2491. slapi_sdn_get_dn(root), MAPPING_TREE_BASE_DN);
  2492. if (NULL == dn) {
  2493. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  2494. "slapi_get_mapping_tree_node_configdn: "
  2495. "failed to crate mapping tree dn for %s\n",
  2496. slapi_sdn_get_dn(root));
  2497. return NULL;
  2498. }
  2499. return dn;
  2500. }
  2501. /*
  2502. * Description: this function returns root of the subtree to which the node applies
  2503. *
  2504. * Arguments: node - mapping tree node
  2505. *
  2506. * Returns: root of the subtree if function is successful and NULL otherwise.
  2507. */
  2508. const Slapi_DN* slapi_get_mapping_tree_node_root (const mapping_tree_node *node)
  2509. {
  2510. if (node)
  2511. return node->mtn_subtree;
  2512. else
  2513. return NULL;
  2514. }
  2515. /* GB : there is a potential problems with this function
  2516. * when several backends are used
  2517. */
  2518. PRBool slapi_mapping_tree_node_is_set (const mapping_tree_node *node, PRUint32 flag)
  2519. {
  2520. if (flag & SLAPI_MTN_LOCAL)
  2521. return PR_TRUE;
  2522. if (flag & SLAPI_MTN_PRIVATE)
  2523. return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_private);
  2524. if (flag & SLAPI_MTN_READONLY)
  2525. return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_be[0]->be_readonly);
  2526. return PR_FALSE;
  2527. }
  2528. /*
  2529. * Description: this function returns root of the subtree to which the node applies
  2530. *
  2531. * Arguments: node
  2532. *
  2533. * Returns: dn of the parent of mapping tree node configuration entry.
  2534. */
  2535. const char* slapi_get_mapping_tree_config_root ()
  2536. {
  2537. return MAPPING_TREE_BASE_DN;
  2538. }
  2539. /*
  2540. * slapi_be_select() finds the backend that should be used to service dn.
  2541. * If no backend with an appropriate suffix is configured, the default backend
  2542. * is returned. This function never returns NULL.
  2543. */
  2544. Slapi_Backend *
  2545. slapi_be_select( const Slapi_DN *sdn ) /* JCM - The name of this should change??? */
  2546. {
  2547. Slapi_Backend *be;
  2548. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2549. if(node!=NULL)
  2550. be= node->mtn_be[0];
  2551. else
  2552. be = NULL;
  2553. if(be==NULL)
  2554. be= defbackend_get_backend();
  2555. return be;
  2556. }
  2557. /* Check if the dn targets an internal reserved backends */
  2558. int
  2559. slapi_on_internal_backends(const Slapi_DN *sdn)
  2560. {
  2561. char *backend_names[] = {DSE_BACKEND, DSE_SCHEMA};
  2562. int internal = 1;
  2563. int numOfInternalBackends = 2;
  2564. int count;
  2565. Slapi_Backend *internal_be;
  2566. Slapi_Backend *be = slapi_be_select(sdn);
  2567. for (count=0; count < numOfInternalBackends ; ++count){
  2568. /* the internal backends are always in the begining of the list
  2569. * so should not be very inefficient
  2570. */
  2571. internal_be = slapi_be_select_by_instance_name(backend_names[count]);
  2572. if(be == internal_be){
  2573. return internal;
  2574. }
  2575. }
  2576. return 0;
  2577. }
  2578. /* Some of the operations are not allowed from the plugins
  2579. * but default to specialized use of those operations
  2580. * e.g rootDse search, ConfigRoot searches
  2581. * cn=config, cn=schema etc
  2582. */
  2583. int slapi_op_reserved(Slapi_PBlock *pb)
  2584. {
  2585. int scope=LDAP_SCOPE_BASE;
  2586. int reservedOp=0;
  2587. int op_type;
  2588. Slapi_Operation *op = NULL;
  2589. Slapi_DN *target_sdn=NULL;
  2590. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2591. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  2592. /* Get the target for this op */
  2593. target_sdn = operation_get_target_spec (op);
  2594. if( op_type == SLAPI_OPERATION_SEARCH){
  2595. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  2596. if( sdn_is_nulldn(target_sdn) && (scope == LDAP_SCOPE_BASE) ){
  2597. reservedOp = 1;
  2598. }
  2599. }
  2600. if(slapi_on_internal_backends(target_sdn)){
  2601. reservedOp = 1;
  2602. }
  2603. return reservedOp;
  2604. }
  2605. /*
  2606. * Returns the name of the Backend that contains specified DN,
  2607. * if only one matches. Otherwise returns NULL
  2608. * The name is pointing to the mapping tree structure
  2609. * and should not be altered.
  2610. */
  2611. const char *
  2612. slapi_mtn_get_backend_name( const Slapi_DN *sdn)
  2613. {
  2614. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2615. if ((node != NULL) &&
  2616. (node->mtn_be_count == 1) &&
  2617. (node->mtn_backend_names != NULL))
  2618. /* There's only one name, return it */
  2619. return node->mtn_backend_names[0];
  2620. else
  2621. return NULL;
  2622. }
  2623. /* Check if the backend that contains specified DN exists */
  2624. int
  2625. slapi_be_exist(const Slapi_DN *sdn) /* JCM - The name of this should change??? */
  2626. {
  2627. Slapi_Backend *def_be = defbackend_get_backend();
  2628. Slapi_Backend *be = slapi_be_select (sdn);
  2629. return (be != def_be);
  2630. }
  2631. /* The two following functions can be used to
  2632. * parse the list of the root suffix of the DIT
  2633. * Using
  2634. */
  2635. Slapi_DN *
  2636. slapi_get_first_suffix(void ** node, int show_private)
  2637. {
  2638. mapping_tree_node * first_node = mapping_tree_root->mtn_children;
  2639. *node = (void * ) first_node ;
  2640. while (first_node && (first_node->mtn_private && (show_private == 0)))
  2641. first_node = first_node->mtn_brother;
  2642. return (first_node ? first_node->mtn_subtree : NULL);
  2643. }
  2644. Slapi_DN *
  2645. slapi_get_next_suffix(void ** node, int show_private)
  2646. {
  2647. mapping_tree_node * next_node = *node;
  2648. if (next_node == NULL)
  2649. return NULL;
  2650. next_node = next_node->mtn_brother;
  2651. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2652. next_node = next_node->mtn_brother;
  2653. *node = next_node;
  2654. return (next_node ? next_node->mtn_subtree : NULL);
  2655. }
  2656. /* check if a suffix is a root of the DIT
  2657. * return 1 if yes, 0 if no
  2658. */
  2659. int slapi_is_root_suffix(Slapi_DN * dn)
  2660. {
  2661. void * node;
  2662. Slapi_DN * suffix = slapi_get_first_suffix (&node, 1);
  2663. while (suffix)
  2664. {
  2665. if ( slapi_sdn_compare(dn, suffix) == 0 )
  2666. return 1;
  2667. suffix = slapi_get_next_suffix(&node, 1);
  2668. }
  2669. return 0 ;
  2670. }
  2671. /* Return value is a part mapping tree; Don't free it. */
  2672. const Slapi_DN *
  2673. slapi_get_suffix_by_dn(const Slapi_DN *dn)
  2674. {
  2675. mapping_tree_node *node = slapi_get_mapping_tree_node_by_dn(dn);
  2676. const Slapi_DN *suffix = NULL;
  2677. if (node)
  2678. {
  2679. suffix = (const Slapi_DN *)slapi_mtn_get_dn(node);
  2680. }
  2681. return suffix;
  2682. }
  2683. /*
  2684. * set referrals for the node
  2685. * notes :
  2686. * - referral is consumed by this function
  2687. * - node must exist before calling this function
  2688. * - mapping tree node state is not changed by this function
  2689. */
  2690. int
  2691. slapi_mtn_set_referral(const Slapi_DN *sdn, char ** referral)
  2692. {
  2693. Slapi_PBlock pb;
  2694. Slapi_Mods smods;
  2695. int rc = LDAP_SUCCESS,i = 0, j = 0;
  2696. char * node_dn;
  2697. char **values = NULL;
  2698. int do_modify = 0;
  2699. slapi_mods_init (&smods, 0);
  2700. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2701. if(!node_dn){
  2702. /* shutdown has been detected */
  2703. return LDAP_OPERATIONS_ERROR;
  2704. }
  2705. if ((referral == NULL) || (referral[0] == NULL))
  2706. {
  2707. /* NULL referral means we want to delete existing referral
  2708. */
  2709. slapi_mods_add(&smods, LDAP_MOD_DELETE, "nsslapd-referral", 0, NULL);
  2710. do_modify = 1;
  2711. }
  2712. else
  2713. {
  2714. int changes = 1;
  2715. int referralCount = 0;
  2716. for(; referral[referralCount]; referralCount++);
  2717. if ( (values = slapi_mtn_get_referral(sdn)) != NULL )
  2718. {
  2719. /* Check if there are differences between current values and values to be set */
  2720. for (i=0; values[i]; i++);
  2721. if (i == referralCount) {
  2722. changes = 0;
  2723. for (i=0;values[i];i++){
  2724. int found = 0;
  2725. for (j=0;referral[j];j++){
  2726. if (strcmp(values[i], referral[j]) == 0){
  2727. found = 1;
  2728. break;
  2729. }
  2730. }
  2731. if (!found) {
  2732. changes = 1;
  2733. break;
  2734. }
  2735. }
  2736. }
  2737. i=0;
  2738. while(values[i])
  2739. slapi_ch_free((void**)&values[i++]);
  2740. slapi_ch_free((void**)&values);
  2741. }
  2742. if (changes){
  2743. Slapi_Value *val;
  2744. Slapi_Value ** svals = NULL;
  2745. do_modify = 1;
  2746. for (j =0; referral[j];j++) {
  2747. val = slapi_value_new_string(referral[j]);
  2748. valuearray_add_value(&svals, val);
  2749. slapi_value_free(&val);
  2750. }
  2751. slapi_mods_add_mod_values(&smods, LDAP_MOD_REPLACE, "nsslapd-referral", svals);
  2752. valuearray_free(&svals);
  2753. }
  2754. }
  2755. if ( do_modify )
  2756. {
  2757. pblock_init (&pb);
  2758. slapi_modify_internal_set_pb (&pb, node_dn,
  2759. slapi_mods_get_ldapmods_byref(&smods), NULL,
  2760. NULL, (void *) plugin_get_default_component_id(), 0);
  2761. slapi_modify_internal_pb (&pb);
  2762. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2763. pblock_done(&pb);
  2764. }
  2765. slapi_mods_done(&smods);
  2766. slapi_ch_free_string(&node_dn);
  2767. return rc;
  2768. }
  2769. /*
  2770. * Change the state of a mapping tree node entry
  2771. * notes :
  2772. * - sdn argument is the dn of the subtree of the DIT managed by this node
  2773. * not the dn of the mapping tree entry
  2774. * - mapping tree node must exist before calling this function
  2775. */
  2776. int
  2777. slapi_mtn_set_state(const Slapi_DN *sdn, char *state)
  2778. {
  2779. Slapi_PBlock pb;
  2780. Slapi_Mods smods;
  2781. int rc = LDAP_SUCCESS;
  2782. char * node_dn;
  2783. char * value;
  2784. if (NULL == state) {
  2785. return LDAP_OPERATIONS_ERROR;
  2786. }
  2787. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2788. if(!node_dn){
  2789. /* shutdown has been detected */
  2790. return LDAP_OPERATIONS_ERROR;
  2791. }
  2792. if ( (value = slapi_mtn_get_state(sdn)) != NULL )
  2793. {
  2794. if ( strcasecmp(value, state) == 0 )
  2795. {
  2796. /* Same state, don't change anything */
  2797. goto bail;
  2798. }
  2799. }
  2800. /* Otherwise, means that the state has changed, modify it */
  2801. slapi_mods_init (&smods, 1);
  2802. slapi_mods_add(&smods, LDAP_MOD_REPLACE, "nsslapd-state", strlen(state), state);
  2803. pblock_init (&pb);
  2804. slapi_modify_internal_set_pb (&pb, node_dn,
  2805. slapi_mods_get_ldapmods_byref(&smods), NULL,
  2806. NULL, (void *) plugin_get_default_component_id(), 0);
  2807. slapi_modify_internal_pb (&pb);
  2808. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2809. slapi_mods_done(&smods);
  2810. pblock_done(&pb);
  2811. bail:
  2812. slapi_ch_free_string(&value);
  2813. slapi_ch_free_string(&node_dn);
  2814. return rc;
  2815. }
  2816. /*
  2817. returns a copy of the attr - the caller must slapi_attr_free it
  2818. */
  2819. Slapi_Attr *
  2820. mtn_get_attr(char* node_dn, char * type)
  2821. {
  2822. Slapi_PBlock pb;
  2823. int res = 0;
  2824. Slapi_Entry **entries = NULL;
  2825. Slapi_Attr *attr = NULL;
  2826. Slapi_Attr *ret_attr = NULL;
  2827. char **attrs = NULL;
  2828. attrs = (char **)slapi_ch_calloc(2, sizeof(char *));
  2829. attrs[0] = slapi_ch_strdup(type);
  2830. pblock_init(&pb);
  2831. slapi_search_internal_set_pb(&pb, node_dn, LDAP_SCOPE_BASE,
  2832. "objectclass=nsMappingTree", attrs, 0, NULL, NULL,
  2833. (void *) plugin_get_default_component_id(), 0);
  2834. slapi_search_internal_pb(&pb);
  2835. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  2836. if (res != LDAP_SUCCESS) {
  2837. goto done;
  2838. }
  2839. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2840. if (NULL == entries || NULL == entries[0]) {
  2841. goto done;
  2842. }
  2843. /* always at most one entry entries[0] */
  2844. res = slapi_entry_attr_find(entries[0], type, &attr);
  2845. if (res == 0)
  2846. /* we need to make a copy here so we can free the search results */
  2847. ret_attr = slapi_attr_dup(attr);
  2848. slapi_free_search_results_internal(&pb);
  2849. done:
  2850. slapi_ch_free((void **)&attrs[0]);
  2851. slapi_ch_free((void **)&attrs);
  2852. pblock_done(&pb);
  2853. return ret_attr;
  2854. }
  2855. /*
  2856. * Get the referral associated to the mapping tree node entry
  2857. * notes :
  2858. * - sdn argument is the dn of the subtree of the DIT managed by this node
  2859. * not the dn of the mapping tree entry
  2860. * - return NULL if no referral
  2861. * - caller is reponsible for freeing the returned referrals
  2862. */
  2863. char **
  2864. slapi_mtn_get_referral(const Slapi_DN *sdn)
  2865. {
  2866. int i, hint, nb;
  2867. char * node_dn;
  2868. Slapi_Attr *attr;
  2869. char ** referral = NULL;
  2870. Slapi_Value *val = NULL;
  2871. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2872. if(!node_dn){
  2873. /* shutdown has been detected */
  2874. return NULL;
  2875. }
  2876. attr = mtn_get_attr(node_dn, "nsslapd-referral");
  2877. if (attr)
  2878. {
  2879. /* if there are some referrals set in the entry build a list
  2880. * to be returned to the caller
  2881. */
  2882. slapi_attr_get_numvalues(attr, &nb);
  2883. referral = (char**) slapi_ch_malloc(sizeof(char*) * (nb+1));
  2884. hint = slapi_attr_first_value(attr, &val);
  2885. i = 0;
  2886. while (val)
  2887. {
  2888. referral[i++] = slapi_ch_strdup(slapi_value_get_string(val));
  2889. hint = slapi_attr_next_value(attr, hint, &val);
  2890. }
  2891. referral[i] = NULL;
  2892. slapi_attr_free(&attr);
  2893. }
  2894. slapi_ch_free_string(&node_dn);
  2895. return referral;
  2896. }
  2897. /*
  2898. * Get the state of a mapping tree node entry
  2899. * notes :
  2900. * - sdn argument is the dn of the subtree of the DIT managed by this node
  2901. * not the dn of the mapping tree entry
  2902. * - the state is return in a newly allocated string that must be freed by
  2903. * the caller
  2904. */
  2905. char *
  2906. slapi_mtn_get_state(const Slapi_DN *sdn)
  2907. {
  2908. char * node_dn;
  2909. Slapi_Attr *attr;
  2910. char * state = NULL;
  2911. Slapi_Value *val = NULL;
  2912. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2913. if(!node_dn){
  2914. /* shutdown has been detected */
  2915. return NULL;
  2916. }
  2917. attr = mtn_get_attr(node_dn, "nsslapd-state");
  2918. if (attr)
  2919. {
  2920. /* entry state was found */
  2921. slapi_attr_first_value(attr, &val);
  2922. state = slapi_ch_strdup(slapi_value_get_string(val));
  2923. slapi_attr_free(&attr);
  2924. }
  2925. slapi_ch_free_string(&node_dn);
  2926. return state;
  2927. }
  2928. static void
  2929. mtn_internal_be_set_state(Slapi_Backend *be, int state)
  2930. {
  2931. mapping_tree_node * node;
  2932. char * be_name;
  2933. int i;
  2934. int change_callback = 0;
  2935. int old_state;
  2936. mtn_wlock();
  2937. be_name = slapi_ch_strdup(slapi_be_get_name(be));
  2938. node = get_mapping_tree_node_by_name(mapping_tree_root, be_name);
  2939. if (node == NULL)
  2940. {
  2941. LDAPDebug(LDAP_DEBUG_TRACE,
  2942. "Warning: backend %s is not declared in mapping tree\n",
  2943. be_name, 0 ,0);
  2944. goto done;
  2945. }
  2946. /* now search the backend in this node */
  2947. i = 0;
  2948. while ( (i < node->mtn_be_count) &&
  2949. (node->mtn_backend_names) &&
  2950. (node->mtn_backend_names[i]) &&
  2951. (strcmp(node->mtn_backend_names[i],be_name)))
  2952. {
  2953. i++;
  2954. }
  2955. if ( (i >= node->mtn_be_count) || (node->mtn_backend_names == NULL) ||
  2956. (node->mtn_backend_names[i] == NULL) )
  2957. {
  2958. /* backend is not declared in the mapping tree node
  2959. * print out a warning
  2960. */
  2961. LDAPDebug(LDAP_DEBUG_TRACE,
  2962. "Warning: backend %s is not declared in mapping node entry\n",
  2963. be_name, 0 ,0);
  2964. goto done;
  2965. }
  2966. change_callback = 1;
  2967. old_state = node->mtn_be_states[i];
  2968. /* OK we found the backend at last, now do the real job: set the state */
  2969. switch (state)
  2970. {
  2971. case SLAPI_BE_STATE_OFFLINE:
  2972. node->mtn_be[i] = be;
  2973. node->mtn_be_states[i] = SLAPI_BE_STATE_OFFLINE;
  2974. break;
  2975. case SLAPI_BE_STATE_ON:
  2976. node->mtn_be[i] = be;
  2977. node->mtn_be_states[i] = SLAPI_BE_STATE_ON;
  2978. break;
  2979. case SLAPI_BE_STATE_DELETE:
  2980. node->mtn_be[i] = NULL;
  2981. node->mtn_be_states[i] = SLAPI_BE_STATE_DELETE;
  2982. break;
  2983. }
  2984. done:
  2985. mtn_unlock();
  2986. if (change_callback)
  2987. mtn_be_state_change(be_name, old_state, state);
  2988. slapi_ch_free( (void **) &be_name);
  2989. }
  2990. /*
  2991. * This procedure must be called by previously stopped backends
  2992. * to signal that they have started and are ready to process requests
  2993. * The backend must be fully ready to handle requests before calling this
  2994. * procedure
  2995. * At startup tiem it is not mandatory for the backends to
  2996. * call this procedure: backends are assumed on by default
  2997. */
  2998. void
  2999. slapi_mtn_be_started(Slapi_Backend *be)
  3000. {
  3001. /* Find the node where this backend stay
  3002. * then update the backend structure
  3003. * In the long term, the backend should have only one suffix and
  3004. * stay in only one node as for now, check all suffixes
  3005. * Rq : since mapping tree is initiatized very soon in the server
  3006. * startup, we can be sure at that time that the mapping
  3007. * tree is initialized
  3008. */
  3009. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3010. }
  3011. /* these procedure can be called when backends need to be put in maintenance mode
  3012. * after call to slapi_mtn_be_disable, the backend will still be known
  3013. * by a server but the mapping tree won't route requests to it anymore
  3014. * The slapi_mtn_be_enable function enable to route requests to the backend
  3015. * again
  3016. * the slapi_mtn_be_disable function only returns when there is no more
  3017. * request in progress in the backend
  3018. */
  3019. void
  3020. slapi_mtn_be_disable(Slapi_Backend *be)
  3021. {
  3022. mtn_internal_be_set_state(be, SLAPI_BE_STATE_OFFLINE);
  3023. /* the two following lines can seem weird, but they allow to check that no
  3024. * LDAP operation is in progress on the backend
  3025. */
  3026. slapi_be_Wlock(be);
  3027. slapi_be_Unlock(be);
  3028. }
  3029. void
  3030. slapi_mtn_be_enable(Slapi_Backend *be)
  3031. {
  3032. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3033. }
  3034. /*
  3035. * This procedure must be called by backends before stopping
  3036. * if some operations are in progress when this procedure
  3037. * is called, this procedure will block until completion
  3038. * of these operations
  3039. * The backend must wait return from this procedure before stopping operation
  3040. * Backends must serve operation until the return from this procedure.
  3041. * Once this procedure return they will not be issued request anymore
  3042. * and they have been removed from the server list of backends
  3043. * It is also the bakend responsability to free the Slapi_Backend structures
  3044. * that was given by slapi_be_new at startup time.
  3045. * Should the backend start again, it would need to issue slapi_be_new again
  3046. */
  3047. void
  3048. slapi_mtn_be_stopping(Slapi_Backend *be)
  3049. {
  3050. mtn_internal_be_set_state(be, SLAPI_BE_STATE_DELETE);
  3051. /* the two following lines can seem weird, but they allow to check that no
  3052. * LDAP operation is in progress on the backend
  3053. */
  3054. slapi_be_Wlock(be);
  3055. slapi_be_Unlock(be);
  3056. slapi_be_stopping(be);
  3057. }
  3058. /*
  3059. * Switch a backend into read-only mode, or back to read-write mode.
  3060. * To switch to read-only mode, we need to wait for all pending operations
  3061. * to finish.
  3062. */
  3063. void
  3064. slapi_mtn_be_set_readonly(Slapi_Backend *be, int readonly)
  3065. {
  3066. if (readonly) {
  3067. slapi_be_Wlock(be);
  3068. slapi_be_set_readonly(be, 1);
  3069. slapi_be_Unlock(be);
  3070. } else {
  3071. slapi_be_set_readonly(be, 0);
  3072. }
  3073. }
  3074. #ifdef DEBUG
  3075. static int lock_count = 0;
  3076. #endif
  3077. void mtn_wlock()
  3078. {
  3079. PR_RWLock_Wlock(myLock);
  3080. #ifdef DEBUG
  3081. lock_count--;
  3082. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_wlock : lock count : %d\n", lock_count, 0, 0);
  3083. #endif
  3084. }
  3085. void mtn_lock()
  3086. {
  3087. PR_RWLock_Rlock(myLock);
  3088. #ifdef DEBUG
  3089. lock_count++;
  3090. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_lock : lock count : %d\n", lock_count, 0, 0);
  3091. #endif
  3092. }
  3093. void mtn_unlock()
  3094. {
  3095. #ifdef DEBUG
  3096. if (lock_count > 0)
  3097. lock_count--;
  3098. else if (lock_count < 0)
  3099. lock_count++;
  3100. else
  3101. lock_count = (int) 11111111; /* this happening means problems */
  3102. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_unlock : lock count : %d\n", lock_count, 0, 0);
  3103. #endif
  3104. PR_RWLock_Unlock(myLock);
  3105. }
  3106. #ifdef TEST_FOR_REGISTER_CHANGE
  3107. void my_test_fnct1(void *handle, char *be_name, int old_state, int new_state)
  3108. {
  3109. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3110. "my_test_fnct1 : handle %d, be %s, old state %d, new state %d\n",
  3111. handle,be_name, old_state, new_state);
  3112. if (old_state == 2)
  3113. slapi_unregister_backend_state_change(handle);
  3114. }
  3115. void my_test_fnct2(void *handle, char *be_name, int old_state, int new_state)
  3116. {
  3117. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3118. "my_test_fnct2 : handle %d, be %s, old state %d, new state %d\n",
  3119. handle, be_name, old_state, new_state);
  3120. }
  3121. void test_register()
  3122. {
  3123. slapi_register_backend_state_change((void *) 1234, my_test_fnct1);
  3124. slapi_register_backend_state_change((void *) 4321, my_test_fnct2);
  3125. }
  3126. #endif
  3127. #ifdef DEBUG
  3128. static void dump_mapping_tree(mapping_tree_node *parent, int depth)
  3129. {
  3130. mapping_tree_node *current = NULL;
  3131. static char dump_indent[256];
  3132. int i;
  3133. if (depth == 0)
  3134. {
  3135. LDAPDebug(LDAP_DEBUG_ANY, "dump_mapping_tree\n", 0, 0, 0);
  3136. }
  3137. dump_indent[0] = '\0';
  3138. for (i = 0; i < depth; i++)
  3139. PL_strcatn(dump_indent, sizeof(dump_indent), " ");
  3140. for (current = parent->mtn_children; current;
  3141. current = current->mtn_brother)
  3142. {
  3143. if (strlen(current->mtn_subtree->dn) == 0)
  3144. {
  3145. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3146. dump_indent, "none", current);
  3147. }
  3148. else
  3149. {
  3150. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3151. dump_indent, current->mtn_subtree->dn, current);
  3152. }
  3153. dump_mapping_tree(current, depth+1);
  3154. }
  3155. return;
  3156. }
  3157. #endif