acl.c 123 KB

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