acl.c 123 KB

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