acl.c 120 KB

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