mapping_tree.c 112 KB

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