acl.c 127 KB

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