acl.c 122 KB

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