mapping_tree.c 112 KB

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