acl.c 120 KB

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