1
0

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