mapping_tree.c 127 KB

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