acl.c 122 KB

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