acl.c 122 KB

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