acl.c 136 KB

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