acl.c 136 KB

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