cos_cache.c 95 KB

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