cos_cache.c 122 KB

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