acl.c 123 KB

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