acl.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "acl.h"
  42. /****************************************************************************
  43. *
  44. * acl.c
  45. *
  46. *
  47. * This file contains the functions related to Access Control List (ACL)
  48. * checking. The ACL checking is based on the ONE ACL design implemented
  49. * in the Web server 2.0. For more information on the ACL design look
  50. * into the barracuda home page.
  51. *
  52. *
  53. ******************************************************************************/
  54. /****************************************************************************/
  55. /* Globals. Must be protected by Mutex. */
  56. /****************************************************************************/
  57. /* Signatures to see if things have changed */
  58. static short acl_signature = 0;
  59. /****************************************************************************/
  60. /* Defines, Constants, ande Declarations */
  61. /****************************************************************************/
  62. static char *ds_map_generic[2] = { NULL, NULL };
  63. /****************************************************************************/
  64. /* prototypes */
  65. /****************************************************************************/
  66. static int acl__resource_match_aci(struct acl_pblock *aclpb, aci_t *aci ,
  67. int skip_attrEval, int *a_matched);
  68. static int acl__TestRights(Acl_PBlock *aclpb,int access, char **right,
  69. char ** map_generic, aclResultReason_t *result_reason);
  70. static int acl__scan_for_acis(struct acl_pblock *aclpb, int *err);
  71. static void acl__reset_cached_result (struct acl_pblock *aclpb );
  72. static int acl__scan_match_handles ( struct acl_pblock *aclpb, int type);
  73. static int acl__attr_cached_result (struct acl_pblock *aclpb, char *attr, int access );
  74. static int acl__match_handlesFromCache (struct acl_pblock *aclpb, char *attr, int access);
  75. static int acl__get_attrEval ( struct acl_pblock *aclpb, char *attr );
  76. static int acl__recompute_acl (Acl_PBlock *aclpb, AclAttrEval *a_eval,
  77. int access, int aciIndex);
  78. static void __acl_set_aclIndex_inResult ( Acl_PBlock *aclpb,
  79. int access, int index );
  80. static int acl__make_filter_test_entry ( Slapi_Entry **entry,
  81. char *attr_type, struct berval *attr_val);
  82. static int acl__test_filter ( Slapi_Entry *entry, struct slapi_filter *f,
  83. int filter_sense);
  84. static void print_access_control_summary( char * source,
  85. int ret_val, char *clientDn,
  86. struct acl_pblock *aclpb,
  87. char *right,
  88. char *attr,
  89. const char *edn,
  90. aclResultReason_t *acl_reason);
  91. static int check_rdn_access( Slapi_PBlock *pb, Slapi_Entry *e,
  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. 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. 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. 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. 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. 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. 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. 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. 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. switch (mod->mod_op & ~LDAP_MOD_BVALUES ) {
  1191. case LDAP_MOD_DELETE:
  1192. if (mod->mod_bvalues != NULL ) {
  1193. break;
  1194. }
  1195. /*
  1196. * Here, check that we have the right to delete all
  1197. * the values of the attribute in the entry.
  1198. */
  1199. case LDAP_MOD_REPLACE:
  1200. if ( !lastmod ) {
  1201. if (be == NULL) {
  1202. if (slapi_pblock_get( pb, SLAPI_BACKEND, &be )) {
  1203. be = NULL;
  1204. }
  1205. }
  1206. if (be != NULL)
  1207. slapi_pblock_get ( pb, SLAPI_BE_LASTMOD, &lastmod );
  1208. }
  1209. if (lastmod &&
  1210. (strcmp (mod->mod_type, "modifiersname")== 0 ||
  1211. strcmp (mod->mod_type, "modifytimestamp")== 0 ||
  1212. strcmp (mod->mod_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD)== 0)
  1213. ) {
  1214. continue;
  1215. }
  1216. slapi_entry_attr_find (e, mod->mod_type, &attr);
  1217. if ( attr != NULL) {
  1218. Slapi_Value *sval=NULL;
  1219. const struct berval *attrVal=NULL;
  1220. int k= slapi_attr_first_value(attr,&sval);
  1221. while(k != -1) {
  1222. attrVal = slapi_value_get_berval(sval);
  1223. rv = slapi_access_allowed (pb, e,
  1224. mod->mod_type,
  1225. (struct berval *)attrVal, /* XXXggood had to cast away const - BAD */
  1226. ACLPB_SLAPI_ACL_WRITE_DEL); /* was SLAPI_ACL_WRITE */
  1227. if ( rv != LDAP_SUCCESS) {
  1228. acl_gen_err_msg (
  1229. SLAPI_ACL_WRITE,
  1230. n_edn,
  1231. mod->mod_type,
  1232. errbuf);
  1233. /* Cleanup */
  1234. slapi_mods_done(&smods);
  1235. return(rv);
  1236. }
  1237. k= slapi_attr_next_value(attr, k, &sval);
  1238. }
  1239. }
  1240. else {
  1241. rv = slapi_access_allowed (pb, e,
  1242. mod->mod_type,
  1243. NULL,
  1244. ACLPB_SLAPI_ACL_WRITE_DEL); /* was SLAPI_ACL_WRITE */
  1245. if ( rv != LDAP_SUCCESS) {
  1246. acl_gen_err_msg (
  1247. SLAPI_ACL_WRITE,
  1248. n_edn,
  1249. mod->mod_type,
  1250. errbuf);
  1251. /* Cleanup */
  1252. slapi_mods_done(&smods);
  1253. return(rv);
  1254. }
  1255. }
  1256. break;
  1257. default:
  1258. break;
  1259. } /* switch */
  1260. /*
  1261. * Check that we have add/delete writes on the specific values
  1262. * we are trying to add.
  1263. */
  1264. if ( aclpb && aclpb->aclpb_curr_entry_sdn )
  1265. slapi_sdn_done ( aclpb->aclpb_curr_entry_sdn );
  1266. if ( mod->mod_bvalues != NULL ) {
  1267. /*
  1268. * Here, there are specific values specified.
  1269. * For add and replace--we need add rights for these values.
  1270. * For delete we need delete rights for these values.
  1271. */
  1272. for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
  1273. if (SLAPI_IS_MOD_ADD(mod->mod_op) ||
  1274. SLAPI_IS_MOD_REPLACE(mod->mod_op)) {
  1275. rv = acl_access_allowed (pb,e,
  1276. mod->mod_type,
  1277. mod->mod_bvalues[i],
  1278. ACLPB_SLAPI_ACL_WRITE_ADD); /*was SLAPI_ACL_WRITE*/
  1279. } else if (SLAPI_IS_MOD_DELETE(mod->mod_op)) {
  1280. rv = acl_access_allowed (pb,e,
  1281. mod->mod_type,
  1282. mod->mod_bvalues[i],
  1283. ACLPB_SLAPI_ACL_WRITE_DEL); /*was SLAPI_ACL_WRITE*/
  1284. } else {
  1285. rv = LDAP_INSUFFICIENT_ACCESS;
  1286. }
  1287. if ( rv != LDAP_SUCCESS ) {
  1288. acl_gen_err_msg (
  1289. SLAPI_ACL_WRITE,
  1290. n_edn,
  1291. mod->mod_type,
  1292. errbuf);
  1293. /* Cleanup */
  1294. slapi_mods_done(&smods);
  1295. return rv;
  1296. }
  1297. /* Need to check for all the values because
  1298. ** we may be modifying a "self<right>" value.
  1299. */
  1300. /* Are we adding/replacing a aci attribute
  1301. ** value. In that case, we need to make
  1302. ** sure that the new value has thr right
  1303. ** syntax
  1304. */
  1305. if (strcmp(mod->mod_type,
  1306. aci_attr_type) == 0) {
  1307. if ( 0 != (rv = acl_verify_syntax( e_sdn,
  1308. mod->mod_bvalues[i], errbuf))) {
  1309. aclutil_print_err(rv, e_sdn,
  1310. mod->mod_bvalues[i],
  1311. errbuf);
  1312. /* Cleanup */
  1313. slapi_mods_done(&smods);
  1314. return LDAP_INVALID_SYNTAX;
  1315. }
  1316. }
  1317. } /* for */
  1318. }
  1319. } /* end of big for */
  1320. /* Cleanup */
  1321. slapi_mods_done(&smods);
  1322. return( LDAP_SUCCESS );
  1323. }
  1324. /***************************************************************************
  1325. *
  1326. * acl_modified
  1327. * Modifies ( removed, add, changes) the ACI LIST.
  1328. *
  1329. * Input:
  1330. * int *optype - op code
  1331. * char *dn - DN of the entry
  1332. * void *change - The change struct which contais the
  1333. * - change value
  1334. *
  1335. * Returns:
  1336. * None.
  1337. *
  1338. * Error Handling:
  1339. * None.
  1340. *
  1341. **************************************************************************/
  1342. extern void
  1343. acl_modified (Slapi_PBlock *pb, int optype, Slapi_DN *e_sdn, void *change)
  1344. {
  1345. struct berval **bvalue;
  1346. char **value;
  1347. int rv=0; /* returned value */
  1348. const char* n_dn;
  1349. char* new_RDN;
  1350. char* parent_DN;
  1351. char* new_DN;
  1352. LDAPMod **mods;
  1353. struct berval b;
  1354. int j;
  1355. Slapi_Attr *attr = NULL;
  1356. Slapi_Entry *e = NULL;
  1357. aclUserGroup *ugroup = NULL;
  1358. if (NULL == e_sdn) {
  1359. return;
  1360. }
  1361. n_dn = slapi_sdn_get_dn(e_sdn);
  1362. /* Before we proceed, Let's first check if we are changing any groups.
  1363. ** If we are, then we need to change the signature
  1364. */
  1365. switch ( optype ) {
  1366. case SLAPI_OPERATION_MODIFY:
  1367. case SLAPI_OPERATION_DELETE:
  1368. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, (void*)&e);
  1369. break;
  1370. case SLAPI_OPERATION_ADD:
  1371. e = (Slapi_Entry *)change;
  1372. break;
  1373. }
  1374. /* e can be null for RDN */
  1375. if ( e ) slapi_entry_attr_find( e, "objectclass", &attr);
  1376. if ( attr ) {
  1377. int group_change = 0;
  1378. Slapi_Value *sval=NULL;
  1379. const struct berval *attrVal;
  1380. int i;
  1381. i= slapi_attr_first_value ( attr,&sval );
  1382. while(i != -1) {
  1383. attrVal = slapi_value_get_berval ( sval );
  1384. if ( (strcasecmp (attrVal->bv_val, "groupOfNames") == 0 ) ||
  1385. (strcasecmp (attrVal->bv_val, "groupOfUniqueNames") == 0 ) ||
  1386. (strcasecmp (attrVal->bv_val, "groupOfCertificates") == 0 ) ||
  1387. (strcasecmp (attrVal->bv_val, "groupOfURLs") == 0 ) ) {
  1388. group_change= 1;
  1389. if ( optype == SLAPI_OPERATION_MODIFY ) {
  1390. Slapi_Attr *a = NULL;
  1391. int rv;
  1392. rv = slapi_entry_attr_find ( e, "uniqueMember", &a);
  1393. if ( rv != 0 ) break;
  1394. rv = slapi_entry_attr_find ( e, "Member", &a );
  1395. if ( rv != 0 ) break;
  1396. rv = slapi_entry_attr_find ( e, "MemberURL", &a );
  1397. if ( rv != 0 ) break;
  1398. /* That means we are not changing the member
  1399. ** list, so it's okay to let go this
  1400. ** change
  1401. */
  1402. group_change = 0;
  1403. }
  1404. break;
  1405. }
  1406. i= slapi_attr_next_value ( attr, i, &sval );
  1407. }
  1408. /*
  1409. ** We can do better here XXX, i.e invalidate the cache for users who
  1410. ** use this group. for now just do the whole thing.
  1411. */
  1412. if ( group_change ) {
  1413. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1414. "Group Change: Invalidating entire UserGroup Cache %s\n",
  1415. n_dn);
  1416. aclg_regen_group_signature();
  1417. if ( (optype == SLAPI_OPERATION_MODIFY) || (optype == SLAPI_OPERATION_DELETE ) ) {
  1418. /* Then we need to invalidate the acl signature also */
  1419. acl_signature = aclutil_gen_signature ( acl_signature );
  1420. }
  1421. }
  1422. }
  1423. /*
  1424. * Here if the target entry is in the group cache
  1425. * as a user then, as it's being changed it may move out of any dynamic
  1426. * groups it belongs to.
  1427. * Just remove it for now--can do better XXX by checking to see if
  1428. * it really needs to be removed by testing to see if he's
  1429. * still in th group after the change--but this requires keeping
  1430. * the memberURL of the group which we don't currently do.
  1431. * Also, if we keep
  1432. * the attributes that are being used in dynamic
  1433. * groups then we could only remove the user if a sensitive
  1434. * attribute was being modified (rather than scanning the whole user cache
  1435. * all the time). Also could do a hash lookup.
  1436. *
  1437. * aclg_find_userGroup() incs a refcnt so we can still refer to ugroup.
  1438. * aclg_markUgroupForRemoval() decs it and marks it for removal
  1439. * , so after that you cannot refer to ugroup.
  1440. *
  1441. */
  1442. if ( (ugroup = aclg_find_userGroup(n_dn)) != NULL) {
  1443. /*
  1444. * Mark this for deletion next time round--try to impact
  1445. * this mainline code as little as possible.
  1446. */
  1447. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1448. "Marking entry %s for removal from ACL user Group Cache\n",
  1449. n_dn);
  1450. aclg_markUgroupForRemoval (ugroup);
  1451. }
  1452. /*
  1453. * Take the write lock around all the mods--so that
  1454. * other operations will see the acicache either before the whole mod
  1455. * or after but not, as it was before, during the mod.
  1456. * This is in line with the LDAP concept of the operation
  1457. * on the whole entry being the atomic unit.
  1458. *
  1459. */
  1460. switch(optype) {
  1461. case SLAPI_OPERATION_DELETE:
  1462. /* In this case we have already checked if the user has
  1463. ** right to delete the entry. Part of delete of entry is
  1464. ** remove all the ACLs also.
  1465. */
  1466. acllist_acicache_WRITE_LOCK();
  1467. rv = acllist_remove_aci_needsLock(e_sdn, NULL);
  1468. acllist_acicache_WRITE_UNLOCK();
  1469. break;
  1470. case SLAPI_OPERATION_ADD:
  1471. slapi_entry_attr_find ( (Slapi_Entry *) change, aci_attr_type, &attr );
  1472. if ( attr ) {
  1473. Slapi_Value *sval=NULL;
  1474. const struct berval *attrVal;
  1475. int i;
  1476. acllist_acicache_WRITE_LOCK();
  1477. i= slapi_attr_first_value ( attr,&sval );
  1478. while ( i != -1 ) {
  1479. attrVal = slapi_value_get_berval(sval);
  1480. rv= acllist_insert_aci_needsLock(e_sdn, attrVal );
  1481. if (rv <= ACL_ERR)
  1482. aclutil_print_err(rv, e_sdn, attrVal, NULL);
  1483. /* Print the aci list */
  1484. i= slapi_attr_next_value ( attr, i, &sval );
  1485. }
  1486. acllist_acicache_WRITE_UNLOCK();
  1487. }
  1488. break;
  1489. case SLAPI_OPERATION_MODIFY:
  1490. {
  1491. int got_write_lock = 0;
  1492. mods = (LDAPMod **) change;
  1493. for (j=0; mods[j] != NULL; j++) {
  1494. if (strcasecmp(mods[j]->mod_type, aci_attr_type) == 0) {
  1495. /* Got an aci to mod in this list of mods, so
  1496. * take the acicache lock for the whole list of mods,
  1497. * remembering to free it below.
  1498. */
  1499. if ( !got_write_lock) {
  1500. acllist_acicache_WRITE_LOCK();
  1501. got_write_lock = 1;
  1502. }
  1503. switch (mods[j]->mod_op & ~LDAP_MOD_BVALUES) {
  1504. case LDAP_MOD_REPLACE:
  1505. /* First remove the item */
  1506. rv = acllist_remove_aci_needsLock(e_sdn, NULL);
  1507. /* now fall thru to add the new one */
  1508. case LDAP_MOD_ADD:
  1509. /* Add the new aci */
  1510. if (mods[j]->mod_op & LDAP_MOD_BVALUES) {
  1511. bvalue = mods[j]->mod_bvalues;
  1512. if (bvalue == NULL)
  1513. break;
  1514. for (; *bvalue != NULL; ++bvalue) {
  1515. rv=acllist_insert_aci_needsLock( e_sdn, *bvalue);
  1516. if (rv <= ACL_ERR) {
  1517. aclutil_print_err(rv, e_sdn,
  1518. *bvalue, NULL);
  1519. }
  1520. }
  1521. } else {
  1522. value = mods[j]->mod_values;
  1523. if (value == NULL)
  1524. break;
  1525. for (; *value != NULL; ++value) {
  1526. b.bv_len = strlen (*value);
  1527. b.bv_val = *value;
  1528. rv=acllist_insert_aci_needsLock( e_sdn, &b);
  1529. if (rv <= ACL_ERR) {
  1530. aclutil_print_err(rv, e_sdn,
  1531. &b, NULL);
  1532. }
  1533. }
  1534. }
  1535. break;
  1536. case LDAP_MOD_DELETE:
  1537. if (mods[j]->mod_op & LDAP_MOD_BVALUES) {
  1538. bvalue = mods[j]->mod_bvalues;
  1539. if (bvalue == NULL || *bvalue == NULL) {
  1540. rv = acllist_remove_aci_needsLock( e_sdn, NULL);
  1541. } else {
  1542. for (; *bvalue != NULL; ++bvalue)
  1543. acllist_remove_aci_needsLock( e_sdn, *bvalue);
  1544. }
  1545. } else {
  1546. value = mods[j]->mod_values;
  1547. if (value == NULL || *value == NULL) {
  1548. acllist_remove_aci_needsLock( e_sdn,NULL);
  1549. } else {
  1550. for (; *value != NULL; ++value) {
  1551. b.bv_len = strlen (*value);
  1552. b.bv_val = *value;
  1553. acllist_remove_aci_needsLock( e_sdn, &b);
  1554. }
  1555. }
  1556. }
  1557. break;
  1558. default:
  1559. break;
  1560. }/* modtype switch */
  1561. }/* attrtype is aci */
  1562. } /* end of for */
  1563. if ( got_write_lock ) {
  1564. acllist_acicache_WRITE_UNLOCK();
  1565. got_write_lock = 0;
  1566. }
  1567. break;
  1568. }/* case op is modify*/
  1569. case SLAPI_OPERATION_MODRDN:
  1570. {
  1571. char **rdn_parent;
  1572. rdn_parent = (char **)change;
  1573. new_RDN = rdn_parent[0];
  1574. parent_DN = rdn_parent[1];
  1575. /* compute new_DN: */
  1576. if (NULL == parent_DN) {
  1577. parent_DN = slapi_dn_parent(n_dn);
  1578. }
  1579. if (NULL == parent_DN) {
  1580. if (NULL == new_RDN) {
  1581. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1582. "acl_modified (MODRDN %s => \"no change\"\n",
  1583. n_dn);
  1584. break;
  1585. } else {
  1586. new_DN = new_RDN;
  1587. }
  1588. } else {
  1589. if (NULL == new_RDN) {
  1590. Slapi_RDN *rdn= slapi_rdn_new();
  1591. slapi_sdn_get_rdn(e_sdn, rdn);
  1592. new_DN = slapi_create_dn_string("%s,%s", slapi_rdn_get_rdn(rdn),
  1593. parent_DN);
  1594. slapi_rdn_free(&rdn);
  1595. } else {
  1596. new_DN = slapi_create_dn_string("%s,%s", new_RDN, parent_DN);
  1597. }
  1598. }
  1599. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1600. "acl_modified (MODRDN %s => \"%s\"\n", n_dn, new_RDN);
  1601. /* Change the acls */
  1602. acllist_acicache_WRITE_LOCK();
  1603. /* acllist_moddn_aci_needsLock expects normalized new_DN,
  1604. * which is no need to be case-ignored */
  1605. acllist_moddn_aci_needsLock ( e_sdn, new_DN );
  1606. acllist_acicache_WRITE_UNLOCK();
  1607. /* deallocat the parent_DN */
  1608. if (parent_DN != NULL) {
  1609. slapi_ch_free_string(&new_DN);
  1610. if (parent_DN != rdn_parent[1]) {
  1611. slapi_ch_free_string(&parent_DN);
  1612. }
  1613. }
  1614. break;
  1615. } /* case op is modrdn */
  1616. default:
  1617. /* print ERROR */
  1618. break;
  1619. } /*optype switch */
  1620. }
  1621. /***************************************************************************
  1622. *
  1623. * acl__scan_for_acis
  1624. * Scan the list and picup the correct acls for evaluation.
  1625. *
  1626. * Input:
  1627. * Acl_PBlock *aclpb - Main ACL pblock
  1628. * int *err; - Any error status
  1629. * Returns:
  1630. * num_handles - Number of handles matched to the
  1631. * - resource + 1.
  1632. * Error Handling:
  1633. * None.
  1634. *
  1635. **************************************************************************/
  1636. static int
  1637. acl__scan_for_acis(Acl_PBlock *aclpb, int *err)
  1638. {
  1639. aci_t *aci;
  1640. NSErr_t errp;
  1641. int attr_matched;
  1642. int deny_handle;
  1643. int allow_handle;
  1644. int gen_allow_handle = ACI_MAX_ELEVEL+1;
  1645. int gen_deny_handle = ACI_MAX_ELEVEL+1;
  1646. PRUint32 cookie;
  1647. TNF_PROBE_0_DEBUG(acl__scan_for_acis_start,"ACL","");
  1648. /*
  1649. ** Determine if we are traversing via the list Vs. we have our own
  1650. ** generated list
  1651. */
  1652. if ( aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST ||
  1653. aclpb->aclpb_handles_index[0] != -1 ) {
  1654. int kk = 0;
  1655. while ( kk < aclpb_max_selected_acls && aclpb->aclpb_handles_index[kk] != -1 ) {
  1656. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Using ACL Container:%d for evaluation\n", kk);
  1657. kk++;
  1658. }
  1659. }
  1660. memset (&errp, 0, sizeof(NSErr_t));
  1661. *err = ACL_FALSE;
  1662. aclpb->aclpb_num_deny_handles = -1;
  1663. aclpb->aclpb_num_allow_handles = -1;
  1664. memset(aclpb->aclpb_deny_handles, 0, sizeof(aci_t *)*(ACI_MAX_ELEVEL+1));
  1665. memset(aclpb->aclpb_allow_handles, 0, sizeof(aci_t *)*(ACI_MAX_ELEVEL+1));
  1666. /* Check the signature. If it has changed, start fresh */
  1667. if ( aclpb->aclpb_signature != acl_signature ) {
  1668. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1669. "Restart the scan -- due to acl changes\n");
  1670. acllist_init_scan ( aclpb->aclpb_pblock, LDAP_SCOPE_BASE, NULL );
  1671. }
  1672. attr_matched = ACL_FALSE;
  1673. deny_handle = 0;
  1674. allow_handle = 0;
  1675. aclpb->aclpb_stat_acllist_scanned++;
  1676. aci = acllist_get_first_aci ( aclpb, &cookie );
  1677. while( aci ) {
  1678. if (acl__resource_match_aci(aclpb, aci, 0, &attr_matched)) {
  1679. /* Generate the ACL list handle */
  1680. if (aci->aci_handle == NULL) {
  1681. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  1682. continue;
  1683. }
  1684. aclutil_print_aci (aci, acl_access2str (aclpb->aclpb_access));
  1685. if (aci->aci_type & ACI_HAS_DENY_RULE) {
  1686. if (aclpb->aclpb_deny_handles[aci->aci_elevel] == NULL ) {
  1687. aclpb->aclpb_deny_handles[aci->aci_elevel] = aci;
  1688. } else {
  1689. if ((gen_deny_handle + ACI_DEFAULT_ELEVEL + 1) ==
  1690. aclpb->aclpb_deny_handles_size ) {
  1691. int num = ACLPB_INCR_LIST_HANDLES +
  1692. aclpb->aclpb_deny_handles_size;
  1693. /* allocate more space */
  1694. aclpb->aclpb_deny_handles =
  1695. (aci_t **)
  1696. slapi_ch_realloc (
  1697. (void *) aclpb->aclpb_deny_handles,
  1698. num * sizeof (aci_t *));
  1699. aclpb->aclpb_deny_handles_size = num;
  1700. }
  1701. aclpb->aclpb_deny_handles [gen_deny_handle] = aci;
  1702. gen_deny_handle++;
  1703. }
  1704. deny_handle++;
  1705. }
  1706. /*
  1707. ** It's possible that a single acl is in both the camps i.e
  1708. ** It has a allow and a deny rule
  1709. ** In that case we keep the same acl in both the camps.
  1710. */
  1711. if (aci->aci_type & ACI_HAS_ALLOW_RULE) {
  1712. if (aclpb->aclpb_allow_handles[aci->aci_elevel] == NULL ) {
  1713. aclpb->aclpb_allow_handles[aci->aci_elevel] = aci;
  1714. } else {
  1715. if ((gen_allow_handle + ACI_DEFAULT_ELEVEL + 1) ==
  1716. aclpb->aclpb_allow_handles_size) {
  1717. /* allocate more space */
  1718. int num = ACLPB_INCR_LIST_HANDLES +
  1719. aclpb->aclpb_allow_handles_size;
  1720. aclpb->aclpb_allow_handles =
  1721. (aci_t **)
  1722. slapi_ch_realloc (
  1723. (void *) aclpb->aclpb_allow_handles,
  1724. num * sizeof (aci_t *));
  1725. aclpb->aclpb_allow_handles_size = num;
  1726. }
  1727. aclpb->aclpb_allow_handles [gen_allow_handle] = aci;
  1728. gen_allow_handle++;
  1729. }
  1730. allow_handle++;
  1731. }
  1732. }
  1733. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  1734. } /* end of while */
  1735. /* make the last one a null */
  1736. aclpb->aclpb_deny_handles [gen_deny_handle] = NULL;
  1737. aclpb->aclpb_allow_handles [gen_allow_handle] = NULL;
  1738. /* specify how many we found */
  1739. aclpb->aclpb_num_deny_handles = deny_handle;
  1740. aclpb->aclpb_num_allow_handles = allow_handle;
  1741. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Num of ALLOW Handles:%d, DENY handles:%d\n",
  1742. aclpb->aclpb_num_allow_handles, aclpb->aclpb_num_deny_handles);
  1743. TNF_PROBE_0_DEBUG(acl__scan_for_acis_end,"ACL","");
  1744. return(allow_handle + deny_handle);
  1745. }
  1746. /***************************************************************************
  1747. *
  1748. * acl__resource_match_aci
  1749. *
  1750. * This compares the ACI for the given resource and determines if
  1751. * the ACL applies to the resource or not.
  1752. *
  1753. * For read/search operation, we collect all the possible acls which
  1754. * will apply, We will be using this list for future acl evaluation
  1755. * for that entry.
  1756. *
  1757. * Input:
  1758. * struct acl_pblock *aclpb - Main acl private block
  1759. * aci_t *aci - The ACI item
  1760. * int skip_attrEval - DOn't check for attrs
  1761. * int *a_matched - Attribute matched
  1762. *
  1763. * Returns:
  1764. *
  1765. * ACL_TRUE - Yep, This ACL is applicable to the
  1766. * - the resource.
  1767. * ACL_FALSE - No it is not.
  1768. *
  1769. * Error Handling:
  1770. * None.
  1771. *
  1772. **************************************************************************/
  1773. #define ACL_RIGHTS_TARGETATTR_NOT_NEEDED ( SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_PROXY)
  1774. static int
  1775. acl__resource_match_aci( Acl_PBlock *aclpb, aci_t *aci, int skip_attrEval, int *a_matched)
  1776. {
  1777. struct slapi_filter *f; /* filter */
  1778. int rv; /* return value */
  1779. /* Assume that resource matches */
  1780. int matches = ACL_TRUE;
  1781. int attr_matched = ACL_TRUE;
  1782. int attr_matched_in_targetattrfilters = 0;
  1783. int dn_matched;
  1784. char *res_attr;
  1785. int aci_right = 0;
  1786. int res_right = 0;
  1787. int star_matched = ACL_FALSE;
  1788. int num_attrs = 0;
  1789. AclAttrEval *c_attrEval = NULL;
  1790. const char *res_ndn = NULL;
  1791. const char *aci_ndn = NULL;
  1792. char *matched_val = NULL;
  1793. int add_matched_val_to_ht = 0;
  1794. char res_right_str[128];
  1795. TNF_PROBE_0_DEBUG(acl__resource_match_aci_start,"ACL","");
  1796. if (NULL == aclpb) {
  1797. matches = ACL_FALSE;
  1798. goto acl__resource_match_aci_EXIT;
  1799. }
  1800. /* Figure out if the acl has the correct rights or not */
  1801. aci_right = aci->aci_access;
  1802. res_right = aclpb->aclpb_access;
  1803. if (!(aci_right & res_right)) {
  1804. /* If we are looking for read/search and the acl has read/search
  1805. ** then go further because if targets match we may keep that
  1806. ** acl in the entry cache list.
  1807. */
  1808. if (!((res_right & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) &&
  1809. (aci_right & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ))))
  1810. matches = ACL_FALSE;
  1811. goto acl__resource_match_aci_EXIT;
  1812. }
  1813. /* first Let's see if the entry is under the subtree where the
  1814. ** ACL resides. We can't let somebody affect a target beyond the
  1815. ** scope of where the ACL resides
  1816. ** Example: ACL is located in "ou=engineering, o=ace industry, c=us
  1817. ** but if the target is "o=ace industry, c=us", then we are in trouble.
  1818. **
  1819. ** If the aci is in the rootdse and the entry is not, then we do not
  1820. ** match--ie. acis in the rootdse do NOT apply below...for the moment.
  1821. **
  1822. */
  1823. res_ndn = slapi_sdn_get_ndn ( aclpb->aclpb_curr_entry_sdn );
  1824. aci_ndn = slapi_sdn_get_ndn ( aci->aci_sdn );
  1825. if (!slapi_sdn_issuffix(aclpb->aclpb_curr_entry_sdn, aci->aci_sdn)
  1826. || (!slapi_is_rootdse(res_ndn) && slapi_is_rootdse(aci_ndn)) ) {
  1827. /* cant' poke around */
  1828. matches = ACL_FALSE;
  1829. goto acl__resource_match_aci_EXIT;
  1830. }
  1831. /*
  1832. ** We have a single ACI which we need to find if it applies to
  1833. ** the resource or not.
  1834. */
  1835. if ((aci->aci_type & ACI_TARGET_DN) && (aclpb->aclpb_curr_entry_sdn)) {
  1836. char *avaType;
  1837. struct berval *avaValue;
  1838. f = aci->target;
  1839. dn_matched = ACL_TRUE;
  1840. slapi_filter_get_ava ( f, &avaType, &avaValue );
  1841. if (!slapi_dn_issuffix( res_ndn, avaValue->bv_val)) {
  1842. dn_matched = ACL_FALSE;
  1843. }
  1844. if (aci->aci_type & ACI_TARGET_NOT) {
  1845. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1846. } else {
  1847. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  1848. }
  1849. }
  1850. /* No need to look further */
  1851. if (matches == ACL_FALSE) {
  1852. goto acl__resource_match_aci_EXIT;
  1853. }
  1854. if (aci->aci_type & ACI_TARGET_PATTERN) {
  1855. f = aci->target;
  1856. dn_matched = ACL_TRUE;
  1857. if ((rv = acl_match_substring(f, (char *)res_ndn, 0 /* match suffux */)) != ACL_TRUE) {
  1858. dn_matched = ACL_FALSE;
  1859. if(rv == ACL_ERR) {
  1860. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1861. "acl__resource_match_aci:pattern err\n");
  1862. matches = ACL_FALSE;
  1863. goto acl__resource_match_aci_EXIT;
  1864. }
  1865. }
  1866. if (aci->aci_type & ACI_TARGET_NOT) {
  1867. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1868. } else {
  1869. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  1870. }
  1871. }
  1872. /* No need to look further */
  1873. if (matches == ACL_FALSE) {
  1874. goto acl__resource_match_aci_EXIT;
  1875. }
  1876. /*
  1877. * Is it a (target="ldap://cn=*,($dn),o=sun.com") kind of thing.
  1878. */
  1879. if (aci->aci_type & ACI_TARGET_MACRO_DN) {
  1880. /*
  1881. * See if the ($dn) component matches the string and
  1882. * retrieve the matched substring for later use
  1883. * in the userdn.
  1884. * The macro string is a function of the dn only, so if the
  1885. * entry is the same one don't recalculate it--
  1886. * this flag only works for search right now, could
  1887. * also optimise for mods by making it work for mods.
  1888. */
  1889. if ( (aclpb->aclpb_res_type & ACLPB_NEW_ENTRY) == 0 ) {
  1890. /*
  1891. * Here same entry so just look up the matched value,
  1892. * calculated from the targetdn and stored judiciously there
  1893. */
  1894. matched_val = (char *)acl_ht_lookup( aclpb->aclpb_macro_ht,
  1895. (PLHashNumber)aci->aci_index);
  1896. }
  1897. if ( matched_val == NULL &&
  1898. (aclpb->aclpb_res_type & (ACLPB_NEW_ENTRY | ACLPB_EFFECTIVE_RIGHTS))) {
  1899. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1900. "Evaluating macro aci(%d)%s for resource %s\n",
  1901. aci->aci_index, aci->aclName,
  1902. aclutil__access_str(res_right, res_right_str));
  1903. matched_val = acl_match_macro_in_target( res_ndn,
  1904. aci->aci_macro->match_this,
  1905. aci->aci_macro->macro_ptr);
  1906. add_matched_val_to_ht = 1; /* may need to add matched value to ht */
  1907. }
  1908. if (matched_val == NULL) {
  1909. dn_matched = ACL_FALSE;
  1910. } else {
  1911. dn_matched = ACL_TRUE;
  1912. }
  1913. if (aci->aci_type & ACI_TARGET_NOT) {
  1914. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1915. } else {
  1916. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  1917. }
  1918. if ( add_matched_val_to_ht ) {
  1919. if ( matches == ACL_TRUE && matched_val ) {
  1920. /*
  1921. * matched_val may be needed later for matching on
  1922. * other targets or on the subject--so optimistically
  1923. * put it in the hash table.
  1924. * If, at the end of this routine, we
  1925. * find that after all the resource did not match then
  1926. * that's ok--the value is freed at aclpb cleanup time.
  1927. * If there is already an entry for this aci in this
  1928. * aclpb then remove it--it's an old value for a
  1929. * different entry.
  1930. */
  1931. acl_ht_add_and_freeOld(aclpb->aclpb_macro_ht,
  1932. (PLHashNumber)aci->aci_index,
  1933. matched_val);
  1934. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1935. "-- Added aci(%d) and matched value (%s) to macro ht\n",
  1936. aci->aci_index, matched_val);
  1937. acl_ht_display_ht(aclpb->aclpb_macro_ht);
  1938. } else {
  1939. slapi_ch_free((void **)&matched_val);
  1940. if (matches == ACL_FALSE) {
  1941. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1942. "Evaluated ACL_FALSE\n");
  1943. }
  1944. }
  1945. }
  1946. } /* MACRO_DN */
  1947. /* No need to look further */
  1948. if (matches == ACL_FALSE) {
  1949. goto acl__resource_match_aci_EXIT;
  1950. }
  1951. /*
  1952. ** Here, if there's a targetfilter field, see if it matches.
  1953. **
  1954. ** The commented out code below was an erroneous attempt to skip
  1955. ** this test. It is wrong because: 1. you need to store
  1956. ** whether the last test matched or not (you cannot just assume it did)
  1957. ** and 2. It may not be the same aci, so the previous matched
  1958. ** value is a function of the aci.
  1959. ** May be interesting to build such a cache...but no evidence for
  1960. ** for that right now. See Bug 383424.
  1961. **
  1962. **
  1963. ** && ((aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST) ||
  1964. ** (aclpb->aclpb_res_type & ACLPB_NEW_ENTRY))
  1965. */
  1966. if (aci->aci_type & ACI_TARGET_FILTER ) {
  1967. int filter_matched = ACL_TRUE;
  1968. /*
  1969. * Check for macros.
  1970. * For targetfilter we need to fake the lasinfo structure--it's
  1971. * created "naturally" for subjects but not targets.
  1972. */
  1973. if ( aci->aci_type & ACI_TARGET_FILTER_MACRO_DN) {
  1974. lasInfo *lasinfo = NULL;
  1975. lasinfo = (lasInfo*) slapi_ch_malloc( sizeof(lasInfo) );
  1976. lasinfo->aclpb = aclpb;
  1977. lasinfo->resourceEntry = aclpb->aclpb_curr_entry;
  1978. aclpb->aclpb_curr_aci = aci;
  1979. filter_matched = aclutil_evaluate_macro( aci->targetFilterStr,
  1980. lasinfo,
  1981. ACL_EVAL_TARGET_FILTER);
  1982. slapi_ch_free((void**)&lasinfo);
  1983. } else {
  1984. if (slapi_vattr_filter_test(NULL, aclpb->aclpb_curr_entry,
  1985. aci->targetFilter,
  1986. 0 /*don't do acess chk*/)!= 0) {
  1987. filter_matched = ACL_FALSE;
  1988. }
  1989. }
  1990. /* If it's a logical value we can do logic on it...otherwise we do not match */
  1991. if ( filter_matched == ACL_TRUE || filter_matched == ACL_FALSE) {
  1992. if (aci->aci_type & ACI_TARGET_FILTER_NOT) {
  1993. matches = (filter_matched == ACL_TRUE ? ACL_FALSE : ACL_TRUE);
  1994. } else {
  1995. matches = (filter_matched == ACL_TRUE ? ACL_TRUE: ACL_FALSE);
  1996. }
  1997. } else {
  1998. matches = ACL_FALSE;
  1999. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2000. "Returning UNDEFINED for targetfilter evaluation.\n");
  2001. }
  2002. if (matches == ACL_FALSE) {
  2003. goto acl__resource_match_aci_EXIT;
  2004. }
  2005. }
  2006. /*
  2007. * Check to see if we need to evaluate any targetattrfilters.
  2008. * They look as follows:
  2009. * (targetattrfilters="add=sn:(sn=rob) && gn:(gn!=byrne),
  2010. * del=sn:(sn=rob) && gn:(gn=byrne)")
  2011. *
  2012. * For ADD/DELETE:
  2013. * If theres's a targetattrfilter then each add/del filter
  2014. * that applies to an attribute in the entry, must be satisfied
  2015. * by each value of the attribute in the entry.
  2016. *
  2017. * For MODIFY:
  2018. * If there's a targetattrfilter then the add/del filter
  2019. * must be satisfied by the attribute to be added/deleted.
  2020. * (MODIFY acl is evaluated one value at a time).
  2021. *
  2022. *
  2023. */
  2024. if (((aclpb->aclpb_access & SLAPI_ACL_ADD) &&
  2025. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS) )||
  2026. ((aclpb->aclpb_access & SLAPI_ACL_DELETE) &&
  2027. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS) ) ) {
  2028. Targetattrfilter **attrFilterArray;
  2029. Targetattrfilter *attrFilter = NULL;
  2030. Slapi_Attr *attr_ptr = NULL;
  2031. Slapi_Value *sval;
  2032. const struct berval *attrVal;
  2033. int k;
  2034. int done;
  2035. if (aclpb->aclpb_access & SLAPI_ACL_ADD &&
  2036. aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS) {
  2037. attrFilterArray = aci->targetAttrAddFilters;
  2038. } else if (aclpb->aclpb_access & SLAPI_ACL_DELETE &&
  2039. aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS) {
  2040. attrFilterArray = aci->targetAttrDelFilters;
  2041. }
  2042. attr_matched = ACL_TRUE;
  2043. num_attrs = 0;
  2044. while (attrFilterArray[num_attrs] && attr_matched) {
  2045. attrFilter = attrFilterArray[num_attrs];
  2046. /*
  2047. * If this filter applies to an attribute in the entry,
  2048. * apply it to the entry.
  2049. * Otherwise just ignore it.
  2050. *
  2051. */
  2052. if (slapi_entry_attr_find ( aclpb->aclpb_curr_entry,
  2053. attrFilter->attr_str,
  2054. &attr_ptr) == 0) {
  2055. /*
  2056. * This is an applicable filter.
  2057. * The filter is to be appplied to the entry being added
  2058. * or deleted.
  2059. * The filter needs to be satisfied by _each_ occurence
  2060. * of the attribute in the entry--otherwise you
  2061. * could satisfy the filter and then put loads of other
  2062. * values in on the back of it.
  2063. */
  2064. sval=NULL;
  2065. attrVal=NULL;
  2066. k= slapi_attr_first_value(attr_ptr,&sval);
  2067. done = 0;
  2068. while(k != -1 && !done) {
  2069. attrVal = slapi_value_get_berval(sval);
  2070. if ( acl__make_filter_test_entry(
  2071. &aclpb->aclpb_filter_test_entry,
  2072. attrFilter->attr_str,
  2073. (struct berval *)attrVal) == LDAP_SUCCESS ) {
  2074. attr_matched= acl__test_filter(
  2075. aclpb->aclpb_filter_test_entry,
  2076. attrFilter->filter,
  2077. 1 /* Do filter sense evaluation below */
  2078. );
  2079. done = !attr_matched;
  2080. slapi_entry_free( aclpb->aclpb_filter_test_entry );
  2081. }
  2082. k= slapi_attr_next_value(attr_ptr, k, &sval);
  2083. }/* while */
  2084. /*
  2085. * Here, we applied an applicable filter to the entry.
  2086. * So if attr_matched is ACL_TRUE then every value
  2087. * of the attribute in the entry satisfied the filter.
  2088. * Otherwise, attr_matched is ACL_FALSE and not every
  2089. * value satisfied the filter, so we will teminate the
  2090. * scan of the filter list.
  2091. */
  2092. }
  2093. num_attrs++;
  2094. } /* while */
  2095. /*
  2096. * Here, we've applied all the applicable filters to the entry.
  2097. * Each one must have been satisfied by all the values of the attribute.
  2098. * The result of this is stored in attr_matched.
  2099. */
  2100. #if 0
  2101. /*
  2102. * Don't support a notion of "add != " or "del != "
  2103. * at the moment.
  2104. * To do this, need to test whether it's an add test or del test
  2105. * then if it's add and ACI_TARGET_ATTR_ADD_FILTERS_NOT then
  2106. * flip the bit. Same for del.
  2107. */
  2108. if (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS_NOT) {
  2109. matches = (matches ? ACL_FALSE : ACL_TRUE);
  2110. } else {
  2111. matches = (matches ? ACL_TRUE: ACL_FALSE);
  2112. }
  2113. #endif
  2114. /* No need to look further */
  2115. if (attr_matched == ACL_FALSE) {
  2116. matches = ACL_FALSE;
  2117. goto acl__resource_match_aci_EXIT;
  2118. }
  2119. } else if ( ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_ADD) &&
  2120. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS)) ||
  2121. ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_DEL) &&
  2122. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS)) ) {
  2123. /*
  2124. * Here, it's a modify add/del and we have attr filters.
  2125. * So, we need to scan the add/del filter list to find the filter
  2126. * that applies to the current attribute.
  2127. * Then the (attribute,value) pair being added/deleted better
  2128. * match that filter.
  2129. *
  2130. *
  2131. */
  2132. Targetattrfilter **attrFilterArray = NULL;
  2133. Targetattrfilter *attrFilter;
  2134. int found = 0;
  2135. if ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_ADD) &&
  2136. (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS)) {
  2137. attrFilterArray = aci->targetAttrAddFilters;
  2138. } else if ((aclpb->aclpb_access & ACLPB_SLAPI_ACL_WRITE_DEL) &&
  2139. (aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS)) {
  2140. attrFilterArray = aci->targetAttrDelFilters;
  2141. }
  2142. /*
  2143. * Scan this filter list for an applicable filter.
  2144. */
  2145. found = 0;
  2146. num_attrs = 0;
  2147. while (attrFilterArray[num_attrs] && !found) {
  2148. attrFilter = attrFilterArray[num_attrs];
  2149. /* If this filter applies to the attribute, stop. */
  2150. if ((aclpb->aclpb_curr_attrEval) &&
  2151. slapi_attr_type_cmp ( aclpb->aclpb_curr_attrEval->attrEval_name,
  2152. attrFilter->attr_str, 1) == 0) {
  2153. found = 1;
  2154. }
  2155. num_attrs++;
  2156. }
  2157. /*
  2158. * Here, if found an applicable filter, then apply the filter to the
  2159. * (attr,val) pair.
  2160. * Otherwise, ignore the targetattrfilters.
  2161. */
  2162. if (found) {
  2163. if ( acl__make_filter_test_entry(
  2164. &aclpb->aclpb_filter_test_entry,
  2165. aclpb->aclpb_curr_attrEval->attrEval_name,
  2166. aclpb->aclpb_curr_attrVal) == LDAP_SUCCESS ) {
  2167. attr_matched= acl__test_filter(aclpb->aclpb_filter_test_entry,
  2168. attrFilter->filter,
  2169. 1 /* Do filter sense evaluation below */
  2170. );
  2171. slapi_entry_free( aclpb->aclpb_filter_test_entry );
  2172. }
  2173. /* No need to look further */
  2174. if (attr_matched == ACL_FALSE) {
  2175. matches = attr_matched;
  2176. goto acl__resource_match_aci_EXIT;
  2177. }
  2178. /*
  2179. * Here this attribute appeared and was matched in a
  2180. * targetattrfilters list, so record this fact so we do
  2181. * not have to scan the targetattr list for the attribute.
  2182. */
  2183. attr_matched_in_targetattrfilters = 1;
  2184. }
  2185. } /* targetvaluefilters */
  2186. /* There are 3 cases by which acis are selected.
  2187. ** 1) By scanning the whole list and picking based on the resource.
  2188. ** 2) By picking a subset of the list which will be used for the whole
  2189. ** acl evaluation.
  2190. ** 3) A finer granularity, i.e, a selected list of acls which will be
  2191. ** used for only that entry's evaluation.
  2192. */
  2193. if ( !(skip_attrEval) && (aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_ENTRY_LIST) &&
  2194. (res_right & SLAPI_ACL_SEARCH) &&
  2195. ((aci->aci_access & SLAPI_ACL_READ) || (aci->aci_access & SLAPI_ACL_SEARCH))) {
  2196. int kk=0;
  2197. while ( kk < aclpb_max_selected_acls && aclpb->aclpb_handles_index[kk] >=0 ) kk++;
  2198. if (kk >= aclpb_max_selected_acls) {
  2199. aclpb->aclpb_state &= ~ACLPB_SEARCH_BASED_ON_ENTRY_LIST;
  2200. } else {
  2201. aclpb->aclpb_handles_index[kk++] = aci->aci_index;
  2202. aclpb->aclpb_handles_index[kk] = -1;
  2203. }
  2204. }
  2205. /* If we are suppose to skip attr eval, then let's skip it */
  2206. if ( (aclpb->aclpb_access & SLAPI_ACL_SEARCH ) && ( ! skip_attrEval ) &&
  2207. ( aclpb->aclpb_res_type & ACLPB_NEW_ENTRY )) {
  2208. aclEvalContext *c_evalContext = &aclpb->aclpb_curr_entryEval_context;
  2209. int nhandle = c_evalContext->acle_numof_tmatched_handles;
  2210. if ( nhandle < aclpb_max_selected_acls) {
  2211. c_evalContext->acle_handles_matched_target[nhandle] = aci->aci_index;
  2212. c_evalContext->acle_numof_tmatched_handles++;
  2213. }
  2214. }
  2215. if ( skip_attrEval ) {
  2216. goto acl__resource_match_aci_EXIT;
  2217. }
  2218. /* We need to check again because we don't want to select this handle
  2219. ** if the right doesn't match for now.
  2220. */
  2221. if (!(aci_right & res_right)) {
  2222. matches = ACL_FALSE;
  2223. goto acl__resource_match_aci_EXIT;
  2224. }
  2225. /*
  2226. * Here if the request is one that requires matching
  2227. * on a targetattr then do it here.
  2228. * If we have already matched an attribute in the targetattrfitlers list
  2229. * then we do not require a match in the targetattr so we can skip it.
  2230. * The operations that require targetattr are SLAPI_ACL_COMPARE,
  2231. * SLAPI_ACL_SEARCH, SLAPI_ACL_READ and SLAPI_ACL_WRITE, as long as
  2232. * c_attrEval is non-null (otherwise it's a modrdn op which
  2233. * does not require the targetattr list).
  2234. *
  2235. * rbyrneXXX if we had a proper permission for modrdn eg SLAPI_ACL_MODRDN
  2236. * then we would not need this crappy way of telling it was a MODRDN
  2237. * request ie. SLAPI_ACL_WRITE && !(c_attrEval).
  2238. */
  2239. c_attrEval = aclpb->aclpb_curr_attrEval;
  2240. /*
  2241. * If we've already matched on targattrfilter then do not
  2242. * bother to look at the attrlist.
  2243. */
  2244. if (!attr_matched_in_targetattrfilters) {
  2245. /* match target attr */
  2246. if ((c_attrEval) &&
  2247. (aci->aci_type & ACI_TARGET_ATTR)) {
  2248. /* there is a target ATTR */
  2249. Targetattr **attrArray = aci->targetAttr;
  2250. Targetattr *attr = NULL;
  2251. res_attr = c_attrEval->attrEval_name;
  2252. attr_matched = ACL_FALSE;
  2253. star_matched = ACL_FALSE;
  2254. num_attrs = 0;
  2255. while (attrArray[num_attrs] && !attr_matched) {
  2256. attr = attrArray[num_attrs];
  2257. if (attr->attr_type & ACL_ATTR_STRING) {
  2258. if (slapi_attr_type_cmp ( res_attr,
  2259. attr->u.attr_str, 1) == 0) {
  2260. attr_matched = ACL_TRUE;
  2261. *a_matched = ACL_TRUE;
  2262. }
  2263. } else if (attr->attr_type & ACL_ATTR_FILTER) {
  2264. if (ACL_TRUE == acl_match_substring (
  2265. attr->u.attr_filter,
  2266. res_attr, 1)) {
  2267. attr_matched = ACL_TRUE;
  2268. *a_matched = ACL_TRUE;
  2269. }
  2270. } else if (attr->attr_type & ACL_ATTR_STAR) {
  2271. attr_matched = ACL_TRUE;
  2272. *a_matched = ACL_TRUE;
  2273. star_matched = ACL_TRUE;
  2274. }
  2275. num_attrs++;
  2276. }
  2277. if (aci->aci_type & ACI_TARGET_ATTR_NOT) {
  2278. matches = (attr_matched ? ACL_FALSE : ACL_TRUE);
  2279. } else {
  2280. matches = (attr_matched ? ACL_TRUE: ACL_FALSE);
  2281. }
  2282. aclpb->aclpb_state &= ~ACLPB_ATTR_STAR_MATCHED;
  2283. /* figure out how it matched, i.e star matched */
  2284. if (matches && star_matched && num_attrs == 1 &&
  2285. !(aclpb->aclpb_state & ACLPB_FOUND_ATTR_RULE))
  2286. aclpb->aclpb_state |= ACLPB_ATTR_STAR_MATCHED;
  2287. else {
  2288. /* we are here means that there is a specific
  2289. ** attr in the rule for this resource.
  2290. ** We need to avoid this case
  2291. ** Rule 1: (targetattr = "uid")
  2292. ** Rule 2: (targetattr = "*")
  2293. ** we cannot use STAR optimization
  2294. */
  2295. aclpb->aclpb_state |= ACLPB_FOUND_ATTR_RULE;
  2296. aclpb->aclpb_state &= ~ACLPB_ATTR_STAR_MATCHED;
  2297. }
  2298. } else if ( (c_attrEval) ||
  2299. (aci->aci_type & ACI_TARGET_ATTR)) {
  2300. if ((aci_right & ACL_RIGHTS_TARGETATTR_NOT_NEEDED) &&
  2301. (aclpb->aclpb_access & ACL_RIGHTS_TARGETATTR_NOT_NEEDED)) {
  2302. /*
  2303. ** Targetattr rule doesn't make any sense
  2304. ** in this case. So select this rule
  2305. ** default: matches = ACL_TRUE;
  2306. */
  2307. ;
  2308. } else if (aci_right & SLAPI_ACL_WRITE &&
  2309. (aci->aci_type & ACI_TARGET_ATTR) &&
  2310. !(c_attrEval)) {
  2311. /* We need to handle modrdn operation. Modrdn doesn't
  2312. ** change any attrs but changes the RDN and so (attr=NULL).
  2313. ** Here we found an acl which has a targetattr but
  2314. ** the resource doesn't need one. In that case, we should
  2315. ** consider this acl.
  2316. ** default: matches = ACL_TRUE;
  2317. */
  2318. ;
  2319. } else {
  2320. matches = ACL_FALSE;
  2321. }
  2322. }
  2323. }/* !attr_matched_in_targetattrfilters */
  2324. /*
  2325. ** Here we are testing if we find a entry test rule (which should
  2326. ** be rare). In that case, just remember it. An entry test rule
  2327. ** doesn't have "(targetattr)".
  2328. */
  2329. if ((aclpb->aclpb_state & ACLPB_EVALUATING_FIRST_ATTR) &&
  2330. (!(aci->aci_type & ACI_TARGET_ATTR))) {
  2331. aclpb->aclpb_state |= ACLPB_FOUND_A_ENTRY_TEST_RULE;
  2332. }
  2333. /*
  2334. * Generic exit point for this routine:
  2335. * matches is ACL_TRUE if the aci matches the target of the resource,
  2336. * ACL_FALSE othrewise.
  2337. * Apologies for the goto--this is a retro-fitted exit point.
  2338. */
  2339. acl__resource_match_aci_EXIT:
  2340. /*
  2341. * For macro acis, there may be a partial macro string
  2342. * placed in the aclpb_macro_ht
  2343. * even if the aci did not finally match.
  2344. * All the partial strings will be freed at aclpb
  2345. * cleanup time.
  2346. */
  2347. if (ACL_TRUE == matches) {
  2348. aclpb->aclpb_stat_aclres_matched++;
  2349. }
  2350. TNF_PROBE_0_DEBUG(acl__resource_match_aci_end,"ACL","");
  2351. return (matches);
  2352. }
  2353. /* Macro to determine if the cached result is valid or not. */
  2354. #define ACL_CACHED_RESULT_VALID( result) \
  2355. (((result & ACLPB_CACHE_READ_RES_ALLOW) && \
  2356. (result & ACLPB_CACHE_READ_RES_SKIP)) || \
  2357. ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) && \
  2358. (result & ACLPB_CACHE_SEARCH_RES_SKIP))) ? 0 : 1
  2359. /***************************************************************************
  2360. *
  2361. * acl__TestRights
  2362. *
  2363. * Test the rights and find out if access is allowed or not.
  2364. *
  2365. * Processing Alogorithm:
  2366. *
  2367. * First, process the DENY rules one by one. If the user is not explicitly
  2368. * denied, then check if the user is allowed by processing the ALLOW handles
  2369. * one by one.
  2370. * The result of the evaluation is cached. Exceptions are
  2371. * -- If an acl happens to be in both DENY and ALLOW camp.
  2372. * -- Only interested for READ/SEARCH right.
  2373. *
  2374. * Input:
  2375. * struct acl_pblock *aclpb - main acl private block
  2376. * int access - The access bits
  2377. * char **right - The right we are looking for
  2378. * char ** generic - Generic rights
  2379. *
  2380. * Returns:
  2381. *
  2382. * ACL_RES_ALLOW - Access allowed
  2383. * ACL_RES_DENY - Access denied
  2384. * err - error condition
  2385. *
  2386. * Error Handling:
  2387. * None.
  2388. *
  2389. **************************************************************************/
  2390. static int
  2391. acl__TestRights(Acl_PBlock *aclpb,int access, char **right, char ** map_generic,
  2392. aclResultReason_t *result_reason)
  2393. {
  2394. ACLEvalHandle_t *acleval;
  2395. int rights_rv = ACL_RES_DENY;
  2396. int rv, i,j, k;
  2397. int index;
  2398. char *deny = NULL;
  2399. char *deny_generic = NULL;
  2400. char *acl_tag;
  2401. int expr_num;
  2402. char *testRights[2];
  2403. aci_t *aci = NULL;
  2404. int numHandles = 0;
  2405. TNF_PROBE_0_DEBUG(acl__TestRights_start,"ACL","");
  2406. /* record the aci and reason for access decision */
  2407. result_reason->deciding_aci = NULL;
  2408. result_reason->reason = ACL_REASON_NONE;
  2409. /* If we don't have any ALLLOW handles, it's DENY by default */
  2410. if (aclpb->aclpb_num_allow_handles <= 0) {
  2411. result_reason->deciding_aci = NULL;
  2412. result_reason->reason = ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS;
  2413. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2414. tnf_string,no_allows,"");
  2415. return ACL_RES_DENY;
  2416. }
  2417. /* Get the ACL evaluation Context */
  2418. acleval = aclpb->aclpb_acleval;
  2419. testRights[0] = *right;
  2420. testRights[1] = '\0';
  2421. /*
  2422. ** START PROCESSING DENY HANDLES
  2423. ** Process each handle at a time. Do not concatenate the handles or else
  2424. ** all the context information will be build again and we will pay a
  2425. ** lot of penalty. The context is built the first time the handle is
  2426. ** processed.
  2427. **
  2428. ** First we set the default to INVALID so that if rules are not matched, then
  2429. ** we get INVALID and if a rule matched, the we get DENY.
  2430. */
  2431. aclpb->aclpb_state &= ~ACLPB_EXECUTING_ALLOW_HANDLES;
  2432. aclpb->aclpb_state |= ACLPB_EXECUTING_DENY_HANDLES;
  2433. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2434. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_deny_handles;
  2435. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_deny_handles ; ++i) {
  2436. int skip_eval = 0;
  2437. /*
  2438. ** If the handle has been evaluated before, we can
  2439. ** cache the result.
  2440. */
  2441. if ((aci = aclpb->aclpb_deny_handles[i]) == NULL) {
  2442. if (i <= ACI_MAX_ELEVEL) {
  2443. continue;
  2444. } else {
  2445. break;
  2446. }
  2447. }
  2448. k++;
  2449. index = aci->aci_index;
  2450. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2451. "Evaluating DENY aci(%d) \"%s\"\n", index, aci->aclName);
  2452. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2453. /*
  2454. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2455. * a cache of info about whether applicable acis
  2456. * allowed, did_not_allow or denied access
  2457. */
  2458. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2459. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2460. short result;
  2461. result = aclpb->aclpb_cache_result[j].result;
  2462. if ( result <= 0) break;
  2463. if (!ACL_CACHED_RESULT_VALID(result)) {
  2464. /* something is wrong. Need to evaluate */
  2465. aclpb->aclpb_cache_result[j].result = -1;
  2466. break;
  2467. }
  2468. /*
  2469. ** We have a valid cached result. Let's see if we
  2470. ** have what we need.
  2471. */
  2472. if (access & SLAPI_ACL_SEARCH) {
  2473. if ( result & ACLPB_CACHE_SEARCH_RES_DENY){
  2474. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2475. "DENY:Found SEARCH DENY in cache\n");
  2476. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2477. result_reason->deciding_aci = aci;
  2478. result_reason->reason = ACL_REASON_RESULT_CACHED_DENY;
  2479. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2480. tnf_string,cached_deny,"");
  2481. return ACL_RES_DENY;
  2482. } else if ((result & ACLPB_CACHE_SEARCH_RES_SKIP) ||
  2483. (result & ACLPB_CACHE_SEARCH_RES_ALLOW)) {
  2484. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2485. "DENY:Found SEARCH SKIP in cache\n");
  2486. skip_eval = 1;
  2487. break;
  2488. } else {
  2489. break;
  2490. }
  2491. } else { /* must be READ */
  2492. if (result & ACLPB_CACHE_READ_RES_DENY) {
  2493. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2494. "DENY:Found READ DENY in cache\n");
  2495. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2496. result_reason->deciding_aci = aci;
  2497. result_reason->reason = ACL_REASON_RESULT_CACHED_DENY;
  2498. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2499. tnf_string,cached_deny,"");
  2500. return ACL_RES_DENY;
  2501. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2502. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2503. "DENY:Found READ SKIP in cache\n");
  2504. skip_eval = 1;
  2505. break;
  2506. } else {
  2507. break;
  2508. }
  2509. }
  2510. }
  2511. }
  2512. }
  2513. if (skip_eval) {
  2514. skip_eval = 0;
  2515. continue;
  2516. }
  2517. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2518. if ( rv < 0) {
  2519. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2520. "acl__TestRights:Unable to set the DENY acllist\n");
  2521. continue;
  2522. }
  2523. /*
  2524. ** Now we have all the information we need. We need to call
  2525. ** the ONE ACL to test the rights.
  2526. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2527. */
  2528. aclpb->aclpb_curr_aci = aci;
  2529. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2530. map_generic, &deny,
  2531. &deny_generic,
  2532. &acl_tag, &expr_num);
  2533. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Processed:%d DENY handles Result:%d\n",index, rights_rv);
  2534. if (rights_rv == ACL_RES_FAIL) {
  2535. result_reason->deciding_aci = aci;
  2536. result_reason->reason = ACL_REASON_NONE;
  2537. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2538. tnf_string,evaled_deny,"");
  2539. return ACL_RES_DENY;
  2540. }
  2541. /* have we executed an ATTR RULE */
  2542. if ( aci->aci_ruleType & ACI_ATTR_RULES )
  2543. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2544. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2545. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2546. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2547. break;
  2548. }
  2549. }
  2550. if ( j < aclpb->aclpb_last_cache_result) {
  2551. /* already in cache */
  2552. } else if ( j < aclpb_max_cache_results ) {
  2553. /* j == aclpb->aclpb_last_cache_result &&
  2554. j < ACLPB_MAX_CACHE_RESULTS */
  2555. aclpb->aclpb_last_cache_result++;
  2556. aclpb->aclpb_cache_result[j].aci_index = index;
  2557. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2558. } else { /* cache overflow */
  2559. if ( rights_rv == ACL_RES_DENY) {
  2560. result_reason->deciding_aci = aci;
  2561. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2562. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2563. tnf_string,evaled_deny,"");
  2564. return ACL_RES_DENY;
  2565. } else {
  2566. continue;
  2567. }
  2568. }
  2569. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2570. if (rights_rv == ACL_RES_DENY) {
  2571. if (access & SLAPI_ACL_SEARCH) {
  2572. aclpb->aclpb_cache_result[j].result |=
  2573. ACLPB_CACHE_SEARCH_RES_DENY;
  2574. } else { /* MUST BE READ */
  2575. aclpb->aclpb_cache_result[j].result |=
  2576. ACLPB_CACHE_READ_RES_DENY;
  2577. }
  2578. /* We are done -- return */
  2579. result_reason->deciding_aci = aci;
  2580. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2581. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2582. tnf_string,evaled_deny,"");
  2583. return ACL_RES_DENY;
  2584. } else if (rights_rv == ACL_RES_ALLOW) {
  2585. /* This will happen, of we have an acl with both deny and allow
  2586. ** Since we may not have finished all the deny acl, go thru all
  2587. ** of them. We will use this cached result when we evaluate this
  2588. ** handle in the context of allow handles.
  2589. */
  2590. if (access & SLAPI_ACL_SEARCH) {
  2591. aclpb->aclpb_cache_result[j].result |=
  2592. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2593. } else {
  2594. aclpb->aclpb_cache_result[j].result |=
  2595. ACLPB_CACHE_READ_RES_ALLOW;
  2596. }
  2597. } else {
  2598. if (access & SLAPI_ACL_SEARCH) {
  2599. aclpb->aclpb_cache_result[j].result |=
  2600. ACLPB_CACHE_SEARCH_RES_SKIP;
  2601. } else {
  2602. aclpb->aclpb_cache_result[j].result |=
  2603. ACLPB_CACHE_READ_RES_SKIP;
  2604. }
  2605. continue;
  2606. }
  2607. } else {
  2608. if ( rights_rv == ACL_RES_DENY ) {
  2609. result_reason->deciding_aci = aci;
  2610. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2611. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2612. tnf_string,evaled_deny,"");
  2613. return ACL_RES_DENY;
  2614. }
  2615. }
  2616. }
  2617. /*
  2618. ** START PROCESSING ALLOW HANDLES.
  2619. ** Process each handle at a time. Do not concatenate the handles or else
  2620. ** all the context information will be build again and we will pay a
  2621. ** lot of penalty. The context is built the first time the handle is
  2622. ** processed.
  2623. **
  2624. ** First we set the default to INVALID so that if rules are not matched, then
  2625. ** we get INVALID and if a rule matched, the we get ALLOW.
  2626. */
  2627. aclpb->aclpb_state &= ~ACLPB_EXECUTING_DENY_HANDLES;
  2628. aclpb->aclpb_state |= ACLPB_EXECUTING_ALLOW_HANDLES;
  2629. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2630. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_allow_handles;
  2631. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_allow_handles ; ++i) {
  2632. int skip_eval = 0;
  2633. /*
  2634. ** If the handle has been evaluated before, we can
  2635. ** cache the result.
  2636. */
  2637. if ((aci = aclpb->aclpb_allow_handles[i]) == NULL) {
  2638. if (i <= ACI_MAX_ELEVEL) {
  2639. continue;
  2640. } else {
  2641. break;
  2642. }
  2643. }
  2644. k++;
  2645. index = aci->aci_index;
  2646. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2647. "%d. Evaluating ALLOW aci(%d) \"%s\"\n", k, index, aci->aclName);
  2648. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2649. /*
  2650. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2651. * a cache of info about whether applicable acis
  2652. * allowed, did_not_allow or denied access
  2653. */
  2654. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2655. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2656. short result;
  2657. result = aclpb->aclpb_cache_result[j].result;
  2658. if ( result <= 0) break;
  2659. if (!ACL_CACHED_RESULT_VALID(result)) {
  2660. /* something is wrong. Need to evaluate */
  2661. aclpb->aclpb_cache_result[j].result = -1;
  2662. break;
  2663. }
  2664. /*
  2665. ** We have a valid cached result. Let's see if we
  2666. ** have what we need.
  2667. */
  2668. if (access & SLAPI_ACL_SEARCH) {
  2669. if (result & ACLPB_CACHE_SEARCH_RES_ALLOW) {
  2670. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2671. "Found SEARCH ALLOW in cache\n");
  2672. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2673. result_reason->deciding_aci = aci;
  2674. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2675. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2676. tnf_string,cached_allow,"");
  2677. return ACL_RES_ALLOW;
  2678. } else if ( result & ACLPB_CACHE_SEARCH_RES_SKIP) {
  2679. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2680. "Found SEARCH SKIP in cache\n");
  2681. skip_eval = 1;
  2682. break;
  2683. } else {
  2684. /* need to evaluate */
  2685. break;
  2686. }
  2687. } else {
  2688. if ( result & ACLPB_CACHE_READ_RES_ALLOW) {
  2689. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2690. "Found READ ALLOW in cache\n");
  2691. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2692. result_reason->deciding_aci = aci;
  2693. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2694. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2695. tnf_string,cached_allow,"");
  2696. return ACL_RES_ALLOW;
  2697. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2698. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2699. "Found READ SKIP in cache\n");
  2700. skip_eval = 1;
  2701. break;
  2702. } else {
  2703. break;
  2704. }
  2705. }
  2706. }
  2707. }
  2708. }
  2709. if ( skip_eval) {
  2710. skip_eval = 0;
  2711. continue;
  2712. }
  2713. TNF_PROBE_0_DEBUG(acl__libaccess_start,"ACL","");
  2714. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2715. if ( rv < 0) {
  2716. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  2717. "acl__TestRights:Unable to set the acllist\n");
  2718. continue;
  2719. }
  2720. /*
  2721. ** Now we have all the information we need. We need to call
  2722. ** the ONE ACL to test the rights.
  2723. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2724. */
  2725. aclpb->aclpb_curr_aci = aci;
  2726. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2727. map_generic, &deny,
  2728. &deny_generic,
  2729. &acl_tag, &expr_num);
  2730. TNF_PROBE_0_DEBUG(acl__libaccess_end,"ACL","");
  2731. if (aci->aci_ruleType & ACI_ATTR_RULES)
  2732. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2733. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2734. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2735. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2736. break;
  2737. }
  2738. }
  2739. if ( j < aclpb->aclpb_last_cache_result) {
  2740. /* already in cache */
  2741. } else if ( j < aclpb_max_cache_results ) {
  2742. /* j == aclpb->aclpb_last_cache_result &&
  2743. j < ACLPB_MAX_CACHE_RESULTS */
  2744. aclpb->aclpb_last_cache_result++;
  2745. aclpb->aclpb_cache_result[j].aci_index = index;
  2746. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2747. } else { /* cache overflow */
  2748. slapi_log_error (SLAPI_LOG_FATAL, "acl__TestRights", "cache overflown\n");
  2749. if ( rights_rv == ACL_RES_ALLOW) {
  2750. result_reason->deciding_aci = aci;
  2751. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2752. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2753. tnf_string,evaled_allow,"");
  2754. return ACL_RES_ALLOW;
  2755. } else {
  2756. continue;
  2757. }
  2758. }
  2759. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2760. if (rights_rv == ACL_RES_ALLOW) {
  2761. if (access & SLAPI_ACL_SEARCH) {
  2762. aclpb->aclpb_cache_result[j].result |=
  2763. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2764. } else { /* must be READ */
  2765. aclpb->aclpb_cache_result[j].result |=
  2766. ACLPB_CACHE_READ_RES_ALLOW;
  2767. }
  2768. /* We are done -- return */
  2769. result_reason->deciding_aci = aci;
  2770. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2771. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2772. tnf_string,evaled_allow,"");
  2773. return ACL_RES_ALLOW;
  2774. } else {
  2775. if (access & SLAPI_ACL_SEARCH) {
  2776. aclpb->aclpb_cache_result[j].result |=
  2777. ACLPB_CACHE_SEARCH_RES_SKIP;
  2778. } else {
  2779. aclpb->aclpb_cache_result[j].result |=
  2780. ACLPB_CACHE_READ_RES_SKIP;
  2781. }
  2782. continue;
  2783. }
  2784. } else {
  2785. if ( rights_rv == ACL_RES_ALLOW ) {
  2786. result_reason->deciding_aci = aci;
  2787. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2788. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2789. tnf_string,evaled_allow,"");
  2790. return ACL_RES_ALLOW;
  2791. }
  2792. }
  2793. }/* for */
  2794. result_reason->deciding_aci = aci;
  2795. result_reason->reason = ACL_REASON_NO_MATCHED_SUBJECT_ALLOWS;
  2796. TNF_PROBE_0_DEBUG(acl__TestRights_end,"ACL","");
  2797. return (ACL_RES_DENY);
  2798. }
  2799. /***************************************************************************
  2800. *
  2801. * acl_match_substring
  2802. *
  2803. * Compare the input string to the patteren in the filter
  2804. *
  2805. * Input:
  2806. * struct slapi_filter *f - Filter which has the patteren
  2807. * char *str - String to compare
  2808. * int exact_match - 1; match the pattern exactly
  2809. * - 0; match the pattern as a suffix
  2810. *
  2811. * Returns:
  2812. * ACL_TRUE - The sting matches with the patteren
  2813. * ACL_FALSE - No it doesn't
  2814. * ACL_ERR - Error
  2815. *
  2816. * Error Handling:
  2817. * None.
  2818. *
  2819. **************************************************************************/
  2820. int
  2821. acl_match_substring ( Slapi_Filter *f, char *str, int exact_match)
  2822. {
  2823. int i, rc, len;
  2824. char *p = NULL;
  2825. char *end, *realval, *tmp;
  2826. char pat[BUFSIZ];
  2827. char buf[BUFSIZ];
  2828. char *type, *initial, *final;
  2829. char **any;
  2830. Slapi_Regex *re = NULL;
  2831. const char *re_result = NULL;
  2832. if ( 0 != slapi_filter_get_subfilt ( f, &type, &initial, &any, &final ) ) {
  2833. return (ACL_FALSE);
  2834. }
  2835. /* convert the input to lower. */
  2836. for (p = str; *p; p++)
  2837. *p = TOLOWER ( *p );
  2838. /* construct a regular expression corresponding to the filter: */
  2839. pat[0] = '\0';
  2840. p = pat;
  2841. end = pat + sizeof(pat) - 2; /* leave room for null */
  2842. if ( initial != NULL) {
  2843. strcpy (p, "^");
  2844. p = strchr (p, '\0');
  2845. /* 2 * in case every char is special */
  2846. if (p + 2 * strlen ( initial ) > end) {
  2847. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2848. "not enough pattern space\n");
  2849. return (ACL_ERR);
  2850. }
  2851. if (!exact_match) {
  2852. strcpy (p, ".*");
  2853. p = strchr (p, '\0');
  2854. }
  2855. acl_strcpy_special (p, initial);
  2856. p = strchr (p, '\0');
  2857. }
  2858. if ( any != NULL) {
  2859. for (i = 0; any && any[i] != NULL; i++) {
  2860. /* ".*" + value */
  2861. if (p + 2 * strlen ( any[i]) + 2 > end) {
  2862. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2863. "not enough pattern space\n");
  2864. return (ACL_ERR);
  2865. }
  2866. strcpy (p, ".*");
  2867. p = strchr (p, '\0');
  2868. acl_strcpy_special (p, any[i]);
  2869. p = strchr (p, '\0');
  2870. }
  2871. }
  2872. if ( final != NULL) {
  2873. /* ".*" + value */
  2874. if (p + 2 * strlen ( final ) + 2 > end) {
  2875. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2876. "not enough pattern space\n");
  2877. return (ACL_ERR);
  2878. }
  2879. strcpy (p, ".*");
  2880. p = strchr (p, '\0');
  2881. acl_strcpy_special (p, final);
  2882. p = strchr (p, '\0');
  2883. strcpy (p, "$");
  2884. }
  2885. /* see if regex matches with the input string */
  2886. tmp = NULL;
  2887. len = strlen(str);
  2888. if (len < sizeof(buf)) {
  2889. strcpy (buf, str);
  2890. realval = buf;
  2891. } else {
  2892. tmp = (char*) slapi_ch_malloc (len + 1);
  2893. strcpy (tmp, str);
  2894. realval = tmp;
  2895. }
  2896. /* What we have built is a regular pattaren expression.
  2897. ** Now we will compile the pattern and compare wth the string to
  2898. ** see if the input string matches with the patteren or not.
  2899. */
  2900. re = slapi_re_comp( pat, &re_result );
  2901. if (NULL == re) {
  2902. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2903. "acl_match_substring:re_comp failed (%s)\n", re_result?re_result:"unknown");
  2904. return (ACL_ERR);
  2905. }
  2906. /* slapi_re_exec() returns 1 if the string p1 matches the last compiled
  2907. ** regular expression, 0 if the string p1 failed to match
  2908. */
  2909. rc = slapi_re_exec( re, realval, -1 /* no timelimit */ );
  2910. slapi_re_free(re);
  2911. slapi_ch_free ( (void **) &tmp );
  2912. if (rc == 1) {
  2913. return ACL_TRUE;
  2914. } else {
  2915. return ACL_FALSE;
  2916. }
  2917. }
  2918. /***************************************************************************
  2919. *
  2920. * acl__reset_cached_result
  2921. *
  2922. * Go thru the cached result and invlalidate the cached evaluation results for
  2923. * rules which can only be cached based on the scope.
  2924. * If we have scope ACI_CACHE_RESULT_PER_ENTRY, then we need to invalidate the
  2925. * cacched reult whenever we hit a new entry.
  2926. *
  2927. * Returns:
  2928. * Null
  2929. *
  2930. **************************************************************************/
  2931. static void
  2932. acl__reset_cached_result (struct acl_pblock *aclpb )
  2933. {
  2934. int j;
  2935. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2936. /* Unless we have to clear the result, move on */
  2937. if (!( aclpb->aclpb_cache_result[j].aci_ruleType & ACI_CACHE_RESULT_PER_ENTRY))
  2938. continue;
  2939. aclpb->aclpb_cache_result[j].result = 0;
  2940. }
  2941. }
  2942. /*
  2943. * acl_access_allowed_disjoint_resource
  2944. *
  2945. * This is an internal module which can be used to verify
  2946. * access to a resource which may not be inside the search scope.
  2947. *
  2948. * Returns:
  2949. * - Same return val as acl_access_allowed().
  2950. */
  2951. int
  2952. acl_access_allowed_disjoint_resource(
  2953. Slapi_PBlock *pb,
  2954. Slapi_Entry *e, /* The Slapi_Entry */
  2955. char *attr, /* Attribute of the entry */
  2956. struct berval *val, /* value of attr. NOT USED */
  2957. int access /* access rights */
  2958. )
  2959. {
  2960. int rv;
  2961. struct acl_pblock *aclpb = NULL;
  2962. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  2963. /*
  2964. ** It's possible that we already have a context of ACLs.
  2965. ** However once in a while, we need to test
  2966. ** access to a resource which (like vlv, schema) which falls
  2967. ** outside the search base. In that case, we need to go
  2968. ** thru all the ACLs and not depend upon the acls which we have
  2969. ** gathered.
  2970. */
  2971. /* If you have the right to use the resource, then we don't need to check for
  2972. ** proxy right on that resource.
  2973. */
  2974. if (aclpb)
  2975. aclpb->aclpb_state |= ( ACLPB_DONOT_USE_CONTEXT_ACLS| ACLPB_DONOT_EVALUATE_PROXY );
  2976. rv = acl_access_allowed(pb, e, attr, val, access);
  2977. if (aclpb) aclpb->aclpb_state &= ~ACLPB_DONOT_USE_CONTEXT_ACLS;
  2978. if (aclpb ) aclpb->aclpb_state &= ~ACLPB_DONOT_EVALUATE_PROXY;
  2979. return rv;
  2980. }
  2981. /*
  2982. * acl__attr_cached_result
  2983. * Loops thru the cached result and determines if we can use the cached value.
  2984. *
  2985. * Inputs:
  2986. * Slapi_pblock *aclpb - acl private block
  2987. * char *attr - attribute name
  2988. * int access - access type
  2989. * Returns:
  2990. * LDAP_SUCCESS: - access is granted
  2991. * LDAP_INSUFFICIENT_ACCESS - access denied
  2992. * ACL_ERR - no cached info about this attr.
  2993. * - or the attr had multiple result and so
  2994. * - we can't determine the outcome.
  2995. *
  2996. */
  2997. static int
  2998. acl__attr_cached_result (struct acl_pblock *aclpb, char *attr, int access )
  2999. {
  3000. int i, rc;
  3001. aclEvalContext *c_evalContext;
  3002. if ( !(access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ) ))
  3003. return ACL_ERR;
  3004. if (aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3005. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3006. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3007. "acl__attr_cached_result:Using Context: ACLPB_ACLCB\n" );
  3008. } else {
  3009. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3010. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3011. "acl__attr_cached_result:Using Context: ACLPB_PREV\n" );
  3012. }
  3013. if ( attr == NULL ) {
  3014. int eval_read = 0;
  3015. /*
  3016. ** Do I have access to at least one attribute, then I have
  3017. ** access to the entry.
  3018. */
  3019. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3020. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3021. if ( (access & SLAPI_ACL_READ ) && a_eval->attrEval_r_status &&
  3022. a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3023. eval_read++;
  3024. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3025. return LDAP_SUCCESS;
  3026. /* rbyrne: recompute if we have to.
  3027. * How does this cached result get turned off for
  3028. * attr style acis which acannot be cached becuase entry
  3029. * can result in a diff value.
  3030. */
  3031. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3032. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3033. a_eval->attrEval_r_aciIndex);
  3034. if ( rc != ACL_ERR ) {
  3035. acl_copyEval_context ( aclpb, c_evalContext,
  3036. &aclpb->aclpb_curr_entryEval_context, 1);
  3037. }
  3038. if ( rc == LDAP_SUCCESS) {
  3039. return LDAP_SUCCESS;
  3040. }
  3041. }
  3042. }
  3043. }/* for */
  3044. /*
  3045. * If we have scanned the whole list without success then
  3046. * we are not granting access to this entry through access
  3047. * to an attribute in the list--however this does not mean
  3048. * that we do not have access to the entry via another attribute
  3049. * not already in the list, so return -1 meaning
  3050. * "don't know".
  3051. */
  3052. return(ACL_ERR);
  3053. #if 0
  3054. if ( eval_read )
  3055. return LDAP_INSUFFICIENT_ACCESS;
  3056. else
  3057. return ACL_ERR;
  3058. #endif
  3059. }
  3060. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3061. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3062. if ( a_eval == NULL ) continue;
  3063. if (strcasecmp ( attr, a_eval->attrEval_name ) == 0 ) {
  3064. if ( access & SLAPI_ACL_SEARCH ) {
  3065. if (a_eval->attrEval_s_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3066. if ( a_eval->attrEval_s_status & ACL_ATTREVAL_SUCCESS)
  3067. return LDAP_SUCCESS;
  3068. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_FAIL)
  3069. return LDAP_INSUFFICIENT_ACCESS;
  3070. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_RECOMPUTE ) {
  3071. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3072. a_eval->attrEval_s_aciIndex);
  3073. if ( rc != ACL_ERR ) {
  3074. acl_copyEval_context ( aclpb, c_evalContext,
  3075. &aclpb->aclpb_curr_entryEval_context, 1);
  3076. }
  3077. } else
  3078. return ACL_ERR;
  3079. } else {
  3080. /* This means that for the same attribute and same type of
  3081. ** access, we had different results at different time.
  3082. ** Since we are not caching per object, we can't
  3083. ** determine exactly. So, can't touch this
  3084. */
  3085. return ACL_ERR;
  3086. }
  3087. } else {
  3088. if (a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3089. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3090. return LDAP_SUCCESS;
  3091. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_FAIL)
  3092. return LDAP_INSUFFICIENT_ACCESS;
  3093. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3094. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3095. a_eval->attrEval_r_aciIndex);
  3096. if ( rc != ACL_ERR ) {
  3097. acl_copyEval_context ( aclpb, c_evalContext,
  3098. &aclpb->aclpb_curr_entryEval_context, 1);
  3099. }
  3100. } else
  3101. return ACL_ERR;
  3102. } else {
  3103. /* Look above for explanation */
  3104. return ACL_ERR;
  3105. }
  3106. }
  3107. }
  3108. }
  3109. return ACL_ERR;
  3110. }
  3111. /*
  3112. * Had to do this juggling of casting to make
  3113. * both Nt & unix compiler happy.
  3114. */
  3115. static int
  3116. acl__cmp(const void *a, const void *b)
  3117. {
  3118. short *i = (short *) a;
  3119. short *j = (short *) b;
  3120. if ( (short) *i > (short) *j )
  3121. return (1);
  3122. if ( (short)*i < (short) *j)
  3123. return (-1);
  3124. return (0);
  3125. }
  3126. /*
  3127. * acl__scan_match_handles
  3128. * Go thru the ACL list and determine if the list of acls selected matches
  3129. * what we have in the cache.
  3130. *
  3131. * Inputs:
  3132. * Acl_PBlock *pb - Main pblock ( blacvk hole)
  3133. * int type - Which context to look on
  3134. *
  3135. * Returns:
  3136. * 0 - matches all the acl handles
  3137. * ACL_ERR - sorry; no match
  3138. *
  3139. * ASSUMPTION: A READER LOCK ON ACL LIST
  3140. */
  3141. static int
  3142. acl__scan_match_handles ( Acl_PBlock *aclpb, int type)
  3143. {
  3144. int matched = 0;
  3145. aci_t *aci = NULL;
  3146. int index;
  3147. PRUint32 cookie;
  3148. aclEvalContext *c_evalContext = NULL;
  3149. if (type == ACLPB_EVALCONTEXT_PREV ) {
  3150. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3151. } else if ( type == ACLPB_EVALCONTEXT_ACLCB ){
  3152. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3153. } else {
  3154. return ACL_ERR;
  3155. }
  3156. if ( !c_evalContext->acle_numof_tmatched_handles )
  3157. return ACL_ERR;
  3158. aclpb->aclpb_stat_acllist_scanned++;
  3159. aci = acllist_get_first_aci ( aclpb, &cookie );
  3160. while ( aci ) {
  3161. index = aci->aci_index;
  3162. if (acl__resource_match_aci(aclpb, aci, 1 /* skip attr matching */, NULL )) {
  3163. int j;
  3164. int s_matched = matched;
  3165. /* We have a sorted list of handles that matched the target */
  3166. for (j=0; j < c_evalContext->acle_numof_tmatched_handles ; j++ ) {
  3167. if ( c_evalContext->acle_handles_matched_target[j] > index )
  3168. break;
  3169. else if ( index == c_evalContext->acle_handles_matched_target[j] ) {
  3170. int jj;
  3171. matched++;
  3172. /* See if this is a ATTR rule that matched -- in that case we have
  3173. ** to nullify the cached result
  3174. */
  3175. if ( aci->aci_ruleType & ACI_ATTR_RULES ) {
  3176. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3177. "Found an attr Rule [Name:%s Index:%d\n", aci->aclName,
  3178. aci->aci_index );
  3179. for ( jj =0; jj < c_evalContext->acle_numof_attrs; jj++ ) {
  3180. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[jj];
  3181. if ( a_eval->attrEval_r_aciIndex == aci->aci_index )
  3182. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3183. if ( a_eval->attrEval_s_aciIndex == aci->aci_index )
  3184. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3185. }
  3186. }
  3187. break;
  3188. }
  3189. }
  3190. if ( s_matched == matched ) return ACL_ERR;
  3191. }
  3192. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3193. }
  3194. if ( matched == c_evalContext->acle_numof_tmatched_handles )
  3195. return 0;
  3196. return ACL_ERR;
  3197. }
  3198. /*
  3199. * acl_copyEval_context
  3200. * Copy the context info which include attr info and handles.
  3201. *
  3202. * Inputs
  3203. * struct acl_pblock *aclpb - acl private main block
  3204. * aclEvalContext *src - src context
  3205. * aclEvalContext *dest - dest context
  3206. * Returns:
  3207. * None.
  3208. *
  3209. */
  3210. void
  3211. acl_copyEval_context ( struct acl_pblock *aclpb, aclEvalContext *src,
  3212. aclEvalContext *dest , int copy_attr_only )
  3213. {
  3214. int d_slot, i;
  3215. /* Do a CLEAN copy we have nothing or else do an incremental copy.*/
  3216. if ( src->acle_numof_attrs < 1 )
  3217. return;
  3218. /* Copy the attr info */
  3219. if ( dest->acle_numof_attrs < 1 )
  3220. acl_clean_aclEval_context ( dest, 0 /*clean */ );
  3221. d_slot = dest->acle_numof_attrs;
  3222. for (i=0; i < src->acle_numof_attrs; i++ ) {
  3223. int j;
  3224. int attr_exists = 0;
  3225. int dd_slot = d_slot;
  3226. if ( aclpb && (i == 0) ) aclpb->aclpb_stat_num_copycontext++;
  3227. if ( src->acle_attrEval[i].attrEval_r_status == 0 &&
  3228. src->acle_attrEval[i].attrEval_s_status == 0 )
  3229. continue;
  3230. for ( j = 0; j < dest->acle_numof_attrs; j++ ) {
  3231. if ( strcasecmp ( src->acle_attrEval[i].attrEval_name,
  3232. dest->acle_attrEval[j].attrEval_name ) == 0 ) {
  3233. /* We have it. skip it. */
  3234. attr_exists = 1;
  3235. dd_slot = j;
  3236. break;
  3237. }
  3238. }
  3239. if ( !attr_exists ) {
  3240. if ( dd_slot >= ACLPB_MAX_ATTRS -1 )
  3241. break;
  3242. if ( aclpb) aclpb->aclpb_stat_num_copy_attrs++;
  3243. if ( dest->acle_attrEval[dd_slot].attrEval_name )
  3244. slapi_ch_free ( (void **) &dest->acle_attrEval[dd_slot].attrEval_name );
  3245. dest->acle_attrEval[dd_slot].attrEval_name =
  3246. slapi_ch_strdup ( src->acle_attrEval[i].attrEval_name );
  3247. }
  3248. /* Copy the result status and the aci index */
  3249. dest->acle_attrEval[dd_slot].attrEval_r_status =
  3250. src->acle_attrEval[i].attrEval_r_status;
  3251. dest->acle_attrEval[dd_slot].attrEval_r_aciIndex =
  3252. src->acle_attrEval[i].attrEval_r_aciIndex;
  3253. dest->acle_attrEval[dd_slot].attrEval_s_status =
  3254. src->acle_attrEval[i].attrEval_s_status;
  3255. dest->acle_attrEval[dd_slot].attrEval_s_aciIndex =
  3256. src->acle_attrEval[i].attrEval_s_aciIndex;
  3257. if (!attr_exists ) d_slot++;
  3258. }
  3259. dest->acle_numof_attrs = d_slot;
  3260. dest->acle_attrEval[d_slot].attrEval_name = NULL;
  3261. if ( copy_attr_only )
  3262. return;
  3263. /* First sort the arrays which keeps the acl index numbers */
  3264. qsort ( (char *) src->acle_handles_matched_target,
  3265. (size_t)src->acle_numof_tmatched_handles, sizeof( int ), acl__cmp );
  3266. for (i=0; i < src->acle_numof_tmatched_handles; i++ ) {
  3267. dest->acle_handles_matched_target[i] =
  3268. src->acle_handles_matched_target[i];
  3269. }
  3270. if ( src->acle_numof_tmatched_handles ) {
  3271. dest->acle_numof_tmatched_handles = src->acle_numof_tmatched_handles;
  3272. if ( aclpb) aclpb->aclpb_stat_num_tmatched_acls = src->acle_numof_tmatched_handles;
  3273. }
  3274. }
  3275. /*
  3276. * acl_clean_aclEval_context
  3277. * Clean the eval context
  3278. *
  3279. * Inputs:
  3280. * aclEvalContext *clean_me - COntext to be cleaned
  3281. * int clean_type - 0: clean, 1 scrub
  3282. *
  3283. */
  3284. void
  3285. acl_clean_aclEval_context ( aclEvalContext *clean_me, int scrub_only )
  3286. {
  3287. int i;
  3288. /* Copy the attr info */
  3289. for (i=0; i < clean_me->acle_numof_attrs; i++ ) {
  3290. char *a_name = clean_me->acle_attrEval[i].attrEval_name;
  3291. if ( a_name && !scrub_only) {
  3292. slapi_ch_free ( (void **) &a_name );
  3293. clean_me->acle_attrEval[i].attrEval_name = NULL;
  3294. }
  3295. clean_me->acle_attrEval[i].attrEval_r_status = 0;
  3296. clean_me->acle_attrEval[i].attrEval_s_status = 0;
  3297. clean_me->acle_attrEval[i].attrEval_r_aciIndex = 0;
  3298. clean_me->acle_attrEval[i].attrEval_s_aciIndex = 0;
  3299. }
  3300. if ( !scrub_only ) clean_me->acle_numof_attrs = 0;
  3301. clean_me->acle_numof_tmatched_handles = 0;
  3302. }
  3303. /*
  3304. * acl__match_handlesFromCache
  3305. *
  3306. * We have 2 cacheed information
  3307. * 1) cached info from the previous operation
  3308. * 2) cached info from the prev entry evaluation
  3309. *
  3310. * What we are doing here is going thru all the acls and see if the same
  3311. * set of acls apply to this resource or not. If it does, then do we have
  3312. * a cached info for the attr. If we don't for both of the cases then we need
  3313. * to evaluate all over again.
  3314. *
  3315. * Inputs:
  3316. * struct acl_pblock - ACL private block;
  3317. * char *attr - Attribute name
  3318. * int access - acces type
  3319. *
  3320. * returns:
  3321. * LDAP_SUCCESS (0) - The same acls apply and we have
  3322. * access ALLOWED on the attr
  3323. * LDAP_INSUFFICIENT_ACCESS - The same acls apply and we have
  3324. * access DENIED on the attr
  3325. * -1 - Acls doesn't match or we don't have
  3326. * cached info for this attr.
  3327. *
  3328. * ASSUMPTIONS: A reader lock has been obtained for the acl list.
  3329. */
  3330. static int
  3331. acl__match_handlesFromCache ( Acl_PBlock *aclpb, char *attr, int access)
  3332. {
  3333. aclEvalContext *c_evalContext = NULL;
  3334. int context_type = 0;
  3335. int ret_val = -1; /* it doen't match by default */
  3336. /* Before we proceed, find out if we have evaluated any ATTR RULE. If we have
  3337. ** then we can't use any caching mechanism
  3338. */
  3339. if ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3340. context_type = ACLPB_EVALCONTEXT_ACLCB;
  3341. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3342. } else {
  3343. context_type = ACLPB_EVALCONTEXT_PREV;
  3344. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3345. }
  3346. if ( aclpb->aclpb_res_type & (ACLPB_NEW_ENTRY | ACLPB_EFFECTIVE_RIGHTS) ) {
  3347. aclpb->aclpb_state |= ACLPB_MATCHES_ALL_ACLS;
  3348. ret_val = acl__scan_match_handles ( aclpb, context_type );
  3349. if ( -1 == ret_val ) {
  3350. aclpb->aclpb_state &= ~ACLPB_MATCHES_ALL_ACLS;
  3351. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  3352. /* Did not match */
  3353. if ( context_type == ACLPB_EVALCONTEXT_ACLCB ) {
  3354. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT;
  3355. } else {
  3356. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  3357. c_evalContext->acle_numof_tmatched_handles = 0;
  3358. }
  3359. }
  3360. }
  3361. if ( aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  3362. /* See if we have a cached result for this attr */
  3363. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3364. /* It's not in the ACLCB context but we might have it in the
  3365. ** current/prev context. Take a look at it. we might have evaluated
  3366. ** this attribute already.
  3367. */
  3368. if ( (-1 == ret_val ) &&
  3369. ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT )) {
  3370. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3371. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3372. aclpb->aclpb_state |= ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3373. /* We need to do an incremental update */
  3374. if ( !ret_val ) aclpb->aclpb_state |= ACLPB_INCR_ACLCB_CACHE;
  3375. }
  3376. }
  3377. return ret_val;
  3378. }
  3379. /*
  3380. * acl__get_attrEval
  3381. * Get the atteval from the current context and hold the ptr in aclpb.
  3382. * If we have too many attrs, then allocate a new one. In that case
  3383. * we let the caller know about that so that it will be deallocated.
  3384. *
  3385. * Returns:
  3386. * int - 0: The context was indexed. So, no allocations.
  3387. * - 1; context was allocated - deallocate it.
  3388. */
  3389. static int
  3390. acl__get_attrEval ( struct acl_pblock *aclpb, char *attr )
  3391. {
  3392. int j;
  3393. aclEvalContext *c_ContextEval = &aclpb->aclpb_curr_entryEval_context;
  3394. int deallocate_attrEval = 0;
  3395. AclAttrEval *c_attrEval = NULL;
  3396. if ( !attr ) return deallocate_attrEval;
  3397. aclpb->aclpb_curr_attrEval = NULL;
  3398. /* Go thru and see if we have the attr already */
  3399. for (j=0; j < c_ContextEval->acle_numof_attrs; j++) {
  3400. c_attrEval = &c_ContextEval->acle_attrEval[j];
  3401. if ( c_attrEval &&
  3402. slapi_attr_type_cmp ( c_attrEval->attrEval_name, attr, 1) == 0 ) {
  3403. aclpb->aclpb_curr_attrEval = c_attrEval;
  3404. break;
  3405. }
  3406. }
  3407. if ( !aclpb->aclpb_curr_attrEval) {
  3408. if ( c_ContextEval->acle_numof_attrs == ACLPB_MAX_ATTRS -1 ) {
  3409. /* Too many attrs. create a temp one */
  3410. c_attrEval = (AclAttrEval * ) slapi_ch_calloc ( 1, sizeof ( AclAttrEval ) );
  3411. deallocate_attrEval =1;
  3412. } else {
  3413. c_attrEval = &c_ContextEval->acle_attrEval[c_ContextEval->acle_numof_attrs++];
  3414. c_attrEval->attrEval_r_status = 0;
  3415. c_attrEval->attrEval_s_status = 0;
  3416. c_attrEval->attrEval_r_aciIndex = 0;
  3417. c_attrEval->attrEval_s_aciIndex = 0;
  3418. }
  3419. /* clean it before use */
  3420. c_attrEval->attrEval_name = slapi_ch_strdup ( attr );
  3421. aclpb->aclpb_curr_attrEval = c_attrEval;
  3422. }
  3423. return deallocate_attrEval;
  3424. }
  3425. /*
  3426. * acl_skip_access_check
  3427. *
  3428. * See if we need to go thru the ACL check or not. We don't need to if I am root
  3429. * or internal operation or ...
  3430. *
  3431. * returns:
  3432. * ACL_TRUE - Yes; skip the ACL check
  3433. * ACL_FALSE - No; you have to go thru ACL check
  3434. *
  3435. */
  3436. int
  3437. acl_skip_access_check ( Slapi_PBlock *pb, Slapi_Entry *e )
  3438. {
  3439. int rv, isRoot, accessCheckDisabled;
  3440. void *conn = NULL;
  3441. Slapi_Backend *be;
  3442. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  3443. if ( isRoot ) return ACL_TRUE;
  3444. /* See if this is local request */
  3445. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn);
  3446. if ( NULL == conn ) return ACL_TRUE;
  3447. /*
  3448. * Turn on access checking in the rootdse--this code used
  3449. * to skip the access check.
  3450. *
  3451. * check if the entry is the RootDSE entry
  3452. if ( e ) {
  3453. char * edn = slapi_entry_get_ndn ( e );
  3454. if ( slapi_is_rootdse ( edn ) ) return ACL_TRUE;
  3455. }
  3456. */
  3457. /* GB : when referrals are directly set in the mappin tree
  3458. * we can reach this code without a backend in the pblock
  3459. * in such a case, allow access for now
  3460. * we may want to reconsider this is NULL DSE implementation happens
  3461. */
  3462. rv = slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  3463. if (be == NULL)
  3464. return ACL_TRUE;
  3465. rv = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  3466. if ( rv != -1 && accessCheckDisabled ) return ACL_TRUE;
  3467. return ACL_FALSE;
  3468. }
  3469. short
  3470. acl_get_aclsignature ()
  3471. {
  3472. return acl_signature;
  3473. }
  3474. void
  3475. acl_set_aclsignature ( short value)
  3476. {
  3477. acl_signature = value;
  3478. }
  3479. void
  3480. acl_regen_aclsignature ()
  3481. {
  3482. acl_signature = aclutil_gen_signature ( acl_signature );
  3483. }
  3484. /*
  3485. *
  3486. * Assumptions:
  3487. * 1) Called for read/search right.
  3488. */
  3489. static int
  3490. acl__recompute_acl ( Acl_PBlock *aclpb,
  3491. AclAttrEval *a_eval,
  3492. int access,
  3493. int aciIndex
  3494. )
  3495. {
  3496. char *unused_str1, *unused_str2;
  3497. char *acl_tag, *testRight[2];
  3498. int j, expr_num;
  3499. int result_status = ACL_RES_INVALID, cache_result;
  3500. PRUint32 cookie;
  3501. aci_t *aci;
  3502. PR_ASSERT ( aciIndex >= 0 );
  3503. PR_ASSERT ( a_eval != NULL );
  3504. PR_ASSERT (aclpb != NULL );
  3505. /* We might have evaluated this acl just now, check it there first */
  3506. for ( j =0; j < aclpb->aclpb_last_cache_result; j++) {
  3507. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3508. short result;
  3509. result_status =ACL_RES_INVALID;
  3510. result = aclpb->aclpb_cache_result[j].result;
  3511. if ( result <= 0) break;
  3512. if (!ACL_CACHED_RESULT_VALID(result)) {
  3513. /* something is wrong. Need to evaluate */
  3514. aclpb->aclpb_cache_result[j].result = -1;
  3515. break;
  3516. }
  3517. /*
  3518. ** We have a valid cached result. Let's see if we
  3519. ** have what we need.
  3520. */
  3521. if ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) ||
  3522. (result & ACLPB_CACHE_READ_RES_ALLOW) )
  3523. result_status = ACL_RES_ALLOW;
  3524. else if ((result & ACLPB_CACHE_SEARCH_RES_DENY) ||
  3525. (result & ACLPB_CACHE_READ_RES_DENY) )
  3526. result_status = ACL_RES_DENY;
  3527. }
  3528. } /* end of for */
  3529. if ( result_status != ACL_RES_INVALID ) {
  3530. goto set_result_status;
  3531. }
  3532. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3533. "Recomputing the ACL Index:%d for entry:%s\n",
  3534. aciIndex, slapi_entry_get_ndn ( aclpb->aclpb_curr_entry) );
  3535. /* First find this one ACL and then evaluate it. */
  3536. aci = acllist_get_first_aci ( aclpb, &cookie );
  3537. while ( aci && aci->aci_index != aciIndex ) {
  3538. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3539. }
  3540. if (NULL == aci)
  3541. return -1;
  3542. ACL_SetDefaultResult (NULL, aclpb->aclpb_acleval, ACL_RES_INVALID);
  3543. ACL_EvalSetACL(NULL, aclpb->aclpb_acleval, aci->aci_handle);
  3544. testRight[0] = acl_access2str ( access );
  3545. testRight[1] = '\0';
  3546. aclpb->aclpb_curr_aci = aci;
  3547. result_status = ACL_EvalTestRights (NULL, aclpb->aclpb_acleval, testRight,
  3548. ds_map_generic, &unused_str1,
  3549. &unused_str2,
  3550. &acl_tag, &expr_num);
  3551. cache_result = 0;
  3552. if ( result_status == ACL_RES_DENY && aci->aci_type & ACI_HAS_DENY_RULE ) {
  3553. if ( access & SLAPI_ACL_SEARCH)
  3554. cache_result = ACLPB_CACHE_SEARCH_RES_DENY;
  3555. else
  3556. cache_result = ACLPB_CACHE_READ_RES_DENY;
  3557. } else if ( result_status == ACL_RES_ALLOW && aci->aci_type & ACI_HAS_ALLOW_RULE ) {
  3558. if ( access & SLAPI_ACL_SEARCH)
  3559. cache_result = ACLPB_CACHE_SEARCH_RES_ALLOW;
  3560. else
  3561. cache_result = ACLPB_CACHE_READ_RES_ALLOW;
  3562. } else {
  3563. result_status = -1;
  3564. }
  3565. /* Now we need to put the cached result in the aclpb */
  3566. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  3567. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3568. break;
  3569. }
  3570. }
  3571. if ( j < aclpb->aclpb_last_cache_result) {
  3572. /* already in cache */
  3573. } else if ( j < aclpb_max_cache_results-1) {
  3574. /* rbyrneXXX: make this same as other last_cache_result code! */
  3575. j = ++aclpb->aclpb_last_cache_result;
  3576. aclpb->aclpb_cache_result[j].aci_index = aci->aci_index;
  3577. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  3578. } else { /* No more space */
  3579. goto set_result_status;
  3580. }
  3581. /* Add the cached result status */
  3582. aclpb->aclpb_cache_result[j].result |= cache_result;
  3583. set_result_status:
  3584. if (result_status == ACL_RES_ALLOW) {
  3585. if (access & SLAPI_ACL_SEARCH)
  3586. /*wrong bit maskes were being used here--
  3587. a_eval->attrEval_s_status = ACLPB_CACHE_SEARCH_RES_ALLOW;*/
  3588. a_eval->attrEval_s_status = ACL_ATTREVAL_SUCCESS;
  3589. else
  3590. a_eval->attrEval_r_status = ACL_ATTREVAL_SUCCESS;
  3591. } else if ( result_status == ACL_RES_DENY) {
  3592. if (access & SLAPI_ACL_SEARCH)
  3593. a_eval->attrEval_s_status = ACL_ATTREVAL_FAIL;
  3594. else
  3595. a_eval->attrEval_r_status = ACL_ATTREVAL_FAIL;
  3596. } else {
  3597. /* Here, set it to recompute--try again later */
  3598. if (access & SLAPI_ACL_SEARCH)
  3599. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3600. else
  3601. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3602. result_status = -1;
  3603. }
  3604. return result_status;
  3605. }
  3606. static void
  3607. __acl_set_aclIndex_inResult ( Acl_PBlock *aclpb, int access, int index )
  3608. {
  3609. AclAttrEval *c_attrEval = aclpb->aclpb_curr_attrEval;
  3610. if ( c_attrEval ) {
  3611. if ( access & SLAPI_ACL_SEARCH )
  3612. c_attrEval->attrEval_s_aciIndex = index;
  3613. else if ( access & SLAPI_ACL_READ )
  3614. c_attrEval->attrEval_r_aciIndex = index;
  3615. }
  3616. }
  3617. /*
  3618. * If filter_sense is true then return (entry satisfies f).
  3619. * Otherwise, return !(entry satisfies f)
  3620. */
  3621. static int
  3622. acl__test_filter ( Slapi_Entry *entry, struct slapi_filter *f, int filter_sense) {
  3623. int filter_matched;
  3624. /* slapi_vattr_filter_test() returns 0 for match */
  3625. filter_matched = !slapi_vattr_filter_test(NULL, entry,
  3626. f,
  3627. 0 /*don't do acess chk*/);
  3628. if (filter_sense) {
  3629. return(filter_matched ? ACL_TRUE : ACL_FALSE);
  3630. } else {
  3631. return(filter_matched ? ACL_FALSE: ACL_TRUE);
  3632. }
  3633. }
  3634. /*
  3635. * Make an entry consisting of attr_type and attr_val and put
  3636. * a pointer to it in *entry.
  3637. * We will use this afterwards to test for against a filter.
  3638. */
  3639. static int
  3640. acl__make_filter_test_entry ( Slapi_Entry **entry, char *attr_type,
  3641. struct berval *attr_val) {
  3642. struct berval *vals_array[2];
  3643. vals_array[0] = attr_val;
  3644. vals_array[1] = NULL;
  3645. *entry = slapi_entry_alloc();
  3646. slapi_entry_init(*entry, NULL, NULL);
  3647. return (slapi_entry_add_values( *entry, (const char *)attr_type,
  3648. (struct berval**)&vals_array[0] ));
  3649. }
  3650. /*********************************************************************************/
  3651. /* E N D */
  3652. /*********************************************************************************/