acl.c 138 KB

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