mapping_tree.c 118 KB

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