1
0

acl.c 126 KB

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