acl.c 138 KB

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