acl.c 122 KB

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