acl.c 122 KB

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