cos_cache.c 99 KB

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