mapping_tree.c 112 KB

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