mapping_tree.c 112 KB

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