acl.c 138 KB

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