cos_cache.c 95 KB

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