mapping_tree.c 123 KB

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