mapping_tree.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896
  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. Slapi_Backend *
  2754. slapi_be_select_exact(const Slapi_DN *sdn)
  2755. {
  2756. Slapi_Backend *be = NULL;
  2757. mapping_tree_node *node = NULL;
  2758. if (!sdn) {
  2759. LDAPDebug0Args(LDAP_DEBUG_ANY, "slapi_be_select_exact: Empty Slapi_DN is given.\n");
  2760. return NULL;
  2761. }
  2762. node = slapi_get_mapping_tree_node_by_dn(sdn);
  2763. if (node && node->mtn_be) {
  2764. be = node->mtn_be[0];
  2765. }
  2766. return be;
  2767. }
  2768. /* Check if the dn targets an internal reserved backends */
  2769. int
  2770. slapi_on_internal_backends(const Slapi_DN *sdn)
  2771. {
  2772. char *backend_names[] = {DSE_BACKEND, DSE_SCHEMA};
  2773. int internal = 1;
  2774. int numOfInternalBackends = 2;
  2775. int count;
  2776. Slapi_Backend *internal_be;
  2777. Slapi_Backend *be = slapi_be_select(sdn);
  2778. for (count=0; count < numOfInternalBackends ; ++count){
  2779. /* the internal backends are always in the begining of the list
  2780. * so should not be very inefficient
  2781. */
  2782. internal_be = slapi_be_select_by_instance_name(backend_names[count]);
  2783. if(be == internal_be){
  2784. return internal;
  2785. }
  2786. }
  2787. return 0;
  2788. }
  2789. /* Some of the operations are not allowed from the plugins
  2790. * but default to specialized use of those operations
  2791. * e.g rootDse search, ConfigRoot searches
  2792. * cn=config, cn=schema etc
  2793. * Return value: 1 if reserved.
  2794. * 0 if not reserved.
  2795. * -1 in error case.
  2796. */
  2797. int slapi_op_reserved(Slapi_PBlock *pb)
  2798. {
  2799. int scope=LDAP_SCOPE_BASE;
  2800. int reservedOp=0;
  2801. int op_type;
  2802. Slapi_Operation *op = NULL;
  2803. Slapi_DN *target_sdn=NULL;
  2804. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2805. if (NULL == op) {
  2806. return -1;
  2807. }
  2808. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  2809. /* Get the target for this op */
  2810. target_sdn = operation_get_target_spec (op);
  2811. if( op_type == SLAPI_OPERATION_SEARCH){
  2812. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  2813. if( sdn_is_nulldn(target_sdn) && (scope == LDAP_SCOPE_BASE) ){
  2814. reservedOp = 1;
  2815. }
  2816. }
  2817. if(slapi_on_internal_backends(target_sdn)){
  2818. reservedOp = 1;
  2819. }
  2820. return reservedOp;
  2821. }
  2822. /*
  2823. * Returns the name of the Backend that contains specified DN,
  2824. * if only one matches. Otherwise returns NULL
  2825. * The name is pointing to the mapping tree structure
  2826. * and should not be altered.
  2827. */
  2828. const char *
  2829. slapi_mtn_get_backend_name( const Slapi_DN *sdn)
  2830. {
  2831. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2832. if ((node != NULL) &&
  2833. (node->mtn_be_count == 1) &&
  2834. (node->mtn_backend_names != NULL))
  2835. /* There's only one name, return it */
  2836. return node->mtn_backend_names[0];
  2837. else
  2838. return NULL;
  2839. }
  2840. /* Check if the backend that contains specified DN exists */
  2841. int
  2842. slapi_be_exist(const Slapi_DN *sdn) /* JCM - The name of this should change??? */
  2843. {
  2844. Slapi_Backend *def_be = defbackend_get_backend();
  2845. Slapi_Backend *be = slapi_be_select (sdn);
  2846. return (be != def_be);
  2847. }
  2848. /* The two following functions can be used to
  2849. * parse the list of the root suffix of the DIT
  2850. * Using
  2851. */
  2852. Slapi_DN *
  2853. slapi_get_first_suffix(void ** node, int show_private)
  2854. {
  2855. mapping_tree_node *first_node;
  2856. if ((NULL == node) || (NULL == mapping_tree_root)) {
  2857. return NULL;
  2858. }
  2859. first_node = mapping_tree_root->mtn_children;
  2860. *node = (void * ) first_node ;
  2861. while (first_node && (first_node->mtn_private && (show_private == 0)))
  2862. first_node = first_node->mtn_brother;
  2863. return (first_node ? first_node->mtn_subtree : NULL);
  2864. }
  2865. Slapi_DN *
  2866. slapi_get_next_suffix(void ** node, int show_private)
  2867. {
  2868. mapping_tree_node *next_node;
  2869. if ((NULL == node) || (NULL == mapping_tree_root)) {
  2870. return NULL;
  2871. }
  2872. next_node = *node;
  2873. if (next_node == NULL) {
  2874. return NULL;
  2875. }
  2876. next_node = next_node->mtn_brother;
  2877. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2878. next_node = next_node->mtn_brother;
  2879. *node = next_node;
  2880. return (next_node ? next_node->mtn_subtree : NULL);
  2881. }
  2882. /* get mapping tree node recursively */
  2883. Slapi_DN *
  2884. slapi_get_next_suffix_ext(void ** node, int show_private)
  2885. {
  2886. mapping_tree_node * next_node = NULL;
  2887. if (NULL == node) {
  2888. return NULL;
  2889. }
  2890. next_node = *node;
  2891. if (next_node == NULL) {
  2892. return NULL;
  2893. }
  2894. if (next_node->mtn_children) {
  2895. next_node = next_node->mtn_children;
  2896. } else if (next_node->mtn_brother) {
  2897. next_node = next_node->mtn_brother;
  2898. } else {
  2899. next_node = next_node->mtn_parent;
  2900. if (next_node) {
  2901. next_node = next_node->mtn_brother;
  2902. }
  2903. }
  2904. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2905. next_node = next_node->mtn_brother;
  2906. if (next_node) {
  2907. *node = next_node;
  2908. }
  2909. return (next_node ? next_node->mtn_subtree : NULL);
  2910. }
  2911. /* check if a suffix is a root of the DIT
  2912. * return 1 if yes, 0 if no
  2913. */
  2914. int slapi_is_root_suffix(Slapi_DN * dn)
  2915. {
  2916. void * node;
  2917. Slapi_DN * suffix = slapi_get_first_suffix (&node, 1);
  2918. while (suffix)
  2919. {
  2920. if ( slapi_sdn_compare(dn, suffix) == 0 )
  2921. return 1;
  2922. suffix = slapi_get_next_suffix(&node, 1);
  2923. }
  2924. return 0 ;
  2925. }
  2926. /* Return value is a part mapping tree; Don't free it. */
  2927. const Slapi_DN *
  2928. slapi_get_suffix_by_dn(const Slapi_DN *dn)
  2929. {
  2930. mapping_tree_node *node = slapi_get_mapping_tree_node_by_dn(dn);
  2931. const Slapi_DN *suffix = NULL;
  2932. if (node)
  2933. {
  2934. suffix = (const Slapi_DN *)slapi_mtn_get_dn(node);
  2935. }
  2936. return suffix;
  2937. }
  2938. /*
  2939. * set referrals for the node
  2940. * notes :
  2941. * - referral is consumed by this function
  2942. * - node must exist before calling this function
  2943. * - mapping tree node state is not changed by this function
  2944. */
  2945. int
  2946. slapi_mtn_set_referral(const Slapi_DN *sdn, char ** referral)
  2947. {
  2948. Slapi_PBlock pb;
  2949. Slapi_Mods smods;
  2950. int rc = LDAP_SUCCESS,i = 0, j = 0;
  2951. Slapi_DN* node_sdn;
  2952. char **values = NULL;
  2953. int do_modify = 0;
  2954. slapi_mods_init (&smods, 0);
  2955. node_sdn = slapi_get_mapping_tree_node_configsdn(sdn);
  2956. if(!node_sdn){
  2957. /* shutdown has been detected */
  2958. return LDAP_OPERATIONS_ERROR;
  2959. }
  2960. if ((referral == NULL) || (referral[0] == NULL))
  2961. {
  2962. /* NULL referral means we want to delete existing referral
  2963. */
  2964. slapi_mods_add(&smods, LDAP_MOD_DELETE, "nsslapd-referral", 0, NULL);
  2965. do_modify = 1;
  2966. }
  2967. else
  2968. {
  2969. int changes = 1;
  2970. int referralCount = 0;
  2971. for(; referral[referralCount]; referralCount++);
  2972. if ( (values = slapi_mtn_get_referral(sdn)) != NULL )
  2973. {
  2974. /* Check if there are differences between current values and values to be set */
  2975. for (i=0; values[i]; i++);
  2976. if (i == referralCount) {
  2977. changes = 0;
  2978. for (i=0;values[i];i++){
  2979. int found = 0;
  2980. for (j=0;referral[j];j++){
  2981. if (strcmp(values[i], referral[j]) == 0){
  2982. found = 1;
  2983. break;
  2984. }
  2985. }
  2986. if (!found) {
  2987. changes = 1;
  2988. break;
  2989. }
  2990. }
  2991. }
  2992. i=0;
  2993. while(values[i])
  2994. slapi_ch_free((void**)&values[i++]);
  2995. slapi_ch_free((void**)&values);
  2996. }
  2997. if (changes){
  2998. Slapi_Value *val;
  2999. Slapi_Value ** svals = NULL;
  3000. do_modify = 1;
  3001. for (j =0; referral[j];j++) {
  3002. val = slapi_value_new_string(referral[j]);
  3003. valuearray_add_value(&svals, val);
  3004. slapi_value_free(&val);
  3005. }
  3006. slapi_mods_add_mod_values(&smods, LDAP_MOD_REPLACE, "nsslapd-referral", svals);
  3007. valuearray_free(&svals);
  3008. }
  3009. }
  3010. if ( do_modify )
  3011. {
  3012. pblock_init (&pb);
  3013. slapi_modify_internal_set_pb_ext (&pb, node_sdn,
  3014. slapi_mods_get_ldapmods_byref(&smods), NULL,
  3015. NULL, (void *) plugin_get_default_component_id(), 0);
  3016. slapi_modify_internal_pb (&pb);
  3017. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3018. pblock_done(&pb);
  3019. }
  3020. slapi_mods_done(&smods);
  3021. slapi_sdn_free(&node_sdn);
  3022. return rc;
  3023. }
  3024. /*
  3025. * Change the state of a mapping tree node entry
  3026. * notes :
  3027. * - sdn argument is the dn of the subtree of the DIT managed by this node
  3028. * not the dn of the mapping tree entry
  3029. * - mapping tree node must exist before calling this function
  3030. */
  3031. int
  3032. slapi_mtn_set_state(const Slapi_DN *sdn, char *state)
  3033. {
  3034. Slapi_PBlock pb;
  3035. Slapi_Mods smods;
  3036. int rc = LDAP_SUCCESS;
  3037. Slapi_DN *node_sdn;
  3038. char * value;
  3039. if (NULL == state) {
  3040. return LDAP_OPERATIONS_ERROR;
  3041. }
  3042. node_sdn = slapi_get_mapping_tree_node_configsdn(sdn);
  3043. if(!node_sdn){
  3044. /* shutdown has been detected */
  3045. return LDAP_OPERATIONS_ERROR;
  3046. }
  3047. if ( (value = slapi_mtn_get_state(sdn)) != NULL )
  3048. {
  3049. if ( strcasecmp(value, state) == 0 )
  3050. {
  3051. /* Same state, don't change anything */
  3052. goto bail;
  3053. }
  3054. }
  3055. /* Otherwise, means that the state has changed, modify it */
  3056. slapi_mods_init (&smods, 1);
  3057. slapi_mods_add(&smods, LDAP_MOD_REPLACE, "nsslapd-state", strlen(state), state);
  3058. pblock_init (&pb);
  3059. slapi_modify_internal_set_pb_ext (&pb, node_sdn,
  3060. slapi_mods_get_ldapmods_byref(&smods), NULL,
  3061. NULL, (void *) plugin_get_default_component_id(), 0);
  3062. slapi_modify_internal_pb (&pb);
  3063. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3064. slapi_mods_done(&smods);
  3065. pblock_done(&pb);
  3066. bail:
  3067. slapi_ch_free_string(&value);
  3068. slapi_sdn_free(&node_sdn);
  3069. return rc;
  3070. }
  3071. /*
  3072. returns a copy of the attr - the caller must slapi_attr_free it
  3073. */
  3074. Slapi_Attr *
  3075. mtn_get_attr(char* node_dn, char * type)
  3076. {
  3077. Slapi_PBlock pb;
  3078. int res = 0;
  3079. Slapi_Entry **entries = NULL;
  3080. Slapi_Attr *attr = NULL;
  3081. Slapi_Attr *ret_attr = NULL;
  3082. char **attrs = NULL;
  3083. attrs = (char **)slapi_ch_calloc(2, sizeof(char *));
  3084. attrs[0] = slapi_ch_strdup(type);
  3085. pblock_init(&pb);
  3086. slapi_search_internal_set_pb(&pb, node_dn, LDAP_SCOPE_BASE,
  3087. "objectclass=nsMappingTree", attrs, 0, NULL, NULL,
  3088. (void *) plugin_get_default_component_id(), 0);
  3089. slapi_search_internal_pb(&pb);
  3090. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3091. if (res != LDAP_SUCCESS) {
  3092. goto done;
  3093. }
  3094. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3095. if (NULL == entries || NULL == entries[0]) {
  3096. goto done;
  3097. }
  3098. /* always at most one entry entries[0] */
  3099. res = slapi_entry_attr_find(entries[0], type, &attr);
  3100. if (res == 0)
  3101. /* we need to make a copy here so we can free the search results */
  3102. ret_attr = slapi_attr_dup(attr);
  3103. slapi_free_search_results_internal(&pb);
  3104. done:
  3105. slapi_ch_free((void **)&attrs[0]);
  3106. slapi_ch_free((void **)&attrs);
  3107. pblock_done(&pb);
  3108. return ret_attr;
  3109. }
  3110. /*
  3111. * Get the referral associated to the mapping tree node entry
  3112. * notes :
  3113. * - sdn argument is the dn of the subtree of the DIT managed by this node
  3114. * not the dn of the mapping tree entry
  3115. * - return NULL if no referral
  3116. * - caller is reponsible for freeing the returned referrals
  3117. */
  3118. char **
  3119. slapi_mtn_get_referral(const Slapi_DN *sdn)
  3120. {
  3121. int i, hint, nb;
  3122. char * node_dn;
  3123. Slapi_Attr *attr;
  3124. char ** referral = NULL;
  3125. Slapi_Value *val = NULL;
  3126. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  3127. if(!node_dn){
  3128. /* shutdown has been detected */
  3129. return NULL;
  3130. }
  3131. attr = mtn_get_attr(node_dn, "nsslapd-referral");
  3132. if (attr)
  3133. {
  3134. /* if there are some referrals set in the entry build a list
  3135. * to be returned to the caller
  3136. */
  3137. slapi_attr_get_numvalues(attr, &nb);
  3138. referral = (char**) slapi_ch_malloc(sizeof(char*) * (nb+1));
  3139. hint = slapi_attr_first_value(attr, &val);
  3140. i = 0;
  3141. while (val)
  3142. {
  3143. referral[i++] = slapi_ch_strdup(slapi_value_get_string(val));
  3144. hint = slapi_attr_next_value(attr, hint, &val);
  3145. }
  3146. referral[i] = NULL;
  3147. slapi_attr_free(&attr);
  3148. }
  3149. slapi_ch_free_string(&node_dn);
  3150. return referral;
  3151. }
  3152. /*
  3153. * Get the state of a mapping tree node entry
  3154. * notes :
  3155. * - sdn argument is the dn of the subtree of the DIT managed by this node
  3156. * not the dn of the mapping tree entry
  3157. * - the state is return in a newly allocated string that must be freed by
  3158. * the caller
  3159. */
  3160. char *
  3161. slapi_mtn_get_state(const Slapi_DN *sdn)
  3162. {
  3163. char * node_dn;
  3164. Slapi_Attr *attr;
  3165. char * state = NULL;
  3166. Slapi_Value *val = NULL;
  3167. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  3168. if(!node_dn){
  3169. /* shutdown has been detected */
  3170. return NULL;
  3171. }
  3172. attr = mtn_get_attr(node_dn, "nsslapd-state");
  3173. if (attr)
  3174. {
  3175. /* entry state was found */
  3176. slapi_attr_first_value(attr, &val);
  3177. state = slapi_ch_strdup(slapi_value_get_string(val));
  3178. slapi_attr_free(&attr);
  3179. }
  3180. slapi_ch_free_string(&node_dn);
  3181. return state;
  3182. }
  3183. static void
  3184. mtn_internal_be_set_state(Slapi_Backend *be, int state)
  3185. {
  3186. mapping_tree_node * node;
  3187. char * be_name;
  3188. int i;
  3189. int change_callback = 0;
  3190. int old_state;
  3191. mtn_wlock();
  3192. be_name = slapi_ch_strdup(slapi_be_get_name(be));
  3193. node = get_mapping_tree_node_by_name(mapping_tree_root, be_name);
  3194. if (node == NULL)
  3195. {
  3196. LDAPDebug(LDAP_DEBUG_TRACE,
  3197. "Warning: backend %s is not declared in mapping tree\n",
  3198. be_name, 0 ,0);
  3199. goto done;
  3200. }
  3201. /* now search the backend in this node */
  3202. i = 0;
  3203. while ( (i < node->mtn_be_count) &&
  3204. (node->mtn_backend_names) &&
  3205. (node->mtn_backend_names[i]) &&
  3206. (strcmp(node->mtn_backend_names[i],be_name)))
  3207. {
  3208. i++;
  3209. }
  3210. if ( (i >= node->mtn_be_count) || (node->mtn_backend_names == NULL) ||
  3211. (node->mtn_backend_names[i] == NULL) )
  3212. {
  3213. /* backend is not declared in the mapping tree node
  3214. * print out a warning
  3215. */
  3216. LDAPDebug(LDAP_DEBUG_TRACE,
  3217. "Warning: backend %s is not declared in mapping node entry\n",
  3218. be_name, 0 ,0);
  3219. goto done;
  3220. }
  3221. change_callback = 1;
  3222. old_state = node->mtn_be_states[i];
  3223. /* OK we found the backend at last, now do the real job: set the state */
  3224. switch (state)
  3225. {
  3226. case SLAPI_BE_STATE_OFFLINE:
  3227. node->mtn_be[i] = be;
  3228. node->mtn_be_states[i] = SLAPI_BE_STATE_OFFLINE;
  3229. break;
  3230. case SLAPI_BE_STATE_ON:
  3231. node->mtn_be[i] = be;
  3232. node->mtn_be_states[i] = SLAPI_BE_STATE_ON;
  3233. break;
  3234. case SLAPI_BE_STATE_DELETE:
  3235. node->mtn_be[i] = NULL;
  3236. node->mtn_be_states[i] = SLAPI_BE_STATE_DELETE;
  3237. break;
  3238. }
  3239. done:
  3240. mtn_unlock();
  3241. if (change_callback)
  3242. mtn_be_state_change(be_name, old_state, state);
  3243. slapi_ch_free( (void **) &be_name);
  3244. }
  3245. /*
  3246. * This procedure must be called by previously stopped backends
  3247. * to signal that they have started and are ready to process requests
  3248. * The backend must be fully ready to handle requests before calling this
  3249. * procedure
  3250. * At startup tiem it is not mandatory for the backends to
  3251. * call this procedure: backends are assumed on by default
  3252. */
  3253. void
  3254. slapi_mtn_be_started(Slapi_Backend *be)
  3255. {
  3256. /* Find the node where this backend stay
  3257. * then update the backend structure
  3258. * In the long term, the backend should have only one suffix and
  3259. * stay in only one node as for now, check all suffixes
  3260. * Rq : since mapping tree is initiatized very soon in the server
  3261. * startup, we can be sure at that time that the mapping
  3262. * tree is initialized
  3263. */
  3264. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3265. }
  3266. /* these procedure can be called when backends need to be put in maintenance mode
  3267. * after call to slapi_mtn_be_disable, the backend will still be known
  3268. * by a server but the mapping tree won't route requests to it anymore
  3269. * The slapi_mtn_be_enable function enable to route requests to the backend
  3270. * again
  3271. * the slapi_mtn_be_disable function only returns when there is no more
  3272. * request in progress in the backend
  3273. */
  3274. void
  3275. slapi_mtn_be_disable(Slapi_Backend *be)
  3276. {
  3277. mtn_internal_be_set_state(be, SLAPI_BE_STATE_OFFLINE);
  3278. /* the two following lines can seem weird, but they allow to check that no
  3279. * LDAP operation is in progress on the backend
  3280. */
  3281. slapi_be_Wlock(be);
  3282. slapi_be_Unlock(be);
  3283. }
  3284. void
  3285. slapi_mtn_be_enable(Slapi_Backend *be)
  3286. {
  3287. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3288. }
  3289. /*
  3290. * This procedure must be called by backends before stopping
  3291. * if some operations are in progress when this procedure
  3292. * is called, this procedure will block until completion
  3293. * of these operations
  3294. * The backend must wait return from this procedure before stopping operation
  3295. * Backends must serve operation until the return from this procedure.
  3296. * Once this procedure return they will not be issued request anymore
  3297. * and they have been removed from the server list of backends
  3298. * It is also the bakend responsability to free the Slapi_Backend structures
  3299. * that was given by slapi_be_new at startup time.
  3300. * Should the backend start again, it would need to issue slapi_be_new again
  3301. */
  3302. void
  3303. slapi_mtn_be_stopping(Slapi_Backend *be)
  3304. {
  3305. mtn_internal_be_set_state(be, SLAPI_BE_STATE_DELETE);
  3306. /* the two following lines can seem weird, but they allow to check that no
  3307. * LDAP operation is in progress on the backend
  3308. */
  3309. slapi_be_Wlock(be);
  3310. slapi_be_Unlock(be);
  3311. slapi_be_stopping(be);
  3312. }
  3313. /*
  3314. * Switch a backend into read-only mode, or back to read-write mode.
  3315. * To switch to read-only mode, we need to wait for all pending operations
  3316. * to finish.
  3317. */
  3318. void
  3319. slapi_mtn_be_set_readonly(Slapi_Backend *be, int readonly)
  3320. {
  3321. if (readonly) {
  3322. slapi_be_Wlock(be);
  3323. slapi_be_set_readonly(be, 1);
  3324. slapi_be_Unlock(be);
  3325. } else {
  3326. slapi_be_set_readonly(be, 0);
  3327. }
  3328. }
  3329. #ifdef DEBUG
  3330. static int lock_count = 0;
  3331. #endif
  3332. void mtn_wlock()
  3333. {
  3334. slapi_rwlock_wrlock(myLock);
  3335. #ifdef DEBUG
  3336. lock_count--;
  3337. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_wlock : lock count : %d\n", lock_count, 0, 0);
  3338. #endif
  3339. }
  3340. void mtn_lock()
  3341. {
  3342. slapi_rwlock_rdlock(myLock);
  3343. #ifdef DEBUG
  3344. lock_count++;
  3345. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_lock : lock count : %d\n", lock_count, 0, 0);
  3346. #endif
  3347. }
  3348. void mtn_unlock()
  3349. {
  3350. #ifdef DEBUG
  3351. if (lock_count > 0)
  3352. lock_count--;
  3353. else if (lock_count < 0)
  3354. lock_count++;
  3355. else
  3356. lock_count = (int) 11111111; /* this happening means problems */
  3357. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_unlock : lock count : %d\n", lock_count, 0, 0);
  3358. #endif
  3359. slapi_rwlock_unlock(myLock);
  3360. }
  3361. #ifdef TEST_FOR_REGISTER_CHANGE
  3362. void my_test_fnct1(void *handle, char *be_name, int old_state, int new_state)
  3363. {
  3364. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3365. "my_test_fnct1 : handle %d, be %s, old state %d, new state %d\n",
  3366. handle,be_name, old_state, new_state);
  3367. if (old_state == 2)
  3368. slapi_unregister_backend_state_change(handle);
  3369. }
  3370. void my_test_fnct2(void *handle, char *be_name, int old_state, int new_state)
  3371. {
  3372. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3373. "my_test_fnct2 : handle %d, be %s, old state %d, new state %d\n",
  3374. handle, be_name, old_state, new_state);
  3375. }
  3376. void test_register()
  3377. {
  3378. slapi_register_backend_state_change((void *) 1234, my_test_fnct1);
  3379. slapi_register_backend_state_change((void *) 4321, my_test_fnct2);
  3380. }
  3381. #endif
  3382. #ifdef DEBUG
  3383. #ifdef USE_DUMP_MAPPING_TREE
  3384. static void dump_mapping_tree(mapping_tree_node *parent, int depth)
  3385. {
  3386. mapping_tree_node *current = NULL;
  3387. static char dump_indent[256];
  3388. int i;
  3389. if (depth == 0)
  3390. {
  3391. LDAPDebug(LDAP_DEBUG_ANY, "dump_mapping_tree\n", 0, 0, 0);
  3392. }
  3393. dump_indent[0] = '\0';
  3394. for (i = 0; i < depth; i++)
  3395. PL_strcatn(dump_indent, sizeof(dump_indent), " ");
  3396. for (current = parent->mtn_children; current;
  3397. current = current->mtn_brother)
  3398. {
  3399. if (strlen(current->mtn_subtree->dn) == 0)
  3400. {
  3401. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3402. dump_indent, "none", current);
  3403. }
  3404. else
  3405. {
  3406. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3407. dump_indent, current->mtn_subtree->dn, current);
  3408. }
  3409. dump_mapping_tree(current, depth+1);
  3410. }
  3411. return;
  3412. }
  3413. #endif /* USE_DUMP_MAPPING_TREE */
  3414. #endif /* DEBUG */
  3415. /* helper function to set/remove the config param in cn=config */
  3416. static int
  3417. _mtn_update_config_param(int op, char *type, char *strvalue)
  3418. {
  3419. Slapi_PBlock confpb;
  3420. Slapi_DN sdn;
  3421. Slapi_Mods smods;
  3422. LDAPMod **mods;
  3423. int rc = LDAP_PARAM_ERROR;
  3424. pblock_init (&confpb);
  3425. slapi_mods_init (&smods, 0);
  3426. switch (op) {
  3427. case LDAP_MOD_DELETE:
  3428. slapi_mods_add(&smods, op, type, 0, NULL);
  3429. break;
  3430. case LDAP_MOD_ADD:
  3431. case LDAP_MOD_REPLACE:
  3432. slapi_mods_add_string(&smods, op, type, strvalue);
  3433. break;
  3434. default:
  3435. return rc;
  3436. }
  3437. slapi_sdn_init_ndn_byref(&sdn, SLAPD_CONFIG_DN);
  3438. slapi_modify_internal_set_pb_ext(&confpb, &sdn,
  3439. slapi_mods_get_ldapmods_byref(&smods),
  3440. NULL, NULL,
  3441. (void *)plugin_get_default_component_id(), 0);
  3442. slapi_modify_internal_pb(&confpb);
  3443. slapi_pblock_get (&confpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3444. slapi_sdn_done(&sdn);
  3445. /* need to free passed out mods
  3446. * since the internal modify could realloced mods. */
  3447. slapi_pblock_get(&confpb, SLAPI_MODIFY_MODS, &mods);
  3448. ldap_mods_free (mods, 1 /* Free the Array and the Elements */);
  3449. pblock_done(&confpb);
  3450. return rc;
  3451. }