acl.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126
  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. ldap_value_free( rdns );
  176. }
  177. 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=%" PRIu64 " 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=%" PRIu64 " 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 syntax. */
  312. if (oid && (strcasecmp(oid, DN_SYNTAX_OID) == 0)) {
  313. /* should use slapi_sdn_compare() but that'a an extra malloc/free */
  314. char *dn_val_to_write = slapi_dn_normalize(slapi_ch_strdup(val->bv_val));
  315. if ( aclpb->aclpb_authorization_sdn &&
  316. slapi_utf8casecmp((ACLUCHP)dn_val_to_write, (ACLUCHP)
  317. slapi_sdn_get_ndn(aclpb->aclpb_authorization_sdn)) == 0) {
  318. access |= SLAPI_ACL_SELF;
  319. }
  320. slapi_ch_free_string(&dn_val_to_write);
  321. }
  322. slapi_ch_free_string(&oid);
  323. slapi_attr_free(&sa);
  324. }
  325. /* Convert access to string of rights eg SLAPI_ACL_ADD->"add". */
  326. if ((right= acl_access2str(access)) == NULL) {
  327. /* ERROR: unknown rights */
  328. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  329. "acl_access_allowed unknown rights:%d\n", access);
  330. ret_val = LDAP_OPERATIONS_ERROR;
  331. goto cleanup_and_ret;
  332. }
  333. /*
  334. * Am I a anonymous dude ? then we can use our anonymous profile
  335. * We don't require the aclpb to have been initialized for anom stuff
  336. *
  337. */
  338. TNF_PROBE_0_DEBUG(acl_anon_test_start,"ACL","");
  339. if ( (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ )) &&
  340. (clientDn && *clientDn == '\0')) {
  341. aclanom_get_suffix_info(e, aclpb);
  342. ret_val = aclanom_match_profile ( pb, aclpb, e, attr, access );
  343. if (ret_val != -1 ) {
  344. if (ret_val == LDAP_SUCCESS ) {
  345. decision_reason.reason = ACL_REASON_ANON_ALLOWED;
  346. } else if (ret_val == LDAP_INSUFFICIENT_ACCESS) {
  347. decision_reason.reason = ACL_REASON_ANON_DENIED;
  348. }
  349. goto cleanup_and_ret;
  350. }
  351. }
  352. TNF_PROBE_0_DEBUG(acl_anon_test_end,"ACL","");
  353. /* copy the value into the aclpb for later checking by the value acl code */
  354. aclpb->aclpb_curr_attrVal = val;
  355. if (!(aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST) &&
  356. (access & SLAPI_ACL_SEARCH)) {
  357. /* We are evaluating SEARCH right for the entry. After that
  358. ** we will eval the READ right. We need to refresh the
  359. ** list of acls selected for evaluation for the entry.
  360. ** Initialize the array so that we indicate nothing has been
  361. ** selected.
  362. */
  363. aclpb->aclpb_handles_index[0] = -1;
  364. /* access is not allowed on entry for search -- it's for
  365. ** read only.
  366. */
  367. aclpb->aclpb_state &= ~ACLPB_ACCESS_ALLOWED_ON_ENTRY;
  368. }
  369. /* set that this is a new entry */
  370. aclpb->aclpb_res_type |= ACLPB_NEW_ENTRY;
  371. aclpb->aclpb_access = 0;
  372. aclpb->aclpb_access |= access;
  373. /*
  374. * stub the Slapi_Entry info first time and only it has changed
  375. * or if the pblock is a psearch pblock--in this case the lifetime
  376. * of entries associated with psearches is such that we cannot cache
  377. * pointers to them--we must always start afresh (see psearch.c).
  378. */
  379. slapi_pblock_get( pb, SLAPI_OPERATION, &op);
  380. if ( operation_is_flag_set(op, OP_FLAG_PS) ||
  381. (aclpb->aclpb_curr_entry_sdn == NULL) ||
  382. (slapi_sdn_compare ( aclpb->aclpb_curr_entry_sdn, e_sdn) != 0) ||
  383. (aclpb->aclpb_curr_entry != e) /* cannot trust the cached entry */ ) {
  384. TNF_PROBE_0_DEBUG(acl_entry_first_touch_start,"ACL","");
  385. slapi_log_error(loglevel, plugin_name,
  386. "#### conn=%" PRIu64 " op=%d binddn=\"%s\"\n",
  387. op->o_connid, op->o_opid, clientDn);
  388. aclpb->aclpb_stat_total_entries++;
  389. if (!(access & SLAPI_ACL_PROXY) &&
  390. !( aclpb->aclpb_state & ACLPB_DONOT_EVALUATE_PROXY )) {
  391. Acl_PBlock *proxy_pb;
  392. proxy_pb = acl_get_aclpb( pb, ACLPB_PROXYDN_PBLOCK );
  393. if (proxy_pb) {
  394. TNF_PROBE_0_DEBUG(acl_access_allowed_proxy_start,"ACL","");
  395. ret_val = acl_access_allowed( pb, e, attr, val, SLAPI_ACL_PROXY );
  396. TNF_PROBE_0_DEBUG(acl_access_allowed_proxy_end,"ACL","");
  397. if (ret_val != LDAP_SUCCESS) goto cleanup_and_ret;
  398. }
  399. }
  400. if ( access & SLAPI_ACL_SEARCH) {
  401. aclpb->aclpb_num_entries++;
  402. if ( aclpb->aclpb_num_entries == 1) {
  403. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  404. } else if ( aclpb->aclpb_state & ACLPB_COPY_EVALCONTEXT ) {
  405. /* We need to copy the evalContext */
  406. acl_copyEval_context ( aclpb, &aclpb->aclpb_curr_entryEval_context,
  407. &aclpb->aclpb_prev_entryEval_context, 0 );
  408. aclpb->aclpb_state &= ~ACLPB_COPY_EVALCONTEXT;
  409. }
  410. acl_clean_aclEval_context ( &aclpb->aclpb_curr_entryEval_context, 1 /*scrub */);
  411. }
  412. /* reset the cached result based on the scope */
  413. acl__reset_cached_result (aclpb );
  414. /* Find all the candidate aci's that apply by scanning up the DIT tree from edn. */
  415. TNF_PROBE_0_DEBUG(acl_aciscan_start,"ACL","");
  416. slapi_sdn_done ( aclpb->aclpb_curr_entry_sdn );
  417. slapi_sdn_set_dn_byval ( aclpb->aclpb_curr_entry_sdn, n_edn );
  418. acllist_aciscan_update_scan ( aclpb, n_edn );
  419. TNF_PROBE_0_DEBUG(acl_aciscan_end,"ACL","");
  420. /* Keep the ptr to the current entry */
  421. aclpb->aclpb_curr_entry = (Slapi_Entry *) e;
  422. /* Get the attr info */
  423. deallocate_attrEval = acl__get_attrEval ( aclpb, attr );
  424. aclutil_print_resource ( aclpb, right, attr, clientDn );
  425. /*
  426. * Used to be PListInitProp(aclpb->aclpb_proplist, 0,
  427. * DS_ATTR_ENTRY, e, 0);
  428. *
  429. * The difference is that PListInitProp() allocates a new property
  430. * every time it's called, overwriting the old name in the PList hash
  431. * table, but not freeing the original property.
  432. * Now, we just create the property at aclpb_malloc() time and
  433. * Assign a new value each time.
  434. */
  435. rv = PListAssignValue(aclpb->aclpb_proplist,
  436. DS_ATTR_ENTRY, e, 0);
  437. if (rv < 0) {
  438. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  439. "Unable to set the Slapi_Entry in the Plist\n",0,0,0);
  440. ret_val = LDAP_OPERATIONS_ERROR;
  441. goto cleanup_and_ret;
  442. }
  443. TNF_PROBE_0_DEBUG(acl_entry_first_touch_end,"ACL","");
  444. } else {
  445. /* we are processing the same entry but for a different
  446. ** attribute. If access is already allowed on that, entry, then
  447. ** it's not a new entry anymore. It's the same old one.
  448. */
  449. TNF_PROBE_0_DEBUG(acl_entry_subs_touch_start,"ACL","");
  450. aclpb->aclpb_res_type &= ~ACLPB_NEW_ENTRY;
  451. /* Get the attr info */
  452. deallocate_attrEval = acl__get_attrEval ( aclpb, attr );
  453. TNF_PROBE_0_DEBUG(acl_entry_subs_touch_end,"ACL","");
  454. }
  455. /* get a lock for the reader */
  456. acllist_acicache_READ_LOCK();
  457. got_reader_locked = 1;
  458. /*
  459. ** Check if we can use any cached information to determine
  460. ** access to this resource
  461. */
  462. if ( (access & SLAPI_ACL_SEARCH) &&
  463. (ret_val = acl__match_handlesFromCache ( aclpb , attr, access)) != -1) {
  464. /* means got a result: allowed or not*/
  465. if (ret_val == LDAP_SUCCESS ) {
  466. decision_reason.reason = ACL_REASON_EVALCONTEXT_CACHED_ALLOW;
  467. } else if (ret_val == LDAP_INSUFFICIENT_ACCESS) {
  468. decision_reason.reason =
  469. ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED;
  470. }
  471. goto cleanup_and_ret;
  472. }
  473. /*
  474. ** Now we have all the information about the resource. Now we need to
  475. ** figure out if there are any ACLs which can be applied.
  476. ** If no ACLs are there, then it's a DENY as default.
  477. */
  478. if (!(acl__scan_for_acis(aclpb, &err))) {
  479. /* We might have accessed the ACL first time which could
  480. ** have caused syntax error.
  481. */
  482. if ( err == ACL_ONEACL_TEXT_ERR)
  483. ret_val = LDAP_INVALID_SYNTAX;
  484. else {
  485. ret_val = LDAP_INSUFFICIENT_ACCESS;
  486. decision_reason.reason = ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS;
  487. }
  488. goto cleanup_and_ret;
  489. }
  490. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  491. "Processed attr:%s for entry:%s\n", attr ? attr : "NULL",
  492. ACL_ESCAPE_STRING_WITH_PUNCTUATION ( n_edn, ebuf), 0);
  493. /*
  494. ** Now evaluate the rights.
  495. ** This is what we have been waiting for.
  496. ** The return value should be ACL_RES_DENY or ACL_RES_ALLOW.
  497. */
  498. rv = acl__TestRights(aclpb, access, &right, ds_map_generic,
  499. &decision_reason);
  500. if ( rv != ACL_RES_ALLOW && (0 == strcasecmp ( right, "selfwrite") ) ) {
  501. /* If I am adding myself to a group, we don't need selfwrite always,
  502. ** write priv is good enough. Since libaccess doesn't provide me a nice
  503. ** way to evaluate OR rights, I have to try again with wite priv.
  504. ** bug: 339051
  505. */
  506. right = access_str_write;
  507. rv = acl__TestRights(aclpb, access, &right, ds_map_generic,
  508. &decision_reason);
  509. }
  510. if (rv == ACL_RES_ALLOW) {
  511. ret_val = LDAP_SUCCESS;
  512. } else {
  513. ret_val = LDAP_INSUFFICIENT_ACCESS;
  514. }
  515. cleanup_and_ret:
  516. TNF_PROBE_0_DEBUG(acl_cleanup_start,"ACL","");
  517. /* I am ready to get out. */
  518. if ( got_reader_locked ) acllist_acicache_READ_UNLOCK();
  519. /* Store the status of the evaluation for this attr */
  520. if ( aclpb && (c_attrEval = aclpb->aclpb_curr_attrEval )) {
  521. if ( deallocate_attrEval ) {
  522. /* In this case we are not caching the result as
  523. ** we have too many attrs. we have malloced space.
  524. ** Get rid of it.
  525. */
  526. slapi_ch_free ( (void **) &c_attrEval->attrEval_name );
  527. slapi_ch_free ( (void **) &c_attrEval );
  528. } else if (ret_val == LDAP_SUCCESS ) {
  529. if ( access & SLAPI_ACL_SEARCH )
  530. c_attrEval->attrEval_s_status |= ACL_ATTREVAL_SUCCESS;
  531. else if ( access & SLAPI_ACL_READ )
  532. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_SUCCESS;
  533. else
  534. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_INVALID;
  535. } else {
  536. if ( access & SLAPI_ACL_SEARCH )
  537. c_attrEval->attrEval_s_status |= ACL_ATTREVAL_FAIL;
  538. else if ( access & SLAPI_ACL_READ )
  539. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_FAIL;
  540. else
  541. c_attrEval->attrEval_r_status |= ACL_ATTREVAL_INVALID;
  542. }
  543. }
  544. if ( aclpb ) aclpb->aclpb_curr_attrEval = NULL;
  545. print_access_control_summary( "main", ret_val, clientDn, aclpb, right,
  546. (attr ? attr : "NULL"),
  547. escape_string_with_punctuation (n_edn, ebuf),
  548. &decision_reason);
  549. TNF_PROBE_0_DEBUG(acl_cleanup_end,"ACL","");
  550. TNF_PROBE_0_DEBUG(acl_access_allowed_end,"ACL","");
  551. return(ret_val);
  552. }
  553. static void print_access_control_summary( char *source, int ret_val, char *clientDn,
  554. struct acl_pblock *aclpb,
  555. char *right,
  556. char *attr,
  557. const char *edn,
  558. aclResultReason_t *acl_reason)
  559. {
  560. struct codebook {
  561. int code;
  562. char *text;
  563. };
  564. static struct codebook reasonbook[] = {
  565. {ACL_REASON_NO_ALLOWS, "no allow acis"},
  566. {ACL_REASON_RESULT_CACHED_DENY, "cached deny"},
  567. {ACL_REASON_RESULT_CACHED_ALLOW, "cached allow"},
  568. {ACL_REASON_EVALUATED_ALLOW, "allowed"},
  569. {ACL_REASON_EVALUATED_DENY, "denied"},
  570. {ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS, "no aci matched the resource"},
  571. {ACL_REASON_NO_MATCHED_SUBJECT_ALLOWS, "no aci matched the subject"},
  572. {ACL_REASON_ANON_ALLOWED, "allow anyone aci matched anon user"},
  573. {ACL_REASON_ANON_DENIED, "no matching anyone aci for anon user"},
  574. {ACL_REASON_EVALCONTEXT_CACHED_ALLOW, "cached context/parent allow"},
  575. {ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED, "cached context/parent deny"},
  576. {ACL_REASON_EVALCONTEXT_CACHED_ATTR_STAR_ALLOW, "cached context/parent allow any attr"},
  577. {ACL_REASON_NONE, "error occurred"},
  578. };
  579. char *anon = "anonymous";
  580. char *null_user = "NULL"; /* bizare case */
  581. char *real_user = NULL;
  582. char *proxy_user = NULL;
  583. char *access_allowed_string = "Allow";
  584. char *access_not_allowed_string = "Deny";
  585. char *access_error_string = "access_error";
  586. char *access_status = NULL;
  587. char *access_reason_none = "no reason available";
  588. char *access_reason = access_reason_none;
  589. char acl_info[ BUFSIZ ];
  590. Slapi_Operation *op = NULL;
  591. int loglevel;
  592. int i;
  593. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  594. if ( !slapi_is_loglevel_set(loglevel) ) {
  595. return;
  596. }
  597. slapi_pblock_get(aclpb->aclpb_pblock, SLAPI_OPERATION, &op); /* for logging */
  598. if (ret_val == LDAP_INSUFFICIENT_ACCESS) {
  599. access_status = access_not_allowed_string;
  600. } else if ( ret_val == LDAP_SUCCESS) {
  601. access_status = access_allowed_string;
  602. } else { /* some kind of error */
  603. access_status = access_error_string;
  604. }
  605. /* decode the reason */
  606. for (i = 0; i < sizeof(reasonbook) / sizeof(struct codebook); i++) {
  607. if ( acl_reason->reason == reasonbook[i].code ) {
  608. access_reason = reasonbook[i].text;
  609. break;
  610. }
  611. }
  612. /* get the acl */
  613. acl_info[0] = '\0';
  614. if (acl_reason->deciding_aci) {
  615. if (acl_reason->reason == ACL_REASON_RESULT_CACHED_DENY ||
  616. acl_reason->reason == ACL_REASON_RESULT_CACHED_ALLOW) {
  617. /* acl is in cache. Its detail must have been printed before.
  618. * So no need to print out acl detail this time.
  619. */
  620. PR_snprintf( &acl_info[0], BUFSIZ, "%s by aci(%d)",
  621. access_reason,
  622. acl_reason->deciding_aci->aci_index);
  623. }
  624. else {
  625. PR_snprintf( &acl_info[0], BUFSIZ, "%s by aci(%d): aciname=%s, acidn=\"%s\"",
  626. access_reason,
  627. acl_reason->deciding_aci->aci_index,
  628. acl_reason->deciding_aci->aclName,
  629. slapi_sdn_get_ndn (acl_reason->deciding_aci->aci_sdn) );
  630. }
  631. }
  632. /* Say who was denied access */
  633. if (clientDn) {
  634. if (clientDn[0] == '\0') {
  635. /* anon */
  636. real_user = anon;
  637. } else {
  638. real_user = clientDn;
  639. }
  640. } else {
  641. real_user = null_user;
  642. }
  643. /* Is there a proxy */
  644. if ( aclpb != NULL && aclpb->aclpb_proxy != NULL) {
  645. if ( aclpb->aclpb_authorization_sdn != NULL ) {
  646. proxy_user = (char *)(aclpb->aclpb_authorization_sdn->ndn ?
  647. aclpb->aclpb_authorization_sdn->ndn:
  648. null_user);
  649. slapi_log_error(loglevel, plugin_name,
  650. "conn=%" PRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) to proxy (%s)"
  651. ": %s\n",
  652. op->o_connid, op->o_opid,
  653. source,
  654. access_status,
  655. right,
  656. edn,
  657. attr ? attr: "NULL",
  658. proxy_user,
  659. acl_info[0] ? acl_info : access_reason);
  660. } else {
  661. proxy_user = null_user;
  662. slapi_log_error(loglevel, plugin_name,
  663. "conn=%" PRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) to proxy (%s)"
  664. ": %s\n",
  665. op->o_connid, op->o_opid,
  666. source,
  667. access_status,
  668. right,
  669. edn,
  670. attr ? attr: "NULL",
  671. proxy_user,
  672. acl_info[0] ? acl_info : access_reason);
  673. }
  674. } else{
  675. slapi_log_error(loglevel, plugin_name,
  676. "conn=%" PRIu64 " op=%d (%s): %s %s on entry(%s).attr(%s) to %s"
  677. ": %s\n",
  678. op->o_connid, op->o_opid,
  679. source,
  680. access_status,
  681. right,
  682. edn,
  683. attr ? attr: "NULL",
  684. real_user,
  685. acl_info[0] ? acl_info : access_reason);
  686. }
  687. }
  688. /***************************************************************************
  689. *
  690. * acl_read_access_allowed_on_entry
  691. * check read access control on the given entry.
  692. *
  693. * Only used during seearch to test for read access on the entry.
  694. * (Could be generalized).
  695. *
  696. * attrs is the list of requested attributes passed with the search.
  697. * If the entry has no attributes (weird case) then the routine survives.
  698. *
  699. * Input:
  700. *
  701. *
  702. * Returns:
  703. * LDAP_SUCCESS - access allowed
  704. * LDAP_INSUFFICIENT_ACCESS - access denied
  705. *
  706. * Error Handling:
  707. * None.
  708. *
  709. **************************************************************************/
  710. int
  711. acl_read_access_allowed_on_entry (
  712. Slapi_PBlock *pb,
  713. Slapi_Entry *e, /* The Slapi_Entry */
  714. char **attrs,
  715. int access /* access rights */
  716. )
  717. {
  718. struct acl_pblock *aclpb;
  719. Slapi_Attr *currAttr;
  720. Slapi_Attr *nextAttr;
  721. int len;
  722. int attr_index = -1;
  723. char *attr_type = NULL;
  724. int rv, isRoot;
  725. char *clientDn;
  726. unsigned long flags;
  727. aclResultReason_t decision_reason;
  728. int loglevel;
  729. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  730. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_entry_start ,"ACL","");
  731. decision_reason.deciding_aci = NULL;
  732. decision_reason.reason = ACL_REASON_NONE;
  733. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  734. /*
  735. ** If it's the root, or acl is off or the entry is a rootdse,
  736. ** Then you have the privilege to read it.
  737. */
  738. if ( acl_skip_access_check ( pb, e ) ) {
  739. char *n_edn = slapi_entry_get_ndn ( e );
  740. char ebuf [ BUFSIZ ];
  741. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  742. "Root access (%s) allowed on entry(%s)\n",
  743. acl_access2str(access),
  744. ACL_ESCAPE_STRING_WITH_PUNCTUATION (n_edn, ebuf));
  745. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  746. tnf_string,skip_access,"");
  747. return LDAP_SUCCESS;
  748. }
  749. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  750. if ( !aclpb ) {
  751. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 2 \n" );
  752. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  753. tnf_string,end,"aclpb error");
  754. return LDAP_OPERATIONS_ERROR;
  755. }
  756. /*
  757. * Am I a anonymous dude ? then we can use our anonympous profile
  758. * We don't require the aclpb to have been initialized for anom stuff
  759. *
  760. */
  761. slapi_pblock_get ( pb, SLAPI_REQUESTOR_DN, &clientDn );
  762. if ( clientDn && *clientDn == '\0' ) {
  763. int ret_val;
  764. ret_val = aclanom_match_profile ( pb, aclpb, e, NULL, SLAPI_ACL_READ );
  765. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  766. tnf_string,end,"anon");
  767. if (ret_val != -1 ) return ret_val;
  768. }
  769. aclpb->aclpb_state &= ~ACLPB_RESET_MASK;
  770. if (aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  771. int ret_val;
  772. ret_val = acl__attr_cached_result (aclpb, NULL, SLAPI_ACL_READ);
  773. if (ret_val != -1 ) {
  774. /* print summary if loglevel set */
  775. if ( slapi_is_loglevel_set(loglevel) ) {
  776. char *n_edn;
  777. n_edn = slapi_entry_get_ndn ( e );
  778. if ( ret_val == LDAP_SUCCESS) {
  779. decision_reason.reason =
  780. ACL_REASON_EVALCONTEXT_CACHED_ALLOW;
  781. } else {
  782. decision_reason.reason =
  783. ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED;
  784. }
  785. /*
  786. * pass NULL as the attr as this routine is concerned with
  787. * access at the entry level.
  788. */
  789. print_access_control_summary( "on entry",
  790. ret_val, clientDn, aclpb,
  791. acl_access2str(SLAPI_ACL_READ),
  792. NULL, n_edn,
  793. &decision_reason);
  794. }
  795. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","",
  796. tnf_string,eval_context_cached,"");
  797. return ret_val;
  798. }
  799. }
  800. /*
  801. * Currently do not use this code--it results in confusing
  802. * behaviour..see 529905
  803. */
  804. #ifdef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  805. /* Do we have access to the entry by virtue of
  806. ** having access to an attr. Before that, let's find out which attrs
  807. ** the user want. If the user has specified certain attributes, then
  808. ** we check aginst that set of attributes.
  809. */
  810. if (!((aclpb->aclpb_state & ACLPB_USER_WANTS_ALL_ATTRS) ||
  811. (aclpb->aclpb_state & ACLPB_USER_SPECIFIED_ATTARS))) {
  812. int i;
  813. if (attrs == NULL) {
  814. aclpb->aclpb_state |= ACLPB_USER_WANTS_ALL_ATTRS;
  815. } else {
  816. for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
  817. if ( strcmp( LDAP_ALL_USER_ATTRS, attrs[i] ) == 0 ) {
  818. aclpb->aclpb_state |= ACLPB_USER_WANTS_ALL_ATTRS;
  819. break;
  820. }
  821. }
  822. }
  823. if (!(aclpb->aclpb_state & ACLPB_USER_WANTS_ALL_ATTRS)) {
  824. for (i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
  825. if ( !slapi_entry_attr_find ( e, attrs[i], &currAttr ) ) {
  826. aclpb->aclpb_state |= ACLPB_USER_SPECIFIED_ATTARS;
  827. break;
  828. }
  829. }
  830. }
  831. } /* end of all user test*/
  832. /*
  833. ** If user has specified a list of attrs, might as well use it
  834. ** to determine access control.
  835. */
  836. currAttr = NULL;
  837. attr_index = -1;
  838. if ( aclpb->aclpb_state & ACLPB_USER_SPECIFIED_ATTARS) {
  839. attr_index = 0;
  840. attr_type = attrs[attr_index++];
  841. } else {
  842. /* Skip the operational attributes -- if there are any in the front */
  843. slapi_entry_first_attr ( e, &currAttr );
  844. if (currAttr != NULL) {
  845. slapi_attr_get_flags ( currAttr, &flags );
  846. while ( flags & SLAPI_ATTR_FLAG_OPATTR ) {
  847. flags = 0;
  848. rv = slapi_entry_next_attr ( e, currAttr, &nextAttr );
  849. if ( !rv ) slapi_attr_get_flags ( nextAttr, &flags );
  850. currAttr = nextAttr;
  851. }
  852. /* Get the attr type */
  853. if ( currAttr ) slapi_attr_get_type ( currAttr , &attr_type );
  854. }
  855. }
  856. #endif /*DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES*/
  857. #ifndef DETERMINE_ACCESS_BASED_ON_REQUESTED_ATTRIBUTES
  858. /*
  859. * Here look at each attribute in the entry and see if
  860. * we have read access to it--if we do
  861. * and we are not denied access to the entry then this
  862. * is taken as implying access to the entry.
  863. */
  864. slapi_entry_first_attr ( e, &currAttr );
  865. if (currAttr != NULL) {
  866. slapi_attr_get_type ( currAttr , &attr_type );
  867. }
  868. #endif
  869. aclpb->aclpb_state |= ACLPB_EVALUATING_FIRST_ATTR;
  870. while (attr_type) {
  871. if (acl_access_allowed (pb, e,attr_type, NULL,
  872. SLAPI_ACL_READ) == LDAP_SUCCESS) {
  873. /*
  874. ** We found a rule which requires us to test access
  875. ** to the entry.
  876. */
  877. if ( aclpb->aclpb_state & ACLPB_FOUND_A_ENTRY_TEST_RULE){
  878. /* Do I have access on the entry itself */
  879. if (acl_access_allowed (pb, e, NULL,
  880. NULL, access) != LDAP_SUCCESS) {
  881. /* How was I denied ?
  882. ** I could be denied on a DENY rule or because
  883. ** there is no allow rule. If it's a DENY from
  884. ** a DENY rule, then we don't have access to
  885. ** the entry ( nice trick to get in )
  886. */
  887. if ( aclpb->aclpb_state &
  888. ACLPB_EXECUTING_DENY_HANDLES)
  889. return LDAP_INSUFFICIENT_ACCESS;
  890. /* The other case is I don't have an
  891. ** explicit allow rule -- which is fine.
  892. ** Since, I am already here, it means that I have
  893. ** an implicit allow to the entry.
  894. */
  895. }
  896. }
  897. aclpb->aclpb_state &= ~ACLPB_EVALUATING_FIRST_ATTR;
  898. /*
  899. ** If we are not sending all the attrs, then we must
  900. ** make sure that we have right on a attr that we are
  901. ** sending
  902. */
  903. len = strlen(attr_type);
  904. if ( len > ACLPB_MAX_ATTR_LEN) {
  905. slapi_ch_free ( (void **) &aclpb->aclpb_Evalattr);
  906. aclpb->aclpb_Evalattr = slapi_ch_malloc(len);
  907. }
  908. PL_strncpyz (aclpb->aclpb_Evalattr, attr_type, len);
  909. if ( attr_index >= 0 ) {
  910. /*
  911. * access was granted to one of the user specified attributes
  912. * which was found in the entry and that attribute is
  913. * now in aclpb_Evalattr
  914. */
  915. aclpb->aclpb_state |=
  916. ACLPB_ACCESS_ALLOWED_USERATTR;
  917. } else {
  918. /*
  919. * Access was granted to _an_ attribute in the entry and that
  920. * attribute is now in aclpb_Evalattr
  921. */
  922. aclpb->aclpb_state |=
  923. ACLPB_ACCESS_ALLOWED_ON_A_ATTR;
  924. }
  925. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_entry_end , "ACL","",
  926. tnf_string,called_access_allowed,"");
  927. return LDAP_SUCCESS;
  928. } else {
  929. /* try the next one */
  930. attr_type = NULL;
  931. if (attr_index >= 0) {
  932. attr_type = attrs[attr_index++];
  933. } else {
  934. rv = slapi_entry_next_attr ( e, currAttr, &nextAttr );
  935. if ( rv != 0 ) break;
  936. currAttr = nextAttr;
  937. slapi_attr_get_flags ( currAttr, &flags );
  938. while ( flags & SLAPI_ATTR_FLAG_OPATTR ) {
  939. flags = 0;
  940. rv = slapi_entry_next_attr ( e, currAttr, &nextAttr );
  941. if ( !rv ) slapi_attr_get_flags ( nextAttr, &flags );
  942. currAttr = nextAttr;
  943. }
  944. /* Get the attr type */
  945. if ( currAttr ) slapi_attr_get_type ( currAttr , &attr_type );
  946. }
  947. }
  948. }
  949. /*
  950. ** That means. we have searched thru all the attrs and found
  951. ** access is denied on all attrs.
  952. **
  953. ** If there were no attributes in the entry at all (can have
  954. ** such entries thrown up by the b/e, then we do
  955. ** not have such an implied access.
  956. */
  957. aclpb->aclpb_state |= ACLPB_ACCESS_DENIED_ON_ALL_ATTRS;
  958. aclpb->aclpb_state &= ~ACLPB_EVALUATING_FIRST_ATTR;
  959. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_entry_end ,"ACL","");
  960. return LDAP_INSUFFICIENT_ACCESS;
  961. }
  962. /***************************************************************************
  963. *
  964. * acl_read_access_allowed_on_attr
  965. * check access control on the given attr.
  966. *
  967. * Only used during search to test for read access to an attr.
  968. * (Could be generalized)
  969. *
  970. * Input:
  971. *
  972. *
  973. * Returns:
  974. * LDAP_SUCCESS - access allowed
  975. * LDAP_INSUFFICIENT_ACCESS - access denied
  976. *
  977. * Error Handling:
  978. * None.
  979. *
  980. **************************************************************************/
  981. int
  982. acl_read_access_allowed_on_attr (
  983. Slapi_PBlock *pb,
  984. Slapi_Entry *e, /* The Slapi_Entry */
  985. char *attr, /* Attribute of the entry */
  986. struct berval *val, /* value of attr. NOT USED */
  987. int access /* access rights */
  988. )
  989. {
  990. struct acl_pblock *aclpb = NULL;
  991. char ebuf [ BUFSIZ ];
  992. char *clientDn = NULL;
  993. char *n_edn;
  994. aclResultReason_t decision_reason;
  995. int ret_val = -1;
  996. int loglevel;
  997. loglevel = slapi_is_loglevel_set(SLAPI_LOG_ACL) ? SLAPI_LOG_ACL : SLAPI_LOG_ACLSUMMARY;
  998. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_attr_start ,"ACL","");
  999. decision_reason.deciding_aci = NULL;
  1000. decision_reason.reason = ACL_REASON_NONE;
  1001. /* I am here, because I have access to the entry */
  1002. n_edn = slapi_entry_get_ndn ( e );
  1003. /* If it's the root or acl is off or rootdse, he has all the priv */
  1004. if ( acl_skip_access_check ( pb, e ) ) {
  1005. char ebuf [ BUFSIZ ];
  1006. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1007. "Root access (%s) allowed on entry(%s)\n",
  1008. acl_access2str(access),
  1009. ACL_ESCAPE_STRING_WITH_PUNCTUATION (n_edn, ebuf), 0);
  1010. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1011. tnf_string,skip_aclcheck,"");
  1012. return LDAP_SUCCESS;
  1013. }
  1014. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  1015. if ( !aclpb ) {
  1016. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 3 \n" );
  1017. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1018. tnf_string,aclpb_error,"");
  1019. return LDAP_OPERATIONS_ERROR;
  1020. }
  1021. /*
  1022. * Am I a anonymous dude ? then we can use our anonympous profile
  1023. * We don't require the aclpb to have been initialized for anom stuff
  1024. *
  1025. */
  1026. slapi_pblock_get (pb, SLAPI_REQUESTOR_DN ,&clientDn );
  1027. if ( clientDn && *clientDn == '\0' ) {
  1028. ret_val = aclanom_match_profile ( pb, aclpb, e, attr,
  1029. SLAPI_ACL_READ );
  1030. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1031. tnf_string,anon_decision,"");
  1032. if (ret_val != -1 ) return ret_val;
  1033. }
  1034. /* Then I must have a access to the entry. */
  1035. aclpb->aclpb_state |= ACLPB_ACCESS_ALLOWED_ON_ENTRY;
  1036. if ( aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  1037. ret_val = acl__attr_cached_result (aclpb, attr, SLAPI_ACL_READ);
  1038. if (ret_val != -1 ) {
  1039. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1040. "MATCHED HANDLE:dn:%s attr: %s val:%d\n",
  1041. ACL_ESCAPE_STRING_WITH_PUNCTUATION (n_edn, ebuf), attr,
  1042. ret_val );
  1043. if ( ret_val == LDAP_SUCCESS) {
  1044. decision_reason.reason =
  1045. ACL_REASON_EVALCONTEXT_CACHED_ALLOW;
  1046. } else {
  1047. decision_reason.reason =
  1048. ACL_REASON_EVALCONTEXT_CACHED_NOT_ALLOWED;
  1049. }
  1050. goto acl_access_allowed_on_attr_Exit;
  1051. } else {
  1052. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  1053. }
  1054. }
  1055. if (aclpb->aclpb_state & ACLPB_ACCESS_DENIED_ON_ALL_ATTRS) {
  1056. /* access is denied on all the attributes */
  1057. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1058. tnf_string,deny_all_attrs,"");
  1059. return LDAP_INSUFFICIENT_ACCESS;
  1060. }
  1061. /* do I have access to all the entries by virtue of having aci
  1062. ** rules with targetattr ="*". If yes, then allow access to
  1063. ** rest of the attributes.
  1064. */
  1065. if (aclpb->aclpb_state & ACLPB_ATTR_STAR_MATCHED) {
  1066. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1067. "STAR Access allowed on attr:%s; entry:%s \n",
  1068. attr, ACL_ESCAPE_STRING_WITH_PUNCTUATION (n_edn, ebuf), 0);
  1069. decision_reason.reason =
  1070. ACL_REASON_EVALCONTEXT_CACHED_ATTR_STAR_ALLOW;
  1071. ret_val = LDAP_SUCCESS;
  1072. goto acl_access_allowed_on_attr_Exit;
  1073. }
  1074. if (aclpb->aclpb_state & ACLPB_ACCESS_ALLOWED_ON_A_ATTR) {
  1075. /* access is allowed on that attr.
  1076. ** for example: Slapi_Entry: cn, sn. phone, uid, passwd, address
  1077. ** We found that access is allowed on phone. That means the
  1078. ** -- access is denied on cn, sn
  1079. ** -- access is allowed on phone
  1080. ** -- Don't know about the rest. Need to evaluate.
  1081. */
  1082. if ( slapi_attr_type_cmp (attr, aclpb->aclpb_Evalattr, 1) == 0) {
  1083. /* from now on we need to evaluate access on
  1084. ** rest of the attrs.
  1085. */
  1086. aclpb->aclpb_state &= ~ACLPB_ACCESS_ALLOWED_ON_A_ATTR;
  1087. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1088. tnf_string,aclp_Evalattr1,"");
  1089. return LDAP_SUCCESS;
  1090. } else {
  1091. /*
  1092. * Here, the attr that implied access to the entry (aclpb_Evalattr),
  1093. * is not
  1094. * the one we currently want evaluated--so
  1095. * we need to evaluate access to attr--so fall through.
  1096. */
  1097. }
  1098. } else if (aclpb->aclpb_state & ACLPB_ACCESS_ALLOWED_USERATTR) {
  1099. /* Only skip evaluation on the user attr on which we have
  1100. ** evaluated before.
  1101. */
  1102. if ( slapi_attr_type_cmp (attr, aclpb->aclpb_Evalattr, 1) == 0) {
  1103. aclpb->aclpb_state &= ~ACLPB_ACCESS_ALLOWED_USERATTR;
  1104. TNF_PROBE_1_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","",
  1105. tnf_string,aclp_Evalattr2,"");
  1106. return LDAP_SUCCESS;
  1107. }
  1108. }
  1109. /* we need to evaluate the access on this attr */
  1110. return ( acl_access_allowed(pb, e, attr, val, access) );
  1111. /* This exit point prints a summary and returns ret_val */
  1112. acl_access_allowed_on_attr_Exit:
  1113. /* print summary if loglevel set */
  1114. if ( slapi_is_loglevel_set(loglevel) ) {
  1115. print_access_control_summary( "on attr",
  1116. ret_val, clientDn, aclpb,
  1117. acl_access2str(SLAPI_ACL_READ),
  1118. attr, n_edn, &decision_reason);
  1119. }
  1120. TNF_PROBE_0_DEBUG(acl_read_access_allowed_on_attr_end ,"ACL","");
  1121. return(ret_val);
  1122. }
  1123. /***************************************************************************
  1124. *
  1125. * acl_check_mods
  1126. * check access control on the given entry to see if
  1127. * it allows the given modifications by the user associated with op.
  1128. *
  1129. *
  1130. * Input:
  1131. *
  1132. *
  1133. * Returns:
  1134. * LDAP_SUCCESS - mods allowed ok
  1135. * <err> - same return value as acl_access_allowed()
  1136. *
  1137. * Error Handling:
  1138. * None.
  1139. *
  1140. **************************************************************************/
  1141. int
  1142. acl_check_mods(
  1143. Slapi_PBlock *pb,
  1144. Slapi_Entry *e,
  1145. LDAPMod **mods,
  1146. char **errbuf
  1147. )
  1148. {
  1149. int i;
  1150. int rv, accessCheckDisabled;
  1151. int lastmod = 0;
  1152. Slapi_Attr *attr = NULL;
  1153. char *n_edn;
  1154. Slapi_Backend *be = NULL;
  1155. Slapi_DN *e_sdn;
  1156. Acl_PBlock *aclpb = acl_get_aclpb ( pb, ACLPB_PROXYDN_PBLOCK );
  1157. LDAPMod *mod;
  1158. Slapi_Mods smods;
  1159. rv = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  1160. if ( rv != -1 && accessCheckDisabled ) return LDAP_SUCCESS;
  1161. if ( NULL == aclpb )
  1162. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  1163. n_edn = slapi_entry_get_ndn ( e );
  1164. e_sdn = slapi_entry_get_sdn ( e );
  1165. slapi_mods_init_byref(&smods,mods);
  1166. for (mod = slapi_mods_get_first_mod(&smods);
  1167. mod != NULL;
  1168. mod = slapi_mods_get_next_mod(&smods)) {
  1169. switch (mod->mod_op & ~LDAP_MOD_BVALUES ) {
  1170. case LDAP_MOD_DELETE:
  1171. if (mod->mod_bvalues != NULL ) {
  1172. break;
  1173. }
  1174. /*
  1175. * Here, check that we have the right to delete all
  1176. * the values of the attribute in the entry.
  1177. */
  1178. case LDAP_MOD_REPLACE:
  1179. if ( !lastmod ) {
  1180. if (be == NULL) {
  1181. if (slapi_pblock_get( pb, SLAPI_BACKEND, &be )) {
  1182. be = NULL;
  1183. }
  1184. }
  1185. if (be != NULL)
  1186. slapi_pblock_get ( pb, SLAPI_BE_LASTMOD, &lastmod );
  1187. }
  1188. if (lastmod &&
  1189. (strcmp (mod->mod_type, "modifiersname")== 0 ||
  1190. strcmp (mod->mod_type, "modifytimestamp")== 0)) {
  1191. continue;
  1192. }
  1193. slapi_entry_attr_find (e, mod->mod_type, &attr);
  1194. if ( attr != NULL) {
  1195. Slapi_Value *sval=NULL;
  1196. const struct berval *attrVal=NULL;
  1197. int k= slapi_attr_first_value(attr,&sval);
  1198. while(k != -1) {
  1199. attrVal = slapi_value_get_berval(sval);
  1200. rv = slapi_access_allowed (pb, e,
  1201. mod->mod_type,
  1202. (struct berval *)attrVal, /* XXXggood had to cast away const - BAD */
  1203. ACLPB_SLAPI_ACL_WRITE_DEL); /* was SLAPI_ACL_WRITE */
  1204. if ( rv != LDAP_SUCCESS) {
  1205. acl_gen_err_msg (
  1206. SLAPI_ACL_WRITE,
  1207. n_edn,
  1208. mod->mod_type,
  1209. errbuf);
  1210. /* Cleanup */
  1211. slapi_mods_done(&smods);
  1212. return(rv);
  1213. }
  1214. k= slapi_attr_next_value(attr, k, &sval);
  1215. }
  1216. }
  1217. else {
  1218. rv = slapi_access_allowed (pb, e,
  1219. mod->mod_type,
  1220. NULL,
  1221. ACLPB_SLAPI_ACL_WRITE_DEL); /* was SLAPI_ACL_WRITE */
  1222. if ( rv != LDAP_SUCCESS) {
  1223. acl_gen_err_msg (
  1224. SLAPI_ACL_WRITE,
  1225. n_edn,
  1226. mod->mod_type,
  1227. errbuf);
  1228. /* Cleanup */
  1229. slapi_mods_done(&smods);
  1230. return(rv);
  1231. }
  1232. }
  1233. break;
  1234. default:
  1235. break;
  1236. } /* switch */
  1237. /*
  1238. * Check that we have add/delete writes on the specific values
  1239. * we are trying to add.
  1240. */
  1241. if ( aclpb && aclpb->aclpb_curr_entry_sdn )
  1242. slapi_sdn_done ( aclpb->aclpb_curr_entry_sdn );
  1243. if ( mod->mod_bvalues != NULL ) {
  1244. /*
  1245. * Here, there are specific values specified.
  1246. * For add and replace--we need add rights for these values.
  1247. * For delete we need delete rights for these values.
  1248. */
  1249. for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
  1250. if ( ((mod->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_ADD) ||
  1251. ((mod->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_REPLACE)) {
  1252. rv = acl_access_allowed (pb,e,
  1253. mod->mod_type,
  1254. mod->mod_bvalues[i],
  1255. ACLPB_SLAPI_ACL_WRITE_ADD); /*was SLAPI_ACL_WRITE*/
  1256. } else if ((mod->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_DELETE) {
  1257. rv = acl_access_allowed (pb,e,
  1258. mod->mod_type,
  1259. mod->mod_bvalues[i],
  1260. ACLPB_SLAPI_ACL_WRITE_DEL); /*was SLAPI_ACL_WRITE*/
  1261. } else {
  1262. rv = LDAP_INSUFFICIENT_ACCESS;
  1263. }
  1264. if ( rv != LDAP_SUCCESS ) {
  1265. acl_gen_err_msg (
  1266. SLAPI_ACL_WRITE,
  1267. n_edn,
  1268. mod->mod_type,
  1269. errbuf);
  1270. /* Cleanup */
  1271. slapi_mods_done(&smods);
  1272. return rv;
  1273. }
  1274. /* Need to check for all the values because
  1275. ** we may be modifying a "self<right>" value.
  1276. */
  1277. /* Are we adding/replacing a aci attribute
  1278. ** value. In that case, we need to make
  1279. ** sure that the new value has thr right
  1280. ** syntax
  1281. */
  1282. if (strcmp(mod->mod_type,
  1283. aci_attr_type) == 0) {
  1284. if ( 0 != (rv = acl_verify_syntax( e_sdn,
  1285. mod->mod_bvalues[i]))) {
  1286. aclutil_print_err(rv, e_sdn,
  1287. mod->mod_bvalues[i],
  1288. errbuf);
  1289. /* Cleanup */
  1290. slapi_mods_done(&smods);
  1291. return LDAP_INVALID_SYNTAX;
  1292. }
  1293. }
  1294. } /* for */
  1295. }
  1296. } /* end of big for */
  1297. /* Cleanup */
  1298. slapi_mods_done(&smods);
  1299. return( LDAP_SUCCESS );
  1300. }
  1301. /***************************************************************************
  1302. *
  1303. * acl_modified
  1304. * Modifies ( removed, add, changes) the ACI LIST.
  1305. *
  1306. * Input:
  1307. * int *optype - op code
  1308. * char *dn - DN of the entry
  1309. * void *change - The change struct which contais the
  1310. * - change value
  1311. *
  1312. * Returns:
  1313. * None.
  1314. *
  1315. * Error Handling:
  1316. * None.
  1317. *
  1318. **************************************************************************/
  1319. extern void
  1320. acl_modified (Slapi_PBlock *pb, int optype, char *n_dn, void *change)
  1321. {
  1322. struct berval **bvalue;
  1323. char **value;
  1324. int rv=0; /* returned value */
  1325. char* new_RDN;
  1326. char* parent_DN;
  1327. char* new_DN;
  1328. LDAPMod **mods;
  1329. struct berval b;
  1330. int j;
  1331. Slapi_Attr *attr = NULL;
  1332. Slapi_Entry *e = NULL;
  1333. char ebuf [ BUFSIZ];
  1334. Slapi_DN *e_sdn;
  1335. aclUserGroup *ugroup = NULL;
  1336. e_sdn = slapi_sdn_new_ndn_byval ( n_dn );
  1337. /* Before we proceed, Let's first check if we are changing any groups.
  1338. ** If we are, then we need to change the signature
  1339. */
  1340. switch ( optype ) {
  1341. case SLAPI_OPERATION_MODIFY:
  1342. case SLAPI_OPERATION_DELETE:
  1343. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, (void*)&e);
  1344. break;
  1345. case SLAPI_OPERATION_ADD:
  1346. e = (Slapi_Entry *)change;
  1347. break;
  1348. }
  1349. /* e can be null for RDN */
  1350. if ( e ) slapi_entry_attr_find( e, "objectclass", &attr);
  1351. if ( attr ) {
  1352. int group_change = 0;
  1353. Slapi_Value *sval=NULL;
  1354. const struct berval *attrVal;
  1355. int i;
  1356. i= slapi_attr_first_value ( attr,&sval );
  1357. while(i != -1) {
  1358. attrVal = slapi_value_get_berval ( sval );
  1359. if ( (strcasecmp (attrVal->bv_val, "groupOfNames") == 0 ) ||
  1360. (strcasecmp (attrVal->bv_val, "groupOfUniqueNames") == 0 ) ||
  1361. (strcasecmp (attrVal->bv_val, "groupOfCertificates") == 0 ) ||
  1362. (strcasecmp (attrVal->bv_val, "groupOfURLs") == 0 ) ) {
  1363. group_change= 1;
  1364. if ( optype == SLAPI_OPERATION_MODIFY ) {
  1365. Slapi_Attr *a = NULL;
  1366. int rv;
  1367. rv = slapi_entry_attr_find ( e, "uniqueMember", &a);
  1368. if ( rv != 0 ) break;
  1369. rv = slapi_entry_attr_find ( e, "Member", &a );
  1370. if ( rv != 0 ) break;
  1371. rv = slapi_entry_attr_find ( e, "MemberURL", &a );
  1372. if ( rv != 0 ) break;
  1373. /* That means we are not changing the member
  1374. ** list, so it's okay to let go this
  1375. ** change
  1376. */
  1377. group_change = 0;
  1378. }
  1379. break;
  1380. }
  1381. i= slapi_attr_next_value ( attr, i, &sval );
  1382. }
  1383. /*
  1384. ** We can do better here XXX, i.e invalidate the cache for users who
  1385. ** use this group. for now just do the whole thing.
  1386. */
  1387. if ( group_change ) {
  1388. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1389. "Group Change: Invalidating entire UserGroup Cache\n",
  1390. ACL_ESCAPE_STRING_WITH_PUNCTUATION(n_dn, ebuf));
  1391. aclg_regen_group_signature();
  1392. if ( (optype == SLAPI_OPERATION_MODIFY) || (optype == SLAPI_OPERATION_DELETE ) ) {
  1393. /* Then we need to invalidate the acl signature also */
  1394. acl_signature = aclutil_gen_signature ( acl_signature );
  1395. }
  1396. }
  1397. }
  1398. /*
  1399. * Here if the target entry is in the group cache
  1400. * as a user then, as it's being changed it may move out of any dynamic
  1401. * groups it belongs to.
  1402. * Just remove it for now--can do better XXX by checking to see if
  1403. * it really needs to be removed by testing to see if he's
  1404. * still in th group after the change--but this requires keeping
  1405. * the memberURL of the group which we don't currently do.
  1406. * Also, if we keep
  1407. * the attributes that are being used in dynamic
  1408. * groups then we could only remove the user if a sensitive
  1409. * attribute was being modified (rather than scanning the whole user cache
  1410. * all the time). Also could do a hash lookup.
  1411. *
  1412. * aclg_find_userGroup() incs a refcnt so we can still refer to ugroup.
  1413. * aclg_markUgroupForRemoval() decs it and marks it for removal
  1414. * , so after that you cannot refer to ugroup.
  1415. *
  1416. */
  1417. if ( (ugroup = aclg_find_userGroup(n_dn)) != NULL) {
  1418. /*
  1419. * Mark this for deletion next time round--try to impact
  1420. * this mainline code as little as possible.
  1421. */
  1422. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1423. "Marking entry %s for removal from ACL user Group Cache\n",
  1424. ACL_ESCAPE_STRING_WITH_PUNCTUATION(n_dn, ebuf));
  1425. aclg_markUgroupForRemoval (ugroup);
  1426. }
  1427. /*
  1428. * Take the write lock around all the mods--so that
  1429. * other operations will see the acicache either before the whole mod
  1430. * or after but not, as it was before, during the mod.
  1431. * This is in line with the LDAP concept of the operation
  1432. * on the whole entry being the atomic unit.
  1433. *
  1434. */
  1435. switch(optype) {
  1436. case SLAPI_OPERATION_DELETE:
  1437. /* In this case we have already checked if the user has
  1438. ** right to delete the entry. Part of delete of entry is
  1439. ** remove all the ACLs also.
  1440. */
  1441. acllist_acicache_WRITE_LOCK();
  1442. rv = acllist_remove_aci_needsLock(e_sdn, NULL);
  1443. acllist_acicache_WRITE_UNLOCK();
  1444. break;
  1445. case SLAPI_OPERATION_ADD:
  1446. slapi_entry_attr_find ( (Slapi_Entry *) change, aci_attr_type, &attr );
  1447. if ( attr ) {
  1448. Slapi_Value *sval=NULL;
  1449. const struct berval *attrVal;
  1450. int i;
  1451. acllist_acicache_WRITE_LOCK();
  1452. i= slapi_attr_first_value ( attr,&sval );
  1453. while ( i != -1 ) {
  1454. attrVal = slapi_value_get_berval(sval);
  1455. rv= acllist_insert_aci_needsLock(e_sdn, attrVal );
  1456. if (rv <= ACL_ERR)
  1457. aclutil_print_err(rv, e_sdn, attrVal, NULL);
  1458. /* Print the aci list */
  1459. i= slapi_attr_next_value ( attr, i, &sval );
  1460. }
  1461. acllist_acicache_WRITE_UNLOCK();
  1462. }
  1463. break;
  1464. case SLAPI_OPERATION_MODIFY:
  1465. {
  1466. int got_write_lock = 0;
  1467. mods = (LDAPMod **) change;
  1468. for (j=0; mods[j] != NULL; j++) {
  1469. if (strcasecmp(mods[j]->mod_type, aci_attr_type) == 0) {
  1470. /* Got an aci to mod in this list of mods, so
  1471. * take the acicache lock for the whole list of mods,
  1472. * remembering to free it below.
  1473. */
  1474. if ( !got_write_lock) {
  1475. acllist_acicache_WRITE_LOCK();
  1476. got_write_lock = 1;
  1477. }
  1478. switch (mods[j]->mod_op & ~LDAP_MOD_BVALUES) {
  1479. case LDAP_MOD_REPLACE:
  1480. /* First remove the item */
  1481. rv = acllist_remove_aci_needsLock(e_sdn, NULL);
  1482. /* now fall thru to add the new one */
  1483. case LDAP_MOD_ADD:
  1484. /* Add the new aci */
  1485. if (mods[j]->mod_op & LDAP_MOD_BVALUES) {
  1486. bvalue = mods[j]->mod_bvalues;
  1487. if (bvalue == NULL)
  1488. break;
  1489. for (; *bvalue != NULL; ++bvalue) {
  1490. rv=acllist_insert_aci_needsLock( e_sdn, *bvalue);
  1491. if (rv <= ACL_ERR) {
  1492. aclutil_print_err(rv, e_sdn,
  1493. *bvalue, NULL);
  1494. }
  1495. }
  1496. } else {
  1497. value = mods[j]->mod_values;
  1498. if (value == NULL)
  1499. break;
  1500. for (; *value != NULL; ++value) {
  1501. b.bv_len = strlen (*value);
  1502. b.bv_val = *value;
  1503. rv=acllist_insert_aci_needsLock( e_sdn, &b);
  1504. if (rv <= ACL_ERR) {
  1505. aclutil_print_err(rv, e_sdn,
  1506. &b, NULL);
  1507. }
  1508. }
  1509. }
  1510. break;
  1511. case LDAP_MOD_DELETE:
  1512. if (mods[j]->mod_op & LDAP_MOD_BVALUES) {
  1513. bvalue = mods[j]->mod_bvalues;
  1514. if (bvalue == NULL || *bvalue == NULL) {
  1515. rv = acllist_remove_aci_needsLock( e_sdn, NULL);
  1516. } else {
  1517. for (; *bvalue != NULL; ++bvalue)
  1518. acllist_remove_aci_needsLock( e_sdn, *bvalue);
  1519. }
  1520. } else {
  1521. value = mods[j]->mod_values;
  1522. if (value == NULL || *value == NULL) {
  1523. acllist_remove_aci_needsLock( e_sdn,NULL);
  1524. } else {
  1525. for (; *value != NULL; ++value) {
  1526. b.bv_len = strlen (*value);
  1527. b.bv_val = *value;
  1528. acllist_remove_aci_needsLock( e_sdn, &b);
  1529. }
  1530. }
  1531. }
  1532. break;
  1533. default:
  1534. break;
  1535. }/* modtype switch */
  1536. }/* attrtype is aci */
  1537. } /* end of for */
  1538. if ( got_write_lock ) {
  1539. acllist_acicache_WRITE_UNLOCK();
  1540. got_write_lock = 0;
  1541. }
  1542. break;
  1543. }/* case op is modify*/
  1544. case SLAPI_OPERATION_MODRDN:
  1545. new_RDN = (char*) change;
  1546. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1547. "acl_modified (MODRDN %s => \"%s\"\n",
  1548. ACL_ESCAPE_STRING_WITH_PUNCTUATION (n_dn, ebuf), new_RDN, 0);
  1549. /* compute new_DN: */
  1550. parent_DN = slapi_dn_parent (n_dn);
  1551. if (parent_DN == NULL) {
  1552. new_DN = new_RDN;
  1553. } else {
  1554. new_DN = slapi_ch_smprintf("%s,%s", new_RDN, parent_DN);
  1555. slapi_dn_normalize (new_DN);
  1556. }
  1557. /* Change the acls */
  1558. acllist_acicache_WRITE_LOCK();
  1559. acllist_moddn_aci_needsLock ( e_sdn, new_DN );
  1560. acllist_acicache_WRITE_UNLOCK();
  1561. /* deallocat the parent_DN */
  1562. if (parent_DN != NULL) {
  1563. slapi_ch_free ( (void **) &new_DN );
  1564. slapi_ch_free ( (void **) &parent_DN );
  1565. }
  1566. break;
  1567. default:
  1568. /* print ERROR */
  1569. break;
  1570. } /*optype switch */
  1571. slapi_sdn_free ( &e_sdn );
  1572. }
  1573. /***************************************************************************
  1574. *
  1575. * acl__scan_for_acis
  1576. * Scan the list and picup the correct acls for evaluation.
  1577. *
  1578. * Input:
  1579. * Acl_PBlock *aclpb - Main ACL pblock
  1580. * int *err; - Any error status
  1581. * Returns:
  1582. * num_handles - Number of handles matched to the
  1583. * - resource + 1.
  1584. * Error Handling:
  1585. * None.
  1586. *
  1587. **************************************************************************/
  1588. static int
  1589. acl__scan_for_acis(Acl_PBlock *aclpb, int *err)
  1590. {
  1591. aci_t *aci;
  1592. NSErr_t errp;
  1593. int attr_matched;
  1594. int deny_handle;
  1595. int allow_handle;
  1596. int gen_allow_handle = ACI_MAX_ELEVEL+1;
  1597. int gen_deny_handle = ACI_MAX_ELEVEL+1;
  1598. int i;
  1599. PRUint32 cookie;
  1600. TNF_PROBE_0_DEBUG(acl__scan_for_acis_start,"ACL","");
  1601. /*
  1602. ** Determine if we are traversing via the list Vs. we have our own
  1603. ** generated list
  1604. */
  1605. if ( aclpb->aclpb_state & ACLPB_SEARCH_BASED_ON_LIST ||
  1606. aclpb->aclpb_handles_index[0] != -1 ) {
  1607. int kk = 0;
  1608. while ( kk < ACLPB_MAX_SELECTED_ACLS && aclpb->aclpb_handles_index[kk] != -1 ) {
  1609. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Using ACL Cointainer:%d for evaluation\n", kk);
  1610. kk++;
  1611. }
  1612. }
  1613. memset (&errp, 0, sizeof(NSErr_t));
  1614. *err = ACL_FALSE;
  1615. aclpb->aclpb_num_deny_handles = -1;
  1616. aclpb->aclpb_num_allow_handles = -1;
  1617. for (i=0; i <= ACI_MAX_ELEVEL; i++) {
  1618. aclpb->aclpb_deny_handles [i] = NULL;
  1619. aclpb->aclpb_allow_handles [i] = NULL;
  1620. }
  1621. /* Check the signature. If it has changed, start fresh */
  1622. if ( aclpb->aclpb_signature != acl_signature ) {
  1623. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  1624. "Restart the scan -- due to acl changes\n");
  1625. acllist_init_scan ( aclpb->aclpb_pblock, LDAP_SCOPE_BASE, NULL );
  1626. }
  1627. attr_matched = ACL_FALSE;
  1628. deny_handle = 0;
  1629. allow_handle = 0;
  1630. i = 0;
  1631. aclpb->aclpb_stat_acllist_scanned++;
  1632. aci = acllist_get_first_aci ( aclpb, &cookie );
  1633. while( aci ) {
  1634. if (acl__resource_match_aci(aclpb, aci, 0, &attr_matched)) {
  1635. /* Generate the ACL list handle */
  1636. if (aci->aci_handle == NULL) {
  1637. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  1638. continue;
  1639. }
  1640. aclutil_print_aci (aci, acl_access2str (aclpb->aclpb_access));
  1641. if (aci->aci_type & ACI_HAS_DENY_RULE) {
  1642. if (aclpb->aclpb_deny_handles[aci->aci_elevel] == NULL ) {
  1643. aclpb->aclpb_deny_handles[aci->aci_elevel] = aci;
  1644. } else {
  1645. if ((gen_deny_handle + ACI_DEFAULT_ELEVEL + 1) ==
  1646. aclpb->aclpb_deny_handles_size ) {
  1647. int num = ACLPB_INCR_LIST_HANDLES +
  1648. aclpb->aclpb_deny_handles_size;
  1649. /* allocate more space */
  1650. aclpb->aclpb_deny_handles =
  1651. (aci_t **)
  1652. slapi_ch_realloc (
  1653. (void *) aclpb->aclpb_deny_handles,
  1654. num * sizeof (aci_t *));
  1655. aclpb->aclpb_deny_handles_size = num;
  1656. }
  1657. aclpb->aclpb_deny_handles [gen_deny_handle] = aci;
  1658. gen_deny_handle++;
  1659. }
  1660. deny_handle++;
  1661. }
  1662. /*
  1663. ** It's possible that a single acl is in both the camps i.e
  1664. ** It has a allow and a deny rule
  1665. ** In that case we keep the same acl in both the camps.
  1666. */
  1667. if (aci->aci_type & ACI_HAS_ALLOW_RULE) {
  1668. if (aclpb->aclpb_allow_handles[aci->aci_elevel] == NULL ) {
  1669. aclpb->aclpb_allow_handles[aci->aci_elevel] = aci;
  1670. } else {
  1671. if ((gen_allow_handle + ACI_DEFAULT_ELEVEL + 1) ==
  1672. aclpb->aclpb_allow_handles_size) {
  1673. /* allocate more space */
  1674. int num = ACLPB_INCR_LIST_HANDLES +
  1675. aclpb->aclpb_allow_handles_size;
  1676. aclpb->aclpb_allow_handles =
  1677. (aci_t **)
  1678. slapi_ch_realloc (
  1679. (void *) aclpb->aclpb_allow_handles,
  1680. num * sizeof (aci_t *));
  1681. aclpb->aclpb_allow_handles_size = num;
  1682. }
  1683. aclpb->aclpb_allow_handles [gen_allow_handle] = aci;
  1684. gen_allow_handle++;
  1685. }
  1686. allow_handle++;
  1687. }
  1688. }
  1689. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  1690. } /* end of while */
  1691. /* make the last one a null */
  1692. aclpb->aclpb_deny_handles [gen_deny_handle] = NULL;
  1693. aclpb->aclpb_allow_handles [gen_allow_handle] = NULL;
  1694. /* specify how many we found */
  1695. aclpb->aclpb_num_deny_handles = deny_handle;
  1696. aclpb->aclpb_num_allow_handles = allow_handle;
  1697. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Num of ALLOW Handles:%d, DENY handles:%d\n",
  1698. aclpb->aclpb_num_allow_handles, aclpb->aclpb_num_deny_handles, 0);
  1699. TNF_PROBE_0_DEBUG(acl__scan_for_acis_end,"ACL","");
  1700. return(allow_handle + deny_handle);
  1701. }
  1702. /***************************************************************************
  1703. *
  1704. * acl__resource_match_aci
  1705. *
  1706. * This compares the ACI for the given resource and determines if
  1707. * the ACL applies to the resource or not.
  1708. *
  1709. * For read/search operation, we collect all the possible acls which
  1710. * will apply, We will be using this list for future acl evaluation
  1711. * for that entry.
  1712. *
  1713. * Input:
  1714. * struct acl_pblock *aclpb - Main acl private block
  1715. * aci_t *aci - The ACI item
  1716. * int skip_attrEval - DOn't check for attrs
  1717. * int *a_matched - Attribute matched
  1718. *
  1719. * Returns:
  1720. *
  1721. * ACL_TRUE - Yep, This ACL is applicable to the
  1722. * - the resource.
  1723. * ACL_FALSE - No it is not.
  1724. *
  1725. * Error Handling:
  1726. * None.
  1727. *
  1728. **************************************************************************/
  1729. #define ACL_RIGHTS_TARGETATTR_NOT_NEEDED ( SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_PROXY)
  1730. static int
  1731. acl__resource_match_aci( Acl_PBlock *aclpb, aci_t *aci, int skip_attrEval, int *a_matched)
  1732. {
  1733. struct slapi_filter *f; /* filter */
  1734. int rv; /* return value */
  1735. int matches;
  1736. int attr_matched;
  1737. int attr_matched_in_targetattrfilters = 0;
  1738. int dn_matched;
  1739. char *res_attr;
  1740. int aci_right = 0;
  1741. int res_right = 0;
  1742. int star_matched = ACL_FALSE;
  1743. int num_attrs = 0;
  1744. AclAttrEval *c_attrEval = NULL;
  1745. const char *res_ndn = NULL;
  1746. const char *aci_ndn = NULL;
  1747. char *matched_val = NULL;
  1748. int add_matched_val_to_ht = 0;
  1749. char res_right_str[128];
  1750. TNF_PROBE_0_DEBUG(acl__resource_match_aci_start,"ACL","");
  1751. aclpb->aclpb_stat_aclres_matched++;
  1752. /* Assume that resource matches */
  1753. matches = ACL_TRUE;
  1754. /* Figure out if the acl has the correct rights or not */
  1755. aci_right = aci->aci_access;
  1756. res_right = aclpb->aclpb_access;
  1757. if (!(aci_right & res_right)) {
  1758. /* If we are looking for read/search and the acl has read/search
  1759. ** then go further because if targets match we may keep that
  1760. ** acl in the entry cache list.
  1761. */
  1762. if (!((res_right & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) &&
  1763. (aci_right & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ))))
  1764. matches = ACL_FALSE;
  1765. goto acl__resource_match_aci_EXIT;
  1766. }
  1767. /* first Let's see if the entry is under the subtree where the
  1768. ** ACL resides. We can't let somebody affect a target beyond the
  1769. ** scope of where the ACL resides
  1770. ** Example: ACL is located in "ou=engineering, o=ace industry, c=us
  1771. ** but if the target is "o=ace industry, c=us", then we are in trouble.
  1772. **
  1773. ** If the aci is in the rootdse and the entry is not, then we do not
  1774. ** match--ie. acis in the rootdse do NOT apply below...for the moment.
  1775. **
  1776. */
  1777. res_ndn = slapi_sdn_get_ndn ( aclpb->aclpb_curr_entry_sdn );
  1778. aci_ndn = slapi_sdn_get_ndn ( aci->aci_sdn );
  1779. if (!slapi_sdn_issuffix(aclpb->aclpb_curr_entry_sdn, aci->aci_sdn)
  1780. || (!slapi_is_rootdse(res_ndn) && slapi_is_rootdse(aci_ndn)) ) {
  1781. /* cant' poke around */
  1782. matches = ACL_FALSE;
  1783. goto acl__resource_match_aci_EXIT;
  1784. }
  1785. /*
  1786. ** We have a single ACI which we need to find if it applies to
  1787. ** the resource or not.
  1788. */
  1789. if ((aci->aci_type & ACI_TARGET_DN) &&
  1790. (aclpb->aclpb_curr_entry_sdn)) {
  1791. char *avaType;
  1792. struct berval *avaValue;
  1793. f = aci->target;
  1794. dn_matched = ACL_TRUE;
  1795. slapi_filter_get_ava ( f, &avaType, &avaValue );
  1796. if (!slapi_dn_issuffix( res_ndn, avaValue->bv_val)) {
  1797. dn_matched = ACL_FALSE;
  1798. }
  1799. if (aci->aci_type & ACI_TARGET_NOT) {
  1800. matches = (dn_matched ? ACL_FALSE : ACL_TRUE);
  1801. } else {
  1802. matches = (dn_matched ? ACL_TRUE: ACL_FALSE);
  1803. }
  1804. }
  1805. /* No need to look further */
  1806. if (matches == ACL_FALSE) {
  1807. goto acl__resource_match_aci_EXIT;
  1808. }
  1809. if (aci->aci_type & ACI_TARGET_PATTERN) {
  1810. f = aci->target;
  1811. dn_matched = ACL_TRUE;
  1812. if ((rv = acl_match_substring(f, (char *)res_ndn, 0 /* match suffux */)) != ACL_TRUE) {
  1813. dn_matched = ACL_FALSE;
  1814. if(rv == ACL_ERR) {
  1815. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1816. "acl__resource_match_aci:pattern err\n",
  1817. 0,0,0);
  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",0,0,0);
  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",0,0,0);
  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",0,0,0);
  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",0,0,0);
  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. 0,0,0);
  2470. continue;
  2471. }
  2472. /*
  2473. ** Now we have all the information we need. We need to call
  2474. ** the ONE ACL to test the rights.
  2475. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2476. */
  2477. aclpb->aclpb_curr_aci = aci;
  2478. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2479. map_generic, &deny,
  2480. &deny_generic,
  2481. &acl_tag, &expr_num);
  2482. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Processed:%d DENY handles Result:%d\n",index, rights_rv,0);
  2483. if (rights_rv == ACL_RES_FAIL) {
  2484. result_reason->deciding_aci = aci;
  2485. result_reason->reason = ACL_REASON_NONE;
  2486. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2487. tnf_string,evaled_deny,"");
  2488. return ACL_RES_DENY;
  2489. }
  2490. /* have we executed an ATTR RULE */
  2491. if ( aci->aci_ruleType & ACI_ATTR_RULES )
  2492. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2493. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2494. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2495. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2496. break;
  2497. }
  2498. }
  2499. if ( j < aclpb->aclpb_last_cache_result) {
  2500. /* already in cache */
  2501. } else if ( j < ACLPB_MAX_CACHE_RESULTS ) {
  2502. /* j == aclpb->aclpb_last_cache_result &&
  2503. j < ACLPB_MAX_CACHE_RESULTS */
  2504. aclpb->aclpb_last_cache_result++;
  2505. aclpb->aclpb_cache_result[j].aci_index = index;
  2506. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2507. } else { /* cache overflow */
  2508. if ( rights_rv == ACL_RES_DENY) {
  2509. result_reason->deciding_aci = aci;
  2510. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2511. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2512. tnf_string,evaled_deny,"");
  2513. return ACL_RES_DENY;
  2514. } else {
  2515. continue;
  2516. }
  2517. }
  2518. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2519. if (rights_rv == ACL_RES_DENY) {
  2520. if (access & SLAPI_ACL_SEARCH) {
  2521. aclpb->aclpb_cache_result[j].result |=
  2522. ACLPB_CACHE_SEARCH_RES_DENY;
  2523. } else { /* MUST BE READ */
  2524. aclpb->aclpb_cache_result[j].result |=
  2525. ACLPB_CACHE_READ_RES_DENY;
  2526. }
  2527. /* We are done -- return */
  2528. result_reason->deciding_aci = aci;
  2529. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2530. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2531. tnf_string,evaled_deny,"");
  2532. return ACL_RES_DENY;
  2533. } else if (rights_rv == ACL_RES_ALLOW) {
  2534. /* This will happen, of we have an acl with both deny and allow
  2535. ** Since we may not have finished all the deny acl, go thru all
  2536. ** of them. We will use this cached result when we evaluate this
  2537. ** handle in the context of allow handles.
  2538. */
  2539. if (access & SLAPI_ACL_SEARCH) {
  2540. aclpb->aclpb_cache_result[j].result |=
  2541. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2542. } else {
  2543. aclpb->aclpb_cache_result[j].result |=
  2544. ACLPB_CACHE_READ_RES_ALLOW;
  2545. }
  2546. } else {
  2547. if (access & SLAPI_ACL_SEARCH) {
  2548. aclpb->aclpb_cache_result[j].result |=
  2549. ACLPB_CACHE_SEARCH_RES_SKIP;
  2550. } else {
  2551. aclpb->aclpb_cache_result[j].result |=
  2552. ACLPB_CACHE_READ_RES_SKIP;
  2553. }
  2554. continue;
  2555. }
  2556. } else {
  2557. if ( rights_rv == ACL_RES_DENY ) {
  2558. result_reason->deciding_aci = aci;
  2559. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2560. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2561. tnf_string,evaled_deny,"");
  2562. return ACL_RES_DENY;
  2563. }
  2564. }
  2565. }
  2566. /*
  2567. ** START PROCESSING ALLOW HANDLES.
  2568. ** Process each handle at a time. Do not concatenate the handles or else
  2569. ** all the context information will be build again and we will pay a
  2570. ** lot of penalty. The context is built the first time the handle is
  2571. ** processed.
  2572. **
  2573. ** First we set the default to INVALID so that if rules are not matched, then
  2574. ** we get INVALID and if a rule matched, the we get ALLOW.
  2575. */
  2576. aclpb->aclpb_state &= ~ACLPB_EXECUTING_DENY_HANDLES;
  2577. aclpb->aclpb_state |= ACLPB_EXECUTING_ALLOW_HANDLES;
  2578. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2579. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_allow_handles;
  2580. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_allow_handles ; ++i) {
  2581. int skip_eval = 0;
  2582. /*
  2583. ** If the handle has been evaluated before, we can
  2584. ** cache the result.
  2585. */
  2586. aci = aclpb->aclpb_allow_handles[i];
  2587. if (((aci = aclpb->aclpb_allow_handles[i]) == NULL) && (i <= ACI_MAX_ELEVEL))
  2588. continue;
  2589. k++;
  2590. index = aci->aci_index;
  2591. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2592. "%d. Evaluating ALLOW aci(%d) \"%s\"\n", k, index, aci->aclName);
  2593. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2594. /*
  2595. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2596. * a cache of info about whether applicable acis
  2597. * allowed, did_not_allow or denied access
  2598. */
  2599. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2600. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2601. short result;
  2602. result = aclpb->aclpb_cache_result[j].result;
  2603. if ( result <= 0) break;
  2604. if (!ACL_CACHED_RESULT_VALID(result)) {
  2605. /* something is wrong. Need to evaluate */
  2606. aclpb->aclpb_cache_result[j].result = -1;
  2607. break;
  2608. }
  2609. /*
  2610. ** We have a valid cached result. Let's see if we
  2611. ** have what we need.
  2612. */
  2613. if (access & SLAPI_ACL_SEARCH) {
  2614. if (result & ACLPB_CACHE_SEARCH_RES_ALLOW) {
  2615. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2616. "Found SEARCH ALLOW in cache\n");
  2617. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2618. result_reason->deciding_aci = aci;
  2619. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2620. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2621. tnf_string,cached_allow,"");
  2622. return ACL_RES_ALLOW;
  2623. } else if ( result & ACLPB_CACHE_SEARCH_RES_SKIP) {
  2624. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2625. "Found SEARCH SKIP in cache\n",0,0,0);
  2626. skip_eval = 1;
  2627. break;
  2628. } else {
  2629. /* need to evaluate */
  2630. break;
  2631. }
  2632. } else {
  2633. if ( result & ACLPB_CACHE_READ_RES_ALLOW) {
  2634. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2635. "Found READ ALLOW in cache\n",0,0,0);
  2636. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2637. result_reason->deciding_aci = aci;
  2638. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2639. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2640. tnf_string,cached_allow,"");
  2641. return ACL_RES_ALLOW;
  2642. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2643. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2644. "Found READ SKIP in cache\n",0,0,0);
  2645. skip_eval = 1;
  2646. break;
  2647. } else {
  2648. break;
  2649. }
  2650. }
  2651. }
  2652. }
  2653. }
  2654. if ( skip_eval) {
  2655. skip_eval = 0;
  2656. continue;
  2657. }
  2658. TNF_PROBE_0_DEBUG(acl__libaccess_start,"ACL","");
  2659. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2660. if ( rv < 0) {
  2661. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  2662. "acl__TestRights:Unable to set the acllist\n",
  2663. 0,0,0);
  2664. continue;
  2665. }
  2666. /*
  2667. ** Now we have all the information we need. We need to call
  2668. ** the ONE ACL to test the rights.
  2669. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2670. */
  2671. aclpb->aclpb_curr_aci = aci;
  2672. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2673. map_generic, &deny,
  2674. &deny_generic,
  2675. &acl_tag, &expr_num);
  2676. TNF_PROBE_0_DEBUG(acl__libaccess_end,"ACL","");
  2677. if (aci->aci_ruleType & ACI_ATTR_RULES)
  2678. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2679. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2680. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2681. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2682. break;
  2683. }
  2684. }
  2685. if ( j < aclpb->aclpb_last_cache_result) {
  2686. /* already in cache */
  2687. } else if ( j < ACLPB_MAX_CACHE_RESULTS ) {
  2688. /* j == aclpb->aclpb_last_cache_result &&
  2689. j < ACLPB_MAX_CACHE_RESULTS */
  2690. aclpb->aclpb_last_cache_result++;
  2691. aclpb->aclpb_cache_result[j].aci_index = index;
  2692. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2693. } else { /* cache overflow */
  2694. slapi_log_error (SLAPI_LOG_FATAL, "acl__TestRights", "cache overflown\n");
  2695. if ( rights_rv == ACL_RES_ALLOW) {
  2696. result_reason->deciding_aci = aci;
  2697. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2698. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2699. tnf_string,evaled_allow,"");
  2700. return ACL_RES_ALLOW;
  2701. } else {
  2702. continue;
  2703. }
  2704. }
  2705. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2706. if (rights_rv == ACL_RES_ALLOW) {
  2707. if (access & SLAPI_ACL_SEARCH) {
  2708. aclpb->aclpb_cache_result[j].result |=
  2709. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2710. } else { /* must be READ */
  2711. aclpb->aclpb_cache_result[j].result |=
  2712. ACLPB_CACHE_READ_RES_ALLOW;
  2713. }
  2714. /* We are done -- return */
  2715. result_reason->deciding_aci = aci;
  2716. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2717. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2718. tnf_string,evaled_allow,"");
  2719. return ACL_RES_ALLOW;
  2720. } else {
  2721. if (access & SLAPI_ACL_SEARCH) {
  2722. aclpb->aclpb_cache_result[j].result |=
  2723. ACLPB_CACHE_SEARCH_RES_SKIP;
  2724. } else {
  2725. aclpb->aclpb_cache_result[j].result |=
  2726. ACLPB_CACHE_READ_RES_SKIP;
  2727. }
  2728. continue;
  2729. }
  2730. } else {
  2731. if ( rights_rv == ACL_RES_ALLOW ) {
  2732. result_reason->deciding_aci = aci;
  2733. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2734. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2735. tnf_string,evaled_allow,"");
  2736. return ACL_RES_ALLOW;
  2737. }
  2738. }
  2739. }/* for */
  2740. result_reason->deciding_aci = aci;
  2741. result_reason->reason = ACL_REASON_NO_MATCHED_SUBJECT_ALLOWS;
  2742. TNF_PROBE_0_DEBUG(acl__TestRights_end,"ACL","");
  2743. return (ACL_RES_DENY);
  2744. }
  2745. /***************************************************************************
  2746. *
  2747. * acl_match_substring
  2748. *
  2749. * Compare the input string to the patteren in the filter
  2750. *
  2751. * Input:
  2752. * struct slapi_filter *f - Filter which has the patteren
  2753. * char *str - String to compare
  2754. * int exact_match - 1; match the pattern exactly
  2755. * - 0; match the pattern as a suffix
  2756. *
  2757. * Returns:
  2758. * ACL_TRUE - The sting matches with the patteren
  2759. * ACL_FALSE - No it doesn't
  2760. * ACL_ERR - Error
  2761. *
  2762. * Error Handling:
  2763. * None.
  2764. *
  2765. **************************************************************************/
  2766. int
  2767. acl_match_substring ( Slapi_Filter *f, char *str, int exact_match)
  2768. {
  2769. int i, rc, len;
  2770. char *p;
  2771. char *end, *realval, *tmp;
  2772. char pat[BUFSIZ];
  2773. char buf[BUFSIZ];
  2774. char *type, *initial, *final;
  2775. char **any;
  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", 0, 0, 0);
  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", 0, 0, 0);
  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", 0, 0, 0);
  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. slapd_re_lock();
  2846. if ((p = slapd_re_comp (pat)) != 0) {
  2847. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2848. "acl_match_substring:re_comp failed (%s)\n", p, 0, 0);
  2849. slapd_re_unlock();
  2850. return (ACL_ERR);
  2851. }
  2852. /* re_exec() returns 1 if the string p1 matches the last compiled
  2853. ** regular expression, 0 if the string p1 failed to match
  2854. ** (see man pages)
  2855. **
  2856. ** IMPORTANT NOTE: If I use compile() and step() to do the patteren
  2857. ** matching, it seems that step() is leaking 1036 bytes/search
  2858. ** I couldn't figure out why it's leaking.
  2859. */
  2860. rc = slapd_re_exec( realval, -1 /* no timelimit */ );
  2861. slapd_re_unlock();
  2862. if (tmp != NULL) {
  2863. slapi_ch_free ( (void **) &tmp );
  2864. }
  2865. if (rc == 1) {
  2866. return ACL_TRUE;
  2867. } else {
  2868. return ACL_FALSE;
  2869. }
  2870. }
  2871. /***************************************************************************
  2872. *
  2873. * acl__reset_cached_result
  2874. *
  2875. * Go thru the cached result and invlalidate the cached evaluation results for
  2876. * rules which can only be cached based on the scope.
  2877. * If we have scope ACI_CACHE_RESULT_PER_ENTRY, then we need to invalidate the
  2878. * cacched reult whenever we hit a new entry.
  2879. *
  2880. * Returns:
  2881. * Null
  2882. *
  2883. **************************************************************************/
  2884. static void
  2885. acl__reset_cached_result (struct acl_pblock *aclpb )
  2886. {
  2887. int j;
  2888. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2889. /* Unless we have to clear the result, move on */
  2890. if (!( aclpb->aclpb_cache_result[j].aci_ruleType & ACI_CACHE_RESULT_PER_ENTRY))
  2891. continue;
  2892. aclpb->aclpb_cache_result[j].result = 0;
  2893. }
  2894. }
  2895. /*
  2896. * acl_access_allowed_disjoint_resource
  2897. *
  2898. * This is an internal module which can be used to verify
  2899. * access to a resource which may not be inside the search scope.
  2900. *
  2901. * Returns:
  2902. * - Same return val as acl_access_allowed().
  2903. */
  2904. int
  2905. acl_access_allowed_disjoint_resource(
  2906. Slapi_PBlock *pb,
  2907. Slapi_Entry *e, /* The Slapi_Entry */
  2908. char *attr, /* Attribute of the entry */
  2909. struct berval *val, /* value of attr. NOT USED */
  2910. int access /* access rights */
  2911. )
  2912. {
  2913. int rv;
  2914. struct acl_pblock *aclpb = NULL;
  2915. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  2916. /*
  2917. ** It's possible that we already have a context of ACLs.
  2918. ** However once in a while, we need to test
  2919. ** access to a resource which (like vlv, schema) which falls
  2920. ** outside the search base. In that case, we need to go
  2921. ** thru all the ACLs and not depend upon the acls which we have
  2922. ** gathered.
  2923. */
  2924. /* If you have the right to use the resource, then we don't need to check for
  2925. ** proxy right on that resource.
  2926. */
  2927. if (aclpb)
  2928. aclpb->aclpb_state |= ( ACLPB_DONOT_USE_CONTEXT_ACLS| ACLPB_DONOT_EVALUATE_PROXY );
  2929. rv = acl_access_allowed(pb, e, attr, val, access);
  2930. if (aclpb) aclpb->aclpb_state &= ~ACLPB_DONOT_USE_CONTEXT_ACLS;
  2931. if (aclpb ) aclpb->aclpb_state &= ~ACLPB_DONOT_EVALUATE_PROXY;
  2932. return rv;
  2933. }
  2934. /*
  2935. * acl__attr_cached_result
  2936. * Loops thru the cached result and determines if we can use the cached value.
  2937. *
  2938. * Inputs:
  2939. * Slapi_pblock *aclpb - acl private block
  2940. * char *attr - attribute name
  2941. * int access - access type
  2942. * Returns:
  2943. * LDAP_SUCCESS: - access is granted
  2944. * LDAP_INSUFFICIENT_ACCESS - access denied
  2945. * ACL_ERR - no cached info about this attr.
  2946. * - or the attr had multiple result and so
  2947. * - we can't determine the outcome.
  2948. *
  2949. */
  2950. static int
  2951. acl__attr_cached_result (struct acl_pblock *aclpb, char *attr, int access )
  2952. {
  2953. int i, rc;
  2954. aclEvalContext *c_evalContext;
  2955. if ( !(access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ) ))
  2956. return ACL_ERR;
  2957. if (aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  2958. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  2959. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  2960. "acl__attr_cached_result:Using Context: ACLPB_ACLCB\n", 0,0,0 );
  2961. } else {
  2962. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  2963. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  2964. "acl__attr_cached_result:Using Context: ACLPB_PREV\n", 0,0,0 );
  2965. }
  2966. if ( attr == NULL ) {
  2967. int eval_read = 0;
  2968. /*
  2969. ** Do I have access to at least one attribute, then I have
  2970. ** access to the entry.
  2971. */
  2972. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  2973. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  2974. if ( (access & SLAPI_ACL_READ ) && a_eval->attrEval_r_status &&
  2975. a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  2976. eval_read++;
  2977. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  2978. return LDAP_SUCCESS;
  2979. /* rbyrne: recompute if we have to.
  2980. * How does this cached result get turned off for
  2981. * attr style acis which acannot be cached becuase entry
  2982. * can result in a diff value.
  2983. */
  2984. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  2985. rc = acl__recompute_acl ( aclpb, a_eval, access,
  2986. a_eval->attrEval_r_aciIndex);
  2987. if ( rc != ACL_ERR ) {
  2988. acl_copyEval_context ( aclpb, c_evalContext,
  2989. &aclpb->aclpb_curr_entryEval_context, 1);
  2990. }
  2991. if ( rc == LDAP_SUCCESS) {
  2992. return LDAP_SUCCESS;
  2993. }
  2994. }
  2995. }
  2996. }/* for */
  2997. /*
  2998. * If we have scanned the whole list without success then
  2999. * we are not granting access to this entry through access
  3000. * to an attribute in the list--however this does not mean
  3001. * that we do not have access to the entry via another attribute
  3002. * not already in the list, so return -1 meaning
  3003. * "don't know".
  3004. */
  3005. return(ACL_ERR);
  3006. #if 0
  3007. if ( eval_read )
  3008. return LDAP_INSUFFICIENT_ACCESS;
  3009. else
  3010. return ACL_ERR;
  3011. #endif
  3012. }
  3013. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3014. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3015. if ( a_eval == NULL ) continue;
  3016. if (strcasecmp ( attr, a_eval->attrEval_name ) == 0 ) {
  3017. if ( access & SLAPI_ACL_SEARCH ) {
  3018. if (a_eval->attrEval_s_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3019. if ( a_eval->attrEval_s_status & ACL_ATTREVAL_SUCCESS)
  3020. return LDAP_SUCCESS;
  3021. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_FAIL)
  3022. return LDAP_INSUFFICIENT_ACCESS;
  3023. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_RECOMPUTE ) {
  3024. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3025. a_eval->attrEval_s_aciIndex);
  3026. if ( rc != ACL_ERR ) {
  3027. acl_copyEval_context ( aclpb, c_evalContext,
  3028. &aclpb->aclpb_curr_entryEval_context, 1);
  3029. }
  3030. } else
  3031. return ACL_ERR;
  3032. } else {
  3033. /* This means that for the same attribute and same type of
  3034. ** access, we had different results at different time.
  3035. ** Since we are not caching per object, we can't
  3036. ** determine exactly. So, can't touch this
  3037. */
  3038. return ACL_ERR;
  3039. }
  3040. } else {
  3041. if (a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3042. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3043. return LDAP_SUCCESS;
  3044. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_FAIL)
  3045. return LDAP_INSUFFICIENT_ACCESS;
  3046. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3047. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3048. a_eval->attrEval_r_aciIndex);
  3049. if ( rc != ACL_ERR ) {
  3050. acl_copyEval_context ( aclpb, c_evalContext,
  3051. &aclpb->aclpb_curr_entryEval_context, 1);
  3052. }
  3053. } else
  3054. return ACL_ERR;
  3055. } else {
  3056. /* Look above for explanation */
  3057. return ACL_ERR;
  3058. }
  3059. }
  3060. }
  3061. }
  3062. return ACL_ERR;
  3063. }
  3064. /*
  3065. * Had to do this juggling of casting to make
  3066. * both Nt & unix compiler happy.
  3067. */
  3068. static int
  3069. acl__cmp(const void *a, const void *b)
  3070. {
  3071. short *i = (short *) a;
  3072. short *j = (short *) b;
  3073. if ( (short) *i > (short) *j )
  3074. return (1);
  3075. if ( (short)*i < (short) *j)
  3076. return (-1);
  3077. return (0);
  3078. }
  3079. /*
  3080. * acl__scan_match_handles
  3081. * Go thru the ACL list and determine if the list of acls selected matches
  3082. * what we have in the cache.
  3083. *
  3084. * Inputs:
  3085. * Acl_PBlock *pb - Main pblock ( blacvk hole)
  3086. * int type - Which context to look on
  3087. *
  3088. * Returns:
  3089. * 0 - matches all the acl handles
  3090. * ACL_ERR - sorry; no match
  3091. *
  3092. * ASSUMPTION: A READER LOCK ON ACL LIST
  3093. */
  3094. static int
  3095. acl__scan_match_handles ( Acl_PBlock *aclpb, int type)
  3096. {
  3097. int matched = 0;
  3098. aci_t *aci = NULL;
  3099. int index;
  3100. PRUint32 cookie;
  3101. aclEvalContext *c_evalContext = NULL;
  3102. if (type == ACLPB_EVALCONTEXT_PREV ) {
  3103. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3104. } else if ( type == ACLPB_EVALCONTEXT_ACLCB ){
  3105. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3106. } else {
  3107. return ACL_ERR;
  3108. }
  3109. if ( !c_evalContext->acle_numof_tmatched_handles )
  3110. return ACL_ERR;
  3111. aclpb->aclpb_stat_acllist_scanned++;
  3112. aci = acllist_get_first_aci ( aclpb, &cookie );
  3113. while ( aci ) {
  3114. index = aci->aci_index;
  3115. if (acl__resource_match_aci(aclpb, aci, 1 /* skip attr matching */, NULL )) {
  3116. int j;
  3117. int s_matched = matched;
  3118. /* We have a sorted list of handles that matched the target */
  3119. for (j=0; j < c_evalContext->acle_numof_tmatched_handles ; j++ ) {
  3120. if ( c_evalContext->acle_handles_matched_target[j] > index )
  3121. break;
  3122. else if ( index == c_evalContext->acle_handles_matched_target[j] ) {
  3123. int jj;
  3124. matched++;
  3125. /* See if this is a ATTR rule that matched -- in that case we have
  3126. ** to nullify the cached result
  3127. */
  3128. if ( aci->aci_ruleType & ACI_ATTR_RULES ) {
  3129. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3130. "Found an attr Rule [Name:%s Index:%d\n", aci->aclName,
  3131. aci->aci_index );
  3132. for ( jj =0; jj < c_evalContext->acle_numof_attrs; jj++ ) {
  3133. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[jj];
  3134. if ( a_eval->attrEval_r_aciIndex == aci->aci_index )
  3135. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3136. if ( a_eval->attrEval_s_aciIndex == aci->aci_index )
  3137. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3138. }
  3139. }
  3140. break;
  3141. }
  3142. }
  3143. if ( s_matched == matched ) return ACL_ERR;
  3144. }
  3145. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3146. }
  3147. if ( matched == c_evalContext->acle_numof_tmatched_handles )
  3148. return 0;
  3149. return ACL_ERR;
  3150. }
  3151. /*
  3152. * acl_copyEval_context
  3153. * Copy the context info which include attr info and handles.
  3154. *
  3155. * Inputs
  3156. * struct acl_pblock *aclpb - acl private main block
  3157. * aclEvalContext *src - src context
  3158. * aclEvalContext *dest - dest context
  3159. * Returns:
  3160. * None.
  3161. *
  3162. */
  3163. void
  3164. acl_copyEval_context ( struct acl_pblock *aclpb, aclEvalContext *src,
  3165. aclEvalContext *dest , int copy_attr_only )
  3166. {
  3167. int d_slot, i;
  3168. /* Do a CLEAN copy we have nothing or else do an incremental copy.*/
  3169. if ( src->acle_numof_attrs < 1 )
  3170. return;
  3171. /* Copy the attr info */
  3172. if ( dest->acle_numof_attrs < 1 )
  3173. acl_clean_aclEval_context ( dest, 0 /*clean */ );
  3174. d_slot = dest->acle_numof_attrs;
  3175. for (i=0; i < src->acle_numof_attrs; i++ ) {
  3176. int j;
  3177. int attr_exists = 0;
  3178. int dd_slot = d_slot;
  3179. if ( aclpb && (i == 0) ) aclpb->aclpb_stat_num_copycontext++;
  3180. if ( src->acle_attrEval[i].attrEval_r_status == 0 &&
  3181. src->acle_attrEval[i].attrEval_s_status == 0 )
  3182. continue;
  3183. for ( j = 0; j < dest->acle_numof_attrs; j++ ) {
  3184. if ( strcasecmp ( src->acle_attrEval[i].attrEval_name,
  3185. dest->acle_attrEval[j].attrEval_name ) == 0 ) {
  3186. /* We have it. skip it. */
  3187. attr_exists = 1;
  3188. dd_slot = j;
  3189. break;
  3190. }
  3191. }
  3192. if ( !attr_exists ) {
  3193. if ( dd_slot >= ACLPB_MAX_ATTRS -1 )
  3194. break;
  3195. if ( aclpb) aclpb->aclpb_stat_num_copy_attrs++;
  3196. if ( dest->acle_attrEval[dd_slot].attrEval_name )
  3197. slapi_ch_free ( (void **) &dest->acle_attrEval[dd_slot].attrEval_name );
  3198. dest->acle_attrEval[dd_slot].attrEval_name =
  3199. slapi_ch_strdup ( src->acle_attrEval[i].attrEval_name );
  3200. }
  3201. /* Copy the result status and the aci index */
  3202. dest->acle_attrEval[dd_slot].attrEval_r_status =
  3203. src->acle_attrEval[i].attrEval_r_status;
  3204. dest->acle_attrEval[dd_slot].attrEval_r_aciIndex =
  3205. src->acle_attrEval[i].attrEval_r_aciIndex;
  3206. dest->acle_attrEval[dd_slot].attrEval_s_status =
  3207. src->acle_attrEval[i].attrEval_s_status;
  3208. dest->acle_attrEval[dd_slot].attrEval_s_aciIndex =
  3209. src->acle_attrEval[i].attrEval_s_aciIndex;
  3210. if (!attr_exists ) d_slot++;
  3211. }
  3212. dest->acle_numof_attrs = d_slot;
  3213. dest->acle_attrEval[d_slot].attrEval_name = NULL;
  3214. if ( copy_attr_only )
  3215. return;
  3216. /* First sort the arrays which keeps the acl index numbers */
  3217. qsort ( (char *) src->acle_handles_matched_target,
  3218. (size_t)src->acle_numof_tmatched_handles, sizeof( int ), acl__cmp );
  3219. for (i=0; i < src->acle_numof_tmatched_handles; i++ ) {
  3220. dest->acle_handles_matched_target[i] =
  3221. src->acle_handles_matched_target[i];
  3222. }
  3223. if ( src->acle_numof_tmatched_handles ) {
  3224. dest->acle_numof_tmatched_handles = src->acle_numof_tmatched_handles;
  3225. if ( aclpb) aclpb->aclpb_stat_num_tmatched_acls = src->acle_numof_tmatched_handles;
  3226. }
  3227. }
  3228. /*
  3229. * acl_clean_aclEval_context
  3230. * Clean the eval context
  3231. *
  3232. * Inputs:
  3233. * aclEvalContext *clean_me - COntext to be cleaned
  3234. * int clean_type - 0: clean, 1 scrub
  3235. *
  3236. */
  3237. void
  3238. acl_clean_aclEval_context ( aclEvalContext *clean_me, int scrub_only )
  3239. {
  3240. int i;
  3241. /* Copy the attr info */
  3242. for (i=0; i < clean_me->acle_numof_attrs; i++ ) {
  3243. char *a_name = clean_me->acle_attrEval[i].attrEval_name;
  3244. if ( a_name && !scrub_only) {
  3245. slapi_ch_free ( (void **) &a_name );
  3246. clean_me->acle_attrEval[i].attrEval_name = NULL;
  3247. }
  3248. clean_me->acle_attrEval[i].attrEval_r_status = 0;
  3249. clean_me->acle_attrEval[i].attrEval_s_status = 0;
  3250. clean_me->acle_attrEval[i].attrEval_r_aciIndex = 0;
  3251. clean_me->acle_attrEval[i].attrEval_s_aciIndex = 0;
  3252. }
  3253. if ( !scrub_only ) clean_me->acle_numof_attrs = 0;
  3254. clean_me->acle_numof_tmatched_handles = 0;
  3255. }
  3256. /*
  3257. * acl__match_handlesFromCache
  3258. *
  3259. * We have 2 cacheed information
  3260. * 1) cached info from the previous operation
  3261. * 2) cached info from the prev entry evaluation
  3262. *
  3263. * What we are doing here is going thru all the acls and see if the same
  3264. * set of acls apply to this resource or not. If it does, then do we have
  3265. * a cached info for the attr. If we don't for both of the cases then we need
  3266. * to evaluate all over again.
  3267. *
  3268. * Inputs:
  3269. * struct acl_pblock - ACL private block;
  3270. * char *attr - Attribute name
  3271. * int access - acces type
  3272. *
  3273. * returns:
  3274. * LDAP_SUCCESS (0) - The same acls apply and we have
  3275. * access ALLOWED on the attr
  3276. * LDAP_INSUFFICIENT_ACCESS - The same acls apply and we have
  3277. * access DENIED on the attr
  3278. * -1 - Acls doesn't match or we don't have
  3279. * cached info for this attr.
  3280. *
  3281. * ASSUMPTIONS: A reader lock has been obtained for the acl list.
  3282. */
  3283. static int
  3284. acl__match_handlesFromCache ( Acl_PBlock *aclpb, char *attr, int access)
  3285. {
  3286. aclEvalContext *c_evalContext = NULL;
  3287. int context_type = 0;
  3288. int ret_val = -1; /* it doen't match by default */
  3289. /* Before we proceed, find out if we have evaluated any ATTR RULE. If we have
  3290. ** then we can't use any caching mechanism
  3291. */
  3292. if ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3293. context_type = ACLPB_EVALCONTEXT_ACLCB;
  3294. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3295. } else {
  3296. context_type = ACLPB_EVALCONTEXT_PREV;
  3297. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3298. }
  3299. if ( aclpb->aclpb_res_type & (ACLPB_NEW_ENTRY | ACLPB_EFFECTIVE_RIGHTS) ) {
  3300. aclpb->aclpb_state |= ACLPB_MATCHES_ALL_ACLS;
  3301. ret_val = acl__scan_match_handles ( aclpb, context_type );
  3302. if ( -1 == ret_val ) {
  3303. aclpb->aclpb_state &= ~ACLPB_MATCHES_ALL_ACLS;
  3304. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  3305. /* Did not match */
  3306. if ( context_type == ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3307. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT;
  3308. } else {
  3309. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  3310. c_evalContext->acle_numof_tmatched_handles = 0;
  3311. }
  3312. }
  3313. }
  3314. if ( aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  3315. /* See if we have a cached result for this attr */
  3316. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3317. /* It's not in the ACLCB context but we might have it in the
  3318. ** current/prev context. Take a look at it. we might have evaluated
  3319. ** this attribute already.
  3320. */
  3321. if ( (-1 == ret_val ) &&
  3322. ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT )) {
  3323. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3324. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3325. aclpb->aclpb_state |= ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3326. /* We need to do an incremental update */
  3327. if ( !ret_val ) aclpb->aclpb_state |= ACLPB_INCR_ACLCB_CACHE;
  3328. }
  3329. }
  3330. return ret_val;
  3331. }
  3332. /*
  3333. * acl__get_attrEval
  3334. * Get the atteval from the current context and hold the ptr in aclpb.
  3335. * If we have too many attrs, then allocate a new one. In that case
  3336. * we let the caller know about that so that it will be deallocated.
  3337. *
  3338. * Returns:
  3339. * int - 0: The context was indexed. So, no allocations.
  3340. * - 1; context was allocated - deallocate it.
  3341. */
  3342. static int
  3343. acl__get_attrEval ( struct acl_pblock *aclpb, char *attr )
  3344. {
  3345. int j;
  3346. aclEvalContext *c_ContextEval = &aclpb->aclpb_curr_entryEval_context;
  3347. int deallocate_attrEval = 0;
  3348. AclAttrEval *c_attrEval = NULL;
  3349. if ( !attr ) return deallocate_attrEval;
  3350. aclpb->aclpb_curr_attrEval = NULL;
  3351. /* Go thru and see if we have the attr already */
  3352. for (j=0; j < c_ContextEval->acle_numof_attrs; j++) {
  3353. c_attrEval = &c_ContextEval->acle_attrEval[j];
  3354. if ( c_attrEval &&
  3355. slapi_attr_type_cmp ( c_attrEval->attrEval_name, attr, 1) == 0 ) {
  3356. aclpb->aclpb_curr_attrEval = c_attrEval;
  3357. break;
  3358. }
  3359. }
  3360. if ( !aclpb->aclpb_curr_attrEval) {
  3361. if ( c_ContextEval->acle_numof_attrs == ACLPB_MAX_ATTRS -1 ) {
  3362. /* Too many attrs. create a temp one */
  3363. c_attrEval = (AclAttrEval * ) slapi_ch_calloc ( 1, sizeof ( AclAttrEval ) );
  3364. deallocate_attrEval =1;
  3365. } else {
  3366. c_attrEval = &c_ContextEval->acle_attrEval[c_ContextEval->acle_numof_attrs++];
  3367. c_attrEval->attrEval_r_status = 0;
  3368. c_attrEval->attrEval_s_status = 0;
  3369. c_attrEval->attrEval_r_aciIndex = 0;
  3370. c_attrEval->attrEval_s_aciIndex = 0;
  3371. }
  3372. /* clean it before use */
  3373. c_attrEval->attrEval_name = slapi_ch_strdup ( attr );
  3374. aclpb->aclpb_curr_attrEval = c_attrEval;
  3375. }
  3376. return deallocate_attrEval;
  3377. }
  3378. /*
  3379. * acl_skip_access_check
  3380. *
  3381. * See if we need to go thru the ACL check or not. We don't need to if I am root
  3382. * or internal operation or ...
  3383. *
  3384. * returns:
  3385. * ACL_TRUE - Yes; skip the ACL check
  3386. * ACL_FALSE - No; you have to go thru ACL check
  3387. *
  3388. */
  3389. int
  3390. acl_skip_access_check ( Slapi_PBlock *pb, Slapi_Entry *e )
  3391. {
  3392. int rv, isRoot, accessCheckDisabled;
  3393. void *conn = NULL;
  3394. Slapi_Backend *be;
  3395. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  3396. if ( isRoot ) return ACL_TRUE;
  3397. /* See if this is local request */
  3398. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn);
  3399. if ( NULL == conn ) return ACL_TRUE;
  3400. /*
  3401. * Turn on access checking in the rootdse--this code used
  3402. * to skip the access check.
  3403. *
  3404. * check if the entry is the RootDSE entry
  3405. if ( e ) {
  3406. char * edn = slapi_entry_get_ndn ( e );
  3407. if ( slapi_is_rootdse ( edn ) ) return ACL_TRUE;
  3408. }
  3409. */
  3410. /* GB : when referrals are directly set in the mappin tree
  3411. * we can reach this code without a backend in the pblock
  3412. * in such a case, allow access for now
  3413. * we may want to reconsider this is NULL DSE implementation happens
  3414. */
  3415. rv = slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  3416. if (be == NULL)
  3417. return ACL_TRUE;
  3418. rv = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  3419. if ( rv != -1 && accessCheckDisabled ) return ACL_TRUE;
  3420. return ACL_FALSE;
  3421. }
  3422. short
  3423. acl_get_aclsignature ()
  3424. {
  3425. return acl_signature;
  3426. }
  3427. void
  3428. acl_set_aclsignature ( short value)
  3429. {
  3430. acl_signature = value;
  3431. }
  3432. void
  3433. acl_regen_aclsignature ()
  3434. {
  3435. acl_signature = aclutil_gen_signature ( acl_signature );
  3436. }
  3437. /*
  3438. *
  3439. * Assumptions:
  3440. * 1) Called for read/search right.
  3441. */
  3442. static int
  3443. acl__recompute_acl ( Acl_PBlock *aclpb,
  3444. AclAttrEval *a_eval,
  3445. int access,
  3446. int aciIndex
  3447. )
  3448. {
  3449. char *unused_str1, *unused_str2;
  3450. char *acl_tag, *testRight[2];
  3451. int j, expr_num;
  3452. int result_status, cache_result;
  3453. PRUint32 cookie;
  3454. aci_t *aci;
  3455. PR_ASSERT ( aciIndex >= 0 );
  3456. PR_ASSERT ( a_eval != NULL );
  3457. PR_ASSERT (aclpb != NULL );
  3458. /* We might have evaluated this acl just now, check it there first */
  3459. for ( j =0; j < aclpb->aclpb_last_cache_result; j++) {
  3460. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3461. short result;
  3462. result_status =ACL_RES_INVALID;
  3463. result = aclpb->aclpb_cache_result[j].result;
  3464. if ( result <= 0) break;
  3465. if (!ACL_CACHED_RESULT_VALID(result)) {
  3466. /* something is wrong. Need to evaluate */
  3467. aclpb->aclpb_cache_result[j].result = -1;
  3468. break;
  3469. }
  3470. /*
  3471. ** We have a valid cached result. Let's see if we
  3472. ** have what we need.
  3473. */
  3474. if ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) ||
  3475. (result & ACLPB_CACHE_READ_RES_ALLOW) )
  3476. result_status = ACL_RES_ALLOW;
  3477. else if ((result & ACLPB_CACHE_SEARCH_RES_DENY) ||
  3478. (result & ACLPB_CACHE_READ_RES_DENY) )
  3479. result_status = ACL_RES_DENY;
  3480. }
  3481. } /* end of for */
  3482. if ( result_status != ACL_RES_INVALID ) {
  3483. goto set_result_status;
  3484. }
  3485. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3486. "Recomputing the ACL Index:%d for entry:%s\n",
  3487. aciIndex, slapi_entry_get_ndn ( aclpb->aclpb_curr_entry) );
  3488. /* First find this one ACL and then evaluate it. */
  3489. aci = acllist_get_first_aci ( aclpb, &cookie );
  3490. while ( aci && aci->aci_index != aciIndex ) {
  3491. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3492. }
  3493. if (NULL == aci)
  3494. return -1;
  3495. ACL_SetDefaultResult (NULL, aclpb->aclpb_acleval, ACL_RES_INVALID);
  3496. ACL_EvalSetACL(NULL, aclpb->aclpb_acleval, aci->aci_handle);
  3497. testRight[0] = acl_access2str ( access );
  3498. testRight[1] = '\0';
  3499. aclpb->aclpb_curr_aci = aci;
  3500. result_status = ACL_EvalTestRights (NULL, aclpb->aclpb_acleval, testRight,
  3501. ds_map_generic, &unused_str1,
  3502. &unused_str2,
  3503. &acl_tag, &expr_num);
  3504. cache_result = 0;
  3505. if ( result_status == ACL_RES_DENY && aci->aci_type & ACI_HAS_DENY_RULE ) {
  3506. if ( access & SLAPI_ACL_SEARCH)
  3507. cache_result = ACLPB_CACHE_SEARCH_RES_DENY;
  3508. else
  3509. cache_result = ACLPB_CACHE_READ_RES_DENY;
  3510. } else if ( result_status == ACL_RES_ALLOW && aci->aci_type & ACI_HAS_ALLOW_RULE ) {
  3511. if ( access & SLAPI_ACL_SEARCH)
  3512. cache_result = ACLPB_CACHE_SEARCH_RES_ALLOW;
  3513. else
  3514. cache_result = ACLPB_CACHE_READ_RES_ALLOW;
  3515. } else {
  3516. result_status = -1;
  3517. }
  3518. /* Now we need to put the cached result in the aclpb */
  3519. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  3520. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3521. break;
  3522. }
  3523. }
  3524. if ( j < aclpb->aclpb_last_cache_result) {
  3525. /* already in cache */
  3526. } else if ( j < ACLPB_MAX_CACHE_RESULTS-1) {
  3527. /* rbyrneXXX: make this same as other last_cache_result code! */
  3528. j = ++aclpb->aclpb_last_cache_result;
  3529. aclpb->aclpb_cache_result[j].aci_index = aci->aci_index;
  3530. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  3531. } else { /* No more space */
  3532. goto set_result_status;
  3533. }
  3534. /* Add the cached result status */
  3535. aclpb->aclpb_cache_result[j].result |= cache_result;
  3536. set_result_status:
  3537. if (result_status == ACL_RES_ALLOW) {
  3538. if (access & SLAPI_ACL_SEARCH)
  3539. /*wrong bit maskes were being used here--
  3540. a_eval->attrEval_s_status = ACLPB_CACHE_SEARCH_RES_ALLOW;*/
  3541. a_eval->attrEval_s_status = ACL_ATTREVAL_SUCCESS;
  3542. else
  3543. a_eval->attrEval_r_status = ACL_ATTREVAL_SUCCESS;
  3544. } else if ( result_status == ACL_RES_DENY) {
  3545. if (access & SLAPI_ACL_SEARCH)
  3546. a_eval->attrEval_s_status = ACL_ATTREVAL_FAIL;
  3547. else
  3548. a_eval->attrEval_r_status = ACL_ATTREVAL_FAIL;
  3549. } else {
  3550. /* Here, set it to recompute--try again later */
  3551. if (access & SLAPI_ACL_SEARCH)
  3552. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3553. else
  3554. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3555. result_status = -1;
  3556. }
  3557. return result_status;
  3558. }
  3559. static void
  3560. __acl_set_aclIndex_inResult ( Acl_PBlock *aclpb, int access, int index )
  3561. {
  3562. AclAttrEval *c_attrEval = aclpb->aclpb_curr_attrEval;
  3563. if ( c_attrEval ) {
  3564. if ( access & SLAPI_ACL_SEARCH )
  3565. c_attrEval->attrEval_s_aciIndex = index;
  3566. else if ( access & SLAPI_ACL_READ )
  3567. c_attrEval->attrEval_r_aciIndex = index;
  3568. }
  3569. }
  3570. /*
  3571. * If filter_sense is true then return (entry satisfies f).
  3572. * Otherwise, return !(entry satisfies f)
  3573. */
  3574. static int
  3575. acl__test_filter ( Slapi_Entry *entry, struct slapi_filter *f, int filter_sense) {
  3576. int filter_matched;
  3577. /* slapi_vattr_filter_test() returns 0 for match */
  3578. filter_matched = !slapi_vattr_filter_test(NULL, entry,
  3579. f,
  3580. 0 /*don't do acess chk*/);
  3581. if (filter_sense) {
  3582. return(filter_matched ? ACL_TRUE : ACL_FALSE);
  3583. } else {
  3584. return(filter_matched ? ACL_FALSE: ACL_TRUE);
  3585. }
  3586. }
  3587. /*
  3588. * Make an entry consisting of attr_type and attr_val and put
  3589. * a pointer to it in *entry.
  3590. * We will use this afterwards to test for against a filter.
  3591. */
  3592. static int
  3593. acl__make_filter_test_entry ( Slapi_Entry **entry, char *attr_type,
  3594. struct berval *attr_val) {
  3595. struct berval *vals_array[2];
  3596. vals_array[0] = attr_val;
  3597. vals_array[1] = NULL;
  3598. *entry = slapi_entry_alloc();
  3599. slapi_entry_init(*entry, NULL, NULL);
  3600. return (slapi_entry_add_values( *entry, (const char *)attr_type,
  3601. (struct berval**)&vals_array[0] ));
  3602. }
  3603. /*********************************************************************************/
  3604. /* E N D */
  3605. /*********************************************************************************/