acl.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211
  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. /* We need to handle modrdn operation. Modrdn doesn't
  2318. ** change any attrs but changes the RDN and so (attr=NULL).
  2319. ** Here we found an acl which has a targetattr but
  2320. ** the resource doesn't need one. In that case, we should
  2321. ** consider this acl.
  2322. ** default: matches = ACL_TRUE;
  2323. */
  2324. ;
  2325. } else {
  2326. matches = ACL_FALSE;
  2327. }
  2328. }
  2329. }/* !attr_matched_in_targetattrfilters */
  2330. /*
  2331. ** Here we are testing if we find a entry test rule (which should
  2332. ** be rare). In that case, just remember it. An entry test rule
  2333. ** doesn't have "(targetattr)".
  2334. */
  2335. if ((aclpb->aclpb_state & ACLPB_EVALUATING_FIRST_ATTR) &&
  2336. (!(aci->aci_type & ACI_TARGET_ATTR))) {
  2337. aclpb->aclpb_state |= ACLPB_FOUND_A_ENTRY_TEST_RULE;
  2338. }
  2339. /*
  2340. * Generic exit point for this routine:
  2341. * matches is ACL_TRUE if the aci matches the target of the resource,
  2342. * ACL_FALSE othrewise.
  2343. * Apologies for the goto--this is a retro-fitted exit point.
  2344. */
  2345. acl__resource_match_aci_EXIT:
  2346. /*
  2347. * For macro acis, there may be a partial macro string
  2348. * placed in the aclpb_macro_ht
  2349. * even if the aci did not finally match.
  2350. * All the partial strings will be freed at aclpb
  2351. * cleanup time.
  2352. */
  2353. if (ACL_TRUE == matches) {
  2354. aclpb->aclpb_stat_aclres_matched++;
  2355. }
  2356. TNF_PROBE_0_DEBUG(acl__resource_match_aci_end,"ACL","");
  2357. return (matches);
  2358. }
  2359. /* Macro to determine if the cached result is valid or not. */
  2360. #define ACL_CACHED_RESULT_VALID( result) \
  2361. (((result & ACLPB_CACHE_READ_RES_ALLOW) && \
  2362. (result & ACLPB_CACHE_READ_RES_SKIP)) || \
  2363. ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) && \
  2364. (result & ACLPB_CACHE_SEARCH_RES_SKIP))) ? 0 : 1
  2365. /***************************************************************************
  2366. *
  2367. * acl__TestRights
  2368. *
  2369. * Test the rights and find out if access is allowed or not.
  2370. *
  2371. * Processing Alogorithm:
  2372. *
  2373. * First, process the DENY rules one by one. If the user is not explicitly
  2374. * denied, then check if the user is allowed by processing the ALLOW handles
  2375. * one by one.
  2376. * The result of the evaluation is cached. Exceptions are
  2377. * -- If an acl happens to be in both DENY and ALLOW camp.
  2378. * -- Only interested for READ/SEARCH right.
  2379. *
  2380. * Input:
  2381. * struct acl_pblock *aclpb - main acl private block
  2382. * int access - The access bits
  2383. * char **right - The right we are looking for
  2384. * char ** generic - Generic rights
  2385. *
  2386. * Returns:
  2387. *
  2388. * ACL_RES_ALLOW - Access allowed
  2389. * ACL_RES_DENY - Access denied
  2390. * err - error condition
  2391. *
  2392. * Error Handling:
  2393. * None.
  2394. *
  2395. **************************************************************************/
  2396. static int
  2397. acl__TestRights(Acl_PBlock *aclpb,int access, char **right, char ** map_generic,
  2398. aclResultReason_t *result_reason)
  2399. {
  2400. ACLEvalHandle_t *acleval;
  2401. int rights_rv = ACL_RES_DENY;
  2402. int rv, i,j, k;
  2403. int index;
  2404. char *deny = NULL;
  2405. char *deny_generic = NULL;
  2406. char *acl_tag;
  2407. int expr_num;
  2408. char *testRights[2];
  2409. aci_t *aci = NULL;
  2410. int numHandles = 0;
  2411. TNF_PROBE_0_DEBUG(acl__TestRights_start,"ACL","");
  2412. /* record the aci and reason for access decision */
  2413. result_reason->deciding_aci = NULL;
  2414. result_reason->reason = ACL_REASON_NONE;
  2415. /* If we don't have any ALLLOW handles, it's DENY by default */
  2416. if (aclpb->aclpb_num_allow_handles <= 0) {
  2417. result_reason->deciding_aci = NULL;
  2418. result_reason->reason = ACL_REASON_NO_MATCHED_RESOURCE_ALLOWS;
  2419. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2420. tnf_string,no_allows,"");
  2421. return ACL_RES_DENY;
  2422. }
  2423. /* Get the ACL evaluation Context */
  2424. acleval = aclpb->aclpb_acleval;
  2425. testRights[0] = *right;
  2426. testRights[1] = '\0';
  2427. /*
  2428. ** START PROCESSING DENY HANDLES
  2429. ** Process each handle at a time. Do not concatenate the handles or else
  2430. ** all the context information will be build again and we will pay a
  2431. ** lot of penalty. The context is built the first time the handle is
  2432. ** processed.
  2433. **
  2434. ** First we set the default to INVALID so that if rules are not matched, then
  2435. ** we get INVALID and if a rule matched, the we get DENY.
  2436. */
  2437. aclpb->aclpb_state &= ~ACLPB_EXECUTING_ALLOW_HANDLES;
  2438. aclpb->aclpb_state |= ACLPB_EXECUTING_DENY_HANDLES;
  2439. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2440. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_deny_handles;
  2441. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_deny_handles ; ++i) {
  2442. int skip_eval = 0;
  2443. /*
  2444. ** If the handle has been evaluated before, we can
  2445. ** cache the result.
  2446. */
  2447. if ((aci = aclpb->aclpb_deny_handles[i]) == NULL) {
  2448. if (i <= ACI_MAX_ELEVEL) {
  2449. continue;
  2450. } else {
  2451. break;
  2452. }
  2453. }
  2454. k++;
  2455. index = aci->aci_index;
  2456. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2457. "Evaluating DENY aci(%d) \"%s\"\n", index, aci->aclName);
  2458. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2459. /* We can not reused results obtained on a other entry */
  2460. if (aci->aci_type & ACI_CACHE_RESULT_PER_ENTRY) {
  2461. aclpb->aclpb_state |= ACLPB_CACHE_RESULT_PER_ENTRY_SKIP;
  2462. }
  2463. /*
  2464. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2465. * a cache of info about whether applicable acis
  2466. * allowed, did_not_allow or denied access
  2467. */
  2468. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2469. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2470. short result;
  2471. result = aclpb->aclpb_cache_result[j].result;
  2472. if ( result <= 0) break;
  2473. if (!ACL_CACHED_RESULT_VALID(result)) {
  2474. /* something is wrong. Need to evaluate */
  2475. aclpb->aclpb_cache_result[j].result = -1;
  2476. break;
  2477. }
  2478. /*
  2479. ** We have a valid cached result. Let's see if we
  2480. ** have what we need.
  2481. */
  2482. if (access & SLAPI_ACL_SEARCH) {
  2483. if ( result & ACLPB_CACHE_SEARCH_RES_DENY){
  2484. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2485. "DENY:Found SEARCH DENY in cache\n");
  2486. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2487. result_reason->deciding_aci = aci;
  2488. result_reason->reason = ACL_REASON_RESULT_CACHED_DENY;
  2489. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2490. tnf_string,cached_deny,"");
  2491. return ACL_RES_DENY;
  2492. } else if ((result & ACLPB_CACHE_SEARCH_RES_SKIP) ||
  2493. (result & ACLPB_CACHE_SEARCH_RES_ALLOW)) {
  2494. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2495. "DENY:Found SEARCH SKIP in cache\n");
  2496. skip_eval = 1;
  2497. break;
  2498. } else {
  2499. break;
  2500. }
  2501. } else { /* must be READ */
  2502. if (result & ACLPB_CACHE_READ_RES_DENY) {
  2503. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2504. "DENY:Found READ DENY in cache\n");
  2505. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2506. result_reason->deciding_aci = aci;
  2507. result_reason->reason = ACL_REASON_RESULT_CACHED_DENY;
  2508. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2509. tnf_string,cached_deny,"");
  2510. return ACL_RES_DENY;
  2511. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2512. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2513. "DENY:Found READ SKIP in cache\n");
  2514. skip_eval = 1;
  2515. break;
  2516. } else {
  2517. break;
  2518. }
  2519. }
  2520. }
  2521. }
  2522. }
  2523. if (skip_eval) {
  2524. skip_eval = 0;
  2525. continue;
  2526. }
  2527. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2528. if ( rv < 0) {
  2529. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2530. "acl__TestRights:Unable to set the DENY acllist\n");
  2531. continue;
  2532. }
  2533. /*
  2534. ** Now we have all the information we need. We need to call
  2535. ** the ONE ACL to test the rights.
  2536. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2537. */
  2538. aclpb->aclpb_curr_aci = aci;
  2539. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2540. map_generic, &deny,
  2541. &deny_generic,
  2542. &acl_tag, &expr_num);
  2543. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Processed:%d DENY handles Result:%d\n",index, rights_rv);
  2544. if (rights_rv == ACL_RES_FAIL) {
  2545. result_reason->deciding_aci = aci;
  2546. result_reason->reason = ACL_REASON_NONE;
  2547. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2548. tnf_string,evaled_deny,"");
  2549. return ACL_RES_DENY;
  2550. }
  2551. /* have we executed an ATTR RULE */
  2552. if ( aci->aci_ruleType & ACI_ATTR_RULES )
  2553. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2554. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2555. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2556. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2557. break;
  2558. }
  2559. }
  2560. if ( j < aclpb->aclpb_last_cache_result) {
  2561. /* already in cache */
  2562. } else if ( j < aclpb_max_cache_results ) {
  2563. /* j == aclpb->aclpb_last_cache_result &&
  2564. j < ACLPB_MAX_CACHE_RESULTS */
  2565. aclpb->aclpb_last_cache_result++;
  2566. aclpb->aclpb_cache_result[j].aci_index = index;
  2567. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2568. } else { /* cache overflow */
  2569. if ( rights_rv == ACL_RES_DENY) {
  2570. result_reason->deciding_aci = aci;
  2571. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2572. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2573. tnf_string,evaled_deny,"");
  2574. return ACL_RES_DENY;
  2575. } else {
  2576. continue;
  2577. }
  2578. }
  2579. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2580. if (rights_rv == ACL_RES_DENY) {
  2581. if (access & SLAPI_ACL_SEARCH) {
  2582. aclpb->aclpb_cache_result[j].result |=
  2583. ACLPB_CACHE_SEARCH_RES_DENY;
  2584. } else { /* MUST BE READ */
  2585. aclpb->aclpb_cache_result[j].result |=
  2586. ACLPB_CACHE_READ_RES_DENY;
  2587. }
  2588. /* We are done -- return */
  2589. result_reason->deciding_aci = aci;
  2590. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2591. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2592. tnf_string,evaled_deny,"");
  2593. return ACL_RES_DENY;
  2594. } else if (rights_rv == ACL_RES_ALLOW) {
  2595. /* This will happen, of we have an acl with both deny and allow
  2596. ** Since we may not have finished all the deny acl, go thru all
  2597. ** of them. We will use this cached result when we evaluate this
  2598. ** handle in the context of allow handles.
  2599. */
  2600. if (access & SLAPI_ACL_SEARCH) {
  2601. aclpb->aclpb_cache_result[j].result |=
  2602. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2603. } else {
  2604. aclpb->aclpb_cache_result[j].result |=
  2605. ACLPB_CACHE_READ_RES_ALLOW;
  2606. }
  2607. } else {
  2608. if (access & SLAPI_ACL_SEARCH) {
  2609. aclpb->aclpb_cache_result[j].result |=
  2610. ACLPB_CACHE_SEARCH_RES_SKIP;
  2611. } else {
  2612. aclpb->aclpb_cache_result[j].result |=
  2613. ACLPB_CACHE_READ_RES_SKIP;
  2614. }
  2615. continue;
  2616. }
  2617. } else {
  2618. if ( rights_rv == ACL_RES_DENY ) {
  2619. result_reason->deciding_aci = aci;
  2620. result_reason->reason = ACL_REASON_EVALUATED_DENY;
  2621. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2622. tnf_string,evaled_deny,"");
  2623. return ACL_RES_DENY;
  2624. }
  2625. }
  2626. }
  2627. /*
  2628. ** START PROCESSING ALLOW HANDLES.
  2629. ** Process each handle at a time. Do not concatenate the handles or else
  2630. ** all the context information will be build again and we will pay a
  2631. ** lot of penalty. The context is built the first time the handle is
  2632. ** processed.
  2633. **
  2634. ** First we set the default to INVALID so that if rules are not matched, then
  2635. ** we get INVALID and if a rule matched, the we get ALLOW.
  2636. */
  2637. aclpb->aclpb_state &= ~ACLPB_EXECUTING_DENY_HANDLES;
  2638. aclpb->aclpb_state |= ACLPB_EXECUTING_ALLOW_HANDLES;
  2639. ACL_SetDefaultResult (NULL, acleval, ACL_RES_INVALID);
  2640. numHandles = ACI_MAX_ELEVEL + aclpb->aclpb_num_allow_handles;
  2641. for (i=0, k=0; i < numHandles && k < aclpb->aclpb_num_allow_handles ; ++i) {
  2642. int skip_eval = 0;
  2643. /*
  2644. ** If the handle has been evaluated before, we can
  2645. ** cache the result.
  2646. */
  2647. if ((aci = aclpb->aclpb_allow_handles[i]) == NULL) {
  2648. if (i <= ACI_MAX_ELEVEL) {
  2649. continue;
  2650. } else {
  2651. break;
  2652. }
  2653. }
  2654. k++;
  2655. index = aci->aci_index;
  2656. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2657. "%d. Evaluating ALLOW aci(%d) \"%s\"\n", k, index, aci->aclName);
  2658. if (access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2659. /* We can not reused results obtained on a other entry */
  2660. if (aci->aci_type & ACI_CACHE_RESULT_PER_ENTRY) {
  2661. aclpb->aclpb_state |= ACLPB_CACHE_RESULT_PER_ENTRY_SKIP;
  2662. }
  2663. /*
  2664. * aclpb->aclpb_cache_result[0..aclpb->aclpb_last_cache_result] is
  2665. * a cache of info about whether applicable acis
  2666. * allowed, did_not_allow or denied access
  2667. */
  2668. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2669. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2670. short result;
  2671. result = aclpb->aclpb_cache_result[j].result;
  2672. if ( result <= 0) break;
  2673. if (!ACL_CACHED_RESULT_VALID(result)) {
  2674. /* something is wrong. Need to evaluate */
  2675. aclpb->aclpb_cache_result[j].result = -1;
  2676. break;
  2677. }
  2678. /*
  2679. ** We have a valid cached result. Let's see if we
  2680. ** have what we need.
  2681. */
  2682. if (access & SLAPI_ACL_SEARCH) {
  2683. if (result & ACLPB_CACHE_SEARCH_RES_ALLOW) {
  2684. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2685. "Found SEARCH ALLOW in cache\n");
  2686. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2687. result_reason->deciding_aci = aci;
  2688. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2689. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2690. tnf_string,cached_allow,"");
  2691. return ACL_RES_ALLOW;
  2692. } else if ( result & ACLPB_CACHE_SEARCH_RES_SKIP) {
  2693. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2694. "Found SEARCH SKIP in cache\n");
  2695. skip_eval = 1;
  2696. break;
  2697. } else {
  2698. /* need to evaluate */
  2699. break;
  2700. }
  2701. } else {
  2702. if ( result & ACLPB_CACHE_READ_RES_ALLOW) {
  2703. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2704. "Found READ ALLOW in cache\n");
  2705. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2706. result_reason->deciding_aci = aci;
  2707. result_reason->reason = ACL_REASON_RESULT_CACHED_ALLOW;
  2708. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2709. tnf_string,cached_allow,"");
  2710. return ACL_RES_ALLOW;
  2711. } else if ( result & ACLPB_CACHE_READ_RES_SKIP) {
  2712. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  2713. "Found READ SKIP in cache\n");
  2714. skip_eval = 1;
  2715. break;
  2716. } else {
  2717. break;
  2718. }
  2719. }
  2720. }
  2721. }
  2722. }
  2723. if ( skip_eval) {
  2724. skip_eval = 0;
  2725. continue;
  2726. }
  2727. TNF_PROBE_0_DEBUG(acl__libaccess_start,"ACL","");
  2728. rv = ACL_EvalSetACL(NULL, acleval, aci->aci_handle);
  2729. if ( rv < 0) {
  2730. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  2731. "acl__TestRights:Unable to set the acllist\n");
  2732. continue;
  2733. }
  2734. /*
  2735. ** Now we have all the information we need. We need to call
  2736. ** the ONE ACL to test the rights.
  2737. ** return value: ACL_RES_DENY, ACL_RES_ALLOW, error codes
  2738. */
  2739. aclpb->aclpb_curr_aci = aci;
  2740. rights_rv = ACL_EvalTestRights (NULL, acleval, testRights,
  2741. map_generic, &deny,
  2742. &deny_generic,
  2743. &acl_tag, &expr_num);
  2744. TNF_PROBE_0_DEBUG(acl__libaccess_end,"ACL","");
  2745. if (aci->aci_ruleType & ACI_ATTR_RULES)
  2746. aclpb->aclpb_state |= ACLPB_ATTR_RULE_EVALUATED;
  2747. if (access & (SLAPI_ACL_SEARCH | SLAPI_ACL_READ)) {
  2748. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  2749. if (index == aclpb->aclpb_cache_result[j].aci_index) {
  2750. break;
  2751. }
  2752. }
  2753. if (j < aclpb->aclpb_last_cache_result) {
  2754. /* already in cache */
  2755. aclpb->aclpb_cache_result[j].result &=
  2756. ~ACLPB_CACHE_ERROR_REPORTED;
  2757. } else if (j < aclpb_max_cache_results) {
  2758. /* j == aclpb->aclpb_last_cache_result &&
  2759. j < ACLPB_MAX_CACHE_RESULTS */
  2760. aclpb->aclpb_last_cache_result++;
  2761. aclpb->aclpb_cache_result[j].aci_index = index;
  2762. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  2763. aclpb->aclpb_cache_result[j].result &=
  2764. ~ACLPB_CACHE_ERROR_REPORTED;
  2765. } else { /* cache overflow */
  2766. if (!(aclpb->aclpb_cache_result[j].result &
  2767. ACLPB_CACHE_ERROR_REPORTED)) {
  2768. slapi_log_error (SLAPI_LOG_FATAL, "acl__TestRights",
  2769. "Your ACL cache of %d slots has overflowed. "
  2770. "This can happen when you have many ACIs. "
  2771. "This ACI evaluation requires %d slots to cache. "
  2772. "You can increase your max value by setting the attribute "
  2773. "%s in cn=ACL Plugin,cn=plugins,cn=config to a value higher. "
  2774. "A server restart is required.\n",
  2775. j, aclpb_max_cache_results, ATTR_ACLPB_MAX_SELECTED_ACLS);
  2776. aclpb->aclpb_cache_result[j].result |=
  2777. ACLPB_CACHE_ERROR_REPORTED;
  2778. }
  2779. if (rights_rv == ACL_RES_ALLOW) {
  2780. result_reason->deciding_aci = aci;
  2781. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2782. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2783. tnf_string,evaled_allow,"");
  2784. return ACL_RES_ALLOW;
  2785. } else {
  2786. continue;
  2787. }
  2788. }
  2789. __acl_set_aclIndex_inResult ( aclpb, access, index );
  2790. if (rights_rv == ACL_RES_ALLOW) {
  2791. if (access & SLAPI_ACL_SEARCH) {
  2792. aclpb->aclpb_cache_result[j].result |=
  2793. ACLPB_CACHE_SEARCH_RES_ALLOW;
  2794. } else { /* must be READ */
  2795. aclpb->aclpb_cache_result[j].result |=
  2796. ACLPB_CACHE_READ_RES_ALLOW;
  2797. }
  2798. /* We are done -- return */
  2799. result_reason->deciding_aci = aci;
  2800. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2801. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2802. tnf_string,evaled_allow,"");
  2803. return ACL_RES_ALLOW;
  2804. } else {
  2805. if (access & SLAPI_ACL_SEARCH) {
  2806. aclpb->aclpb_cache_result[j].result |=
  2807. ACLPB_CACHE_SEARCH_RES_SKIP;
  2808. } else {
  2809. aclpb->aclpb_cache_result[j].result |=
  2810. ACLPB_CACHE_READ_RES_SKIP;
  2811. }
  2812. continue;
  2813. }
  2814. } else {
  2815. if ( rights_rv == ACL_RES_ALLOW ) {
  2816. result_reason->deciding_aci = aci;
  2817. result_reason->reason = ACL_REASON_EVALUATED_ALLOW;
  2818. TNF_PROBE_1_DEBUG(acl__TestRights_end,"ACL","",
  2819. tnf_string,evaled_allow,"");
  2820. return ACL_RES_ALLOW;
  2821. }
  2822. }
  2823. }/* for */
  2824. result_reason->deciding_aci = aci;
  2825. result_reason->reason = ACL_REASON_NO_MATCHED_SUBJECT_ALLOWS;
  2826. TNF_PROBE_0_DEBUG(acl__TestRights_end,"ACL","");
  2827. return (ACL_RES_DENY);
  2828. }
  2829. /***************************************************************************
  2830. *
  2831. * acl_match_substring
  2832. *
  2833. * Compare the input string to the patteren in the filter
  2834. *
  2835. * Input:
  2836. * struct slapi_filter *f - Filter which has the patteren
  2837. * char *str - String to compare
  2838. * int exact_match - 1; match the pattern exactly
  2839. * - 0; match the pattern as a suffix
  2840. *
  2841. * Returns:
  2842. * ACL_TRUE - The sting matches with the patteren
  2843. * ACL_FALSE - No it doesn't
  2844. * ACL_ERR - Error
  2845. *
  2846. * Error Handling:
  2847. * None.
  2848. *
  2849. **************************************************************************/
  2850. int
  2851. acl_match_substring ( Slapi_Filter *f, char *str, int exact_match)
  2852. {
  2853. int i, rc, len;
  2854. char *p = NULL;
  2855. char *end, *realval, *tmp;
  2856. char pat[BUFSIZ];
  2857. char buf[BUFSIZ];
  2858. char *type, *initial, *final;
  2859. char **any;
  2860. Slapi_Regex *re = NULL;
  2861. const char *re_result = NULL;
  2862. if ( 0 != slapi_filter_get_subfilt ( f, &type, &initial, &any, &final ) ) {
  2863. return (ACL_FALSE);
  2864. }
  2865. /* convert the input to lower. */
  2866. for (p = str; *p; p++)
  2867. *p = TOLOWER ( *p );
  2868. /* construct a regular expression corresponding to the filter: */
  2869. pat[0] = '\0';
  2870. p = pat;
  2871. end = pat + sizeof(pat) - 2; /* leave room for null */
  2872. if ( initial != NULL) {
  2873. strcpy (p, "^");
  2874. p = strchr (p, '\0');
  2875. /* 2 * in case every char is special */
  2876. if (p + 2 * strlen ( initial ) > end) {
  2877. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2878. "not enough pattern space\n");
  2879. return (ACL_ERR);
  2880. }
  2881. if (!exact_match) {
  2882. strcpy (p, ".*");
  2883. p = strchr (p, '\0');
  2884. }
  2885. acl_strcpy_special (p, initial);
  2886. p = strchr (p, '\0');
  2887. }
  2888. if ( any != NULL) {
  2889. for (i = 0; any && any[i] != NULL; i++) {
  2890. /* ".*" + value */
  2891. if (p + 2 * strlen ( any[i]) + 2 > end) {
  2892. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2893. "not enough pattern space\n");
  2894. return (ACL_ERR);
  2895. }
  2896. strcpy (p, ".*");
  2897. p = strchr (p, '\0');
  2898. acl_strcpy_special (p, any[i]);
  2899. p = strchr (p, '\0');
  2900. }
  2901. }
  2902. if ( final != NULL) {
  2903. /* ".*" + value */
  2904. if (p + 2 * strlen ( final ) + 2 > end) {
  2905. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2906. "not enough pattern space\n");
  2907. return (ACL_ERR);
  2908. }
  2909. strcpy (p, ".*");
  2910. p = strchr (p, '\0');
  2911. acl_strcpy_special (p, final);
  2912. p = strchr (p, '\0');
  2913. strcpy (p, "$");
  2914. }
  2915. /* see if regex matches with the input string */
  2916. tmp = NULL;
  2917. len = strlen(str);
  2918. if (len < sizeof(buf)) {
  2919. strcpy (buf, str);
  2920. realval = buf;
  2921. } else {
  2922. tmp = (char*) slapi_ch_malloc (len + 1);
  2923. strcpy (tmp, str);
  2924. realval = tmp;
  2925. }
  2926. /* What we have built is a regular pattaren expression.
  2927. ** Now we will compile the pattern and compare wth the string to
  2928. ** see if the input string matches with the patteren or not.
  2929. */
  2930. re = slapi_re_comp( pat, &re_result );
  2931. if (NULL == re) {
  2932. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  2933. "acl_match_substring:re_comp failed (%s)\n", re_result?re_result:"unknown");
  2934. return (ACL_ERR);
  2935. }
  2936. /* slapi_re_exec() returns 1 if the string p1 matches the last compiled
  2937. ** regular expression, 0 if the string p1 failed to match
  2938. */
  2939. rc = slapi_re_exec( re, realval, -1 /* no timelimit */ );
  2940. slapi_re_free(re);
  2941. slapi_ch_free ( (void **) &tmp );
  2942. if (rc == 1) {
  2943. return ACL_TRUE;
  2944. } else {
  2945. return ACL_FALSE;
  2946. }
  2947. }
  2948. /***************************************************************************
  2949. *
  2950. * acl__reset_cached_result
  2951. *
  2952. * Go thru the cached result and invlalidate the cached evaluation results for
  2953. * rules which can only be cached based on the scope.
  2954. * If we have scope ACI_CACHE_RESULT_PER_ENTRY, then we need to invalidate the
  2955. * cacched reult whenever we hit a new entry.
  2956. *
  2957. * Returns:
  2958. * Null
  2959. *
  2960. **************************************************************************/
  2961. static void
  2962. acl__reset_cached_result (struct acl_pblock *aclpb )
  2963. {
  2964. int j;
  2965. for (j =0; j < aclpb->aclpb_last_cache_result; j++) {
  2966. /* Unless we have to clear the result, move on */
  2967. if (!( aclpb->aclpb_cache_result[j].aci_ruleType & ACI_CACHE_RESULT_PER_ENTRY))
  2968. continue;
  2969. aclpb->aclpb_cache_result[j].result = 0;
  2970. }
  2971. }
  2972. /*
  2973. * acl_access_allowed_disjoint_resource
  2974. *
  2975. * This is an internal module which can be used to verify
  2976. * access to a resource which may not be inside the search scope.
  2977. *
  2978. * Returns:
  2979. * - Same return val as acl_access_allowed().
  2980. */
  2981. int
  2982. acl_access_allowed_disjoint_resource(
  2983. Slapi_PBlock *pb,
  2984. Slapi_Entry *e, /* The Slapi_Entry */
  2985. char *attr, /* Attribute of the entry */
  2986. struct berval *val, /* value of attr. NOT USED */
  2987. int access /* access rights */
  2988. )
  2989. {
  2990. int rv;
  2991. struct acl_pblock *aclpb = NULL;
  2992. aclpb = acl_get_aclpb ( pb, ACLPB_BINDDN_PBLOCK );
  2993. /*
  2994. ** It's possible that we already have a context of ACLs.
  2995. ** However once in a while, we need to test
  2996. ** access to a resource which (like vlv, schema) which falls
  2997. ** outside the search base. In that case, we need to go
  2998. ** thru all the ACLs and not depend upon the acls which we have
  2999. ** gathered.
  3000. */
  3001. /* If you have the right to use the resource, then we don't need to check for
  3002. ** proxy right on that resource.
  3003. */
  3004. if (aclpb)
  3005. aclpb->aclpb_state |= ( ACLPB_DONOT_USE_CONTEXT_ACLS| ACLPB_DONOT_EVALUATE_PROXY );
  3006. rv = acl_access_allowed(pb, e, attr, val, access);
  3007. if (aclpb) aclpb->aclpb_state &= ~ACLPB_DONOT_USE_CONTEXT_ACLS;
  3008. if (aclpb ) aclpb->aclpb_state &= ~ACLPB_DONOT_EVALUATE_PROXY;
  3009. return rv;
  3010. }
  3011. /*
  3012. * acl__attr_cached_result
  3013. * Loops thru the cached result and determines if we can use the cached value.
  3014. *
  3015. * Inputs:
  3016. * Slapi_pblock *aclpb - acl private block
  3017. * char *attr - attribute name
  3018. * int access - access type
  3019. * Returns:
  3020. * LDAP_SUCCESS: - access is granted
  3021. * LDAP_INSUFFICIENT_ACCESS - access denied
  3022. * ACL_ERR - no cached info about this attr.
  3023. * - or the attr had multiple result and so
  3024. * - we can't determine the outcome.
  3025. *
  3026. */
  3027. static int
  3028. acl__attr_cached_result (struct acl_pblock *aclpb, char *attr, int access )
  3029. {
  3030. int i, rc;
  3031. aclEvalContext *c_evalContext;
  3032. if ( !(access & ( SLAPI_ACL_SEARCH | SLAPI_ACL_READ) ))
  3033. return ACL_ERR;
  3034. if (aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3035. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3036. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3037. "acl__attr_cached_result:Using Context: ACLPB_ACLCB\n" );
  3038. } else {
  3039. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3040. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3041. "acl__attr_cached_result:Using Context: ACLPB_PREV\n" );
  3042. }
  3043. if ( attr == NULL ) {
  3044. int eval_read = 0;
  3045. /*
  3046. ** Do I have access to at least one attribute, then I have
  3047. ** access to the entry.
  3048. */
  3049. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3050. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3051. if ( (access & SLAPI_ACL_READ ) && a_eval->attrEval_r_status &&
  3052. a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3053. eval_read++;
  3054. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3055. return LDAP_SUCCESS;
  3056. /* rbyrne: recompute if we have to.
  3057. * How does this cached result get turned off for
  3058. * attr style acis which acannot be cached becuase entry
  3059. * can result in a diff value.
  3060. */
  3061. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3062. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3063. a_eval->attrEval_r_aciIndex);
  3064. if ( rc != ACL_ERR ) {
  3065. acl_copyEval_context ( aclpb, c_evalContext,
  3066. &aclpb->aclpb_curr_entryEval_context, 1);
  3067. }
  3068. if ( rc == LDAP_SUCCESS) {
  3069. return LDAP_SUCCESS;
  3070. }
  3071. }
  3072. }
  3073. }/* for */
  3074. /*
  3075. * If we have scanned the whole list without success then
  3076. * we are not granting access to this entry through access
  3077. * to an attribute in the list--however this does not mean
  3078. * that we do not have access to the entry via another attribute
  3079. * not already in the list, so return -1 meaning
  3080. * "don't know".
  3081. */
  3082. return(ACL_ERR);
  3083. #if 0
  3084. if ( eval_read )
  3085. return LDAP_INSUFFICIENT_ACCESS;
  3086. else
  3087. return ACL_ERR;
  3088. #endif
  3089. }
  3090. for (i=0; i < c_evalContext->acle_numof_attrs; i++ ) {
  3091. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[i];
  3092. if ( a_eval == NULL ) continue;
  3093. if (strcasecmp ( attr, a_eval->attrEval_name ) == 0 ) {
  3094. if ( access & SLAPI_ACL_SEARCH ) {
  3095. if (a_eval->attrEval_s_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3096. if ( a_eval->attrEval_s_status & ACL_ATTREVAL_SUCCESS)
  3097. return LDAP_SUCCESS;
  3098. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_FAIL)
  3099. return LDAP_INSUFFICIENT_ACCESS;
  3100. else if ( a_eval->attrEval_s_status & ACL_ATTREVAL_RECOMPUTE ) {
  3101. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3102. a_eval->attrEval_s_aciIndex);
  3103. if ( rc != ACL_ERR ) {
  3104. acl_copyEval_context ( aclpb, c_evalContext,
  3105. &aclpb->aclpb_curr_entryEval_context, 1);
  3106. }
  3107. } else
  3108. return ACL_ERR;
  3109. } else {
  3110. /* This means that for the same attribute and same type of
  3111. ** access, we had different results at different time.
  3112. ** Since we are not caching per object, we can't
  3113. ** determine exactly. So, can't touch this
  3114. */
  3115. return ACL_ERR;
  3116. }
  3117. } else {
  3118. if (a_eval->attrEval_r_status < ACL_ATTREVAL_DETERMINISTIC ) {
  3119. if ( a_eval->attrEval_r_status & ACL_ATTREVAL_SUCCESS)
  3120. return LDAP_SUCCESS;
  3121. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_FAIL)
  3122. return LDAP_INSUFFICIENT_ACCESS;
  3123. else if ( a_eval->attrEval_r_status & ACL_ATTREVAL_RECOMPUTE ) {
  3124. rc = acl__recompute_acl ( aclpb, a_eval, access,
  3125. a_eval->attrEval_r_aciIndex);
  3126. if ( rc != ACL_ERR ) {
  3127. acl_copyEval_context ( aclpb, c_evalContext,
  3128. &aclpb->aclpb_curr_entryEval_context, 1);
  3129. }
  3130. } else
  3131. return ACL_ERR;
  3132. } else {
  3133. /* Look above for explanation */
  3134. return ACL_ERR;
  3135. }
  3136. }
  3137. }
  3138. }
  3139. return ACL_ERR;
  3140. }
  3141. /*
  3142. * Had to do this juggling of casting to make
  3143. * both Nt & unix compiler happy.
  3144. */
  3145. static int
  3146. acl__cmp(const void *a, const void *b)
  3147. {
  3148. short *i = (short *) a;
  3149. short *j = (short *) b;
  3150. if ( (short) *i > (short) *j )
  3151. return (1);
  3152. if ( (short)*i < (short) *j)
  3153. return (-1);
  3154. return (0);
  3155. }
  3156. /*
  3157. * acl__scan_match_handles
  3158. * Go thru the ACL list and determine if the list of acls selected matches
  3159. * what we have in the cache.
  3160. *
  3161. * Inputs:
  3162. * Acl_PBlock *pb - Main pblock ( blacvk hole)
  3163. * int type - Which context to look on
  3164. *
  3165. * Returns:
  3166. * 0 - matches all the acl handles
  3167. * ACL_ERR - sorry; no match
  3168. *
  3169. * ASSUMPTION: A READER LOCK ON ACL LIST
  3170. */
  3171. static int
  3172. acl__scan_match_handles ( Acl_PBlock *aclpb, int type)
  3173. {
  3174. int matched = 0;
  3175. aci_t *aci = NULL;
  3176. int index;
  3177. PRUint32 cookie;
  3178. aclEvalContext *c_evalContext = NULL;
  3179. if (type == ACLPB_EVALCONTEXT_PREV ) {
  3180. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3181. } else if ( type == ACLPB_EVALCONTEXT_ACLCB ){
  3182. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3183. } else {
  3184. return ACL_ERR;
  3185. }
  3186. if ( !c_evalContext->acle_numof_tmatched_handles )
  3187. return ACL_ERR;
  3188. aclpb->aclpb_stat_acllist_scanned++;
  3189. aci = acllist_get_first_aci ( aclpb, &cookie );
  3190. while ( aci ) {
  3191. index = aci->aci_index;
  3192. if (acl__resource_match_aci(aclpb, aci, 1 /* skip attr matching */, NULL )) {
  3193. int j;
  3194. int s_matched = matched;
  3195. /* We have a sorted list of handles that matched the target */
  3196. for (j=0; j < c_evalContext->acle_numof_tmatched_handles ; j++ ) {
  3197. if ( c_evalContext->acle_handles_matched_target[j] > index )
  3198. break;
  3199. else if ( index == c_evalContext->acle_handles_matched_target[j] ) {
  3200. int jj;
  3201. matched++;
  3202. /* See if this is a ATTR rule that matched -- in that case we have
  3203. ** to nullify the cached result
  3204. */
  3205. if ( aci->aci_ruleType & ACI_ATTR_RULES ) {
  3206. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3207. "Found an attr Rule [Name:%s Index:%d\n", aci->aclName,
  3208. aci->aci_index );
  3209. for ( jj =0; jj < c_evalContext->acle_numof_attrs; jj++ ) {
  3210. AclAttrEval *a_eval = &c_evalContext->acle_attrEval[jj];
  3211. if ( a_eval->attrEval_r_aciIndex == aci->aci_index )
  3212. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3213. if ( a_eval->attrEval_s_aciIndex == aci->aci_index )
  3214. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3215. }
  3216. }
  3217. break;
  3218. }
  3219. }
  3220. if ( s_matched == matched ) return ACL_ERR;
  3221. }
  3222. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3223. }
  3224. if ( matched == c_evalContext->acle_numof_tmatched_handles )
  3225. return 0;
  3226. return ACL_ERR;
  3227. }
  3228. /*
  3229. * acl_copyEval_context
  3230. * Copy the context info which include attr info and handles.
  3231. *
  3232. * Inputs
  3233. * struct acl_pblock *aclpb - acl private main block
  3234. * aclEvalContext *src - src context
  3235. * aclEvalContext *dest - dest context
  3236. * Returns:
  3237. * None.
  3238. *
  3239. */
  3240. void
  3241. acl_copyEval_context ( struct acl_pblock *aclpb, aclEvalContext *src,
  3242. aclEvalContext *dest , int copy_attr_only )
  3243. {
  3244. int d_slot, i;
  3245. /* Do a CLEAN copy we have nothing or else do an incremental copy.*/
  3246. if ( src->acle_numof_attrs < 1 )
  3247. return;
  3248. /* Copy the attr info */
  3249. if ( dest->acle_numof_attrs < 1 )
  3250. acl_clean_aclEval_context ( dest, 0 /*clean */ );
  3251. d_slot = dest->acle_numof_attrs;
  3252. for (i=0; i < src->acle_numof_attrs; i++ ) {
  3253. int j;
  3254. int attr_exists = 0;
  3255. int dd_slot = d_slot;
  3256. if ( aclpb && (i == 0) ) aclpb->aclpb_stat_num_copycontext++;
  3257. if ( src->acle_attrEval[i].attrEval_r_status == 0 &&
  3258. src->acle_attrEval[i].attrEval_s_status == 0 )
  3259. continue;
  3260. for ( j = 0; j < dest->acle_numof_attrs; j++ ) {
  3261. if ( strcasecmp ( src->acle_attrEval[i].attrEval_name,
  3262. dest->acle_attrEval[j].attrEval_name ) == 0 ) {
  3263. /* We have it. skip it. */
  3264. attr_exists = 1;
  3265. dd_slot = j;
  3266. break;
  3267. }
  3268. }
  3269. if ( !attr_exists ) {
  3270. if ( dd_slot >= ACLPB_MAX_ATTRS -1 )
  3271. break;
  3272. if ( aclpb) aclpb->aclpb_stat_num_copy_attrs++;
  3273. if ( dest->acle_attrEval[dd_slot].attrEval_name )
  3274. slapi_ch_free ( (void **) &dest->acle_attrEval[dd_slot].attrEval_name );
  3275. dest->acle_attrEval[dd_slot].attrEval_name =
  3276. slapi_ch_strdup ( src->acle_attrEval[i].attrEval_name );
  3277. }
  3278. /* Copy the result status and the aci index */
  3279. dest->acle_attrEval[dd_slot].attrEval_r_status =
  3280. src->acle_attrEval[i].attrEval_r_status;
  3281. dest->acle_attrEval[dd_slot].attrEval_r_aciIndex =
  3282. src->acle_attrEval[i].attrEval_r_aciIndex;
  3283. dest->acle_attrEval[dd_slot].attrEval_s_status =
  3284. src->acle_attrEval[i].attrEval_s_status;
  3285. dest->acle_attrEval[dd_slot].attrEval_s_aciIndex =
  3286. src->acle_attrEval[i].attrEval_s_aciIndex;
  3287. if (!attr_exists ) d_slot++;
  3288. }
  3289. dest->acle_numof_attrs = d_slot;
  3290. dest->acle_attrEval[d_slot].attrEval_name = NULL;
  3291. if ( copy_attr_only )
  3292. return;
  3293. /* First sort the arrays which keeps the acl index numbers */
  3294. qsort ( (char *) src->acle_handles_matched_target,
  3295. (size_t)src->acle_numof_tmatched_handles, sizeof( int ), acl__cmp );
  3296. for (i=0; i < src->acle_numof_tmatched_handles; i++ ) {
  3297. dest->acle_handles_matched_target[i] =
  3298. src->acle_handles_matched_target[i];
  3299. }
  3300. if ( src->acle_numof_tmatched_handles ) {
  3301. dest->acle_numof_tmatched_handles = src->acle_numof_tmatched_handles;
  3302. if ( aclpb) aclpb->aclpb_stat_num_tmatched_acls = src->acle_numof_tmatched_handles;
  3303. }
  3304. }
  3305. /*
  3306. * acl_clean_aclEval_context
  3307. * Clean the eval context
  3308. *
  3309. * Inputs:
  3310. * aclEvalContext *clean_me - COntext to be cleaned
  3311. * int clean_type - 0: clean, 1 scrub
  3312. *
  3313. */
  3314. void
  3315. acl_clean_aclEval_context ( aclEvalContext *clean_me, int scrub_only )
  3316. {
  3317. int i;
  3318. /* Copy the attr info */
  3319. for (i=0; i < clean_me->acle_numof_attrs; i++ ) {
  3320. char *a_name = clean_me->acle_attrEval[i].attrEval_name;
  3321. if ( a_name && !scrub_only) {
  3322. slapi_ch_free ( (void **) &a_name );
  3323. clean_me->acle_attrEval[i].attrEval_name = NULL;
  3324. }
  3325. clean_me->acle_attrEval[i].attrEval_r_status = 0;
  3326. clean_me->acle_attrEval[i].attrEval_s_status = 0;
  3327. clean_me->acle_attrEval[i].attrEval_r_aciIndex = 0;
  3328. clean_me->acle_attrEval[i].attrEval_s_aciIndex = 0;
  3329. }
  3330. if ( !scrub_only ) clean_me->acle_numof_attrs = 0;
  3331. clean_me->acle_numof_tmatched_handles = 0;
  3332. }
  3333. /*
  3334. * acl__match_handlesFromCache
  3335. *
  3336. * We have 2 cacheed information
  3337. * 1) cached info from the previous operation
  3338. * 2) cached info from the prev entry evaluation
  3339. *
  3340. * What we are doing here is going thru all the acls and see if the same
  3341. * set of acls apply to this resource or not. If it does, then do we have
  3342. * a cached info for the attr. If we don't for both of the cases then we need
  3343. * to evaluate all over again.
  3344. *
  3345. * Inputs:
  3346. * struct acl_pblock - ACL private block;
  3347. * char *attr - Attribute name
  3348. * int access - acces type
  3349. *
  3350. * returns:
  3351. * LDAP_SUCCESS (0) - The same acls apply and we have
  3352. * access ALLOWED on the attr
  3353. * LDAP_INSUFFICIENT_ACCESS - The same acls apply and we have
  3354. * access DENIED on the attr
  3355. * -1 - Acls doesn't match or we don't have
  3356. * cached info for this attr.
  3357. *
  3358. * ASSUMPTIONS: A reader lock has been obtained for the acl list.
  3359. */
  3360. static int
  3361. acl__match_handlesFromCache ( Acl_PBlock *aclpb, char *attr, int access)
  3362. {
  3363. aclEvalContext *c_evalContext = NULL;
  3364. int context_type = 0;
  3365. int ret_val = -1; /* it doen't match by default */
  3366. /* Before we proceed, find out if we have evaluated any ATTR RULE. If we have
  3367. ** then we can't use any caching mechanism
  3368. */
  3369. if ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT ) {
  3370. context_type = ACLPB_EVALCONTEXT_ACLCB;
  3371. c_evalContext = &aclpb->aclpb_prev_opEval_context;
  3372. } else {
  3373. context_type = ACLPB_EVALCONTEXT_PREV;
  3374. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  3375. }
  3376. /* we can not reused access evaluation done on a previous entry
  3377. * so just skip that cache
  3378. */
  3379. if (aclpb->aclpb_state & ACLPB_CACHE_RESULT_PER_ENTRY_SKIP) {
  3380. aclpb->aclpb_state &= ~ACLPB_MATCHES_ALL_ACLS;
  3381. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  3382. /* Did not match */
  3383. if (context_type == ACLPB_EVALCONTEXT_ACLCB) {
  3384. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT;
  3385. } else {
  3386. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  3387. c_evalContext->acle_numof_tmatched_handles = 0;
  3388. }
  3389. return -1;
  3390. }
  3391. if ( aclpb->aclpb_res_type & (ACLPB_NEW_ENTRY | ACLPB_EFFECTIVE_RIGHTS) ) {
  3392. aclpb->aclpb_state |= ACLPB_MATCHES_ALL_ACLS;
  3393. ret_val = acl__scan_match_handles ( aclpb, context_type );
  3394. if ( -1 == ret_val ) {
  3395. aclpb->aclpb_state &= ~ACLPB_MATCHES_ALL_ACLS;
  3396. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  3397. /* Did not match */
  3398. if ( context_type == ACLPB_EVALCONTEXT_ACLCB ) {
  3399. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT;
  3400. } else {
  3401. aclpb->aclpb_state |= ACLPB_COPY_EVALCONTEXT;
  3402. c_evalContext->acle_numof_tmatched_handles = 0;
  3403. }
  3404. }
  3405. }
  3406. if ( aclpb->aclpb_state & ACLPB_MATCHES_ALL_ACLS ) {
  3407. /* See if we have a cached result for this attr */
  3408. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3409. /* It's not in the ACLCB context but we might have it in the
  3410. ** current/prev context. Take a look at it. we might have evaluated
  3411. ** this attribute already.
  3412. */
  3413. if ( (-1 == ret_val ) &&
  3414. ( aclpb->aclpb_state & ACLPB_HAS_ACLCB_EVALCONTEXT )) {
  3415. aclpb->aclpb_state &= ~ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3416. ret_val = acl__attr_cached_result (aclpb, attr, access);
  3417. aclpb->aclpb_state |= ACLPB_HAS_ACLCB_EVALCONTEXT ;
  3418. /* We need to do an incremental update */
  3419. if ( !ret_val ) aclpb->aclpb_state |= ACLPB_INCR_ACLCB_CACHE;
  3420. }
  3421. }
  3422. return ret_val;
  3423. }
  3424. /*
  3425. * acl__get_attrEval
  3426. * Get the atteval from the current context and hold the ptr in aclpb.
  3427. * If we have too many attrs, then allocate a new one. In that case
  3428. * we let the caller know about that so that it will be deallocated.
  3429. *
  3430. * Returns:
  3431. * int - 0: The context was indexed. So, no allocations.
  3432. * - 1; context was allocated - deallocate it.
  3433. */
  3434. static int
  3435. acl__get_attrEval ( struct acl_pblock *aclpb, char *attr )
  3436. {
  3437. int j;
  3438. aclEvalContext *c_ContextEval = &aclpb->aclpb_curr_entryEval_context;
  3439. int deallocate_attrEval = 0;
  3440. AclAttrEval *c_attrEval = NULL;
  3441. if ( !attr ) return deallocate_attrEval;
  3442. aclpb->aclpb_curr_attrEval = NULL;
  3443. /* Go thru and see if we have the attr already */
  3444. for (j=0; j < c_ContextEval->acle_numof_attrs; j++) {
  3445. c_attrEval = &c_ContextEval->acle_attrEval[j];
  3446. if ( c_attrEval &&
  3447. slapi_attr_type_cmp ( c_attrEval->attrEval_name, attr, 1) == 0 ) {
  3448. aclpb->aclpb_curr_attrEval = c_attrEval;
  3449. break;
  3450. }
  3451. }
  3452. if ( !aclpb->aclpb_curr_attrEval) {
  3453. if ( c_ContextEval->acle_numof_attrs == ACLPB_MAX_ATTRS -1 ) {
  3454. /* Too many attrs. create a temp one */
  3455. c_attrEval = (AclAttrEval * ) slapi_ch_calloc ( 1, sizeof ( AclAttrEval ) );
  3456. deallocate_attrEval =1;
  3457. } else {
  3458. c_attrEval = &c_ContextEval->acle_attrEval[c_ContextEval->acle_numof_attrs++];
  3459. c_attrEval->attrEval_r_status = 0;
  3460. c_attrEval->attrEval_s_status = 0;
  3461. c_attrEval->attrEval_r_aciIndex = 0;
  3462. c_attrEval->attrEval_s_aciIndex = 0;
  3463. }
  3464. /* clean it before use */
  3465. c_attrEval->attrEval_name = slapi_ch_strdup ( attr );
  3466. aclpb->aclpb_curr_attrEval = c_attrEval;
  3467. }
  3468. return deallocate_attrEval;
  3469. }
  3470. /*
  3471. * acl_skip_access_check
  3472. *
  3473. * See if we need to go thru the ACL check or not. We don't need to if I am root
  3474. * or internal operation or ...
  3475. *
  3476. * returns:
  3477. * ACL_TRUE - Yes; skip the ACL check
  3478. * ACL_FALSE - No; you have to go thru ACL check
  3479. *
  3480. */
  3481. int
  3482. acl_skip_access_check ( Slapi_PBlock *pb, Slapi_Entry *e )
  3483. {
  3484. int rv, isRoot, accessCheckDisabled;
  3485. void *conn = NULL;
  3486. Slapi_Backend *be;
  3487. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  3488. if ( isRoot ) return ACL_TRUE;
  3489. /* See if this is local request */
  3490. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn);
  3491. if ( NULL == conn ) return ACL_TRUE;
  3492. /*
  3493. * Turn on access checking in the rootdse--this code used
  3494. * to skip the access check.
  3495. *
  3496. * check if the entry is the RootDSE entry
  3497. if ( e ) {
  3498. char * edn = slapi_entry_get_ndn ( e );
  3499. if ( slapi_is_rootdse ( edn ) ) return ACL_TRUE;
  3500. }
  3501. */
  3502. /* GB : when referrals are directly set in the mappin tree
  3503. * we can reach this code without a backend in the pblock
  3504. * in such a case, allow access for now
  3505. * we may want to reconsider this is NULL DSE implementation happens
  3506. */
  3507. rv = slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  3508. if (be == NULL)
  3509. return ACL_TRUE;
  3510. rv = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  3511. if ( rv != -1 && accessCheckDisabled ) return ACL_TRUE;
  3512. return ACL_FALSE;
  3513. }
  3514. short
  3515. acl_get_aclsignature ()
  3516. {
  3517. return acl_signature;
  3518. }
  3519. void
  3520. acl_set_aclsignature ( short value)
  3521. {
  3522. acl_signature = value;
  3523. }
  3524. void
  3525. acl_regen_aclsignature ()
  3526. {
  3527. acl_signature = aclutil_gen_signature ( acl_signature );
  3528. }
  3529. /*
  3530. *
  3531. * Assumptions:
  3532. * 1) Called for read/search right.
  3533. */
  3534. static int
  3535. acl__recompute_acl ( Acl_PBlock *aclpb,
  3536. AclAttrEval *a_eval,
  3537. int access,
  3538. int aciIndex
  3539. )
  3540. {
  3541. char *unused_str1, *unused_str2;
  3542. char *acl_tag, *testRight[2];
  3543. int j, expr_num;
  3544. int result_status = ACL_RES_INVALID, cache_result;
  3545. PRUint32 cookie;
  3546. aci_t *aci;
  3547. PR_ASSERT ( aciIndex >= 0 );
  3548. PR_ASSERT ( a_eval != NULL );
  3549. PR_ASSERT (aclpb != NULL );
  3550. /* We might have evaluated this acl just now, check it there first */
  3551. for ( j =0; j < aclpb->aclpb_last_cache_result; j++) {
  3552. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3553. short result;
  3554. result_status =ACL_RES_INVALID;
  3555. result = aclpb->aclpb_cache_result[j].result;
  3556. if ( result <= 0) break;
  3557. if (!ACL_CACHED_RESULT_VALID(result)) {
  3558. /* something is wrong. Need to evaluate */
  3559. aclpb->aclpb_cache_result[j].result = -1;
  3560. break;
  3561. }
  3562. /*
  3563. ** We have a valid cached result. Let's see if we
  3564. ** have what we need.
  3565. */
  3566. if ((result & ACLPB_CACHE_SEARCH_RES_ALLOW) ||
  3567. (result & ACLPB_CACHE_READ_RES_ALLOW) )
  3568. result_status = ACL_RES_ALLOW;
  3569. else if ((result & ACLPB_CACHE_SEARCH_RES_DENY) ||
  3570. (result & ACLPB_CACHE_READ_RES_DENY) )
  3571. result_status = ACL_RES_DENY;
  3572. }
  3573. } /* end of for */
  3574. if ( result_status != ACL_RES_INVALID ) {
  3575. goto set_result_status;
  3576. }
  3577. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  3578. "Recomputing the ACL Index:%d for entry:%s\n",
  3579. aciIndex, slapi_entry_get_ndn ( aclpb->aclpb_curr_entry) );
  3580. /* First find this one ACL and then evaluate it. */
  3581. aci = acllist_get_first_aci ( aclpb, &cookie );
  3582. while ( aci && aci->aci_index != aciIndex ) {
  3583. aci = acllist_get_next_aci ( aclpb, aci, &cookie );
  3584. }
  3585. if (NULL == aci)
  3586. return -1;
  3587. ACL_SetDefaultResult (NULL, aclpb->aclpb_acleval, ACL_RES_INVALID);
  3588. ACL_EvalSetACL(NULL, aclpb->aclpb_acleval, aci->aci_handle);
  3589. testRight[0] = acl_access2str ( access );
  3590. testRight[1] = '\0';
  3591. aclpb->aclpb_curr_aci = aci;
  3592. result_status = ACL_EvalTestRights (NULL, aclpb->aclpb_acleval, testRight,
  3593. ds_map_generic, &unused_str1,
  3594. &unused_str2,
  3595. &acl_tag, &expr_num);
  3596. cache_result = 0;
  3597. if ( result_status == ACL_RES_DENY && aci->aci_type & ACI_HAS_DENY_RULE ) {
  3598. if ( access & SLAPI_ACL_SEARCH)
  3599. cache_result = ACLPB_CACHE_SEARCH_RES_DENY;
  3600. else
  3601. cache_result = ACLPB_CACHE_READ_RES_DENY;
  3602. } else if ( result_status == ACL_RES_ALLOW && aci->aci_type & ACI_HAS_ALLOW_RULE ) {
  3603. if ( access & SLAPI_ACL_SEARCH)
  3604. cache_result = ACLPB_CACHE_SEARCH_RES_ALLOW;
  3605. else
  3606. cache_result = ACLPB_CACHE_READ_RES_ALLOW;
  3607. } else {
  3608. result_status = -1;
  3609. }
  3610. /* Now we need to put the cached result in the aclpb */
  3611. for (j=0; j <aclpb->aclpb_last_cache_result; ++j) {
  3612. if (aciIndex == aclpb->aclpb_cache_result[j].aci_index) {
  3613. break;
  3614. }
  3615. }
  3616. if ( j < aclpb->aclpb_last_cache_result) {
  3617. /* already in cache */
  3618. } else if ( j < aclpb_max_cache_results-1) {
  3619. /* rbyrneXXX: make this same as other last_cache_result code! */
  3620. j = ++aclpb->aclpb_last_cache_result;
  3621. aclpb->aclpb_cache_result[j].aci_index = aci->aci_index;
  3622. aclpb->aclpb_cache_result[j].aci_ruleType = aci->aci_ruleType;
  3623. } else { /* No more space */
  3624. goto set_result_status;
  3625. }
  3626. /* Add the cached result status */
  3627. aclpb->aclpb_cache_result[j].result |= cache_result;
  3628. set_result_status:
  3629. if (result_status == ACL_RES_ALLOW) {
  3630. if (access & SLAPI_ACL_SEARCH)
  3631. /*wrong bit maskes were being used here--
  3632. a_eval->attrEval_s_status = ACLPB_CACHE_SEARCH_RES_ALLOW;*/
  3633. a_eval->attrEval_s_status = ACL_ATTREVAL_SUCCESS;
  3634. else
  3635. a_eval->attrEval_r_status = ACL_ATTREVAL_SUCCESS;
  3636. } else if ( result_status == ACL_RES_DENY) {
  3637. if (access & SLAPI_ACL_SEARCH)
  3638. a_eval->attrEval_s_status = ACL_ATTREVAL_FAIL;
  3639. else
  3640. a_eval->attrEval_r_status = ACL_ATTREVAL_FAIL;
  3641. } else {
  3642. /* Here, set it to recompute--try again later */
  3643. if (access & SLAPI_ACL_SEARCH)
  3644. a_eval->attrEval_s_status = ACL_ATTREVAL_RECOMPUTE;
  3645. else
  3646. a_eval->attrEval_r_status = ACL_ATTREVAL_RECOMPUTE;
  3647. result_status = -1;
  3648. }
  3649. return result_status;
  3650. }
  3651. static void
  3652. __acl_set_aclIndex_inResult ( Acl_PBlock *aclpb, int access, int index )
  3653. {
  3654. AclAttrEval *c_attrEval = aclpb->aclpb_curr_attrEval;
  3655. if ( c_attrEval ) {
  3656. if ( access & SLAPI_ACL_SEARCH )
  3657. c_attrEval->attrEval_s_aciIndex = index;
  3658. else if ( access & SLAPI_ACL_READ )
  3659. c_attrEval->attrEval_r_aciIndex = index;
  3660. }
  3661. }
  3662. /*
  3663. * If filter_sense is true then return (entry satisfies f).
  3664. * Otherwise, return !(entry satisfies f)
  3665. */
  3666. static int
  3667. acl__test_filter ( Slapi_Entry *entry, struct slapi_filter *f, int filter_sense) {
  3668. int filter_matched;
  3669. /* slapi_vattr_filter_test() returns 0 for match */
  3670. filter_matched = !slapi_vattr_filter_test(NULL, entry,
  3671. f,
  3672. 0 /*don't do acess chk*/);
  3673. if (filter_sense) {
  3674. return(filter_matched ? ACL_TRUE : ACL_FALSE);
  3675. } else {
  3676. return(filter_matched ? ACL_FALSE: ACL_TRUE);
  3677. }
  3678. }
  3679. /*
  3680. * Make an entry consisting of attr_type and attr_val and put
  3681. * a pointer to it in *entry.
  3682. * We will use this afterwards to test for against a filter.
  3683. */
  3684. static int
  3685. acl__make_filter_test_entry ( Slapi_Entry **entry, char *attr_type,
  3686. struct berval *attr_val) {
  3687. struct berval *vals_array[2];
  3688. vals_array[0] = attr_val;
  3689. vals_array[1] = NULL;
  3690. *entry = slapi_entry_alloc();
  3691. slapi_entry_init(*entry, NULL, NULL);
  3692. return (slapi_entry_add_values( *entry, (const char *)attr_type,
  3693. (struct berval**)&vals_array[0] ));
  3694. }
  3695. /*********************************************************************************/
  3696. /* E N D */
  3697. /*********************************************************************************/