cos_cache.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. The cos cache keeps in memory all of
  14. the data related to cos. This allows
  15. very fast lookups at the expense of RAM.
  16. All meta data is indexed, allowing fast
  17. binary search lookups.
  18. The cache is not dynamic, in the sense
  19. that it does not iteratively modify
  20. itself as changes are made to the cos
  21. meta-data. Rather, it is designed to
  22. be fast to read, with non-locking
  23. multiple thread access to the cache,
  24. at the expense of modification speed.
  25. This means that when changes do occur,
  26. the cache must be rebuilt from scratch.
  27. However, this is achieved in such a way,
  28. so as to allow cache queries during the
  29. building of the new cache - so once a
  30. cache has been built, there is no down
  31. time.
  32. Of course, the configuration of the cos meta
  33. data is likely to be a thing which does not
  34. happen often. Any other use, is probably a
  35. mis-use of the mechanism, and certainly will
  36. suffer from performance problems.
  37. */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include "portable.h"
  42. #include "slapi-plugin.h"
  43. /* this is naughty, but the api for backend state change is currently here */
  44. #include "slapi-private.h"
  45. /* include NSPR header files */
  46. #include "prthread.h"
  47. #include "prlock.h"
  48. #include "prerror.h"
  49. #include "prcvar.h"
  50. #include "prio.h"
  51. #include "vattr_spi.h"
  52. #include "cos_cache.h"
  53. #include "views.h"
  54. static void **views_api;
  55. /*** secret functions and structs in slapd ***/
  56. /*
  57. these are required here because they are not available
  58. in any public header. They must exactly match their
  59. counterparts in the server or they will fail to work
  60. correctly.
  61. */
  62. /*** from slap.h ***/
  63. struct objclass {
  64. char *oc_name; /* NAME */
  65. char *oc_desc; /* DESC */
  66. char *oc_oid; /* object identifier */
  67. char *oc_superior; /* SUP -- XXXmcs: should be an array */
  68. PRUint8 oc_kind; /* ABSTRACT/STRUCTURAL/AUXILIARY */
  69. PRUint8 oc_flags; /* misc. flags, e.g., OBSOLETE */
  70. char **oc_required;
  71. char **oc_allowed;
  72. char **oc_orig_required; /* MUST */
  73. char **oc_orig_allowed; /* MAY */
  74. char **oc_origin; /* X-ORIGIN extension */
  75. struct objclass *oc_next;
  76. };
  77. /*** from proto-slap.h ***/
  78. int config_get_schemacheck();
  79. void oc_lock_read( void );
  80. void oc_unlock( void );
  81. struct objclass* g_get_global_oc_nolock();
  82. int slapd_log_error_proc( char *subsystem, char *fmt, ... );
  83. /* defined in cos.c */
  84. void * cos_get_plugin_identity();
  85. /*** end secrets ***/
  86. #define COS_PLUGIN_SUBSYSTEM "cos-plugin" /* used for logging */
  87. #define COSTYPE_BADTYPE 0
  88. #define COSTYPE_CLASSIC 1
  89. #define COSTYPE_POINTER 2
  90. #define COSTYPE_INDIRECT 3
  91. #define COS_DEF_ERROR_NO_TEMPLATES -2
  92. /* the global plugin handle */
  93. static volatile vattr_sp_handle *vattr_handle = NULL;
  94. static int cos_cache_notify_flag = 0;
  95. /* service definition cache structs */
  96. /* cosIndexedLinkedList: provides an object oriented type interface to
  97. link lists where each element contains an index for the entire
  98. list. All structures that contain this one must specify this one
  99. as the first member otherwise the supporting functions will not work.
  100. {PARPAR} The indexing ability is not currently used since the
  101. fastest lookup is achieved via a cache level index of all attributes,
  102. however this mechanism may prove useful in the future
  103. */
  104. struct _cosIndexedLinkedList
  105. {
  106. void *pNext;
  107. void **index;
  108. };
  109. typedef struct _cosIndexedLinkedList cosIndexedLinkedList;
  110. struct _cosAttrValue
  111. {
  112. cosIndexedLinkedList list;
  113. char *val;
  114. };
  115. typedef struct _cosAttrValue cosAttrValue;
  116. struct _cosAttribute
  117. {
  118. cosIndexedLinkedList list;
  119. char *pAttrName;
  120. cosAttrValue *pAttrValue;
  121. cosAttrValue *pObjectclasses;
  122. int attr_override;
  123. int attr_operational;
  124. int attr_operational_default;
  125. int attr_cos_merge;
  126. void *pParent;
  127. };
  128. typedef struct _cosAttribute cosAttributes;
  129. struct _cosTemplate
  130. {
  131. cosIndexedLinkedList list;
  132. cosAttrValue *pDn;
  133. cosAttrValue *pObjectclasses;
  134. cosAttributes *pAttrs;
  135. char *cosGrade;
  136. int template_default;
  137. void *pParent;
  138. unsigned long cosPriority;
  139. };
  140. typedef struct _cosTemplate cosTemplates;
  141. struct _cosDefinition
  142. {
  143. cosIndexedLinkedList list;
  144. int cosType;
  145. cosAttrValue *pDn;
  146. cosAttrValue *pCosTargetTree;
  147. cosAttrValue *pCosTemplateDn;
  148. cosAttrValue *pCosSpecifier;
  149. cosAttrValue *pCosAttrs;
  150. cosAttrValue *pCosOverrides;
  151. cosAttrValue *pCosOperational;
  152. cosAttrValue *pCosOpDefault;
  153. cosAttrValue *pCosMerge;
  154. cosTemplates *pCosTmps;
  155. };
  156. typedef struct _cosDefinition cosDefinitions;
  157. struct _cos_cache
  158. {
  159. cosDefinitions *pDefs;
  160. cosAttributes **ppAttrIndex;
  161. int attrCount;
  162. char **ppTemplateList;
  163. int templateCount;
  164. int refCount;
  165. int vattr_cacheable;
  166. };
  167. typedef struct _cos_cache cosCache;
  168. /* cache manipulation function prototypes*/
  169. static cosCache *pCache; /* always the current global cache, only use getref to get */
  170. /* the place to start if you want a new cache */
  171. static int cos_cache_create();
  172. /* cache index related functions */
  173. static int cos_cache_index_all(cosCache *pCache);
  174. static int cos_cache_attr_compare(const void *e1, const void *e2);
  175. static int cos_cache_template_index_compare(const void *e1, const void *e2);
  176. static int cos_cache_string_compare(const void *e1, const void *e2);
  177. static int cos_cache_template_index_bsearch(const char *dn);
  178. static int cos_cache_attr_index_bsearch( const cosCache *pCache, const cosAttributes *key, int lower, int upper );
  179. /* the multi purpose list creation function, pass it something and it links it */
  180. static void cos_cache_add_ll_entry(void **attrval, void *theVal, int ( *compare )(const void *elem1, const void *elem2 ));
  181. /* cosAttrValue manipulation */
  182. static int cos_cache_add_attrval(cosAttrValue **attrval, char *val);
  183. static void cos_cache_del_attrval_list(cosAttrValue **pVal);
  184. static int cos_cache_attrval_exists(cosAttrValue *pAttrs, const char *val);
  185. /* cosAttributes manipulation */
  186. static int cos_cache_add_attr(cosAttributes **pAttrs, char *name, cosAttrValue *val);
  187. static void cos_cache_del_attr_list(cosAttributes **pAttrs);
  188. static int cos_cache_find_attr(cosCache *pCache, char *type);
  189. static int cos_cache_total_attr_count(cosCache *pCache);
  190. static int cos_cache_cos_2_slapi_valueset(cosAttributes *pAttr, Slapi_ValueSet **out_vs);
  191. static int cos_cache_cmp_attr(cosAttributes *pAttr, Slapi_Value *test_this, int *result);
  192. /* cosTemplates manipulation */
  193. static int cos_cache_add_dn_tmpls(char *dn, cosAttrValue *pCosSpecifier, cosAttrValue *pAttrs, cosTemplates **pTmpls);
  194. static int cos_cache_add_tmpl(cosTemplates **pTemplates, cosAttrValue *dn, cosAttrValue *objclasses, cosAttrValue *pCosSpecifier, cosAttributes *pAttrs,cosAttrValue *cosPriority);
  195. /* cosDefinitions manipulation */
  196. static int cos_cache_build_definition_list(cosDefinitions **pDefs, int *vattr_cacheable);
  197. static int cos_cache_add_dn_defs(char *dn, cosDefinitions **pDefs);
  198. static int cos_cache_add_defn(cosDefinitions **pDefs, cosAttrValue **dn, int cosType, cosAttrValue **tree, cosAttrValue **tmpDn, cosAttrValue **spec, cosAttrValue **pAttrs, cosAttrValue **pOverrides, cosAttrValue **pOperational, cosAttrValue **pCosMerge, cosAttrValue **pCosOpDefault);
  199. static int cos_cache_entry_is_cos_related( Slapi_Entry *e);
  200. /* schema checking */
  201. static int cos_cache_schema_check(cosCache *pCache, int cache_attr_index, Slapi_Attr *pObjclasses);
  202. static int cos_cache_schema_build(cosCache *pCache);
  203. static void cos_cache_del_schema(cosCache *pCache);
  204. /* special cos scheme implimentations (special = other than cos classic) */
  205. static int cos_cache_follow_pointer( vattr_context *context, const char *dn, char *type, Slapi_ValueSet **out_vs, Slapi_Value *test_this, int *result, int flags );
  206. /* this dude is the thread function which performs dynamic config of the cache */
  207. static void cos_cache_wait_on_change(void *arg);
  208. /* this gets called when a backend changes state */
  209. void cos_cache_backend_state_change(void *handle, char *be_name,
  210. int old_be_state, int new_be_state);
  211. /* operation callbacks for vattr service */
  212. static int cos_cache_vattr_get(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, char *type, Slapi_ValueSet** results,int *type_name_disposition, char** actual_type_name, int flags, int *free_flags, void *hint);
  213. static int cos_cache_vattr_compare(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, char *type, Slapi_Value *test_this, int* result, int flags, void *hint);
  214. static int cos_cache_vattr_types(vattr_sp_handle *handle,Slapi_Entry *e,vattr_type_list_context *type_context,int flags);
  215. static int cos_cache_query_attr(cos_cache *ptheCache, vattr_context *context, Slapi_Entry *e, char *type, Slapi_ValueSet **out_attr, Slapi_Value *test_this, int *result, int *ops, int *indirect_cos);
  216. /*
  217. compares s2 to s1 starting from end of string until the beginning of either
  218. matches result in the s2 value being clipped from s1 with a NULL char
  219. and 1 being returned as opposed to 0
  220. */
  221. static int cos_cache_backwards_stricmp_and_clip(char*s1,char*s2);
  222. /* module level thread control stuff */
  223. static int keeprunning = 0;
  224. static int started = 0;
  225. static Slapi_Mutex *cache_lock;
  226. static Slapi_Mutex *change_lock;
  227. static Slapi_Mutex *start_lock;
  228. static Slapi_Mutex *stop_lock;
  229. static Slapi_CondVar *something_changed = NULL;
  230. static Slapi_CondVar *start_cond = NULL;
  231. /*
  232. cos_cache_init
  233. --------------
  234. starts up the thread which waits for changes and
  235. fires off the cache re-creation when one is detected
  236. also registers vattr callbacks
  237. */
  238. int cos_cache_init()
  239. {
  240. int ret = 0;
  241. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_init\n",0,0,0);
  242. slapi_vattrcache_cache_none();
  243. cache_lock = slapi_new_mutex();
  244. change_lock = slapi_new_mutex();
  245. stop_lock = slapi_new_mutex();
  246. something_changed = slapi_new_condvar(change_lock);
  247. keeprunning =1;
  248. start_lock = slapi_new_mutex();
  249. start_cond = slapi_new_condvar(start_lock);
  250. started = 0;
  251. if ( stop_lock == NULL ||
  252. change_lock == NULL ||
  253. cache_lock == NULL ||
  254. stop_lock == NULL ||
  255. start_lock == NULL ||
  256. start_cond == NULL ||
  257. something_changed == NULL)
  258. {
  259. slapi_log_error( SLAPI_LOG_FATAL, COS_PLUGIN_SUBSYSTEM,
  260. "cos_cache_init: cannot create mutexes\n" );
  261. ret = -1;
  262. goto out;
  263. }
  264. /* grab the views interface */
  265. if(slapi_apib_get_interface(Views_v1_0_GUID, &views_api))
  266. {
  267. /* lets be tolerant if views is disabled */
  268. views_api = 0;
  269. }
  270. if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
  271. cos_cache_vattr_get,
  272. cos_cache_vattr_compare,
  273. cos_cache_vattr_types) != 0)
  274. {
  275. slapi_log_error( SLAPI_LOG_FATAL, COS_PLUGIN_SUBSYSTEM,
  276. "cos_cache_init: cannot register as service provider\n" );
  277. ret = -1;
  278. goto out;
  279. }
  280. if ( PR_CreateThread (PR_USER_THREAD,
  281. cos_cache_wait_on_change,
  282. NULL,
  283. PR_PRIORITY_NORMAL,
  284. PR_GLOBAL_THREAD,
  285. PR_UNJOINABLE_THREAD,
  286. SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL )
  287. {
  288. slapi_log_error( SLAPI_LOG_FATAL, COS_PLUGIN_SUBSYSTEM,
  289. "cos_cache_init: PR_CreateThread failed\n" );
  290. ret = -1;
  291. goto out;
  292. }
  293. /* wait for that thread to get started */
  294. if (ret == 0) {
  295. slapi_lock_mutex(start_lock);
  296. while (!started) {
  297. while (slapi_wait_condvar(start_cond, NULL) == 0);
  298. }
  299. slapi_unlock_mutex(start_lock);
  300. }
  301. out:
  302. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_init\n",0,0,0);
  303. return ret;
  304. }
  305. /*
  306. cos_cache_wait_on_change
  307. ------------------------
  308. sit around waiting on a notification that something has
  309. changed, then fires off the cache re-creation
  310. The way this stuff is written, we can look for the
  311. template for a definiton, before the template has been added--I think
  312. that's OK--we'll see it when it arrives--get this error message:
  313. "skipping cos definition cn=cosPointerGenerateSt,ou=People,o=cosAll--no templates found"
  314. */
  315. static void cos_cache_wait_on_change(void *arg)
  316. {
  317. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_wait_on_change thread\n",0,0,0);
  318. slapi_lock_mutex(stop_lock);
  319. slapi_lock_mutex(change_lock);
  320. /* first register our backend state change func (we'll use func pointer as handle) */
  321. slapi_register_backend_state_change((void *)cos_cache_backend_state_change, cos_cache_backend_state_change);
  322. pCache = 0;
  323. /* create initial cache */
  324. cos_cache_create();
  325. slapi_lock_mutex(start_lock);
  326. started = 1;
  327. slapi_notify_condvar(start_cond, 1);
  328. slapi_unlock_mutex(start_lock);
  329. while(keeprunning)
  330. {
  331. slapi_unlock_mutex(change_lock);
  332. slapi_lock_mutex(change_lock);
  333. if ( !cos_cache_notify_flag && keeprunning) {
  334. /*
  335. * Nothing to do right now, so go to sleep--as
  336. * we have the mutex, we are sure to wait before any modify
  337. * thread notifies our condvar, and so we will not miss any
  338. * notifications, including the shutdown notification.
  339. */
  340. slapi_wait_condvar( something_changed, NULL );
  341. } else {
  342. /* Something to do...do it below */
  343. }
  344. /*
  345. * We're here because:
  346. * 1. we were asleep and got a signal, on our condvar OR
  347. * 2. we were about to wait on the condvar and noticed that a modfiy
  348. * thread
  349. * had passed, setting the cos_cache_notify_flag and notifying us--
  350. * we did not wait in that case as we would have missed the notify
  351. * (notify when noone is waiting == no-op).
  352. * before we go running off doing lots of stuff lets check if we should stop
  353. */
  354. if(keeprunning) {
  355. cos_cache_create();
  356. }
  357. cos_cache_notify_flag = 0; /* Dealt with it */
  358. }/* while */
  359. /* shut down the cache */
  360. slapi_unlock_mutex(change_lock);
  361. slapi_unlock_mutex(stop_lock);
  362. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_wait_on_change thread exit\n",0,0,0);
  363. }
  364. /*
  365. cos_cache_create
  366. ---------------------
  367. Walks the definitions in the DIT and creates the cache.
  368. Once created, it swaps the new cache for the old one,
  369. releasing its refcount to the old cache and allowing it
  370. to be destroyed.
  371. */
  372. static int cos_cache_create()
  373. {
  374. int ret = -1;
  375. cosCache *pNewCache;
  376. static int firstTime = 1;
  377. int cache_built = 0;
  378. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_create\n",0,0,0);
  379. pNewCache = (cosCache*)slapi_ch_malloc(sizeof(cosCache));
  380. if(pNewCache)
  381. {
  382. pNewCache->pDefs = 0;
  383. pNewCache->refCount = 1; /* 1 is for us */
  384. pNewCache->vattr_cacheable = 0; /* default is not cacheable */
  385. ret = cos_cache_build_definition_list(&(pNewCache->pDefs), &(pNewCache->vattr_cacheable));
  386. if(!ret)
  387. {
  388. /* OK, we have a cache, lets add indexing for
  389. that faster than slow feeling */
  390. ret = cos_cache_index_all(pNewCache);
  391. if(ret == 0)
  392. {
  393. /* right, indexed cache, lets do our duty for the schema */
  394. ret = cos_cache_schema_build(pNewCache);
  395. if(ret == 0)
  396. {
  397. /* now to swap the new cache for the old cache */
  398. cosCache *pOldCache;
  399. slapi_lock_mutex(cache_lock);
  400. /* turn off caching until the old cache is done */
  401. if(pCache)
  402. {
  403. slapi_vattrcache_cache_none();
  404. /*
  405. * be sure not to uncache other stuff
  406. * like roles if there is no change in
  407. * state
  408. */
  409. if(pCache->vattr_cacheable)
  410. slapi_entrycache_vattrcache_watermark_invalidate();
  411. }
  412. else
  413. {
  414. if(pNewCache && pNewCache->vattr_cacheable)
  415. {
  416. slapi_vattrcache_cache_all();
  417. }
  418. }
  419. pOldCache = pCache;
  420. pCache = pNewCache;
  421. slapi_unlock_mutex(cache_lock);
  422. if(pOldCache)
  423. cos_cache_release(pOldCache);
  424. cache_built = 1;
  425. }
  426. else
  427. {
  428. /* we should not go on without proper schema checking */
  429. cos_cache_release(pNewCache);
  430. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_create: failed to cache the schema\n",0,0,0);
  431. }
  432. }
  433. else
  434. {
  435. /* currently we cannot go on without the indexes */
  436. cos_cache_release(pNewCache);
  437. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_create: failed to index cache\n",0,0,0);
  438. }
  439. }
  440. else
  441. {
  442. if(firstTime)
  443. {
  444. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_create: cos disabled\n",0,0,0);
  445. firstTime = 0;
  446. }
  447. slapi_ch_free((void**)&pNewCache);
  448. }
  449. }
  450. else
  451. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_create: memory allocation failure\n",0,0,0);
  452. /* make sure we have a new cache */
  453. if(!cache_built)
  454. {
  455. /* we do not have a new cache, must make sure the old cache is destroyed */
  456. cosCache *pOldCache;
  457. slapi_lock_mutex(cache_lock);
  458. slapi_vattrcache_cache_none();
  459. /*
  460. * be sure not to uncache other stuff
  461. * like roles if there is no change in
  462. * state
  463. */
  464. if(pCache && pCache->vattr_cacheable)
  465. slapi_entrycache_vattrcache_watermark_invalidate();
  466. pOldCache = pCache;
  467. pCache = NULL;
  468. slapi_unlock_mutex(cache_lock);
  469. if(pOldCache)
  470. cos_cache_release(pOldCache); /* release our reference to the old cache */
  471. }
  472. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_create\n",0,0,0);
  473. return ret;
  474. }
  475. /*
  476. cos_cache_build_definition_list
  477. -------------------------------
  478. builds the list of cos definitions by searching for them throughout the DIT
  479. */
  480. static int cos_cache_build_definition_list(cosDefinitions **pDefs, int *vattr_cacheable)
  481. {
  482. int ret = 0;
  483. Slapi_PBlock *pSuffixSearch = 0;
  484. Slapi_Entry **pSuffixList = 0;
  485. Slapi_Attr *suffixAttr;
  486. struct berval **suffixVals;
  487. char *attrType = 0;
  488. char *attrs[2];
  489. int suffixIndex = 0;
  490. int valIndex = 0;
  491. int cos_def_available = 0;
  492. static int firstTime = 1;
  493. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_build_definition_list\n",0,0,0);
  494. /*
  495. * The class of service definitions may be anywhere in the DIT,
  496. * so our first task is to find them.
  497. */
  498. attrs[0] = "namingcontexts";
  499. attrs[1] = 0;
  500. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos: Building class of service cache after status change.\n",0,0,0);
  501. /*
  502. * XXXrbyrne: this looks really inefficient--should be using
  503. * slapi_get_next_suffix(), rather than searching for namingcontexts.
  504. */
  505. pSuffixSearch = slapi_search_internal("",LDAP_SCOPE_BASE,"(objectclass=*)",NULL,attrs,0);
  506. if(pSuffixSearch)
  507. slapi_pblock_get( pSuffixSearch, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  508. if(!pSuffixSearch || ret != LDAP_SUCCESS) {
  509. LDAPDebug( LDAP_DEBUG_ANY,
  510. "cos_cache_build_definition_list: failed to find suffixes\n",
  511. 0,0,0);
  512. ret = -1;
  513. goto next;
  514. }
  515. /* iterate through the suffixes and search for cos definitions */
  516. slapi_pblock_get( pSuffixSearch, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  517. &pSuffixList );
  518. if(!pSuffixList) {
  519. goto next;
  520. }
  521. while(pSuffixList[suffixIndex])
  522. {
  523. if(!slapi_entry_first_attr(pSuffixList[suffixIndex], &suffixAttr))
  524. {
  525. do
  526. {
  527. attrType = 0;
  528. slapi_attr_get_type(suffixAttr, &attrType);
  529. if(attrType && !slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"namingcontexts"))
  530. {
  531. if(!slapi_attr_get_bervals_copy(suffixAttr, &suffixVals))
  532. {
  533. valIndex = 0;
  534. if(suffixVals)
  535. {
  536. while(suffixVals[valIndex])
  537. {
  538. /* here's a suffix, lets search it... */
  539. if(suffixVals[valIndex]->bv_val)
  540. {
  541. if(!cos_cache_add_dn_defs(suffixVals[valIndex]->bv_val ,pDefs))
  542. {
  543. *vattr_cacheable = -1;
  544. cos_def_available = 1;
  545. break;
  546. }
  547. }
  548. valIndex++;
  549. }
  550. ber_bvecfree( suffixVals );
  551. suffixVals = NULL;
  552. }
  553. }
  554. }
  555. } while(!slapi_entry_next_attr(pSuffixList[suffixIndex], suffixAttr, &suffixAttr));
  556. }
  557. suffixIndex++;
  558. }
  559. next:
  560. if(cos_def_available == 0)
  561. {
  562. if(firstTime)
  563. {
  564. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_build_definition_list: Found no cos definitions, cos disabled while waiting for updates\n",0,0,0);
  565. firstTime = 0;
  566. }
  567. ret = -1;
  568. }
  569. else
  570. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos: Class of service cache built.\n",0,0,0);
  571. /* clean up */
  572. if(pSuffixSearch)
  573. {
  574. slapi_free_search_results_internal(pSuffixSearch);
  575. slapi_pblock_destroy(pSuffixSearch);
  576. }
  577. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_build_definition_list\n",0,0,0);
  578. return ret;
  579. }
  580. /* struct to support search callback API */
  581. struct dn_defs_info {
  582. cosDefinitions **pDefs;
  583. int vattr_cacheable;
  584. int ret;
  585. };
  586. /*
  587. * Currently, always returns 0 to continue the search for definitions, even
  588. * if a particular attempt to add a definition fails: info.ret gets set to
  589. * zero only if we succeed to add a def.
  590. */
  591. static int
  592. cos_dn_defs_cb (Slapi_Entry* e, void *callback_data)
  593. {
  594. struct dn_defs_info *info;
  595. cosAttrValue **pSneakyVal = 0;
  596. cosAttrValue *pObjectclass = 0;
  597. cosAttrValue *pCosTargetTree = 0;
  598. cosAttrValue *pCosTemplateDn = 0;
  599. cosAttrValue *pCosSpecifier = 0;
  600. cosAttrValue *pCosAttribute = 0;
  601. cosAttrValue *pCosOverrides = 0;
  602. cosAttrValue *pCosOperational = 0;
  603. cosAttrValue *pCosOpDefault = 0;
  604. cosAttrValue *pCosMerge = 0;
  605. cosAttrValue *pDn = 0;
  606. struct berval **dnVals;
  607. int cosType = 0;
  608. int valIndex = 0;
  609. Slapi_Attr *dnAttr;
  610. char *attrType = 0;
  611. char *norm_dn = NULL;
  612. info=(struct dn_defs_info *)callback_data;
  613. cos_cache_add_attrval(&pDn, slapi_entry_get_dn(e));
  614. if(slapi_entry_first_attr(e, &dnAttr)) {
  615. goto bail;
  616. }
  617. do {
  618. attrType = 0;
  619. /* we need to fill in the details of the definition now */
  620. slapi_attr_get_type(dnAttr, &attrType);
  621. if(!attrType) {
  622. continue;
  623. }
  624. pSneakyVal = 0;
  625. if(!slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"objectclass"))
  626. pSneakyVal = &pObjectclass;
  627. else if(!slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"cosTargetTree")){
  628. if(pCosTargetTree){
  629. norm_dn = slapi_create_dn_string("%s", pCosTargetTree->val);
  630. if(norm_dn){
  631. slapi_ch_free_string(&pCosTargetTree->val);
  632. pCosTargetTree->val = norm_dn;
  633. }
  634. }
  635. pSneakyVal = &pCosTargetTree;
  636. } else if(!slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"cosTemplateDn"))
  637. pSneakyVal = &pCosTemplateDn;
  638. else if(!slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"cosSpecifier"))
  639. pSneakyVal = &pCosSpecifier;
  640. else if(!slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"cosAttribute"))
  641. pSneakyVal = &pCosAttribute;
  642. else if(!slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"cosIndirectSpecifier"))
  643. pSneakyVal = &pCosSpecifier;
  644. if(!pSneakyVal) {
  645. continue;
  646. }
  647. /* It's a type we're interested in */
  648. if(slapi_attr_get_bervals_copy(dnAttr, &dnVals)) {
  649. continue;
  650. }
  651. valIndex = 0;
  652. if(!dnVals) {
  653. continue;
  654. }
  655. for (valIndex = 0; dnVals[valIndex]; valIndex++)
  656. {
  657. if(!dnVals[valIndex]->bv_val) {
  658. continue;
  659. }
  660. /*
  661. parse any overide or default values
  662. and deal with them
  663. */
  664. if(pSneakyVal == &pCosAttribute)
  665. {
  666. int qualifier_hit = 0;
  667. int op_qualifier_hit = 0;
  668. int merge_schemes_qualifier_hit = 0;
  669. int override_qualifier_hit =0;
  670. int default_qualifier_hit = 0;
  671. int operational_default_qualifier_hit = 0;
  672. do
  673. {
  674. qualifier_hit = 0;
  675. if(cos_cache_backwards_stricmp_and_clip(dnVals[valIndex]->bv_val, " operational"))
  676. {
  677. /* matched */
  678. op_qualifier_hit = 1;
  679. qualifier_hit = 1;
  680. }
  681. if(cos_cache_backwards_stricmp_and_clip(dnVals[valIndex]->bv_val, " merge-schemes"))
  682. {
  683. /* matched */
  684. merge_schemes_qualifier_hit = 1;
  685. qualifier_hit = 1;
  686. }
  687. if(cos_cache_backwards_stricmp_and_clip(dnVals[valIndex]->bv_val, " override"))
  688. {
  689. /* matched */
  690. override_qualifier_hit = 1;
  691. qualifier_hit = 1;
  692. }
  693. if(cos_cache_backwards_stricmp_and_clip(dnVals[valIndex]->bv_val, " default")) {
  694. default_qualifier_hit = 1;
  695. qualifier_hit = 1;
  696. }
  697. if(cos_cache_backwards_stricmp_and_clip(dnVals[valIndex]->bv_val, " operational-default")) {
  698. operational_default_qualifier_hit = 1;
  699. qualifier_hit = 1;
  700. }
  701. }
  702. while(qualifier_hit == 1);
  703. /*
  704. * At this point, dnVals[valIndex]->bv_val
  705. * is the value of cosAttribute, stripped of
  706. * any qualifiers, so add this pure attribute type to
  707. * the appropriate lists.
  708. */
  709. if ( op_qualifier_hit ) {
  710. cos_cache_add_attrval(&pCosOperational,
  711. dnVals[valIndex]->bv_val);
  712. }
  713. if ( merge_schemes_qualifier_hit ) {
  714. cos_cache_add_attrval(&pCosMerge, dnVals[valIndex]->bv_val);
  715. }
  716. if ( override_qualifier_hit ) {
  717. cos_cache_add_attrval(&pCosOverrides,
  718. dnVals[valIndex]->bv_val);
  719. }
  720. if ( default_qualifier_hit ) {
  721. /* attr is added below in pSneakyVal, in any case */
  722. }
  723. if ( operational_default_qualifier_hit ) {
  724. cos_cache_add_attrval(&pCosOpDefault,
  725. dnVals[valIndex]->bv_val);
  726. }
  727. slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle,
  728. dnVals[valIndex]->bv_val, NULL, NULL);
  729. } /* if(attrType is cosAttribute) */
  730. /*
  731. * Add the attributetype to the appropriate
  732. * list.
  733. */
  734. cos_cache_add_attrval(pSneakyVal, dnVals[valIndex]->bv_val);
  735. }/* for (valIndex = 0; dnVals[valIndex]; valIndex++) */
  736. ber_bvecfree( dnVals );
  737. dnVals = NULL;
  738. } while(!slapi_entry_next_attr(e, dnAttr, &dnAttr));
  739. if (pCosAttribute && (!pCosTargetTree || !pCosTemplateDn)) {
  740. /* get the parent of the definition */
  741. char *orig = slapi_dn_parent(pDn->val);
  742. char *parent = NULL;
  743. if (orig) {
  744. parent = slapi_create_dn_string("%s", orig);
  745. if (!parent) {
  746. parent = orig;
  747. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  748. "cos_dn_defs_cb: "
  749. "failed to normalize parent dn %s. "
  750. "Adding the pre normalized dn.\n",
  751. parent);
  752. }
  753. if (!pCosTargetTree) {
  754. cos_cache_add_attrval(&pCosTargetTree, parent);
  755. }
  756. if (!pCosTemplateDn) {
  757. cos_cache_add_attrval(&pCosTemplateDn, parent);
  758. }
  759. if (parent != orig) {
  760. slapi_ch_free_string(&parent);
  761. }
  762. slapi_ch_free_string(&orig);
  763. } else {
  764. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  765. "cos_dn_defs_cb: "
  766. "failed to get parent dn of cos definition %s.\n",
  767. pDn->val);
  768. if (!pCosTemplateDn) {
  769. if (!pCosTargetTree) {
  770. LDAPDebug0Args(LDAP_DEBUG_ANY, "cosTargetTree and cosTemplateDn are not set.\n");
  771. } else {
  772. LDAPDebug0Args(LDAP_DEBUG_ANY, "cosTemplateDn is not set.\n");
  773. }
  774. } else if (!pCosTargetTree) {
  775. LDAPDebug0Args(LDAP_DEBUG_ANY, "cosTargetTree is not set.\n");
  776. }
  777. }
  778. }
  779. /*
  780. determine the type of class of service scheme
  781. */
  782. if(pObjectclass)
  783. {
  784. if(cos_cache_attrval_exists(pObjectclass, "cosDefinition"))
  785. {
  786. cosType = COSTYPE_CLASSIC;
  787. }
  788. else if(cos_cache_attrval_exists(pObjectclass, "cosClassicDefinition"))
  789. {
  790. cosType = COSTYPE_CLASSIC;
  791. }
  792. else if(cos_cache_attrval_exists(pObjectclass, "cosPointerDefinition"))
  793. {
  794. cosType = COSTYPE_POINTER;
  795. }
  796. else if(cos_cache_attrval_exists(pObjectclass, "cosIndirectDefinition"))
  797. {
  798. cosType = COSTYPE_INDIRECT;
  799. }
  800. else
  801. cosType = COSTYPE_BADTYPE;
  802. }
  803. /*
  804. we should now have a full definition,
  805. do some sanity checks because we don't
  806. want bogus entries in the cache
  807. then ship it
  808. */
  809. /* these must exist */
  810. if(pDn && pObjectclass &&
  811. (
  812. (cosType == COSTYPE_CLASSIC &&
  813. pCosTemplateDn &&
  814. pCosSpecifier &&
  815. pCosAttribute )
  816. ||
  817. (cosType == COSTYPE_POINTER &&
  818. pCosTemplateDn &&
  819. pCosAttribute )
  820. ||
  821. (cosType == COSTYPE_INDIRECT &&
  822. pCosSpecifier &&
  823. pCosAttribute )
  824. )
  825. )
  826. {
  827. int rc = 0;
  828. /*
  829. we'll leave the referential integrity stuff
  830. up to the referint plug-in and assume all
  831. is good - if it's not then we just have a
  832. useless definition and we'll nag copiously later.
  833. */
  834. char *pTmpDn = slapi_ch_strdup(pDn->val); /* because dn gets hosed on error */
  835. if(!(rc = cos_cache_add_defn(info->pDefs, &pDn, cosType,
  836. &pCosTargetTree, &pCosTemplateDn,
  837. &pCosSpecifier, &pCosAttribute,
  838. &pCosOverrides, &pCosOperational,
  839. &pCosMerge, &pCosOpDefault))) {
  840. info->ret = 0; /* we have succeeded to add the defn*/
  841. } else {
  842. /*
  843. * Failed but we will continue the search for other defs
  844. * Don't reset info->ret....it keeps track of any success
  845. */
  846. if ( rc == COS_DEF_ERROR_NO_TEMPLATES) {
  847. LDAPDebug(LDAP_DEBUG_ANY, "Skipping CoS Definition %s"
  848. "--no CoS Templates found, "
  849. "which should be added before the CoS Definition.\n",
  850. pTmpDn, 0, 0);
  851. } else {
  852. LDAPDebug(LDAP_DEBUG_ANY, "Skipping CoS Definition %s\n"
  853. "--error(%d)\n",
  854. pTmpDn, rc, 0);
  855. }
  856. }
  857. slapi_ch_free_string(&pTmpDn);
  858. }
  859. else
  860. {
  861. /*
  862. this definition is brain dead - bail
  863. if we have a dn use it to report, if not then *really* bad
  864. things are going on
  865. */
  866. if(pDn)
  867. {
  868. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_dn_defs: incomplete cos definition detected in %s, discarding from cache.\n",pDn->val,0,0);
  869. }
  870. else
  871. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_dn_defs: incomplete cos definition detected, no DN to report, discarding from cache.\n",0,0,0);
  872. if(pCosTargetTree)
  873. cos_cache_del_attrval_list(&pCosTargetTree);
  874. if(pCosTemplateDn)
  875. cos_cache_del_attrval_list(&pCosTemplateDn);
  876. if(pCosSpecifier)
  877. cos_cache_del_attrval_list(&pCosSpecifier);
  878. if(pCosAttribute)
  879. cos_cache_del_attrval_list(&pCosAttribute);
  880. if(pDn)
  881. cos_cache_del_attrval_list(&pDn);
  882. }
  883. bail:
  884. /* we don't keep the objectclasses, so lets free them */
  885. if(pObjectclass) {
  886. cos_cache_del_attrval_list(&pObjectclass);
  887. }
  888. /* This particular definition may not have yielded anything
  889. * worth caching (eg. no template was found for it) but
  890. * that should not cause us to abort the search for other more well behaved
  891. * definitions unless we are shutting down.
  892. * return info->ret;
  893. */
  894. return slapi_is_shutting_down();
  895. }
  896. /*
  897. cos_cache_add_dn_defs
  898. -------------------------
  899. takes a dn as argument and searches the dn for cos definitions,
  900. adding any found to the definition list. Change to use search callback API.
  901. Returns: 0: found at least one definition entry that got added to the
  902. cache successfully.
  903. non-zero: added no cos defs to the cache.
  904. */
  905. #define DN_DEF_FILTER "(&(|(objectclass=cosSuperDefinition)(objectclass=cosDefinition))(objectclass=ldapsubentry))"
  906. static int cos_cache_add_dn_defs(char *dn, cosDefinitions **pDefs)
  907. {
  908. Slapi_PBlock *pDnSearch = 0;
  909. struct dn_defs_info info = {NULL, 0, 0};
  910. pDnSearch = slapi_pblock_new();
  911. if (pDnSearch) {
  912. info.ret=-1; /* assume no good defs */
  913. info.pDefs=pDefs;
  914. slapi_search_internal_set_pb(pDnSearch, dn, LDAP_SCOPE_SUBTREE,
  915. DN_DEF_FILTER,NULL,0,
  916. NULL,NULL,cos_get_plugin_identity(),0);
  917. slapi_search_internal_callback_pb(pDnSearch,
  918. &info /* callback_data */,
  919. NULL/* result_callback */,
  920. cos_dn_defs_cb,
  921. NULL /* referral_callback */);
  922. slapi_pblock_destroy (pDnSearch);
  923. }
  924. return info.ret;
  925. }
  926. /* struct to support call back API */
  927. struct tmpl_info {
  928. cosAttrValue *pCosSpecifier;
  929. cosAttrValue *pAttrs;
  930. cosTemplates **pTmpls;
  931. int ret;
  932. };
  933. /*
  934. * Currently, always returns 0 to continue the search for templates, even
  935. * if a particular attempt to add a template fails: info.ret gets set to
  936. * zero only if we succeed to add at least one tmpl.
  937. */
  938. static int cos_dn_tmpl_entries_cb (Slapi_Entry* e, void *callback_data) {
  939. cosAttrValue *pDn = 0;
  940. cosAttrValue *pCosPriority = 0;
  941. cosAttributes *pAttributes = 0;
  942. cosAttrValue *pObjectclass = 0;
  943. cosAttrValue *pCosAttribute = 0;
  944. Slapi_Attr *dnAttr;
  945. struct berval **dnVals;
  946. int itsAnAttr = 0;
  947. int valIndex = 0;
  948. cosAttrValue **pSneakyVal = 0;
  949. char *attrType = 0;
  950. struct tmpl_info *info;
  951. info = (struct tmpl_info *)callback_data;
  952. pDn = 0;
  953. cos_cache_add_attrval(&pDn, slapi_entry_get_dn(e));
  954. pAttributes = 0;
  955. pObjectclass = 0;
  956. pCosPriority = 0;
  957. if(!slapi_entry_first_attr(e, &dnAttr))
  958. {
  959. int attrs_present = 0;
  960. do
  961. {
  962. attrType = 0;
  963. pCosAttribute = 0;
  964. /* we need to fill in the details of the template now */
  965. slapi_attr_get_type(dnAttr, &attrType);
  966. if(attrType)
  967. {
  968. itsAnAttr = 0;
  969. pSneakyVal = 0;
  970. if(!slapi_utf8casecmp((unsigned char*)attrType,
  971. (unsigned char*)"objectclass"))
  972. pSneakyVal = &pObjectclass;
  973. if(!slapi_utf8casecmp((unsigned char*)attrType,
  974. (unsigned char*)"cosPriority"))
  975. pSneakyVal = &pCosPriority;
  976. if(pSneakyVal == NULL)
  977. {
  978. /* look for the attribute in the dynamic attributes */
  979. if(cos_cache_attrval_exists(info->pAttrs, attrType))
  980. {
  981. pSneakyVal = &pCosAttribute;
  982. itsAnAttr = 1;
  983. attrs_present = 1;
  984. }
  985. }
  986. if(pSneakyVal)
  987. {
  988. if(!slapi_attr_get_bervals_copy(dnAttr, &dnVals))
  989. {
  990. valIndex = 0;
  991. if(dnVals)
  992. {
  993. while(dnVals[valIndex])
  994. {
  995. if(dnVals[valIndex]->bv_val)
  996. cos_cache_add_attrval(pSneakyVal,
  997. dnVals[valIndex]->bv_val);
  998. valIndex++;
  999. }
  1000. if(itsAnAttr)
  1001. {
  1002. /* got all vals, add this attribute to the attribute list */
  1003. cos_cache_add_attr(&pAttributes, attrType,
  1004. *pSneakyVal);
  1005. }
  1006. ber_bvecfree( dnVals );
  1007. dnVals = NULL;
  1008. }
  1009. }
  1010. }
  1011. }
  1012. } while(!slapi_entry_next_attr(e, dnAttr, &dnAttr));
  1013. /*
  1014. we should now have a full template,
  1015. do some sanity checks because we don't
  1016. want bogus entries in the cache if we can help it
  1017. - then ship it
  1018. */
  1019. /* these must exist */
  1020. if(
  1021. attrs_present &&
  1022. pObjectclass &&
  1023. pAttributes &&
  1024. pDn
  1025. )
  1026. {
  1027. /*
  1028. we'll leave the referential integrity stuff
  1029. up to the referint plug-in if set up and assume all
  1030. is good - if it's not then we just have a
  1031. useless definition and we'll nag copiously later.
  1032. */
  1033. if(!cos_cache_add_tmpl(info->pTmpls, pDn, pObjectclass,
  1034. info->pCosSpecifier, pAttributes,pCosPriority)){
  1035. info->ret = 0; /* we have succeed to add the tmpl */
  1036. } else {
  1037. /* Don't reset info->ret....it keeps track of any success */
  1038. LDAPDebug(LDAP_DEBUG_ANY, "cos_cache_add_dn_tmpls:"
  1039. "could not cache cos template %s\n",pDn,0,0);
  1040. }
  1041. }
  1042. else
  1043. {
  1044. /*
  1045. this template is brain dead - bail
  1046. if we have a dn use it to report, if not then *really* bad
  1047. things are going on
  1048. - of course it might not be a template, so lets
  1049. not tell the world unless the world wants to know,
  1050. we'll make it a plugin level message
  1051. */
  1052. if(pDn)
  1053. {
  1054. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_add_dn_tmpls: incomplete cos template detected in %s, discarding from cache.\n",pDn->val,0,0);
  1055. }
  1056. else
  1057. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_add_dn_tmpls: incomplete cos template detected, no DN to report, discarding from cache.\n",0,0,0);
  1058. if(pObjectclass)
  1059. cos_cache_del_attrval_list(&pObjectclass);
  1060. if(pCosAttribute)
  1061. cos_cache_del_attrval_list(&pCosAttribute);
  1062. if(pDn)
  1063. cos_cache_del_attrval_list(&pDn);
  1064. if(pAttributes)
  1065. cos_cache_del_attr_list(&pAttributes);
  1066. if(pCosPriority)
  1067. cos_cache_del_attrval_list(&pCosPriority);
  1068. }
  1069. }
  1070. /*
  1071. * Always continue the search even if a particular attempt
  1072. * to add a template failed unless we are shutting down
  1073. */
  1074. return slapi_is_shutting_down();
  1075. }
  1076. /*
  1077. cos_cache_add_dn_tmpls
  1078. -------------------------
  1079. takes a dn as argument and searches the dn for cos templates,
  1080. adding any found to the template list
  1081. This is the new version using call back search API
  1082. Returns: zero for success--found at least one good tmpl for this def.
  1083. non-zero: failed to add any templs for this def.
  1084. */
  1085. #define TMPL_FILTER "(&(objectclass=costemplate)(|(objectclass=costemplate)(objectclass=ldapsubentry)))"
  1086. static int cos_cache_add_dn_tmpls(char *dn, cosAttrValue *pCosSpecifier, cosAttrValue *pAttrs, cosTemplates **pTmpls)
  1087. {
  1088. void *plugin_id;
  1089. int scope;
  1090. struct tmpl_info info = {NULL, 0, 0};
  1091. Slapi_PBlock *pDnSearch = 0;
  1092. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_add_dn_tmpls\n",0,0,0);
  1093. /* no cos specifier means this is an indirect scheme */
  1094. if(pCosSpecifier)
  1095. scope = LDAP_SCOPE_ONELEVEL;
  1096. else
  1097. scope = LDAP_SCOPE_BASE;
  1098. /* Use new internal operation API */
  1099. pDnSearch = slapi_pblock_new();
  1100. plugin_id=cos_get_plugin_identity();
  1101. if (pDnSearch) {
  1102. info.pAttrs=pAttrs;
  1103. info.pTmpls=pTmpls;
  1104. info.pCosSpecifier=pCosSpecifier;
  1105. info.ret=-1; /* assume no good tmpls */
  1106. slapi_search_internal_set_pb(pDnSearch, dn, scope,
  1107. TMPL_FILTER,NULL,0,
  1108. NULL,NULL,plugin_id,0);
  1109. slapi_search_internal_callback_pb(pDnSearch,
  1110. &info /* callback_data */,
  1111. NULL/* result_callback */,
  1112. cos_dn_tmpl_entries_cb,
  1113. NULL /* referral_callback */);
  1114. slapi_pblock_destroy (pDnSearch);
  1115. }
  1116. /*
  1117. * info.ret comes out zero only if we succeed to add at least one
  1118. * tmpl to the cache.
  1119. */
  1120. return (info.ret);
  1121. }
  1122. /*
  1123. cos_cache_add_defn
  1124. ------------------
  1125. Add a cos definition to the list and create the template
  1126. cache for this definition
  1127. returns: 0: successfully added the definition to the cache
  1128. non-zero: failed to add the definition to the cache (eg. because
  1129. there was no template for it.)
  1130. ret==COS_DEF_ERROR_NO_TEMPLATES then no templs were found
  1131. for thsi def. We make a special case of this and pass the
  1132. back the error so we can roll two messages into one--this
  1133. is to reduce the number of error messages at cos definiton
  1134. load time--it is common to see the defs before the tmpls
  1135. arrive.
  1136. */
  1137. static int cos_cache_add_defn(
  1138. cosDefinitions **pDefs,
  1139. cosAttrValue **dn,
  1140. int cosType,
  1141. cosAttrValue **tree,
  1142. cosAttrValue **tmpDn,
  1143. cosAttrValue **spec,
  1144. cosAttrValue **pAttrs,
  1145. cosAttrValue **pOverrides,
  1146. cosAttrValue **pOperational,
  1147. cosAttrValue **pCosMerge,
  1148. cosAttrValue **pCosOpDefault
  1149. )
  1150. {
  1151. int ret = 0;
  1152. int tmplCount = 0;
  1153. cosDefinitions *theDef = 0;
  1154. cosAttrValue *pTmpTmplDn = 0;
  1155. cosAttrValue *pDummyAttrVal = 0;
  1156. cosAttrValue *pAttrsIter = 0;
  1157. cosAttributes *pDummyAttributes = 0;
  1158. cosAttrValue *pSpecsIter = 0;
  1159. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_add_defn\n",0,0,0);
  1160. if (!spec) {
  1161. LDAPDebug( LDAP_DEBUG_ANY, "missing spec\n",0,0,0);
  1162. ret = -1;
  1163. goto out;
  1164. }
  1165. pSpecsIter = *spec;
  1166. if (!tmpDn) {
  1167. LDAPDebug( LDAP_DEBUG_ANY, "missing tmpDn\n",0,0,0);
  1168. ret = -1;
  1169. goto out;
  1170. }
  1171. pTmpTmplDn = *tmpDn;
  1172. /* we don't want cosspecifiers that can be supplied by the same scheme */
  1173. while( pSpecsIter )
  1174. {
  1175. if( cos_cache_attrval_exists(*pAttrs, pSpecsIter->val ) )
  1176. {
  1177. /* no, this is not sane, lets reject the whole darn scheme in disgust */
  1178. LDAPDebug( LDAP_DEBUG_ANY, "cos definition %s supplies its own cosspecifier, rejecting scheme\n",(*dn)->val,0,0);
  1179. ret = -1;
  1180. }
  1181. pSpecsIter = pSpecsIter->list.pNext;
  1182. }
  1183. /* create the definition */
  1184. if(0 == ret)
  1185. {
  1186. theDef = (cosDefinitions*) slapi_ch_malloc(sizeof(cosDefinitions));
  1187. if(theDef)
  1188. {
  1189. theDef->pCosTmps = NULL;
  1190. /* process each template in turn */
  1191. LDAPDebug( LDAP_DEBUG_PLUGIN, "Processing cosDefinition %s\n",(*dn)->val,0,0);
  1192. while(pTmpTmplDn && cosType != COSTYPE_INDIRECT)
  1193. {
  1194. /* create the template */
  1195. if(!cos_cache_add_dn_tmpls(pTmpTmplDn->val, *spec, *pAttrs, &(theDef->pCosTmps)))
  1196. tmplCount++;
  1197. pTmpTmplDn = pTmpTmplDn->list.pNext;
  1198. }
  1199. if(tmplCount == 0 && cosType != COSTYPE_INDIRECT)
  1200. {
  1201. /* without our golden templates we are nothing
  1202. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_defn:"
  1203. "no templates for cos definition at %s.\n",(*dn)->val,0,0);*/
  1204. ret = COS_DEF_ERROR_NO_TEMPLATES;
  1205. }
  1206. else
  1207. {
  1208. if(cosType == COSTYPE_INDIRECT)
  1209. {
  1210. /*
  1211. Indirect cos schemes have no templates,
  1212. however, in order to take advantage of existing code
  1213. which is optimized to do a binary search on attributes
  1214. which are found through their templates, we add a dummy
  1215. template and dummy attributes. The value of the attributes
  1216. will be ignored when later assessing a query.
  1217. */
  1218. pAttrsIter = *pAttrs;
  1219. while(pAttrsIter)
  1220. {
  1221. pDummyAttrVal = NULL;
  1222. cos_cache_add_attrval(&pDummyAttrVal, "not used");
  1223. cos_cache_add_attr(&pDummyAttributes, pAttrsIter->val, pDummyAttrVal);
  1224. pAttrsIter = pAttrsIter->list.pNext;
  1225. }
  1226. cos_cache_add_attrval(tmpDn, "cn=dummy,");
  1227. cos_cache_add_tmpl(&(theDef->pCosTmps), *tmpDn, NULL, *spec, pDummyAttributes,NULL);
  1228. *tmpDn = 0;
  1229. }
  1230. theDef->pDn = *dn;
  1231. theDef->cosType = cosType;
  1232. theDef->pCosTargetTree = *tree;
  1233. theDef->pCosTemplateDn = *tmpDn;
  1234. theDef->pCosSpecifier = *spec;
  1235. theDef->pCosAttrs = *pAttrs;
  1236. theDef->pCosOverrides = *pOverrides;
  1237. theDef->pCosOperational = *pOperational;
  1238. theDef->pCosMerge = *pCosMerge;
  1239. theDef->pCosOpDefault = *pCosOpDefault;
  1240. cos_cache_add_ll_entry((void**)pDefs, theDef, NULL);
  1241. LDAPDebug( LDAP_DEBUG_PLUGIN, "Added cosDefinition %s\n",(*dn)->val,0,0);
  1242. }
  1243. }
  1244. else
  1245. {
  1246. ret = -1;
  1247. }
  1248. }
  1249. out:
  1250. if(ret < 0)
  1251. {
  1252. slapi_ch_free((void**)&theDef);
  1253. if(dn)
  1254. cos_cache_del_attrval_list(dn);
  1255. if(tree)
  1256. cos_cache_del_attrval_list(tree);
  1257. if(tmpDn)
  1258. cos_cache_del_attrval_list(tmpDn);
  1259. if(spec)
  1260. cos_cache_del_attrval_list(spec);
  1261. if(pAttrs)
  1262. cos_cache_del_attrval_list(pAttrs);
  1263. }
  1264. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_add_defn\n",0,0,0);
  1265. return ret;
  1266. }
  1267. /*
  1268. cos_cache_del_attrval_list
  1269. --------------------------
  1270. walks the list deleting as it goes
  1271. */
  1272. static void cos_cache_del_attrval_list(cosAttrValue **pVal)
  1273. {
  1274. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_del_attrval_list\n",0,0,0);
  1275. while(*pVal)
  1276. {
  1277. cosAttrValue *pTmp = (*pVal)->list.pNext;
  1278. slapi_ch_free((void**)&((*pVal)->val));
  1279. slapi_ch_free((void**)&(*pVal));
  1280. *pVal = pTmp;
  1281. }
  1282. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_del_attrval_list\n",0,0,0);
  1283. }
  1284. /*
  1285. cos_cache_add_attrval
  1286. ---------------------
  1287. adds a value to an attribute value list
  1288. */
  1289. static int
  1290. cos_cache_add_attrval(cosAttrValue **attrval, char *val)
  1291. {
  1292. int ret = 0;
  1293. cosAttrValue *theVal;
  1294. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_add_attrval\n",0,0,0);
  1295. /* create the attrvalue */
  1296. theVal = (cosAttrValue*) slapi_ch_malloc(sizeof(cosAttrValue));
  1297. if(theVal)
  1298. {
  1299. theVal->val = slapi_ch_strdup(val);
  1300. if(theVal->val)
  1301. {
  1302. cos_cache_add_ll_entry((void**)attrval, theVal, NULL);
  1303. }
  1304. else
  1305. {
  1306. slapi_ch_free((void**)&theVal);
  1307. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_attrval: failed to allocate memory\n",0,0,0);
  1308. ret = -1;
  1309. }
  1310. }
  1311. else
  1312. {
  1313. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_attrval: failed to allocate memory\n",0,0,0);
  1314. ret = -1;
  1315. }
  1316. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_add_attrval\n",0,0,0);
  1317. return ret;
  1318. }
  1319. /*
  1320. cos_cache_add_ll_entry - RECURSIVE for sorted lists
  1321. ---------------------------------------------------
  1322. if a compare function is passed as argument, the element
  1323. is added to the linked list in the sorted order according
  1324. to that compare functions algorithm. This prepares the list
  1325. for ultra fast indexing - requiring only to walk the list once
  1326. to build the index.
  1327. if no compare function is passed, the element is added
  1328. to the head of the linked list
  1329. the index is created after the linked list is complete,
  1330. and so is always null until explicitly indexed
  1331. *NOTE* this function assumes and *requires* that the structures
  1332. passed to it in "attrval" and "theVal" have a cosIndexedLinkedList
  1333. member, and it is the *first* member of the structure. This
  1334. is safe because this is a module level function, and all functions
  1335. which call this one are part of the same sub-system.
  1336. The compare function will also make a similar assumption, but
  1337. likely one that recognizes the full structure or type, it is
  1338. the responsibility of the caller to match input structures with
  1339. the appropriate compare function.
  1340. WARNING: current recursive sorting code never used, never tested
  1341. */
  1342. static void cos_cache_add_ll_entry(void** attrval, void *theVal, int ( *compare )(const void *elem1, const void *elem2 ))
  1343. {
  1344. static cosIndexedLinkedList *pLastList = 0;
  1345. static cosIndexedLinkedList *first_element;
  1346. static int call_count = 0;
  1347. call_count++;
  1348. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_add_ll_entry - recursion level %d\n",call_count,0,0);
  1349. /*
  1350. need to save the first element of the list
  1351. we update this first element whenever an entry
  1352. is added to the start of the list, this way
  1353. we can ensure that the head of the list is always
  1354. *attrval - callers pass us the head of the list
  1355. and can expect that what they get back is also
  1356. the head of the list
  1357. */
  1358. if(call_count == 1)
  1359. first_element = *attrval;
  1360. if(*attrval)
  1361. {
  1362. if(compare == NULL)
  1363. {
  1364. /* we dont want this list sorted */
  1365. /* push this to the start of the list (because its quick) */
  1366. ((cosIndexedLinkedList*)theVal)->pNext = *attrval;
  1367. ((cosIndexedLinkedList*)theVal)->index = NULL;
  1368. *attrval = theVal;
  1369. }
  1370. else
  1371. {
  1372. /* insert this in the list in sorted order
  1373. (because its quicker for building indexes later) */
  1374. int comp_ret = 0;
  1375. comp_ret = compare(*attrval, theVal);
  1376. if(comp_ret == 0 || comp_ret > 0)
  1377. {
  1378. /* insert prior to this element */
  1379. if(pLastList)
  1380. pLastList->pNext = theVal;
  1381. else
  1382. first_element = theVal;
  1383. ((cosIndexedLinkedList*)theVal)->pNext = *attrval;
  1384. ((cosIndexedLinkedList*)theVal)->index = NULL;
  1385. }
  1386. else
  1387. {
  1388. /* still looking - recurse on next element */
  1389. pLastList = (cosIndexedLinkedList*)attrval;
  1390. cos_cache_add_ll_entry(&(((cosIndexedLinkedList*)attrval)->pNext), theVal, compare);
  1391. }
  1392. if(call_count == 1)
  1393. *attrval = first_element;
  1394. }
  1395. }
  1396. else
  1397. {
  1398. /* new or end of list */
  1399. ((cosIndexedLinkedList*)theVal)->pNext = NULL;
  1400. ((cosIndexedLinkedList*)theVal)->index = NULL;
  1401. if(call_count == 1) /* if new list */
  1402. *attrval = theVal;
  1403. }
  1404. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_add_ll_entry - recursion level %d\n",call_count,0,0);
  1405. call_count--;
  1406. }
  1407. /*
  1408. cos_cache_getref
  1409. ----------------
  1410. retrieves a reference to the current cache and adds to the cache reference count
  1411. */
  1412. int cos_cache_getref(cos_cache **pptheCache)
  1413. {
  1414. int ret = -1;
  1415. static int first_time = 1;
  1416. cosCache **ppCache = (cosCache**)pptheCache;
  1417. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_getref\n",0,0,0);
  1418. if(first_time)
  1419. {
  1420. first_time = 0;
  1421. /* first customer, create the cache */
  1422. slapi_lock_mutex(change_lock);
  1423. if(pCache == NULL)
  1424. {
  1425. if(cos_cache_create())
  1426. {
  1427. /* there was a problem or no COS definitions were found */
  1428. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_getref: no cos cache created\n",0,0,0);
  1429. }
  1430. }
  1431. slapi_unlock_mutex(change_lock);
  1432. }
  1433. slapi_lock_mutex(cache_lock);
  1434. *ppCache = pCache;
  1435. if(pCache)
  1436. ret = ++((*ppCache)->refCount);
  1437. slapi_unlock_mutex(cache_lock);
  1438. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_getref\n",0,0,0);
  1439. return ret;
  1440. }
  1441. /*
  1442. cos_cache_addref
  1443. ----------------
  1444. adds 1 to the cache reference count
  1445. */
  1446. int cos_cache_addref(cos_cache *ptheCache)
  1447. {
  1448. int ret = 0;
  1449. cosCache *pCache = (cosCache*)ptheCache;
  1450. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_addref\n",0,0,0);
  1451. slapi_lock_mutex(cache_lock);
  1452. if(pCache)
  1453. ret = ++(pCache->refCount);
  1454. slapi_unlock_mutex(cache_lock);
  1455. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_addref\n",0,0,0);
  1456. return ret;
  1457. }
  1458. /*
  1459. cos_cache_release
  1460. -----------------
  1461. subtracts 1 from the cache reference count, if the count falls
  1462. below 1, the cache is destroyed.
  1463. */
  1464. int cos_cache_release(cos_cache *ptheCache)
  1465. {
  1466. int ret = 0;
  1467. int destroy = 0;
  1468. cosCache *pOldCache = (cosCache*)ptheCache;
  1469. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_release\n",0,0,0);
  1470. slapi_lock_mutex(cache_lock);
  1471. if(pOldCache)
  1472. {
  1473. ret = --(pOldCache->refCount);
  1474. if(ret == 0)
  1475. destroy = 1;
  1476. }
  1477. slapi_unlock_mutex(cache_lock);
  1478. if(destroy)
  1479. {
  1480. cosDefinitions *pDef = pOldCache->pDefs;
  1481. /* now is the first time it is
  1482. * safe to assess whether
  1483. * vattr caching can be turned on
  1484. */
  1485. if(pCache && pCache->vattr_cacheable)
  1486. {
  1487. slapi_vattrcache_cache_all();
  1488. }
  1489. /* destroy the cache here - no locking required, no references outstanding */
  1490. if(pDef)
  1491. cos_cache_del_schema(pOldCache);
  1492. while(pDef)
  1493. {
  1494. cosDefinitions *pTmpD = pDef;
  1495. cosTemplates *pCosTmps = pDef->pCosTmps;
  1496. while(pCosTmps)
  1497. {
  1498. cosTemplates *pTmpT = pCosTmps;
  1499. pCosTmps = pCosTmps->list.pNext;
  1500. cos_cache_del_attr_list(&(pTmpT->pAttrs));
  1501. cos_cache_del_attrval_list(&(pTmpT->pObjectclasses));
  1502. cos_cache_del_attrval_list(&(pTmpT->pDn));
  1503. slapi_ch_free((void**)&(pTmpT->cosGrade));
  1504. slapi_ch_free((void**)&pTmpT);
  1505. }
  1506. pDef = pDef->list.pNext;
  1507. cos_cache_del_attrval_list(&(pTmpD->pDn));
  1508. cos_cache_del_attrval_list(&(pTmpD->pCosTargetTree));
  1509. cos_cache_del_attrval_list(&(pTmpD->pCosTemplateDn));
  1510. cos_cache_del_attrval_list(&(pTmpD->pCosSpecifier));
  1511. cos_cache_del_attrval_list(&(pTmpD->pCosAttrs));
  1512. cos_cache_del_attrval_list(&(pTmpD->pCosOverrides));
  1513. cos_cache_del_attrval_list(&(pTmpD->pCosOperational));
  1514. cos_cache_del_attrval_list(&(pTmpD->pCosMerge));
  1515. cos_cache_del_attrval_list(&(pTmpD->pCosOpDefault));
  1516. slapi_ch_free((void**)&pTmpD);
  1517. }
  1518. if(pOldCache->ppAttrIndex)
  1519. slapi_ch_free((void**)&(pOldCache->ppAttrIndex));
  1520. if(pOldCache->ppTemplateList)
  1521. slapi_ch_free((void**)&(pOldCache->ppTemplateList));
  1522. slapi_ch_free((void**)&pOldCache);
  1523. }
  1524. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_release\n",0,0,0);
  1525. return ret;
  1526. }
  1527. /*
  1528. cos_cache_del_attr_list
  1529. -----------------------
  1530. walk the list deleting as we go
  1531. */
  1532. static void cos_cache_del_attr_list(cosAttributes **pAttrs)
  1533. {
  1534. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_del_attr_list\n",0,0,0);
  1535. while(*pAttrs)
  1536. {
  1537. cosAttributes *pTmp = (*pAttrs)->list.pNext;
  1538. cos_cache_del_attrval_list(&((*pAttrs)->pAttrValue));
  1539. slapi_ch_free((void**)&((*pAttrs)->pAttrName));
  1540. slapi_ch_free((void**)&(*pAttrs));
  1541. *pAttrs = pTmp;
  1542. }
  1543. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_del_attr_list\n",0,0,0);
  1544. }
  1545. /*
  1546. cos_cache_del_schema
  1547. --------------------
  1548. delete the object class lists used for schema checking
  1549. */
  1550. static void cos_cache_del_schema(cosCache *pCache)
  1551. {
  1552. char *pLastName = 0;
  1553. int attr_index = 0;
  1554. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_del_schema\n",0,0,0);
  1555. if(pCache && pCache->attrCount && pCache->ppAttrIndex)
  1556. {
  1557. pLastName = pCache->ppAttrIndex[0]->pAttrName;
  1558. for(attr_index=1; attr_index<pCache->attrCount; attr_index++)
  1559. {
  1560. if(slapi_utf8casecmp((unsigned char*)pCache->ppAttrIndex[attr_index]->pAttrName, (unsigned char*)pLastName))
  1561. {
  1562. /* remember what went before */
  1563. pLastName = pCache->ppAttrIndex[attr_index]->pAttrName;
  1564. /* then blow it away */
  1565. cos_cache_del_attrval_list(&(pCache->ppAttrIndex[attr_index]->pObjectclasses));
  1566. }
  1567. }
  1568. /* Finally, remove the first attribute's objectclass list */
  1569. cos_cache_del_attrval_list(&(pCache->ppAttrIndex[0]->pObjectclasses));
  1570. }
  1571. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_del_schema\n",0,0,0);
  1572. }
  1573. /*
  1574. cos_cache_add_attr
  1575. ------------------
  1576. Adds an attribute to the list
  1577. */
  1578. static int cos_cache_add_attr(cosAttributes **pAttrs, char *name, cosAttrValue *val)
  1579. {
  1580. int ret =0;
  1581. cosAttributes *theAttr;
  1582. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_add_attr\n",0,0,0);
  1583. /* create the attribute */
  1584. theAttr = (cosAttributes*) slapi_ch_malloc(sizeof(cosAttributes));
  1585. if(theAttr)
  1586. {
  1587. theAttr->pAttrValue = val;
  1588. theAttr->pObjectclasses = 0; /* schema issues come later */
  1589. theAttr->pAttrName = slapi_ch_strdup(name);
  1590. if(theAttr->pAttrName)
  1591. {
  1592. cos_cache_add_ll_entry((void**)pAttrs, theAttr, NULL);
  1593. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_add_attr: Added attribute %s\n",name,0,0);
  1594. }
  1595. else
  1596. {
  1597. slapi_ch_free((void**)&theAttr);
  1598. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_attr: failed to allocate memory\n",0,0,0);
  1599. ret = -1;
  1600. }
  1601. }
  1602. else
  1603. {
  1604. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_attr: failed to allocate memory\n",0,0,0);
  1605. ret = -1;
  1606. }
  1607. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_add_attr\n",0,0,0);
  1608. return ret;
  1609. }
  1610. /*
  1611. cos_cache_add_tmpl
  1612. ------------------
  1613. Adds a template to the list
  1614. */
  1615. static int cos_cache_add_tmpl(cosTemplates **pTemplates, cosAttrValue *dn, cosAttrValue *objclasses, cosAttrValue *pCosSpecifier, cosAttributes *pAttrs,cosAttrValue *cosPriority)
  1616. {
  1617. int ret =0;
  1618. cosTemplates *theTemp;
  1619. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_add_tmpl\n",0,0,0);
  1620. /* create the attribute */
  1621. theTemp = (cosTemplates*) slapi_ch_malloc(sizeof(cosTemplates));
  1622. if(theTemp)
  1623. {
  1624. char *grade = NULL;
  1625. int grade_index = 0;
  1626. int index = 0;
  1627. int template_default = 0;
  1628. char *ptr = NULL;
  1629. char *normed = slapi_create_dn_string("%s", dn->val);
  1630. if (normed) {
  1631. slapi_ch_free_string(&dn->val);
  1632. dn->val = normed;
  1633. } else {
  1634. LDAPDebug(LDAP_DEBUG_ANY,
  1635. "cos_cache_add_tmpl: failed to normalize dn %s. "
  1636. "Processing the pre normalized dn.\n",
  1637. dn->val, 0, 0);
  1638. }
  1639. grade = (char*)slapi_ch_malloc(strlen(dn->val)+1);
  1640. /* extract the cos grade */
  1641. ptr = strchr(dn->val, '=');
  1642. if (ptr)
  1643. {
  1644. int quotes = 0;
  1645. int lastindex = strlen(dn->val) - 1;
  1646. index = ptr - dn->val + 1;
  1647. /* copy the grade (supports one level of quote nesting in rdn) */
  1648. while(dn->val[index] != ',' || dn->val[index-1] == '\\' || quotes == 1)
  1649. {
  1650. if(dn->val[index] == '"')
  1651. {
  1652. if(quotes == 0)
  1653. quotes = 1;
  1654. else
  1655. quotes = 0;
  1656. }
  1657. else
  1658. {
  1659. if(dn->val[index] == '\\')
  1660. {
  1661. if ((index+2 <= lastindex) && isxdigit(dn->val[index+1]) &&
  1662. isxdigit(dn->val[index+2])) {
  1663. /* Convert ESC HEX HEX to a real char */
  1664. int n = slapi_hexchar2int(dn->val[index+1]);
  1665. int n2 = slapi_hexchar2int(dn->val[index+2]);
  1666. n = (n << 4) + n2;
  1667. if (n == 0) { /* don't change \00 */
  1668. grade[grade_index] = dn->val[index++]; /* '\\' */
  1669. grade_index++;
  1670. grade[grade_index] = dn->val[index++]; /* 0 */
  1671. grade_index++;
  1672. grade[grade_index] = dn->val[index]; /* 0 */
  1673. grade_index++;
  1674. } else {
  1675. grade[grade_index] = n;
  1676. index += 2;
  1677. grade_index++;
  1678. }
  1679. } /* else: skip escape chars */
  1680. }
  1681. else
  1682. {
  1683. grade[grade_index] = dn->val[index];
  1684. grade_index++;
  1685. }
  1686. }
  1687. index++;
  1688. }
  1689. grade[grade_index] = '\0';
  1690. /* ok, grade copied, is it the default template? */
  1691. if(pCosSpecifier) /* some schemes don't have one */
  1692. {
  1693. char tmpGrade[BUFSIZ];
  1694. if (strlen(pCosSpecifier->val) < (sizeof(tmpGrade) - 9)) { /* 9 for "-default" */
  1695. strcpy(tmpGrade, pCosSpecifier->val);
  1696. strcat(tmpGrade, "-default");
  1697. if(!slapi_utf8casecmp((unsigned char*)grade, (unsigned char*)tmpGrade))
  1698. template_default = 1;
  1699. }
  1700. else
  1701. {
  1702. /*
  1703. * We shouldn't pass ever through this code as we expect
  1704. * pCosSpecifier values to be reasonably smaller than BUFSIZ
  1705. *
  1706. * only 2 lines of code -> no need to set an indirect char *
  1707. * duplicate the lines of code for clearness instead
  1708. */
  1709. char * newTmpGrade = PR_smprintf("%s-default", pCosSpecifier->val);
  1710. if(!slapi_utf8casecmp((unsigned char*)grade, (unsigned char*)newTmpGrade))
  1711. template_default = 1;
  1712. PR_smprintf_free(newTmpGrade);
  1713. }
  1714. }
  1715. }
  1716. else
  1717. {
  1718. /* mmm, should never get here, it means the dn is whacky */
  1719. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_tmpl: malformed dn detected: %s\n",dn,0,0);
  1720. grade[0] = '\0';
  1721. }
  1722. /* now fill in the blanks */
  1723. theTemp->pDn = dn;
  1724. theTemp->pObjectclasses = objclasses;
  1725. theTemp->pAttrs = pAttrs;
  1726. theTemp->cosGrade = slapi_ch_strdup(grade);
  1727. theTemp->template_default = template_default;
  1728. theTemp->cosPriority = (unsigned long)-1;
  1729. if(cosPriority)
  1730. {
  1731. theTemp->cosPriority = atol(cosPriority->val);
  1732. cos_cache_del_attrval_list(&cosPriority);
  1733. }
  1734. cos_cache_add_ll_entry((void**)pTemplates, theTemp, NULL);
  1735. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_add_tmpl: Added template %s\n",dn->val,0,0);
  1736. slapi_ch_free((void**)&grade);
  1737. }
  1738. else
  1739. {
  1740. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_add_tmpl: failed to allocate memory\n",0,0,0);
  1741. ret = -1;
  1742. }
  1743. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_add_tmpl\n",0,0,0);
  1744. return ret;
  1745. }
  1746. /*
  1747. cos_cache_attrval_exists
  1748. ------------------------
  1749. performs linear search on the list for a
  1750. cosAttrValue that matches val
  1751. however, if the "index" member of cosAttrValue
  1752. is non-null then a binary search is performed
  1753. on the index {PARPAR: TO BE DONE}
  1754. return 1 on found, 0 otherwise
  1755. */
  1756. static int cos_cache_attrval_exists(cosAttrValue *pAttrs, const char *val)
  1757. {
  1758. int ret = 0;
  1759. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_attrval_exists\n",0,0,0);
  1760. while(pAttrs)
  1761. {
  1762. if(!slapi_utf8casecmp((unsigned char*)pAttrs->val,(unsigned char*)val))
  1763. {
  1764. ret = 1;
  1765. break;
  1766. }
  1767. pAttrs = pAttrs->list.pNext;
  1768. }
  1769. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_attrval_exists\n",0,0,0);
  1770. return ret;
  1771. }
  1772. static int cos_cache_vattr_get(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, char *type, Slapi_ValueSet** results,int *type_name_disposition, char** actual_type_name, int flags, int *free_flags, void *hint)
  1773. {
  1774. cos_cache *pCache = 0;
  1775. int indirect_cos = 0;
  1776. int ret = -1;
  1777. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_vattr_get\n",0,0,0);
  1778. if(cos_cache_getref(&pCache) < 1)
  1779. {
  1780. /* problems we are hosed */
  1781. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_vattr_get: failed to get class of service reference\n",0,0,0);
  1782. goto bail;
  1783. }
  1784. ret = cos_cache_query_attr(pCache, c, e, type, results, NULL, NULL, NULL, &indirect_cos);
  1785. if(ret == 0)
  1786. {
  1787. if(indirect_cos){
  1788. /* we can't cache indirect cos */
  1789. *free_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
  1790. } else {
  1791. *free_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES | SLAPI_VIRTUALATTRS_VALUES_CACHEABLE;
  1792. }
  1793. *actual_type_name = slapi_ch_strdup(type);
  1794. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
  1795. }
  1796. cos_cache_release(pCache);
  1797. bail:
  1798. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_vattr_get\n",0,0,0);
  1799. return ret;
  1800. }
  1801. static int cos_cache_vattr_compare(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, char *type, Slapi_Value *test_this, int* result, int flags, void *hint)
  1802. {
  1803. int ret = -1;
  1804. cos_cache *pCache = 0;
  1805. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_vattr_compare\n",0,0,0);
  1806. if(cos_cache_getref(&pCache) < 1)
  1807. {
  1808. /* problems we are hosed */
  1809. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_vattr_compare: failed to get class of service reference\n",0,0,0);
  1810. goto bail;
  1811. }
  1812. ret = cos_cache_query_attr(pCache, c, e, type, NULL, test_this, result, NULL, NULL);
  1813. cos_cache_release(pCache);
  1814. bail:
  1815. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_vattr_compare\n",0,0,0);
  1816. return ret;
  1817. }
  1818. /*
  1819. * this imp is damn slow
  1820. *
  1821. */
  1822. static int cos_cache_vattr_types(vattr_sp_handle *handle,Slapi_Entry *e,
  1823. vattr_type_list_context *type_context,int flags)
  1824. {
  1825. int ret = 0;
  1826. int index = 0;
  1827. cosCache *pCache;
  1828. char *lastattr = "thisisfakeforcos";
  1829. int props = 0;
  1830. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_vattr_types\n",0,0,0);
  1831. if(cos_cache_getref((cos_cache **)&pCache) < 1)
  1832. {
  1833. /* problems we are hosed */
  1834. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_vattr_types: failed to get class of service reference\n",0,0,0);
  1835. goto bail;
  1836. }
  1837. while(index < pCache->attrCount )
  1838. {
  1839. if(slapi_utf8casecmp(
  1840. (unsigned char *)pCache->ppAttrIndex[index]->pAttrName,
  1841. (unsigned char *)lastattr))
  1842. {
  1843. lastattr = pCache->ppAttrIndex[index]->pAttrName;
  1844. if(1 == cos_cache_query_attr(pCache, NULL, e, lastattr, NULL, NULL,
  1845. NULL, &props, NULL))
  1846. {
  1847. /* entry contains this attr */
  1848. vattr_type_thang thang = {0};
  1849. thang.type_name = lastattr;
  1850. thang.type_flags = props;
  1851. slapi_vattrspi_add_type(type_context,&thang,0);
  1852. }
  1853. }
  1854. index++;
  1855. }
  1856. cos_cache_release(pCache);
  1857. bail:
  1858. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_vattr_types\n",0,0,0);
  1859. return ret;
  1860. }
  1861. /*
  1862. cos_cache_query_attr
  1863. --------------------
  1864. queries the cache to determine if this entry
  1865. should have an attribute of "type", and if so,
  1866. returns the attribute values in "vals" - which
  1867. should be subsequently freed by a call to
  1868. cos_cache_query_attr_free()
  1869. returns
  1870. 0 on success, we added a computed attribute
  1871. 1 on outright failure
  1872. > LDAP ERROR CODE
  1873. -1 when doesn't know about attribute
  1874. {PARPAR} must also check the attribute does not exist if we are not
  1875. overriding and allow the DS logic to pick it up by denying knowledge
  1876. of attribute
  1877. */
  1878. static int cos_cache_query_attr(cos_cache *ptheCache, vattr_context *context,
  1879. Slapi_Entry *e, char *type, Slapi_ValueSet **out_attr,
  1880. Slapi_Value *test_this, int *result, int *props,
  1881. int *indirect_cos)
  1882. {
  1883. int ret = -1;
  1884. cosCache *pCache = (cosCache*)ptheCache;
  1885. char *pDn = 0;
  1886. Slapi_Attr *pObjclasses = 0;
  1887. int attr_index = 0; /* for looping through attributes */
  1888. int attr_matched_index = 0; /* for identifying the matched attribute */
  1889. int hit = 0;
  1890. cosAttributes *pDefAttr = 0;
  1891. Slapi_Value *val;
  1892. /* int type_name_disposition;
  1893. char *actual_type_name;
  1894. int flags = 0;
  1895. int free_flags;*/
  1896. Slapi_Attr *pTmpVals;
  1897. int using_default = 0;
  1898. int entry_has_value = 0;
  1899. int merge_mode = 0;
  1900. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_query_attr\n",0,0,0);
  1901. if(out_attr)
  1902. *out_attr = 0;
  1903. /*
  1904. to perform this operation we need to know:
  1905. * if we know about the attribute, if not just exit
  1906. --
  1907. * dn,to determine its relevancy to any cos definition,
  1908. it must be a child of cosTargetTree
  1909. --
  1910. * objectclasses,to determine if the cos attribute will
  1911. violate schema (only when schema checking is on)
  1912. --
  1913. * class of service specifier, for matching definitions
  1914. - cosSpecifier is the attribute name and is used to
  1915. determine the cosDefinition to use, its value determines
  1916. the template to use
  1917. --
  1918. * the cosAttribute(s) (from the cosDefinition(s)) that match
  1919. the attribute name.
  1920. ($$)If these have a postfix of "default", then it is the same
  1921. as no postfix i.e. this acts as the default value. If it has
  1922. a postfix of "override", then the value in the matching template
  1923. is used regardless of any value stored in the entry. This has
  1924. been worked out previously so we can use a bool indicator in
  1925. the cosDefinition structure to determine what to do.
  1926. --
  1927. * the value of the attribute in the entry -
  1928. if present it overrides any default template value (see $$)
  1929. Also this ordering ensures least work done to fail (in this
  1930. implementation)
  1931. */
  1932. /** attribute **/
  1933. /*
  1934. lets be sure we need to do something
  1935. most of the time we probably don't
  1936. */
  1937. attr_index = cos_cache_find_attr(pCache, type);
  1938. if(attr_index == -1)
  1939. {
  1940. /* we don't know about this attribute */
  1941. goto bail;
  1942. }
  1943. /*
  1944. if there is a value in the entry the outcome
  1945. of the cos attribute resolution may be different
  1946. */
  1947. slapi_entry_attr_find(e, type, &pTmpVals);
  1948. if(pTmpVals)
  1949. entry_has_value = 1;
  1950. /** dn **/
  1951. pDn = slapi_entry_get_dn(e);
  1952. if(pDn == 0)
  1953. {
  1954. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_query_attr: failed to get entry dn\n",0,0,0);
  1955. ret = 1;
  1956. goto bail;
  1957. }
  1958. slapi_dn_normalize( pDn );
  1959. /** objectclasses **/
  1960. if(pCache->ppAttrIndex[attr_index]->attr_operational == 0 && config_get_schemacheck() &&
  1961. pCache->ppAttrIndex[attr_index]->attr_operational_default == 0)
  1962. {
  1963. /* does this entry violate schema? */
  1964. if(slapi_entry_attr_find( e, "objectclass", &pObjclasses ))
  1965. {
  1966. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_query_attr: failed to get objectclass from %s\n",pDn,0,0);
  1967. goto bail;
  1968. }
  1969. if(!cos_cache_schema_check(pCache, attr_index, pObjclasses))
  1970. {
  1971. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos_cache_query_attr: cos attribute %s failed schema check on dn: %s\n",type,pDn,0);
  1972. goto bail;
  1973. }
  1974. }
  1975. /** class of service specifier **/
  1976. /*
  1977. Now we need to iterate through the attributes to discover
  1978. if one fits all the criteria, we'll take the first that does
  1979. and blow off the rest unless the definition has merge-scheme
  1980. set.
  1981. */
  1982. do
  1983. {
  1984. /* for convenience, define some pointers */
  1985. cosAttributes *pAttr = pCache->ppAttrIndex[attr_index];
  1986. cosTemplates *pTemplate = (cosTemplates*)pAttr->pParent;
  1987. cosDefinitions *pDef = (cosDefinitions*)pTemplate->pParent;
  1988. cosAttrValue *pTargetTree = pDef->pCosTargetTree;
  1989. /* now for the tests */
  1990. /* would we be allowed to supply this attribute if we had one? */
  1991. if(entry_has_value && pAttr->attr_override == 0 && pAttr->attr_operational == 0)
  1992. {
  1993. /* answer: no, move on to the next attribute */
  1994. attr_index++;
  1995. continue;
  1996. }
  1997. /* if we are in merge_mode, can the attribute be merged? */
  1998. if(merge_mode && pAttr->attr_cos_merge == 0)
  1999. {
  2000. /* answer: no, move on to the next attribute */
  2001. attr_index++;
  2002. continue;
  2003. }
  2004. /* If we haven't found a hit yet, or if we are in merge mode, look for
  2005. * hits. We only check if this entry is a child of the target tree(s). */
  2006. while((hit == 0 || merge_mode) && pTargetTree)
  2007. {
  2008. if( pTargetTree->val == 0 ||
  2009. slapi_dn_issuffix(pDn, pTargetTree->val) != 0 ||
  2010. (views_api && views_entry_exists(views_api, pTargetTree->val, e)) /* might be in a view */
  2011. )
  2012. {
  2013. cosAttrValue *pSpec = pDef->pCosSpecifier;
  2014. Slapi_ValueSet *pAttrSpecs = 0;
  2015. /* Does this entry have a correct cosSpecifier? */
  2016. do
  2017. {
  2018. int type_name_disposition = 0;
  2019. char *actual_type_name = 0;
  2020. int free_flags = 0;
  2021. if(pSpec && pSpec->val) {
  2022. ret = slapi_vattr_values_get_sp(context, e, pSpec->val, &pAttrSpecs, &type_name_disposition, &actual_type_name, 0, &free_flags);
  2023. /* MAB: We need to free actual_type_name here !!!
  2024. XXX BAD--should use slapi_vattr_values_free() */
  2025. slapi_ch_free((void **) &actual_type_name);
  2026. }
  2027. if(pAttrSpecs || pDef->cosType == COSTYPE_POINTER)
  2028. {
  2029. int index = 0;
  2030. /* does the cosSpecifier value correspond to this template? */
  2031. if(pDef->cosType == COSTYPE_INDIRECT)
  2032. {
  2033. /*
  2034. it always does correspond for indirect schemes (it's a dummy value)
  2035. now we must follow the dn of our pointer and retrieve a value to
  2036. return
  2037. Note: we support one dn only, the result of multiple pointers is undefined
  2038. */
  2039. Slapi_Value *indirectdn;
  2040. Slapi_ValueSet *tmp_vals = NULL;
  2041. int pointer_flags = 0;
  2042. int hint = 0;
  2043. hint = slapi_valueset_first_value( pAttrSpecs, &indirectdn );
  2044. if(props)
  2045. pointer_flags = *props;
  2046. while (indirectdn != NULL)
  2047. {
  2048. if (cos_cache_follow_pointer( context, (char*)slapi_value_get_string(indirectdn),
  2049. type, &tmp_vals, test_this, result, pointer_flags) == 0)
  2050. {
  2051. if(indirect_cos){
  2052. *indirect_cos = 1;
  2053. }
  2054. hit = 1;
  2055. /* If the caller requested values, set them. We need
  2056. * to append values when we follow multiple pointers DNs. */
  2057. if (out_attr && tmp_vals)
  2058. {
  2059. if (*out_attr)
  2060. {
  2061. Slapi_Attr *attr = NULL;
  2062. Slapi_Value *val = NULL;
  2063. int idx = 0;
  2064. /* Create an attr to use for duplicate detection. */
  2065. attr = slapi_attr_new();
  2066. slapi_attr_init(attr, type);
  2067. /* Copy any values into out_attr if they don't already exist. */
  2068. for (idx = slapi_valueset_first_value(tmp_vals, &val);
  2069. val && (idx != -1);
  2070. idx = slapi_valueset_next_value(tmp_vals, idx, &val))
  2071. {
  2072. if (slapi_valueset_find(attr, *out_attr, val) == NULL)
  2073. {
  2074. slapi_valueset_add_value(*out_attr, val);
  2075. }
  2076. }
  2077. slapi_attr_free(&attr);
  2078. slapi_valueset_free(tmp_vals);
  2079. tmp_vals = NULL;
  2080. } else {
  2081. *out_attr = tmp_vals;
  2082. tmp_vals = NULL;
  2083. }
  2084. }
  2085. }
  2086. /* If this definition has merge-scheme set, we
  2087. * need to follow the rest of the pointers. */
  2088. if (pAttr->attr_cos_merge)
  2089. {
  2090. hint = slapi_valueset_next_value(pAttrSpecs, hint, &indirectdn);
  2091. } else {
  2092. indirectdn = NULL;
  2093. }
  2094. }
  2095. /* If merge-scheme is specified, set merge mode. This will allow
  2096. * us to merge in values from other CoS definitions for this attr. */
  2097. if (pAttr->attr_cos_merge)
  2098. {
  2099. merge_mode = 1;
  2100. attr_matched_index = attr_index;
  2101. }
  2102. }
  2103. else
  2104. {
  2105. if(pDef->cosType != COSTYPE_POINTER)
  2106. index = slapi_valueset_first_value( pAttrSpecs, &val );
  2107. while(pDef->cosType == COSTYPE_POINTER || val)
  2108. {
  2109. if(pDef->cosType == COSTYPE_POINTER || !slapi_utf8casecmp((unsigned char*)pTemplate->cosGrade, (unsigned char*)slapi_value_get_string(val)))
  2110. {
  2111. /* we have a hit */
  2112. if(out_attr)
  2113. {
  2114. if(cos_cache_cos_2_slapi_valueset(pAttr, out_attr) == 0)
  2115. hit = 1;
  2116. else
  2117. {
  2118. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_query_attr: could not create values to return\n",0,0,0);
  2119. goto bail;
  2120. }
  2121. if(pAttr->attr_cos_merge)
  2122. {
  2123. merge_mode = 1;
  2124. attr_matched_index = attr_index;
  2125. }
  2126. }
  2127. else
  2128. {
  2129. if(test_this && result)
  2130. {
  2131. /* compare op */
  2132. if(cos_cache_cmp_attr(pAttr, test_this, result))
  2133. {
  2134. hit = 1;
  2135. }
  2136. }
  2137. else
  2138. {
  2139. /* well, this must be a request for type only */
  2140. hit = 1;
  2141. }
  2142. }
  2143. break;
  2144. }
  2145. if(pDef->cosType != COSTYPE_POINTER)
  2146. index = slapi_valueset_next_value( pAttrSpecs, index, &val );
  2147. }
  2148. }
  2149. }
  2150. if(pSpec)
  2151. pSpec = pSpec->list.pNext;
  2152. } while(hit == 0 && pSpec);
  2153. /* MAB: We need to free pAttrSpecs here !!!
  2154. XXX BAD--should use slapi_vattr_values_free()*/
  2155. slapi_valueset_free(pAttrSpecs);
  2156. /* is the cosTemplate the default template? */
  2157. if(hit == 0 && pTemplate->template_default && !pDefAttr)
  2158. {
  2159. /* then lets save the attr in case we need it later */
  2160. pDefAttr = pAttr;
  2161. }
  2162. }
  2163. pTargetTree = pTargetTree->list.pNext;
  2164. } /* while(hit == 0 && pTargetTree) */
  2165. if(hit==0 || merge_mode)
  2166. attr_index++;
  2167. } while(
  2168. (hit == 0 || merge_mode) &&
  2169. pCache->attrCount > attr_index &&
  2170. !slapi_utf8casecmp((unsigned char*)type, (unsigned char*)pCache->ppAttrIndex[attr_index]->pAttrName)
  2171. );
  2172. if(!merge_mode)
  2173. attr_matched_index = attr_index;
  2174. /* should we use a default value? */
  2175. if(hit == 0 && pDefAttr)
  2176. {
  2177. /* we have a hit */
  2178. using_default = 1;
  2179. if(out_attr)
  2180. {
  2181. if(cos_cache_cos_2_slapi_valueset(pDefAttr, out_attr) == 0)
  2182. hit = 1;
  2183. else
  2184. {
  2185. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_query_attr: could not create values to return\n",0,0,0);
  2186. goto bail;
  2187. }
  2188. }
  2189. else
  2190. {
  2191. if(test_this && result)
  2192. {
  2193. /* compare op */
  2194. if(cos_cache_cmp_attr(pDefAttr, test_this, result))
  2195. hit = 1;
  2196. }
  2197. else
  2198. {
  2199. /* well, this must be a request for type only and the entry gets default template value */
  2200. hit = 1;
  2201. }
  2202. }
  2203. }
  2204. if(hit == 1 && out_attr == NULL && test_this == NULL)
  2205. ret = 1;
  2206. else if(hit == 1)
  2207. ret = 0;
  2208. else
  2209. ret = -1;
  2210. if(props)
  2211. *props = 0;
  2212. if(hit == 1 && props) {
  2213. if (
  2214. (using_default && pDefAttr &&
  2215. ((pDefAttr->attr_operational == 1) || (pDefAttr->attr_operational_default == 1))) ||
  2216. (!using_default && pCache && pCache->ppAttrIndex && pCache->ppAttrIndex[attr_matched_index] &&
  2217. ((pCache->ppAttrIndex[attr_matched_index]->attr_operational == 1) ||
  2218. (pCache->ppAttrIndex[attr_matched_index]->attr_operational_default == 1))))
  2219. {
  2220. /* this is an operational attribute, lets mark it so */
  2221. *props |= SLAPI_ATTR_FLAG_OPATTR;
  2222. }
  2223. }
  2224. bail:
  2225. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_query_attr\n",0,0,0);
  2226. return ret;
  2227. }
  2228. /*
  2229. cos_cache_find_attr
  2230. -------------------
  2231. searches for the attribute "type", and if found returns the index
  2232. of the first occurrance of the attribute in the cache top level
  2233. indexed attribute list.
  2234. */
  2235. static int cos_cache_find_attr(cosCache *pCache, char *type)
  2236. {
  2237. int ret = -1; /* assume failure */
  2238. cosAttributes attr;
  2239. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_find_attr\n",0,0,0);
  2240. attr.pAttrName = type;
  2241. if(pCache->attrCount-1 != 0)
  2242. ret = cos_cache_attr_index_bsearch(pCache, &attr, 0, pCache->attrCount-1);
  2243. else
  2244. {
  2245. /* only one attribute (that will fool our bsearch) lets check it here */
  2246. if(!slapi_utf8casecmp((unsigned char*)type, (unsigned char*)(pCache->ppAttrIndex)[0]->pAttrName))
  2247. {
  2248. ret = 0;
  2249. }
  2250. }
  2251. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_find_attr\n",0,0,0);
  2252. return ret;
  2253. }
  2254. /*
  2255. cos_cache_schema_check
  2256. ----------------------
  2257. check those object classes which match in the input list and the
  2258. cached set for allowed attribute types
  2259. return non-null for schema matches, zero otherwise
  2260. */
  2261. static int cos_cache_schema_check(cosCache *pCache, int attr_index, Slapi_Attr *pObjclasses)
  2262. {
  2263. int ret = 0; /* assume failure */
  2264. Slapi_Value *val;
  2265. int hint;
  2266. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_schema_check\n",0,0,0);
  2267. hint = slapi_attr_first_value( pObjclasses, &val );
  2268. while(hint != -1)
  2269. {
  2270. ret = cos_cache_attrval_exists(pCache->ppAttrIndex[attr_index]->pObjectclasses, (char*) slapi_value_get_string(val));
  2271. if(ret)
  2272. break;
  2273. hint = slapi_attr_next_value( pObjclasses, hint, &val );
  2274. }
  2275. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_schema_check\n",0,0,0);
  2276. return ret;
  2277. }
  2278. /*
  2279. cos_cache_schema_build
  2280. ----------------------
  2281. For each attribute in our global cache add the objectclasses which allow it.
  2282. This may be referred to later to check schema is not being violated.
  2283. */
  2284. static int cos_cache_schema_build(cosCache *pCache)
  2285. {
  2286. int ret = 0; /* we assume success, with operational attributes not supplied in schema we might fail otherwise */
  2287. struct objclass *oc;
  2288. char *pLastName = 0;
  2289. cosAttrValue *pLastRef = 0;
  2290. int attr_index = 0;
  2291. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_schema_build\n",0,0,0);
  2292. if(!config_get_schemacheck())
  2293. ret = 0;
  2294. /*
  2295. it is expected that in all but the most hard core cases, the number of
  2296. objectclasses will out number the attributes we look after - so we make
  2297. the objectclasses the outside loop. However, even if they are not, we
  2298. perform binary searches on the attribute list anyway, so it should be
  2299. considerably faster to search than the linked list of objectclasses (even
  2300. with the string comparisons going on)
  2301. */
  2302. oc_lock_read();
  2303. for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next )
  2304. {
  2305. char **pppAttrs[2];
  2306. int index;
  2307. int attrType = 0;
  2308. pppAttrs[0] = oc->oc_required;
  2309. pppAttrs[1] = oc->oc_allowed;
  2310. /* we need to check both required and allowed attributes I think */
  2311. while(attrType < 2)
  2312. {
  2313. if(pppAttrs[attrType])
  2314. {
  2315. index = 0;
  2316. while(pppAttrs[attrType][index])
  2317. {
  2318. attr_index = cos_cache_find_attr(pCache, pppAttrs[attrType][index]);
  2319. if(attr_index != -1)
  2320. {
  2321. /*
  2322. this attribute is one of ours, add this
  2323. objectclass to the objectclass list
  2324. note the index refers to the first
  2325. occurrence of this attribute in the list,
  2326. later we will copy over references to this
  2327. list to all the other attribute duplicates.
  2328. */
  2329. cos_cache_add_attrval(&(pCache->ppAttrIndex[attr_index]->pObjectclasses), oc->oc_name);
  2330. ret = 0;
  2331. }
  2332. index++;
  2333. }
  2334. }
  2335. attrType++;
  2336. }
  2337. }
  2338. oc_unlock();
  2339. /*
  2340. OK, now we need to add references to the real
  2341. lists to the duplicate attribute entries.
  2342. (this allows the schema check to be a little
  2343. less complex and just a little quicker)
  2344. */
  2345. pLastName = pCache->ppAttrIndex[0]->pAttrName;
  2346. pLastRef = pCache->ppAttrIndex[0]->pObjectclasses;
  2347. for(attr_index=1; attr_index<pCache->attrCount; attr_index++)
  2348. {
  2349. if(!slapi_utf8casecmp((unsigned char*)pCache->ppAttrIndex[attr_index]->pAttrName, (unsigned char*)pLastName))
  2350. {
  2351. /* copy over reference */
  2352. pCache->ppAttrIndex[attr_index]->pObjectclasses = pLastRef;
  2353. }
  2354. else
  2355. {
  2356. /* remember what went before */
  2357. pLastName = pCache->ppAttrIndex[attr_index]->pAttrName;
  2358. pLastRef = pCache->ppAttrIndex[attr_index]->pObjectclasses;
  2359. }
  2360. }
  2361. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_schema_build\n",0,0,0);
  2362. return ret;
  2363. }
  2364. /*
  2365. cos_cache_index_all
  2366. -------------------
  2367. Indexes every attribute in the cache for fast binary lookup
  2368. on attributes from the top level of the cache.
  2369. Also fixes up all parent pointers so that a single attribute
  2370. lookup will allow access to all information regarding that attribute.
  2371. Attributes that appear more than once in the cache will also
  2372. be indexed more than once - this means that a pure binary
  2373. search is not possible, but it is possible to make use of a
  2374. duplicate entry aware binary search function - which are rare beasts,
  2375. so we'll need to provide cos_cache_attr_bsearch()
  2376. This is also a handy time to mark the attributes as overides if
  2377. necessary
  2378. */
  2379. static int cos_cache_index_all(cosCache *pCache)
  2380. {
  2381. int ret = -1;
  2382. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_index_all\n",0,0,0);
  2383. /*
  2384. first fixup the index array so we can use qsort()
  2385. also fixup the parent pointers
  2386. */
  2387. pCache->ppTemplateList = 0;
  2388. pCache->templateCount = 0;
  2389. pCache->ppAttrIndex = 0;
  2390. pCache->attrCount = cos_cache_total_attr_count(pCache);
  2391. if(pCache->attrCount && pCache->templateCount)
  2392. {
  2393. int tmpindex = 0;
  2394. int cmpindex = 0;
  2395. int actualCount = 0;
  2396. pCache->ppAttrIndex = (cosAttributes**)slapi_ch_malloc(sizeof(cosAttributes*) * pCache->attrCount);
  2397. pCache->ppTemplateList = (char**)slapi_ch_calloc((pCache->templateCount + 1) * 2, sizeof(char*));
  2398. if(pCache->ppAttrIndex && pCache->ppTemplateList)
  2399. {
  2400. int attrcount = 0;
  2401. cosDefinitions *pDef = pCache->pDefs;
  2402. cosAttrValue *pAttrVal = 0;
  2403. while(pDef)
  2404. {
  2405. cosTemplates *pCosTmps = pDef->pCosTmps;
  2406. while(pCosTmps)
  2407. {
  2408. cosAttributes *pAttrs = pCosTmps->pAttrs;
  2409. pCosTmps->pParent = pDef;
  2410. while(pAttrs)
  2411. {
  2412. pAttrs->pParent = pCosTmps;
  2413. (pCache->ppAttrIndex)[attrcount] = pAttrs;
  2414. if(cos_cache_attrval_exists(pDef->pCosOverrides, pAttrs->pAttrName))
  2415. pAttrs->attr_override = 1;
  2416. else
  2417. pAttrs->attr_override = 0;
  2418. if(cos_cache_attrval_exists(pDef->pCosOperational, pAttrs->pAttrName))
  2419. pAttrs->attr_operational = 1;
  2420. else
  2421. pAttrs->attr_operational = 0;
  2422. if(cos_cache_attrval_exists(pDef->pCosMerge, pAttrs->pAttrName))
  2423. pAttrs->attr_cos_merge = 1;
  2424. else
  2425. pAttrs->attr_cos_merge = 0;
  2426. if(cos_cache_attrval_exists(pDef->pCosOpDefault, pAttrs->pAttrName))
  2427. pAttrs->attr_operational_default = 1;
  2428. else
  2429. pAttrs->attr_operational_default = 0;
  2430. attrcount++;
  2431. pAttrs = pAttrs->list.pNext;
  2432. }
  2433. pCosTmps = pCosTmps->list.pNext;
  2434. }
  2435. /*
  2436. we need to build the template dn list too,
  2437. we are going to take care that we do not
  2438. add duplicate dns or dns that have
  2439. ancestors elsewhere in the list since this
  2440. list will be binary searched (with a special
  2441. BS alg) to find an ancestor tree for a target
  2442. that has been modified - that comes later in
  2443. this function however - right now we'll just
  2444. slap them in the list
  2445. */
  2446. pAttrVal = pDef->pCosTemplateDn;
  2447. while(pAttrVal)
  2448. {
  2449. char *normed = slapi_create_dn_string("%s", pAttrVal->val);
  2450. if (normed) {
  2451. slapi_ch_free_string(&pAttrVal->val);
  2452. pAttrVal->val = normed;
  2453. } else {
  2454. LDAPDebug(LDAP_DEBUG_ANY,
  2455. "cos_cache_index_all: failed to normalize dn %s. "
  2456. "Processing the pre normalized dn.\n",
  2457. pAttrVal->val, 0, 0);
  2458. }
  2459. pCache->ppTemplateList[tmpindex] = pAttrVal->val;
  2460. tmpindex++;
  2461. pAttrVal = pAttrVal->list.pNext;
  2462. }
  2463. pDef = pDef->list.pNext;
  2464. }
  2465. /* now sort the index array */
  2466. qsort(pCache->ppAttrIndex, attrcount, sizeof(cosAttributes*), cos_cache_attr_compare);
  2467. qsort(pCache->ppTemplateList, tmpindex, sizeof(char*), cos_cache_string_compare);
  2468. /*
  2469. now we have the sorted template dn list, we can get rid of
  2470. duplicates and entries that have an ancestor elsewhere in
  2471. the list - all this in the name of faster searches
  2472. */
  2473. /* first go through zapping the useless PARPAR - THIS DOES NOT WORK */
  2474. tmpindex = 1;
  2475. cmpindex = 0;
  2476. actualCount = pCache->templateCount;
  2477. while(tmpindex < pCache->templateCount)
  2478. {
  2479. if(
  2480. !slapi_utf8casecmp((unsigned char*)pCache->ppTemplateList[tmpindex],(unsigned char*)pCache->ppTemplateList[cmpindex]) ||
  2481. slapi_dn_issuffix(pCache->ppTemplateList[tmpindex], pCache->ppTemplateList[cmpindex])
  2482. )
  2483. {
  2484. /* this guy is a waste of space */
  2485. pCache->ppTemplateList[tmpindex] = 0;
  2486. actualCount--;
  2487. }
  2488. else
  2489. cmpindex = tmpindex;
  2490. tmpindex++;
  2491. }
  2492. /* now shuffle everything up to the front to cover the bald spots */
  2493. tmpindex = 1;
  2494. cmpindex = 0;
  2495. while(tmpindex < pCache->templateCount)
  2496. {
  2497. if(pCache->ppTemplateList[tmpindex] != 0)
  2498. {
  2499. if(cmpindex)
  2500. {
  2501. pCache->ppTemplateList[cmpindex] = pCache->ppTemplateList[tmpindex];
  2502. pCache->ppTemplateList[tmpindex] = 0;
  2503. cmpindex++;
  2504. }
  2505. }
  2506. else
  2507. {
  2508. if(cmpindex == 0)
  2509. cmpindex = tmpindex;
  2510. }
  2511. tmpindex++;
  2512. }
  2513. pCache->templateCount = actualCount;
  2514. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos: cos cache index built\n",0,0,0);
  2515. ret = 0;
  2516. }
  2517. else
  2518. {
  2519. if(pCache->ppAttrIndex)
  2520. slapi_ch_free((void**)(&pCache->ppAttrIndex));
  2521. if(pCache->ppTemplateList)
  2522. slapi_ch_free((void**)(&pCache->ppTemplateList));
  2523. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_index_all: failed to allocate index memory\n",0,0,0);
  2524. }
  2525. }
  2526. else
  2527. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_index_all: no attributes to index\n",0,0,0);
  2528. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_index_all\n",0,0,0);
  2529. return ret;
  2530. }
  2531. /*
  2532. cos_cache_total_attr_count
  2533. --------------------------
  2534. walks the entire cache counting all attributes
  2535. note: this is coded so that it may be called
  2536. prior to the cache indexing of attributes - in
  2537. fact it is called by the code that creates the
  2538. index. Once indexing has been performed, it is
  2539. *much* *much* faster to get the count from the
  2540. cache object itself - cosCache::attrCount.
  2541. Additionally - a side effect is that the template
  2542. target trees are counted and placed in the cache level
  2543. target tree count - probably should be renamed,
  2544. but lets let it slide for now
  2545. returns the number of attributes counted
  2546. */
  2547. static int cos_cache_total_attr_count(cosCache *pCache)
  2548. {
  2549. int count = 0;
  2550. cosDefinitions *pDef = pCache->pDefs;
  2551. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_total_attr_count\n",0,0,0);
  2552. pCache->templateCount = 0;
  2553. while(pDef)
  2554. {
  2555. cosTemplates *pCosTmps = pDef->pCosTmps;
  2556. while(pCosTmps)
  2557. {
  2558. cosAttributes *pAttrs = pCosTmps->pAttrs;
  2559. while(pAttrs)
  2560. {
  2561. count++;
  2562. pAttrs = pAttrs->list.pNext;
  2563. }
  2564. pCache->templateCount++;
  2565. pCosTmps = pCosTmps->list.pNext;
  2566. }
  2567. pDef = pDef->list.pNext;
  2568. }
  2569. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_total_attr_count\n",0,0,0);
  2570. return count;
  2571. }
  2572. static int cos_cache_attr_compare(const void *e1, const void *e2)
  2573. {
  2574. int com_Result;
  2575. cosAttributes *pAttr = (*(cosAttributes**)e1);
  2576. cosTemplates *pTemplate = (cosTemplates*)pAttr->pParent;
  2577. cosAttributes *pAttr1 = (*(cosAttributes**)e2);
  2578. cosTemplates *pTemplate1 = (cosTemplates*)pAttr1->pParent;
  2579. /* Now compare the names of the attributes */
  2580. com_Result = slapi_utf8casecmp((unsigned char*)(*(cosAttributes**)e1)->pAttrName,(unsigned char*)(*(cosAttributes**)e2)->pAttrName);
  2581. if(0 == com_Result){
  2582. /* Now compare the cosPriorities */
  2583. com_Result = pTemplate->cosPriority - pTemplate1->cosPriority;
  2584. }
  2585. if(0 == com_Result)
  2586. return -1;
  2587. return com_Result;
  2588. }
  2589. static int cos_cache_string_compare(const void *e1, const void *e2)
  2590. {
  2591. return slapi_utf8casecmp((*(unsigned char**)e1),(*(unsigned char**)e2));
  2592. }
  2593. static int cos_cache_template_index_compare(const void *e1, const void *e2)
  2594. {
  2595. int ret = 0;
  2596. if(0 == slapi_dn_issuffix((const char*)e1,*(const char**)e2))
  2597. ret = slapi_utf8casecmp(*(unsigned char**)e2,(unsigned char*)e1);
  2598. else
  2599. ret = 0;
  2600. return ret;
  2601. }
  2602. /*
  2603. cos_cache_template_index_bsearch
  2604. --------------------------------
  2605. searches the template dn index for a match
  2606. */
  2607. static int cos_cache_template_index_bsearch(const char *dn)
  2608. {
  2609. int ret = 0;
  2610. cosCache *pCache;
  2611. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_template_index_bsearch\n",0,0,0);
  2612. if(-1 != cos_cache_getref((cos_cache**)&pCache))
  2613. {
  2614. if(bsearch(dn, pCache->ppTemplateList, pCache->templateCount, sizeof(char*), cos_cache_template_index_compare))
  2615. ret = 1;
  2616. cos_cache_release((cos_cache*)pCache);
  2617. }
  2618. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_template_index_bsearch\n",0,0,0);
  2619. return ret;
  2620. }
  2621. /*
  2622. cos_cache_attr_index_bsearch - RECURSIVE
  2623. ----------------------------------------
  2624. performs a binary search on the cache attribute index
  2625. return -1 if key is not found
  2626. the index into attribute index array of the first occurrance
  2627. of that attribute type otherwise
  2628. */
  2629. static int cos_cache_attr_index_bsearch( const cosCache *pCache, const cosAttributes *key, int lower, int upper )
  2630. {
  2631. int ret = -1;
  2632. int index = 0;
  2633. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_attr_index_bsearch\n",0,0,0);
  2634. if(upper >= lower)
  2635. {
  2636. if(upper != 0)
  2637. index = ((upper-lower)/2) + lower;
  2638. else
  2639. index = 0;
  2640. ret = slapi_utf8casecmp((unsigned char*)key->pAttrName, (unsigned char*)(pCache->ppAttrIndex)[index]->pAttrName);
  2641. if(ret == 0)
  2642. {
  2643. /*
  2644. we have a match, backtrack to the
  2645. first occurrance of this attribute
  2646. type
  2647. */
  2648. do
  2649. {
  2650. index--;
  2651. if(index >= 0)
  2652. ret = slapi_utf8casecmp((unsigned char*)key->pAttrName, (unsigned char*)(pCache->ppAttrIndex)[index]->pAttrName);
  2653. } while(index >= 0 && ret == 0);
  2654. index++;
  2655. }
  2656. else
  2657. {
  2658. /* seek elsewhere */
  2659. if(ret < 0)
  2660. {
  2661. /* take the low road */
  2662. index = cos_cache_attr_index_bsearch(pCache, key, lower, index-1);
  2663. }
  2664. else
  2665. {
  2666. /* go high */
  2667. index = cos_cache_attr_index_bsearch(pCache, key, index+1, upper);
  2668. }
  2669. }
  2670. }
  2671. else
  2672. index = -1;
  2673. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_attr_index_bsearch\n",0,0,0);
  2674. return index;
  2675. }
  2676. static int cos_cache_cmp_attr(cosAttributes *pAttr, Slapi_Value *test_this, int *result)
  2677. {
  2678. int ret = 0;
  2679. int index = 0;
  2680. cosAttrValue *pAttrVal = pAttr->pAttrValue;
  2681. char *the_cmp = (char *)slapi_value_get_string(test_this);
  2682. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_cmp_attr\n",0,0,0);
  2683. *result = 0;
  2684. while( pAttrVal )
  2685. {
  2686. ret = 1; /* CoS attribute exists; return 1 */
  2687. if(!slapi_utf8casecmp((unsigned char*)the_cmp, (unsigned char*)pAttrVal->val))
  2688. {
  2689. /* compare match */
  2690. *result = 1;
  2691. break;
  2692. }
  2693. pAttrVal = pAttrVal->list.pNext;
  2694. index++;
  2695. }
  2696. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_cmp_attr\n",0,0,0);
  2697. return ret;
  2698. }
  2699. /*
  2700. cos_cache_cos_2_slapi_attr
  2701. ----------------------
  2702. converts a cosAttributes structure to a Slapi_Attribute
  2703. */
  2704. static int cos_cache_cos_2_slapi_valueset(cosAttributes *pAttr, Slapi_ValueSet **out_vs)
  2705. {
  2706. int ret = 0;
  2707. int index = 0;
  2708. cosAttrValue *pAttrVal = pAttr->pAttrValue;
  2709. int add_mode = 0;
  2710. static Slapi_Attr *attr = 0; /* allocated once, never freed */
  2711. static int done_once = 0;
  2712. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_cos_2_slapi_attr\n",0,0,0);
  2713. /* test out_vs for existing values */
  2714. if(*out_vs)
  2715. {
  2716. add_mode = 1;
  2717. if(!done_once)
  2718. {
  2719. attr = slapi_attr_new(); /* lord knows why this is needed by slapi_valueset_find*/
  2720. slapi_attr_init(attr, "cos-bogus");
  2721. done_once = 1;
  2722. }
  2723. }
  2724. else
  2725. *out_vs = slapi_valueset_new();
  2726. if(*out_vs)
  2727. {
  2728. if(!add_mode)
  2729. slapi_valueset_init(*out_vs);
  2730. while( pAttrVal )
  2731. {
  2732. Slapi_Value *val = slapi_value_new_string(pAttrVal->val);
  2733. if(val) {
  2734. if(!add_mode || !slapi_valueset_find(attr, *out_vs, val)) {
  2735. slapi_valueset_add_value_ext(*out_vs, val, SLAPI_VALUE_FLAG_PASSIN);
  2736. }
  2737. else {
  2738. slapi_value_free(&val);
  2739. }
  2740. }
  2741. else
  2742. {
  2743. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_cos_2_slapi_attr: memory allocation error\n",0,0,0);
  2744. ret = -1;
  2745. goto bail;
  2746. }
  2747. pAttrVal = pAttrVal->list.pNext;
  2748. index++;
  2749. }
  2750. }
  2751. else
  2752. {
  2753. LDAPDebug( LDAP_DEBUG_ANY, "cos_cache_cos_2_slapi_attr: memory allocation error\n",0,0,0);
  2754. ret = -1;
  2755. }
  2756. bail:
  2757. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_cos_2_slapi_attr\n",0,0,0);
  2758. return ret;
  2759. }
  2760. /*
  2761. cos_cache_change_notify
  2762. -----------------------
  2763. determines if the change effects the cache and if so
  2764. signals a rebuild.
  2765. XXXrbyrne This whole mechanism needs to be revisited--it means that
  2766. the modifying client gets his LDAP response, and an unspecified and
  2767. variable
  2768. period of time later, his mods get taken into account in the cos cache.
  2769. This makes it hard to program reliable admin tools for COS--DSAME
  2770. has already indicated this is an issue for them.
  2771. Additionally, it regenerates the _whole_ cache even for eeny weeny mods--
  2772. does it really neeed to ? Additionally, in order to ensure we
  2773. do not miss any mods, we may tend to regen the cache, even if we've already
  2774. taken a mod into account in an earlier regeneration--currently there is no
  2775. way to know we've already dealt with the mod.
  2776. The right thing is something like: figure out what's being changed
  2777. and change only that in the cos cache and do it _before_ the response
  2778. goes to the client....or do a task that he can poll.
  2779. */
  2780. void cos_cache_change_notify(Slapi_PBlock *pb)
  2781. {
  2782. const char *dn;
  2783. Slapi_DN *sdn = NULL;
  2784. int do_update = 0;
  2785. struct slapi_entry *e;
  2786. Slapi_Backend *be=NULL;
  2787. int rc = 0;
  2788. int optype = -1;
  2789. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_change_notify\n",0,0,0);
  2790. /* Don't update local cache when remote entries are updated. */
  2791. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  2792. if ((be && (slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA))) ||
  2793. (NULL == be)) {
  2794. goto bail;
  2795. }
  2796. /* need to work out if a cache rebuild is necessary */
  2797. if(slapi_pblock_get( pb, SLAPI_TARGET_SDN, &sdn )) {
  2798. LDAPDebug0Args(LDAP_DEBUG_ANY, "cos_cache_change_notify: "
  2799. "failed to get dn of changed entry\n");
  2800. goto bail;
  2801. }
  2802. dn = slapi_sdn_get_dn(sdn);
  2803. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  2804. if (0 != rc) {
  2805. /* The operation did not succeed. As far as the cos cache is concerned, no need to update anything */
  2806. goto bail;
  2807. }
  2808. /*
  2809. * For DELETE, MODIFY, MODRDN: see if the pre-op entry was cos significant.
  2810. * For ADD, MODIFY, MODRDN: see if the post-op was cos significant.
  2811. * Touching a cos significant entry triggers the update
  2812. * of the whole cache.
  2813. */
  2814. slapi_pblock_get ( pb, SLAPI_OPERATION_TYPE, &optype );
  2815. if ( optype == SLAPI_OPERATION_DELETE ||
  2816. optype == SLAPI_OPERATION_MODIFY ||
  2817. optype == SLAPI_OPERATION_MODRDN ) {
  2818. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &e);
  2819. if ( cos_cache_entry_is_cos_related(e)) {
  2820. do_update = 1;
  2821. }
  2822. }
  2823. if ( !do_update &&
  2824. (optype == SLAPI_OPERATION_ADD ||
  2825. optype == SLAPI_OPERATION_MODIFY ||
  2826. optype == SLAPI_OPERATION_MODRDN )) {
  2827. /* Adds have null pre-op entries */
  2828. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  2829. if ( cos_cache_entry_is_cos_related(e)) {
  2830. do_update = 1;
  2831. }
  2832. }
  2833. /*
  2834. * Check if this was an entry in a template tree (dn contains
  2835. * the old dn value).
  2836. * It's only relevant for indirect templates, which will
  2837. * not usually contain the "objectclass: costemplate" pair
  2838. * and so will not get detected by the above code.
  2839. * In fact, everything would still work fine if
  2840. * we just ignored a mod of one of these indirect templates,
  2841. * as we do not cache values from them, but the advantage of
  2842. * triggering an update here is that
  2843. * we can maintain the invariant that we only ever cache
  2844. * definitions that have _valid_ templates--the active cache
  2845. * stays lean in the face of errors.
  2846. */
  2847. if( !do_update && cos_cache_template_index_bsearch(dn)) {
  2848. LDAPDebug1Arg(LDAP_DEBUG_PLUGIN, "cos_cache_change_notify: "
  2849. "updating due to indirect template change(%s)\n", dn);
  2850. do_update = 1;
  2851. }
  2852. /* Do the update if required */
  2853. if(do_update)
  2854. {
  2855. slapi_lock_mutex(change_lock);
  2856. slapi_notify_condvar( something_changed, 1 );
  2857. cos_cache_notify_flag = 1;
  2858. slapi_unlock_mutex(change_lock);
  2859. }
  2860. bail:
  2861. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_change_notify\n",0,0,0);
  2862. }
  2863. /*
  2864. cos_cache_stop
  2865. --------------
  2866. notifies the cache thread we are stopping
  2867. */
  2868. void cos_cache_stop()
  2869. {
  2870. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_stop\n",0,0,0);
  2871. /* first deregister our state change func */
  2872. slapi_unregister_backend_state_change((void *)cos_cache_backend_state_change);
  2873. slapi_lock_mutex(change_lock);
  2874. keeprunning = 0;
  2875. slapi_notify_condvar( something_changed, 1 );
  2876. slapi_unlock_mutex(change_lock);
  2877. /* wait on shutdown */
  2878. slapi_lock_mutex(stop_lock);
  2879. /* release the caches reference to the cache */
  2880. cos_cache_release(pCache);
  2881. slapi_destroy_mutex(cache_lock); cache_lock = NULL;
  2882. slapi_destroy_mutex(change_lock); change_lock = NULL;
  2883. slapi_destroy_condvar(something_changed); something_changed = NULL;
  2884. slapi_unlock_mutex(stop_lock);
  2885. slapi_destroy_mutex(stop_lock); stop_lock = NULL;
  2886. slapi_destroy_condvar(start_cond); start_cond = NULL;
  2887. slapi_destroy_mutex(start_lock); start_lock = NULL;
  2888. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_stop\n",0,0,0);
  2889. }
  2890. /*
  2891. cos_cache_backwards_stricmp_and_clip
  2892. ------------------------------------
  2893. compares s2 to s1 starting from end of the strings until the beginning of
  2894. either matches result in the s2 value being clipped from s1 with a NULL char
  2895. and 1 being returned as opposed to 0
  2896. */
  2897. static int cos_cache_backwards_stricmp_and_clip(char*s1,char*s2)
  2898. {
  2899. int ret = 0;
  2900. int s1len = 0;
  2901. int s2len = 0;
  2902. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_cache_backwards_stricmp_and_clip\n",0,0,0);
  2903. s1len = strlen(s1);
  2904. s2len = strlen(s2);
  2905. if(s1len > s2len && s2len > 0)
  2906. {
  2907. while(s1len > -1 && s2len > -1)
  2908. {
  2909. s1len--;
  2910. s2len--;
  2911. if(s1[s1len] != s2[s2len])
  2912. break;
  2913. else
  2914. {
  2915. if(s2len == 0)
  2916. {
  2917. /* hit! now clip */
  2918. ret = 1;
  2919. s1[s1len] = '\0';
  2920. }
  2921. }
  2922. }
  2923. }
  2924. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_cache_backwards_stricmp_and_clip\n",0,0,0);
  2925. return ret;
  2926. }
  2927. static int cos_cache_follow_pointer( vattr_context *c, const char *dn, char *type, Slapi_ValueSet **out_vs, Slapi_Value *test_this, int *result, int flags)
  2928. {
  2929. int ret = -1; /* assume failure */
  2930. Slapi_PBlock *pDnSearch = 0;
  2931. Slapi_Entry **pEntryList = 0;
  2932. char *attrs[2];
  2933. int op = 0;
  2934. int type_test = 0;
  2935. int type_name_disposition = 0;
  2936. char *actual_type_name = 0;
  2937. int free_flags = 0;
  2938. Slapi_ValueSet *tmp_vs = 0;
  2939. attrs[0] = type;
  2940. attrs[1] = 0;
  2941. /* Use new internal operation API */
  2942. pDnSearch = slapi_pblock_new();
  2943. if (pDnSearch) {
  2944. slapi_search_internal_set_pb(pDnSearch, dn, LDAP_SCOPE_BASE,"(|(objectclass=*)(objectclass=ldapsubentry))",attrs,
  2945. 0,NULL,NULL,cos_get_plugin_identity(),0);
  2946. slapi_search_internal_pb(pDnSearch);
  2947. slapi_pblock_get( pDnSearch, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2948. }
  2949. if(pDnSearch && (ret == LDAP_SUCCESS))
  2950. {
  2951. ret = -1;
  2952. slapi_pblock_get( pDnSearch, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &pEntryList);
  2953. if(pEntryList)
  2954. {
  2955. if(out_vs) /* if this set, a value is required */
  2956. op = 1;
  2957. else if(test_this && result) /* compare op */
  2958. op = 2;
  2959. else
  2960. {
  2961. /* requires only type present */
  2962. op = 1;
  2963. type_test = 1;
  2964. }
  2965. switch(op)
  2966. {
  2967. case 1:
  2968. /* straight value return or type test */
  2969. if(type_test)
  2970. out_vs = &tmp_vs;
  2971. ret = slapi_vattr_values_get_sp(c, pEntryList[0], type, out_vs,&type_name_disposition, &actual_type_name, flags, &free_flags);
  2972. if(actual_type_name)
  2973. slapi_ch_free((void **) &actual_type_name);
  2974. if(type_test && free_flags == SLAPI_VIRTUALATTRS_RETURNED_COPIES)
  2975. slapi_valueset_free(*out_vs);
  2976. break;
  2977. case 2:
  2978. /* this must be a compare op */
  2979. ret = slapi_vattr_value_compare_sp(c, pEntryList[0],type, test_this, result, flags);
  2980. break;
  2981. }
  2982. }
  2983. }
  2984. /* clean up */
  2985. if(pDnSearch)
  2986. {
  2987. slapi_free_search_results_internal(pDnSearch);
  2988. slapi_pblock_destroy(pDnSearch);
  2989. }
  2990. return ret;
  2991. }
  2992. /*
  2993. * cos_cache_backend_state_change()
  2994. * --------------------------------
  2995. * This is called when a backend changes state
  2996. * We simply signal to rebuild the cos cache in this case
  2997. *
  2998. */
  2999. void cos_cache_backend_state_change(void *handle, char *be_name,
  3000. int old_be_state, int new_be_state)
  3001. {
  3002. slapi_lock_mutex(change_lock);
  3003. slapi_notify_condvar( something_changed, 1 );
  3004. slapi_unlock_mutex(change_lock);
  3005. }
  3006. /*
  3007. * returns non-zero: entry is cos significant (note does not detect indirect
  3008. * template entries).
  3009. * 0 : entry is not cos significant.
  3010. */
  3011. static int cos_cache_entry_is_cos_related( Slapi_Entry *e) {
  3012. int rc = 0;
  3013. Slapi_Attr *pObjclasses = NULL;
  3014. if ( e == NULL ) {
  3015. LDAPDebug0Args(LDAP_DEBUG_ANY, "cos_cache_change_notify: "
  3016. "modified entry is NULL--updating cache just in case\n");
  3017. rc = 1;
  3018. } else {
  3019. if(slapi_entry_attr_find( e, "objectclass", &pObjclasses ))
  3020. {
  3021. LDAPDebug1Arg(LDAP_DEBUG_ANY, "cos_cache_change_notify: "
  3022. " failed to get objectclass from %s\n",
  3023. slapi_entry_get_dn(e));
  3024. rc = 0;
  3025. } else {
  3026. Slapi_Value *val = NULL;
  3027. int index = 0;
  3028. char *pObj;
  3029. /* check out the object classes to see if this was a cosDefinition */
  3030. index = slapi_attr_first_value( pObjclasses, &val );
  3031. while(!rc && val)
  3032. {
  3033. pObj = (char*)slapi_value_get_string(val);
  3034. if(!strcasecmp(pObj, "cosdefinition") ||
  3035. !strcasecmp(pObj, "cossuperdefinition") ||
  3036. !strcasecmp(pObj, "costemplate"))
  3037. {
  3038. rc = 1;
  3039. }
  3040. index = slapi_attr_next_value( pObjclasses, index, &val );
  3041. }
  3042. }
  3043. }
  3044. return(rc);
  3045. }