acl.c 126 KB

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