acl.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "acl.h"
  13. /****************************************************************************
  14. *
  15. * acl.c
  16. *
  17. *
  18. * This file contains the functions related to Access Control List (ACL)
  19. * checking. The ACL checking is based on the ONE ACL design implemented
  20. * in the Web server 2.0. For more information on the ACL design look
  21. * into the barracuda home page.
  22. *
  23. *
  24. ******************************************************************************/
  25. /****************************************************************************/
  26. /* Globals. Must be protected by Mutex. */
  27. /****************************************************************************/
  28. /* Signatures to see if things have changed */
  29. static short acl_signature = 0;
  30. /****************************************************************************/
  31. /* Defines, Constants, ande Declarations */
  32. /****************************************************************************/
  33. static const char *ds_map_generic[2] = { NULL, NULL };
  34. /****************************************************************************/
  35. /* prototypes */
  36. /****************************************************************************/
  37. static int acl__resource_match_aci(struct acl_pblock *aclpb, aci_t *aci ,
  38. int skip_attrEval, int *a_matched);
  39. static int acl__TestRights(Acl_PBlock *aclpb,int access, const char **right,
  40. const char ** map_generic, aclResultReason_t *result_reason);
  41. static int acl__scan_for_acis(struct acl_pblock *aclpb, int *err);
  42. static void acl__reset_cached_result (struct acl_pblock *aclpb );
  43. static int acl__scan_match_handles ( struct acl_pblock *aclpb, int type);
  44. static int acl__attr_cached_result (struct acl_pblock *aclpb, char *attr, int access );
  45. static int acl__match_handlesFromCache (struct acl_pblock *aclpb, char *attr, int access);
  46. static int acl__get_attrEval ( struct acl_pblock *aclpb, char *attr );
  47. static int acl__recompute_acl (Acl_PBlock *aclpb, AclAttrEval *a_eval,
  48. int access, int aciIndex);
  49. static void __acl_set_aclIndex_inResult ( Acl_PBlock *aclpb,
  50. int access, int index );
  51. static int acl__make_filter_test_entry ( Slapi_Entry **entry,
  52. char *attr_type, struct berval *attr_val);
  53. static int acl__test_filter ( Slapi_Entry *entry, struct slapi_filter *f,
  54. int filter_sense);
  55. static void print_access_control_summary( char * source,
  56. int ret_val, char *clientDn,
  57. struct acl_pblock *aclpb,
  58. const char *right,
  59. char *attr,
  60. const char *edn,
  61. aclResultReason_t *acl_reason);
  62. static int check_rdn_access( Slapi_PBlock *pb, Slapi_Entry *e,
  63. const char * newrdn, int access);
  64. /*
  65. * Check the rdn permissions for this entry:
  66. * require: write access to the entry, write (add) access to the new
  67. * naming attribute, write (del) access to the old naming attribute if
  68. * deleteoldrdn set.
  69. *
  70. * Valid only for the modrdn operation.
  71. */
  72. int
  73. acl_access_allowed_modrdn(
  74. Slapi_PBlock *pb,
  75. Slapi_Entry *e, /* The Slapi_Entry */
  76. char *attr, /* Attribute of the entry */
  77. struct berval *val, /* value of attr. NOT USED */
  78. int access /* requested access rights */
  79. )
  80. {
  81. int retCode ;
  82. char *newrdn;
  83. char *ci_newrdn;
  84. const char *oldrdn;
  85. Slapi_DN *target_sdn = NULL;
  86. int deleteoldrdn = 0;
  87. /*
  88. * First check write permission on the entry--this is actually
  89. * specially for modrdn.
  90. */
  91. retCode = acl_access_allowed ( pb, e, NULL /* attr */, NULL /* val */,
  92. SLAPI_ACL_WRITE);
  93. if ( retCode != LDAP_SUCCESS ) {
  94. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  95. "modrdn:write permission to entry not allowed\n");
  96. return(retCode);
  97. }
  98. /* Now get the new rdn attribute name and value */
  99. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET_SDN, &target_sdn );
  100. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  101. ci_newrdn = slapi_ch_strdup(newrdn);
  102. slapi_dn_ignore_case(ci_newrdn);
  103. /* Check can add the new naming attribute */
  104. retCode = check_rdn_access( pb, e, ci_newrdn, ACLPB_SLAPI_ACL_WRITE_ADD) ;
  105. slapi_ch_free_string(&ci_newrdn);
  106. if ( retCode != LDAP_SUCCESS ) {
  107. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  108. "modrdn:write permission to add new naming attribute not allowed\n");
  109. return(retCode);
  110. }
  111. /* Check can delete the new naming attribute--if required */
  112. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deleteoldrdn );
  113. if ( deleteoldrdn ) {
  114. oldrdn = slapi_sdn_get_ndn(target_sdn);
  115. retCode = check_rdn_access( pb, e, oldrdn, ACLPB_SLAPI_ACL_WRITE_DEL) ;
  116. if ( retCode != LDAP_SUCCESS ) {
  117. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  118. "modrdn:write permission to delete old naming attribute not allowed\n");
  119. return(retCode);
  120. }
  121. }
  122. return(retCode);
  123. }
  124. /*
  125. * Test if have access to make the first rdn of dn in entry e.
  126. */
  127. static int
  128. check_rdn_access( Slapi_PBlock *pb, Slapi_Entry *e, const char *dn, int access)
  129. {
  130. char **dns;
  131. char **rdns;
  132. int retCode = LDAP_INSUFFICIENT_ACCESS;
  133. int i;
  134. if ( (dns = slapi_ldap_explode_dn( dn, 0 )) != NULL ) {
  135. if ( (rdns = slapi_ldap_explode_rdn( dns[0], 0 )) != NULL ) {
  136. for ( i = 0; rdns[i] != NULL; i++ ) {
  137. char *type;
  138. struct berval bv;
  139. if ( slapi_rdn2typeval( rdns[i], &type, &bv ) != 0 ) {
  140. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  141. "modrdn: rdn2typeval (%s) failed\n", rdns[i]);
  142. retCode = LDAP_INSUFFICIENT_ACCESS;
  143. break;
  144. } else {
  145. if ( (retCode = acl_access_allowed ( pb, e, type /* attr */,
  146. &bv /* val */,
  147. access)) != LDAP_SUCCESS) {
  148. break;
  149. }
  150. }
  151. }
  152. slapi_ldap_value_free( rdns );
  153. }
  154. slapi_ldap_value_free( dns );
  155. }
  156. return(retCode);
  157. }
  158. /***************************************************************************
  159. *
  160. * acl_access_allowed
  161. * Determines if access to the resource is allowed or not.
  162. *
  163. * Input:
  164. * 
  165. *
  166. * Returns:
  167. *
  168. * Returns success/Denied/error condition
  169. *
  170. * LDAP_SUCCESS -- access allowed
  171. * LDAP_INSUFFICIENT_ACCESS -- access denied
  172. *
  173. * Errors returned:
  174. *
  175. * Some of the definition of the return values used copied from
  176. * "ldap.h" for convienience.
  177. * LDAP_OPERATIONS_ERROR
  178. * LDAP_PROTOCOL_ERROR
  179. * LDAP_UNWILLING_TO_PERFORM
  180. *
  181. *
  182. * Error Handling:
  183. * Returned error code.
  184. **************************************************************************/
  185. int
  186. acl_access_allowed(
  187. Slapi_PBlock *pb,
  188. Slapi_Entry *e, /* The Slapi_Entry */
  189. char *attr, /* Attribute of the entry */
  190. struct berval *val, /* value of attr */
  191. int access /* requested access rights */
  192. )
  193. {
  194. char *n_edn; /* Normalized DN of the entry */
  195. int rv;
  196. int err;
  197. int ret_val;
  198. const char *right;
  199. struct acl_pblock *aclpb = NULL;
  200. AclAttrEval *c_attrEval = NULL;
  201. int got_reader_locked = 0;
  202. int deallocate_attrEval = 0;
  203. char *clientDn;
  204. Slapi_DN *e_sdn;
  205. Slapi_Operation *op = NULL;
  206. aclResultReason_t decision_reason;
  207. int loglevel;
  208. PRUint64 o_connid = 0xffffffffffffffff; /* no op */
  209. int o_opid = -1; /* no op */
  210. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  211. slapi_pblock_get(pb, SLAPI_OPERATION, &op); /* for logging */
  212. if (op) {
  213. o_connid = op->o_connid;
  214. o_opid = op->o_opid;
  215. }
  216. TNF_PROBE_1_DEBUG(acl_access_allowed_start,"ACL","", tnf_int,access,access);
  217. decision_reason.deciding_aci = NULL;
  218. decision_reason.reason = ACL_REASON_NONE;
  219. /**
  220. * First, if the acl private write/delete on attribute right
  221. * is requested, turn this into SLAPI_ACL_WRITE
  222. * and record the original value.
  223. * Need to make sure that these rights do not clash with the SLAPI
  224. * public rights. This should be easy as the requested rights
  225. * in the aclpb are stored in the bottom byte of aclpb_res_type,
  226. * so if we keep the ACL private bits here too we make sure
  227. * not to clash.
  228. *
  229. */
  230. if ( access & (ACLPB_SLAPI_ACL_WRITE_ADD | ACLPB_SLAPI_ACL_WRITE_DEL) ) {
  231. access |= SLAPI_ACL_WRITE;
  232. }
  233. n_edn = slapi_entry_get_ndn ( e );
  234. e_sdn = slapi_entry_get_sdn ( e );
  235. /* Check if this is a write operation and the database is readonly */
  236. /* No one, even the rootdn should be allowed to write to the database */
  237. /* jcm: ReadOnly only applies to the public backends, the private ones */
  238. /* (the DSEs) should still be writable for configuration. */
  239. if ( access & ( SLAPI_ACL_WRITE | SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_MODDN)) {
  240. int be_readonly, privateBackend;
  241. Slapi_Backend *be;
  242. slapi_pblock_get ( pb, SLAPI_BE_READONLY, &be_readonly );
  243. slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  244. privateBackend = slapi_be_private ( be );
  245. if ( !privateBackend && (be_readonly || slapi_config_get_readonly () )){
  246. slapi_log_error (loglevel, plugin_name,
  247. "conn=%" NSPRIu64 " op=%d (main): Deny %s on entry(%s)"
  248. ": readonly backend\n",
  249. o_connid, o_opid,
  250. acl_access2str(access),
  251. n_edn);
  252. return LDAP_UNWILLING_TO_PERFORM;
  253. }
  254. }
  255. /* Check for things we need to skip */
  256. TNF_PROBE_0_DEBUG(acl_skipaccess_start,"ACL","");
  257. if ( acl_skip_access_check ( pb, e )) {
  258. slapi_log_error (loglevel, plugin_name,
  259. "conn=%" NSPRIu64 " op=%d (main): Allow %s on entry(%s)"
  260. ": root user\n",
  261. o_connid, o_opid,
  262. acl_access2str(access),
  263. n_edn);
  264. return(LDAP_SUCCESS);
  265. }
  266. TNF_PROBE_0_DEBUG(acl_skipaccess_end,"ACL","");
  267. /* Get the bindDN (normalized & case-ignored) */
  268. slapi_pblock_get ( pb, SLAPI_REQUESTOR_DN, &clientDn );
  269. /* Initialize aclpb */
  270. aclplugin_preop_common( pb );
  271. /* get the right acl pblock to work with */
  272. if ( access & SLAPI_ACL_PROXY )
  273. aclpb = acl_get_aclpb ( pb, ACLPB_PROXYDN_PBLOCK );
  274. else
  275. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  276. if ( !aclpb ) {
  277. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 1 \n" );
  278. ret_val = LDAP_OPERATIONS_ERROR;
  279. goto cleanup_and_ret;
  280. }
  281. if ( !aclpb->aclpb_curr_entry_sdn ) {
  282. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "NULL aclpb_curr_entry_sdn \n" );
  283. ret_val = LDAP_OPERATIONS_ERROR;
  284. goto cleanup_and_ret;
  285. }
  286. /* check if aclpb is initialized or not */
  287. TNF_PROBE_0_DEBUG(acl_aclpbinit_start,"ACL","");
  288. acl_init_aclpb ( pb, aclpb, clientDn, 0 );
  289. TNF_PROBE_0_DEBUG(acl_aclpbinit_end,"ACL","");
  290. if ( access & SLAPI_ACL_MODDN) {
  291. /* with MODDN, the entry 'e' is the destination entry.
  292. * This destination entry will be checked against a possible 'target_to'
  293. * The source entry will be checked against a possible 'target_from'.
  294. * 'e' will be given down to acl_resource_match_aci, so no pb on this side.
  295. * For the source entry we need to keep its dn into the aclpb.
  296. */
  297. entry_address *old_addr;
  298. slapi_pblock_get(pb, SLAPI_TARGET_ADDRESS, &old_addr);
  299. aclpb->aclpb_moddn_source_sdn = old_addr->sdn;
  300. }
  301. /* Here we mean if "I am trying to add/delete "myself" to a group, etc." We
  302. * basically just want to see if the value matches the DN of the user that
  303. * we're checking access for */
  304. if (val && (access & SLAPI_ACL_WRITE) && (val->bv_len > 0)) {
  305. Slapi_Attr *sa = slapi_attr_new();
  306. char *oid = NULL;
  307. slapi_attr_init(sa, attr);
  308. slapi_attr_get_syntax_oid_copy(sa, &oid);
  309. /* We only want to perform this check if the attribute is
  310. * defined using the DN or Name And Optional UID syntaxes. */
  311. if (oid && ((strcasecmp(oid, DN_SYNTAX_OID) == 0) ||
  312. (strcasecmp(oid, NAMEANDOPTIONALUID_SYNTAX_OID) == 0))) {
  313. /* should use slapi_sdn_compare() but that'a an extra malloc/free */
  314. char *dn_val_to_write = slapi_create_dn_string("%s", val->bv_val);
  315. if ( dn_val_to_write && aclpb->aclpb_authorization_sdn &&
  316. slapi_utf8casecmp((ACLUCHP)dn_val_to_write, (ACLUCHP)
  317. slapi_sdn_get_ndn(aclpb->aclpb_authorization_sdn)) == 0) {
  318. access |= SLAPI_ACL_SELF;
  319. }
  320. slapi_ch_free_string(&dn_val_to_write);
  321. }
  322. slapi_ch_free_string(&oid);
  323. slapi_attr_free(&sa);
  324. }
  325. /* Convert access to string of rights eg SLAPI_ACL_ADD->"add". */
  326. if ((right= acl_access2str(access)) == NULL) {
  327. /* ERROR: unknown rights */
  328. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  329. "acl_access_allowed unknown rights:%d\n", access);
  330. ret_val = LDAP_OPERATIONS_ERROR;
  331. goto cleanup_and_ret;
  332. }
  333. /*
  334. * Am I a anonymous dude ? then we can use our anonymous profile
  335. * We don't require the aclpb to have been initialized for anom stuff
  336. *
  337. */
  338. TNF_PROBE_0_DEBUG(acl_anon_test_start,"ACL","");
  339. if ( (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ )) &&
  340. (clientDn && *clientDn == '\0')) {
  341. aclanom_get_suffix_info(e, aclpb);
  342. ret_val = aclanom_match_profile ( pb, aclpb, e, attr, access );
  343. if (ret_val != -1 ) {
  344. if (ret_val == LDAP_SUCCESS ) {
  345. decision_reason.reason = ACL_REASON_ANON_ALLOWED;
  346. } else if (ret_val == LDAP_INSUFFICIENT_ACCESS) {
  347. decision_reason.reason = ACL_REASON_ANON_DENIED;
  348. }
  349. goto cleanup_and_ret;
  350. }
  351. }
  352. TNF_PROBE_0_DEBUG(acl_anon_test_end,"ACL","");
  353. /* copy the value into the aclpb for later checking by the value acl code */
  354. aclpb->aclpb_curr_attrVal = val;
  355. if (!(aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST) &&
  356. (access & SLAPI_ACL_SEARCH)) {
  357. /* We are evaluating SEARCH right for the entry. After that
  358. ** we will eval the READ right. We need to refresh the
  359. ** list of acls selected for evaluation for the entry.
  360. ** Initialize the array so that we indicate nothing has been
  361. ** selected.
  362. */
  363. aclpb->aclpb_handles_index[0] = -1;
  364. /* access is not allowed on entry for search -- it's for
  365. ** read only.
  366. */
  367. aclpb->aclpb_state &= ~ACLPB_ACCESS_ALLOWED_ON_ENTRY;
  368. }
  369. /* set that this is a new entry */
  370. aclpb->aclpb_res_type |= ACLPB_NEW_ENTRY;
  371. aclpb->aclpb_access = 0;
  372. aclpb->aclpb_access |= access;
  373. /*
  374. * stub the Slapi_Entry info first time and only it has changed
  375. * or if the pblock is a psearch pblock--in this case the lifetime
  376. * of entries associated with psearches is such that we cannot cache
  377. * pointers to them--we must always start afresh (see psearch.c).
  378. */
  379. slapi_pblock_get( pb, SLAPI_OPERATION, &op);
  380. if ( operation_is_flag_set(op, OP_FLAG_PS) ||
  381. (aclpb->aclpb_curr_entry_sdn == NULL) ||
  382. (slapi_sdn_compare ( aclpb->aclpb_curr_entry_sdn, e_sdn) != 0) ||
  383. (aclpb->aclpb_curr_entry != e) /* cannot trust the cached entry */ ) {
  384. TNF_PROBE_0_DEBUG(acl_entry_first_touch_start,"ACL","");
  385. slapi_log_error(loglevel, plugin_name,
  386. "#### conn=%" NSPRIu64 " op=%d binddn=\"%s\"\n",
  387. o_connid, o_opid, clientDn);
  388. aclpb->aclpb_stat_total_entries++;
  389. if (!(access & SLAPI_ACL_PROXY) &&
  390. !( aclpb->aclpb_state & ACLPB_DONOT_EVALUATE_PROXY )) {
  391. Acl_PBlock *proxy_pb;
  392. proxy_pb = acl_get_aclpb( pb, ACLPB_PROXYDN_PBLOCK );
  393. if (proxy_pb) {
  394. TNF_PROBE_0_DEBUG(acl_access_allowed_proxy_start,"ACL","");
  395. ret_val = acl_access_allowed( pb, e, attr, val, SLAPI_ACL_PROXY );
  396. TNF_PROBE_0_DEBUG(acl_access_allowed_proxy_end,"ACL","");
  397. if (ret_val != LDAP_SUCCESS) goto cleanup_and_ret;
  398. }
  399. }
  400. if ( access & SLAPI_ACL_SEARCH) {
  401. aclpb->aclpb_num_entries++;
  402. if ( aclpb->aclpb_num_entries == 1) {
  403. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  404. } else if ( aclpb->aclpb_state & ACLPB_COPY_EVALCONTEXT ) {
  405. /* We need to copy the evalContext */
  406. acl_copyEval_context ( aclpb, &aclpb->aclpb_curr_entryEval_context,
  407. &aclpb->aclpb_prev_entryEval_context, 0 );
  408. aclpb->aclpb_state &= ~ACLPB_COPY_EVALCONTEXT;
  409. }
  410. acl_clean_aclEval_context ( &aclpb->aclpb_curr_entryEval_context, 1 /*scrub */);
  411. }
  412. /* reset the cached result based on the scope */
  413. acl__reset_cached_result (aclpb );
  414. /* Find all the candidate aci's that apply by scanning up the DIT tree from edn. */
  415. TNF_PROBE_0_DEBUG(acl_aciscan_start,"ACL","");
  416. slapi_sdn_done ( aclpb->aclpb_curr_entry_sdn );
  417. slapi_sdn_set_ndn_byval ( aclpb->aclpb_curr_entry_sdn, n_edn );
  418. acllist_aciscan_update_scan ( aclpb, n_edn );
  419. TNF_PROBE_0_DEBUG(acl_aciscan_end,"ACL","");
  420. /* Keep the ptr to the current entry */
  421. aclpb->aclpb_curr_entry = (Slapi_Entry *) e;
  422. /* Get the attr info */
  423. deallocate_attrEval = acl__get_attrEval ( aclpb, attr );
  424. aclutil_print_resource ( aclpb, right, attr, clientDn );
  425. /*
  426. * Used to be PListInitProp(aclpb->aclpb_proplist, 0,
  427. * DS_ATTR_ENTRY, e, 0);
  428. *
  429. * The difference is that PListInitProp() allocates a new property
  430. * every time it's called, overwriting the old name in the PList hash
  431. * table, but not freeing the original property.
  432. * Now, we just create the property at aclpb_malloc() time and
  433. * Assign a new value each time.
  434. */
  435. rv = PListAssignValue(aclpb->aclpb_proplist,
  436. DS_ATTR_ENTRY, e, 0);
  437. if (rv < 0) {
  438. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  439. "Unable to set the Slapi_Entry in the Plist\n");
  440. ret_val = LDAP_OPERATIONS_ERROR;
  441. goto cleanup_and_ret;
  442. }
  443. TNF_PROBE_0_DEBUG(acl_entry_first_touch_end,"ACL","");
  444. } else {
  445. /* we are processing the same entry but for a different
  446. ** attribute. If access is already allowed on that, entry, then
  447. ** it's not a new entry anymore. It's the same old one.
  448. */
  449. TNF_PROBE_0_DEBUG(acl_entry_subs_touch_start,"ACL","");
  450. aclpb->aclpb_res_type &= ~ACLPB_NEW_ENTRY;
  451. /* Get the attr info */
  452. deallocate_attrEval = acl__get_attrEval ( aclpb, attr );
  453. TNF_PROBE_0_DEBUG(acl_entry_subs_touch_end,"ACL","");
  454. }
  455. /* get a lock for the reader */
  456. acllist_acicache_READ_LOCK();
  457. got_reader_locked = 1;
  458. /*
  459. ** Check if we can use any cached information to determine
  460. ** access to this resource
  461. */
  462. if ((access & SLAPI_ACL_SEARCH) &&
  463. (ret_val = acl__match_handlesFromCache(aclpb, attr, access)) != -1) {
  464. /* means got a result: allowed or not*/
  465. if (ret_val == LDAP_SUCCESS ) {
  466. decision_reason.reason = ACL_REASON_EVALCONTEXT_CACHED_ALLOW;
  467. } else if (ret_val == LDAP_INSUFFICIENT_ACCESS) {
  468. decision_reason.reason = ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED;
  469. }
  470. goto cleanup_and_ret;
  471. }
  472. /*
  473. ** Now we have all the information about the resource. Now we need to
  474. ** figure out if there are any ACLs which can be applied.
  475. ** If no ACLs are there, then it's a DENY as default.
  476. */
  477. if (!(acl__scan_for_acis(aclpb, &err))) {
  478. /* We might have accessed the ACL first time which could
  479. ** have caused syntax error.
  480. */
  481. if ( err == ACL_ONEACL_TEXT_ERR)
  482. ret_val = LDAP_INVALID_SYNTAX;
  483. else {
  484. ret_val = LDAP_INSUFFICIENT_ACCESS;
  485. decision_reason.reason = ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS;
  486. }
  487. goto cleanup_and_ret;
  488. }
  489. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  490. "Processed attr:%s for entry:%s\n", attr ? attr : "NULL",
  491. n_edn);
  492. /*
  493. ** Now evaluate the rights.
  494. ** This is what we have been waiting for.
  495. ** The return value should be ACL_RES_DENY or ACL_RES_ALLOW.
  496. */
  497. rv = acl__TestRights(aclpb, access, &right, ds_map_generic,
  498. &decision_reason);
  499. if ( rv != ACL_RES_ALLOW && (0 == strcasecmp ( right, "selfwrite") ) ) {
  500. /* If I am adding myself to a group, we don't need selfwrite always,
  501. ** write priv is good enough. Since libaccess doesn't provide me a nice
  502. ** way to evaluate OR rights, I have to try again with wite priv.
  503. ** bug: 339051
  504. */
  505. right = access_str_write;
  506. rv = acl__TestRights(aclpb, access, &right, ds_map_generic,
  507. &decision_reason);
  508. }
  509. if (rv == ACL_RES_ALLOW) {
  510. ret_val = LDAP_SUCCESS;
  511. } else {
  512. ret_val = LDAP_INSUFFICIENT_ACCESS;
  513. }
  514. cleanup_and_ret:
  515. TNF_PROBE_0_DEBUG(acl_cleanup_start,"ACL","");
  516. /* I am ready to get out. */
  517. if ( got_reader_locked ) acllist_acicache_READ_UNLOCK();
  518. /* Store the status of the evaluation for this attr */
  519. if ( aclpb && (c_attrEval = aclpb->aclpb_curr_attrEval )) {
  520. if ( deallocate_attrEval ) {
  521. /* In this case we are not caching the result as
  522. ** we have too many attrs. we have malloced space.
  523. ** Get rid of it.
  524. */
  525. slapi_ch_free ( (void **) &c_attrEval->attrEval_name );
  526. slapi_ch_free ( (void **) &c_attrEval );
  527. } else if (ret_val == LDAP_SUCCESS ) {
  528. if ( access & SLAPI_ACL_SEARCH )
  529. c_attrEval->attrEval_s_status |= ACL_ATTREVAL_SUCCESS;
  530. else if ( access & SLAPI_ACL_READ )
  531. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_SUCCESS;
  532. else
  533. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_INVALID;
  534. } else {
  535. if ( access & SLAPI_ACL_SEARCH )
  536. c_attrEval->attrEval_s_status |= ACL_ATTREVAL_FAIL;
  537. else if ( access & SLAPI_ACL_READ )
  538. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_FAIL;
  539. else
  540. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_INVALID;
  541. }
  542. }
  543. if ( aclpb ) aclpb->aclpb_curr_attrEval = NULL;
  544. print_access_control_summary( "main", ret_val, clientDn, aclpb, right,
  545. (attr ? attr : "NULL"), n_edn,
  546. &decision_reason );
  547. TNF_PROBE_0_DEBUG(acl_cleanup_end,"ACL","");
  548. TNF_PROBE_0_DEBUG(acl_access_allowed_end,"ACL","");
  549. return(ret_val);
  550. }
  551. static void
  552. print_access_control_summary( char *source, int ret_val, char *clientDn,
  553. struct acl_pblock *aclpb,
  554. const char *right,
  555. char *attr,
  556. const char *edn,
  557. aclResultReason_t *acl_reason)
  558. {
  559. struct codebook {
  560. int code;
  561. char *text;
  562. };
  563. static struct codebook reasonbook[] = {
  564. {ACL_REASON_NO_ALLOWS, "no allow acis"},
  565. {ACL_REASON_RESULT_CACHED_DENY, "cached deny"},
  566. {ACL_REASON_RESULT_CACHED_ALLOW, "cached allow"},
  567. {ACL_REASON_EVALUATED_ALLOW, "allowed"},
  568. {ACL_REASON_EVALUATED_DENY, "denied"},
  569. {ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS, "no aci matched the resource"},
  570. {ACL_REASON_NO_MATCHED_SUBJECT_ALLOWS, "no aci matched the subject"},
  571. {ACL_REASON_ANON_ALLOWED, "allow anyone aci matched anon user"},
  572. {ACL_REASON_ANON_DENIED, "no matching anyone aci for anon user"},
  573. {ACL_REASON_EVALCONTEXT_CACHED_ALLOW, "cached context/parent allow"},
  574. {ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED, "cached context/parent deny"},
  575. {ACL_REASON_EVALCONTEXT_CACHED_ATTR_STAR_ALLOW, "cached context/parent allow any attr"},
  576. {ACL_REASON_NONE, "error occurred"},
  577. };
  578. char *anon = "anonymous";
  579. char *null_user = "NULL"; /* bizare case */
  580. char *real_user = NULL;
  581. char *proxy_user = NULL;
  582. char *access_allowed_string = "Allow";
  583. char *access_not_allowed_string = "Deny";
  584. char *access_error_string = "access_error";
  585. char *access_status = NULL;
  586. char *access_reason_none = "no reason available";
  587. char *access_reason = access_reason_none;
  588. char acl_info[ BUFSIZ ];
  589. Slapi_Operation *op = NULL;
  590. int loglevel;
  591. int i;
  592. PRUint64 o_connid = 0xffffffffffffffff; /* no op */
  593. int o_opid = -1; /* no op */
  594. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  595. if ( !slapi_is_loglevel_set(loglevel) ) {
  596. return;
  597. }
  598. if ( !aclpb ) {
  599. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb \n" );
  600. return;
  601. }
  602. slapi_pblock_get(aclpb->aclpb_pblock, SLAPI_OPERATION, &op); /* for logging */
  603. if (op) {
  604. o_connid = op->o_connid;
  605. o_opid = op->o_opid;
  606. }
  607. if (ret_val == LDAP_INSUFFICIENT_ACCESS) {
  608. access_status = access_not_allowed_string;
  609. } else if ( ret_val == LDAP_SUCCESS) {
  610. access_status = access_allowed_string;
  611. } else { /* some kind of error */
  612. access_status = access_error_string;
  613. }
  614. /* decode the reason */
  615. for (i = 0; i < sizeof(reasonbook) / sizeof(struct codebook); i++) {
  616. if ( acl_reason->reason == reasonbook[i].code ) {
  617. access_reason = reasonbook[i].text;
  618. break;
  619. }
  620. }
  621. /* get the acl */
  622. acl_info[0] = '\0';
  623. if (acl_reason->deciding_aci) {
  624. if (acl_reason->reason == ACL_REASON_RESULT_CACHED_DENY ||
  625. acl_reason->reason == ACL_REASON_RESULT_CACHED_ALLOW) {
  626. /* acl is in cache. Its detail must have been printed before.
  627. * So no need to print out acl detail this time.
  628. */
  629. PR_snprintf( &acl_info[0], BUFSIZ, "%s by aci(%d)",
  630. access_reason,
  631. acl_reason->deciding_aci->aci_index);
  632. }
  633. else {
  634. PR_snprintf( &acl_info[0], BUFSIZ, "%s by aci(%d): aciname=%s, acidn=\"%s\"",
  635. access_reason,
  636. acl_reason->deciding_aci->aci_index,
  637. acl_reason->deciding_aci->aclName,
  638. slapi_sdn_get_ndn (acl_reason->deciding_aci->aci_sdn) );
  639. }
  640. }
  641. /* Say who was denied access */
  642. if (clientDn) {
  643. if (clientDn[0] == '\0') {
  644. /* anon */
  645. real_user = anon;
  646. } else {
  647. real_user = clientDn;
  648. }
  649. } else {
  650. real_user = null_user;
  651. }
  652. /* Is there a proxy */
  653. if ( aclpb->aclpb_proxy != NULL) {
  654. if ( aclpb->aclpb_authorization_sdn != NULL ) {
  655. proxy_user =
  656. (char *)(slapi_sdn_get_ndn(aclpb->aclpb_authorization_sdn)?
  657. slapi_sdn_get_ndn(aclpb->aclpb_authorization_sdn):
  658. null_user);
  659. if (strcasecmp(right, access_str_moddn) == 0) {
  660. slapi_log_error(loglevel, plugin_name,
  661. "conn=%" NSPRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) [from %s] to proxy (%s)"
  662. ": %s\n",
  663. o_connid, o_opid,
  664. source,
  665. access_status,
  666. right,
  667. edn,
  668. attr ? attr: "NULL",
  669. aclpb->aclpb_moddn_source_sdn ? slapi_sdn_get_dn(aclpb->aclpb_moddn_source_sdn) : "NULL", /* from entry - stored in pblock */
  670. proxy_user,
  671. acl_info[0] ? acl_info : access_reason);
  672. } else {
  673. slapi_log_error(loglevel, plugin_name,
  674. "conn=%" NSPRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) to proxy (%s)"
  675. ": %s\n",
  676. o_connid, o_opid,
  677. source,
  678. access_status,
  679. right,
  680. edn,
  681. attr ? attr: "NULL",
  682. proxy_user,
  683. acl_info[0] ? acl_info : access_reason);
  684. }
  685. } else {
  686. proxy_user = null_user;
  687. if (strcasecmp(right, access_str_moddn) == 0) {
  688. slapi_log_error(loglevel, plugin_name,
  689. "conn=%" NSPRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) [from %s] to proxy (%s)"
  690. ": %s\n",
  691. o_connid, o_opid,
  692. source,
  693. access_status,
  694. right,
  695. edn,
  696. attr ? attr: "NULL",
  697. aclpb->aclpb_moddn_source_sdn ? slapi_sdn_get_dn(aclpb->aclpb_moddn_source_sdn) : "NULL", /* from entry - stored in pblock */
  698. proxy_user,
  699. acl_info[0] ? acl_info : access_reason);
  700. } else {
  701. slapi_log_error(loglevel, plugin_name,
  702. "conn=%" NSPRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) to proxy (%s)"
  703. ": %s\n",
  704. o_connid, o_opid,
  705. source,
  706. access_status,
  707. right,
  708. edn,
  709. attr ? attr: "NULL",
  710. proxy_user,
  711. acl_info[0] ? acl_info : access_reason);
  712. }
  713. }
  714. } else {
  715. if (strcasecmp(right, access_str_moddn) == 0) {
  716. slapi_log_error(loglevel, plugin_name,
  717. "conn=%" NSPRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) [from %s] to %s"
  718. ": %s\n",
  719. o_connid, o_opid,
  720. source,
  721. access_status,
  722. right,
  723. edn,
  724. attr ? attr: "NULL",
  725. aclpb->aclpb_moddn_source_sdn ? slapi_sdn_get_dn(aclpb->aclpb_moddn_source_sdn) : "NULL", /* from entry - stored in pblock */
  726. real_user,
  727. acl_info[0] ? acl_info : access_reason);
  728. } else {
  729. slapi_log_error(loglevel, plugin_name,
  730. "conn=%" NSPRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) to %s"
  731. ": %s\n",
  732. o_connid, o_opid,
  733. source,
  734. access_status,
  735. right,
  736. edn,
  737. attr ? attr: "NULL",
  738. real_user,
  739. acl_info[0] ? acl_info : access_reason);
  740. }
  741. }
  742. }
  743. /***************************************************************************
  744. *
  745. * acl_read_access_allowed_on_entry
  746. * check read access control on the given entry.
  747. *
  748. * Only used during seearch to test for read access on the entry.
  749. * (Could be generalized).
  750. *
  751. * attrs is the list of requested attributes passed with the search.
  752. * If the entry has no attributes (weird case) then the routine survives.
  753. *
  754. * Input:
  755. *
  756. *
  757. * Returns:
  758. * LDAP_SUCCESS - access allowed
  759. * LDAP_INSUFFICIENT_ACCESS - access denied
  760. *
  761. * Error Handling:
  762. * None.
  763. *
  764. **************************************************************************/
  765. int
  766. acl_read_access_allowed_on_entry (
  767. Slapi_PBlock *pb,
  768. Slapi_Entry *e, /* The Slapi_Entry */
  769. char **attrs,
  770. int access /* access rights */
  771. )
  772. {
  773. struct acl_pblock *aclpb;
  774. Slapi_Attr *currAttr;
  775. Slapi_Attr *nextAttr;
  776. int len;
  777. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  778. int attr_index = -1;
  779. #endif
  780. char *attr_type = NULL;
  781. int rv, isRoot;
  782. char *clientDn;
  783. unsigned long flags;
  784. aclResultReason_t decision_reason;
  785. int loglevel;
  786. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  787. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_entry_start ,"ACL","");
  788. decision_reason.deciding_aci = NULL;
  789. decision_reason.reason = ACL_REASON_NONE;
  790. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  791. /*
  792. ** If it's the root, or acl is off or the entry is a rootdse,
  793. ** Then you have the privilege to read it.
  794. */
  795. if ( acl_skip_access_check ( pb, e ) ) {
  796. char *n_edn = slapi_entry_get_ndn ( e );
  797. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  798. "Root access (%s) allowed on entry(%s)\n",
  799. acl_access2str(access),
  800. n_edn);
  801. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  802. tnf_string,skip_access,"");
  803. return LDAP_SUCCESS;
  804. }
  805. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  806. if ( !aclpb ) {
  807. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 2 \n" );
  808. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  809. tnf_string,end,"aclpb error");
  810. return LDAP_OPERATIONS_ERROR;
  811. }
  812. /*
  813. * Am I a anonymous dude ? then we can use our anonympous profile
  814. * We don't require the aclpb to have been initialized for anom stuff
  815. *
  816. */
  817. slapi_pblock_get ( pb, SLAPI_REQUESTOR_DN, &clientDn );
  818. if ( clientDn && *clientDn == '\0' ) {
  819. int ret_val;
  820. ret_val = aclanom_match_profile ( pb, aclpb, e, NULL, SLAPI_ACL_READ );
  821. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  822. tnf_string,end,"anon");
  823. if (ret_val != -1 ) return ret_val;
  824. }
  825. aclpb->aclpb_state &= ~ACLPB_RESET_MASK;
  826. if (aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  827. int ret_val;
  828. ret_val = acl__attr_cached_result (aclpb, NULL, SLAPI_ACL_READ);
  829. if (ret_val != -1 ) {
  830. /* print summary if loglevel set */
  831. if ( slapi_is_loglevel_set(loglevel) ) {
  832. char *n_edn;
  833. n_edn = slapi_entry_get_ndn ( e );
  834. if ( ret_val == LDAP_SUCCESS) {
  835. decision_reason.reason =
  836. ACL_REASON_EVALCONTEXT_CACHED_ALLOW;
  837. } else {
  838. decision_reason.reason =
  839. ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED;
  840. }
  841. /*
  842. * pass NULL as the attr as this routine is concerned with
  843. * access at the entry level.
  844. */
  845. print_access_control_summary( "on entry",
  846. ret_val, clientDn, aclpb,
  847. acl_access2str(SLAPI_ACL_READ),
  848. NULL, n_edn,
  849. &decision_reason);
  850. }
  851. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  852. tnf_string,eval_context_cached,"");
  853. return ret_val;
  854. }
  855. }
  856. /*
  857. * Currently do not use this code--it results in confusing
  858. * behaviour..see 529905
  859. */
  860. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  861. /* Do we have access to the entry by virtue of
  862. ** having access to an attr. Before that, let's find out which attrs
  863. ** the user want. If the user has specified certain attributes, then
  864. ** we check aginst that set of attributes.
  865. */
  866. if (!((aclpb->aclpb_state & ACLPB_USER_WANTS_ALL_ATTRS) ||
  867. (aclpb->aclpb_state & ACLPB_USER_SPECIFIED_ATTARS))) {
  868. int i;
  869. if (attrs == NULL) {
  870. aclpb->aclpb_state |= ACLPB_USER_WANTS_ALL_ATTRS;
  871. } else {
  872. for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
  873. if ( strcmp( LDAP_ALL_USER_ATTRS, attrs[i] ) == 0 ) {
  874. aclpb->aclpb_state |= ACLPB_USER_WANTS_ALL_ATTRS;
  875. break;
  876. }
  877. }
  878. }
  879. if (!(aclpb->aclpb_state & ACLPB_USER_WANTS_ALL_ATTRS)) {
  880. for (i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
  881. if ( !slapi_entry_attr_find ( e, attrs[i], &currAttr ) ) {
  882. aclpb->aclpb_state |= ACLPB_USER_SPECIFIED_ATTARS;
  883. break;
  884. }
  885. }
  886. }
  887. } /* end of all user test*/
  888. /*
  889. ** If user has specified a list of attrs, might as well use it
  890. ** to determine access control.
  891. */
  892. currAttr = NULL;
  893. attr_index = -1;
  894. if ( aclpb->aclpb_state & ACLPB_USER_SPECIFIED_ATTARS) {
  895. attr_index = 0;
  896. attr_type = attrs[attr_index++];
  897. } else {
  898. /* Skip the operational attributes -- if there are any in the front */
  899. slapi_entry_first_attr ( e, &currAttr );
  900. if (currAttr != NULL) {
  901. slapi_attr_get_flags ( currAttr, &flags );
  902. while ( flags & SLAPI_ATTR_FLAG_OPATTR ) {
  903. flags = 0;
  904. rv = slapi_entry_next_attr ( e, currAttr, &nextAttr );
  905. if ( !rv ) slapi_attr_get_flags ( nextAttr, &flags );
  906. currAttr = nextAttr;
  907. }
  908. /* Get the attr type */
  909. if ( currAttr ) slapi_attr_get_type ( currAttr , &attr_type );
  910. }
  911. }
  912. #endif /*DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES*/
  913. #ifndef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  914. /*
  915. * Here look at each attribute in the entry and see if
  916. * we have read access to it--if we do
  917. * and we are not denied access to the entry then this
  918. * is taken as implying access to the entry.
  919. */
  920. slapi_entry_first_attr ( e, &currAttr );
  921. if (currAttr != NULL) {
  922. slapi_attr_get_type ( currAttr , &attr_type );
  923. }
  924. #endif
  925. aclpb->aclpb_state |= ACLPB_EVALUATING_FIRST_ATTR;
  926. while (attr_type) {
  927. if (acl_access_allowed (pb, e,attr_type, NULL,
  928. SLAPI_ACL_READ) == LDAP_SUCCESS) {
  929. /*
  930. ** We found a rule which requires us to test access
  931. ** to the entry.
  932. */
  933. if ( aclpb->aclpb_state & ACLPB_FOUND_A_ENTRY_TEST_RULE){
  934. /* Do I have access on the entry itself */
  935. if (acl_access_allowed (pb, e, NULL,
  936. NULL, access) != LDAP_SUCCESS) {
  937. /* How was I denied ?
  938. ** I could be denied on a DENY rule or because
  939. ** there is no allow rule. If it's a DENY from
  940. ** a DENY rule, then we don't have access to
  941. ** the entry ( nice trick to get in )
  942. */
  943. if ( aclpb->aclpb_state &
  944. ACLPB_EXECUTING_DENY_HANDLES)
  945. return LDAP_INSUFFICIENT_ACCESS;
  946. /* The other case is I don't have an
  947. ** explicit allow rule -- which is fine.
  948. ** Since, I am already here, it means that I have
  949. ** an implicit allow to the entry.
  950. */
  951. }
  952. }
  953. aclpb->aclpb_state &= ~ACLPB_EVALUATING_FIRST_ATTR;
  954. /*
  955. ** If we are not sending all the attrs, then we must
  956. ** make sure that we have right on a attr that we are
  957. ** sending
  958. */
  959. len = strlen(attr_type);
  960. if ( (len + 1) > ACLPB_MAX_ATTR_LEN) {
  961. slapi_ch_free ( (void **) &aclpb->aclpb_Evalattr);
  962. aclpb->aclpb_Evalattr = slapi_ch_malloc(len+1);
  963. }
  964. /* length needs to have 1 for '\0' */
  965. PL_strncpyz (aclpb->aclpb_Evalattr, attr_type, len+1);
  966. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  967. if ( attr_index >= 0 ) {
  968. /*
  969. * access was granted to one of the user specified attributes
  970. * which was found in the entry and that attribute is
  971. * now in aclpb_Evalattr
  972. */
  973. aclpb->aclpb_state |= ACLPB_ACCESS_ALLOWED_USERATTR;
  974. } else {
  975. #endif /* DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES */
  976. /*
  977. * Access was granted to _an_ attribute in the entry and that
  978. * attribute is now in aclpb_Evalattr
  979. */
  980. aclpb->aclpb_state |= ACLPB_ACCESS_ALLOWED_ON_A_ATTR;
  981. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  982. }
  983. #endif /* DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES */
  984. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end , "ACL","",
  985. tnf_string,called_access_allowed,"");
  986. return LDAP_SUCCESS;
  987. } else {
  988. /* try the next one */
  989. attr_type = NULL;
  990. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  991. if (attr_index >= 0) {
  992. attr_type = attrs[attr_index++];
  993. } else {
  994. #endif /* DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES */
  995. rv = slapi_entry_next_attr ( e, currAttr, &nextAttr );
  996. if ( rv != 0 ) break;
  997. currAttr = nextAttr;
  998. slapi_attr_get_flags ( currAttr, &flags );
  999. while ( flags & SLAPI_ATTR_FLAG_OPATTR ) {
  1000. flags = 0;
  1001. rv = slapi_entry_next_attr ( e, currAttr, &nextAttr );
  1002. if ( !rv ) slapi_attr_get_flags ( nextAttr, &flags );
  1003. currAttr = nextAttr;
  1004. }
  1005. /* Get the attr type */
  1006. if ( currAttr ) slapi_attr_get_type ( currAttr , &attr_type );
  1007. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  1008. }
  1009. #endif /* DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES */
  1010. }
  1011. }
  1012. /*
  1013. ** That means. we have searched thru all the attrs and found
  1014. ** access is denied on all attrs.
  1015. **
  1016. ** If there were no attributes in the entry at all (can have
  1017. ** such entries thrown up by the b/e, then we do
  1018. ** not have such an implied access.
  1019. */
  1020. aclpb->aclpb_state |= ACLPB_ACCESS_DENIED_ON_ALL_ATTRS;
  1021. aclpb->aclpb_state &= ~ACLPB_EVALUATING_FIRST_ATTR;
  1022. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","");
  1023. return LDAP_INSUFFICIENT_ACCESS;
  1024. }
  1025. /***************************************************************************
  1026. *
  1027. * acl_read_access_allowed_on_attr
  1028. * check access control on the given attr.
  1029. *
  1030. * Only used during search to test for read access to an attr.
  1031. * (Could be generalized)
  1032. *
  1033. * Input:
  1034. *
  1035. *
  1036. * Returns:
  1037. * LDAP_SUCCESS - access allowed
  1038. * LDAP_INSUFFICIENT_ACCESS - access denied
  1039. *
  1040. * Error Handling:
  1041. * None.
  1042. *
  1043. **************************************************************************/
  1044. int
  1045. acl_read_access_allowed_on_attr (
  1046. Slapi_PBlock *pb,
  1047. Slapi_Entry *e, /* The Slapi_Entry */
  1048. char *attr, /* Attribute of the entry */
  1049. struct berval *val, /* value of attr. NOT USED */
  1050. int access /* access rights */
  1051. )
  1052. {
  1053. struct acl_pblock *aclpb = NULL;
  1054. char *clientDn = NULL;
  1055. char *n_edn;
  1056. aclResultReason_t decision_reason;
  1057. int ret_val = -1;
  1058. int loglevel;
  1059. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  1060. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_attr_start ,"ACL","");
  1061. decision_reason.deciding_aci = NULL;
  1062. decision_reason.reason = ACL_REASON_NONE;
  1063. /* I am here, because I have access to the entry */
  1064. n_edn = slapi_entry_get_ndn ( e );
  1065. /* If it's the root or acl is off or rootdse, he has all the priv */
  1066. if ( acl_skip_access_check ( pb, e ) ) {
  1067. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1068. "Root access (%s) allowed on entry(%s)\n",
  1069. acl_access2str(access),
  1070. n_edn);
  1071. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1072. tnf_string,skip_aclcheck,"");
  1073. return LDAP_SUCCESS;
  1074. }
  1075. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  1076. if ( !aclpb ) {
  1077. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 3 \n" );
  1078. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1079. tnf_string,aclpb_error,"");
  1080. return LDAP_OPERATIONS_ERROR;
  1081. }
  1082. /*
  1083. * Am I anonymous? then we can use our anonympous profile
  1084. * We don't require the aclpb to have been initialized for anom stuff
  1085. */
  1086. slapi_pblock_get (pb, SLAPI_REQUESTOR_DN ,&clientDn );
  1087. if (clientDn && (*clientDn == '\0')) {
  1088. ret_val = aclanom_match_profile (pb, aclpb, e, attr, SLAPI_ACL_READ);
  1089. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1090. tnf_string,anon_decision,"");
  1091. if (ret_val != -1) {
  1092. return ret_val;
  1093. }
  1094. }
  1095. /* Then I must have a access to the entry. */
  1096. aclpb->aclpb_state |= ACLPB_ACCESS_ALLOWED_ON_ENTRY;
  1097. if (aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS) {
  1098. ret_val = acl__attr_cached_result (aclpb, attr, SLAPI_ACL_READ);
  1099. if (ret_val != -1) {
  1100. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1101. "MATCHED HANDLE:dn:%s attr: %s val:%d\n",
  1102. n_edn, attr, ret_val );
  1103. if (ret_val == LDAP_SUCCESS) {
  1104. decision_reason.reason = ACL_REASON_EVALCONTEXT_CACHED_ALLOW;
  1105. } else {
  1106. decision_reason.reason = ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED;
  1107. }
  1108. goto acl_access_allowed_on_attr_Exit;
  1109. } else {
  1110. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  1111. }
  1112. }
  1113. if (aclpb->aclpb_state & ACLPB_ACCESS_DENIED_ON_ALL_ATTRS) {
  1114. /* access is denied on all the attributes */
  1115. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1116. tnf_string,deny_all_attrs,"");
  1117. return LDAP_INSUFFICIENT_ACCESS;
  1118. }
  1119. /* do I have access to all the entries by virtue of having aci
  1120. ** rules with targetattr ="*". If yes, then allow access to
  1121. ** rest of the attributes.
  1122. */
  1123. if (aclpb->aclpb_state & ACLPB_ATTR_STAR_MATCHED) {
  1124. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1125. "STAR Access allowed on attr:%s; entry:%s \n",
  1126. attr, n_edn);
  1127. decision_reason.reason = ACL_REASON_EVALCONTEXT_CACHED_ATTR_STAR_ALLOW;
  1128. ret_val = LDAP_SUCCESS;
  1129. goto acl_access_allowed_on_attr_Exit;
  1130. }
  1131. if (aclpb->aclpb_state & ACLPB_ACCESS_ALLOWED_ON_A_ATTR) {
  1132. /* access is allowed on that attr.
  1133. ** for example: Slapi_Entry: cn, sn. phone, uid, passwd, address
  1134. ** We found that access is allowed on phone. That means the
  1135. ** -- access is denied on cn, sn
  1136. ** -- access is allowed on phone
  1137. ** -- Don't know about the rest. Need to evaluate.
  1138. */
  1139. if (slapi_attr_type_cmp(aclpb->aclpb_Evalattr, attr, SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  1140. /* from now on we need to evaluate access on
  1141. ** rest of the attrs.
  1142. */
  1143. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1144. tnf_string,aclp_Evalattr1,"");
  1145. return LDAP_SUCCESS;
  1146. }
  1147. /*
  1148. * Here, the attr that implied access to the entry (aclpb_Evalattr),
  1149. * is not
  1150. * the one we currently want evaluated--so
  1151. * we need to evaluate access to attr--so fall through.
  1152. */
  1153. } else if (aclpb->aclpb_state & ACLPB_ACCESS_ALLOWED_USERATTR) {
  1154. /* Only skip evaluation on the user attr on which we have
  1155. ** evaluated before.
  1156. */
  1157. if (slapi_attr_type_cmp(aclpb->aclpb_Evalattr, attr, SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  1158. aclpb->aclpb_state &= ~ACLPB_ACCESS_ALLOWED_USERATTR;
  1159. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1160. tnf_string,aclp_Evalattr2,"");
  1161. return LDAP_SUCCESS;
  1162. }
  1163. }
  1164. /* we need to evaluate the access on this attr */
  1165. /*
  1166. * targetattr=sn;en
  1167. * search attribute list: cn sn
  1168. * ==>
  1169. * attr: cn sn;en
  1170. * aclpb_Evalattr: sn;en
  1171. */
  1172. return ( acl_access_allowed(pb, e, attr, val, access) );
  1173. /* This exit point prints a summary and returns ret_val */
  1174. acl_access_allowed_on_attr_Exit:
  1175. /* print summary if loglevel set */
  1176. if ( slapi_is_loglevel_set(loglevel) ) {
  1177. print_access_control_summary( "on attr",
  1178. ret_val, clientDn, aclpb,
  1179. acl_access2str(SLAPI_ACL_READ),
  1180. attr, n_edn, &decision_reason);
  1181. }
  1182. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","");
  1183. return(ret_val);
  1184. }
  1185. /***************************************************************************
  1186. *
  1187. * acl_check_mods
  1188. * check access control on the given entry to see if
  1189. * it allows the given modifications by the user associated with op.
  1190. *
  1191. *
  1192. * Input:
  1193. *
  1194. *
  1195. * Returns:
  1196. * LDAP_SUCCESS - mods allowed ok
  1197. * <err> - same return value as acl_access_allowed()
  1198. *
  1199. * Error Handling:
  1200. * None.
  1201. *
  1202. **************************************************************************/
  1203. int
  1204. acl_check_mods(
  1205. Slapi_PBlock *pb,
  1206. Slapi_Entry *e,
  1207. LDAPMod **mods,
  1208. char **errbuf
  1209. )
  1210. {
  1211. int i;
  1212. int rv, accessCheckDisabled;
  1213. int lastmod = 0;
  1214. Slapi_Attr *attr = NULL;
  1215. char *n_edn;
  1216. Slapi_Backend *be = NULL;
  1217. Slapi_DN *e_sdn;
  1218. Acl_PBlock *aclpb = acl_get_aclpb ( pb, ACLPB_PROXYDN_PBLOCK );
  1219. LDAPMod *mod;
  1220. Slapi_Mods smods;
  1221. rv = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  1222. if ( rv != -1 && accessCheckDisabled ) return LDAP_SUCCESS;
  1223. if ( NULL == aclpb )
  1224. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  1225. n_edn = slapi_entry_get_ndn ( e );
  1226. e_sdn = slapi_entry_get_sdn ( e );
  1227. slapi_mods_init_byref(&smods,mods);
  1228. for (mod = slapi_mods_get_first_mod(&smods);
  1229. mod != NULL;
  1230. mod = slapi_mods_get_next_mod(&smods)) {
  1231. if (0 == strcmp(mod->mod_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD)) {
  1232. continue;
  1233. }
  1234. switch (mod->mod_op & ~LDAP_MOD_BVALUES ) {
  1235. case LDAP_MOD_DELETE:
  1236. if (strcmp (mod->mod_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD) == 0) {
  1237. /* skip pseudo attr(s) */
  1238. continue;
  1239. }
  1240. if (mod->mod_bvalues != NULL ) {
  1241. break;
  1242. }
  1243. /*
  1244. * Here, check that we have the right to delete all
  1245. * the values of the attribute in the entry.
  1246. */
  1247. case LDAP_MOD_REPLACE:
  1248. if ( !lastmod ) {
  1249. if (be == NULL) {
  1250. if (slapi_pblock_get( pb, SLAPI_BACKEND, &be )) {
  1251. be = NULL;
  1252. }
  1253. }
  1254. if (be != NULL)
  1255. slapi_pblock_get ( pb, SLAPI_BE_LASTMOD, &lastmod );
  1256. }
  1257. if (lastmod && slapi_attr_is_last_mod(mod->mod_type)) {
  1258. /* skip pseudo attr(s) */
  1259. continue;
  1260. }
  1261. slapi_entry_attr_find (e, mod->mod_type, &attr);
  1262. if ( attr != NULL) {
  1263. Slapi_Value *sval=NULL;
  1264. const struct berval *attrVal=NULL;
  1265. int k= slapi_attr_first_value(attr,&sval);
  1266. while(k != -1) {
  1267. attrVal = slapi_value_get_berval(sval);
  1268. rv = slapi_access_allowed (pb, e,
  1269. mod->mod_type,
  1270. (struct berval *)attrVal, /* XXXggood had to cast away const - BAD */
  1271. ACLPB_SLAPI_ACL_WRITE_DEL); /* was SLAPI_ACL_WRITE */
  1272. if ( rv != LDAP_SUCCESS) {
  1273. acl_gen_err_msg (
  1274. SLAPI_ACL_WRITE,
  1275. n_edn,
  1276. mod->mod_type,
  1277. errbuf);
  1278. /* Cleanup */
  1279. slapi_mods_done(&smods);
  1280. return(rv);
  1281. }
  1282. k= slapi_attr_next_value(attr, k, &sval);
  1283. }
  1284. }
  1285. else {
  1286. rv = slapi_access_allowed (pb, e,
  1287. mod->mod_type,
  1288. NULL,
  1289. ACLPB_SLAPI_ACL_WRITE_DEL); /* was SLAPI_ACL_WRITE */
  1290. if ( rv != LDAP_SUCCESS) {
  1291. acl_gen_err_msg (
  1292. SLAPI_ACL_WRITE,
  1293. n_edn,
  1294. mod->mod_type,
  1295. errbuf);
  1296. /* Cleanup */
  1297. slapi_mods_done(&smods);
  1298. return(rv);
  1299. }
  1300. }
  1301. break;
  1302. default: /* including LDAP_MOD_ADD */
  1303. break;
  1304. } /* switch */
  1305. /*
  1306. * Check that we have add/delete writes on the specific values
  1307. * we are trying to add.
  1308. */
  1309. if ( aclpb && aclpb->aclpb_curr_entry_sdn )
  1310. slapi_sdn_done ( aclpb->aclpb_curr_entry_sdn );
  1311. if ( mod->mod_bvalues != NULL ) {
  1312. /*
  1313. * Here, there are specific values specified.
  1314. * For add and replace--we need add rights for these values.
  1315. * For delete we need delete rights for these values.
  1316. */
  1317. for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
  1318. if (SLAPI_IS_MOD_ADD(mod->mod_op) ||
  1319. SLAPI_IS_MOD_REPLACE(mod->mod_op)) {
  1320. rv = acl_access_allowed (pb,e,
  1321. mod->mod_type,
  1322. mod->mod_bvalues[i],
  1323. ACLPB_SLAPI_ACL_WRITE_ADD); /*was SLAPI_ACL_WRITE*/
  1324. } else if (SLAPI_IS_MOD_DELETE(mod->mod_op)) {
  1325. rv = acl_access_allowed (pb,e,
  1326. mod->mod_type,
  1327. mod->mod_bvalues[i],
  1328. ACLPB_SLAPI_ACL_WRITE_DEL); /*was SLAPI_ACL_WRITE*/
  1329. } else {
  1330. rv = LDAP_INSUFFICIENT_ACCESS;
  1331. }
  1332. if ( rv != LDAP_SUCCESS ) {
  1333. acl_gen_err_msg (
  1334. SLAPI_ACL_WRITE,
  1335. n_edn,
  1336. mod->mod_type,
  1337. errbuf);
  1338. /* Cleanup */
  1339. slapi_mods_done(&smods);
  1340. return rv;
  1341. }
  1342. /* Need to check for all the values because
  1343. ** we may be modifying a "self<right>" value.
  1344. */
  1345. /* Are we adding/replacing a aci attribute
  1346. ** value. In that case, we need to make
  1347. ** sure that the new value has the right
  1348. ** syntax
  1349. */
  1350. if (!SLAPI_IS_MOD_DELETE(mod->mod_op) &&
  1351. strcmp(mod->mod_type, aci_attr_type) == 0)
  1352. {
  1353. if ( 0 != (rv = acl_verify_syntax(pb, e_sdn,
  1354. mod->mod_bvalues[i], errbuf))) {
  1355. aclutil_print_err(rv, e_sdn,
  1356. mod->mod_bvalues[i],
  1357. errbuf);
  1358. /* Cleanup */
  1359. slapi_mods_done(&smods);
  1360. return LDAP_INVALID_SYNTAX;
  1361. }
  1362. }
  1363. } /* for */
  1364. }
  1365. } /* end of big for */
  1366. /* Cleanup */
  1367. slapi_mods_done(&smods);
  1368. return( LDAP_SUCCESS );
  1369. }
  1370. /***************************************************************************
  1371. *
  1372. * acl_modified
  1373. * Modifies ( removed, add, changes) the ACI LIST.
  1374. *
  1375. * Input:
  1376. * int *optype - op code
  1377. * char *dn - DN of the entry
  1378. * void *change - The change struct which contais the
  1379. * - change value
  1380. *
  1381. * Returns:
  1382. * None.
  1383. *
  1384. * Error Handling:
  1385. * None.
  1386. *
  1387. **************************************************************************/
  1388. extern void
  1389. acl_modified (Slapi_PBlock *pb, int optype, Slapi_DN *e_sdn, void *change)
  1390. {
  1391. struct berval **bvalue;
  1392. char **value;
  1393. int rv=0; /* returned value */
  1394. const char* n_dn;
  1395. char* new_RDN;
  1396. char* parent_DN;
  1397. char* new_DN;
  1398. LDAPMod **mods;
  1399. struct berval b;
  1400. int j;
  1401. Slapi_Attr *attr = NULL;
  1402. Slapi_Entry *e = NULL;
  1403. aclUserGroup *ugroup = NULL;
  1404. if (NULL == e_sdn) {
  1405. return;
  1406. }
  1407. n_dn = slapi_sdn_get_dn(e_sdn);
  1408. /* Before we proceed, Let's first check if we are changing any groups.
  1409. ** If we are, then we need to change the signature
  1410. */
  1411. switch ( optype ) {
  1412. case SLAPI_OPERATION_MODIFY:
  1413. case SLAPI_OPERATION_DELETE:
  1414. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, (void*)&e);
  1415. break;
  1416. case SLAPI_OPERATION_ADD:
  1417. e = (Slapi_Entry *)change;
  1418. break;
  1419. }
  1420. /* e can be null for RDN */
  1421. if ( e ) slapi_entry_attr_find( e, "objectclass", &attr);
  1422. if ( attr ) {
  1423. int group_change = 0;
  1424. Slapi_Value *sval=NULL;
  1425. const struct berval *attrVal;
  1426. int i;
  1427. i= slapi_attr_first_value ( attr,&sval );
  1428. while(i != -1) {
  1429. attrVal = slapi_value_get_berval ( sval );
  1430. if ( (strcasecmp (attrVal->bv_val, "groupOfNames") == 0 ) ||
  1431. (strcasecmp (attrVal->bv_val, "groupOfUniqueNames") == 0 ) ||
  1432. (strcasecmp (attrVal->bv_val, "groupOfCertificates") == 0 ) ||
  1433. (strcasecmp (attrVal->bv_val, "groupOfURLs") == 0 ) ) {
  1434. group_change= 1;
  1435. if ( optype == SLAPI_OPERATION_MODIFY ) {
  1436. Slapi_Attr *a = NULL;
  1437. int rv;
  1438. rv = slapi_entry_attr_find ( e, "uniqueMember", &a);
  1439. if ( rv != 0 ) break;
  1440. rv = slapi_entry_attr_find ( e, "Member", &a );
  1441. if ( rv != 0 ) break;
  1442. rv = slapi_entry_attr_find ( e, "MemberURL", &a );
  1443. if ( rv != 0 ) break;
  1444. /* That means we are not changing the member
  1445. ** list, so it's okay to let go this
  1446. ** change
  1447. */
  1448. group_change = 0;
  1449. }
  1450. break;
  1451. }
  1452. i= slapi_attr_next_value ( attr, i, &sval );
  1453. }
  1454. /*
  1455. ** We can do better here XXX, i.e invalidate the cache for users who
  1456. ** use this group. for now just do the whole thing.
  1457. */
  1458. if ( group_change ) {
  1459. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1460. "Group Change: Invalidating entire UserGroup Cache %s\n",
  1461. n_dn);
  1462. aclg_regen_group_signature();
  1463. if ( (optype == SLAPI_OPERATION_MODIFY) || (optype == SLAPI_OPERATION_DELETE ) ) {
  1464. /* Then we need to invalidate the acl signature also */
  1465. acl_signature = aclutil_gen_signature ( acl_signature );
  1466. }
  1467. }
  1468. }
  1469. /*
  1470. * Here if the target entry is in the group cache
  1471. * as a user then, as it's being changed it may move out of any dynamic
  1472. * groups it belongs to.
  1473. * Just remove it for now--can do better XXX by checking to see if
  1474. * it really needs to be removed by testing to see if he's
  1475. * still in th group after the change--but this requires keeping
  1476. * the memberURL of the group which we don't currently do.
  1477. * Also, if we keep
  1478. * the attributes that are being used in dynamic
  1479. * groups then we could only remove the user if a sensitive
  1480. * attribute was being modified (rather than scanning the whole user cache
  1481. * all the time). Also could do a hash lookup.
  1482. *
  1483. * aclg_find_userGroup() incs a refcnt so we can still refer to ugroup.
  1484. * aclg_markUgroupForRemoval() decs it and marks it for removal
  1485. * , so after that you cannot refer to ugroup.
  1486. *
  1487. */
  1488. if ( (ugroup = aclg_find_userGroup(n_dn)) != NULL) {
  1489. /*
  1490. * Mark this for deletion next time round--try to impact
  1491. * this mainline code as little as possible.
  1492. */
  1493. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1494. "Marking entry %s for removal from ACL user Group Cache\n",
  1495. n_dn);
  1496. aclg_markUgroupForRemoval (ugroup);
  1497. }
  1498. /*
  1499. * Take the write lock around all the mods--so that
  1500. * other operations will see the acicache either before the whole mod
  1501. * or after but not, as it was before, during the mod.
  1502. * This is in line with the LDAP concept of the operation
  1503. * on the whole entry being the atomic unit.
  1504. *
  1505. */
  1506. switch(optype) {
  1507. case SLAPI_OPERATION_DELETE:
  1508. /* In this case we have already checked if the user has
  1509. ** right to delete the entry. Part of delete of entry is
  1510. ** remove all the ACLs also.
  1511. */
  1512. acllist_acicache_WRITE_LOCK();
  1513. rv = acllist_remove_aci_needsLock(e_sdn, NULL);
  1514. acllist_acicache_WRITE_UNLOCK();
  1515. break;
  1516. case SLAPI_OPERATION_ADD:
  1517. slapi_entry_attr_find ( (Slapi_Entry *) change, aci_attr_type, &attr );
  1518. if ( attr ) {
  1519. Slapi_Value *sval=NULL;
  1520. const struct berval *attrVal;
  1521. int i;
  1522. acllist_acicache_WRITE_LOCK();
  1523. i= slapi_attr_first_value ( attr,&sval );
  1524. while ( i != -1 ) {
  1525. attrVal = slapi_value_get_berval(sval);
  1526. rv= acllist_insert_aci_needsLock_ext(pb, e_sdn, attrVal );
  1527. if (rv <= ACL_ERR)
  1528. aclutil_print_err(rv, e_sdn, attrVal, NULL);
  1529. /* Print the aci list */
  1530. i= slapi_attr_next_value ( attr, i, &sval );
  1531. }
  1532. acllist_acicache_WRITE_UNLOCK();
  1533. }
  1534. break;
  1535. case SLAPI_OPERATION_MODIFY:
  1536. {
  1537. int got_write_lock = 0;
  1538. mods = (LDAPMod **) change;
  1539. for (j=0; mods && mods[j]; j++) {
  1540. if (slapi_attr_type_cmp(mods[j]->mod_type, aci_attr_type,
  1541. SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  1542. /* Got an aci to mod in this list of mods, so
  1543. * take the acicache lock for the whole list of mods,
  1544. * remembering to free it below.
  1545. */
  1546. if ( !got_write_lock) {
  1547. acllist_acicache_WRITE_LOCK();
  1548. got_write_lock = 1;
  1549. }
  1550. switch (mods[j]->mod_op & ~LDAP_MOD_BVALUES) {
  1551. case LDAP_MOD_REPLACE:
  1552. /* First remove the item */
  1553. rv = acllist_remove_aci_needsLock(e_sdn, NULL);
  1554. /* now fall thru to add the new one */
  1555. case LDAP_MOD_ADD:
  1556. /* Add the new aci */
  1557. if (mods[j]->mod_op & LDAP_MOD_BVALUES) {
  1558. bvalue = mods[j]->mod_bvalues;
  1559. if (bvalue == NULL)
  1560. break;
  1561. for (; *bvalue != NULL; ++bvalue) {
  1562. rv=acllist_insert_aci_needsLock_ext(pb, e_sdn, *bvalue);
  1563. if (rv <= ACL_ERR) {
  1564. aclutil_print_err(rv, e_sdn,
  1565. *bvalue, NULL);
  1566. }
  1567. }
  1568. } else {
  1569. value = mods[j]->mod_values;
  1570. if (value == NULL)
  1571. break;
  1572. for (; *value != NULL; ++value) {
  1573. b.bv_len = strlen (*value);
  1574. b.bv_val = *value;
  1575. rv=acllist_insert_aci_needsLock_ext(pb, e_sdn, &b);
  1576. if (rv <= ACL_ERR) {
  1577. aclutil_print_err(rv, e_sdn,
  1578. &b, NULL);
  1579. }
  1580. }
  1581. }
  1582. break;
  1583. case LDAP_MOD_DELETE:
  1584. if (mods[j]->mod_op & LDAP_MOD_BVALUES) {
  1585. bvalue = mods[j]->mod_bvalues;
  1586. if (bvalue == NULL || *bvalue == NULL) {
  1587. rv = acllist_remove_aci_needsLock( e_sdn, NULL);
  1588. } else {
  1589. for (; *bvalue != NULL; ++bvalue)
  1590. acllist_remove_aci_needsLock( e_sdn, *bvalue);
  1591. }
  1592. } else {
  1593. value = mods[j]->mod_values;
  1594. if (value == NULL || *value == NULL) {
  1595. acllist_remove_aci_needsLock( e_sdn,NULL);
  1596. } else {
  1597. for (; *value != NULL; ++value) {
  1598. b.bv_len = strlen (*value);
  1599. b.bv_val = *value;
  1600. acllist_remove_aci_needsLock( e_sdn, &b);
  1601. }
  1602. }
  1603. }
  1604. break;
  1605. default:
  1606. break;
  1607. }/* modtype switch */
  1608. }/* attrtype is aci */
  1609. } /* end of for */
  1610. if ( got_write_lock ) {
  1611. acllist_acicache_WRITE_UNLOCK();
  1612. got_write_lock = 0;
  1613. }
  1614. break;
  1615. }/* case op is modify*/
  1616. case SLAPI_OPERATION_MODRDN:
  1617. {
  1618. char **rdn_parent;
  1619. rdn_parent = (char **)change;
  1620. new_RDN = rdn_parent[0];
  1621. parent_DN = rdn_parent[1];
  1622. /* compute new_DN: */
  1623. if (NULL == parent_DN) {
  1624. parent_DN = slapi_dn_parent(n_dn);
  1625. }
  1626. if (NULL == parent_DN) {
  1627. if (NULL == new_RDN) {
  1628. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1629. "acl_modified (MODRDN %s => \"no change\"\n",
  1630. n_dn);
  1631. break;
  1632. } else {
  1633. new_DN = new_RDN;
  1634. }
  1635. } else {
  1636. if (NULL == new_RDN) {
  1637. Slapi_RDN *rdn= slapi_rdn_new();
  1638. slapi_sdn_get_rdn(e_sdn, rdn);
  1639. new_DN = slapi_create_dn_string("%s,%s", slapi_rdn_get_rdn(rdn),
  1640. parent_DN);
  1641. slapi_rdn_free(&rdn);
  1642. } else {
  1643. new_DN = slapi_create_dn_string("%s,%s", new_RDN, parent_DN);
  1644. }
  1645. }
  1646. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1647. "acl_modified (MODRDN %s => \"%s\"\n", n_dn, new_RDN);
  1648. /* Change the acls */
  1649. acllist_acicache_WRITE_LOCK();
  1650. /* acllist_moddn_aci_needsLock expects normalized new_DN,
  1651. * which is no need to be case-ignored */
  1652. acllist_moddn_aci_needsLock ( e_sdn, new_DN );
  1653. acllist_acicache_WRITE_UNLOCK();
  1654. /* deallocat the parent_DN */
  1655. if (parent_DN != NULL) {
  1656. slapi_ch_free_string(&new_DN);
  1657. if (parent_DN != rdn_parent[1]) {
  1658. slapi_ch_free_string(&parent_DN);
  1659. }
  1660. }
  1661. break;
  1662. } /* case op is modrdn */
  1663. default:
  1664. /* print ERROR */
  1665. break;
  1666. } /*optype switch */
  1667. }
  1668. /***************************************************************************
  1669. *
  1670. * acl__scan_for_acis
  1671. * Scan the list and picup the correct acls for evaluation.
  1672. *
  1673. * Input:
  1674. * Acl_PBlock *aclpb - Main ACL pblock
  1675. * int *err; - Any error status
  1676. * Returns:
  1677. * num_handles - Number of handles matched to the
  1678. * - resource + 1.
  1679. * Error Handling:
  1680. * None.
  1681. *
  1682. **************************************************************************/
  1683. static int
  1684. acl__scan_for_acis(Acl_PBlock *aclpb, int *err)
  1685. {
  1686. aci_t *aci;
  1687. NSErr_t errp;
  1688. int attr_matched;
  1689. int deny_handle;
  1690. int allow_handle;
  1691. int gen_allow_handle = ACI_MAX_ELEVEL+1;
  1692. int gen_deny_handle = ACI_MAX_ELEVEL+1;
  1693. PRUint32 cookie;
  1694. TNF_PROBE_0_DEBUG(acl__scan_for_acis_start,"ACL","");
  1695. /*
  1696. ** Determine if we are traversing via the list Vs. we have our own
  1697. ** generated list
  1698. */
  1699. if ( aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST ||
  1700. aclpb->aclpb_handles_index[0] != -1 ) {
  1701. int kk = 0;
  1702. while ( kk < aclpb_max_selected_acls && aclpb->aclpb_handles_index[kk] != -1 ) {
  1703. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Using ACL Container:%d for evaluation\n", kk);
  1704. kk++;
  1705. }
  1706. }
  1707. memset (&errp, 0, sizeof(NSErr_t));
  1708. *err = ACL_FALSE;
  1709. aclpb->aclpb_num_deny_handles = -1;
  1710. aclpb->aclpb_num_allow_handles = -1;
  1711. memset(aclpb->aclpb_deny_handles, 0, sizeof(aci_t *)*(ACI_MAX_ELEVEL+1));
  1712. memset(aclpb->aclpb_allow_handles, 0, sizeof(aci_t *)*(ACI_MAX_ELEVEL+1));
  1713. /* Check the signature. If it has changed, start fresh */
  1714. if ( aclpb->aclpb_signature != acl_signature ) {
  1715. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1716. "Restart the scan -- due to acl changes\n");
  1717. acllist_init_scan ( aclpb->aclpb_pblock, LDAP_SCOPE_BASE, NULL );
  1718. }
  1719. attr_matched = ACL_FALSE;
  1720. deny_handle = 0;
  1721. allow_handle = 0;
  1722. aclpb->aclpb_stat_acllist_scanned++;
  1723. aci = acllist_get_first_aci ( aclpb, &cookie );
  1724. while( aci ) {
  1725. if (acl__resource_match_aci(aclpb, aci, 0, &attr_matched)) {
  1726. /* Generate the ACL list handle */
  1727. if (aci->aci_handle == NULL) {
  1728. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  1729. continue;
  1730. }
  1731. aclutil_print_aci (aci, acl_access2str (aclpb->aclpb_access));
  1732. if (aci->aci_type & ACI_HAS_DENY_RULE) {
  1733. if (aclpb->aclpb_deny_handles[aci->aci_elevel] == NULL ) {
  1734. aclpb->aclpb_deny_handles[aci->aci_elevel] = aci;
  1735. } else {
  1736. if ((gen_deny_handle + ACI_DEFAULT_ELEVEL + 1) ==
  1737. aclpb->aclpb_deny_handles_size ) {
  1738. int num = ACLPB_INCR_LIST_HANDLES +
  1739. aclpb->aclpb_deny_handles_size;
  1740. /* allocate more space */
  1741. aclpb->aclpb_deny_handles =
  1742. (aci_t **)
  1743. slapi_ch_realloc (
  1744. (void *) aclpb->aclpb_deny_handles,
  1745. num * sizeof (aci_t *));
  1746. aclpb->aclpb_deny_handles_size = num;
  1747. }
  1748. aclpb->aclpb_deny_handles [gen_deny_handle] = aci;
  1749. gen_deny_handle++;
  1750. }
  1751. deny_handle++;
  1752. }
  1753. /*
  1754. ** It's possible that a single acl is in both the camps i.e
  1755. ** It has a allow and a deny rule
  1756. ** In that case we keep the same acl in both the camps.
  1757. */
  1758. if (aci->aci_type & ACI_HAS_ALLOW_RULE) {
  1759. if (aclpb->aclpb_allow_handles[aci->aci_elevel] == NULL ) {
  1760. aclpb->aclpb_allow_handles[aci->aci_elevel] = aci;
  1761. } else {
  1762. if ((gen_allow_handle + ACI_DEFAULT_ELEVEL + 1) ==
  1763. aclpb->aclpb_allow_handles_size) {
  1764. /* allocate more space */
  1765. int num = ACLPB_INCR_LIST_HANDLES +
  1766. aclpb->aclpb_allow_handles_size;
  1767. aclpb->aclpb_allow_handles =
  1768. (aci_t **)
  1769. slapi_ch_realloc (
  1770. (void *) aclpb->aclpb_allow_handles,
  1771. num * sizeof (aci_t *));
  1772. aclpb->aclpb_allow_handles_size = num;
  1773. }
  1774. aclpb->aclpb_allow_handles [gen_allow_handle] = aci;
  1775. gen_allow_handle++;
  1776. }
  1777. allow_handle++;
  1778. }
  1779. }
  1780. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  1781. } /* end of while */
  1782. /* make the last one a null */
  1783. aclpb->aclpb_deny_handles [gen_deny_handle] = NULL;
  1784. aclpb->aclpb_allow_handles [gen_allow_handle] = NULL;
  1785. /* specify how many we found */
  1786. aclpb->aclpb_num_deny_handles = deny_handle;
  1787. aclpb->aclpb_num_allow_handles = allow_handle;
  1788. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Num of ALLOW Handles:%d, DENY handles:%d\n",
  1789. aclpb->aclpb_num_allow_handles, aclpb->aclpb_num_deny_handles);
  1790. TNF_PROBE_0_DEBUG(acl__scan_for_acis_end,"ACL","");
  1791. return(allow_handle + deny_handle);
  1792. }
  1793. /***************************************************************************
  1794. *
  1795. * acl__resource_match_aci
  1796. *
  1797. * This compares the ACI for the given resource and determines if
  1798. * the ACL applies to the resource or not.
  1799. *
  1800. * For read/search operation, we collect all the possible acls which
  1801. * will apply, We will be using this list for future acl evaluation
  1802. * for that entry.
  1803. *
  1804. * Input:
  1805. * struct acl_pblock *aclpb - Main acl private block
  1806. * aci_t *aci - The ACI item
  1807. * int skip_attrEval - DOn't check for attrs
  1808. * int *a_matched - Attribute matched
  1809. *
  1810. * Returns:
  1811. *
  1812. * ACL_TRUE - Yep, This ACL is applicable to the
  1813. * - the resource.
  1814. * ACL_FALSE - No it is not.
  1815. *
  1816. * Error Handling:
  1817. * None.
  1818. *
  1819. **************************************************************************/
  1820. #define ACL_RIGHTS_TARGETATTR_NOT_NEEDED ( SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_PROXY | SLAPI_ACL_MODDN)
  1821. static int
  1822. acl__resource_match_aci( Acl_PBlock *aclpb, aci_t *aci, int skip_attrEval, int *a_matched)
  1823. {
  1824. struct slapi_filter *f; /* filter */
  1825. int rv; /* return value */
  1826. /* Assume that resource matches */
  1827. int matches = ACL_TRUE;
  1828. int attr_matched = ACL_TRUE;
  1829. int attr_matched_in_targetattrfilters = 0;
  1830. int dn_matched;
  1831. char *res_attr;
  1832. int aci_right = 0;
  1833. int res_right = 0;
  1834. int star_matched = ACL_FALSE;
  1835. int num_attrs = 0;
  1836. AclAttrEval *c_attrEval = NULL;
  1837. const char *res_ndn = NULL;
  1838. const char *aci_ndn = NULL;
  1839. char *matched_val = NULL;
  1840. int add_matched_val_to_ht = 0;
  1841. char res_right_str[128];
  1842. TNF_PROBE_0_DEBUG(acl__resource_match_aci_start,"ACL","");
  1843. if (NULL == aclpb) {
  1844. matches = ACL_FALSE;
  1845. goto acl__resource_match_aci_EXIT;
  1846. }
  1847. /* Figure out if the acl has the correct rights or not */
  1848. aci_right = aci->aci_access;
  1849. res_right = aclpb->aclpb_access;
  1850. if (!(aci_right & res_right)) {
  1851. /* If we are looking for read/search and the acl has read/search
  1852. ** then go further because if targets match we may keep that
  1853. ** acl in the entry cache list.
  1854. */
  1855. if (!((res_right & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) &&
  1856. (aci_right & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)))){
  1857. matches = ACL_FALSE;
  1858. goto acl__resource_match_aci_EXIT;
  1859. }
  1860. }
  1861. /* first Let's see if the entry is under the subtree where the
  1862. ** ACL resides. We can't let somebody affect a target beyond the
  1863. ** scope of where the ACL resides
  1864. ** Example: ACL is located in "ou=engineering, o=ace industry, c=us
  1865. ** but if the target is "o=ace industry, c=us", then we are in trouble.
  1866. **
  1867. ** If the aci is in the rootdse and the entry is not, then we do not
  1868. ** match--ie. acis in the rootdse do NOT apply below...for the moment.
  1869. **
  1870. */
  1871. res_ndn = slapi_sdn_get_ndn ( aclpb->aclpb_curr_entry_sdn );
  1872. aci_ndn = slapi_sdn_get_ndn ( aci->aci_sdn );
  1873. if (!slapi_sdn_issuffix(aclpb->aclpb_curr_entry_sdn, aci->aci_sdn)
  1874. || (!slapi_is_rootdse(res_ndn) && slapi_is_rootdse(aci_ndn)) ) {
  1875. /* cant' poke around */
  1876. matches = ACL_FALSE;
  1877. goto acl__resource_match_aci_EXIT;
  1878. }
  1879. /*
  1880. ** We have a single ACI which we need to find if it applies to
  1881. ** the resource or not.
  1882. */
  1883. if ((aci->aci_type & ACI_TARGET_DN) && (aclpb->aclpb_curr_entry_sdn)) {
  1884. char *avaType;
  1885. struct berval *avaValue;
  1886. f = aci->target;
  1887. dn_matched = ACL_TRUE;
  1888. slapi_filter_get_ava ( f, &avaType, &avaValue );
  1889. if (!slapi_dn_issuffix( res_ndn, avaValue->bv_val)) {
  1890. dn_matched = ACL_FALSE;
  1891. }
  1892. if (aci->aci_type & ACI_TARGET_NOT) {
  1893. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1894. } else {
  1895. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  1896. }
  1897. }
  1898. /* No need to look further */
  1899. if (matches == ACL_FALSE) {
  1900. goto acl__resource_match_aci_EXIT;
  1901. }
  1902. if ((aci->aci_type & ACI_TARGET_MODDN) && (res_right & SLAPI_ACL_MODDN) && (aclpb->aclpb_curr_entry_sdn)) {
  1903. char *avaType;
  1904. struct berval *avaValue;
  1905. char logbuf[1024];
  1906. /* We are evaluating the moddn permission.
  1907. * The aci contains target_to and target_from
  1908. *
  1909. * target_to filter must be checked against the resource ndn that was stored in
  1910. * aclpb->aclpb_curr_entry_sdn
  1911. *
  1912. * target_from filter must be check against the entry ndn that is in aclpb->aclpb_moddn_source_sdn
  1913. * (sdn was stored in the pblock)
  1914. */
  1915. if (aci->target_to) {
  1916. f = aci->target_to;
  1917. dn_matched = ACL_TRUE;
  1918. /* Now check if the filter is a simple or substring filter */
  1919. if (aci->aci_type & ACI_TARGET_MODDN_TO_PATTERN) {
  1920. /* This is a filter with substring
  1921. * e.g. ldap:///uid=*,cn=accounts,dc=example,dc=com
  1922. */
  1923. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "moddn target_to substring: %s\n",
  1924. slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
  1925. if ((rv = acl_match_substring(f, (char *) res_ndn, 0 /* match suffix */)) != ACL_TRUE) {
  1926. dn_matched = ACL_FALSE;
  1927. if (rv == ACL_ERR) {
  1928. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1929. "acl__resource_match_aci:pattern err\n");
  1930. matches = ACL_FALSE;
  1931. goto acl__resource_match_aci_EXIT;
  1932. }
  1933. }
  1934. } else {
  1935. /* This is a filter without substring
  1936. * e.g. ldap:///cn=accounts,dc=example,dc=com
  1937. */
  1938. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "moddn target_to: %s\n",
  1939. slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
  1940. slapi_filter_get_ava(f, &avaType, &avaValue);
  1941. if (!slapi_dn_issuffix(res_ndn, avaValue->bv_val)) {
  1942. dn_matched = ACL_FALSE;
  1943. }
  1944. }
  1945. if (aci->aci_type & ACI_TARGET_NOT) {
  1946. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1947. } else {
  1948. matches = (dn_matched ? ACL_TRUE : ACL_FALSE);
  1949. }
  1950. } /* target_to */
  1951. if ((matches == ACL_TRUE) && (aci->target_from) && aclpb->aclpb_moddn_source_sdn) {
  1952. f = aci->target_from;
  1953. dn_matched = ACL_TRUE;
  1954. slapi_filter_get_ava ( f, &avaType, &avaValue );
  1955. /* Now check if the filter is a simple or substring filter */
  1956. if (aci->aci_type & ACI_TARGET_MODDN_FROM_PATTERN) {
  1957. /* This is a filter with substring
  1958. * e.g. ldap:///uid=*,cn=accounts,dc=example,dc=com
  1959. */
  1960. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "moddn target_from substring: %s\n",
  1961. slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
  1962. if ((rv = acl_match_substring(f, (char *) slapi_sdn_get_dn(aclpb->aclpb_moddn_source_sdn), 0 /* match suffix */)) != ACL_TRUE) {
  1963. dn_matched = ACL_FALSE;
  1964. if (rv == ACL_ERR) {
  1965. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1966. "acl__resource_match_aci:pattern err\n");
  1967. matches = ACL_FALSE;
  1968. goto acl__resource_match_aci_EXIT;
  1969. }
  1970. }
  1971. } else {
  1972. /* This is a filter without substring
  1973. * e.g. ldap:///cn=accounts,dc=example,dc=com
  1974. */
  1975. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "moddn target_from: %s\n",
  1976. slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
  1977. if (!slapi_dn_issuffix(slapi_sdn_get_dn(aclpb->aclpb_moddn_source_sdn), avaValue->bv_val)) {
  1978. dn_matched = ACL_FALSE;
  1979. }
  1980. }
  1981. if (aci->aci_type & ACI_TARGET_NOT) {
  1982. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1983. } else {
  1984. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  1985. }
  1986. }
  1987. }
  1988. /* No need to look further */
  1989. if (matches == ACL_FALSE) {
  1990. goto acl__resource_match_aci_EXIT;
  1991. }
  1992. if (aci->aci_type & ACI_TARGET_PATTERN) {
  1993. f = aci->target;
  1994. dn_matched = ACL_TRUE;
  1995. if ((rv = acl_match_substring(f, (char *)res_ndn, 0 /* match suffux */)) != ACL_TRUE) {
  1996. dn_matched = ACL_FALSE;
  1997. if(rv == ACL_ERR) {
  1998. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1999. "acl__resource_match_aci:pattern err\n");
  2000. matches = ACL_FALSE;
  2001. goto acl__resource_match_aci_EXIT;
  2002. }
  2003. }
  2004. if (aci->aci_type & ACI_TARGET_NOT) {
  2005. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  2006. } else {
  2007. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  2008. }
  2009. }
  2010. /* No need to look further */
  2011. if (matches == ACL_FALSE) {
  2012. goto acl__resource_match_aci_EXIT;
  2013. }
  2014. /*
  2015. * Is it a (target="ldap://cn=*,($dn),o=sun.com") kind of thing.
  2016. */
  2017. if (aci->aci_type & ACI_TARGET_MACRO_DN) {
  2018. /*
  2019. * See if the ($dn) component matches the string and
  2020. * retrieve the matched substring for later use
  2021. * in the userdn.
  2022. * The macro string is a function of the dn only, so if the
  2023. * entry is the same one don't recalculate it--
  2024. * this flag only works for search right now, could
  2025. * also optimise for mods by making it work for mods.
  2026. */
  2027. if ( (aclpb->aclpb_res_type & ACLPB_NEW_ENTRY) == 0 ) {
  2028. /*
  2029. * Here same entry so just look up the matched value,
  2030. * calculated from the targetdn and stored judiciously there
  2031. */
  2032. matched_val = (char *)acl_ht_lookup( aclpb->aclpb_macro_ht,
  2033. (PLHashNumber)aci->aci_index);
  2034. } else {
  2035. /* new entry, remove macro evaluation from hash table */
  2036. acl_ht_remove_and_free( aclpb->aclpb_macro_ht,
  2037. (PLHashNumber)aci->aci_index);
  2038. }
  2039. if ( matched_val == NULL &&
  2040. (aclpb->aclpb_res_type & (ACLPB_NEW_ENTRY | ACLPB_EFFECTIVE_RIGHTS))) {
  2041. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2042. "Evaluating macro aci(%d)%s for resource %s\n",
  2043. aci->aci_index, aci->aclName,
  2044. aclutil__access_str(res_right, res_right_str));
  2045. matched_val = acl_match_macro_in_target( res_ndn,
  2046. aci->aci_macro->match_this,
  2047. aci->aci_macro->macro_ptr);
  2048. add_matched_val_to_ht = 1; /* may need to add matched value to ht */
  2049. }
  2050. if (matched_val == NULL) {
  2051. dn_matched = ACL_FALSE;
  2052. } else {
  2053. dn_matched = ACL_TRUE;
  2054. }
  2055. if (aci->aci_type & ACI_TARGET_NOT) {
  2056. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  2057. } else {
  2058. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  2059. }
  2060. if ( add_matched_val_to_ht ) {
  2061. if ( matches == ACL_TRUE && matched_val ) {
  2062. /*
  2063. * matched_val may be needed later for matching on
  2064. * other targets or on the subject--so optimistically
  2065. * put it in the hash table.
  2066. * If, at the end of this routine, we
  2067. * find that after all the resource did not match then
  2068. * that's ok--the value is freed at aclpb cleanup time.
  2069. * If there is already an entry for this aci in this
  2070. * aclpb then remove it--it's an old value for a
  2071. * different entry.
  2072. */
  2073. acl_ht_add_and_freeOld(aclpb->aclpb_macro_ht,
  2074. (PLHashNumber)aci->aci_index,
  2075. matched_val);
  2076. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2077. "-- Added aci(%d) and matched value (%s) to macro ht\n",
  2078. aci->aci_index, matched_val);
  2079. acl_ht_display_ht(aclpb->aclpb_macro_ht);
  2080. } else {
  2081. slapi_ch_free((void **)&matched_val);
  2082. if (matches == ACL_FALSE) {
  2083. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2084. "Evaluated ACL_FALSE\n");
  2085. }
  2086. }
  2087. }
  2088. } /* MACRO_DN */
  2089. /* No need to look further */
  2090. if (matches == ACL_FALSE) {
  2091. goto acl__resource_match_aci_EXIT;
  2092. }
  2093. /*
  2094. ** Here, if there's a targetfilter field, see if it matches.
  2095. **
  2096. ** The commented out code below was an erroneous attempt to skip
  2097. ** this test. It is wrong because: 1. you need to store
  2098. ** whether the last test matched or not (you cannot just assume it did)
  2099. ** and 2. It may not be the same aci, so the previous matched
  2100. ** value is a function of the aci.
  2101. ** May be interesting to build such a cache...but no evidence for
  2102. ** for that right now. See Bug 383424.
  2103. **
  2104. **
  2105. ** && ((aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST) ||
  2106. ** (aclpb->aclpb_res_type & ACLPB_NEW_ENTRY))
  2107. */
  2108. if (aci->aci_type & ACI_TARGET_FILTER ) {
  2109. int filter_matched = ACL_TRUE;
  2110. /*
  2111. * Check for macros.
  2112. * For targetfilter we need to fake the lasinfo structure--it's
  2113. * created "naturally" for subjects but not targets.
  2114. */
  2115. if ( aci->aci_type & ACI_TARGET_FILTER_MACRO_DN) {
  2116. lasInfo *lasinfo = NULL;
  2117. lasinfo = (lasInfo*) slapi_ch_malloc( sizeof(lasInfo) );
  2118. lasinfo->aclpb = aclpb;
  2119. lasinfo->resourceEntry = aclpb->aclpb_curr_entry;
  2120. aclpb->aclpb_curr_aci = aci;
  2121. filter_matched = aclutil_evaluate_macro( aci->targetFilterStr,
  2122. lasinfo,
  2123. ACL_EVAL_TARGET_FILTER);
  2124. slapi_ch_free((void**)&lasinfo);
  2125. } else {
  2126. if (slapi_vattr_filter_test(NULL, aclpb->aclpb_curr_entry,
  2127. aci->targetFilter,
  2128. 0 /*don't do acess chk*/)!= 0) {
  2129. filter_matched = ACL_FALSE;
  2130. }
  2131. }
  2132. /* If it's a logical value we can do logic on it...otherwise we do not match */
  2133. if ( filter_matched == ACL_TRUE || filter_matched == ACL_FALSE) {
  2134. if (aci->aci_type & ACI_TARGET_FILTER_NOT) {
  2135. matches = (filter_matched == ACL_TRUE ? ACL_FALSE : ACL_TRUE);
  2136. } else {
  2137. matches = (filter_matched == ACL_TRUE ? ACL_TRUE: ACL_FALSE);
  2138. }
  2139. } else {
  2140. matches = ACL_FALSE;
  2141. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2142. "Returning UNDEFINED for targetfilter evaluation.\n");
  2143. }
  2144. if (matches == ACL_FALSE) {
  2145. goto acl__resource_match_aci_EXIT;
  2146. }
  2147. }
  2148. /*
  2149. * Check to see if we need to evaluate any targetattrfilters.
  2150. * They look as follows:
  2151. * (targetattrfilters="add=sn:(sn=rob) && gn:(gn!=byrne),
  2152. * del=sn:(sn=rob) && gn:(gn=byrne)")
  2153. *
  2154. * For ADD/DELETE:
  2155. * If theres's a targetattrfilter then each add/del filter
  2156. * that applies to an attribute in the entry, must be satisfied
  2157. * by each value of the attribute in the entry.
  2158. *
  2159. * For MODIFY:
  2160. * If there's a targetattrfilter then the add/del filter
  2161. * must be satisfied by the attribute to be added/deleted.
  2162. * (MODIFY acl is evaluated one value at a time).
  2163. *
  2164. *
  2165. */
  2166. if (((aclpb->aclpb_access & SLAPI_ACL_ADD) &&
  2167. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS) )||
  2168. ((aclpb->aclpb_access & SLAPI_ACL_DELETE) &&
  2169. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS) ) ) {
  2170. Targetattrfilter **attrFilterArray;
  2171. Targetattrfilter *attrFilter = NULL;
  2172. Slapi_Attr *attr_ptr = NULL;
  2173. Slapi_Value *sval;
  2174. const struct berval *attrVal;
  2175. int k;
  2176. int done;
  2177. if ((aclpb->aclpb_access & SLAPI_ACL_ADD) &&
  2178. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS)) {
  2179. attrFilterArray = aci->targetAttrAddFilters;
  2180. } else if ((aclpb->aclpb_access & SLAPI_ACL_DELETE) &&
  2181. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS)) {
  2182. attrFilterArray = aci->targetAttrDelFilters;
  2183. }
  2184. attr_matched = ACL_TRUE;
  2185. num_attrs = 0;
  2186. while (attrFilterArray && attrFilterArray[num_attrs] && attr_matched) {
  2187. attrFilter = attrFilterArray[num_attrs];
  2188. /*
  2189. * If this filter applies to an attribute in the entry,
  2190. * apply it to the entry.
  2191. * Otherwise just ignore it.
  2192. *
  2193. */
  2194. if (slapi_entry_attr_find ( aclpb->aclpb_curr_entry,
  2195. attrFilter->attr_str,
  2196. &attr_ptr) == 0) {
  2197. /*
  2198. * This is an applicable filter.
  2199. * The filter is to be appplied to the entry being added
  2200. * or deleted.
  2201. * The filter needs to be satisfied by _each_ occurence
  2202. * of the attribute in the entry--otherwise you
  2203. * could satisfy the filter and then put loads of other
  2204. * values in on the back of it.
  2205. */
  2206. sval=NULL;
  2207. attrVal=NULL;
  2208. k= slapi_attr_first_value(attr_ptr,&sval);
  2209. done = 0;
  2210. while(k != -1 && !done) {
  2211. attrVal = slapi_value_get_berval(sval);
  2212. if (acl__make_filter_test_entry(&aclpb->aclpb_filter_test_entry,
  2213. attrFilter->attr_str,
  2214. (struct berval *)attrVal) == LDAP_SUCCESS ) {
  2215. attr_matched = acl__test_filter(aclpb->aclpb_filter_test_entry,
  2216. attrFilter->filter,
  2217. 1 /* Do filter sense evaluation below */
  2218. );
  2219. done = !attr_matched;
  2220. slapi_entry_free( aclpb->aclpb_filter_test_entry );
  2221. }
  2222. k= slapi_attr_next_value(attr_ptr, k, &sval);
  2223. }/* while */
  2224. /*
  2225. * Here, we applied an applicable filter to the entry.
  2226. * So if attr_matched is ACL_TRUE then every value
  2227. * of the attribute in the entry satisfied the filter.
  2228. * Otherwise, attr_matched is ACL_FALSE and not every
  2229. * value satisfied the filter, so we will teminate the
  2230. * scan of the filter list.
  2231. */
  2232. }
  2233. num_attrs++;
  2234. } /* while */
  2235. /*
  2236. * Here, we've applied all the applicable filters to the entry.
  2237. * Each one must have been satisfied by all the values of the attribute.
  2238. * The result of this is stored in attr_matched.
  2239. */
  2240. #if 0
  2241. /*
  2242. * Don't support a notion of "add != " or "del != "
  2243. * at the moment.
  2244. * To do this, need to test whether it's an add test or del test
  2245. * then if it's add and ACI_TARGET_ATTR_ADD_FILTERS_NOT then
  2246. * flip the bit. Same for del.
  2247. */
  2248. if (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS_NOT) {
  2249. matches = (matches ? ACL_FALSE : ACL_TRUE);
  2250. } else {
  2251. matches = (matches ? ACL_TRUE: ACL_FALSE);
  2252. }
  2253. #endif
  2254. /* No need to look further */
  2255. if (attr_matched == ACL_FALSE) {
  2256. matches = ACL_FALSE;
  2257. goto acl__resource_match_aci_EXIT;
  2258. }
  2259. } else if ( ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_ADD) &&
  2260. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS)) ||
  2261. ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_DEL) &&
  2262. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS)) ) {
  2263. /*
  2264. * Here, it's a modify add/del and we have attr filters.
  2265. * So, we need to scan the add/del filter list to find the filter
  2266. * that applies to the current attribute.
  2267. * Then the (attribute,value) pair being added/deleted better
  2268. * match that filter.
  2269. *
  2270. *
  2271. */
  2272. Targetattrfilter **attrFilterArray = NULL;
  2273. Targetattrfilter *attrFilter;
  2274. int found = 0;
  2275. if ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_ADD) &&
  2276. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS)) {
  2277. attrFilterArray = aci->targetAttrAddFilters;
  2278. } else if ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_DEL) &&
  2279. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS)) {
  2280. attrFilterArray = aci->targetAttrDelFilters;
  2281. }
  2282. /*
  2283. * Scan this filter list for an applicable filter.
  2284. */
  2285. found = 0;
  2286. num_attrs = 0;
  2287. while (attrFilterArray[num_attrs] && !found) {
  2288. attrFilter = attrFilterArray[num_attrs];
  2289. /* If this filter applies to the attribute, stop. */
  2290. if ((aclpb->aclpb_curr_attrEval) &&
  2291. slapi_attr_type_cmp(aclpb->aclpb_curr_attrEval->attrEval_name,
  2292. attrFilter->attr_str, SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  2293. found = 1;
  2294. }
  2295. num_attrs++;
  2296. }
  2297. /*
  2298. * Here, if found an applicable filter, then apply the filter to the
  2299. * (attr,val) pair.
  2300. * Otherwise, ignore the targetattrfilters.
  2301. */
  2302. if (found) {
  2303. if (acl__make_filter_test_entry(&aclpb->aclpb_filter_test_entry,
  2304. aclpb->aclpb_curr_attrEval->attrEval_name,
  2305. aclpb->aclpb_curr_attrVal) == LDAP_SUCCESS ) {
  2306. attr_matched= acl__test_filter(aclpb->aclpb_filter_test_entry,
  2307. attrFilter->filter,
  2308. 1 /* Do filter sense evaluation below */
  2309. );
  2310. slapi_entry_free( aclpb->aclpb_filter_test_entry );
  2311. }
  2312. /* No need to look further */
  2313. if (attr_matched == ACL_FALSE) {
  2314. matches = attr_matched;
  2315. goto acl__resource_match_aci_EXIT;
  2316. }
  2317. /*
  2318. * Here this attribute appeared and was matched in a
  2319. * targetattrfilters list, so record this fact so we do
  2320. * not have to scan the targetattr list for the attribute.
  2321. */
  2322. attr_matched_in_targetattrfilters = 1;
  2323. }
  2324. } /* targetvaluefilters */
  2325. /* There are 3 cases by which acis are selected.
  2326. ** 1) By scanning the whole list and picking based on the resource.
  2327. ** 2) By picking a subset of the list which will be used for the whole
  2328. ** acl evaluation.
  2329. ** 3) A finer granularity, i.e, a selected list of acls which will be
  2330. ** used for only that entry's evaluation.
  2331. */
  2332. if ( !(skip_attrEval) && (aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_ENTRY_LIST) &&
  2333. (res_right & SLAPI_ACL_SEARCH) &&
  2334. ((aci->aci_access & SLAPI_ACL_READ) || (aci->aci_access & SLAPI_ACL_SEARCH))) {
  2335. int kk=0;
  2336. while ( kk < aclpb_max_selected_acls && aclpb->aclpb_handles_index[kk] >=0 ) kk++;
  2337. if (kk >= aclpb_max_selected_acls) {
  2338. aclpb->aclpb_state &= ~ACLPB_SEARCH_BASED_ON_ENTRY_LIST;
  2339. } else {
  2340. aclpb->aclpb_handles_index[kk++] = aci->aci_index;
  2341. aclpb->aclpb_handles_index[kk] = -1;
  2342. }
  2343. }
  2344. /* If we are suppose to skip attr eval, then let's skip it */
  2345. if ( (aclpb->aclpb_access & SLAPI_ACL_SEARCH ) && ( ! skip_attrEval ) &&
  2346. ( aclpb->aclpb_res_type & ACLPB_NEW_ENTRY )) {
  2347. aclEvalContext *c_evalContext = &aclpb->aclpb_curr_entryEval_context;
  2348. int nhandle = c_evalContext->acle_numof_tmatched_handles;
  2349. if ( nhandle < aclpb_max_selected_acls) {
  2350. c_evalContext->acle_handles_matched_target[nhandle] = aci->aci_index;
  2351. c_evalContext->acle_numof_tmatched_handles++;
  2352. }
  2353. }
  2354. if ( skip_attrEval ) {
  2355. goto acl__resource_match_aci_EXIT;
  2356. }
  2357. /* We need to check again because we don't want to select this handle
  2358. ** if the right doesn't match for now.
  2359. */
  2360. if (!(aci_right & res_right)) {
  2361. matches = ACL_FALSE;
  2362. goto acl__resource_match_aci_EXIT;
  2363. }
  2364. /*
  2365. * Here if the request is one that requires matching
  2366. * on a targetattr then do it here.
  2367. * If we have already matched an attribute in the targetattrfitlers list
  2368. * then we do not require a match in the targetattr so we can skip it.
  2369. * The operations that require targetattr are SLAPI_ACL_COMPARE,
  2370. * SLAPI_ACL_SEARCH, SLAPI_ACL_READ and SLAPI_ACL_WRITE, as long as
  2371. * c_attrEval is non-null (otherwise it's a modrdn op which
  2372. * does not require the targetattr list).
  2373. *
  2374. * rbyrneXXX if we had a proper permission for modrdn eg SLAPI_ACL_MODRDN
  2375. * then we would not need this crappy way of telling it was a MODRDN
  2376. * request ie. SLAPI_ACL_WRITE && !(c_attrEval).
  2377. */
  2378. c_attrEval = aclpb->aclpb_curr_attrEval;
  2379. /*
  2380. * If we've already matched on targattrfilter then do not
  2381. * bother to look at the attrlist.
  2382. */
  2383. if (!attr_matched_in_targetattrfilters) {
  2384. /* match target attr */
  2385. if ((c_attrEval) &&
  2386. (aci->aci_type & ACI_TARGET_ATTR)) {
  2387. /* there is a target ATTR */
  2388. Targetattr **attrArray = aci->targetAttr;
  2389. Targetattr *attr = NULL;
  2390. res_attr = c_attrEval->attrEval_name;
  2391. attr_matched = ACL_FALSE;
  2392. star_matched = ACL_FALSE;
  2393. num_attrs = 0;
  2394. while (attrArray[num_attrs] && !attr_matched) {
  2395. attr = attrArray[num_attrs];
  2396. if (attr->attr_type & ACL_ATTR_STRING) {
  2397. /*
  2398. * res_attr: attr type to eval (e.g., filter "(sn;en=*)")
  2399. * attr->u.attr_str: targetattr value (e.g., sn;en)
  2400. */
  2401. if (slapi_attr_type_cmp(attr->u.attr_str, res_attr, SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  2402. attr_matched = ACL_TRUE;
  2403. *a_matched = ACL_TRUE;
  2404. }
  2405. } else if (attr->attr_type & ACL_ATTR_FILTER) {
  2406. if (ACL_TRUE == acl_match_substring (
  2407. attr->u.attr_filter,
  2408. res_attr, 1)) {
  2409. attr_matched = ACL_TRUE;
  2410. *a_matched = ACL_TRUE;
  2411. }
  2412. } else if (attr->attr_type & ACL_ATTR_STAR) {
  2413. attr_matched = ACL_TRUE;
  2414. *a_matched = ACL_TRUE;
  2415. star_matched = ACL_TRUE;
  2416. }
  2417. num_attrs++;
  2418. }
  2419. if (aci->aci_type & ACI_TARGET_ATTR_NOT) {
  2420. matches = (attr_matched ? ACL_FALSE : ACL_TRUE);
  2421. } else {
  2422. matches = (attr_matched ? ACL_TRUE: ACL_FALSE);
  2423. }
  2424. aclpb->aclpb_state &= ~ACLPB_ATTR_STAR_MATCHED;
  2425. /* figure out how it matched, i.e star matched */
  2426. if (matches && star_matched && num_attrs == 1 &&
  2427. !(aclpb->aclpb_state & ACLPB_FOUND_ATTR_RULE))
  2428. aclpb->aclpb_state |= ACLPB_ATTR_STAR_MATCHED;
  2429. else {
  2430. /* we are here means that there is a specific
  2431. ** attr in the rule for this resource.
  2432. ** We need to avoid this case
  2433. ** Rule 1: (targetattr = "uid")
  2434. ** Rule 2: (targetattr = "*")
  2435. ** we cannot use STAR optimization
  2436. */
  2437. aclpb->aclpb_state |= ACLPB_FOUND_ATTR_RULE;
  2438. aclpb->aclpb_state &= ~ACLPB_ATTR_STAR_MATCHED;
  2439. }
  2440. } else if ( (c_attrEval) ||
  2441. (aci->aci_type & ACI_TARGET_ATTR)) {
  2442. if ((aci_right & ACL_RIGHTS_TARGETATTR_NOT_NEEDED) &&
  2443. (aclpb->aclpb_access & ACL_RIGHTS_TARGETATTR_NOT_NEEDED)) {
  2444. /*
  2445. ** Targetattr rule doesn't make any sense
  2446. ** in this case. So select this rule
  2447. ** default: matches = ACL_TRUE;
  2448. */
  2449. ;
  2450. } else if (aci_right & SLAPI_ACL_WRITE &&
  2451. (aci->aci_type & ACI_TARGET_ATTR) &&
  2452. !(c_attrEval) &&
  2453. (aci->aci_type & ACI_HAS_ALLOW_RULE)) {
  2454. /* We need to handle modrdn operation. Modrdn doesn't
  2455. ** change any attrs but changes the RDN and so (attr=NULL).
  2456. ** Here we found an acl which has a targetattr but
  2457. ** the resource doesn't need one. In that case, we should
  2458. ** consider this acl.
  2459. ** the opposite is true if it is a deny rule, only a deny without
  2460. ** any targetattr should deny modrdn
  2461. ** default: matches = ACL_TRUE;
  2462. */
  2463. ;
  2464. } else {
  2465. matches = ACL_FALSE;
  2466. }
  2467. }
  2468. }/* !attr_matched_in_targetattrfilters */
  2469. /*
  2470. ** Here we are testing if we find a entry test rule (which should
  2471. ** be rare). In that case, just remember it. An entry test rule
  2472. ** doesn't have "(targetattr)".
  2473. */
  2474. if ((aclpb->aclpb_state & ACLPB_EVALUATING_FIRST_ATTR) &&
  2475. (!(aci->aci_type & ACI_TARGET_ATTR))) {
  2476. aclpb->aclpb_state |= ACLPB_FOUND_A_ENTRY_TEST_RULE;
  2477. }
  2478. /*
  2479. * Generic exit point for this routine:
  2480. * matches is ACL_TRUE if the aci matches the target of the resource,
  2481. * ACL_FALSE othrewise.
  2482. * Apologies for the goto--this is a retro-fitted exit point.
  2483. */
  2484. acl__resource_match_aci_EXIT:
  2485. /*
  2486. * For macro acis, there may be a partial macro string
  2487. * placed in the aclpb_macro_ht
  2488. * even if the aci did not finally match.
  2489. * All the partial strings will be freed at aclpb
  2490. * cleanup time.
  2491. */
  2492. if (ACL_TRUE == matches) {
  2493. aclpb->aclpb_stat_aclres_matched++;
  2494. }
  2495. TNF_PROBE_0_DEBUG(acl__resource_match_aci_end,"ACL","");
  2496. return (matches);
  2497. }
  2498. /* Macro to determine if the cached result is valid or not. */
  2499. #define ACL_CACHED_RESULT_VALID( result) \
  2500. (((result & ACLPB_CACHE_READ_RES_ALLOW) && \
  2501. (result & ACLPB_CACHE_READ_RES_SKIP)) || \
  2502. ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) && \
  2503. (result & ACLPB_CACHE_SEARCH_RES_SKIP))) ? 0 : 1
  2504. /***************************************************************************
  2505. *
  2506. * acl__TestRights
  2507. *
  2508. * Test the rights and find out if access is allowed or not.
  2509. *
  2510. * Processing Alogorithm:
  2511. *
  2512. * First, process the DENY rules one by one. If the user is not explicitly
  2513. * denied, then check if the user is allowed by processing the ALLOW handles
  2514. * one by one.
  2515. * The result of the evaluation is cached. Exceptions are
  2516. * -- If an acl happens to be in both DENY and ALLOW camp.
  2517. * -- Only interested for READ/SEARCH right.
  2518. *
  2519. * Input:
  2520. * struct acl_pblock *aclpb - main acl private block
  2521. * int access - The access bits
  2522. * char **right - The right we are looking for
  2523. * char ** generic - Generic rights
  2524. *
  2525. * Returns:
  2526. *
  2527. * ACL_RES_ALLOW - Access allowed
  2528. * ACL_RES_DENY - Access denied
  2529. * err - error condition
  2530. *
  2531. * Error Handling:
  2532. * None.
  2533. *
  2534. **************************************************************************/
  2535. static int
  2536. acl__TestRights(Acl_PBlock *aclpb,int access, const char **right, const char ** map_generic,
  2537. aclResultReason_t *result_reason)
  2538. {
  2539. ACLEvalHandle_t *acleval;
  2540. int rights_rv = ACL_RES_DENY;
  2541. int rv, i,j, k;
  2542. int index;
  2543. char *deny = NULL;
  2544. char *deny_generic = NULL;
  2545. char *acl_tag;
  2546. int expr_num;
  2547. const char *testRights[2];
  2548. aci_t *aci = NULL;
  2549. int numHandles = 0;
  2550. TNF_PROBE_0_DEBUG(acl__TestRights_start,"ACL","");
  2551. /* record the aci and reason for access decision */
  2552. result_reason->deciding_aci = NULL;
  2553. result_reason->reason = ACL_REASON_NONE;
  2554. /* If we don't have any ALLLOW handles, it's DENY by default */
  2555. if (aclpb->aclpb_num_allow_handles <= 0) {
  2556. result_reason->deciding_aci = NULL;
  2557. result_reason->reason = ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS;
  2558. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2559. tnf_string,no_allows,"");
  2560. return ACL_RES_DENY;
  2561. }
  2562. /* Get the ACL evaluation Context */
  2563. acleval = aclpb->aclpb_acleval;
  2564. testRights[0] = *right;
  2565. testRights[1] = '\0';
  2566. /*
  2567. ** START PROCESSING DENY HANDLES
  2568. ** Process each handle at a time. Do not concatenate the handles or else
  2569. ** all the context information will be build again and we will pay a
  2570. ** lot of penalty. The context is built the first time the handle is
  2571. ** processed.
  2572. **
  2573. ** First we set the default to INVALID so that if rules are not matched, then
  2574. ** we get INVALID and if a rule matched, the we get DENY.
  2575. */
  2576. aclpb->aclpb_state &= ~ACLPB_EXECUTING_ALLOW_HANDLES;
  2577. aclpb->aclpb_state |= ACLPB_EXECUTING_DENY_HANDLES;
  2578. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2579. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_deny_handles;
  2580. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_deny_handles ; ++i) {
  2581. int skip_eval = 0;
  2582. /*
  2583. ** If the handle has been evaluated before, we can
  2584. ** cache the result.
  2585. */
  2586. if ((aci = aclpb->aclpb_deny_handles[i]) == NULL) {
  2587. if (i <= ACI_MAX_ELEVEL) {
  2588. continue;
  2589. } else {
  2590. break;
  2591. }
  2592. }
  2593. k++;
  2594. index = aci->aci_index;
  2595. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2596. "Evaluating DENY aci(%d) \"%s\"\n", index, aci->aclName);
  2597. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2598. /* We can not reused results obtained on a other entry */
  2599. if (aci->aci_type & ACI_CACHE_RESULT_PER_ENTRY) {
  2600. aclpb->aclpb_state |= ACLPB_CACHE_RESULT_PER_ENTRY_SKIP;
  2601. }
  2602. /*
  2603. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2604. * a cache of info about whether applicable acis
  2605. * allowed, did_not_allow or denied access
  2606. */
  2607. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2608. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2609. short result;
  2610. result = aclpb->aclpb_cache_result[j].result;
  2611. if ( result <= 0) break;
  2612. if (!ACL_CACHED_RESULT_VALID(result)) {
  2613. /* something is wrong. Need to evaluate */
  2614. aclpb->aclpb_cache_result[j].result = -1;
  2615. break;
  2616. }
  2617. /*
  2618. ** We have a valid cached result. Let's see if we
  2619. ** have what we need.
  2620. */
  2621. if (access & SLAPI_ACL_SEARCH) {
  2622. if ( result & ACLPB_CACHE_SEARCH_RES_DENY){
  2623. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2624. "DENY:Found SEARCH DENY in cache\n");
  2625. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2626. result_reason->deciding_aci = aci;
  2627. result_reason->reason = ACL_REASON_RESULT_CACHED_DENY;
  2628. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2629. tnf_string,cached_deny,"");
  2630. return ACL_RES_DENY;
  2631. } else if ((result & ACLPB_CACHE_SEARCH_RES_SKIP) ||
  2632. (result & ACLPB_CACHE_SEARCH_RES_ALLOW)) {
  2633. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2634. "DENY:Found SEARCH SKIP in cache\n");
  2635. skip_eval = 1;
  2636. break;
  2637. } else {
  2638. break;
  2639. }
  2640. } else { /* must be READ */
  2641. if (result & ACLPB_CACHE_READ_RES_DENY) {
  2642. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2643. "DENY:Found READ DENY in cache\n");
  2644. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2645. result_reason->deciding_aci = aci;
  2646. result_reason->reason = ACL_REASON_RESULT_CACHED_DENY;
  2647. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2648. tnf_string,cached_deny,"");
  2649. return ACL_RES_DENY;
  2650. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2651. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2652. "DENY:Found READ SKIP in cache\n");
  2653. skip_eval = 1;
  2654. break;
  2655. } else {
  2656. break;
  2657. }
  2658. }
  2659. }
  2660. }
  2661. }
  2662. if (skip_eval) {
  2663. skip_eval = 0;
  2664. continue;
  2665. }
  2666. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2667. if ( rv < 0) {
  2668. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2669. "acl__TestRights:Unable to set the DENY acllist\n");
  2670. continue;
  2671. }
  2672. /*
  2673. ** Now we have all the information we need. We need to call
  2674. ** the ONE ACL to test the rights.
  2675. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2676. */
  2677. aclpb->aclpb_curr_aci = aci;
  2678. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2679. map_generic, &deny,
  2680. &deny_generic,
  2681. &acl_tag, &expr_num);
  2682. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Processed:%d DENY handles Result:%d\n",index, rights_rv);
  2683. if (rights_rv == ACL_RES_FAIL) {
  2684. result_reason->deciding_aci = aci;
  2685. result_reason->reason = ACL_REASON_NONE;
  2686. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2687. tnf_string,evaled_deny,"");
  2688. return ACL_RES_DENY;
  2689. }
  2690. /* have we executed an ATTR RULE */
  2691. if ( aci->aci_ruleType & ACI_ATTR_RULES )
  2692. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2693. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2694. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2695. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2696. break;
  2697. }
  2698. }
  2699. if ( j < aclpb->aclpb_last_cache_result) {
  2700. /* already in cache */
  2701. aclpb->aclpb_cache_result[j].result &= ~ACLPB_CACHE_ERROR_REPORTED;
  2702. } else if ( j < aclpb_max_cache_results ) {
  2703. /* j == aclpb->aclpb_last_cache_result &&
  2704. j < ACLPB_MAX_CACHE_RESULTS */
  2705. aclpb->aclpb_last_cache_result++;
  2706. aclpb->aclpb_cache_result[j].aci_index = index;
  2707. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2708. aclpb->aclpb_cache_result[j].result &= ~ACLPB_CACHE_ERROR_REPORTED;
  2709. } else { /* cache overflow */
  2710. if (!(aclpb->aclpb_cache_result[j].result &
  2711. ACLPB_CACHE_ERROR_REPORTED)) {
  2712. slapi_log_error (SLAPI_LOG_FATAL, "acl__TestRights",
  2713. "Your ACL cache of %d slots has overflowed. "
  2714. "This can happen when you have many ACIs. "
  2715. "This ACI evaluation requires %d slots to cache. "
  2716. "You can increase your max value by setting the attribute "
  2717. "%s in cn=ACL Plugin,cn=plugins,cn=config to a value higher. "
  2718. "A server restart is required.\n",
  2719. j, aclpb_max_cache_results, ATTR_ACLPB_MAX_SELECTED_ACLS);
  2720. aclpb->aclpb_cache_result[j].result |= ACLPB_CACHE_ERROR_REPORTED;
  2721. }
  2722. if (rights_rv == ACL_RES_DENY) {
  2723. result_reason->deciding_aci = aci;
  2724. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2725. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2726. tnf_string,evaled_deny,"");
  2727. return ACL_RES_DENY;
  2728. } else {
  2729. continue;
  2730. }
  2731. }
  2732. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2733. if (rights_rv == ACL_RES_DENY) {
  2734. if (access & SLAPI_ACL_SEARCH) {
  2735. aclpb->aclpb_cache_result[j].result |=
  2736. ACLPB_CACHE_SEARCH_RES_DENY;
  2737. } else { /* MUST BE READ */
  2738. aclpb->aclpb_cache_result[j].result |=
  2739. ACLPB_CACHE_READ_RES_DENY;
  2740. }
  2741. /* We are done -- return */
  2742. result_reason->deciding_aci = aci;
  2743. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2744. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2745. tnf_string,evaled_deny,"");
  2746. return ACL_RES_DENY;
  2747. } else if (rights_rv == ACL_RES_ALLOW) {
  2748. /* This will happen, of we have an acl with both deny and allow
  2749. ** Since we may not have finished all the deny acl, go thru all
  2750. ** of them. We will use this cached result when we evaluate this
  2751. ** handle in the context of allow handles.
  2752. */
  2753. if (access & SLAPI_ACL_SEARCH) {
  2754. aclpb->aclpb_cache_result[j].result |=
  2755. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2756. } else {
  2757. aclpb->aclpb_cache_result[j].result |=
  2758. ACLPB_CACHE_READ_RES_ALLOW;
  2759. }
  2760. } else {
  2761. if (access & SLAPI_ACL_SEARCH) {
  2762. aclpb->aclpb_cache_result[j].result |=
  2763. ACLPB_CACHE_SEARCH_RES_SKIP;
  2764. } else {
  2765. aclpb->aclpb_cache_result[j].result |=
  2766. ACLPB_CACHE_READ_RES_SKIP;
  2767. }
  2768. continue;
  2769. }
  2770. } else {
  2771. if ( rights_rv == ACL_RES_DENY ) {
  2772. result_reason->deciding_aci = aci;
  2773. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2774. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2775. tnf_string,evaled_deny,"");
  2776. return ACL_RES_DENY;
  2777. }
  2778. }
  2779. } /* for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_deny_handles; ++i) */
  2780. /*
  2781. ** START PROCESSING ALLOW HANDLES.
  2782. ** Process each handle at a time. Do not concatenate the handles or else
  2783. ** all the context information will be build again and we will pay a
  2784. ** lot of penalty. The context is built the first time the handle is
  2785. ** processed.
  2786. **
  2787. ** First we set the default to INVALID so that if rules are not matched, then
  2788. ** we get INVALID and if a rule matched, the we get ALLOW.
  2789. */
  2790. aclpb->aclpb_state &= ~ACLPB_EXECUTING_DENY_HANDLES;
  2791. aclpb->aclpb_state |= ACLPB_EXECUTING_ALLOW_HANDLES;
  2792. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2793. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_allow_handles;
  2794. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_allow_handles ; ++i) {
  2795. int skip_eval = 0;
  2796. /*
  2797. ** If the handle has been evaluated before, we can
  2798. ** cache the result.
  2799. */
  2800. if ((aci = aclpb->aclpb_allow_handles[i]) == NULL) {
  2801. if (i <= ACI_MAX_ELEVEL) {
  2802. continue;
  2803. } else {
  2804. break;
  2805. }
  2806. }
  2807. k++;
  2808. index = aci->aci_index;
  2809. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2810. "%d. Evaluating ALLOW aci(%d) \"%s\"\n", k, index, aci->aclName);
  2811. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2812. /* We can not reused results obtained on a other entry */
  2813. if (aci->aci_type & ACI_CACHE_RESULT_PER_ENTRY) {
  2814. aclpb->aclpb_state |= ACLPB_CACHE_RESULT_PER_ENTRY_SKIP;
  2815. }
  2816. /*
  2817. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2818. * a cache of info about whether applicable acis
  2819. * allowed, did_not_allow or denied access
  2820. */
  2821. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2822. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2823. short result;
  2824. result = aclpb->aclpb_cache_result[j].result;
  2825. if ( result <= 0) break;
  2826. if (!ACL_CACHED_RESULT_VALID(result)) {
  2827. /* something is wrong. Need to evaluate */
  2828. aclpb->aclpb_cache_result[j].result = -1;
  2829. break;
  2830. }
  2831. /*
  2832. ** We have a valid cached result. Let's see if we
  2833. ** have what we need.
  2834. */
  2835. if (access & SLAPI_ACL_SEARCH) {
  2836. if (result & ACLPB_CACHE_SEARCH_RES_ALLOW) {
  2837. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2838. "Found SEARCH ALLOW in cache\n");
  2839. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2840. result_reason->deciding_aci = aci;
  2841. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2842. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2843. tnf_string,cached_allow,"");
  2844. return ACL_RES_ALLOW;
  2845. } else if ( result & ACLPB_CACHE_SEARCH_RES_SKIP) {
  2846. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2847. "Found SEARCH SKIP in cache\n");
  2848. skip_eval = 1;
  2849. break;
  2850. } else {
  2851. /* need to evaluate */
  2852. break;
  2853. }
  2854. } else {
  2855. if ( result & ACLPB_CACHE_READ_RES_ALLOW) {
  2856. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2857. "Found READ ALLOW in cache\n");
  2858. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2859. result_reason->deciding_aci = aci;
  2860. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2861. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2862. tnf_string,cached_allow,"");
  2863. return ACL_RES_ALLOW;
  2864. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2865. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2866. "Found READ SKIP in cache\n");
  2867. skip_eval = 1;
  2868. break;
  2869. } else {
  2870. break;
  2871. }
  2872. }
  2873. }
  2874. }
  2875. }
  2876. if ( skip_eval) {
  2877. skip_eval = 0;
  2878. continue;
  2879. }
  2880. TNF_PROBE_0_DEBUG(acl__libaccess_start,"ACL","");
  2881. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2882. if ( rv < 0) {
  2883. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  2884. "acl__TestRights:Unable to set the acllist\n");
  2885. continue;
  2886. }
  2887. /*
  2888. ** Now we have all the information we need. We need to call
  2889. ** the ONE ACL to test the rights.
  2890. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2891. */
  2892. aclpb->aclpb_curr_aci = aci;
  2893. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2894. map_generic, &deny,
  2895. &deny_generic,
  2896. &acl_tag, &expr_num);
  2897. TNF_PROBE_0_DEBUG(acl__libaccess_end,"ACL","");
  2898. if (aci->aci_ruleType & ACI_ATTR_RULES)
  2899. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2900. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2901. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2902. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2903. break;
  2904. }
  2905. }
  2906. if (j < aclpb->aclpb_last_cache_result) {
  2907. /* already in cache */
  2908. aclpb->aclpb_cache_result[j].result &=
  2909. ~ACLPB_CACHE_ERROR_REPORTED;
  2910. } else if (j < aclpb_max_cache_results) {
  2911. /* j == aclpb->aclpb_last_cache_result &&
  2912. j < ACLPB_MAX_CACHE_RESULTS */
  2913. aclpb->aclpb_last_cache_result++;
  2914. aclpb->aclpb_cache_result[j].aci_index = index;
  2915. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2916. aclpb->aclpb_cache_result[j].result &=
  2917. ~ACLPB_CACHE_ERROR_REPORTED;
  2918. } else { /* cache overflow */
  2919. if (!(aclpb->aclpb_cache_result[j].result &
  2920. ACLPB_CACHE_ERROR_REPORTED)) {
  2921. slapi_log_error (SLAPI_LOG_FATAL, "acl__TestRights",
  2922. "Your ACL cache of %d slots has overflowed. "
  2923. "This can happen when you have many ACIs. "
  2924. "This ACI evaluation requires %d slots to cache. "
  2925. "You can increase your max value by setting the attribute "
  2926. "%s in cn=ACL Plugin,cn=plugins,cn=config to a value higher. "
  2927. "A server restart is required.\n",
  2928. j, aclpb_max_cache_results, ATTR_ACLPB_MAX_SELECTED_ACLS);
  2929. aclpb->aclpb_cache_result[j].result |=
  2930. ACLPB_CACHE_ERROR_REPORTED;
  2931. }
  2932. if (rights_rv == ACL_RES_ALLOW) {
  2933. result_reason->deciding_aci = aci;
  2934. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2935. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2936. tnf_string,evaled_allow,"");
  2937. return ACL_RES_ALLOW;
  2938. } else {
  2939. continue;
  2940. }
  2941. }
  2942. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2943. if (rights_rv == ACL_RES_ALLOW) {
  2944. if (access & SLAPI_ACL_SEARCH) {
  2945. aclpb->aclpb_cache_result[j].result |=
  2946. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2947. } else { /* must be READ */
  2948. aclpb->aclpb_cache_result[j].result |=
  2949. ACLPB_CACHE_READ_RES_ALLOW;
  2950. }
  2951. /* We are done -- return */
  2952. result_reason->deciding_aci = aci;
  2953. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2954. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2955. tnf_string,evaled_allow,"");
  2956. return ACL_RES_ALLOW;
  2957. } else {
  2958. if (access & SLAPI_ACL_SEARCH) {
  2959. aclpb->aclpb_cache_result[j].result |=
  2960. ACLPB_CACHE_SEARCH_RES_SKIP;
  2961. } else {
  2962. aclpb->aclpb_cache_result[j].result |=
  2963. ACLPB_CACHE_READ_RES_SKIP;
  2964. }
  2965. continue;
  2966. }
  2967. } else {
  2968. if ( rights_rv == ACL_RES_ALLOW ) {
  2969. result_reason->deciding_aci = aci;
  2970. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2971. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2972. tnf_string,evaled_allow,"");
  2973. return ACL_RES_ALLOW;
  2974. }
  2975. }
  2976. } /* for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_allow_handles ; ++i) { */
  2977. result_reason->deciding_aci = aci;
  2978. result_reason->reason = ACL_REASON_NO_MATCHED_SUBJECT_ALLOWS;
  2979. TNF_PROBE_0_DEBUG(acl__TestRights_end,"ACL","");
  2980. return (ACL_RES_DENY);
  2981. }
  2982. /***************************************************************************
  2983. *
  2984. * acl_match_substring
  2985. *
  2986. * Compare the input string to the patteren in the filter
  2987. *
  2988. * Input:
  2989. * struct slapi_filter *f - Filter which has the patteren
  2990. * char *str - String to compare
  2991. * int exact_match - 1; match the pattern exactly
  2992. * - 0; match the pattern as a suffix
  2993. *
  2994. * Returns:
  2995. * ACL_TRUE - The sting matches with the patteren
  2996. * ACL_FALSE - No it doesn't
  2997. * ACL_ERR - Error
  2998. *
  2999. * Error Handling:
  3000. * None.
  3001. *
  3002. **************************************************************************/
  3003. int
  3004. acl_match_substring ( Slapi_Filter *f, char *str, int exact_match)
  3005. {
  3006. int i, len, tlen;
  3007. char *p = str;
  3008. char *type, *initial, *final;
  3009. char **any;
  3010. if ( 0 != slapi_filter_get_subfilt ( f, &type, &initial, &any, &final ) ) {
  3011. return (ACL_FALSE);
  3012. }
  3013. /* this assumes that str and the filter components are already
  3014. * normalized. If not, it shoul be done
  3015. */
  3016. if ( initial != NULL) {
  3017. len = strlen(initial);
  3018. if (exact_match) {
  3019. int rc = strncmp(p, initial, len);
  3020. if (rc) {
  3021. return ACL_FALSE;
  3022. } else {
  3023. p += len;
  3024. }
  3025. } else {
  3026. p = strstr(p, initial);
  3027. if (p) {
  3028. p += len;
  3029. } else {
  3030. return ACL_FALSE;
  3031. }
  3032. }
  3033. }
  3034. if ( any != NULL) {
  3035. for (i = 0; any && any[i] != NULL; i++) {
  3036. p = strstr(p, any[i]);
  3037. if (p) {
  3038. p += strlen(any[i]);
  3039. } else {
  3040. return ACL_FALSE;
  3041. }
  3042. }
  3043. }
  3044. if ( final != NULL) {
  3045. len = strlen(final);
  3046. tlen = strlen(p);
  3047. if (len > tlen) return ACL_FALSE;
  3048. if (strcmp(p+tlen-len, final)) return ACL_FALSE;
  3049. }
  3050. return ACL_TRUE;
  3051. }
  3052. /***************************************************************************
  3053. *
  3054. * acl__reset_cached_result
  3055. *
  3056. * Go thru the cached result and invlalidate the cached evaluation results for
  3057. * rules which can only be cached based on the scope.
  3058. * If we have scope ACI_CACHE_RESULT_PER_ENTRY, then we need to invalidate the
  3059. * cacched reult whenever we hit a new entry.
  3060. *
  3061. * Returns:
  3062. * Null
  3063. *
  3064. **************************************************************************/
  3065. static void
  3066. acl__reset_cached_result (struct acl_pblock *aclpb )
  3067. {
  3068. int j;
  3069. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  3070. /* Unless we have to clear the result, move on */
  3071. if (!( aclpb->aclpb_cache_result[j].aci_ruleType & ACI_CACHE_RESULT_PER_ENTRY))
  3072. continue;
  3073. aclpb->aclpb_cache_result[j].result = 0;
  3074. }
  3075. }
  3076. /*
  3077. * acl_access_allowed_disjoint_resource
  3078. *
  3079. * This is an internal module which can be used to verify
  3080. * access to a resource which may not be inside the search scope.
  3081. *
  3082. * Returns:
  3083. * - Same return val as acl_access_allowed().
  3084. */
  3085. int
  3086. acl_access_allowed_disjoint_resource(
  3087. Slapi_PBlock *pb,
  3088. Slapi_Entry *e, /* The Slapi_Entry */
  3089. char *attr, /* Attribute of the entry */
  3090. struct berval *val, /* value of attr. NOT USED */
  3091. int access /* access rights */
  3092. )
  3093. {
  3094. int rv;
  3095. struct acl_pblock *aclpb = NULL;
  3096. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  3097. /*
  3098. ** It's possible that we already have a context of ACLs.
  3099. ** However once in a while, we need to test
  3100. ** access to a resource which (like vlv, schema) which falls
  3101. ** outside the search base. In that case, we need to go
  3102. ** thru all the ACLs and not depend upon the acls which we have
  3103. ** gathered.
  3104. */
  3105. /* If you have the right to use the resource, then we don't need to check for
  3106. ** proxy right on that resource.
  3107. */
  3108. if (aclpb)
  3109. aclpb->aclpb_state |= ( ACLPB_DONOT_USE_CONTEXT_ACLS| ACLPB_DONOT_EVALUATE_PROXY );
  3110. rv = acl_access_allowed(pb, e, attr, val, access);
  3111. if (aclpb) aclpb->aclpb_state &= ~ACLPB_DONOT_USE_CONTEXT_ACLS;
  3112. if (aclpb ) aclpb->aclpb_state &= ~ACLPB_DONOT_EVALUATE_PROXY;
  3113. return rv;
  3114. }
  3115. /*
  3116. * acl__attr_cached_result
  3117. * Loops thru the cached result and determines if we can use the cached value.
  3118. *
  3119. * Inputs:
  3120. * Slapi_pblock *aclpb - acl private block
  3121. * char *attr - attribute name
  3122. * int access - access type
  3123. * Returns:
  3124. * LDAP_SUCCESS: - access is granted
  3125. * LDAP_INSUFFICIENT_ACCESS - access denied
  3126. * ACL_ERR - no cached info about this attr.
  3127. * - or the attr had multiple result and so
  3128. * - we can't determine the outcome.
  3129. *
  3130. */
  3131. static int
  3132. acl__attr_cached_result (struct acl_pblock *aclpb, char *attr, int access )
  3133. {
  3134. int i, rc;
  3135. aclEvalContext *c_evalContext;
  3136. if ( !(access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ) ))
  3137. return ACL_ERR;
  3138. if (aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3139. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3140. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3141. "acl__attr_cached_result:Using Context: ACLPB_ACLCB\n" );
  3142. } else {
  3143. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3144. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3145. "acl__attr_cached_result:Using Context: ACLPB_PREV\n" );
  3146. }
  3147. if ( attr == NULL ) {
  3148. int eval_read = 0;
  3149. /*
  3150. ** Do I have access to at least one attribute, then I have
  3151. ** access to the entry.
  3152. */
  3153. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3154. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3155. if ( (access & SLAPI_ACL_READ ) && a_eval->attrEval_r_status &&
  3156. a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3157. eval_read++;
  3158. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3159. return LDAP_SUCCESS;
  3160. /* rbyrne: recompute if we have to.
  3161. * How does this cached result get turned off for
  3162. * attr style acis which acannot be cached becuase entry
  3163. * can result in a diff value.
  3164. */
  3165. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3166. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3167. a_eval->attrEval_r_aciIndex);
  3168. if ( rc != ACL_ERR ) {
  3169. acl_copyEval_context ( aclpb, c_evalContext,
  3170. &aclpb->aclpb_curr_entryEval_context, 1);
  3171. }
  3172. if ( rc == LDAP_SUCCESS) {
  3173. return LDAP_SUCCESS;
  3174. }
  3175. }
  3176. }
  3177. }/* for */
  3178. /*
  3179. * If we have scanned the whole list without success then
  3180. * we are not granting access to this entry through access
  3181. * to an attribute in the list--however this does not mean
  3182. * that we do not have access to the entry via another attribute
  3183. * not already in the list, so return -1 meaning
  3184. * "don't know".
  3185. */
  3186. return(ACL_ERR);
  3187. #if 0
  3188. if ( eval_read )
  3189. return LDAP_INSUFFICIENT_ACCESS;
  3190. else
  3191. return ACL_ERR;
  3192. #endif
  3193. }
  3194. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3195. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3196. if ( a_eval == NULL ) continue;
  3197. if (slapi_attr_type_cmp(a_eval->attrEval_name, attr, SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  3198. if ( access & SLAPI_ACL_SEARCH ) {
  3199. if (a_eval->attrEval_s_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3200. if ( a_eval->attrEval_s_status & ACL_ATTREVAL_SUCCESS)
  3201. return LDAP_SUCCESS;
  3202. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_FAIL)
  3203. return LDAP_INSUFFICIENT_ACCESS;
  3204. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_RECOMPUTE ) {
  3205. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3206. a_eval->attrEval_s_aciIndex);
  3207. if ( rc != ACL_ERR ) {
  3208. acl_copyEval_context ( aclpb, c_evalContext,
  3209. &aclpb->aclpb_curr_entryEval_context, 1);
  3210. }
  3211. } else
  3212. return ACL_ERR;
  3213. } else {
  3214. /* This means that for the same attribute and same type of
  3215. ** access, we had different results at different time.
  3216. ** Since we are not caching per object, we can't
  3217. ** determine exactly. So, can't touch this
  3218. */
  3219. return ACL_ERR;
  3220. }
  3221. } else {
  3222. if (a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3223. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3224. return LDAP_SUCCESS;
  3225. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_FAIL)
  3226. return LDAP_INSUFFICIENT_ACCESS;
  3227. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3228. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3229. a_eval->attrEval_r_aciIndex);
  3230. if ( rc != ACL_ERR ) {
  3231. acl_copyEval_context ( aclpb, c_evalContext,
  3232. &aclpb->aclpb_curr_entryEval_context, 1);
  3233. }
  3234. } else
  3235. return ACL_ERR;
  3236. } else {
  3237. /* Look above for explanation */
  3238. return ACL_ERR;
  3239. }
  3240. }
  3241. }
  3242. }
  3243. return ACL_ERR;
  3244. }
  3245. /*
  3246. * Had to do this juggling of casting to make
  3247. * both Nt & unix compiler happy.
  3248. */
  3249. static int
  3250. acl__cmp(const void *a, const void *b)
  3251. {
  3252. short *i = (short *) a;
  3253. short *j = (short *) b;
  3254. if ( (short) *i > (short) *j )
  3255. return (1);
  3256. if ( (short)*i < (short) *j)
  3257. return (-1);
  3258. return (0);
  3259. }
  3260. /*
  3261. * acl__scan_match_handles
  3262. * Go thru the ACL list and determine if the list of acls selected matches
  3263. * what we have in the cache.
  3264. *
  3265. * Inputs:
  3266. * Acl_PBlock *pb - Main pblock ( blacvk hole)
  3267. * int type - Which context to look on
  3268. *
  3269. * Returns:
  3270. * 0 - matches all the acl handles
  3271. * ACL_ERR - sorry; no match
  3272. *
  3273. * ASSUMPTION: A READER LOCK ON ACL LIST
  3274. */
  3275. static int
  3276. acl__scan_match_handles ( Acl_PBlock *aclpb, int type)
  3277. {
  3278. int matched = 0;
  3279. aci_t *aci = NULL;
  3280. int index;
  3281. PRUint32 cookie;
  3282. aclEvalContext *c_evalContext = NULL;
  3283. if (type == ACLPB_EVALCONTEXT_PREV ) {
  3284. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3285. } else if ( type == ACLPB_EVALCONTEXT_ACLCB ){
  3286. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3287. } else {
  3288. return ACL_ERR;
  3289. }
  3290. if ( !c_evalContext->acle_numof_tmatched_handles )
  3291. return ACL_ERR;
  3292. aclpb->aclpb_stat_acllist_scanned++;
  3293. aci = acllist_get_first_aci ( aclpb, &cookie );
  3294. while ( aci ) {
  3295. index = aci->aci_index;
  3296. if (acl__resource_match_aci(aclpb, aci, 1 /* skip attr matching */, NULL )) {
  3297. int j;
  3298. int s_matched = matched;
  3299. /* We have a sorted list of handles that matched the target */
  3300. for (j=0; j < c_evalContext->acle_numof_tmatched_handles ; j++ ) {
  3301. if ( c_evalContext->acle_handles_matched_target[j] > index )
  3302. break;
  3303. else if ( index == c_evalContext->acle_handles_matched_target[j] ) {
  3304. int jj;
  3305. matched++;
  3306. /* See if this is a ATTR rule that matched -- in that case we have
  3307. ** to nullify the cached result
  3308. */
  3309. if ( aci->aci_ruleType & ACI_ATTR_RULES ) {
  3310. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3311. "Found an attr Rule [Name:%s Index:%d\n", aci->aclName,
  3312. aci->aci_index );
  3313. for ( jj =0; jj < c_evalContext->acle_numof_attrs; jj++ ) {
  3314. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[jj];
  3315. if ( a_eval->attrEval_r_aciIndex == aci->aci_index )
  3316. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3317. if ( a_eval->attrEval_s_aciIndex == aci->aci_index )
  3318. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3319. }
  3320. }
  3321. break;
  3322. }
  3323. }
  3324. if ( s_matched == matched ) return ACL_ERR;
  3325. }
  3326. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3327. }
  3328. if ( matched == c_evalContext->acle_numof_tmatched_handles )
  3329. return 0;
  3330. return ACL_ERR;
  3331. }
  3332. /*
  3333. * acl_copyEval_context
  3334. * Copy the context info which include attr info and handles.
  3335. *
  3336. * Inputs
  3337. * struct acl_pblock *aclpb - acl private main block
  3338. * aclEvalContext *src - src context
  3339. * aclEvalContext *dest - dest context
  3340. * Returns:
  3341. * None.
  3342. *
  3343. */
  3344. void
  3345. acl_copyEval_context ( struct acl_pblock *aclpb, aclEvalContext *src,
  3346. aclEvalContext *dest , int copy_attr_only )
  3347. {
  3348. int d_slot, i;
  3349. /* Do a CLEAN copy we have nothing or else do an incremental copy.*/
  3350. if ( src->acle_numof_attrs < 1 )
  3351. return;
  3352. /* Copy the attr info */
  3353. if ( dest->acle_numof_attrs < 1 )
  3354. acl_clean_aclEval_context ( dest, 0 /*clean */ );
  3355. d_slot = dest->acle_numof_attrs;
  3356. for (i=0; i < src->acle_numof_attrs; i++ ) {
  3357. int j;
  3358. int attr_exists = 0;
  3359. int dd_slot = d_slot;
  3360. if ( aclpb && (i == 0) ) aclpb->aclpb_stat_num_copycontext++;
  3361. if ( src->acle_attrEval[i].attrEval_r_status == 0 &&
  3362. src->acle_attrEval[i].attrEval_s_status == 0 )
  3363. continue;
  3364. for ( j = 0; j < dest->acle_numof_attrs; j++ ) {
  3365. if (slapi_attr_type_cmp(src->acle_attrEval[i].attrEval_name,
  3366. dest->acle_attrEval[j].attrEval_name,
  3367. SLAPI_TYPE_CMP_SUBTYPE) == 0) {
  3368. /* We have it. skip it. */
  3369. attr_exists = 1;
  3370. dd_slot = j;
  3371. break;
  3372. }
  3373. }
  3374. if ( !attr_exists ) {
  3375. if ( dd_slot >= ACLPB_MAX_ATTRS -1 )
  3376. break;
  3377. if ( aclpb) aclpb->aclpb_stat_num_copy_attrs++;
  3378. slapi_ch_free_string( &dest->acle_attrEval[dd_slot].attrEval_name);
  3379. dest->acle_attrEval[dd_slot].attrEval_name =
  3380. slapi_ch_strdup ( src->acle_attrEval[i].attrEval_name );
  3381. }
  3382. /* Copy the result status and the aci index */
  3383. dest->acle_attrEval[dd_slot].attrEval_r_status =
  3384. src->acle_attrEval[i].attrEval_r_status;
  3385. dest->acle_attrEval[dd_slot].attrEval_r_aciIndex =
  3386. src->acle_attrEval[i].attrEval_r_aciIndex;
  3387. dest->acle_attrEval[dd_slot].attrEval_s_status =
  3388. src->acle_attrEval[i].attrEval_s_status;
  3389. dest->acle_attrEval[dd_slot].attrEval_s_aciIndex =
  3390. src->acle_attrEval[i].attrEval_s_aciIndex;
  3391. if (!attr_exists ) d_slot++;
  3392. }
  3393. dest->acle_numof_attrs = d_slot;
  3394. slapi_ch_free_string( &dest->acle_attrEval[d_slot].attrEval_name);
  3395. if ( copy_attr_only )
  3396. return;
  3397. /* First sort the arrays which keeps the acl index numbers */
  3398. qsort ( (char *) src->acle_handles_matched_target,
  3399. (size_t)src->acle_numof_tmatched_handles, sizeof( int ), acl__cmp );
  3400. for (i=0; i < src->acle_numof_tmatched_handles; i++ ) {
  3401. dest->acle_handles_matched_target[i] = src->acle_handles_matched_target[i];
  3402. }
  3403. if ( src->acle_numof_tmatched_handles ) {
  3404. dest->acle_numof_tmatched_handles = src->acle_numof_tmatched_handles;
  3405. if ( aclpb) aclpb->aclpb_stat_num_tmatched_acls = src->acle_numof_tmatched_handles;
  3406. }
  3407. }
  3408. /*
  3409. * acl_clean_aclEval_context
  3410. * Clean the eval context
  3411. *
  3412. * Inputs:
  3413. * aclEvalContext *clean_me - COntext to be cleaned
  3414. * int clean_type - 0: clean, 1 scrub
  3415. *
  3416. */
  3417. void
  3418. acl_clean_aclEval_context ( aclEvalContext *clean_me, int scrub_only )
  3419. {
  3420. int i;
  3421. /* Copy the attr info */
  3422. for (i=0; i < clean_me->acle_numof_attrs; i++ ) {
  3423. char *a_name = clean_me->acle_attrEval[i].attrEval_name;
  3424. if ( a_name && !scrub_only) {
  3425. slapi_ch_free ( (void **) &a_name );
  3426. clean_me->acle_attrEval[i].attrEval_name = NULL;
  3427. }
  3428. clean_me->acle_attrEval[i].attrEval_r_status = 0;
  3429. clean_me->acle_attrEval[i].attrEval_s_status = 0;
  3430. clean_me->acle_attrEval[i].attrEval_r_aciIndex = 0;
  3431. clean_me->acle_attrEval[i].attrEval_s_aciIndex = 0;
  3432. }
  3433. if ( !scrub_only ) clean_me->acle_numof_attrs = 0;
  3434. clean_me->acle_numof_tmatched_handles = 0;
  3435. }
  3436. /*
  3437. * acl__match_handlesFromCache
  3438. *
  3439. * We have 2 cacheed information
  3440. * 1) cached info from the previous operation
  3441. * 2) cached info from the prev entry evaluation
  3442. *
  3443. * What we are doing here is going thru all the acls and see if the same
  3444. * set of acls apply to this resource or not. If it does, then do we have
  3445. * a cached info for the attr. If we don't for both of the cases then we need
  3446. * to evaluate all over again.
  3447. *
  3448. * Inputs:
  3449. * struct acl_pblock - ACL private block;
  3450. * char *attr - Attribute name
  3451. * int access - acces type
  3452. *
  3453. * returns:
  3454. * LDAP_SUCCESS (0) - The same acls apply and we have
  3455. * access ALLOWED on the attr
  3456. * LDAP_INSUFFICIENT_ACCESS - The same acls apply and we have
  3457. * access DENIED on the attr
  3458. * -1 - Acls doesn't match or we don't have
  3459. * cached info for this attr.
  3460. *
  3461. * ASSUMPTIONS: A reader lock has been obtained for the acl list.
  3462. */
  3463. static int
  3464. acl__match_handlesFromCache ( Acl_PBlock *aclpb, char *attr, int access)
  3465. {
  3466. aclEvalContext *c_evalContext = NULL;
  3467. int context_type = 0;
  3468. int ret_val = -1; /* it doen't match by default */
  3469. /* Before we proceed, find out if we have evaluated any ATTR RULE. If we have
  3470. ** then we can't use any caching mechanism
  3471. */
  3472. if ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3473. context_type = ACLPB_EVALCONTEXT_ACLCB;
  3474. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3475. } else {
  3476. context_type = ACLPB_EVALCONTEXT_PREV;
  3477. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3478. }
  3479. /* we can not reused access evaluation done on a previous entry
  3480. * so just skip that cache
  3481. */
  3482. if (aclpb->aclpb_state & ACLPB_CACHE_RESULT_PER_ENTRY_SKIP) {
  3483. aclpb->aclpb_state &= ~ACLPB_MATCHES_ALL_ACLS;
  3484. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  3485. /* Did not match */
  3486. if (context_type == ACLPB_EVALCONTEXT_ACLCB) {
  3487. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT;
  3488. } else {
  3489. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  3490. c_evalContext->acle_numof_tmatched_handles = 0;
  3491. }
  3492. return -1;
  3493. }
  3494. if ( aclpb->aclpb_res_type & (ACLPB_NEW_ENTRY | ACLPB_EFFECTIVE_RIGHTS) ) {
  3495. aclpb->aclpb_state |= ACLPB_MATCHES_ALL_ACLS;
  3496. ret_val = acl__scan_match_handles ( aclpb, context_type );
  3497. if ( -1 == ret_val ) {
  3498. aclpb->aclpb_state &= ~ACLPB_MATCHES_ALL_ACLS;
  3499. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  3500. /* Did not match */
  3501. if ( context_type == ACLPB_EVALCONTEXT_ACLCB ) {
  3502. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT;
  3503. } else {
  3504. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  3505. c_evalContext->acle_numof_tmatched_handles = 0;
  3506. }
  3507. }
  3508. }
  3509. if ( aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  3510. /* See if we have a cached result for this attr */
  3511. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3512. /* It's not in the ACLCB context but we might have it in the
  3513. ** current/prev context. Take a look at it. we might have evaluated
  3514. ** this attribute already.
  3515. */
  3516. if ( (-1 == ret_val ) &&
  3517. ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT )) {
  3518. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3519. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3520. aclpb->aclpb_state |= ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3521. /* We need to do an incremental update */
  3522. if ( !ret_val ) aclpb->aclpb_state |= ACLPB_INCR_ACLCB_CACHE;
  3523. }
  3524. }
  3525. return ret_val;
  3526. }
  3527. /*
  3528. * acl__get_attrEval
  3529. * Get the atteval from the current context and hold the ptr in aclpb.
  3530. * If we have too many attrs, then allocate a new one. In that case
  3531. * we let the caller know about that so that it will be deallocated.
  3532. *
  3533. * Returns:
  3534. * int - 0: The context was indexed. So, no allocations.
  3535. * - 1; context was allocated - deallocate it.
  3536. */
  3537. static int
  3538. acl__get_attrEval ( struct acl_pblock *aclpb, char *attr )
  3539. {
  3540. int j;
  3541. aclEvalContext *c_ContextEval = &aclpb->aclpb_curr_entryEval_context;
  3542. int deallocate_attrEval = 0;
  3543. AclAttrEval *c_attrEval = NULL;
  3544. if ( !attr ) return deallocate_attrEval;
  3545. aclpb->aclpb_curr_attrEval = NULL;
  3546. /* Go thru and see if we have the attr already */
  3547. for (j=0; j < c_ContextEval->acle_numof_attrs; j++) {
  3548. c_attrEval = &c_ContextEval->acle_attrEval[j];
  3549. if (c_attrEval &&
  3550. /* attr: e.g., filter "(sn;en=*)" / attr list / attr in entry */
  3551. /* This compare must check all subtypes. "sn" vs. "sn;fr" should return 1 */
  3552. slapi_attr_type_cmp(c_attrEval->attrEval_name,
  3553. attr, SLAPI_TYPE_CMP_SUBTYPES) == 0) {
  3554. aclpb->aclpb_curr_attrEval = c_attrEval;
  3555. break;
  3556. }
  3557. }
  3558. if ( !aclpb->aclpb_curr_attrEval) {
  3559. if ( c_ContextEval->acle_numof_attrs == ACLPB_MAX_ATTRS -1 ) {
  3560. /* Too many attrs. create a temp one */
  3561. c_attrEval = (AclAttrEval * ) slapi_ch_calloc ( 1, sizeof ( AclAttrEval ) );
  3562. deallocate_attrEval =1;
  3563. } else {
  3564. c_attrEval = &c_ContextEval->acle_attrEval[c_ContextEval->acle_numof_attrs++];
  3565. c_attrEval->attrEval_r_status = 0;
  3566. c_attrEval->attrEval_s_status = 0;
  3567. c_attrEval->attrEval_r_aciIndex = 0;
  3568. c_attrEval->attrEval_s_aciIndex = 0;
  3569. }
  3570. /* clean it before use */
  3571. slapi_ch_free_string(&c_attrEval->attrEval_name);
  3572. c_attrEval->attrEval_name = slapi_ch_strdup ( attr );
  3573. aclpb->aclpb_curr_attrEval = c_attrEval;
  3574. }
  3575. return deallocate_attrEval;
  3576. }
  3577. /*
  3578. * acl_skip_access_check
  3579. *
  3580. * See if we need to go thru the ACL check or not. We don't need to if I am root
  3581. * or internal operation or ...
  3582. *
  3583. * returns:
  3584. * ACL_TRUE - Yes; skip the ACL check
  3585. * ACL_FALSE - No; you have to go thru ACL check
  3586. *
  3587. */
  3588. int
  3589. acl_skip_access_check ( Slapi_PBlock *pb, Slapi_Entry *e )
  3590. {
  3591. int rv, isRoot, accessCheckDisabled;
  3592. void *conn = NULL;
  3593. Slapi_Backend *be;
  3594. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  3595. if ( isRoot ) return ACL_TRUE;
  3596. /* See if this is local request */
  3597. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn);
  3598. if ( NULL == conn ) return ACL_TRUE;
  3599. /*
  3600. * Turn on access checking in the rootdse--this code used
  3601. * to skip the access check.
  3602. *
  3603. * check if the entry is the RootDSE entry
  3604. if ( e ) {
  3605. char * edn = slapi_entry_get_ndn ( e );
  3606. if ( slapi_is_rootdse ( edn ) ) return ACL_TRUE;
  3607. }
  3608. */
  3609. /* GB : when referrals are directly set in the mappin tree
  3610. * we can reach this code without a backend in the pblock
  3611. * in such a case, allow access for now
  3612. * we may want to reconsider this is NULL DSE implementation happens
  3613. */
  3614. rv = slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  3615. if (be == NULL)
  3616. return ACL_TRUE;
  3617. rv = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  3618. if ( rv != -1 && accessCheckDisabled ) return ACL_TRUE;
  3619. return ACL_FALSE;
  3620. }
  3621. short
  3622. acl_get_aclsignature ()
  3623. {
  3624. return acl_signature;
  3625. }
  3626. void
  3627. acl_set_aclsignature ( short value)
  3628. {
  3629. acl_signature = value;
  3630. }
  3631. void
  3632. acl_regen_aclsignature ()
  3633. {
  3634. acl_signature = aclutil_gen_signature ( acl_signature );
  3635. }
  3636. /*
  3637. *
  3638. * Assumptions:
  3639. * 1) Called for read/search right.
  3640. */
  3641. static int
  3642. acl__recompute_acl ( Acl_PBlock *aclpb,
  3643. AclAttrEval *a_eval,
  3644. int access,
  3645. int aciIndex
  3646. )
  3647. {
  3648. char *unused_str1, *unused_str2;
  3649. char *acl_tag;
  3650. const char *testRight[2];
  3651. int j, expr_num;
  3652. int result_status = ACL_RES_INVALID, cache_result;
  3653. PRUint32 cookie;
  3654. aci_t *aci;
  3655. PR_ASSERT ( aciIndex >= 0 );
  3656. PR_ASSERT ( a_eval != NULL );
  3657. PR_ASSERT (aclpb != NULL );
  3658. /* We might have evaluated this acl just now, check it there first */
  3659. for ( j =0; j < aclpb->aclpb_last_cache_result; j++) {
  3660. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3661. short result;
  3662. result_status =ACL_RES_INVALID;
  3663. result = aclpb->aclpb_cache_result[j].result;
  3664. if ( result <= 0) break;
  3665. if (!ACL_CACHED_RESULT_VALID(result)) {
  3666. /* something is wrong. Need to evaluate */
  3667. aclpb->aclpb_cache_result[j].result = -1;
  3668. break;
  3669. }
  3670. /*
  3671. ** We have a valid cached result. Let's see if we
  3672. ** have what we need.
  3673. */
  3674. if ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) ||
  3675. (result & ACLPB_CACHE_READ_RES_ALLOW) )
  3676. result_status = ACL_RES_ALLOW;
  3677. else if ((result & ACLPB_CACHE_SEARCH_RES_DENY) ||
  3678. (result & ACLPB_CACHE_READ_RES_DENY) )
  3679. result_status = ACL_RES_DENY;
  3680. }
  3681. } /* end of for */
  3682. if ( result_status != ACL_RES_INVALID ) {
  3683. goto set_result_status;
  3684. }
  3685. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3686. "Recomputing the ACL Index:%d for entry:%s\n",
  3687. aciIndex, slapi_entry_get_ndn ( aclpb->aclpb_curr_entry) );
  3688. /* First find this one ACL and then evaluate it. */
  3689. aci = acllist_get_first_aci ( aclpb, &cookie );
  3690. while ( aci && aci->aci_index != aciIndex ) {
  3691. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3692. }
  3693. if (NULL == aci)
  3694. return -1;
  3695. ACL_SetDefaultResult (NULL, aclpb->aclpb_acleval, ACL_RES_INVALID);
  3696. ACL_EvalSetACL(NULL, aclpb->aclpb_acleval, aci->aci_handle);
  3697. testRight[0] = acl_access2str ( access );
  3698. testRight[1] = '\0';
  3699. aclpb->aclpb_curr_aci = aci;
  3700. result_status = ACL_EvalTestRights (NULL, aclpb->aclpb_acleval, testRight,
  3701. ds_map_generic, &unused_str1,
  3702. &unused_str2,
  3703. &acl_tag, &expr_num);
  3704. cache_result = 0;
  3705. if ( result_status == ACL_RES_DENY && aci->aci_type & ACI_HAS_DENY_RULE ) {
  3706. if ( access & SLAPI_ACL_SEARCH)
  3707. cache_result = ACLPB_CACHE_SEARCH_RES_DENY;
  3708. else
  3709. cache_result = ACLPB_CACHE_READ_RES_DENY;
  3710. } else if ( result_status == ACL_RES_ALLOW && aci->aci_type & ACI_HAS_ALLOW_RULE ) {
  3711. if ( access & SLAPI_ACL_SEARCH)
  3712. cache_result = ACLPB_CACHE_SEARCH_RES_ALLOW;
  3713. else
  3714. cache_result = ACLPB_CACHE_READ_RES_ALLOW;
  3715. } else {
  3716. result_status = -1;
  3717. }
  3718. /* Now we need to put the cached result in the aclpb */
  3719. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  3720. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3721. break;
  3722. }
  3723. }
  3724. if ( j < aclpb->aclpb_last_cache_result) {
  3725. /* already in cache */
  3726. } else if ( j < aclpb_max_cache_results-1) {
  3727. /* rbyrneXXX: make this same as other last_cache_result code! */
  3728. j = ++aclpb->aclpb_last_cache_result;
  3729. aclpb->aclpb_cache_result[j].aci_index = aci->aci_index;
  3730. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  3731. } else { /* No more space */
  3732. goto set_result_status;
  3733. }
  3734. /* Add the cached result status */
  3735. aclpb->aclpb_cache_result[j].result |= cache_result;
  3736. set_result_status:
  3737. if (result_status == ACL_RES_ALLOW) {
  3738. if (access & SLAPI_ACL_SEARCH)
  3739. /*wrong bit maskes were being used here--
  3740. a_eval->attrEval_s_status = ACLPB_CACHE_SEARCH_RES_ALLOW;*/
  3741. a_eval->attrEval_s_status = ACL_ATTREVAL_SUCCESS;
  3742. else
  3743. a_eval->attrEval_r_status = ACL_ATTREVAL_SUCCESS;
  3744. } else if ( result_status == ACL_RES_DENY) {
  3745. if (access & SLAPI_ACL_SEARCH)
  3746. a_eval->attrEval_s_status = ACL_ATTREVAL_FAIL;
  3747. else
  3748. a_eval->attrEval_r_status = ACL_ATTREVAL_FAIL;
  3749. } else {
  3750. /* Here, set it to recompute--try again later */
  3751. if (access & SLAPI_ACL_SEARCH)
  3752. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3753. else
  3754. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3755. result_status = -1;
  3756. }
  3757. return result_status;
  3758. }
  3759. static void
  3760. __acl_set_aclIndex_inResult ( Acl_PBlock *aclpb, int access, int index )
  3761. {
  3762. AclAttrEval *c_attrEval = aclpb->aclpb_curr_attrEval;
  3763. if ( c_attrEval ) {
  3764. if ( access & SLAPI_ACL_SEARCH )
  3765. c_attrEval->attrEval_s_aciIndex = index;
  3766. else if ( access & SLAPI_ACL_READ )
  3767. c_attrEval->attrEval_r_aciIndex = index;
  3768. }
  3769. }
  3770. /*
  3771. * If filter_sense is true then return (entry satisfies f).
  3772. * Otherwise, return !(entry satisfies f)
  3773. */
  3774. static int
  3775. acl__test_filter ( Slapi_Entry *entry, struct slapi_filter *f, int filter_sense) {
  3776. int filter_matched;
  3777. /* slapi_vattr_filter_test() returns 0 for match */
  3778. filter_matched = !slapi_vattr_filter_test(NULL, entry,
  3779. f,
  3780. 0 /*don't do acess chk*/);
  3781. if (filter_sense) {
  3782. return(filter_matched ? ACL_TRUE : ACL_FALSE);
  3783. } else {
  3784. return(filter_matched ? ACL_FALSE: ACL_TRUE);
  3785. }
  3786. }
  3787. /*
  3788. * Make an entry consisting of attr_type and attr_val and put
  3789. * a pointer to it in *entry.
  3790. * We will use this afterwards to test for against a filter.
  3791. */
  3792. static int
  3793. acl__make_filter_test_entry ( Slapi_Entry **entry, char *attr_type,
  3794. struct berval *attr_val) {
  3795. struct berval *vals_array[2];
  3796. vals_array[0] = attr_val;
  3797. vals_array[1] = NULL;
  3798. *entry = slapi_entry_alloc();
  3799. slapi_entry_init(*entry, NULL, NULL);
  3800. return (slapi_entry_add_values( *entry, (const char *)attr_type,
  3801. (struct berval**)&vals_array[0] ));
  3802. }
  3803. /*********************************************************************************/
  3804. /* E N D */
  3805. /*********************************************************************************/