acl.c 123 KB

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