cos_cache.c 98 KB

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