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