mapping_tree.c 112 KB

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