acl.c 134 KB

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