mapping_tree.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896
  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 != NULL) && (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. int fixup = 0;
  1935. if(mapping_tree_freed){
  1936. /* shutdown detected */
  1937. return LDAP_OPERATIONS_ERROR;
  1938. }
  1939. if (errorbuf) {
  1940. errorbuf[0] = '\0';
  1941. }
  1942. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1943. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  1944. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  1945. /* Get the target for this op */
  1946. target_sdn = operation_get_target_spec (op);
  1947. fixup = operation_is_flag_set(op, OP_FLAG_TOMBSTONE_FIXUP);
  1948. if(!mapping_tree_inited) {
  1949. mapping_tree_init();
  1950. }
  1951. be[0] = NULL;
  1952. referral[0] = NULL;
  1953. mtn_lock();
  1954. /* Get the mapping tree node that is the best match for the target dn. */
  1955. target_node = slapi_get_mapping_tree_node_by_dn(target_sdn);
  1956. if (target_node == NULL)
  1957. target_node = mapping_tree_root;
  1958. /* The processing of the base scope root DSE search and all other LDAP operations on ""
  1959. * will be transferred to the internal DSE backend
  1960. */
  1961. if( sdn_is_nulldn(target_sdn) &&
  1962. (((op_type == SLAPI_OPERATION_SEARCH) && (scope == LDAP_SCOPE_BASE)) ||
  1963. (op_type != SLAPI_OPERATION_SEARCH)) ) {
  1964. mtn_unlock();
  1965. *be = slapi_be_select_by_instance_name(DSE_BACKEND);
  1966. if(*be != NULL && !be_isdeleted(*be))
  1967. {
  1968. ret = LDAP_SUCCESS;
  1969. slapi_be_Rlock(*be); /* also done inside mtn_get_be() below */
  1970. } else {
  1971. ret = LDAP_OPERATIONS_ERROR;
  1972. }
  1973. return ret;
  1974. }
  1975. /* index == -1 is used to specify that we want only one backend not a list
  1976. * used for BASE search, ADD, DELETE, MODIFY
  1977. */
  1978. index = -1;
  1979. ret = mtn_get_be(target_node, pb, be, &index, referral, errorbuf);
  1980. slapi_pblock_set(pb, SLAPI_BACKEND_COUNT, &index);
  1981. mtn_unlock();
  1982. /* if a backend was returned, make sure that all non-search operations
  1983. * fail if the backend is read-only,
  1984. * or if the whole server is readonly AND backend is public (!private)
  1985. */
  1986. if ((ret == LDAP_SUCCESS) && *be && !be_isdeleted(*be) &&
  1987. (((*be)->be_readonly && !fixup) ||
  1988. ((slapi_config_get_readonly() && !fixup) &&
  1989. !slapi_be_private(*be))) )
  1990. {
  1991. unsigned long op_type = operation_get_type(op);
  1992. if ((op_type != SLAPI_OPERATION_SEARCH) &&
  1993. (op_type != SLAPI_OPERATION_COMPARE) &&
  1994. (op_type != SLAPI_OPERATION_BIND) &&
  1995. (op_type != SLAPI_OPERATION_UNBIND))
  1996. {
  1997. ret = LDAP_UNWILLING_TO_PERFORM;
  1998. PL_strncpyz(errorbuf, slapi_config_get_readonly() ?
  1999. "Server is read-only" :
  2000. "database is read-only", BUFSIZ);
  2001. slapi_be_Unlock(*be);
  2002. *be = NULL;
  2003. }
  2004. }
  2005. return ret;
  2006. }
  2007. int slapi_mapping_tree_select_all(Slapi_PBlock *pb, Slapi_Backend **be_list,
  2008. Slapi_Entry **referral_list, char *errorbuf)
  2009. {
  2010. Slapi_DN *target_sdn = NULL;
  2011. mapping_tree_node *node_list;
  2012. mapping_tree_node *node;
  2013. Slapi_Operation *op;
  2014. int index;
  2015. int ret;
  2016. int ret_code = LDAP_SUCCESS;
  2017. int be_index = 0 ;
  2018. int referral_index = 0 ;
  2019. Slapi_Backend * be;
  2020. Slapi_Entry * referral;
  2021. int scope = LDAP_SCOPE_BASE;
  2022. Slapi_DN *sdn = NULL;
  2023. int flag_partial_result = 0;
  2024. int op_type;
  2025. if(mapping_tree_freed){
  2026. return LDAP_OPERATIONS_ERROR;
  2027. }
  2028. if (errorbuf) {
  2029. errorbuf[0] = '\0';
  2030. }
  2031. /* get the operational parameters */
  2032. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &sdn);
  2033. if (NULL == sdn) {
  2034. slapi_log_error(SLAPI_LOG_FATAL, NULL, "Error: Null target DN");
  2035. return LDAP_OPERATIONS_ERROR;
  2036. }
  2037. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2038. target_sdn = operation_get_target_spec (op);
  2039. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  2040. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  2041. if(!mapping_tree_inited){
  2042. mapping_tree_init();
  2043. }
  2044. mtn_lock();
  2045. be_list[0] = NULL;
  2046. referral_list[0] = NULL;
  2047. /* Get the mapping tree node that is the best match for the target dn. */
  2048. node_list = slapi_get_mapping_tree_node_by_dn(target_sdn);
  2049. if (node_list == NULL)
  2050. node_list = mapping_tree_root;
  2051. if( sdn_is_nulldn(target_sdn) && ( op_type == SLAPI_OPERATION_SEARCH)
  2052. && (scope == LDAP_SCOPE_BASE) ) {
  2053. mtn_unlock();
  2054. be = slapi_be_select_by_instance_name(DSE_BACKEND);
  2055. if(be != NULL && !be_isdeleted(be))
  2056. {
  2057. be_list[0]=be;
  2058. be_list[1] = NULL;
  2059. ret_code = LDAP_SUCCESS;
  2060. slapi_be_Rlock(be); /* also done inside mtn_get_be() below */
  2061. } else {
  2062. ret_code = LDAP_OPERATIONS_ERROR;
  2063. }
  2064. return ret_code;
  2065. }
  2066. ret = slapi_mtn_get_first_be(node_list, &node, pb, &be, &index, &referral, errorbuf, scope);
  2067. while ((node) && (be_index <= BE_LIST_SIZE))
  2068. {
  2069. if (ret != LDAP_SUCCESS)
  2070. {
  2071. /* flag we have problems at least on part of the tree */
  2072. flag_partial_result = 1;
  2073. }
  2074. else if ( ( ((!slapi_sdn_issuffix(sdn, slapi_mtn_get_dn(node))
  2075. && !slapi_sdn_issuffix(slapi_mtn_get_dn(node), sdn)))
  2076. || ((node_list == mapping_tree_root) && node->mtn_private
  2077. && (scope != LDAP_SCOPE_BASE)) )
  2078. && (!be || strncmp(be->be_name, "default", 8)))
  2079. {
  2080. if (be && !be_isdeleted(be))
  2081. {
  2082. /* wrong backend or referall, ignore it */
  2083. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2084. "mapping tree release backend : %s\n",
  2085. slapi_be_get_name(be));
  2086. slapi_be_Unlock(be);
  2087. }
  2088. }
  2089. else
  2090. {
  2091. if (be && !be_isdeleted(be))
  2092. {
  2093. if (be_index == BE_LIST_SIZE) { /* error - too many backends */
  2094. ret_code = LDAP_ADMINLIMIT_EXCEEDED;
  2095. PR_snprintf(errorbuf, BUFSIZ-1,
  2096. "Error: too many backends match search request - cannot proceed");
  2097. slapi_log_error(SLAPI_LOG_FATAL, NULL, "%s\n", errorbuf);
  2098. break;
  2099. } else {
  2100. be_list[be_index++]=be;
  2101. }
  2102. }
  2103. if (referral)
  2104. {
  2105. referral_list[referral_index++] = referral;
  2106. /* if we hit a referral at the base of the search
  2107. * we must return a REFERRAL error with only this referral
  2108. * all backend or referral below this node are ignored
  2109. */
  2110. if (slapi_sdn_issuffix(target_sdn, slapi_mtn_get_dn(node)))
  2111. {
  2112. ret_code = LDAP_REFERRAL;
  2113. break; /* get out of the while loop */
  2114. }
  2115. }
  2116. }
  2117. ret = slapi_mtn_get_next_be(node_list, &node, pb, &be, &index,
  2118. &referral, errorbuf, scope);
  2119. }
  2120. mtn_unlock();
  2121. be_list[be_index] = NULL;
  2122. referral_list[referral_index] = NULL;
  2123. if (flag_partial_result)
  2124. {
  2125. /* if no node in active has been found -> return LDAP_OPERATIONS_ERROR
  2126. * but if only part of the nodes are disabled
  2127. * do not return an error to allow directory browser to work OK
  2128. * in the console
  2129. * It would be better to return a meaningfull error
  2130. * unfortunately LDAP_PARTIAL_RESULTS is not usable because
  2131. * it is already used for V2 referrals
  2132. * leave no error for now and fix this later
  2133. */
  2134. if ((be_index == 0) && (referral_index == 0))
  2135. return LDAP_OPERATIONS_ERROR;
  2136. else
  2137. return ret_code;
  2138. }
  2139. else
  2140. return ret_code;
  2141. }
  2142. void slapi_mapping_tree_free_all(Slapi_Backend **be_list, Slapi_Entry **referral_list)
  2143. {
  2144. int index = 0;
  2145. /* go through the list of all backends that was used for the operation
  2146. * and unlock them
  2147. * go through the list of referrals and free them
  2148. * free the two tables that were used to store the two lists
  2149. */
  2150. if (be_list[index] != NULL)
  2151. {
  2152. Slapi_Backend * be;
  2153. while ((be = be_list[index++]))
  2154. {
  2155. slapi_log_error(SLAPI_LOG_ARGS, NULL, "mapping tree release backend : %s\n", slapi_be_get_name(be));
  2156. slapi_be_Unlock(be);
  2157. }
  2158. }
  2159. index = 0;
  2160. if (referral_list[index] != NULL)
  2161. {
  2162. Slapi_Entry * referral;
  2163. while ((referral = referral_list[index++]))
  2164. {
  2165. slapi_entry_free(referral);
  2166. }
  2167. }
  2168. }
  2169. /* same as slapi_mapping_tree_select() but will also check that the supplied
  2170. * newdn is in the same backend
  2171. */
  2172. int slapi_mapping_tree_select_and_check(Slapi_PBlock *pb,char *newdn, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
  2173. {
  2174. Slapi_DN *target_sdn = NULL;
  2175. Slapi_DN dn_newdn;
  2176. Slapi_Backend * new_be = NULL;
  2177. Slapi_Backend * def_be = defbackend_get_backend();
  2178. Slapi_Entry * new_referral = NULL;
  2179. mapping_tree_node *target_node;
  2180. int index;
  2181. Slapi_Operation *op;
  2182. int ret;
  2183. int need_unlock = 0;
  2184. if(mapping_tree_freed){
  2185. return LDAP_OPERATIONS_ERROR;
  2186. }
  2187. slapi_sdn_init(&dn_newdn);
  2188. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2189. target_sdn = operation_get_target_spec (op);
  2190. * referral = NULL;
  2191. ret = slapi_mapping_tree_select(pb, be, referral, errorbuf);
  2192. if (ret)
  2193. goto unlock_and_return;
  2194. slapi_sdn_init_dn_byref(&dn_newdn,newdn);
  2195. /* acquire lock now, after slapi_mapping_tree_select() which also locks,
  2196. because we are accessing mt internals */
  2197. mtn_lock();
  2198. need_unlock = 1; /* we have now acquired the lock */
  2199. target_node = slapi_get_mapping_tree_node_by_dn(&dn_newdn);
  2200. if (target_node == NULL)
  2201. target_node = mapping_tree_root;
  2202. index = -1;
  2203. ret = mtn_get_be(target_node, pb, &new_be, &index, &new_referral, errorbuf);
  2204. if (ret)
  2205. goto unlock_and_return;
  2206. if (*be)
  2207. {
  2208. /* suffix is a part of mapping tree. We should not free it */
  2209. const Slapi_DN *suffix = slapi_get_suffix_by_dn(target_sdn);
  2210. if ((*be != def_be) && (NULL == suffix))
  2211. {
  2212. ret = LDAP_NO_SUCH_OBJECT;
  2213. PR_snprintf(errorbuf, BUFSIZ,
  2214. "Target entry \"%s\" does not exist\n",
  2215. slapi_sdn_get_dn(target_sdn));
  2216. goto unlock_and_return;
  2217. }
  2218. if (suffix && (0 == slapi_sdn_compare(target_sdn, suffix)))
  2219. {
  2220. /* target_sdn is a suffix */
  2221. const Slapi_DN *new_suffix = NULL;
  2222. /* new_suffix is a part of mapping tree. We should not free it */
  2223. new_suffix = slapi_get_suffix_by_dn(&dn_newdn);
  2224. if (!slapi_be_exist((const Slapi_DN *)&dn_newdn))
  2225. {
  2226. /* new_be is an empty backend */
  2227. ret = LDAP_NO_SUCH_OBJECT;
  2228. PR_snprintf(errorbuf, BUFSIZ,
  2229. "Backend for suffix \"%s\" does not exist\n", newdn);
  2230. goto unlock_and_return;
  2231. }
  2232. if (0 == slapi_sdn_compare(&dn_newdn, new_suffix))
  2233. {
  2234. ret = LDAP_ALREADY_EXISTS;
  2235. PR_snprintf(errorbuf, BUFSIZ,
  2236. "Suffix \"%s\" already exists\n", newdn);
  2237. goto unlock_and_return;
  2238. }
  2239. ret = LDAP_NAMING_VIOLATION;
  2240. PR_snprintf(errorbuf, BUFSIZ, "Cannot rename suffix \"%s\"\n",
  2241. slapi_sdn_get_dn(target_sdn));
  2242. goto unlock_and_return;
  2243. }
  2244. else
  2245. {
  2246. if ((*be != new_be) || mtn_sdn_has_child(target_sdn))
  2247. {
  2248. ret = LDAP_AFFECTS_MULTIPLE_DSAS;
  2249. PR_snprintf(errorbuf, BUFSIZ,
  2250. "Cannot move entries across backends\n");
  2251. goto unlock_and_return;
  2252. }
  2253. }
  2254. }
  2255. unlock_and_return:
  2256. /* if slapi_mapping_tree_select failed, we won't have the lock */
  2257. if (need_unlock) {
  2258. mtn_unlock();
  2259. }
  2260. slapi_sdn_done(&dn_newdn);
  2261. if (new_be)
  2262. slapi_be_Unlock(new_be);
  2263. if (new_referral)
  2264. slapi_entry_free(new_referral);
  2265. if (ret != LDAP_SUCCESS)
  2266. {
  2267. if (be && *be && !be_isdeleted(*be))
  2268. {
  2269. slapi_be_Unlock(*be);
  2270. *be = NULL;
  2271. }
  2272. if (*referral)
  2273. {
  2274. slapi_entry_free(*referral);
  2275. *referral = NULL;
  2276. }
  2277. }
  2278. return ret;
  2279. }
  2280. /*
  2281. * allow to solve the distribution problem when several back-ends are defined
  2282. */
  2283. static int
  2284. mtn_get_be_distributed(Slapi_PBlock *pb, mapping_tree_node * target_node,
  2285. Slapi_DN *target_sdn, int * flag_stop)
  2286. {
  2287. int index;
  2288. *flag_stop = 0;
  2289. if (target_node->mtn_dstr_plg)
  2290. {
  2291. index = (*target_node->mtn_dstr_plg)(pb, target_sdn,
  2292. target_node->mtn_backend_names, target_node->mtn_be_count,
  2293. target_node->mtn_subtree, target_node->mtn_be_states, target_node->mtn_dstr_plg_rootmode);
  2294. if (index == SLAPI_BE_ALL_BACKENDS)
  2295. {
  2296. /* special value to indicate all backends must be scanned
  2297. * start with first one
  2298. */
  2299. index = 0;
  2300. }
  2301. /* check if distribution plugi returned a special mode for
  2302. * updates as root */
  2303. else if (index == SLAPI_BE_NO_BACKEND || index == SLAPI_BE_REMOTE_BACKEND)
  2304. {
  2305. /* nothing special to do */
  2306. }
  2307. /* paranoid check, never trust another programmer */
  2308. else if ((index >= target_node->mtn_be_count) || (index < 0))
  2309. {
  2310. LDAPDebug(LDAP_DEBUG_ANY,
  2311. "Warning: distribution plugin returned wrong backend"
  2312. " : %d for entry %s at node %s\n",
  2313. index, slapi_sdn_get_ndn(target_sdn),
  2314. slapi_sdn_get_ndn(target_node->mtn_subtree));
  2315. index = 0;
  2316. }
  2317. else
  2318. {
  2319. /* only one backend to scan
  2320. * set flag_stop to indicate we must stop the search here
  2321. */
  2322. *flag_stop = 1;
  2323. }
  2324. }
  2325. else
  2326. {
  2327. /* there is several backends but no distribution function
  2328. * return the first backend
  2329. */
  2330. LDAPDebug(LDAP_DEBUG_ANY,
  2331. "Warning: distribution plugin not configured at node : %s\n",
  2332. slapi_sdn_get_ndn(target_node->mtn_subtree), 0, 0);
  2333. index = 0;
  2334. }
  2335. return index;
  2336. }
  2337. /*
  2338. * this function is in charge of choosing the right backend for a given
  2339. * mapping tree node
  2340. * In case when several backends are used it is in charge of the spanning the
  2341. * request among all the backend or choosing the only backend to use depending
  2342. * on the type and scope of the LDAP operation
  2343. *
  2344. * index == -1 is used to specify that we want only the one best backend
  2345. * used for BASE search, ADD, DELETE, MODIFY
  2346. * index >0 means we are doing a SUBTREE or ONELEVEL search and that the be in
  2347. * that position must be returned
  2348. */
  2349. static int mtn_get_be(mapping_tree_node *target_node, Slapi_PBlock *pb,
  2350. Slapi_Backend **be, int * index, Slapi_Entry **referral, char *errorbuf)
  2351. {
  2352. Slapi_DN *target_sdn;
  2353. Slapi_Operation *op;
  2354. int result = LDAP_SUCCESS;
  2355. int override_referral = 0;
  2356. unsigned long op_type;
  2357. int flag_stop = 0;
  2358. struct slapi_componentid *cid = NULL;
  2359. if(mapping_tree_freed){
  2360. /* shut down detected */
  2361. return LDAP_OPERATIONS_ERROR;
  2362. }
  2363. /* Get usefull stuff like the type of operation, target dn */
  2364. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2365. op_type = operation_get_type(op);
  2366. target_sdn = operation_get_target_spec (op);
  2367. if (target_node->mtn_state == MTN_DISABLED) {
  2368. if (errorbuf) {
  2369. PR_snprintf(errorbuf, BUFSIZ,
  2370. "Warning: Operation attempted on a disabled node : %s\n",
  2371. slapi_sdn_get_dn(target_node->mtn_subtree));
  2372. }
  2373. result = LDAP_OPERATIONS_ERROR;
  2374. return result;
  2375. }
  2376. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &cid);
  2377. override_referral =
  2378. ((cid != NULL) && (pw_get_componentID() != NULL) && (pw_get_componentID() == cid)) ||
  2379. operation_is_flag_set(op, OP_FLAG_LEGACY_REPLICATION_DN) || /* 4.0 lgacy update */
  2380. operation_is_flag_set(op, OP_FLAG_REPLICATED) || /* 5.0 replication update */
  2381. operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY) || /* 5.1 fix to enable tombstone delete on a R-O consumer */
  2382. operation_is_flag_set(op, SLAPI_OP_FLAG_BYPASS_REFERRALS); /* 6.1 fix to allow internal updates from plugins on R-O consumer */
  2383. if ((target_node->mtn_state == MTN_BACKEND) ||
  2384. (target_node->mtn_state == MTN_CONTAINER ) ||
  2385. ((target_node->mtn_state == MTN_REFERRAL_ON_UPDATE) &&
  2386. ((SLAPI_OPERATION_SEARCH == op_type)||(SLAPI_OPERATION_BIND == op_type) ||
  2387. (SLAPI_OPERATION_UNBIND == op_type) || (SLAPI_OPERATION_COMPARE == op_type))) ||
  2388. override_referral) {
  2389. *referral = NULL;
  2390. if ((target_node == mapping_tree_root) ){
  2391. /* If we got here, then we couldn't find a matching node
  2392. * for the target. We'll use the default backend. Once
  2393. * we fully support the NULL suffix, we should do something more
  2394. * clever here.
  2395. */
  2396. *be = defbackend_get_backend();
  2397. } else {
  2398. if ((*index == -1) || (*index == 0)) {
  2399. /* In this case, we are doing
  2400. * a READ, ADD, MODIDY or DELETE on a single entry
  2401. * or we are starting a SEARCH
  2402. * if there is several possible backend we want to apply
  2403. * the distribution plugin
  2404. */
  2405. if (target_node->mtn_be_count <= 1) {
  2406. /* there is only one backend no choice possible */
  2407. *index = 0;
  2408. } else {
  2409. *index = mtn_get_be_distributed(pb, target_node,
  2410. target_sdn, &flag_stop);
  2411. if (*index == SLAPI_BE_NO_BACKEND)
  2412. result = LDAP_UNWILLING_TO_PERFORM;
  2413. }
  2414. }
  2415. if (*index == SLAPI_BE_REMOTE_BACKEND) {
  2416. *be = NULL;
  2417. *referral = (target_node->mtn_referral_entry ?
  2418. slapi_entry_dup(target_node->mtn_referral_entry) :
  2419. NULL);
  2420. (*index)++;
  2421. }else if ((*index == SLAPI_BE_NO_BACKEND) || (*index >= target_node->mtn_be_count)) {
  2422. /* we have already returned all backends -> return NULL */
  2423. *be = NULL;
  2424. *referral = NULL;
  2425. } else {
  2426. /* return next backend, increment index */
  2427. *be = target_node->mtn_be[*index];
  2428. if(*be==NULL) {
  2429. if (NULL != target_node->mtn_be_states &&
  2430. target_node->mtn_be_states[*index] == SLAPI_BE_STATE_DELETE) {
  2431. /* This MTN is being deleted */
  2432. *be = defbackend_get_backend();
  2433. } else {
  2434. /* This MTN has not been linked to its backend
  2435. * instance yet. */
  2436. target_node->mtn_be[*index] =
  2437. slapi_be_select_by_instance_name(
  2438. target_node->mtn_backend_names[*index]);
  2439. *be = target_node->mtn_be[*index];
  2440. if(*be==NULL) {
  2441. LDAPDebug(LDAP_DEBUG_BACKLDBM,
  2442. "Warning: Mapping tree node entry for %s "
  2443. "point to an unknown backend : %s\n",
  2444. slapi_sdn_get_dn(target_node->mtn_subtree),
  2445. target_node->mtn_backend_names[*index], 0);
  2446. /* Well there's still not backend instance for
  2447. * this MTN, so let's have the default backend
  2448. * deal with this.
  2449. */
  2450. *be = defbackend_get_backend();
  2451. }
  2452. }
  2453. }
  2454. if ((target_node->mtn_be_states) &&
  2455. (target_node->mtn_be_states[*index] == SLAPI_BE_STATE_OFFLINE)) {
  2456. LDAPDebug(LDAP_DEBUG_TRACE,
  2457. "Warning: Operation attempted on backend in OFFLINE "
  2458. "state : %s\n",
  2459. target_node->mtn_backend_names[*index], 0, 0);
  2460. result = LDAP_OPERATIONS_ERROR;
  2461. *be = defbackend_get_backend();
  2462. }
  2463. if (flag_stop)
  2464. *index = SLAPI_BE_NO_BACKEND;
  2465. else
  2466. (*index)++;
  2467. }
  2468. }
  2469. } else {
  2470. /* otherwise we must return the referral
  2471. * if ((target_node->mtn_state == MTN_REFERRAL) ||
  2472. * (target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) */
  2473. if (*index > 0) {
  2474. /* we have already returned this referral
  2475. * send back NULL to jump to next node
  2476. */
  2477. *be = NULL;
  2478. *referral = NULL;
  2479. result = LDAP_SUCCESS;
  2480. } else {
  2481. /* first time we hit this referral -> return it
  2482. * set the be variable to NULL to indicate we use a referral
  2483. * and increment index to rememeber later that we already
  2484. * returned this referral
  2485. */
  2486. *be = NULL;
  2487. *referral = (target_node->mtn_referral_entry ?
  2488. slapi_entry_dup(target_node->mtn_referral_entry) :
  2489. NULL);
  2490. (*index)++;
  2491. if (NULL == *referral) {
  2492. if (errorbuf) {
  2493. PR_snprintf(errorbuf, BUFSIZ,
  2494. "Mapping tree node for %s is set to return a referral,"
  2495. " but no referral is configured for it",
  2496. slapi_sdn_get_ndn(target_node->mtn_subtree));
  2497. }
  2498. result = LDAP_OPERATIONS_ERROR;
  2499. } else {
  2500. result = LDAP_SUCCESS;
  2501. }
  2502. }
  2503. }
  2504. if (result == LDAP_SUCCESS) {
  2505. if (*be && !be_isdeleted(*be)) {
  2506. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2507. "mapping tree selected backend : %s\n",
  2508. slapi_be_get_name(*be));
  2509. slapi_be_Rlock(*be);
  2510. } else if (*referral) {
  2511. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  2512. "mapping tree selected referral at node : %s\n",
  2513. slapi_sdn_get_dn(target_node->mtn_subtree));
  2514. }
  2515. }
  2516. return result;
  2517. }
  2518. /*
  2519. * Description:
  2520. * Finds the best match for the targetdn from the children of parent. Uses
  2521. * slapi_sdn_issuffix and the number of rdns to pick the best node.
  2522. *
  2523. * Arguments:
  2524. * parent is a pointer to a mapping tree node.
  2525. * targetdn is the dn we're trying to find the best match for.
  2526. *
  2527. * Returns:
  2528. * A pointer to the child of parent that best matches the targetdn. NULL
  2529. * if there were no good matches.
  2530. */
  2531. static mapping_tree_node *best_matching_child(mapping_tree_node *parent,
  2532. const Slapi_DN *targetdn)
  2533. {
  2534. mapping_tree_node *highest_match_node = NULL;
  2535. mapping_tree_node *current;
  2536. if(mapping_tree_freed){
  2537. /* shutdown detected */
  2538. return NULL;
  2539. }
  2540. for (current = parent->mtn_children; current;
  2541. current = current->mtn_brother) {
  2542. if (slapi_sdn_issuffix(targetdn, current->mtn_subtree)) {
  2543. if ( (highest_match_node == NULL) ||
  2544. ((slapi_sdn_get_ndn_len(current->mtn_subtree)) >
  2545. slapi_sdn_get_ndn_len(highest_match_node->mtn_subtree)) ) {
  2546. highest_match_node = current;
  2547. }
  2548. }
  2549. }
  2550. return highest_match_node;
  2551. }
  2552. /*
  2553. * look for the exact mapping tree node corresponding to a given entry dn
  2554. */
  2555. static mapping_tree_node *
  2556. mtn_get_mapping_tree_node_by_entry(mapping_tree_node* node, const Slapi_DN *dn)
  2557. {
  2558. mapping_tree_node *found_node = NULL;
  2559. if(mapping_tree_freed){
  2560. /* shutdown detected */
  2561. return NULL;
  2562. }
  2563. if(NULL == dn){
  2564. /* bad mapping tree entry operation */
  2565. return NULL;
  2566. }
  2567. if (slapi_sdn_compare(node->mtn_subtree, dn) == 0)
  2568. {
  2569. return node;
  2570. }
  2571. if (node->mtn_children)
  2572. {
  2573. found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_children, dn);
  2574. if (found_node)
  2575. return found_node;
  2576. }
  2577. if (node->mtn_brother)
  2578. {
  2579. found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_brother, dn);
  2580. }
  2581. return found_node;
  2582. }
  2583. /*
  2584. * Description:
  2585. * Gets a mapping tree node that best matches the given dn. If the root
  2586. * node is returned and the target dn is not "", then no match was found.
  2587. *
  2588. * Arguments:
  2589. * dn is the target of the search.
  2590. *
  2591. * Returns:
  2592. * The best matching node for the dn
  2593. * if nothing match, NULL is returned
  2594. */
  2595. mapping_tree_node *
  2596. slapi_get_mapping_tree_node_by_dn(const Slapi_DN *dn)
  2597. {
  2598. mapping_tree_node *current_best_match = mapping_tree_root;
  2599. mapping_tree_node *next_best_match = mapping_tree_root;
  2600. if(mapping_tree_freed){
  2601. /* shutdown detected */
  2602. return NULL;
  2603. }
  2604. /* Handle special case where the dn is "" and the mapping root
  2605. * does not belong to the frontend-internal (DSE_BACKEND);
  2606. * it has been assigned to a different backend.
  2607. * e.g: a container backend
  2608. */
  2609. if ( sdn_is_nulldn(dn) && mapping_tree_root && mapping_tree_root->mtn_be[0] &&
  2610. mapping_tree_root->mtn_be[0] != slapi_be_select_by_instance_name(DSE_BACKEND)) {
  2611. return( mapping_tree_root );
  2612. }
  2613. /* Start at the root and walk down the tree to find the best match. */
  2614. while (next_best_match) {
  2615. current_best_match = next_best_match;
  2616. next_best_match = best_matching_child(current_best_match, dn);
  2617. }
  2618. if (current_best_match == mapping_tree_root)
  2619. return NULL;
  2620. else
  2621. return current_best_match;
  2622. }
  2623. static mapping_tree_node *
  2624. get_mapping_tree_node_by_name(mapping_tree_node * node, char * be_name)
  2625. {
  2626. int i;
  2627. mapping_tree_node *found_node = NULL;
  2628. if(mapping_tree_freed){
  2629. /* shutdown detected */
  2630. return NULL;
  2631. }
  2632. /* now search the backend in this node */
  2633. i = 0;
  2634. while ( ( i < node->mtn_be_count) &&
  2635. (node->mtn_backend_names) &&
  2636. (node->mtn_backend_names[i]) &&
  2637. (strcmp(node->mtn_backend_names[i],be_name)))
  2638. {
  2639. i++;
  2640. }
  2641. if ((i < node->mtn_be_count) &&
  2642. (node->mtn_backend_names != NULL) &&
  2643. (node->mtn_backend_names[i] != NULL))
  2644. {
  2645. return node;
  2646. }
  2647. if (node->mtn_children)
  2648. {
  2649. found_node = get_mapping_tree_node_by_name(node->mtn_children, be_name);
  2650. if (found_node)
  2651. return found_node;
  2652. }
  2653. if (node->mtn_brother)
  2654. {
  2655. found_node = get_mapping_tree_node_by_name(node->mtn_brother, be_name);
  2656. }
  2657. return found_node;
  2658. }
  2659. /*
  2660. * Description: construct the dn of the configuration entry for the
  2661. * node originated at the root. The function just constructs
  2662. * the dn it does not verify that the entry actually exist.
  2663. * The format of the dn is
  2664. * cn="<normalized root>",cn=mapping tree,cn=config
  2665. *
  2666. * Arguments: root - root of the node
  2667. *
  2668. * Returns: dn of the configuration entry if successful and null otherwise.
  2669. */
  2670. char*
  2671. slapi_get_mapping_tree_node_configdn (const Slapi_DN *root)
  2672. {
  2673. char *dn = NULL;
  2674. if(mapping_tree_freed){
  2675. /* shutdown detected */
  2676. return NULL;
  2677. }
  2678. if (root == NULL)
  2679. return NULL;
  2680. /* This function converts the old DN style to the new one. */
  2681. dn = slapi_create_dn_string("cn=\"%s\",%s",
  2682. slapi_sdn_get_udn(root), MAPPING_TREE_BASE_DN);
  2683. if (NULL == dn) {
  2684. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  2685. "slapi_get_mapping_tree_node_configdn: "
  2686. "failed to crate mapping tree dn for %s\n",
  2687. slapi_sdn_get_dn(root));
  2688. return NULL;
  2689. }
  2690. return dn;
  2691. }
  2692. Slapi_DN *
  2693. slapi_get_mapping_tree_node_configsdn (const Slapi_DN *root)
  2694. {
  2695. char *dn = NULL;
  2696. Slapi_DN *sdn = NULL;
  2697. if(mapping_tree_freed){
  2698. /* shutdown detected */
  2699. return NULL;
  2700. }
  2701. if (root == NULL)
  2702. return NULL;
  2703. /* This function converts the old DN style to the new one. */
  2704. dn = slapi_create_dn_string("cn=\"%s\",%s",
  2705. slapi_sdn_get_udn(root), MAPPING_TREE_BASE_DN);
  2706. if (NULL == dn) {
  2707. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  2708. "slapi_get_mapping_tree_node_configsdn: "
  2709. "failed to crate mapping tree dn for %s\n",
  2710. slapi_sdn_get_dn(root));
  2711. return NULL;
  2712. }
  2713. sdn = slapi_sdn_new_normdn_passin(dn);
  2714. return sdn;
  2715. }
  2716. /*
  2717. * Description: this function returns root of the subtree to which the node applies
  2718. *
  2719. * Arguments: node - mapping tree node
  2720. *
  2721. * Returns: root of the subtree if function is successful and NULL otherwise.
  2722. */
  2723. const Slapi_DN* slapi_get_mapping_tree_node_root (const mapping_tree_node *node)
  2724. {
  2725. if (node)
  2726. return node->mtn_subtree;
  2727. else
  2728. return NULL;
  2729. }
  2730. /* GB : there is a potential problems with this function
  2731. * when several backends are used
  2732. */
  2733. PRBool slapi_mapping_tree_node_is_set (const mapping_tree_node *node, PRUint32 flag)
  2734. {
  2735. if (flag & SLAPI_MTN_LOCAL)
  2736. return PR_TRUE;
  2737. if (flag & SLAPI_MTN_PRIVATE)
  2738. return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_private);
  2739. if (flag & SLAPI_MTN_READONLY)
  2740. return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_be[0]->be_readonly);
  2741. return PR_FALSE;
  2742. }
  2743. /*
  2744. * Description: this function returns root of the subtree to which the node applies
  2745. *
  2746. * Arguments: node
  2747. *
  2748. * Returns: dn of the parent of mapping tree node configuration entry.
  2749. */
  2750. const char* slapi_get_mapping_tree_config_root ()
  2751. {
  2752. return MAPPING_TREE_BASE_DN;
  2753. }
  2754. /*
  2755. * slapi_be_select() finds the backend that should be used to service dn.
  2756. * If no backend with an appropriate suffix is configured, the default backend
  2757. * is returned. This function never returns NULL.
  2758. */
  2759. Slapi_Backend *
  2760. slapi_be_select( const Slapi_DN *sdn ) /* JCM - The name of this should change??? */
  2761. {
  2762. Slapi_Backend *be;
  2763. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2764. if((node!=NULL) && (node->mtn_be!=NULL))
  2765. be= node->mtn_be[0];
  2766. else
  2767. be = NULL;
  2768. if(be==NULL)
  2769. be= defbackend_get_backend();
  2770. return be;
  2771. }
  2772. /* Check if the dn targets an internal reserved backends */
  2773. int
  2774. slapi_on_internal_backends(const Slapi_DN *sdn)
  2775. {
  2776. char *backend_names[] = {DSE_BACKEND, DSE_SCHEMA};
  2777. int internal = 1;
  2778. int numOfInternalBackends = 2;
  2779. int count;
  2780. Slapi_Backend *internal_be;
  2781. Slapi_Backend *be = slapi_be_select(sdn);
  2782. for (count=0; count < numOfInternalBackends ; ++count){
  2783. /* the internal backends are always in the begining of the list
  2784. * so should not be very inefficient
  2785. */
  2786. internal_be = slapi_be_select_by_instance_name(backend_names[count]);
  2787. if(be == internal_be){
  2788. return internal;
  2789. }
  2790. }
  2791. return 0;
  2792. }
  2793. /* Some of the operations are not allowed from the plugins
  2794. * but default to specialized use of those operations
  2795. * e.g rootDse search, ConfigRoot searches
  2796. * cn=config, cn=schema etc
  2797. * Return value: 1 if reserved.
  2798. * 0 if not reserved.
  2799. * -1 in error case.
  2800. */
  2801. int slapi_op_reserved(Slapi_PBlock *pb)
  2802. {
  2803. int scope=LDAP_SCOPE_BASE;
  2804. int reservedOp=0;
  2805. int op_type;
  2806. Slapi_Operation *op = NULL;
  2807. Slapi_DN *target_sdn=NULL;
  2808. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2809. if (NULL == op) {
  2810. return -1;
  2811. }
  2812. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
  2813. /* Get the target for this op */
  2814. target_sdn = operation_get_target_spec (op);
  2815. if( op_type == SLAPI_OPERATION_SEARCH){
  2816. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  2817. if( sdn_is_nulldn(target_sdn) && (scope == LDAP_SCOPE_BASE) ){
  2818. reservedOp = 1;
  2819. }
  2820. }
  2821. if(slapi_on_internal_backends(target_sdn)){
  2822. reservedOp = 1;
  2823. }
  2824. return reservedOp;
  2825. }
  2826. /*
  2827. * Returns the name of the Backend that contains specified DN,
  2828. * if only one matches. Otherwise returns NULL
  2829. * The name is pointing to the mapping tree structure
  2830. * and should not be altered.
  2831. */
  2832. const char *
  2833. slapi_mtn_get_backend_name( const Slapi_DN *sdn)
  2834. {
  2835. mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
  2836. if ((node != NULL) &&
  2837. (node->mtn_be_count == 1) &&
  2838. (node->mtn_backend_names != NULL))
  2839. /* There's only one name, return it */
  2840. return node->mtn_backend_names[0];
  2841. else
  2842. return NULL;
  2843. }
  2844. /* Check if the backend that contains specified DN exists */
  2845. int
  2846. slapi_be_exist(const Slapi_DN *sdn) /* JCM - The name of this should change??? */
  2847. {
  2848. Slapi_Backend *def_be = defbackend_get_backend();
  2849. Slapi_Backend *be = slapi_be_select (sdn);
  2850. return (be != def_be);
  2851. }
  2852. /* The two following functions can be used to
  2853. * parse the list of the root suffix of the DIT
  2854. * Using
  2855. */
  2856. Slapi_DN *
  2857. slapi_get_first_suffix(void ** node, int show_private)
  2858. {
  2859. mapping_tree_node *first_node;
  2860. if ((NULL == node) || (NULL == mapping_tree_root)) {
  2861. return NULL;
  2862. }
  2863. first_node = mapping_tree_root->mtn_children;
  2864. *node = (void * ) first_node ;
  2865. while (first_node && (first_node->mtn_private && (show_private == 0)))
  2866. first_node = first_node->mtn_brother;
  2867. return (first_node ? first_node->mtn_subtree : NULL);
  2868. }
  2869. Slapi_DN *
  2870. slapi_get_next_suffix(void ** node, int show_private)
  2871. {
  2872. mapping_tree_node *next_node;
  2873. if ((NULL == node) || (NULL == mapping_tree_root)) {
  2874. return NULL;
  2875. }
  2876. next_node = *node;
  2877. if (next_node == NULL) {
  2878. return NULL;
  2879. }
  2880. next_node = next_node->mtn_brother;
  2881. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2882. next_node = next_node->mtn_brother;
  2883. *node = next_node;
  2884. return (next_node ? next_node->mtn_subtree : NULL);
  2885. }
  2886. /* get mapping tree node recursively */
  2887. Slapi_DN *
  2888. slapi_get_next_suffix_ext(void ** node, int show_private)
  2889. {
  2890. mapping_tree_node * next_node = NULL;
  2891. if (NULL == node) {
  2892. return NULL;
  2893. }
  2894. next_node = *node;
  2895. if (next_node == NULL) {
  2896. return NULL;
  2897. }
  2898. if (next_node->mtn_children) {
  2899. next_node = next_node->mtn_children;
  2900. } else if (next_node->mtn_brother) {
  2901. next_node = next_node->mtn_brother;
  2902. } else {
  2903. next_node = next_node->mtn_parent;
  2904. if (next_node) {
  2905. next_node = next_node->mtn_brother;
  2906. }
  2907. }
  2908. while (next_node && (next_node->mtn_private && (show_private == 0)))
  2909. next_node = next_node->mtn_brother;
  2910. if (next_node) {
  2911. *node = next_node;
  2912. }
  2913. return (next_node ? next_node->mtn_subtree : NULL);
  2914. }
  2915. /* check if a suffix is a root of the DIT
  2916. * return 1 if yes, 0 if no
  2917. */
  2918. int slapi_is_root_suffix(Slapi_DN * dn)
  2919. {
  2920. void * node;
  2921. Slapi_DN * suffix = slapi_get_first_suffix (&node, 1);
  2922. while (suffix)
  2923. {
  2924. if ( slapi_sdn_compare(dn, suffix) == 0 )
  2925. return 1;
  2926. suffix = slapi_get_next_suffix(&node, 1);
  2927. }
  2928. return 0 ;
  2929. }
  2930. /* Return value is a part mapping tree; Don't free it. */
  2931. const Slapi_DN *
  2932. slapi_get_suffix_by_dn(const Slapi_DN *dn)
  2933. {
  2934. mapping_tree_node *node = slapi_get_mapping_tree_node_by_dn(dn);
  2935. const Slapi_DN *suffix = NULL;
  2936. if (node)
  2937. {
  2938. suffix = (const Slapi_DN *)slapi_mtn_get_dn(node);
  2939. }
  2940. return suffix;
  2941. }
  2942. /*
  2943. * set referrals for the node
  2944. * notes :
  2945. * - referral is consumed by this function
  2946. * - node must exist before calling this function
  2947. * - mapping tree node state is not changed by this function
  2948. */
  2949. int
  2950. slapi_mtn_set_referral(const Slapi_DN *sdn, char ** referral)
  2951. {
  2952. Slapi_PBlock pb;
  2953. Slapi_Mods smods;
  2954. int rc = LDAP_SUCCESS,i = 0, j = 0;
  2955. Slapi_DN* node_sdn;
  2956. char **values = NULL;
  2957. int do_modify = 0;
  2958. slapi_mods_init (&smods, 0);
  2959. node_sdn = slapi_get_mapping_tree_node_configsdn(sdn);
  2960. if(!node_sdn){
  2961. /* shutdown has been detected */
  2962. return LDAP_OPERATIONS_ERROR;
  2963. }
  2964. if ((referral == NULL) || (referral[0] == NULL))
  2965. {
  2966. /* NULL referral means we want to delete existing referral
  2967. */
  2968. slapi_mods_add(&smods, LDAP_MOD_DELETE, "nsslapd-referral", 0, NULL);
  2969. do_modify = 1;
  2970. }
  2971. else
  2972. {
  2973. int changes = 1;
  2974. int referralCount = 0;
  2975. for(; referral[referralCount]; referralCount++);
  2976. if ( (values = slapi_mtn_get_referral(sdn)) != NULL )
  2977. {
  2978. /* Check if there are differences between current values and values to be set */
  2979. for (i=0; values[i]; i++);
  2980. if (i == referralCount) {
  2981. changes = 0;
  2982. for (i=0;values[i];i++){
  2983. int found = 0;
  2984. for (j=0;referral[j];j++){
  2985. if (strcmp(values[i], referral[j]) == 0){
  2986. found = 1;
  2987. break;
  2988. }
  2989. }
  2990. if (!found) {
  2991. changes = 1;
  2992. break;
  2993. }
  2994. }
  2995. }
  2996. i=0;
  2997. while(values[i])
  2998. slapi_ch_free((void**)&values[i++]);
  2999. slapi_ch_free((void**)&values);
  3000. }
  3001. if (changes){
  3002. Slapi_Value *val;
  3003. Slapi_Value ** svals = NULL;
  3004. do_modify = 1;
  3005. for (j =0; referral[j];j++) {
  3006. val = slapi_value_new_string(referral[j]);
  3007. valuearray_add_value(&svals, val);
  3008. slapi_value_free(&val);
  3009. }
  3010. slapi_mods_add_mod_values(&smods, LDAP_MOD_REPLACE, "nsslapd-referral", svals);
  3011. valuearray_free(&svals);
  3012. }
  3013. }
  3014. if ( do_modify )
  3015. {
  3016. pblock_init (&pb);
  3017. slapi_modify_internal_set_pb_ext (&pb, node_sdn,
  3018. slapi_mods_get_ldapmods_byref(&smods), NULL,
  3019. NULL, (void *) plugin_get_default_component_id(), 0);
  3020. slapi_modify_internal_pb (&pb);
  3021. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3022. pblock_done(&pb);
  3023. }
  3024. slapi_mods_done(&smods);
  3025. slapi_sdn_free(&node_sdn);
  3026. return rc;
  3027. }
  3028. /*
  3029. * Change the state of a mapping tree node entry
  3030. * notes :
  3031. * - sdn argument is the dn of the subtree of the DIT managed by this node
  3032. * not the dn of the mapping tree entry
  3033. * - mapping tree node must exist before calling this function
  3034. */
  3035. int
  3036. slapi_mtn_set_state(const Slapi_DN *sdn, char *state)
  3037. {
  3038. Slapi_PBlock pb;
  3039. Slapi_Mods smods;
  3040. int rc = LDAP_SUCCESS;
  3041. Slapi_DN *node_sdn;
  3042. char * value;
  3043. if (NULL == state) {
  3044. return LDAP_OPERATIONS_ERROR;
  3045. }
  3046. node_sdn = slapi_get_mapping_tree_node_configsdn(sdn);
  3047. if(!node_sdn){
  3048. /* shutdown has been detected */
  3049. return LDAP_OPERATIONS_ERROR;
  3050. }
  3051. if ( (value = slapi_mtn_get_state(sdn)) != NULL )
  3052. {
  3053. if ( strcasecmp(value, state) == 0 )
  3054. {
  3055. /* Same state, don't change anything */
  3056. goto bail;
  3057. }
  3058. }
  3059. /* Otherwise, means that the state has changed, modify it */
  3060. slapi_mods_init (&smods, 1);
  3061. slapi_mods_add(&smods, LDAP_MOD_REPLACE, "nsslapd-state", strlen(state), state);
  3062. pblock_init (&pb);
  3063. slapi_modify_internal_set_pb_ext (&pb, node_sdn,
  3064. slapi_mods_get_ldapmods_byref(&smods), NULL,
  3065. NULL, (void *) plugin_get_default_component_id(), 0);
  3066. slapi_modify_internal_pb (&pb);
  3067. slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3068. slapi_mods_done(&smods);
  3069. pblock_done(&pb);
  3070. bail:
  3071. slapi_ch_free_string(&value);
  3072. slapi_sdn_free(&node_sdn);
  3073. return rc;
  3074. }
  3075. /*
  3076. returns a copy of the attr - the caller must slapi_attr_free it
  3077. */
  3078. Slapi_Attr *
  3079. mtn_get_attr(char* node_dn, char * type)
  3080. {
  3081. Slapi_PBlock pb;
  3082. int res = 0;
  3083. Slapi_Entry **entries = NULL;
  3084. Slapi_Attr *attr = NULL;
  3085. Slapi_Attr *ret_attr = NULL;
  3086. char **attrs = NULL;
  3087. attrs = (char **)slapi_ch_calloc(2, sizeof(char *));
  3088. attrs[0] = slapi_ch_strdup(type);
  3089. pblock_init(&pb);
  3090. slapi_search_internal_set_pb(&pb, node_dn, LDAP_SCOPE_BASE,
  3091. "objectclass=nsMappingTree", attrs, 0, NULL, NULL,
  3092. (void *) plugin_get_default_component_id(), 0);
  3093. slapi_search_internal_pb(&pb);
  3094. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3095. if (res != LDAP_SUCCESS) {
  3096. goto done;
  3097. }
  3098. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3099. if (NULL == entries || NULL == entries[0]) {
  3100. goto done;
  3101. }
  3102. /* always at most one entry entries[0] */
  3103. res = slapi_entry_attr_find(entries[0], type, &attr);
  3104. if (res == 0)
  3105. /* we need to make a copy here so we can free the search results */
  3106. ret_attr = slapi_attr_dup(attr);
  3107. slapi_free_search_results_internal(&pb);
  3108. done:
  3109. slapi_ch_free((void **)&attrs[0]);
  3110. slapi_ch_free((void **)&attrs);
  3111. pblock_done(&pb);
  3112. return ret_attr;
  3113. }
  3114. /*
  3115. * Get the referral associated to the mapping tree node entry
  3116. * notes :
  3117. * - sdn argument is the dn of the subtree of the DIT managed by this node
  3118. * not the dn of the mapping tree entry
  3119. * - return NULL if no referral
  3120. * - caller is reponsible for freeing the returned referrals
  3121. */
  3122. char **
  3123. slapi_mtn_get_referral(const Slapi_DN *sdn)
  3124. {
  3125. int i, hint, nb;
  3126. char * node_dn;
  3127. Slapi_Attr *attr;
  3128. char ** referral = NULL;
  3129. Slapi_Value *val = NULL;
  3130. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  3131. if(!node_dn){
  3132. /* shutdown has been detected */
  3133. return NULL;
  3134. }
  3135. attr = mtn_get_attr(node_dn, "nsslapd-referral");
  3136. if (attr)
  3137. {
  3138. /* if there are some referrals set in the entry build a list
  3139. * to be returned to the caller
  3140. */
  3141. slapi_attr_get_numvalues(attr, &nb);
  3142. referral = (char**) slapi_ch_malloc(sizeof(char*) * (nb+1));
  3143. hint = slapi_attr_first_value(attr, &val);
  3144. i = 0;
  3145. while (val)
  3146. {
  3147. referral[i++] = slapi_ch_strdup(slapi_value_get_string(val));
  3148. hint = slapi_attr_next_value(attr, hint, &val);
  3149. }
  3150. referral[i] = NULL;
  3151. slapi_attr_free(&attr);
  3152. }
  3153. slapi_ch_free_string(&node_dn);
  3154. return referral;
  3155. }
  3156. /*
  3157. * Get the state of a mapping tree node entry
  3158. * notes :
  3159. * - sdn argument is the dn of the subtree of the DIT managed by this node
  3160. * not the dn of the mapping tree entry
  3161. * - the state is return in a newly allocated string that must be freed by
  3162. * the caller
  3163. */
  3164. char *
  3165. slapi_mtn_get_state(const Slapi_DN *sdn)
  3166. {
  3167. char * node_dn;
  3168. Slapi_Attr *attr;
  3169. char * state = NULL;
  3170. Slapi_Value *val = NULL;
  3171. node_dn = slapi_get_mapping_tree_node_configdn(sdn);
  3172. if(!node_dn){
  3173. /* shutdown has been detected */
  3174. return NULL;
  3175. }
  3176. attr = mtn_get_attr(node_dn, "nsslapd-state");
  3177. if (attr)
  3178. {
  3179. /* entry state was found */
  3180. slapi_attr_first_value(attr, &val);
  3181. state = slapi_ch_strdup(slapi_value_get_string(val));
  3182. slapi_attr_free(&attr);
  3183. }
  3184. slapi_ch_free_string(&node_dn);
  3185. return state;
  3186. }
  3187. static void
  3188. mtn_internal_be_set_state(Slapi_Backend *be, int state)
  3189. {
  3190. mapping_tree_node * node;
  3191. char * be_name;
  3192. int i;
  3193. int change_callback = 0;
  3194. int old_state;
  3195. mtn_wlock();
  3196. be_name = slapi_ch_strdup(slapi_be_get_name(be));
  3197. node = get_mapping_tree_node_by_name(mapping_tree_root, be_name);
  3198. if (node == NULL)
  3199. {
  3200. LDAPDebug(LDAP_DEBUG_TRACE,
  3201. "Warning: backend %s is not declared in mapping tree\n",
  3202. be_name, 0 ,0);
  3203. goto done;
  3204. }
  3205. /* now search the backend in this node */
  3206. i = 0;
  3207. while ( (i < node->mtn_be_count) &&
  3208. (node->mtn_backend_names) &&
  3209. (node->mtn_backend_names[i]) &&
  3210. (strcmp(node->mtn_backend_names[i],be_name)))
  3211. {
  3212. i++;
  3213. }
  3214. if ( (i >= node->mtn_be_count) || (node->mtn_backend_names == NULL) ||
  3215. (node->mtn_backend_names[i] == NULL) )
  3216. {
  3217. /* backend is not declared in the mapping tree node
  3218. * print out a warning
  3219. */
  3220. LDAPDebug(LDAP_DEBUG_TRACE,
  3221. "Warning: backend %s is not declared in mapping node entry\n",
  3222. be_name, 0 ,0);
  3223. goto done;
  3224. }
  3225. change_callback = 1;
  3226. old_state = node->mtn_be_states[i];
  3227. /* OK we found the backend at last, now do the real job: set the state */
  3228. switch (state)
  3229. {
  3230. case SLAPI_BE_STATE_OFFLINE:
  3231. node->mtn_be[i] = be;
  3232. node->mtn_be_states[i] = SLAPI_BE_STATE_OFFLINE;
  3233. break;
  3234. case SLAPI_BE_STATE_ON:
  3235. node->mtn_be[i] = be;
  3236. node->mtn_be_states[i] = SLAPI_BE_STATE_ON;
  3237. break;
  3238. case SLAPI_BE_STATE_DELETE:
  3239. node->mtn_be[i] = NULL;
  3240. node->mtn_be_states[i] = SLAPI_BE_STATE_DELETE;
  3241. break;
  3242. }
  3243. done:
  3244. mtn_unlock();
  3245. if (change_callback)
  3246. mtn_be_state_change(be_name, old_state, state);
  3247. slapi_ch_free( (void **) &be_name);
  3248. }
  3249. /*
  3250. * This procedure must be called by previously stopped backends
  3251. * to signal that they have started and are ready to process requests
  3252. * The backend must be fully ready to handle requests before calling this
  3253. * procedure
  3254. * At startup tiem it is not mandatory for the backends to
  3255. * call this procedure: backends are assumed on by default
  3256. */
  3257. void
  3258. slapi_mtn_be_started(Slapi_Backend *be)
  3259. {
  3260. /* Find the node where this backend stay
  3261. * then update the backend structure
  3262. * In the long term, the backend should have only one suffix and
  3263. * stay in only one node as for now, check all suffixes
  3264. * Rq : since mapping tree is initiatized very soon in the server
  3265. * startup, we can be sure at that time that the mapping
  3266. * tree is initialized
  3267. */
  3268. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3269. }
  3270. /* these procedure can be called when backends need to be put in maintenance mode
  3271. * after call to slapi_mtn_be_disable, the backend will still be known
  3272. * by a server but the mapping tree won't route requests to it anymore
  3273. * The slapi_mtn_be_enable function enable to route requests to the backend
  3274. * again
  3275. * the slapi_mtn_be_disable function only returns when there is no more
  3276. * request in progress in the backend
  3277. */
  3278. void
  3279. slapi_mtn_be_disable(Slapi_Backend *be)
  3280. {
  3281. mtn_internal_be_set_state(be, SLAPI_BE_STATE_OFFLINE);
  3282. /* the two following lines can seem weird, but they allow to check that no
  3283. * LDAP operation is in progress on the backend
  3284. */
  3285. slapi_be_Wlock(be);
  3286. slapi_be_Unlock(be);
  3287. }
  3288. void
  3289. slapi_mtn_be_enable(Slapi_Backend *be)
  3290. {
  3291. mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
  3292. }
  3293. /*
  3294. * This procedure must be called by backends before stopping
  3295. * if some operations are in progress when this procedure
  3296. * is called, this procedure will block until completion
  3297. * of these operations
  3298. * The backend must wait return from this procedure before stopping operation
  3299. * Backends must serve operation until the return from this procedure.
  3300. * Once this procedure return they will not be issued request anymore
  3301. * and they have been removed from the server list of backends
  3302. * It is also the bakend responsability to free the Slapi_Backend structures
  3303. * that was given by slapi_be_new at startup time.
  3304. * Should the backend start again, it would need to issue slapi_be_new again
  3305. */
  3306. void
  3307. slapi_mtn_be_stopping(Slapi_Backend *be)
  3308. {
  3309. mtn_internal_be_set_state(be, SLAPI_BE_STATE_DELETE);
  3310. /* the two following lines can seem weird, but they allow to check that no
  3311. * LDAP operation is in progress on the backend
  3312. */
  3313. slapi_be_Wlock(be);
  3314. slapi_be_Unlock(be);
  3315. slapi_be_stopping(be);
  3316. }
  3317. /*
  3318. * Switch a backend into read-only mode, or back to read-write mode.
  3319. * To switch to read-only mode, we need to wait for all pending operations
  3320. * to finish.
  3321. */
  3322. void
  3323. slapi_mtn_be_set_readonly(Slapi_Backend *be, int readonly)
  3324. {
  3325. if (readonly) {
  3326. slapi_be_Wlock(be);
  3327. slapi_be_set_readonly(be, 1);
  3328. slapi_be_Unlock(be);
  3329. } else {
  3330. slapi_be_set_readonly(be, 0);
  3331. }
  3332. }
  3333. #ifdef DEBUG
  3334. static int lock_count = 0;
  3335. #endif
  3336. void mtn_wlock()
  3337. {
  3338. slapi_rwlock_wrlock(myLock);
  3339. #ifdef DEBUG
  3340. lock_count--;
  3341. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_wlock : lock count : %d\n", lock_count, 0, 0);
  3342. #endif
  3343. }
  3344. void mtn_lock()
  3345. {
  3346. slapi_rwlock_rdlock(myLock);
  3347. #ifdef DEBUG
  3348. lock_count++;
  3349. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_lock : lock count : %d\n", lock_count, 0, 0);
  3350. #endif
  3351. }
  3352. void mtn_unlock()
  3353. {
  3354. #ifdef DEBUG
  3355. if (lock_count > 0)
  3356. lock_count--;
  3357. else if (lock_count < 0)
  3358. lock_count++;
  3359. else
  3360. lock_count = (int) 11111111; /* this happening means problems */
  3361. LDAPDebug(LDAP_DEBUG_ARGS, "mtn_unlock : lock count : %d\n", lock_count, 0, 0);
  3362. #endif
  3363. slapi_rwlock_unlock(myLock);
  3364. }
  3365. #ifdef TEST_FOR_REGISTER_CHANGE
  3366. void my_test_fnct1(void *handle, char *be_name, int old_state, int new_state)
  3367. {
  3368. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3369. "my_test_fnct1 : handle %d, be %s, old state %d, new state %d\n",
  3370. handle,be_name, old_state, new_state);
  3371. if (old_state == 2)
  3372. slapi_unregister_backend_state_change(handle);
  3373. }
  3374. void my_test_fnct2(void *handle, char *be_name, int old_state, int new_state)
  3375. {
  3376. slapi_log_error(SLAPI_LOG_ARGS, NULL,
  3377. "my_test_fnct2 : handle %d, be %s, old state %d, new state %d\n",
  3378. handle, be_name, old_state, new_state);
  3379. }
  3380. void test_register()
  3381. {
  3382. slapi_register_backend_state_change((void *) 1234, my_test_fnct1);
  3383. slapi_register_backend_state_change((void *) 4321, my_test_fnct2);
  3384. }
  3385. #endif
  3386. #ifdef DEBUG
  3387. #ifdef USE_DUMP_MAPPING_TREE
  3388. static void dump_mapping_tree(mapping_tree_node *parent, int depth)
  3389. {
  3390. mapping_tree_node *current = NULL;
  3391. static char dump_indent[256];
  3392. int i;
  3393. if (depth == 0)
  3394. {
  3395. LDAPDebug(LDAP_DEBUG_ANY, "dump_mapping_tree\n", 0, 0, 0);
  3396. }
  3397. dump_indent[0] = '\0';
  3398. for (i = 0; i < depth; i++)
  3399. PL_strcatn(dump_indent, sizeof(dump_indent), " ");
  3400. for (current = parent->mtn_children; current;
  3401. current = current->mtn_brother)
  3402. {
  3403. if (strlen(current->mtn_subtree->dn) == 0)
  3404. {
  3405. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3406. dump_indent, "none", current);
  3407. }
  3408. else
  3409. {
  3410. LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
  3411. dump_indent, current->mtn_subtree->dn, current);
  3412. }
  3413. dump_mapping_tree(current, depth+1);
  3414. }
  3415. return;
  3416. }
  3417. #endif /* USE_DUMP_MAPPING_TREE */
  3418. #endif /* DEBUG */
  3419. /* helper function to set/remove the config param in cn=config */
  3420. static int
  3421. _mtn_update_config_param(int op, char *type, char *strvalue)
  3422. {
  3423. Slapi_PBlock confpb;
  3424. Slapi_DN sdn;
  3425. Slapi_Mods smods;
  3426. LDAPMod **mods;
  3427. int rc = LDAP_PARAM_ERROR;
  3428. pblock_init (&confpb);
  3429. slapi_mods_init (&smods, 0);
  3430. switch (op) {
  3431. case LDAP_MOD_DELETE:
  3432. slapi_mods_add(&smods, op, type, 0, NULL);
  3433. break;
  3434. case LDAP_MOD_ADD:
  3435. case LDAP_MOD_REPLACE:
  3436. slapi_mods_add_string(&smods, op, type, strvalue);
  3437. break;
  3438. default:
  3439. return rc;
  3440. }
  3441. slapi_sdn_init_ndn_byref(&sdn, SLAPD_CONFIG_DN);
  3442. slapi_modify_internal_set_pb_ext(&confpb, &sdn,
  3443. slapi_mods_get_ldapmods_byref(&smods),
  3444. NULL, NULL,
  3445. (void *)plugin_get_default_component_id(), 0);
  3446. slapi_modify_internal_pb(&confpb);
  3447. slapi_pblock_get (&confpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3448. slapi_sdn_done(&sdn);
  3449. /* need to free passed out mods
  3450. * since the internal modify could realloced mods. */
  3451. slapi_pblock_get(&confpb, SLAPI_MODIFY_MODS, &mods);
  3452. ldap_mods_free (mods, 1 /* Free the Array and the Elements */);
  3453. pblock_done(&confpb);
  3454. return rc;
  3455. }