cos_cache.c 98 KB

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