mapping_tree.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641
  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. /*
  874. * Description:
  875. * A first attempt at walking over the mapping tree and making sure things
  876. * make sense. Right now it just makes sure that each parent node has a
  877. * subtree that is the suffix of its children's subtrees. This function
  878. * is called recursively.
  879. *
  880. * Arguments:
  881. * The root node of the mapping tree.
  882. *
  883. * Returns:
  884. * Nothing - it just prints warnings. This should probably change.
  885. */
  886. static void
  887. mapping_tree_node_validate(mapping_tree_node *node)
  888. {
  889. mapping_tree_node *child_entry;
  890. /* Call this function for all of nodes children */
  891. for (child_entry = node->mtn_children; child_entry; child_entry = child_entry->mtn_brother) {
  892. mapping_tree_node_validate(child_entry);
  893. }
  894. if (node->mtn_parent) {
  895. if (!slapi_sdn_issuffix(node->mtn_subtree, node->mtn_parent->mtn_subtree)) {
  896. LDAPDebug(LDAP_DEBUG_ANY,
  897. "Warning: Invalid mapping tree. %s can not be a child of %s\n",
  898. slapi_sdn_get_ndn(node->mtn_subtree),
  899. slapi_sdn_get_ndn(node->mtn_parent->mtn_subtree), 0);
  900. }
  901. }
  902. }
  903. static void
  904. mtn_free_referral_in_node (mapping_tree_node *node)
  905. {
  906. char ** referral = node->mtn_referral;
  907. if (referral)
  908. {
  909. int i;
  910. for (i=0; referral[i]; i++)
  911. slapi_ch_free((void **) &(referral[i]));
  912. slapi_ch_free((void **) &referral);
  913. }
  914. if (node->mtn_referral_entry)
  915. slapi_entry_free(node->mtn_referral_entry);
  916. node->mtn_referral = NULL;
  917. node->mtn_referral_entry = NULL;
  918. }
  919. int mapping_tree_entry_modify_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  920. {
  921. LDAPMod **mods;
  922. int i;
  923. mapping_tree_node * node;
  924. Slapi_DN * subtree;
  925. Slapi_Attr * attr;
  926. Slapi_Value *val = NULL;
  927. int be_list_count = 0;
  928. int be_list_size = 0;
  929. backend **backends = NULL;
  930. char **be_names = NULL;
  931. int * be_states = NULL;
  932. char * plugin_fct = NULL;
  933. char * plugin_lib = NULL;
  934. int plugin_flag = 0;
  935. mtn_distrib_fct plugin = NULL;
  936. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  937. subtree = get_subtree_from_entry(entryAfter);
  938. node = mtn_get_mapping_tree_node_by_entry(mapping_tree_root, subtree);
  939. if (node == NULL)
  940. {
  941. /* should never happen */
  942. *returncode = LDAP_OPERATIONS_ERROR;
  943. return SLAPI_DSE_CALLBACK_ERROR;
  944. }
  945. for (i = 0; mods[i] != NULL; i++) {
  946. if ( (strcasecmp(mods[i]->mod_type, "cn") == 0) ||
  947. (strcasecmp(mods[i]->mod_type,
  948. MAPPING_TREE_PARENT_ATTRIBUTE) == 0) )
  949. {
  950. mapping_tree_node * parent_node;
  951. /* if we are deleting this attribute the new parent
  952. * node will be mapping_tree_root
  953. */
  954. if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  955. {
  956. parent_node = mapping_tree_root;
  957. }
  958. else if ((strcasecmp(mods[i]->mod_type, "cn") == 0) &&
  959. SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  960. {
  961. /* Allow to add an additional cn.
  962. * e.g., cn: "<suffix>" for the backward compatibility.
  963. * No need to update the mapping tree node itself.
  964. */
  965. continue;
  966. }
  967. else
  968. {
  969. /* we have to find the new parent node */
  970. Slapi_DN *parent_node_dn;
  971. parent_node_dn = get_parent_from_entry(entryAfter);
  972. parent_node = mtn_get_mapping_tree_node_by_entry(
  973. mapping_tree_root, parent_node_dn);
  974. if (parent_node == NULL)
  975. {
  976. parent_node = mapping_tree_root;
  977. LDAPDebug(LDAP_DEBUG_ANY,
  978. "Error: could not find parent for %s\n",
  979. slapi_entry_get_dn(entryAfter), 0, 0);
  980. slapi_sdn_free(&subtree);
  981. *returncode = LDAP_UNWILLING_TO_PERFORM;
  982. return SLAPI_DSE_CALLBACK_ERROR;
  983. }
  984. slapi_sdn_free(&parent_node_dn);
  985. }
  986. mtn_wlock();
  987. /* modifying the parent of a node means moving it to an
  988. * other place of the tree
  989. * this can be done simply by removing it from its old place and
  990. * moving it to the new one
  991. */
  992. mtn_remove_node(node);
  993. mapping_tree_node_add_child(parent_node, node);
  994. node->mtn_parent = parent_node;
  995. mtn_unlock();
  996. }
  997. else if (strcasecmp(mods[i]->mod_type, "nsslapd-backend" ) == 0)
  998. {
  999. slapi_entry_attr_find(entryAfter, "nsslapd-backend", &attr);
  1000. if (NULL == attr)
  1001. {
  1002. /* if nsslapd-backend attribute is empty all backends have
  1003. * been suppressed, set backend list to NULL
  1004. * checks on the state are done a bit later
  1005. */
  1006. backends = NULL;
  1007. be_names = NULL;
  1008. be_states = NULL;
  1009. be_list_count = 0;
  1010. be_list_size = 0;
  1011. }
  1012. else if (get_backends_from_attr(attr, &backends, &be_names,
  1013. &be_states, &be_list_count, &be_list_size, node))
  1014. {
  1015. free_mapping_tree_node_arrays(&backends, &be_names, &be_states, &be_list_count);
  1016. slapi_sdn_free(&subtree);
  1017. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1018. return SLAPI_DSE_CALLBACK_ERROR;
  1019. }
  1020. mtn_wlock();
  1021. if ((backends == NULL) && (node->mtn_state == MTN_BACKEND))
  1022. {
  1023. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "mapping tree entry need at least one nsslapd-backend\n");
  1024. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1025. mtn_unlock();
  1026. free_mapping_tree_node_arrays(&backends, &be_names, &be_states, &be_list_count);
  1027. slapi_sdn_free(&subtree);
  1028. return SLAPI_DSE_CALLBACK_ERROR;
  1029. }
  1030. /* free any old data */
  1031. free_mapping_tree_node_arrays(&node->mtn_be, &node->mtn_backend_names, &node->mtn_be_states, &node->mtn_be_count);
  1032. node->mtn_be_states = be_states;
  1033. node->mtn_be = backends;
  1034. node->mtn_backend_names = be_names;
  1035. node->mtn_be_count = be_list_count;
  1036. node->mtn_be_list_size = be_list_size;
  1037. mtn_unlock();
  1038. }
  1039. else if (strcasecmp(mods[i]->mod_type, "nsslapd-state" ) == 0)
  1040. {
  1041. Slapi_Value * val;
  1042. const char * new_state;
  1043. Slapi_Attr * attr;
  1044. /* state change
  1045. * for now only allow replace
  1046. */
  1047. if (!SLAPI_IS_MOD_REPLACE(mods[i]->mod_op))
  1048. {
  1049. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "must use replace operation to change state\n");
  1050. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1051. slapi_sdn_free(&subtree);
  1052. return SLAPI_DSE_CALLBACK_ERROR;
  1053. }
  1054. if ((mods[i]->mod_bvalues == NULL) || (mods[i]->mod_bvalues[0] == NULL))
  1055. {
  1056. slapi_sdn_free(&subtree);
  1057. *returncode = LDAP_OPERATIONS_ERROR;
  1058. return SLAPI_DSE_CALLBACK_ERROR;
  1059. }
  1060. slapi_entry_attr_find(entryAfter, "nsslapd-state", &attr);
  1061. slapi_attr_first_value(attr, &val);
  1062. new_state = slapi_value_get_string(val);
  1063. if (mtn_state_to_int(new_state, entryAfter) == MTN_BACKEND)
  1064. {
  1065. if (slapi_entry_attr_find(entryAfter, "nsslapd-backend", &attr))
  1066. {
  1067. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-backend before moving to backend state\n");
  1068. slapi_sdn_free(&subtree);
  1069. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1070. return SLAPI_DSE_CALLBACK_ERROR;
  1071. }
  1072. }
  1073. if ((mtn_state_to_int(new_state, entryAfter) == MTN_REFERRAL) ||
  1074. (mtn_state_to_int(new_state, entryAfter) == MTN_REFERRAL_ON_UPDATE))
  1075. {
  1076. if (slapi_entry_attr_find(entryAfter, "nsslapd-referral", &attr))
  1077. {
  1078. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-referral before moving to referral state\n");
  1079. slapi_sdn_free(&subtree);
  1080. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1081. return SLAPI_DSE_CALLBACK_ERROR;
  1082. }
  1083. }
  1084. mtn_wlock();
  1085. node->mtn_state = mtn_state_to_int(new_state, entryAfter);
  1086. mtn_unlock();
  1087. }
  1088. else if (strcasecmp(mods[i]->mod_type, "nsslapd-referral" ) == 0)
  1089. {
  1090. char ** referral;
  1091. mtn_wlock();
  1092. if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)
  1093. || SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  1094. {
  1095. /* delete old referrals, set new ones */
  1096. mtn_free_referral_in_node(node);
  1097. referral = mtn_get_referral_from_entry(entryAfter);
  1098. node->mtn_referral = referral;
  1099. node->mtn_referral_entry =
  1100. referral2entry(referral, slapi_sdn_get_dn(subtree));
  1101. } else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  1102. {
  1103. /* it is not OK to delete the referrals if they are still
  1104. * used
  1105. */
  1106. if ((node->mtn_state == MTN_REFERRAL) ||
  1107. (node->mtn_state == MTN_REFERRAL_ON_UPDATE))
  1108. {
  1109. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1110. "cannot delete referrals in this state\n");
  1111. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1112. mtn_unlock();
  1113. slapi_sdn_free(&subtree);
  1114. return SLAPI_DSE_CALLBACK_ERROR;
  1115. }
  1116. mtn_free_referral_in_node(node);
  1117. } else
  1118. {
  1119. *returncode = LDAP_OPERATIONS_ERROR;
  1120. mtn_unlock();
  1121. slapi_sdn_free(&subtree);
  1122. return SLAPI_DSE_CALLBACK_ERROR;
  1123. }
  1124. mtn_unlock();
  1125. slapi_sdn_free(&subtree);
  1126. *returncode = LDAP_SUCCESS;
  1127. return SLAPI_DSE_CALLBACK_OK;
  1128. }
  1129. else if (strcasecmp(mods[i]->mod_type,
  1130. "nsslapd-distribution-funct" ) == 0)
  1131. {
  1132. if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)
  1133. || SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  1134. {
  1135. slapi_entry_attr_find(entryAfter,
  1136. "nsslapd-distribution-funct", &attr);
  1137. slapi_attr_first_value(attr, &val);
  1138. if (NULL == val) {
  1139. LDAPDebug(LDAP_DEBUG_ANY,
  1140. "Warning: The nsslapd-distribution-funct attribute"
  1141. " has no value for the mapping tree node %s\n",
  1142. slapi_entry_get_dn(entryAfter), 0, 0);
  1143. plugin_fct = NULL;
  1144. }
  1145. plugin_fct = slapi_ch_strdup(slapi_value_get_string(val));
  1146. }
  1147. else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  1148. {
  1149. plugin_fct = NULL;
  1150. }
  1151. plugin_flag = 1;
  1152. }
  1153. else if (strcasecmp(mods[i]->mod_type,
  1154. "nsslapd-distribution-plugin" ) == 0)
  1155. {
  1156. if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)
  1157. || SLAPI_IS_MOD_ADD(mods[i]->mod_op))
  1158. {
  1159. slapi_entry_attr_find(entryAfter,
  1160. "nsslapd-distribution-plugin", &attr);
  1161. slapi_attr_first_value(attr, &val);
  1162. if (NULL == val) {
  1163. LDAPDebug(LDAP_DEBUG_ANY,
  1164. "Warning: The nsslapd-distribution-plugin attribute"
  1165. " has no value for the mapping tree node %s\n",
  1166. slapi_entry_get_dn(entryAfter), 0, 0);
  1167. plugin_lib = NULL;
  1168. }
  1169. plugin_lib = slapi_ch_strdup(slapi_value_get_string(val));
  1170. }
  1171. else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op))
  1172. {
  1173. plugin_lib = NULL;
  1174. }
  1175. plugin_flag = 1;
  1176. }
  1177. }
  1178. /* if distribution plugin has been configured or modified
  1179. * check that the library and function exist
  1180. * and if yes apply the modifications
  1181. */
  1182. if (plugin_flag)
  1183. {
  1184. if (plugin_lib && plugin_fct)
  1185. {
  1186. plugin = (mtn_distrib_fct) sym_load(plugin_lib, plugin_fct, "Entry Distribution", 1);
  1187. if (plugin == NULL)
  1188. {
  1189. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "cannot find distribution plugin\n");
  1190. slapi_ch_free((void **) &plugin_fct);
  1191. slapi_ch_free((void **) &plugin_lib);
  1192. slapi_sdn_free(&subtree);
  1193. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1194. return SLAPI_DSE_CALLBACK_ERROR;
  1195. }
  1196. }
  1197. else if ((plugin_lib == NULL) && (plugin_fct == NULL))
  1198. {
  1199. /* nothing configured -> OK */
  1200. plugin = NULL;
  1201. }
  1202. else
  1203. {
  1204. /* only one parameter configured -> ERROR */
  1205. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1206. "must define distribution function and library\n");
  1207. slapi_ch_free((void **) &plugin_fct);
  1208. slapi_ch_free((void **) &plugin_lib);
  1209. slapi_sdn_free(&subtree);
  1210. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1211. return SLAPI_DSE_CALLBACK_ERROR;
  1212. }
  1213. mtn_wlock();
  1214. if (node->mtn_dstr_plg_lib)
  1215. slapi_ch_free((void **) &node->mtn_dstr_plg_lib);
  1216. node->mtn_dstr_plg_lib = plugin_lib;
  1217. if (node->mtn_dstr_plg_name)
  1218. slapi_ch_free((void **) &node->mtn_dstr_plg_name);
  1219. node->mtn_dstr_plg_name = plugin_fct;
  1220. node->mtn_dstr_plg = plugin;
  1221. mtn_unlock();
  1222. }
  1223. slapi_sdn_free(&subtree);
  1224. return SLAPI_DSE_CALLBACK_OK;
  1225. }
  1226. int mapping_tree_entry_add_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  1227. {
  1228. mapping_tree_node *node = NULL;
  1229. int i;
  1230. backend * be;
  1231. /* WARNING
  1232. * for adds we don't need to grab the mapping tree global lock,
  1233. * because the add operation in the tree is atomic because
  1234. * only one pointer is updated in the tree.
  1235. * Should the mapping tree stucture change, this would have to
  1236. * be checked again
  1237. */
  1238. *returncode = mapping_tree_entry_add(entryBefore, &node);
  1239. if (LDAP_SUCCESS != *returncode || !node)
  1240. {
  1241. return SLAPI_DSE_CALLBACK_ERROR;
  1242. }
  1243. if(node->mtn_parent != NULL && node != mapping_tree_root )
  1244. {
  1245. /* If the node has a parent and the node is not the mapping tree root,
  1246. * then add it as a child node. Note that the special case when the
  1247. * node is the mapping tree root and has no parent is handled inside
  1248. * the mapping_tree_entry_add() function by replacing the contents of
  1249. * the mapping tree root node with information from the add request.
  1250. */
  1251. mapping_tree_node_add_child(node->mtn_parent, node);
  1252. }
  1253. for (i = 0; ((i < node->mtn_be_count) && (node->mtn_backend_names) &&
  1254. (node->mtn_backend_names[i])); i++)
  1255. {
  1256. if ((be = slapi_be_select_by_instance_name(node->mtn_backend_names[i]))
  1257. && (be->be_state == BE_STATE_STARTED))
  1258. {
  1259. mtn_be_state_change(node->mtn_backend_names[i], SLAPI_BE_STATE_DELETE,
  1260. node->mtn_be_states[i]);
  1261. }
  1262. }
  1263. node->mtn_extension = factory_create_extension(mapping_tree_get_extension_type(), node, NULL);
  1264. return SLAPI_DSE_CALLBACK_OK;
  1265. }
  1266. /* utility function to remove a node from the tree of mapping_tree_node
  1267. */
  1268. static void mtn_remove_node(mapping_tree_node * node)
  1269. {
  1270. if (node->mtn_parent->mtn_children == node)
  1271. node->mtn_parent->mtn_children = node->mtn_brother;
  1272. else
  1273. {
  1274. mapping_tree_node * tmp_node = node->mtn_parent->mtn_children;
  1275. while (tmp_node && (tmp_node->mtn_brother != node))
  1276. tmp_node = tmp_node->mtn_brother;
  1277. PR_ASSERT(tmp_node != NULL);
  1278. if (tmp_node) tmp_node->mtn_brother = node->mtn_brother;
  1279. }
  1280. node->mtn_brother = NULL;
  1281. }
  1282. int mapping_tree_entry_delete_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  1283. {
  1284. int result;
  1285. mapping_tree_node *node = NULL;
  1286. Slapi_DN * subtree;
  1287. int i;
  1288. int removed = 0;
  1289. mtn_wlock();
  1290. subtree = get_subtree_from_entry(entryBefore);
  1291. if (subtree == NULL)
  1292. {
  1293. /* there is no cn attribute in this entry
  1294. * -> this is not a mapping tree node
  1295. * -> nothing to do
  1296. */
  1297. result = SLAPI_DSE_CALLBACK_OK;
  1298. goto done;
  1299. }
  1300. node = slapi_get_mapping_tree_node_by_dn(subtree);
  1301. if (node == NULL)
  1302. {
  1303. /* should never happen */
  1304. *returncode = LDAP_OPERATIONS_ERROR;
  1305. result = SLAPI_DSE_CALLBACK_ERROR;
  1306. goto done;
  1307. }
  1308. if (slapi_sdn_compare(subtree, node->mtn_subtree))
  1309. {
  1310. /* There is no node associated to this entry
  1311. * -> nothing to do
  1312. */
  1313. result = SLAPI_DSE_CALLBACK_OK;
  1314. goto done;
  1315. }
  1316. /* if node has children we must refuse the delete */
  1317. if (node->mtn_children)
  1318. {
  1319. result = SLAPI_DSE_CALLBACK_ERROR;
  1320. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1321. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "this node has some children");
  1322. goto done;
  1323. }
  1324. /* at this point the node should be different from mapping_tree_root
  1325. * and therefore have a parent
  1326. */
  1327. PR_ASSERT(node->mtn_parent != NULL);
  1328. /* lets get the node out of the mapping tree */
  1329. mtn_remove_node(node);
  1330. result = SLAPI_DSE_CALLBACK_OK;
  1331. removed = 1;
  1332. done:
  1333. mtn_unlock();
  1334. slapi_sdn_free(&subtree);
  1335. if (SLAPI_DSE_CALLBACK_OK == result && removed)
  1336. {
  1337. /* Signal the plugins that a new backend-suffix has been deleted
  1338. * rq : we have to unlock the mapping tree in that case because
  1339. * most of the plugins will try to search upon this notification
  1340. * and should we keep the lock we would end with a dead-lock
  1341. */
  1342. for (i = 0; ((i < node->mtn_be_count) && (node->mtn_backend_names) &&
  1343. (node->mtn_backend_names[i])); i++)
  1344. {
  1345. if ((node->mtn_be_states[i] != SLAPI_BE_STATE_DELETE) &&
  1346. (NULL != slapi_be_select_by_instance_name(
  1347. node->mtn_backend_names[i])))
  1348. {
  1349. mtn_be_state_change(node->mtn_backend_names[i],
  1350. node->mtn_be_states[i], SLAPI_BE_STATE_DELETE);
  1351. }
  1352. }
  1353. /* at this point the node is out of the mapping tree,
  1354. * we can now free the structure
  1355. */
  1356. mtn_free_node(&node);
  1357. }
  1358. return result;
  1359. }
  1360. /*
  1361. * Add an internal mapping tree node.
  1362. */
  1363. static mapping_tree_node *
  1364. add_internal_mapping_tree_node(const char *subtree, Slapi_Backend *be, mapping_tree_node *parent)
  1365. {
  1366. Slapi_DN *dn;
  1367. mapping_tree_node *node;
  1368. backend ** be_list = (backend **) slapi_ch_malloc(sizeof(backend **));
  1369. be_list[0] = be;
  1370. dn = slapi_sdn_new_dn_byval(subtree);
  1371. node= mapping_tree_node_new(
  1372. dn,
  1373. be_list,
  1374. NULL, /* backend_name */
  1375. NULL,
  1376. 1, /* number of backends at this node */
  1377. 1, /* size of backend list structure */
  1378. NULL, /* referral */
  1379. parent,
  1380. MTN_BACKEND,
  1381. 1, /* The config node is a private node.
  1382. * People can't see or change it. */
  1383. NULL, NULL, NULL);
  1384. return node;
  1385. }
  1386. /*
  1387. * Description:
  1388. * Inits the mapping tree. The mapping tree is rooted at a node with
  1389. * subtree "". Think of this node as the node for the NULL suffix
  1390. * even though we don't really support it. This function will
  1391. * create the root node and then consult the DIT for the rest of
  1392. * the nodes. It will also add the node for cn=config.
  1393. *
  1394. * One thing to note... Until the mapping tree is inited. We use
  1395. * slapi_be_select for all our selection needs. To read in the mapping
  1396. * tree from the DIT, we need to some internal operations. These
  1397. * operations need to use slapi_be_select.
  1398. *
  1399. * Arguments:
  1400. * Nothing
  1401. *
  1402. * Returns:
  1403. * Right now it always returns 0. This will most likely change. Right
  1404. * now, we just log warnings when ever something goes wrong.
  1405. */
  1406. int
  1407. mapping_tree_init()
  1408. {
  1409. Slapi_Backend *be;
  1410. mapping_tree_node *node;
  1411. /* Create the root of the mapping tree. */
  1412. /* The root of the mapping tree is the NULL suffix. It's always there,
  1413. * but, because we don't really support it, we won't have an entry in
  1414. * the dit for the NULL suffix mapping tree node. */
  1415. /* Once we support the NULL suffix we should do something more clever here.
  1416. * For now will use the current backend we use for "" */
  1417. /* I'm not really sure what the state of the root node should be. The root
  1418. * node will end up being selected if none of the suffices for the backends
  1419. * would work with the target. For now when the root node is selected,
  1420. * the default backend will be returned. (The special case where the
  1421. * target dn is "" is handled differently.) */
  1422. /* we call this function from a single thread, so it should be ok */
  1423. if(mapping_tree_freed){
  1424. /* shutdown has been detected */
  1425. return 0;
  1426. }
  1427. if (mapping_tree_inited)
  1428. return 0;
  1429. /* ONREPL - I have moved this up because otherwise we can endup calling this
  1430. * function recursively */
  1431. mapping_tree_inited = 1;
  1432. slapi_register_supported_control(MTN_CONTROL_USE_ONE_BACKEND_OID,
  1433. SLAPI_OPERATION_SEARCH);
  1434. slapi_register_supported_control(MTN_CONTROL_USE_ONE_BACKEND_EXT_OID,
  1435. SLAPI_OPERATION_SEARCH);
  1436. myLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "mapping tree");
  1437. be= slapi_be_select_by_instance_name(DSE_BACKEND);
  1438. mapping_tree_root= add_internal_mapping_tree_node("", be, NULL);
  1439. /* We also need to add the config and schema backends to the mapping tree.
  1440. * They are special in that users will not know about it's node in the
  1441. * mapping tree. This is to prevent them from disableing it or
  1442. * returning a referral for it. */
  1443. node= add_internal_mapping_tree_node("cn=config", be, mapping_tree_root);
  1444. mapping_tree_node_add_child(mapping_tree_root, node);
  1445. node= add_internal_mapping_tree_node("cn=monitor", be, mapping_tree_root);
  1446. mapping_tree_node_add_child(mapping_tree_root, node);
  1447. be= slapi_be_select_by_instance_name( DSE_SCHEMA );
  1448. node= add_internal_mapping_tree_node("cn=schema", be, mapping_tree_root);
  1449. mapping_tree_node_add_child(mapping_tree_root, node);
  1450. /*
  1451. * Now we need to look under cn=mapping tree, cn=config to find the rest
  1452. * of the mapping tree entries.
  1453. * Builds the mapping tree from entries in the DIT. This function just
  1454. * calls mapping_tree_node_get_children with the special case for the
  1455. * root node.
  1456. */
  1457. if (mapping_tree_node_get_children(mapping_tree_root, 1))
  1458. return -1;
  1459. mtn_create_extension(mapping_tree_root);
  1460. /* setup the dse callback functions for the ldbm instance config entry */
  1461. {
  1462. slapi_config_register_callback(SLAPI_OPERATION_MODIFY,
  1463. DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
  1464. "(objectclass=nsMappingTree)",
  1465. mapping_tree_entry_modify_callback, NULL);
  1466. slapi_config_register_callback(SLAPI_OPERATION_ADD,
  1467. DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
  1468. "(objectclass=nsMappingTree)", mapping_tree_entry_add_callback,
  1469. NULL);
  1470. slapi_config_register_callback(SLAPI_OPERATION_DELETE,
  1471. DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
  1472. "(objectclass=nsMappingTree)", mapping_tree_entry_delete_callback,
  1473. NULL);
  1474. }
  1475. return 0;
  1476. }
  1477. static void
  1478. mtn_free_node (mapping_tree_node **node)
  1479. {
  1480. mapping_tree_node *child = (*node)->mtn_children;
  1481. /* free children first */
  1482. while (child)
  1483. {
  1484. mapping_tree_node * tmp_child = child->mtn_brother;
  1485. mtn_free_node (&child);
  1486. child = tmp_child;
  1487. }
  1488. (*node)->mtn_children = NULL;
  1489. (*node)->mtn_parent = NULL;
  1490. /* free this node */
  1491. /* ONREPL - not quite sure which fields should be freed. For now,
  1492. only freeing fields explicitely allocated in the new_node function */
  1493. factory_destroy_extension (mapping_tree_get_extension_type(), *node, NULL,
  1494. &((*node)->mtn_extension));
  1495. slapi_sdn_free(&((*node)->mtn_subtree));
  1496. mtn_free_referral_in_node(*node);
  1497. if ((*node)->mtn_be_count > 0)
  1498. {
  1499. free_mapping_tree_node_arrays(&((*node)->mtn_be), &((*node)->mtn_backend_names),
  1500. &((*node)->mtn_be_states), &((*node)->mtn_be_count));
  1501. }
  1502. slapi_ch_free_string(&((*node)->mtn_dstr_plg_lib));
  1503. slapi_ch_free_string(&((*node)->mtn_dstr_plg_name));
  1504. slapi_ch_free ((void**) node);
  1505. }
  1506. /* Description: frees the tree; should be called when the server shuts down
  1507. */
  1508. void
  1509. mapping_tree_free ()
  1510. {
  1511. /* unregister dse callbacks */
  1512. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_modify_callback);
  1513. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_add_callback);
  1514. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_delete_callback);
  1515. /* The state change plugins registered on the mapping tree
  1516. * should not get any state change information
  1517. * - unregister all those callbacks
  1518. */
  1519. slapi_unregister_backend_state_change_all();
  1520. /* recursively free tree nodes */
  1521. mtn_free_node (&mapping_tree_root);
  1522. mapping_tree_freed = 1;
  1523. }
  1524. /* This function returns the first node to parse when a search is done
  1525. * on a given node in the mapping tree
  1526. */
  1527. static mapping_tree_node *
  1528. mtn_get_first_node(mapping_tree_node * node, int scope)
  1529. {
  1530. if (node == NULL)
  1531. return NULL;
  1532. /* never climb down the tree from base "" */
  1533. if (node == mapping_tree_root)
  1534. {
  1535. return node;
  1536. }
  1537. if (scope == LDAP_SCOPE_BASE)
  1538. return node;
  1539. if (scope == LDAP_SCOPE_ONELEVEL)
  1540. {
  1541. if (node->mtn_children)
  1542. return node->mtn_children;
  1543. else
  1544. return node;
  1545. }
  1546. while (node->mtn_children)
  1547. node = node->mtn_children;
  1548. return node;
  1549. }
  1550. int slapi_mtn_get_first_be(mapping_tree_node * node_list,
  1551. mapping_tree_node ** node, Slapi_PBlock *pb, Slapi_Backend **be,
  1552. int * be_index, Slapi_Entry **referral, char *errorbuf, int scope)
  1553. {
  1554. *node = mtn_get_first_node(node_list, scope);
  1555. if (scope == LDAP_SCOPE_BASE)
  1556. *be_index = -1;
  1557. else
  1558. *be_index = 0;
  1559. return mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
  1560. }
  1561. int slapi_mtn_get_next_be(mapping_tree_node * node_list,
  1562. mapping_tree_node ** node, Slapi_PBlock *pb, Slapi_Backend **be,
  1563. int * be_index, Slapi_Entry **referral, char *errorbuf, int scope)
  1564. {
  1565. int rc;
  1566. if (((*node)->mtn_parent == NULL) || /* -> node has been deleted */
  1567. (scope == LDAP_SCOPE_BASE))
  1568. {
  1569. *node = NULL;
  1570. *be = NULL;
  1571. *referral = NULL;
  1572. return 0;
  1573. }
  1574. /* never climb down the tree from the rootDSE */
  1575. if (node_list == mapping_tree_root)
  1576. {
  1577. *node = NULL;
  1578. *be = NULL;
  1579. *referral = NULL;
  1580. return 0;
  1581. }
  1582. rc = mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
  1583. if (rc != LDAP_SUCCESS)
  1584. {
  1585. *node = mtn_get_next_node(*node, node_list, scope);
  1586. return rc;
  1587. }
  1588. if ((*be == NULL) && (*referral == NULL))
  1589. {
  1590. *node = mtn_get_next_node(*node, node_list, scope);
  1591. if (*node == NULL)
  1592. {
  1593. *be = NULL;
  1594. return 0;
  1595. }
  1596. *be_index = 0;
  1597. return mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
  1598. }
  1599. return LDAP_SUCCESS;
  1600. }
  1601. /* This function returns the next node to parse when a subtree search is done
  1602. * on a given node in the mapping tree
  1603. */
  1604. static mapping_tree_node *
  1605. mtn_get_next_node(mapping_tree_node * node, mapping_tree_node * node_list, int scope)
  1606. {
  1607. if (scope == LDAP_SCOPE_BASE)
  1608. return NULL;
  1609. /* if we are back to the top of the subtree searched then we have finished */
  1610. if (node == node_list)
  1611. node = NULL;
  1612. else if (node->mtn_brother)
  1613. {
  1614. node = node->mtn_brother;
  1615. if (scope == LDAP_SCOPE_SUBTREE)
  1616. while (node->mtn_children)
  1617. node = node->mtn_children;
  1618. }
  1619. else
  1620. node = node->mtn_parent;
  1621. return node;
  1622. }
  1623. /* Description :
  1624. * return 0 if the given entry does not have any child node in the mapping tree
  1625. * != otherwise
  1626. *
  1627. */
  1628. int
  1629. mtn_sdn_has_child(Slapi_DN *target_sdn)
  1630. {
  1631. mapping_tree_node *node;
  1632. /* algo : get the target node for the given dn
  1633. * then loop through all its child to check if one of them is below
  1634. * the target dn
  1635. */
  1636. node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1637. /* if there is no node for this dn then there is no child either */
  1638. if (node == NULL)
  1639. return 0;
  1640. node = node->mtn_children;
  1641. while (node)
  1642. {
  1643. if (slapi_sdn_issuffix(node->mtn_subtree, target_sdn))
  1644. return 1;
  1645. node = node->mtn_brother;
  1646. }
  1647. return 0;
  1648. }
  1649. /* Description:
  1650. * Find the backend that would be used to store a dn.
  1651. */
  1652. Slapi_Backend *slapi_mapping_tree_find_backend_for_sdn(Slapi_DN *sdn)
  1653. {
  1654. mapping_tree_node *target_node;
  1655. Slapi_Backend *be;
  1656. int flag_stop = 0, index;
  1657. Slapi_PBlock *pb;
  1658. Slapi_Operation *op;
  1659. mtn_lock();
  1660. target_node = slapi_get_mapping_tree_node_by_dn(sdn);
  1661. if ((target_node == mapping_tree_root) &&
  1662. (slapi_sdn_get_ndn_len(sdn) > 0)) {
  1663. /* couldn't find a matching node */
  1664. be = defbackend_get_backend();
  1665. goto done;
  1666. }
  1667. if ((target_node == NULL) || (target_node->mtn_be_count == 0)) {
  1668. /* no backend configured for this node */
  1669. be = NULL;
  1670. goto done;
  1671. }
  1672. if (target_node->mtn_be_count == 1) {
  1673. /* not distributed, so we've already found it */
  1674. if (target_node->mtn_be[0] == NULL) {
  1675. target_node->mtn_be[0] = slapi_be_select_by_instance_name(
  1676. target_node->mtn_backend_names[0]);
  1677. }
  1678. be = target_node->mtn_be[0];
  1679. goto done;
  1680. }
  1681. /* have to call the distribution plugin */
  1682. be = defbackend_get_backend();
  1683. pb = slapi_pblock_new();
  1684. if (!pb) {
  1685. goto done;
  1686. }
  1687. op = internal_operation_new(SLAPI_OPERATION_ADD, 0);
  1688. if (!op) {
  1689. slapi_pblock_destroy(pb);
  1690. goto done;
  1691. }
  1692. operation_set_target_spec(op, sdn);
  1693. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  1694. index = mtn_get_be_distributed(pb, target_node, sdn, &flag_stop);
  1695. slapi_pblock_destroy(pb); /* also frees the operation */
  1696. if (target_node->mtn_be[index] == NULL) {
  1697. target_node->mtn_be[index] = slapi_be_select_by_instance_name(
  1698. target_node->mtn_backend_names[index]);
  1699. }
  1700. be = target_node->mtn_be[index];
  1701. done:
  1702. mtn_unlock();
  1703. return be;
  1704. }
  1705. /* Check if the target dn is '\0' - the null dn */
  1706. static int sdn_is_nulldn(const Slapi_DN *sdn){
  1707. if(sdn){
  1708. const char *dn= slapi_sdn_get_ndn(sdn);
  1709. if(dn && ( '\0' == *dn)){
  1710. return 1;
  1711. }
  1712. }
  1713. return 0;
  1714. }
  1715. /* Checks if a write operation for a particular DN would
  1716. * require a referral to be sent. */
  1717. int slapi_dn_write_needs_referral(Slapi_DN *target_sdn, Slapi_Entry **referral)
  1718. {
  1719. mapping_tree_node *target_node = NULL;
  1720. int ret = 0;
  1721. if(mapping_tree_freed){
  1722. /* shutdown detected */
  1723. goto done;
  1724. }
  1725. if(!mapping_tree_inited) {
  1726. mapping_tree_init();
  1727. }
  1728. if (target_sdn) {
  1729. mtn_lock();
  1730. /* Get the mapping tree node that is the best match for the target dn. */
  1731. target_node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1732. if (target_node == NULL) {
  1733. target_node = mapping_tree_root;
  1734. }
  1735. /* See if we need to return a referral. */
  1736. if ((target_node->mtn_state == MTN_REFERRAL) ||
  1737. (target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) {
  1738. *referral = (target_node->mtn_referral_entry ?
  1739. slapi_entry_dup(target_node->mtn_referral_entry) :
  1740. NULL);
  1741. if (*referral) {
  1742. ret = 1;
  1743. }
  1744. }
  1745. mtn_unlock();
  1746. }
  1747. done:
  1748. return ret;
  1749. }
  1750. /*
  1751. * Description:
  1752. * The reason we have a mapping tree. This function selects a backend or
  1753. * referral to handle a given request. Uses the target of the operation to
  1754. * find a mapping tree node, then based on the operation type, bind dn, state
  1755. * of the node, etc. it selects a backend or referral.
  1756. *
  1757. * In this initial implementation of the mapping tree, each node can only have
  1758. * one backend and one referral. Later we should change this so each node has
  1759. * a list of backends and a list of referrals. Then we should add a modifier
  1760. * to the state of the node. For example, MTN_MODIFIER_ROUND_ROBIN could be a
  1761. * modifer on the way a backend or referral is returned from the lists.
  1762. *
  1763. * Arguments:
  1764. * pb is the pblock being used to service the operation.
  1765. * be is an output param that will be set to the selected backend.
  1766. * referral is an output param that will be set to the selected referral.
  1767. * errorbuf is a pointer to a buffer that an error string will be written to
  1768. * if there is an error. The caller is responsible for passing in a big
  1769. * enough chunk of memory. BUFSIZ should be fine. If errorbuf is NULL,
  1770. * no error string is written to it. The string returned in errorbuf
  1771. * would be a good candidate for sending back to the client to describe the
  1772. * error.
  1773. *
  1774. * Returns:
  1775. * LDAP_SUCCESS on success, other LDAP result codes if there is a problem.
  1776. */
  1777. int slapi_mapping_tree_select(Slapi_PBlock *pb, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
  1778. {
  1779. Slapi_DN *target_sdn = NULL;
  1780. mapping_tree_node *target_node;
  1781. Slapi_Operation *op;
  1782. int index;
  1783. int ret;
  1784. int scope=LDAP_SCOPE_BASE;
  1785. int op_type;
  1786. if(mapping_tree_freed){
  1787. /* shutdown detected */
  1788. return LDAP_OPERATIONS_ERROR;
  1789. }
  1790. if (errorbuf) {
  1791. errorbuf[0] = '\0';
  1792. }
  1793. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1794. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  1795. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  1796. /* Get the target for this op */
  1797. target_sdn = operation_get_target_spec (op);
  1798. if(!mapping_tree_inited) {
  1799. mapping_tree_init();
  1800. }
  1801. be[0] = NULL;
  1802. referral[0] = NULL;
  1803. mtn_lock();
  1804. /* Get the mapping tree node that is the best match for the target dn. */
  1805. target_node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1806. if (target_node == NULL)
  1807. target_node = mapping_tree_root;
  1808. /* The processing of the base scope root DSE search and all other LDAP operations on ""
  1809. * will be transferred to the internal DSE backend
  1810. */
  1811. if( sdn_is_nulldn(target_sdn) &&
  1812. (((op_type == SLAPI_OPERATION_SEARCH) && (scope == LDAP_SCOPE_BASE)) ||
  1813. (op_type != SLAPI_OPERATION_SEARCH)) ) {
  1814. mtn_unlock();
  1815. *be = slapi_be_select_by_instance_name(DSE_BACKEND);
  1816. if(*be != NULL && !be_isdeleted(*be))
  1817. {
  1818. ret = LDAP_SUCCESS;
  1819. slapi_be_Rlock(*be); /* also done inside mtn_get_be() below */
  1820. } else {
  1821. ret = LDAP_OPERATIONS_ERROR;
  1822. }
  1823. return ret;
  1824. }
  1825. /* index == -1 is used to specify that we want only one backend not a list
  1826. * used for BASE search, ADD, DELETE, MODIFY
  1827. */
  1828. index = -1;
  1829. ret = mtn_get_be(target_node, pb, be, &index, referral, errorbuf);
  1830. slapi_pblock_set(pb, SLAPI_BACKEND_COUNT, &index);
  1831. mtn_unlock();
  1832. /* if a backend was returned, make sure that all non-search operations
  1833. * fail if the backend is read-only,
  1834. * or if the whole server is readonly AND backend is public (!private)
  1835. */
  1836. if ((ret == LDAP_SUCCESS) && *be && !be_isdeleted(*be) &&
  1837. ((*be)->be_readonly ||
  1838. (slapi_config_get_readonly() && !slapi_be_private(*be)))) {
  1839. unsigned long op_type = operation_get_type(op);
  1840. if ((op_type != SLAPI_OPERATION_SEARCH) &&
  1841. (op_type != SLAPI_OPERATION_COMPARE) &&
  1842. (op_type != SLAPI_OPERATION_BIND) &&
  1843. (op_type != SLAPI_OPERATION_UNBIND)) {
  1844. ret = LDAP_UNWILLING_TO_PERFORM;
  1845. PL_strncpyz(errorbuf, slapi_config_get_readonly() ?
  1846. "Server is read-only" :
  1847. "database is read-only", BUFSIZ);
  1848. slapi_be_Unlock(*be);
  1849. *be = NULL;
  1850. }
  1851. }
  1852. return ret;
  1853. }
  1854. int slapi_mapping_tree_select_all(Slapi_PBlock *pb, Slapi_Backend **be_list,
  1855. Slapi_Entry **referral_list, char *errorbuf)
  1856. {
  1857. Slapi_DN *target_sdn = NULL;
  1858. mapping_tree_node *node_list;
  1859. mapping_tree_node *node;
  1860. Slapi_Operation *op;
  1861. int index;
  1862. int ret;
  1863. int ret_code = LDAP_SUCCESS;
  1864. int be_index = 0 ;
  1865. int referral_index = 0 ;
  1866. Slapi_Backend * be;
  1867. Slapi_Entry * referral;
  1868. int scope = LDAP_SCOPE_BASE;
  1869. Slapi_DN sdn;
  1870. char *base;
  1871. int flag_partial_result = 0;
  1872. int op_type;
  1873. if(mapping_tree_freed){
  1874. return LDAP_OPERATIONS_ERROR;
  1875. }
  1876. if (errorbuf) {
  1877. errorbuf[0] = '\0';
  1878. }
  1879. /* get the operational parameters */
  1880. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &base);
  1881. slapi_sdn_init_dn_ndn_byref(&sdn, base); /* normalized in opshared.c */
  1882. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1883. target_sdn = operation_get_target_spec (op);
  1884. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  1885. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  1886. if(!mapping_tree_inited){
  1887. mapping_tree_init();
  1888. }
  1889. mtn_lock();
  1890. be_list[0] = NULL;
  1891. referral_list[0] = NULL;
  1892. /* Get the mapping tree node that is the best match for the target dn. */
  1893. node_list = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1894. if (node_list == NULL)
  1895. node_list = mapping_tree_root;
  1896. if( sdn_is_nulldn(target_sdn) && ( op_type == SLAPI_OPERATION_SEARCH)
  1897. && (scope == LDAP_SCOPE_BASE) ) {
  1898. mtn_unlock();
  1899. be = slapi_be_select_by_instance_name(DSE_BACKEND);
  1900. if(be != NULL && !be_isdeleted(be))
  1901. {
  1902. be_list[0]=be;
  1903. be_list[1] = NULL;
  1904. ret_code = LDAP_SUCCESS;
  1905. slapi_be_Rlock(be); /* also done inside mtn_get_be() below */
  1906. } else {
  1907. ret_code = LDAP_OPERATIONS_ERROR;
  1908. }
  1909. return ret_code;
  1910. }
  1911. ret = slapi_mtn_get_first_be(node_list, &node, pb, &be, &index, &referral, errorbuf, scope);
  1912. while ((node) &&(index < BE_LIST_SIZE))
  1913. {
  1914. if (ret != LDAP_SUCCESS)
  1915. {
  1916. /* flag we have problems at least on part of the tree */
  1917. flag_partial_result = 1;
  1918. }
  1919. else if ( ( ((!slapi_sdn_issuffix(&sdn, slapi_mtn_get_dn(node))
  1920. && !slapi_sdn_issuffix(slapi_mtn_get_dn(node), &sdn)))
  1921. || ((node_list == mapping_tree_root) && node->mtn_private
  1922. && (scope != LDAP_SCOPE_BASE)) )
  1923. && (!be || strncmp(be->be_name, "default", 8)))
  1924. {
  1925. if (be && !be_isdeleted(be))
  1926. {
  1927. /* wrong backend or referall, ignore it */
  1928. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  1929. "mapping tree release backend : %s\n",
  1930. slapi_be_get_name(be));
  1931. slapi_be_Unlock(be);
  1932. }
  1933. }
  1934. else
  1935. {
  1936. if (be && !be_isdeleted(be))
  1937. {
  1938. be_list[be_index++]=be;
  1939. }
  1940. if (referral)
  1941. {
  1942. referral_list[referral_index++] = referral;
  1943. /* if we hit a referral at the base of the search
  1944. * we must return a REFERRAL error with only this referral
  1945. * all backend or referral below this node are ignored
  1946. */
  1947. if (slapi_sdn_issuffix(target_sdn, slapi_mtn_get_dn(node)))
  1948. {
  1949. ret_code = LDAP_REFERRAL;
  1950. break; /* get out of the while loop */
  1951. }
  1952. }
  1953. }
  1954. ret = slapi_mtn_get_next_be(node_list, &node, pb, &be, &index,
  1955. &referral, errorbuf, scope);
  1956. }
  1957. mtn_unlock();
  1958. slapi_sdn_done(&sdn);
  1959. be_list[be_index] = NULL;
  1960. referral_list[referral_index] = NULL;
  1961. if (flag_partial_result)
  1962. {
  1963. /* if no node in active has been found -> return LDAP_OPERATIONS_ERROR
  1964. * but if only part of the nodes are disabled
  1965. * do not return an error to allow directory browser to work OK
  1966. * in the console
  1967. * It would be better to return a meaningfull error
  1968. * unfortunately LDAP_PARTIAL_RESULTS is not usable because
  1969. * it is already used for V2 referrals
  1970. * leave no error for now and fix this later
  1971. */
  1972. if ((be_index == 0) && (referral_index == 0))
  1973. return LDAP_OPERATIONS_ERROR;
  1974. else
  1975. return ret_code;
  1976. }
  1977. else
  1978. return ret_code;
  1979. }
  1980. void slapi_mapping_tree_free_all(Slapi_Backend **be_list, Slapi_Entry **referral_list)
  1981. {
  1982. int index = 0;
  1983. /* go through the list of all backends that was used for the operation
  1984. * and unlock them
  1985. * go through the list of referrals and free them
  1986. * free the two tables that were used to store the two lists
  1987. */
  1988. if (be_list[index] != NULL)
  1989. {
  1990. Slapi_Backend * be;
  1991. while ((be = be_list[index++]))
  1992. {
  1993. slapi_log_error(SLAPI_LOG_ARGS, NULL, "mapping tree release backend : %s\n", slapi_be_get_name(be));
  1994. slapi_be_Unlock(be);
  1995. }
  1996. }
  1997. index = 0;
  1998. if (referral_list[index] != NULL)
  1999. {
  2000. Slapi_Entry * referral;
  2001. while ((referral = referral_list[index++]))
  2002. {
  2003. slapi_entry_free(referral);
  2004. }
  2005. }
  2006. }
  2007. /* same as slapi_mapping_tree_select() but will also check that the supplied
  2008. * newdn is in the same backend
  2009. */
  2010. int slapi_mapping_tree_select_and_check(Slapi_PBlock *pb,char *newdn, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
  2011. {
  2012. Slapi_DN *target_sdn = NULL;
  2013. Slapi_DN dn_newdn;
  2014. Slapi_Backend * new_be = NULL;
  2015. Slapi_Backend * def_be = defbackend_get_backend();
  2016. Slapi_Entry * new_referral = NULL;
  2017. mapping_tree_node *target_node;
  2018. int index;
  2019. Slapi_Operation *op;
  2020. int ret;
  2021. int need_unlock = 0;
  2022. if(mapping_tree_freed){
  2023. return LDAP_OPERATIONS_ERROR;
  2024. }
  2025. slapi_sdn_init(&dn_newdn);
  2026. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2027. target_sdn = operation_get_target_spec (op);
  2028. * referral = NULL;
  2029. ret = slapi_mapping_tree_select(pb, be, referral, errorbuf);
  2030. if (ret)
  2031. goto unlock_and_return;
  2032. slapi_sdn_init_dn_byref(&dn_newdn,newdn);
  2033. /* acquire lock now, after slapi_mapping_tree_select() which also locks,
  2034. because we are accessing mt internals */
  2035. mtn_lock();
  2036. need_unlock = 1; /* we have now acquired the lock */
  2037. target_node = slapi_get_mapping_tree_node_by_dn(&dn_newdn);
  2038. if (target_node == NULL)
  2039. target_node = mapping_tree_root;
  2040. index = -1;
  2041. ret = mtn_get_be(target_node, pb, &new_be, &index, &new_referral, errorbuf);
  2042. if (ret)
  2043. goto unlock_and_return;
  2044. if (*be)
  2045. {
  2046. /* suffix is a part of mapping tree. We should not free it */
  2047. const Slapi_DN *suffix = slapi_get_suffix_by_dn(target_sdn);
  2048. if ((*be != def_be) && (NULL == suffix))
  2049. {
  2050. ret = LDAP_NO_SUCH_OBJECT;
  2051. PR_snprintf(errorbuf, BUFSIZ,
  2052. "Target entry \"%s\" does not exist\n",
  2053. slapi_sdn_get_dn(target_sdn));
  2054. goto unlock_and_return;
  2055. }
  2056. if (suffix && (0 == slapi_sdn_compare(target_sdn, suffix)))
  2057. {
  2058. /* target_sdn is a suffix */
  2059. const Slapi_DN *new_suffix = NULL;
  2060. /* new_suffix is a part of mapping tree. We should not free it */
  2061. new_suffix = slapi_get_suffix_by_dn(&dn_newdn);
  2062. if (!slapi_be_exist((const Slapi_DN *)&dn_newdn))
  2063. {
  2064. /* new_be is an empty backend */
  2065. ret = LDAP_NO_SUCH_OBJECT;
  2066. PR_snprintf(errorbuf, BUFSIZ,
  2067. "Backend for suffix \"%s\" does not exist\n", newdn);
  2068. goto unlock_and_return;
  2069. }
  2070. if (0 == slapi_sdn_compare(&dn_newdn, new_suffix))
  2071. {
  2072. ret = LDAP_ALREADY_EXISTS;
  2073. PR_snprintf(errorbuf, BUFSIZ,
  2074. "Suffix \"%s\" already exists\n", newdn);
  2075. goto unlock_and_return;
  2076. }
  2077. ret = LDAP_NAMING_VIOLATION;
  2078. PR_snprintf(errorbuf, BUFSIZ, "Cannot rename suffix \"%s\"\n",
  2079. slapi_sdn_get_dn(target_sdn));
  2080. goto unlock_and_return;
  2081. }
  2082. else
  2083. {
  2084. if ((*be != new_be) || mtn_sdn_has_child(target_sdn))
  2085. {
  2086. ret = LDAP_AFFECTS_MULTIPLE_DSAS;
  2087. PR_snprintf(errorbuf, BUFSIZ,
  2088. "Cannot move entries accross backends\n");
  2089. goto unlock_and_return;
  2090. }
  2091. }
  2092. }
  2093. unlock_and_return:
  2094. /* if slapi_mapping_tree_select failed, we won't have the lock */
  2095. if (need_unlock) {
  2096. mtn_unlock();
  2097. }
  2098. slapi_sdn_done(&dn_newdn);
  2099. if (new_be)
  2100. slapi_be_Unlock(new_be);
  2101. if (new_referral)
  2102. slapi_entry_free(new_referral);
  2103. if (ret != LDAP_SUCCESS)
  2104. {
  2105. if (be && *be && !be_isdeleted(*be))
  2106. {
  2107. slapi_be_Unlock(*be);
  2108. *be = NULL;
  2109. }
  2110. if (*referral)
  2111. {
  2112. slapi_entry_free(*referral);
  2113. *referral = NULL;
  2114. }
  2115. }
  2116. return ret;
  2117. }
  2118. /*
  2119. * allow to solve the distribution problem when several back-ends are defined
  2120. */
  2121. static int
  2122. mtn_get_be_distributed(Slapi_PBlock *pb, mapping_tree_node * target_node,
  2123. Slapi_DN *target_sdn, int * flag_stop)
  2124. {
  2125. int index;
  2126. *flag_stop = 0;
  2127. if (target_node->mtn_dstr_plg)
  2128. {
  2129. index = (*target_node->mtn_dstr_plg)(pb, target_sdn,
  2130. target_node->mtn_backend_names, target_node->mtn_be_count,
  2131. target_node->mtn_subtree, target_node->mtn_be_states);
  2132. if (index == SLAPI_BE_ALL_BACKENDS)
  2133. {
  2134. /* special value to indicate all backends must be scanned
  2135. * start with first one
  2136. */
  2137. index = 0;
  2138. }
  2139. /* paranoid check, never trust another programmer */
  2140. else if ((index >= target_node->mtn_be_count) || (index < 0))
  2141. {
  2142. LDAPDebug(LDAP_DEBUG_ANY,
  2143. "Warning: distribution plugin returned wrong backend"
  2144. " : %d for entry %s at node %s\n",
  2145. index, slapi_sdn_get_ndn(target_sdn),
  2146. slapi_sdn_get_ndn(target_node->mtn_subtree));
  2147. index = 0;
  2148. }
  2149. else
  2150. {
  2151. /* only one backend to scan
  2152. * set flag_stop to indicate we must stop the search here
  2153. */
  2154. *flag_stop = 1;
  2155. }
  2156. }
  2157. else
  2158. {
  2159. /* there is several backends but no distribution function
  2160. * return the first backend
  2161. */
  2162. LDAPDebug(LDAP_DEBUG_ANY,
  2163. "Warning: distribution plugin not configured at node : %s\n",
  2164. slapi_sdn_get_ndn(target_node->mtn_subtree), 0, 0);
  2165. index = 0;
  2166. }
  2167. return index;
  2168. }
  2169. /*
  2170. * this function is in charge of choosing the right backend for a given
  2171. * mapping tree node
  2172. * In case when several backends are used it is in charge of the spanning the
  2173. * request among all the backend or choosing the only backend to use depending
  2174. * on the type and scope of the LDAP operation
  2175. *
  2176. * index == -1 is used to specify that we want only the one best backend
  2177. * used for BASE search, ADD, DELETE, MODIFY
  2178. * index >0 means we are doing a SUBTREE or ONELEVEL search and that the be in
  2179. * that position must be returned
  2180. */
  2181. static int mtn_get_be(mapping_tree_node *target_node, Slapi_PBlock *pb,
  2182. Slapi_Backend **be, int * index, Slapi_Entry **referral, char *errorbuf)
  2183. {
  2184. Slapi_DN *target_sdn;
  2185. Slapi_Operation *op;
  2186. int result = LDAP_SUCCESS;
  2187. int override_referral = 0;
  2188. unsigned long op_type;
  2189. int flag_stop = 0;
  2190. struct slapi_componentid *cid = NULL;
  2191. if(mapping_tree_freed){
  2192. /* shut down detected */
  2193. return LDAP_OPERATIONS_ERROR;
  2194. }
  2195. /* Get usefull stuff like the type of operation, target dn */
  2196. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2197. op_type = operation_get_type(op);
  2198. target_sdn = operation_get_target_spec (op);
  2199. if (target_node->mtn_state == MTN_DISABLED) {
  2200. if (errorbuf) {
  2201. PR_snprintf(errorbuf, BUFSIZ,
  2202. "Warning: Operation attempted on a disabled node : %s\n",
  2203. slapi_sdn_get_dn(target_node->mtn_subtree));
  2204. }
  2205. result = LDAP_OPERATIONS_ERROR;
  2206. return result;
  2207. }
  2208. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &cid);
  2209. override_referral =
  2210. ((cid != NULL) && (pw_get_componentID() != NULL) && (pw_get_componentID() == cid)) ||
  2211. operation_is_flag_set(op, OP_FLAG_LEGACY_REPLICATION_DN) || /* 4.0 lgacy update */
  2212. operation_is_flag_set(op, OP_FLAG_REPLICATED) || /* 5.0 replication update */
  2213. operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY); /* 5.1 fix to enable tombstone delete on a R-O consumer */
  2214. if ((target_node->mtn_state == MTN_BACKEND) ||
  2215. (target_node->mtn_state == MTN_CONTAINER ) ||
  2216. ((target_node->mtn_state == MTN_REFERRAL_ON_UPDATE) &&
  2217. ((SLAPI_OPERATION_SEARCH == op_type)||(SLAPI_OPERATION_BIND == op_type) ||
  2218. (SLAPI_OPERATION_UNBIND == op_type) || (SLAPI_OPERATION_COMPARE == op_type))) ||
  2219. override_referral) {
  2220. if ((target_node == mapping_tree_root) ){
  2221. /* If we got here, then we couldn't find a matching node
  2222. * for the target. We'll use the default backend. Once
  2223. * we fully support the NULL suffix, we should do something more
  2224. * clever here.
  2225. */
  2226. *be = defbackend_get_backend();
  2227. } else {
  2228. if ((*index == -1) || (*index == 0)) {
  2229. /* In this case, we are doing
  2230. * a READ, ADD, MODIDY or DELETE on a single entry
  2231. * or we are starting a SEARCH
  2232. * if there is several possible backend we want to apply
  2233. * the distribution plugin
  2234. */
  2235. if (target_node->mtn_be_count <= 1) {
  2236. /* there is only one backend no choice possible */
  2237. *index = 0;
  2238. } else {
  2239. *index = mtn_get_be_distributed(pb, target_node,
  2240. target_sdn, &flag_stop);
  2241. }
  2242. }
  2243. if ((*index == -2) || (*index >= target_node->mtn_be_count)) {
  2244. /* we have already returned all backends -> return NULL */
  2245. *be = NULL;
  2246. *referral = NULL;
  2247. } else {
  2248. /* return next backend, increment index */
  2249. *be = target_node->mtn_be[*index];
  2250. if(*be==NULL) {
  2251. if (NULL != target_node->mtn_be_states &&
  2252. target_node->mtn_be_states[*index] == SLAPI_BE_STATE_DELETE) {
  2253. /* This MTN is being deleted */
  2254. *be = defbackend_get_backend();
  2255. } else {
  2256. /* This MTN has not been linked to its backend
  2257. * instance yet. */
  2258. target_node->mtn_be[*index] =
  2259. slapi_be_select_by_instance_name(
  2260. target_node->mtn_backend_names[*index]);
  2261. *be = target_node->mtn_be[*index];
  2262. if(*be==NULL) {
  2263. LDAPDebug(LDAP_DEBUG_ANY,
  2264. "Warning: Mapping tree node entry for %s point to "
  2265. "an unknown backend : %s\n",
  2266. slapi_sdn_get_dn(target_node->mtn_subtree),
  2267. target_node->mtn_backend_names[*index], 0);
  2268. /* Well there's still not backend instance for
  2269. * this MTN, so let's have the default backend
  2270. * deal with this.
  2271. */
  2272. *be = defbackend_get_backend();
  2273. }
  2274. }
  2275. }
  2276. if ((target_node->mtn_be_states) &&
  2277. (target_node->mtn_be_states[*index] == SLAPI_BE_STATE_OFFLINE)) {
  2278. LDAPDebug(LDAP_DEBUG_TRACE,
  2279. "Warning: Operation attempted on backend in OFFLINE "
  2280. "state : %s\n",
  2281. target_node->mtn_backend_names[*index], 0, 0);
  2282. result = LDAP_OPERATIONS_ERROR;
  2283. *be = defbackend_get_backend();
  2284. }
  2285. if (flag_stop)
  2286. *index = -2;
  2287. else
  2288. (*index)++;
  2289. }
  2290. }
  2291. *referral = NULL;
  2292. } else {
  2293. /* otherwise we must return the referral
  2294. * if ((target_node->mtn_state == MTN_REFERRAL) ||
  2295. * (target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) */
  2296. if (*index > 0) {
  2297. /* we have already returned this referral
  2298. * send back NULL to jump to next node
  2299. */
  2300. *be = NULL;
  2301. *referral = NULL;
  2302. result = LDAP_SUCCESS;
  2303. } else {
  2304. /* first time we hit this referral -> return it
  2305. * set the be variable to NULL to indicate we use a referral
  2306. * and increment index to rememeber later that we already
  2307. * returned this referral
  2308. */
  2309. *be = NULL;
  2310. *referral = (target_node->mtn_referral_entry ?
  2311. slapi_entry_dup(target_node->mtn_referral_entry) :
  2312. NULL);
  2313. (*index)++;
  2314. if (NULL == *referral) {
  2315. if (errorbuf) {
  2316. PR_snprintf(errorbuf, BUFSIZ,
  2317. "Mapping tree node for %s is set to return a referral,"
  2318. " but no referral is configured for it",
  2319. slapi_sdn_get_ndn(target_node->mtn_subtree));
  2320. }
  2321. result = LDAP_OPERATIONS_ERROR;
  2322. } else {
  2323. result = LDAP_SUCCESS;
  2324. }
  2325. }
  2326. }
  2327. if (result == LDAP_SUCCESS) {
  2328. if (*be && !be_isdeleted(*be)) {
  2329. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2330. "mapping tree selected backend : %s\n",
  2331. slapi_be_get_name(*be));
  2332. slapi_be_Rlock(*be);
  2333. } else if (*referral) {
  2334. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2335. "mapping tree selected referral at node : %s\n",
  2336. slapi_sdn_get_dn(target_node->mtn_subtree));
  2337. }
  2338. }
  2339. return result;
  2340. }
  2341. /*
  2342. * Description:
  2343. * Finds the best match for the targetdn from the children of parent. Uses
  2344. * slapi_sdn_issuffix and the number of rdns to pick the best node.
  2345. *
  2346. * Arguments:
  2347. * parent is a pointer to a mapping tree node.
  2348. * targetdn is the dn we're trying to find the best match for.
  2349. *
  2350. * Returns:
  2351. * A pointer to the child of parent that best matches the targetdn. NULL
  2352. * if there were no good matches.
  2353. */
  2354. static mapping_tree_node *best_matching_child(mapping_tree_node *parent,
  2355. const Slapi_DN *targetdn)
  2356. {
  2357. mapping_tree_node *highest_match_node = NULL;
  2358. mapping_tree_node *current;
  2359. if(mapping_tree_freed){
  2360. /* shutdown detected */
  2361. return NULL;
  2362. }
  2363. for (current = parent->mtn_children; current;
  2364. current = current->mtn_brother) {
  2365. if (slapi_sdn_issuffix(targetdn, current->mtn_subtree)) {
  2366. if ( (highest_match_node == NULL) ||
  2367. ((slapi_sdn_get_ndn_len(current->mtn_subtree)) >
  2368. slapi_sdn_get_ndn_len(highest_match_node->mtn_subtree)) ) {
  2369. highest_match_node = current;
  2370. }
  2371. }
  2372. }
  2373. return highest_match_node;
  2374. }
  2375. /*
  2376. * look for the exact mapping tree node corresponding to a given entry dn
  2377. */
  2378. static mapping_tree_node *
  2379. mtn_get_mapping_tree_node_by_entry(mapping_tree_node* node, const Slapi_DN *dn)
  2380. {
  2381. mapping_tree_node *found_node = NULL;
  2382. if(mapping_tree_freed){
  2383. /* shutdown detected */
  2384. return NULL;
  2385. }
  2386. if(NULL == dn){
  2387. /* bad mapping tree entry operation */
  2388. return NULL;
  2389. }
  2390. if (slapi_sdn_compare(node->mtn_subtree, dn) == 0)
  2391. {
  2392. return node;
  2393. }
  2394. if (node->mtn_children)
  2395. {
  2396. found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_children, dn);
  2397. if (found_node)
  2398. return found_node;
  2399. }
  2400. if (node->mtn_brother)
  2401. {
  2402. found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_brother, dn);
  2403. }
  2404. return found_node;
  2405. }
  2406. /*
  2407. * Description:
  2408. * Gets a mapping tree node that best matches the given dn. If the root
  2409. * node is returned and the target dn is not "", then no match was found.
  2410. *
  2411. * Arguments:
  2412. * dn is the target of the search.
  2413. *
  2414. * Returns:
  2415. * The best matching node for the dn
  2416. * if nothing match, NULL is returned
  2417. */
  2418. mapping_tree_node *
  2419. slapi_get_mapping_tree_node_by_dn(const Slapi_DN *dn)
  2420. {
  2421. mapping_tree_node *current_best_match = mapping_tree_root;
  2422. mapping_tree_node *next_best_match = mapping_tree_root;
  2423. if(mapping_tree_freed){
  2424. /* shutdown detected */
  2425. return NULL;
  2426. }
  2427. /* Handle special case where the dn is "" and the mapping root
  2428. * does not belong to the frontend-internal (DSE_BACKEND);
  2429. * it has been assigned to a different backend.
  2430. * e.g: a container backend
  2431. */
  2432. if ( sdn_is_nulldn(dn) && mapping_tree_root && mapping_tree_root->mtn_be[0] &&
  2433. mapping_tree_root->mtn_be[0] != slapi_be_select_by_instance_name(DSE_BACKEND)) {
  2434. return( mapping_tree_root );
  2435. }
  2436. /* Start at the root and walk down the tree to find the best match. */
  2437. while (next_best_match) {
  2438. current_best_match = next_best_match;
  2439. next_best_match = best_matching_child(current_best_match, dn);
  2440. }
  2441. if (current_best_match == mapping_tree_root)
  2442. return NULL;
  2443. else
  2444. return current_best_match;
  2445. }
  2446. static mapping_tree_node *
  2447. get_mapping_tree_node_by_name(mapping_tree_node * node, char * be_name)
  2448. {
  2449. int i;
  2450. mapping_tree_node *found_node = NULL;
  2451. if(mapping_tree_freed){
  2452. /* shutdown detected */
  2453. return NULL;
  2454. }
  2455. /* now search the backend in this node */
  2456. i = 0;
  2457. while ( ( i < node->mtn_be_count) &&
  2458. (node->mtn_backend_names) &&
  2459. (node->mtn_backend_names[i]) &&
  2460. (strcmp(node->mtn_backend_names[i],be_name)))
  2461. {
  2462. i++;
  2463. }
  2464. if ((i < node->mtn_be_count) &&
  2465. (node->mtn_backend_names != NULL) &&
  2466. (node->mtn_backend_names[i] != NULL))
  2467. {
  2468. return node;
  2469. }
  2470. if (node->mtn_children)
  2471. {
  2472. found_node = get_mapping_tree_node_by_name(node->mtn_children, be_name);
  2473. if (found_node)
  2474. return found_node;
  2475. }
  2476. if (node->mtn_brother)
  2477. {
  2478. found_node = get_mapping_tree_node_by_name(node->mtn_brother, be_name);
  2479. }
  2480. return found_node;
  2481. }
  2482. /*
  2483. * Description: construct the dn of the configuration entry for the
  2484. * node originated at the root. The function just constructs
  2485. * the dn it does not verify that the entry actually exist.
  2486. * The format of the dn is
  2487. * cn="<normalized root>",cn=mapping tree,cn=config
  2488. *
  2489. * Arguments: root - root of the node
  2490. *
  2491. * Returns: dn of the configuration entry if successful and null otherwise.
  2492. */
  2493. char*
  2494. slapi_get_mapping_tree_node_configdn (const Slapi_DN *root)
  2495. {
  2496. char *dn = NULL;
  2497. if(mapping_tree_freed){
  2498. /* shutdown detected */
  2499. return NULL;
  2500. }
  2501. if (root == NULL)
  2502. return NULL;
  2503. /* This function converts the old DN style to the new one. */
  2504. dn = slapi_create_dn_string("cn=\"%s\",%s",
  2505. slapi_sdn_get_dn(root), MAPPING_TREE_BASE_DN);
  2506. if (NULL == dn) {
  2507. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  2508. "slapi_get_mapping_tree_node_configdn: "
  2509. "failed to crate mapping tree dn for %s\n",
  2510. slapi_sdn_get_dn(root));
  2511. return NULL;
  2512. }
  2513. return dn;
  2514. }
  2515. /*
  2516. * Description: this function returns root of the subtree to which the node applies
  2517. *
  2518. * Arguments: node - mapping tree node
  2519. *
  2520. * Returns: root of the subtree if function is successful and NULL otherwise.
  2521. */
  2522. const Slapi_DN* slapi_get_mapping_tree_node_root (const mapping_tree_node *node)
  2523. {
  2524. if (node)
  2525. return node->mtn_subtree;
  2526. else
  2527. return NULL;
  2528. }
  2529. /* GB : there is a potential problems with this function
  2530. * when several backends are used
  2531. */
  2532. PRBool slapi_mapping_tree_node_is_set (const mapping_tree_node *node, PRUint32 flag)
  2533. {
  2534. if (flag & SLAPI_MTN_LOCAL)
  2535. return PR_TRUE;
  2536. if (flag & SLAPI_MTN_PRIVATE)
  2537. return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_private);
  2538. if (flag & SLAPI_MTN_READONLY)
  2539. return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_be[0]->be_readonly);
  2540. return PR_FALSE;
  2541. }
  2542. /*
  2543. * Description: this function returns root of the subtree to which the node applies
  2544. *
  2545. * Arguments: node
  2546. *
  2547. * Returns: dn of the parent of mapping tree node configuration entry.
  2548. */
  2549. const char* slapi_get_mapping_tree_config_root ()
  2550. {
  2551. return MAPPING_TREE_BASE_DN;
  2552. }
  2553. /*
  2554. * slapi_be_select() finds the backend that should be used to service dn.
  2555. * If no backend with an appropriate suffix is configured, the default backend
  2556. * is returned. This function never returns NULL.
  2557. */
  2558. Slapi_Backend *
  2559. slapi_be_select( const Slapi_DN *sdn ) /* JCM - The name of this should change??? */
  2560. {
  2561. Slapi_Backend *be;
  2562. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2563. if(node!=NULL)
  2564. be= node->mtn_be[0];
  2565. else
  2566. be = NULL;
  2567. if(be==NULL)
  2568. be= defbackend_get_backend();
  2569. return be;
  2570. }
  2571. /* Check if the dn targets an internal reserved backends */
  2572. int
  2573. slapi_on_internal_backends(const Slapi_DN *sdn)
  2574. {
  2575. char *backend_names[] = {DSE_BACKEND, DSE_SCHEMA};
  2576. int internal = 1;
  2577. int numOfInternalBackends = 2;
  2578. int count;
  2579. Slapi_Backend *internal_be;
  2580. Slapi_Backend *be = slapi_be_select(sdn);
  2581. for (count=0; count < numOfInternalBackends ; ++count){
  2582. /* the internal backends are always in the begining of the list
  2583. * so should not be very inefficient
  2584. */
  2585. internal_be = slapi_be_select_by_instance_name(backend_names[count]);
  2586. if(be == internal_be){
  2587. return internal;
  2588. }
  2589. }
  2590. return 0;
  2591. }
  2592. /* Some of the operations are not allowed from the plugins
  2593. * but default to specialized use of those operations
  2594. * e.g rootDse search, ConfigRoot searches
  2595. * cn=config, cn=schema etc
  2596. */
  2597. int slapi_op_reserved(Slapi_PBlock *pb)
  2598. {
  2599. int scope=LDAP_SCOPE_BASE;
  2600. int reservedOp=0;
  2601. int op_type;
  2602. Slapi_Operation *op = NULL;
  2603. Slapi_DN *target_sdn=NULL;
  2604. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2605. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  2606. /* Get the target for this op */
  2607. target_sdn = operation_get_target_spec (op);
  2608. if( op_type == SLAPI_OPERATION_SEARCH){
  2609. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  2610. if( sdn_is_nulldn(target_sdn) && (scope == LDAP_SCOPE_BASE) ){
  2611. reservedOp = 1;
  2612. }
  2613. }
  2614. if(slapi_on_internal_backends(target_sdn)){
  2615. reservedOp = 1;
  2616. }
  2617. return reservedOp;
  2618. }
  2619. /*
  2620. * Returns the name of the Backend that contains specified DN,
  2621. * if only one matches. Otherwise returns NULL
  2622. * The name is pointing to the mapping tree structure
  2623. * and should not be altered.
  2624. */
  2625. const char *
  2626. slapi_mtn_get_backend_name( const Slapi_DN *sdn)
  2627. {
  2628. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2629. if ((node != NULL) &&
  2630. (node->mtn_be_count == 1) &&
  2631. (node->mtn_backend_names != NULL))
  2632. /* There's only one name, return it */
  2633. return node->mtn_backend_names[0];
  2634. else
  2635. return NULL;
  2636. }
  2637. /* Check if the backend that contains specified DN exists */
  2638. int
  2639. slapi_be_exist(const Slapi_DN *sdn) /* JCM - The name of this should change??? */
  2640. {
  2641. Slapi_Backend *def_be = defbackend_get_backend();
  2642. Slapi_Backend *be = slapi_be_select (sdn);
  2643. return (be != def_be);
  2644. }
  2645. /* The two following functions can be used to
  2646. * parse the list of the root suffix of the DIT
  2647. * Using
  2648. */
  2649. Slapi_DN *
  2650. slapi_get_first_suffix(void ** node, int show_private)
  2651. {
  2652. mapping_tree_node * first_node = mapping_tree_root->mtn_children;
  2653. if (NULL == node) {
  2654. return NULL;
  2655. }
  2656. *node = (void * ) first_node ;
  2657. while (first_node && (first_node->mtn_private && (show_private == 0)))
  2658. first_node = first_node->mtn_brother;
  2659. return (first_node ? first_node->mtn_subtree : NULL);
  2660. }
  2661. Slapi_DN *
  2662. slapi_get_next_suffix(void ** node, int show_private)
  2663. {
  2664. mapping_tree_node * next_node = NULL;
  2665. if (NULL == node) {
  2666. return NULL;
  2667. }
  2668. next_node = *node;
  2669. if (next_node == NULL) {
  2670. return NULL;
  2671. }
  2672. next_node = next_node->mtn_brother;
  2673. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2674. next_node = next_node->mtn_brother;
  2675. *node = next_node;
  2676. return (next_node ? next_node->mtn_subtree : NULL);
  2677. }
  2678. /* get mapping tree node recursively */
  2679. Slapi_DN *
  2680. slapi_get_next_suffix_ext(void ** node, int show_private)
  2681. {
  2682. mapping_tree_node * next_node = NULL;
  2683. if (NULL == node) {
  2684. return NULL;
  2685. }
  2686. next_node = *node;
  2687. if (next_node == NULL) {
  2688. return NULL;
  2689. }
  2690. if (next_node->mtn_children) {
  2691. next_node = next_node->mtn_children;
  2692. } else if (next_node->mtn_brother) {
  2693. next_node = next_node->mtn_brother;
  2694. } else {
  2695. next_node = next_node->mtn_parent;
  2696. if (next_node) {
  2697. next_node = next_node->mtn_brother;
  2698. }
  2699. }
  2700. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2701. next_node = next_node->mtn_brother;
  2702. if (next_node) {
  2703. *node = next_node;
  2704. }
  2705. return (next_node ? next_node->mtn_subtree : NULL);
  2706. }
  2707. /* check if a suffix is a root of the DIT
  2708. * return 1 if yes, 0 if no
  2709. */
  2710. int slapi_is_root_suffix(Slapi_DN * dn)
  2711. {
  2712. void * node;
  2713. Slapi_DN * suffix = slapi_get_first_suffix (&node, 1);
  2714. while (suffix)
  2715. {
  2716. if ( slapi_sdn_compare(dn, suffix) == 0 )
  2717. return 1;
  2718. suffix = slapi_get_next_suffix(&node, 1);
  2719. }
  2720. return 0 ;
  2721. }
  2722. /* Return value is a part mapping tree; Don't free it. */
  2723. const Slapi_DN *
  2724. slapi_get_suffix_by_dn(const Slapi_DN *dn)
  2725. {
  2726. mapping_tree_node *node = slapi_get_mapping_tree_node_by_dn(dn);
  2727. const Slapi_DN *suffix = NULL;
  2728. if (node)
  2729. {
  2730. suffix = (const Slapi_DN *)slapi_mtn_get_dn(node);
  2731. }
  2732. return suffix;
  2733. }
  2734. /*
  2735. * set referrals for the node
  2736. * notes :
  2737. * - referral is consumed by this function
  2738. * - node must exist before calling this function
  2739. * - mapping tree node state is not changed by this function
  2740. */
  2741. int
  2742. slapi_mtn_set_referral(const Slapi_DN *sdn, char ** referral)
  2743. {
  2744. Slapi_PBlock pb;
  2745. Slapi_Mods smods;
  2746. int rc = LDAP_SUCCESS,i = 0, j = 0;
  2747. char * node_dn;
  2748. char **values = NULL;
  2749. int do_modify = 0;
  2750. slapi_mods_init (&smods, 0);
  2751. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2752. if(!node_dn){
  2753. /* shutdown has been detected */
  2754. return LDAP_OPERATIONS_ERROR;
  2755. }
  2756. if ((referral == NULL) || (referral[0] == NULL))
  2757. {
  2758. /* NULL referral means we want to delete existing referral
  2759. */
  2760. slapi_mods_add(&smods, LDAP_MOD_DELETE, "nsslapd-referral", 0, NULL);
  2761. do_modify = 1;
  2762. }
  2763. else
  2764. {
  2765. int changes = 1;
  2766. int referralCount = 0;
  2767. for(; referral[referralCount]; referralCount++);
  2768. if ( (values = slapi_mtn_get_referral(sdn)) != NULL )
  2769. {
  2770. /* Check if there are differences between current values and values to be set */
  2771. for (i=0; values[i]; i++);
  2772. if (i == referralCount) {
  2773. changes = 0;
  2774. for (i=0;values[i];i++){
  2775. int found = 0;
  2776. for (j=0;referral[j];j++){
  2777. if (strcmp(values[i], referral[j]) == 0){
  2778. found = 1;
  2779. break;
  2780. }
  2781. }
  2782. if (!found) {
  2783. changes = 1;
  2784. break;
  2785. }
  2786. }
  2787. }
  2788. i=0;
  2789. while(values[i])
  2790. slapi_ch_free((void**)&values[i++]);
  2791. slapi_ch_free((void**)&values);
  2792. }
  2793. if (changes){
  2794. Slapi_Value *val;
  2795. Slapi_Value ** svals = NULL;
  2796. do_modify = 1;
  2797. for (j =0; referral[j];j++) {
  2798. val = slapi_value_new_string(referral[j]);
  2799. valuearray_add_value(&svals, val);
  2800. slapi_value_free(&val);
  2801. }
  2802. slapi_mods_add_mod_values(&smods, LDAP_MOD_REPLACE, "nsslapd-referral", svals);
  2803. valuearray_free(&svals);
  2804. }
  2805. }
  2806. if ( do_modify )
  2807. {
  2808. pblock_init (&pb);
  2809. slapi_modify_internal_set_pb (&pb, node_dn,
  2810. slapi_mods_get_ldapmods_byref(&smods), NULL,
  2811. NULL, (void *) plugin_get_default_component_id(), 0);
  2812. slapi_modify_internal_pb (&pb);
  2813. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2814. pblock_done(&pb);
  2815. }
  2816. slapi_mods_done(&smods);
  2817. slapi_ch_free_string(&node_dn);
  2818. return rc;
  2819. }
  2820. /*
  2821. * Change the state of a mapping tree node entry
  2822. * notes :
  2823. * - sdn argument is the dn of the subtree of the DIT managed by this node
  2824. * not the dn of the mapping tree entry
  2825. * - mapping tree node must exist before calling this function
  2826. */
  2827. int
  2828. slapi_mtn_set_state(const Slapi_DN *sdn, char *state)
  2829. {
  2830. Slapi_PBlock pb;
  2831. Slapi_Mods smods;
  2832. int rc = LDAP_SUCCESS;
  2833. char * node_dn;
  2834. char * value;
  2835. if (NULL == state) {
  2836. return LDAP_OPERATIONS_ERROR;
  2837. }
  2838. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2839. if(!node_dn){
  2840. /* shutdown has been detected */
  2841. return LDAP_OPERATIONS_ERROR;
  2842. }
  2843. if ( (value = slapi_mtn_get_state(sdn)) != NULL )
  2844. {
  2845. if ( strcasecmp(value, state) == 0 )
  2846. {
  2847. /* Same state, don't change anything */
  2848. goto bail;
  2849. }
  2850. }
  2851. /* Otherwise, means that the state has changed, modify it */
  2852. slapi_mods_init (&smods, 1);
  2853. slapi_mods_add(&smods, LDAP_MOD_REPLACE, "nsslapd-state", strlen(state), state);
  2854. pblock_init (&pb);
  2855. slapi_modify_internal_set_pb (&pb, node_dn,
  2856. slapi_mods_get_ldapmods_byref(&smods), NULL,
  2857. NULL, (void *) plugin_get_default_component_id(), 0);
  2858. slapi_modify_internal_pb (&pb);
  2859. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2860. slapi_mods_done(&smods);
  2861. pblock_done(&pb);
  2862. bail:
  2863. slapi_ch_free_string(&value);
  2864. slapi_ch_free_string(&node_dn);
  2865. return rc;
  2866. }
  2867. /*
  2868. returns a copy of the attr - the caller must slapi_attr_free it
  2869. */
  2870. Slapi_Attr *
  2871. mtn_get_attr(char* node_dn, char * type)
  2872. {
  2873. Slapi_PBlock pb;
  2874. int res = 0;
  2875. Slapi_Entry **entries = NULL;
  2876. Slapi_Attr *attr = NULL;
  2877. Slapi_Attr *ret_attr = NULL;
  2878. char **attrs = NULL;
  2879. attrs = (char **)slapi_ch_calloc(2, sizeof(char *));
  2880. attrs[0] = slapi_ch_strdup(type);
  2881. pblock_init(&pb);
  2882. slapi_search_internal_set_pb(&pb, node_dn, LDAP_SCOPE_BASE,
  2883. "objectclass=nsMappingTree", attrs, 0, NULL, NULL,
  2884. (void *) plugin_get_default_component_id(), 0);
  2885. slapi_search_internal_pb(&pb);
  2886. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  2887. if (res != LDAP_SUCCESS) {
  2888. goto done;
  2889. }
  2890. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2891. if (NULL == entries || NULL == entries[0]) {
  2892. goto done;
  2893. }
  2894. /* always at most one entry entries[0] */
  2895. res = slapi_entry_attr_find(entries[0], type, &attr);
  2896. if (res == 0)
  2897. /* we need to make a copy here so we can free the search results */
  2898. ret_attr = slapi_attr_dup(attr);
  2899. slapi_free_search_results_internal(&pb);
  2900. done:
  2901. slapi_ch_free((void **)&attrs[0]);
  2902. slapi_ch_free((void **)&attrs);
  2903. pblock_done(&pb);
  2904. return ret_attr;
  2905. }
  2906. /*
  2907. * Get the referral associated to the mapping tree node entry
  2908. * notes :
  2909. * - sdn argument is the dn of the subtree of the DIT managed by this node
  2910. * not the dn of the mapping tree entry
  2911. * - return NULL if no referral
  2912. * - caller is reponsible for freeing the returned referrals
  2913. */
  2914. char **
  2915. slapi_mtn_get_referral(const Slapi_DN *sdn)
  2916. {
  2917. int i, hint, nb;
  2918. char * node_dn;
  2919. Slapi_Attr *attr;
  2920. char ** referral = NULL;
  2921. Slapi_Value *val = NULL;
  2922. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2923. if(!node_dn){
  2924. /* shutdown has been detected */
  2925. return NULL;
  2926. }
  2927. attr = mtn_get_attr(node_dn, "nsslapd-referral");
  2928. if (attr)
  2929. {
  2930. /* if there are some referrals set in the entry build a list
  2931. * to be returned to the caller
  2932. */
  2933. slapi_attr_get_numvalues(attr, &nb);
  2934. referral = (char**) slapi_ch_malloc(sizeof(char*) * (nb+1));
  2935. hint = slapi_attr_first_value(attr, &val);
  2936. i = 0;
  2937. while (val)
  2938. {
  2939. referral[i++] = slapi_ch_strdup(slapi_value_get_string(val));
  2940. hint = slapi_attr_next_value(attr, hint, &val);
  2941. }
  2942. referral[i] = NULL;
  2943. slapi_attr_free(&attr);
  2944. }
  2945. slapi_ch_free_string(&node_dn);
  2946. return referral;
  2947. }
  2948. /*
  2949. * Get the state of a mapping tree node entry
  2950. * notes :
  2951. * - sdn argument is the dn of the subtree of the DIT managed by this node
  2952. * not the dn of the mapping tree entry
  2953. * - the state is return in a newly allocated string that must be freed by
  2954. * the caller
  2955. */
  2956. char *
  2957. slapi_mtn_get_state(const Slapi_DN *sdn)
  2958. {
  2959. char * node_dn;
  2960. Slapi_Attr *attr;
  2961. char * state = NULL;
  2962. Slapi_Value *val = NULL;
  2963. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  2964. if(!node_dn){
  2965. /* shutdown has been detected */
  2966. return NULL;
  2967. }
  2968. attr = mtn_get_attr(node_dn, "nsslapd-state");
  2969. if (attr)
  2970. {
  2971. /* entry state was found */
  2972. slapi_attr_first_value(attr, &val);
  2973. state = slapi_ch_strdup(slapi_value_get_string(val));
  2974. slapi_attr_free(&attr);
  2975. }
  2976. slapi_ch_free_string(&node_dn);
  2977. return state;
  2978. }
  2979. static void
  2980. mtn_internal_be_set_state(Slapi_Backend *be, int state)
  2981. {
  2982. mapping_tree_node * node;
  2983. char * be_name;
  2984. int i;
  2985. int change_callback = 0;
  2986. int old_state;
  2987. mtn_wlock();
  2988. be_name = slapi_ch_strdup(slapi_be_get_name(be));
  2989. node = get_mapping_tree_node_by_name(mapping_tree_root, be_name);
  2990. if (node == NULL)
  2991. {
  2992. LDAPDebug(LDAP_DEBUG_TRACE,
  2993. "Warning: backend %s is not declared in mapping tree\n",
  2994. be_name, 0 ,0);
  2995. goto done;
  2996. }
  2997. /* now search the backend in this node */
  2998. i = 0;
  2999. while ( (i < node->mtn_be_count) &&
  3000. (node->mtn_backend_names) &&
  3001. (node->mtn_backend_names[i]) &&
  3002. (strcmp(node->mtn_backend_names[i],be_name)))
  3003. {
  3004. i++;
  3005. }
  3006. if ( (i >= node->mtn_be_count) || (node->mtn_backend_names == NULL) ||
  3007. (node->mtn_backend_names[i] == NULL) )
  3008. {
  3009. /* backend is not declared in the mapping tree node
  3010. * print out a warning
  3011. */
  3012. LDAPDebug(LDAP_DEBUG_TRACE,
  3013. "Warning: backend %s is not declared in mapping node entry\n",
  3014. be_name, 0 ,0);
  3015. goto done;
  3016. }
  3017. change_callback = 1;
  3018. old_state = node->mtn_be_states[i];
  3019. /* OK we found the backend at last, now do the real job: set the state */
  3020. switch (state)
  3021. {
  3022. case SLAPI_BE_STATE_OFFLINE:
  3023. node->mtn_be[i] = be;
  3024. node->mtn_be_states[i] = SLAPI_BE_STATE_OFFLINE;
  3025. break;
  3026. case SLAPI_BE_STATE_ON:
  3027. node->mtn_be[i] = be;
  3028. node->mtn_be_states[i] = SLAPI_BE_STATE_ON;
  3029. break;
  3030. case SLAPI_BE_STATE_DELETE:
  3031. node->mtn_be[i] = NULL;
  3032. node->mtn_be_states[i] = SLAPI_BE_STATE_DELETE;
  3033. break;
  3034. }
  3035. done:
  3036. mtn_unlock();
  3037. if (change_callback)
  3038. mtn_be_state_change(be_name, old_state, state);
  3039. slapi_ch_free( (void **) &be_name);
  3040. }
  3041. /*
  3042. * This procedure must be called by previously stopped backends
  3043. * to signal that they have started and are ready to process requests
  3044. * The backend must be fully ready to handle requests before calling this
  3045. * procedure
  3046. * At startup tiem it is not mandatory for the backends to
  3047. * call this procedure: backends are assumed on by default
  3048. */
  3049. void
  3050. slapi_mtn_be_started(Slapi_Backend *be)
  3051. {
  3052. /* Find the node where this backend stay
  3053. * then update the backend structure
  3054. * In the long term, the backend should have only one suffix and
  3055. * stay in only one node as for now, check all suffixes
  3056. * Rq : since mapping tree is initiatized very soon in the server
  3057. * startup, we can be sure at that time that the mapping
  3058. * tree is initialized
  3059. */
  3060. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3061. }
  3062. /* these procedure can be called when backends need to be put in maintenance mode
  3063. * after call to slapi_mtn_be_disable, the backend will still be known
  3064. * by a server but the mapping tree won't route requests to it anymore
  3065. * The slapi_mtn_be_enable function enable to route requests to the backend
  3066. * again
  3067. * the slapi_mtn_be_disable function only returns when there is no more
  3068. * request in progress in the backend
  3069. */
  3070. void
  3071. slapi_mtn_be_disable(Slapi_Backend *be)
  3072. {
  3073. mtn_internal_be_set_state(be, SLAPI_BE_STATE_OFFLINE);
  3074. /* the two following lines can seem weird, but they allow to check that no
  3075. * LDAP operation is in progress on the backend
  3076. */
  3077. slapi_be_Wlock(be);
  3078. slapi_be_Unlock(be);
  3079. }
  3080. void
  3081. slapi_mtn_be_enable(Slapi_Backend *be)
  3082. {
  3083. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3084. }
  3085. /*
  3086. * This procedure must be called by backends before stopping
  3087. * if some operations are in progress when this procedure
  3088. * is called, this procedure will block until completion
  3089. * of these operations
  3090. * The backend must wait return from this procedure before stopping operation
  3091. * Backends must serve operation until the return from this procedure.
  3092. * Once this procedure return they will not be issued request anymore
  3093. * and they have been removed from the server list of backends
  3094. * It is also the bakend responsability to free the Slapi_Backend structures
  3095. * that was given by slapi_be_new at startup time.
  3096. * Should the backend start again, it would need to issue slapi_be_new again
  3097. */
  3098. void
  3099. slapi_mtn_be_stopping(Slapi_Backend *be)
  3100. {
  3101. mtn_internal_be_set_state(be, SLAPI_BE_STATE_DELETE);
  3102. /* the two following lines can seem weird, but they allow to check that no
  3103. * LDAP operation is in progress on the backend
  3104. */
  3105. slapi_be_Wlock(be);
  3106. slapi_be_Unlock(be);
  3107. slapi_be_stopping(be);
  3108. }
  3109. /*
  3110. * Switch a backend into read-only mode, or back to read-write mode.
  3111. * To switch to read-only mode, we need to wait for all pending operations
  3112. * to finish.
  3113. */
  3114. void
  3115. slapi_mtn_be_set_readonly(Slapi_Backend *be, int readonly)
  3116. {
  3117. if (readonly) {
  3118. slapi_be_Wlock(be);
  3119. slapi_be_set_readonly(be, 1);
  3120. slapi_be_Unlock(be);
  3121. } else {
  3122. slapi_be_set_readonly(be, 0);
  3123. }
  3124. }
  3125. #ifdef DEBUG
  3126. static int lock_count = 0;
  3127. #endif
  3128. void mtn_wlock()
  3129. {
  3130. PR_RWLock_Wlock(myLock);
  3131. #ifdef DEBUG
  3132. lock_count--;
  3133. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_wlock : lock count : %d\n", lock_count, 0, 0);
  3134. #endif
  3135. }
  3136. void mtn_lock()
  3137. {
  3138. PR_RWLock_Rlock(myLock);
  3139. #ifdef DEBUG
  3140. lock_count++;
  3141. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_lock : lock count : %d\n", lock_count, 0, 0);
  3142. #endif
  3143. }
  3144. void mtn_unlock()
  3145. {
  3146. #ifdef DEBUG
  3147. if (lock_count > 0)
  3148. lock_count--;
  3149. else if (lock_count < 0)
  3150. lock_count++;
  3151. else
  3152. lock_count = (int) 11111111; /* this happening means problems */
  3153. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_unlock : lock count : %d\n", lock_count, 0, 0);
  3154. #endif
  3155. PR_RWLock_Unlock(myLock);
  3156. }
  3157. #ifdef TEST_FOR_REGISTER_CHANGE
  3158. void my_test_fnct1(void *handle, char *be_name, int old_state, int new_state)
  3159. {
  3160. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3161. "my_test_fnct1 : handle %d, be %s, old state %d, new state %d\n",
  3162. handle,be_name, old_state, new_state);
  3163. if (old_state == 2)
  3164. slapi_unregister_backend_state_change(handle);
  3165. }
  3166. void my_test_fnct2(void *handle, char *be_name, int old_state, int new_state)
  3167. {
  3168. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3169. "my_test_fnct2 : handle %d, be %s, old state %d, new state %d\n",
  3170. handle, be_name, old_state, new_state);
  3171. }
  3172. void test_register()
  3173. {
  3174. slapi_register_backend_state_change((void *) 1234, my_test_fnct1);
  3175. slapi_register_backend_state_change((void *) 4321, my_test_fnct2);
  3176. }
  3177. #endif
  3178. #ifdef DEBUG
  3179. static void dump_mapping_tree(mapping_tree_node *parent, int depth)
  3180. {
  3181. mapping_tree_node *current = NULL;
  3182. static char dump_indent[256];
  3183. int i;
  3184. if (depth == 0)
  3185. {
  3186. LDAPDebug(LDAP_DEBUG_ANY, "dump_mapping_tree\n", 0, 0, 0);
  3187. }
  3188. dump_indent[0] = '\0';
  3189. for (i = 0; i < depth; i++)
  3190. PL_strcatn(dump_indent, sizeof(dump_indent), " ");
  3191. for (current = parent->mtn_children; current;
  3192. current = current->mtn_brother)
  3193. {
  3194. if (strlen(current->mtn_subtree->dn) == 0)
  3195. {
  3196. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3197. dump_indent, "none", current);
  3198. }
  3199. else
  3200. {
  3201. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3202. dump_indent, current->mtn_subtree->dn, current);
  3203. }
  3204. dump_mapping_tree(current, depth+1);
  3205. }
  3206. return;
  3207. }
  3208. #endif