mapping_tree.c 128 KB

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