mapping_tree.c 112 KB

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