mapping_tree.c 127 KB

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