cos_cache.c 96 KB

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