acllas.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848
  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 <ipfstruct.h>
  7. #include "acl.h"
  8. /*
  9. A word on this file:
  10. The various routines here implement each component of the subject of an aci
  11. eg. "groupdn", "userdn","roledn", "userattr" etc.
  12. They are responsible for evaluating each individual keyword not for doing
  13. the boolean combination of these keywords, nor for combining multiple
  14. allow()/deny() statements--that's libaccess's job.
  15. For example, for "groupdn", DS_LASGroupDnEval might have to evaluate
  16. something like this:
  17. "groupdn = "ldap:///cn=G1,o=sun.com || ldap:///cn=G2,o=sun.com"
  18. The "=" here may be "!=" as well and these routines take care of the
  19. comparator.
  20. These rotuines get called via acl__TestRights(), which calls
  21. ACL_EvalTestRights() a libaccess routine (the immediately calling routine is
  22. ACLEvalAce() in oneeval.cpp).
  23. They should return LAS_EVAL_TRUE, if that keyword component evaluates to
  24. TRUE, LAS_EVAL_FALSE if it evaluates to FALSE and LAS_EVAL_FAIL if an
  25. error occurrs during evaluation. Note that once any component of a subject
  26. returns LAS_EVAL_FAIL, the evaluation in libaccess stops and the whole
  27. subject does not match and that aci is not applied.
  28. */
  29. /*
  30. A word on three-valued logic:
  31. In general when you do boolean combination of terms some of which
  32. may evaluate to UNDEFINED then you need to define what the combination
  33. means.
  34. So, for example libaccess implements a scheme which once UNDEFINED
  35. is returned for a term, it bales out of the
  36. evaluation and the whole expression evaluates to UNDEFINED.
  37. In this case the aci will not apply.
  38. On the other hand LDAP filters (cf. rfc2251 4.5.1) say that for OR,
  39. an expression will
  40. evaluate to TRUE if any term is TRUE, even if some terms are UNDEFINED.
  41. Other off the cuff options might be to redefine UNDEFINED to be FALSE,
  42. or TRUE.
  43. Which is best ?
  44. Well it probably depends on exactly what is to decided based on the
  45. evaluation of the logical expression. However, the final suggestion is
  46. almost certainly
  47. bad--you are unlikely to want to take an action based on an undefined
  48. result and
  49. defining UNDEFINED to be either TRUE or FALSE may result in the overall
  50. expression
  51. returning TRUE--a security hole. The only case this might work is if you
  52. are dealing with restricted
  53. expressions eg. terms may only be AND'ed togther--in this case defining
  54. UNDEFINED to be FALSE would guarantee a result of FALSE.
  55. The libaccess approach of returning UNDEFINED once an UNDEFINED is
  56. encountered during
  57. evaluation is not too bad--at least it guarantees that no aci will apply
  58. based on an
  59. undefined value. However, with an aci like this "...allow(all) A or B"
  60. where A returned UNDEFINED, you might be disappointed not to receive the
  61. rights if it was B that
  62. was granting you the rights and evaluation of A, which has nothing to do
  63. with you, returns UNDEFINED. In the case of an aci like
  64. "...deny(all) A or B" then the same
  65. situation is arguably a security hole. Note that this scheme also makes
  66. the final result
  67. dependent on the evaluation order and so if the evaluation engine does
  68. anything fancy internally (eg. reordering the terms in an OR so that fast
  69. to evaluate ones came first) then
  70. this would need to be documented so that a user (or a tool) could look at
  71. the external syntax and figure out the result of the evaluation.
  72. Also it breaks commutivity and De Morgans law.
  73. The LDAP filter scheme is starting to look good--it solves the problems of
  74. the
  75. libaccess approach, makes the final result of an expression independent of
  76. the evaluation order and
  77. gives you back commutivity of OR and AND. De Morgans is still broken, but
  78. that's because of the asymmetry of behaviour of UNDEFINED with OR and AND.
  79. So...?
  80. For acis, in general it can look like this:
  81. "...allow(rights)(LogicalCombinationofBindRule);
  82. deny(LogicalCombinationOfBindRule)...."
  83. A BindRule is one of the "userdn", "groupdn" or "userattr" things and it
  84. can look like this:
  85. "groupdn = "ldap:///cn=G1,o=sun.com || ldap:///cn=G2,o=sun.com"
  86. The "=" here may be "!=" as well and these routines take care of the
  87. comparator.
  88. For "userattr" keywords a mutilvalued attribute amounts a logical OR of the
  89. individual values. There is also a logical OR over the different levels
  90. as specified by the "parent" keyword.
  91. In fact there are three levels of logical combination:
  92. 1. In the aclplugin:
  93. The "||" and "!=" combinator for BindRule keywords like userdn and
  94. groupdn.
  95. The fact that for the "userattr" keyword, a mutilvalued attribute is
  96. evaluated as "||". Same for the different levels.
  97. 2. In libaccess:
  98. The logical combination of BindRules.
  99. 3. In libaccess:
  100. The evaluation of multiple BindRules seperated by ";", which means OR.
  101. The LDAP filter three-valued logic SHOULD be applied to each level but
  102. here's the way it works right now:
  103. 1. At this level it depends....
  104. DS_LASIpGetter - get attr for IP -
  105. returns ip address or LAS_EVAL_FAIL for error.
  106. no logical combination.
  107. DS_LASDnsGetter - get attr for DNS-
  108. returns dns name or LAS_EVAL_FAIL for error
  109. no logical combination.
  110. DS_LASUserDnEval - LAS Evaluation for USERDN -
  111. three-valued logic
  112. logical combination: || and !=
  113. DS_LASGroupDnEval - LAS Evaluation for GROUPDN -
  114. three-valued logic
  115. logical combination: || and !=
  116. DS_LASRoleDnEval - LAS Evaluation for ROLEDN -
  117. three-valued logic
  118. logical combination: || and !=
  119. DS_LASUserDnAttrEval - LAS Evaluation for USERDNATTR -
  120. three-valued logic
  121. logical combination || (over specified attribute values and
  122. parent keyword levels), !=
  123. DS_LASAuthMethodEval - LAS Evaluation for AUTHMETHOD -
  124. three-valued logic ( logical combinations: !=)
  125. DS_LASGroupDnAttrEval - LAS Evaluation for GROUPDNATTR -
  126. three-valued logic
  127. logical combination || (over specified attribute values and
  128. parent keyword levels), !=
  129. DS_LASUserAttrEval - LAS Evaluation for USERATTR -
  130. USER, GROUPDN and ROLEDN as above.
  131. LDAPURL -- three-valued logic (logical combinations: || over
  132. specified attribute vales, !=)
  133. attrname#attrvalue -- three-valued logic, logical combination:!=
  134. 2. The libaccess scheme applies at this level.
  135. 3. The LDAP filter three-valued logic applies at this level.
  136. Example of realistic, non-bizarre things that cause evaluation of a
  137. BindRule to be undefined are exceeding some resource limits (nesting level,
  138. lookthrough limit) in group membership evaluation, or trying to get ADD
  139. permission from the "userattr" keyword at "parent" level 0.
  140. Note that not everything that might be construed as an error needs to be
  141. taken as UNDEFINED. For example, things like not finding a user or an
  142. attribute in an entry can be defined away as TRUE or FALSE. eg. in an
  143. LDAP filter (cn=rob) applied to an entry where cn is not present is FALSE,
  144. not UNDEFINED. Similarly, if the number of levels in a parent keyword
  145. exceeds the allowed limit, we just ignore the rest--though this
  146. is a syntax error which should be detected at parse time.
  147. */
  148. /* To get around warning: declared in ldapserver/lib/ldaputil/ldaputili.h */
  149. extern int ldapu_member_certificate_match (void* cert, const char* desc);
  150. /****************************************************************************/
  151. /* Defines, Constants, ande Declarations */
  152. /****************************************************************************/
  153. static char* const type_objectClass = "objectclass";
  154. static char* const filter_groups = "(|(objectclass=groupOfNames) (objectclass=groupOfUniqueNames)(objectclass=groupOfCertificates)(objectclass=groupOfURLs))";
  155. static char* const type_member = "member";
  156. static char* const type_uniquemember = "uniquemember";
  157. static char* const type_memberURL = "memberURL";
  158. static char* const type_memberCert = "memberCertificateDescription";
  159. /* cache strategy for groups */
  160. #define ACLLAS_CACHE_MEMBER_GROUPS 0x1
  161. #define ACLLAS_CACHE_NOT_MEMBER_GROUPS 0x2
  162. #define ACLLAS_CACHE_ALL_GROUPS 0x3
  163. /****************************************************************************/
  164. /* prototypes */
  165. /****************************************************************************/
  166. static int acllas__handle_group_entry(Slapi_Entry *, void *);
  167. static int acllas__user_ismember_of_group(struct acl_pblock *aclpb,
  168. char* groupDN,
  169. char* clientDN,
  170. int cache_status,
  171. CERTCertificate *clientCert);
  172. static int acllas__user_has_role( struct acl_pblock *aclpb,
  173. Slapi_DN *roleDN, Slapi_DN *clientDn);
  174. static int acllas__add_allgroups (Slapi_Entry* e, void *callback_data);
  175. static int acllas__eval_memberGroupDnAttr (char *attrName,
  176. Slapi_Entry *e,
  177. char *n_clientdn,
  178. struct acl_pblock *aclpb);
  179. static int acllas__verify_client (Slapi_Entry* e, void *callback_data);
  180. static char* acllas__dn_parent( char *dn, int level);
  181. static int acllas__get_members (Slapi_Entry* e, void *callback_data);
  182. static int acllas__client_match_URL (struct acl_pblock *aclpb,
  183. char *n_dn, char *url );
  184. static int acllas__handle_client_search (Slapi_Entry *e, void *callback_data);
  185. static int __acllas_setup ( NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  186. char *attr_pattern, int *cachable, void **LAS_cookie,
  187. PList_t subject, PList_t resource, PList_t auth_info,
  188. PList_t global_auth, char *lasType, char *lasName, lasInfo *linfo);
  189. int
  190. aclutil_evaluate_macro( char * user, lasInfo *lasinfo,
  191. acl_eval_types evalType );
  192. static int
  193. acllas_eval_one_user( struct acl_pblock *aclpb,
  194. char * clientDN, char *userKeyword);
  195. static int
  196. acllas_eval_one_group(char *group, lasInfo *lasinfo);
  197. static int
  198. acllas_eval_one_role(char *role, lasInfo *lasinfo);
  199. static char **
  200. acllas_replace_dn_macro( char *rule, char *matched_val, lasInfo *lasinfo);
  201. static char **
  202. acllas_replace_attr_macro( char *rule, lasInfo *lasinfo);
  203. static int
  204. acllas_eval_one_target_filter( char * str, Slapi_Entry *e);
  205. /****************************************************************************/
  206. int
  207. DS_LASIpGetter(NSErr_t *errp, PList_t subject, PList_t resource, PList_t
  208. auth_info, PList_t global_auth, void *arg)
  209. {
  210. struct acl_pblock *aclpb = NULL;
  211. IPAddr_t ip=0;
  212. PRNetAddr client_praddr;
  213. struct in_addr client_addr;
  214. int rv;
  215. rv = ACL_GetAttribute(errp, DS_PROP_ACLPB, (void **)&aclpb,
  216. subject, resource, auth_info, global_auth);
  217. if ( rv != LAS_EVAL_TRUE || ( NULL == aclpb )) {
  218. acl_print_acllib_err(errp, NULL);
  219. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  220. "DS_LASIpGetter:Unable to get the ACLPB(%d)\n", rv,0,0);
  221. return LAS_EVAL_FAIL;
  222. }
  223. if ( slapi_pblock_get( aclpb->aclpb_pblock, SLAPI_CONN_CLIENTNETADDR,
  224. &client_praddr ) != 0 ) {
  225. slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Could not get client IP.\n" );
  226. return( LAS_EVAL_FAIL );
  227. }
  228. if ( !PR_IsNetAddrType(&client_praddr, PR_IpAddrV4Mapped) ) {
  229. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  230. "Client address is IPv6. ACLs only support IPv4 addresses so far.\n");
  231. return( LAS_EVAL_FAIL );
  232. }
  233. client_addr.s_addr = client_praddr.ipv6.ip.pr_s6_addr32[3];
  234. ip = (IPAddr_t) ntohl( client_addr.s_addr );
  235. rv = PListInitProp(subject, 0, ACL_ATTR_IP, (void *)ip, NULL);
  236. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  237. "Returning client ip address '%s'\n",
  238. (slapi_is_loglevel_set(SLAPI_LOG_ACL) ? inet_ntoa(client_addr) : ""));
  239. return LAS_EVAL_TRUE;
  240. }
  241. /*
  242. * This is called from the libaccess code when it needs to find a dns name.
  243. * It's called from ACL_GetAttribute() when it finds that ACL_ATTR_DNS is
  244. * not already part of the proplist.
  245. *
  246. */
  247. int
  248. DS_LASDnsGetter(NSErr_t *errp, PList_t subject, PList_t resource, PList_t
  249. auth_info, PList_t global_auth, void *arg)
  250. {
  251. struct acl_pblock *aclpb = NULL;
  252. PRNetAddr client_praddr;
  253. PRHostEnt *hp;
  254. char *dnsName = NULL;
  255. int rv;
  256. struct berval **clientDns;
  257. rv = ACL_GetAttribute(errp, DS_PROP_ACLPB, (void **)&aclpb,
  258. subject, resource, auth_info, global_auth);
  259. if ( rv != LAS_EVAL_TRUE || ( NULL == aclpb )) {
  260. acl_print_acllib_err(errp, NULL);
  261. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  262. "DS_LASDnsGetter:Unable to get the ACLPB(%d)\n", rv,0,0);
  263. return LAS_EVAL_FAIL;
  264. }
  265. if ( slapi_pblock_get( aclpb->aclpb_pblock, SLAPI_CLIENT_DNS, &clientDns ) != 0 ) {
  266. slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Could not get client IP.\n" );
  267. return( LAS_EVAL_FAIL );
  268. }
  269. /*
  270. * If the client hostname has already been put into the pblock then
  271. * use that. Otherwise we work it out and add it ourselves.
  272. * This info is connection-lifetime so with multiple operaitons on the same
  273. * connection we will only do the calculation once.
  274. *
  275. * rbyrneXXX surely this code would be better in connection.c so
  276. * the name would be just there waiting for us, and everyone else.
  277. *
  278. */
  279. if ( clientDns && clientDns[0] != NULL && clientDns[0]->bv_val ) {
  280. dnsName = clientDns[0]->bv_val;
  281. } else {
  282. struct berval **dnsList;
  283. char buf[PR_NETDB_BUF_SIZE];
  284. if ( slapi_pblock_get( aclpb->aclpb_pblock, SLAPI_CONN_CLIENTNETADDR, &client_praddr ) != 0 ) {
  285. slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Could not get client IP.\n" );
  286. return( LAS_EVAL_FAIL );
  287. }
  288. hp = (PRHostEnt *)slapi_ch_malloc( sizeof(PRHostEnt) );
  289. if ( PR_GetHostByAddr( &(client_praddr), (char *)buf, sizeof(buf), hp ) == PR_SUCCESS ) {
  290. if ( hp->h_name != NULL ) {
  291. dnsList = (struct berval**)
  292. slapi_ch_calloc (1, sizeof(struct berval*) * (1 + 1));
  293. *dnsList = (struct berval*)
  294. slapi_ch_calloc ( 1, sizeof(struct berval));
  295. dnsName = (*dnsList)->bv_val = slapi_ch_strdup( hp->h_name );
  296. (*dnsList)->bv_len = strlen ( (*dnsList)->bv_val );
  297. slapi_pblock_set( aclpb->aclpb_pblock, SLAPI_CLIENT_DNS, &dnsList );
  298. }
  299. }
  300. slapi_ch_free( (void **)&hp );
  301. }
  302. if ( NULL == dnsName ) return LAS_EVAL_FAIL;
  303. rv = PListInitProp(subject, 0, ACL_ATTR_DNS, dnsName, NULL);
  304. if (rv < 0) {
  305. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  306. "DS_LASDnsGetter:Couldn't set the DNS property(%d)\n", rv );
  307. return LAS_EVAL_FAIL;
  308. }
  309. slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "DNS name: %s\n", dnsName );
  310. return LAS_EVAL_TRUE;
  311. }
  312. /***************************************************************************/
  313. /* New LASes */
  314. /* */
  315. /* 1. user, groups. -- stubs to report errors. Not supported. */
  316. /* 2. userdn */
  317. /* 3. groupdn */
  318. /* 4. userdnattr */
  319. /* 5. authmethod */
  320. /* 6. groupdnattr */
  321. /* 7. roledn */
  322. /* */
  323. /* */
  324. /***************************************************************************/
  325. int
  326. DS_LASUserEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  327. char *attr_pattern, int *cachable, void **LAS_cookie,
  328. PList_t subject, PList_t resource, PList_t auth_info,
  329. PList_t global_auth)
  330. {
  331. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  332. "User LAS is not supported in the ACL\n",0,0,0);
  333. return LAS_EVAL_INVALID;
  334. }
  335. int
  336. DS_LASGroupEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  337. char *attr_pattern, int *cachable, void **LAS_cookie,
  338. PList_t subject, PList_t resource, PList_t auth_info,
  339. PList_t global_auth)
  340. {
  341. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  342. "Group LAS is not supported in the ACL\n",0,0,0);
  343. return LAS_EVAL_INVALID;
  344. }
  345. /***************************************************************************
  346. *
  347. * DS_LASUserDnEval
  348. * Evaluate the "userdn" LAS. See if the user has rights.
  349. *
  350. * Input:
  351. * attr_name The string "userdn" - in lower case.
  352. * comparator CMP_OP_EQ or CMP_OP_NE only
  353. * attr_pattern A comma-separated list of users
  354. * cachable Always set to FALSE.
  355. * subject Subject property list
  356. * resource Resource property list
  357. * auth_info Authentication info, if any
  358. *
  359. * Returns:
  360. * retcode The usual LAS return codes.
  361. *
  362. * Error Handling:
  363. * None.
  364. *
  365. **************************************************************************/
  366. int
  367. DS_LASUserDnEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  368. char *attr_pattern, int *cachable, void **LAS_cookie,
  369. PList_t subject, PList_t resource, PList_t auth_info,
  370. PList_t global_auth)
  371. {
  372. char *users = NULL;
  373. char *s_user, *user = NULL;
  374. char *ptr = NULL;
  375. char *end_dn = NULL;
  376. char *n_edn = NULL;
  377. char *parent_dn = NULL;
  378. int matched;
  379. int rc;
  380. short len;
  381. char *s = NULL;
  382. const size_t LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
  383. lasInfo lasinfo;
  384. int got_undefined = 0;
  385. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  386. attr_pattern,cachable,LAS_cookie,
  387. subject, resource, auth_info,global_auth,
  388. DS_LAS_USERDN, "DS_LASUserDnEval", &lasinfo )) ) {
  389. return LAS_EVAL_FAIL;
  390. }
  391. users = slapi_ch_strdup(attr_pattern);
  392. user = users;
  393. matched = ACL_FALSE;
  394. /* check if the clientdn is one of the users */
  395. while(user != 0 && *user != 0 && matched != ACL_TRUE ) {
  396. /* ignore leading whitespace */
  397. while(ldap_utf8isspace(user))
  398. LDAP_UTF8INC(user);
  399. /* Now we must see the userdn in the following
  400. ** formats:
  401. **
  402. ** The following formats are supported:
  403. **
  404. ** 1. The DN itself:
  405. ** allow (read) userdn = "ldap:///cn=prasanta, ..."
  406. **
  407. ** 2. keyword SELF:
  408. ** allow (write)
  409. ** userdn = "ldap:///self"
  410. **
  411. ** 3. Pattern:
  412. ** deny (read) userdn = "ldap:///cn=*, o=netscape, c = us";
  413. **
  414. ** 4. Anonymous user
  415. ** deny (read, write) userdn = "ldap:///anyone"
  416. **
  417. ** 5. All users (All authenticated users)
  418. ** allow (search) ** userdn = "ldap:///all"
  419. ** 6. parent "ldap:///parent"
  420. ** 7. Synamic users using the URL
  421. **
  422. **
  423. ** DNs must be separated by "||". Ex:
  424. ** allow (read)
  425. ** userdn = "ldap:///DN1 || ldap:///DN2"
  426. */
  427. /* The DN is now "ldap:///DN"
  428. ** remove the "ldap:///" part
  429. */
  430. if (strncasecmp (user, LDAP_URL_prefix,
  431. LDAP_URL_prefix_len) == 0) {
  432. s_user = user;
  433. user += LDAP_URL_prefix_len;
  434. } else {
  435. char ebuf[ BUFSIZ ];
  436. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  437. "DS_LASUserDnEval:Syntax error(%s)\n",
  438. escape_string_with_punctuation( user, ebuf ), 0,0);
  439. return LAS_EVAL_FAIL;
  440. }
  441. /* Now we have the starting point of the "userdn" */
  442. if ((end_dn = strstr(user, "||")) != NULL) {
  443. auto char *t = end_dn;
  444. LDAP_UTF8INC(end_dn);
  445. LDAP_UTF8INC(end_dn);
  446. *t = 0;
  447. }
  448. /* Now user is a null terminated string */
  449. if (*user) {
  450. while(ldap_utf8isspace(user))
  451. LDAP_UTF8INC(user);
  452. /* ignore trailing whitespace */
  453. len = strlen(user);
  454. ptr = user+len-1;
  455. while(ldap_utf8isspace(ptr)){ *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  456. }
  457. /*
  458. ** Check , if the user is a anonymous user. In that case
  459. ** We must find the rule "ldap:///anyone"
  460. */
  461. if (lasinfo.anomUser) {
  462. if (strcasecmp(user, "anyone") == 0 ) {
  463. /* matches -- anonymous user */
  464. matched = ACL_TRUE;
  465. break;
  466. }
  467. } else {
  468. /* URL format */
  469. if ((s = strstr (user, ACL_RULE_MACRO_DN_KEY)) != NULL ||
  470. (s = strstr (user, ACL_RULE_MACRO_DN_LEVELS_KEY)) != NULL ||
  471. (s = strstr (user, ACL_RULE_MACRO_ATTR_KEY)) != NULL) {
  472. matched = aclutil_evaluate_macro( s_user, &lasinfo,
  473. ACL_EVAL_USER);
  474. if (matched == ACL_TRUE) {
  475. break;
  476. }
  477. } else if ((s = strchr (user, '?'))!= NULL) {
  478. /* URL format */
  479. if (acllas__client_match_URL ( lasinfo.aclpb, lasinfo.clientDn,
  480. s_user) == ACL_TRUE) {
  481. matched = ACL_TRUE;
  482. break;
  483. }
  484. } else if (strcasecmp(user, "anyone") == 0 ) {
  485. /* Anyone means anyone in the world */
  486. matched = ACL_TRUE;
  487. break;
  488. } else if (strcasecmp(user, "self") == 0) {
  489. if (n_edn == NULL) {
  490. n_edn = slapi_entry_get_ndn ( lasinfo.resourceEntry );
  491. }
  492. if (slapi_utf8casecmp((ACLUCHP)lasinfo.clientDn, (ACLUCHP)n_edn) == 0)
  493. matched = ACL_TRUE;
  494. break;
  495. } else if (strcasecmp(user, "parent") == 0) {
  496. if (n_edn == NULL) {
  497. n_edn = slapi_entry_get_ndn ( lasinfo.resourceEntry );
  498. }
  499. /* get the parent */
  500. parent_dn = slapi_dn_parent(n_edn);
  501. if (parent_dn &&
  502. slapi_utf8casecmp ((ACLUCHP)lasinfo.clientDn, (ACLUCHP)parent_dn) == 0)
  503. matched = ACL_TRUE;
  504. if (parent_dn) slapi_ch_free ( (void **) &parent_dn );
  505. break;
  506. } else if (strcasecmp(user, "all") == 0) {
  507. /* matches -- */
  508. matched = ACL_TRUE;
  509. break;
  510. } else if (strchr(user, '*')) {
  511. char line[200];
  512. char *lineptr = &line[0];
  513. char *newline = NULL;
  514. int lenu = 0;
  515. Slapi_Filter *f = NULL;
  516. char *tt;
  517. int filterChoice;
  518. /*
  519. ** what we are doing is faking the str2simple()
  520. ** function with a "userdn = "user")
  521. */
  522. for (tt = user; *tt; tt++)
  523. *tt = TOLOWER ( *tt );
  524. if ((lenu = strlen(user)) > 190) { /* 200 - 9 for "(userdn=%s)" */
  525. newline = slapi_ch_malloc(lenu + 10);
  526. lineptr = newline;
  527. }
  528. sprintf (lineptr, "(userdn=%s)", user);
  529. if ((f = slapi_str2filter (lineptr)) == NULL) {
  530. if (newline) slapi_ch_free((void **) &newline);
  531. /* try the next one */
  532. break;
  533. }
  534. if (newline) slapi_ch_free((void **) &newline);
  535. filterChoice = slapi_filter_get_choice ( f );
  536. if (( filterChoice != LDAP_FILTER_SUBSTRINGS) &&
  537. ( filterChoice != LDAP_FILTER_PRESENT)) {
  538. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  539. "DS_LASUserDnEval:Error in gen. filter(%s)\n", user);
  540. }
  541. if ((rc = acl_match_substring( f,
  542. lasinfo.clientDn,
  543. 1 /*exact match */)
  544. ) == ACL_TRUE) {
  545. matched = ACL_TRUE;
  546. slapi_filter_free(f,1);
  547. break;
  548. }
  549. if (rc == ACL_ERR) {
  550. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  551. "DS_LASUserDnEval:Error in matching patteren(%s)\n",
  552. user,0,0);
  553. }
  554. slapi_filter_free(f,1);
  555. } else {
  556. /* Must be a simple dn then */
  557. if (slapi_utf8casecmp((ACLUCHP)lasinfo.clientDn,
  558. (ACLUCHP)slapi_dn_normalize(user)) == 0) {
  559. matched = ACL_TRUE;
  560. break;
  561. }
  562. }
  563. }
  564. if ( matched == ACL_DONT_KNOW ) {
  565. /* record this but keep going--maybe another user will evaluate to TRUE */
  566. got_undefined = 1;
  567. }
  568. /* Nothing matched -- try the next DN */
  569. user = end_dn;
  570. } /* end of while */
  571. slapi_ch_free ( (void **) &users);
  572. /*
  573. * If no terms were undefined, then evaluate as normal.
  574. * If there was an undefined term, but another one was TRUE, then we also evaluate
  575. * as normal. Otherwise, the whole expression is UNDEFINED.
  576. */
  577. if ( matched == ACL_TRUE || !got_undefined ) {
  578. if (comparator == CMP_OP_EQ) {
  579. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  580. } else {
  581. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  582. }
  583. } else {
  584. rc = LAS_EVAL_FAIL;
  585. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  586. "Returning UNDEFINED for userdn evaluation.\n");
  587. }
  588. return rc;
  589. }
  590. /***************************************************************************
  591. *
  592. * DS_LASGroupDnEval
  593. *
  594. *
  595. * Input:
  596. * attr_name The string "userdn" - in lower case.
  597. * comparator CMP_OP_EQ or CMP_OP_NE only
  598. * attr_pattern A comma-separated list of users
  599. * cachable Always set to FALSE.
  600. * subject Subject property list
  601. * resource Resource property list
  602. * auth_info Authentication info, if any
  603. *
  604. * Returns:
  605. * retcode The usual LAS return code
  606. * If the client is in any of the groups mentioned this groupdn keywrod
  607. * then returns LAS_EVAL_TRUE, if he's not in any LAS_EVAL_FALSE.
  608. * If any of the membership evaluations fail, then it goes on to evaluate the
  609. * others.
  610. *
  611. * Error Handling:
  612. * None.
  613. *
  614. **************************************************************************/
  615. int
  616. DS_LASGroupDnEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  617. char *attr_pattern, int *cachable, void **LAS_cookie,
  618. PList_t subject, PList_t resource, PList_t auth_info,
  619. PList_t global_auth)
  620. {
  621. char *groups;
  622. char *groupName;
  623. char *ptr;
  624. char *end_dn;
  625. int matched;
  626. int rc;
  627. int len;
  628. const size_t LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
  629. int any_group = 0;
  630. lasInfo lasinfo;
  631. int got_undefined = 0;
  632. /* the setup should not fail under normal operation */
  633. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  634. attr_pattern,cachable,LAS_cookie,
  635. subject, resource, auth_info,global_auth,
  636. DS_LAS_GROUPDN, "DS_LASGroupDnEval", &lasinfo )) ) {
  637. return LAS_EVAL_FAIL;
  638. }
  639. groups = slapi_ch_strdup(attr_pattern);
  640. groupName = groups;
  641. matched = ACL_FALSE;
  642. /* check if the groupdn is one of the users */
  643. while(groupName != 0 && *groupName != 0 && matched != ACL_TRUE) {
  644. /* ignore leading whitespace */
  645. while(ldap_utf8isspace(groupName))
  646. LDAP_UTF8INC(groupName);
  647. /*
  648. ** The syntax allowed for the groupdn is
  649. **
  650. ** Example:
  651. ** groupdn = "ldap:///dn1 || ldap:///dn2";
  652. **
  653. */
  654. if (strncasecmp (groupName, LDAP_URL_prefix,
  655. LDAP_URL_prefix_len) == 0) {
  656. groupName += LDAP_URL_prefix_len;
  657. } else {
  658. char ebuf[ BUFSIZ ];
  659. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  660. "DS_LASGroupDnEval:Syntax error(%s)\n",
  661. escape_string_with_punctuation( groupName, ebuf ),0,0);
  662. }
  663. /* Now we have the starting point of the "groupdn" */
  664. if ((end_dn = strstr(groupName, "||")) != NULL) {
  665. auto char *t = end_dn;
  666. LDAP_UTF8INC(end_dn);
  667. LDAP_UTF8INC(end_dn);
  668. *t = 0;
  669. }
  670. if (*groupName) {
  671. while(ldap_utf8isspace(groupName))
  672. LDAP_UTF8INC(groupName);
  673. /* ignore trailing whitespace */
  674. len = strlen(groupName);
  675. ptr = groupName+len-1;
  676. while(ldap_utf8isspace(ptr)) { *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  677. }
  678. /*
  679. ** Now we have the DN of the group. Evaluate the "clientdn"
  680. ** and see if the user is a member of the group.
  681. */
  682. if (0 == (strcasecmp(groupName, "anyone"))) {
  683. any_group = 1;
  684. }
  685. if (any_group) {
  686. /* anyone in the world */
  687. matched = ACL_TRUE;
  688. break;
  689. } else if ( lasinfo.anomUser &&
  690. (lasinfo.aclpb->aclpb_clientcert == NULL) && (!any_group)) {
  691. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  692. "Group not evaluated(%s)\n", groupName);
  693. break;
  694. } else {
  695. char *s;
  696. if ((s = strstr (groupName, ACL_RULE_MACRO_DN_KEY)) != NULL ||
  697. (s = strstr (groupName, ACL_RULE_MACRO_DN_LEVELS_KEY)) != NULL ||
  698. (s = strstr (groupName, ACL_RULE_MACRO_ATTR_KEY)) != NULL) {
  699. matched = aclutil_evaluate_macro( groupName, &lasinfo,
  700. ACL_EVAL_GROUP);
  701. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  702. "DS_LASGroupDnEval: Param group name:%s\n",
  703. groupName);
  704. } else {/* normal evaluation */
  705. matched = acllas_eval_one_group( groupName, &lasinfo);
  706. }
  707. if ( matched == ACL_TRUE ) {
  708. break;
  709. } else if ( matched == ACL_DONT_KNOW ) {
  710. /* record this but keep going--maybe another group will evaluate to TRUE */
  711. got_undefined = 1;
  712. }
  713. }
  714. /* Nothing matched -- try the next DN */
  715. groupName = end_dn;
  716. } /* end of while */
  717. /*
  718. * If no terms were undefined, then evaluate as normal.
  719. * If there was an undefined term, but another one was TRUE, then we also evaluate
  720. * as normal. Otherwise, the whole expression is UNDEFINED.
  721. */
  722. if ( matched == ACL_TRUE || !got_undefined ) {
  723. if (comparator == CMP_OP_EQ) {
  724. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  725. } else {
  726. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  727. }
  728. } else {
  729. rc = LAS_EVAL_FAIL;
  730. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  731. "Returning UNDEFINED for groupdn evaluation.\n");
  732. }
  733. slapi_ch_free ((void**) &groups);
  734. return rc;
  735. }
  736. /***************************************************************************
  737. *
  738. * DS_LASRoleDnEval
  739. *
  740. *
  741. * Input:
  742. * attr_name The string "roledn" - in lower case.
  743. * comparator CMP_OP_EQ or CMP_OP_NE only
  744. * attr_pattern A "||" sperated list of roles
  745. * cachable Always set to FALSE.
  746. * subject Subject property list
  747. * resource Resource property list
  748. * auth_info Authentication info, if any
  749. *
  750. * Returns:
  751. * retcode The usual LAS return codes.
  752. *
  753. * Error Handling:
  754. * None.
  755. *
  756. **************************************************************************/
  757. int
  758. DS_LASRoleDnEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  759. char *attr_pattern, int *cachable, void **LAS_cookie,
  760. PList_t subject, PList_t resource, PList_t auth_info,
  761. PList_t global_auth)
  762. {
  763. char *roles;
  764. char *role;
  765. char *ptr;
  766. char *end_dn;
  767. int matched;
  768. int rc;
  769. int len;
  770. const size_t LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
  771. int any_role = 0;
  772. lasInfo lasinfo;
  773. int got_undefined = 0;
  774. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  775. attr_pattern,cachable,LAS_cookie,
  776. subject, resource, auth_info,global_auth,
  777. DS_LAS_ROLEDN, "DS_LASRoleDnEval",
  778. &lasinfo )) ) {
  779. return LAS_EVAL_FALSE;
  780. }
  781. roles = slapi_ch_strdup(attr_pattern);
  782. role = roles;
  783. matched = ACL_FALSE;
  784. /* check if the roledn is one of the users */
  785. while(role != 0 && *role != 0 && matched != ACL_TRUE) {
  786. /* ignore leading whitespace */
  787. while(ldap_utf8isspace(role))
  788. LDAP_UTF8INC(role);
  789. /*
  790. ** The syntax allowed for the roledn is
  791. **
  792. ** Example:
  793. ** roledn = "ldap:///roledn1 || ldap:///roledn2";
  794. **
  795. */
  796. if (strncasecmp (role, LDAP_URL_prefix,
  797. LDAP_URL_prefix_len) == 0) {
  798. role += LDAP_URL_prefix_len;
  799. } else {
  800. char ebuf[ BUFSIZ ];
  801. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  802. "DS_LASRoleDnEval:Syntax error(%s)\n",
  803. escape_string_with_punctuation( role, ebuf ),0,0);
  804. }
  805. /* Now we have the starting point of the "roledn" */
  806. if ((end_dn = strstr(role, "||")) != NULL) {
  807. auto char *t = end_dn;
  808. LDAP_UTF8INC(end_dn);
  809. LDAP_UTF8INC(end_dn);
  810. *t = 0;
  811. }
  812. if (*role) {
  813. while(ldap_utf8isspace(role))
  814. LDAP_UTF8INC(role);
  815. /* ignore trailing whitespace */
  816. len = strlen(role);
  817. ptr = role+len-1;
  818. while(ldap_utf8isspace(ptr)) { *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  819. }
  820. /*
  821. ** Now we have the DN of the role. Evaluate the "clientdn"
  822. ** and see if the user has this role.
  823. */
  824. if (0 == (strcasecmp(role, "anyone"))) {
  825. any_role = 1;
  826. }
  827. if (any_role) {
  828. /* anyone in the world */
  829. matched = ACL_TRUE;
  830. break;
  831. } else if ( lasinfo.anomUser &&
  832. (lasinfo.aclpb->aclpb_clientcert == NULL) && (!any_role)) {
  833. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  834. "Role not evaluated(%s) for anon user\n", role);
  835. break;
  836. } else {
  837. /* Take care of param strings */
  838. char *s;
  839. if ((s = strstr (role, ACL_RULE_MACRO_DN_KEY)) != NULL ||
  840. (s = strstr (role, ACL_RULE_MACRO_DN_LEVELS_KEY)) != NULL ||
  841. (s = strstr (role, ACL_RULE_MACRO_ATTR_KEY)) != NULL) {
  842. matched = aclutil_evaluate_macro( role, &lasinfo,
  843. ACL_EVAL_ROLE);
  844. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  845. "DS_LASRoleDnEval: Param role name:%s\n",
  846. role);
  847. } else {/* normal evaluation */
  848. matched = acllas_eval_one_role( role, &lasinfo);
  849. }
  850. if ( matched == ACL_TRUE ) {
  851. break;
  852. } else if ( matched == ACL_DONT_KNOW ) {
  853. /* record this but keep going--maybe another role will evaluate to TRUE */
  854. got_undefined = 1;
  855. }
  856. }
  857. /* Nothing matched -- try the next DN */
  858. role = end_dn;
  859. } /* end of while */
  860. /*
  861. * If no terms were undefined, then evaluate as normal.
  862. * If there was an undefined term, but another one was TRUE, then we also evaluate
  863. * as normal. Otherwise, the whole expression is UNDEFINED.
  864. */
  865. if ( matched == ACL_TRUE || !got_undefined ) {
  866. if (comparator == CMP_OP_EQ) {
  867. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  868. } else {
  869. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  870. }
  871. } else {
  872. rc = LAS_EVAL_FAIL;
  873. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  874. "Returning UNDEFINED for roledn evaluation.\n");
  875. }
  876. slapi_ch_free ((void**) &roles);
  877. return rc;
  878. }
  879. /***************************************************************************
  880. *
  881. * DS_LASUserDnAttrEval
  882. *
  883. *
  884. * Input:
  885. * attr_name The string "userdn" - in lower case.
  886. * comparator CMP_OP_EQ or CMP_OP_NE only
  887. * attr_pattern A comma-separated list of users
  888. * cachable Always set to FALSE.
  889. * subject Subject property list
  890. * resource Resource property list
  891. * auth_info Authentication info, if any
  892. *
  893. * Returns:
  894. * retcode The usual LAS return codes.
  895. *
  896. * Error Handling:
  897. * None.
  898. *
  899. **************************************************************************/
  900. struct userdnattr_info {
  901. char *attr;
  902. int result;
  903. char *clientdn;
  904. };
  905. #define ACLLAS_MAX_LEVELS 10
  906. int
  907. DS_LASUserDnAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  908. char *attr_pattern, int *cachable, void **LAS_cookie,
  909. PList_t subject, PList_t resource, PList_t auth_info,
  910. PList_t global_auth)
  911. {
  912. char *n_currEntryDn = NULL;
  913. char *s_attrName, *attrName;
  914. char *ptr;
  915. int matched;
  916. int rc, len, i;
  917. char *val;
  918. Slapi_Attr *a;
  919. int levels[ACLLAS_MAX_LEVELS];
  920. int numOflevels =0;
  921. struct userdnattr_info info;
  922. char *attrs[2] = { LDAP_ALL_USER_ATTRS, NULL };
  923. lasInfo lasinfo;
  924. int got_undefined = 0;
  925. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  926. attr_pattern,cachable,LAS_cookie,
  927. subject, resource, auth_info,global_auth,
  928. DS_LAS_USERDNATTR, "DS_LASUserDnAttrEval",
  929. &lasinfo )) ) {
  930. return LAS_EVAL_FAIL;
  931. }
  932. /*
  933. ** The userdnAttr syntax is
  934. ** userdnattr = <attribute> or
  935. ** userdnattr = parent[0,2,4].attribute"
  936. ** Ex:
  937. ** userdnattr = manager; or
  938. ** userdnattr = "parent[0,2,4].manager";
  939. **
  940. ** Here 0 means current level, 2 means grandfather and
  941. ** 4 (great great grandfather)
  942. **
  943. ** The function of this LAS is to compare the value of the
  944. ** attribute in the Slapi_Entry with the "userdn".
  945. **
  946. ** Ex: userdn: "cn=prasanta, o= netscape, c= us"
  947. ** and in the Slapi_Entry the manager attribute has
  948. ** manager = <value>. Compare the userdn with manager.value to
  949. ** determine the result.
  950. **
  951. */
  952. s_attrName = attrName = slapi_ch_strdup (attr_pattern);
  953. /* ignore leading/trailing whitespace */
  954. while(ldap_utf8isspace(attrName)) LDAP_UTF8INC(attrName);
  955. len = strlen(attrName);
  956. ptr = attrName+len-1;
  957. while(ldap_utf8isspace(ptr)) { *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  958. /* See if we have a parent[2].attr" rule */
  959. if ( (ptr = strstr(attrName, "parent[")) != NULL) {
  960. char *word, *str, *next;
  961. numOflevels = 0;
  962. n_currEntryDn = slapi_entry_get_ndn ( lasinfo.resourceEntry );
  963. str = attrName;
  964. word = ldap_utf8strtok_r(str, "[],. ",&next);
  965. /* The first word is "parent[" and so it's not important */
  966. while ((word= ldap_utf8strtok_r(NULL, "[],.", &next)) != NULL) {
  967. if (ldap_utf8isdigit(word)) {
  968. while (word && ldap_utf8isspace(word)) LDAP_UTF8INC(word);
  969. if (numOflevels < ACLLAS_MAX_LEVELS)
  970. levels[numOflevels++] = atoi (word);
  971. else {
  972. /*
  973. * Here, ignore the extra levels..it's really
  974. * a syntax error which should have been ruled out at parse time
  975. */
  976. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  977. "DS_LASUserDnattr: Exceeded the ATTR LIMIT:%d: Ignoring extra levels\n",
  978. ACLLAS_MAX_LEVELS);
  979. }
  980. } else {
  981. /* Must be the attr name. We can goof of by
  982. ** having parent[1,2,a] but then you have to be
  983. ** stupid to do that.
  984. */
  985. char *p = word;
  986. if (*--p == '.') {
  987. attrName = word;
  988. break;
  989. }
  990. }
  991. }
  992. info.attr = attrName;
  993. info.clientdn = lasinfo.clientDn;
  994. info.result = 0;
  995. } else {
  996. levels[0] = 0;
  997. numOflevels = 1;
  998. }
  999. /* No attribute name specified--it's a syntax error and so undefined */
  1000. if (attrName == NULL ) {
  1001. slapi_ch_free ( (void**) &s_attrName);
  1002. return LAS_EVAL_FAIL;
  1003. }
  1004. slapi_log_error( SLAPI_LOG_ACL, plugin_name,"Attr:%s\n" , attrName, 0,0);
  1005. matched = ACL_FALSE;
  1006. for (i=0; i < numOflevels; i++) {
  1007. if ( levels[i] == 0 ) {
  1008. Slapi_Value *sval=NULL;
  1009. const struct berval *attrVal;
  1010. int j;
  1011. /*
  1012. * For the add operation, the resource itself (level 0)
  1013. * must never be allowed to grant access--
  1014. * This is because access would be granted based on a value
  1015. * of an attribute in the new entry--security hole.
  1016. *
  1017. */
  1018. if ( lasinfo.aclpb->aclpb_optype == SLAPI_OPERATION_ADD) {
  1019. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1020. "ACL info: userdnAttr does not allow ADD permission at level 0.\n");
  1021. got_undefined = 1;
  1022. continue;
  1023. }
  1024. slapi_entry_attr_find( lasinfo.resourceEntry, attrName, &a);
  1025. if ( NULL == a ) continue;
  1026. j= slapi_attr_first_value ( a,&sval );
  1027. while ( j != -1 ) {
  1028. attrVal = slapi_value_get_berval ( sval );
  1029. /* Here if atleast 1 value matches then we are done.*/
  1030. val = slapi_dn_normalize (
  1031. slapi_ch_strdup( attrVal->bv_val));
  1032. if (slapi_utf8casecmp((ACLUCHP)val, (ACLUCHP)lasinfo.clientDn ) == 0) {
  1033. char ebuf [ BUFSIZ ];
  1034. /* Wow it matches */
  1035. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1036. "userdnAttr matches(%s, %s) level (%d)\n",
  1037. val,
  1038. ACL_ESCAPE_STRING_WITH_PUNCTUATION (lasinfo.clientDn, ebuf),
  1039. 0);
  1040. matched = ACL_TRUE;
  1041. slapi_ch_free ( (void **) &val);
  1042. break;
  1043. }
  1044. slapi_ch_free ( (void**) &val);
  1045. j = slapi_attr_next_value ( a, j, &sval );
  1046. }
  1047. } else {
  1048. char *p_dn; /* parent dn */
  1049. p_dn = acllas__dn_parent (n_currEntryDn, levels[i]);
  1050. if (p_dn == NULL) continue;
  1051. /* use new search internal API */
  1052. {
  1053. Slapi_PBlock *aPb = slapi_pblock_new ();
  1054. /*
  1055. * This search may be chained if chaining for ACL is
  1056. * is enabled in the backend and the entry is in
  1057. * a chained backend.
  1058. */
  1059. slapi_search_internal_set_pb ( aPb,
  1060. p_dn,
  1061. LDAP_SCOPE_BASE,
  1062. "objectclass=*",
  1063. &attrs[0],
  1064. 0,
  1065. NULL /* controls */,
  1066. NULL /* uniqueid */,
  1067. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  1068. 0 /* actions */);
  1069. slapi_search_internal_callback_pb(aPb,
  1070. &info /* callback_data */,
  1071. NULL/* result_callback */,
  1072. acllas__verify_client,
  1073. NULL /* referral_callback */);
  1074. slapi_pblock_destroy(aPb);
  1075. }
  1076. /*
  1077. * Currently info.result is boolean so
  1078. * we do not need to check for ACL_DONT_KNOW
  1079. */
  1080. if (info.result) {
  1081. matched = ACL_TRUE;
  1082. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1083. "userdnAttr matches at level (%d)\n", levels[i]);
  1084. }
  1085. }
  1086. if (matched == ACL_TRUE) {
  1087. break;
  1088. }
  1089. }
  1090. slapi_ch_free ( (void **) &s_attrName);
  1091. /*
  1092. * If no terms were undefined, then evaluate as normal.
  1093. * If there was an undefined term, but another one was TRUE, then we also evaluate
  1094. * as normal. Otherwise, the whole expression is UNDEFINED.
  1095. */
  1096. if ( matched == ACL_TRUE || !got_undefined ) {
  1097. if (comparator == CMP_OP_EQ) {
  1098. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  1099. } else {
  1100. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  1101. }
  1102. } else {
  1103. rc = LAS_EVAL_FAIL;
  1104. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1105. "Returning UNDEFINED for userdnattr evaluation.\n");
  1106. }
  1107. return rc;
  1108. }
  1109. /***************************************************************************
  1110. *
  1111. * DS_LASAuthMethodEval
  1112. *
  1113. *
  1114. * Input:
  1115. * attr_name The string "authmethod" - in lower case.
  1116. * comparator CMP_OP_EQ or CMP_OP_NE only
  1117. * attr_pattern A comma-separated list of users
  1118. * cachable Always set to FALSE.
  1119. * subject Subject property list
  1120. * resource Resource property list
  1121. * auth_info Authentication info, if any
  1122. *
  1123. * Returns:
  1124. * retcode The usual LAS return codes.
  1125. *
  1126. * Error Handling:
  1127. * None.
  1128. *
  1129. **************************************************************************/
  1130. int
  1131. DS_LASAuthMethodEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  1132. char *attr_pattern, int *cachable, void **LAS_cookie,
  1133. PList_t subject, PList_t resource, PList_t auth_info,
  1134. PList_t global_auth)
  1135. {
  1136. char *attr;
  1137. char *ptr;
  1138. int len;
  1139. int matched;
  1140. int rc;
  1141. char *s = NULL;
  1142. lasInfo lasinfo;
  1143. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  1144. attr_pattern,cachable,LAS_cookie,
  1145. subject, resource, auth_info,global_auth,
  1146. DS_LAS_AUTHMETHOD, "DS_LASAuthMethodEval",
  1147. &lasinfo )) ) {
  1148. return LAS_EVAL_FAIL;
  1149. }
  1150. attr = attr_pattern;
  1151. matched = ACL_FALSE;
  1152. /* ignore leading whitespace */
  1153. s = strstr (attr, SLAPD_AUTH_SASL);
  1154. if ( s) {
  1155. s +=4;
  1156. attr = s;
  1157. }
  1158. while(ldap_utf8isspace(attr)) LDAP_UTF8INC(attr);
  1159. len = strlen(attr);
  1160. ptr = attr+len-1;
  1161. while(ldap_utf8isspace(ptr)) { *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  1162. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1163. "DS_LASAuthMethodEval:authtype:%s authmethod:%s\n",
  1164. lasinfo.authType, attr);
  1165. /* None method means, we don't care -- otherwise we care */
  1166. if ((strcasecmp(attr, "none") == 0) ||
  1167. (strcasecmp(attr, lasinfo.authType) == 0)) {
  1168. matched = ACL_TRUE;
  1169. }
  1170. if ( matched == ACL_TRUE || matched == ACL_FALSE) {
  1171. if (comparator == CMP_OP_EQ) {
  1172. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  1173. } else {
  1174. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  1175. }
  1176. } else {
  1177. rc = LAS_EVAL_FAIL;
  1178. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1179. "Returning UNDEFINED for authmethod evaluation.\n");
  1180. }
  1181. return rc;
  1182. }
  1183. /****************************************************************************
  1184. * Struct to evaluate and keep the current members being evaluated
  1185. *
  1186. * 0 1 2 3 4 5
  1187. * member: [a,b,c,d,e,f]
  1188. * c_idx may point to 2 i.e to "c" if "c" is being evaluated to
  1189. * see if any of "c" members is the clientDN.
  1190. * lu_idx points to the last used spot i.e 5.
  1191. * lu_idx++ is the next free spot.
  1192. *
  1193. * We allocate ACLLAS_MAX_GRP_MEMBER ptr first and then we add if it
  1194. * is required.
  1195. *
  1196. ***************************************************************************/
  1197. #define ACLLAS_MAX_GRP_MEMBER 50
  1198. struct member_info
  1199. {
  1200. char *member; /* member DN */
  1201. struct member_info *parent; /* parent of this member */
  1202. } member_info;
  1203. struct eval_info
  1204. {
  1205. int result; /* result status */
  1206. char *userDN; /* client's normalized DN */
  1207. int c_idx; /* Index to the current member being processed */
  1208. int lu_idx; /* Index to the slot where the last member is stored */
  1209. char **member; /* mmebers list */
  1210. struct member_info **memberInfo;/* array of memberInfo */
  1211. CERTCertificate *clientCert; /* ptr to cert */
  1212. struct acl_pblock *aclpb; /*aclpblock */
  1213. } eval_info;
  1214. static void
  1215. dump_member_info ( struct member_info *minfo, char *buf )
  1216. {
  1217. if ( minfo )
  1218. {
  1219. if ( minfo->parent )
  1220. {
  1221. dump_member_info ( minfo->parent, buf );
  1222. }
  1223. else
  1224. {
  1225. strcat ( buf, "<nil>" );
  1226. }
  1227. strcat ( buf, "->" );
  1228. strcat ( buf, minfo->member );
  1229. }
  1230. }
  1231. static void
  1232. dump_eval_info (char *caller, struct eval_info *info, int idx)
  1233. {
  1234. char buf[1024];
  1235. int len;
  1236. int i;
  1237. if ( idx < 0 )
  1238. {
  1239. sprintf ( buf, "\nuserDN=\"%s\"\nmember=", info->userDN);
  1240. if (info->member)
  1241. {
  1242. len = strlen (buf);
  1243. sprintf ( &(buf[len]), "\"%s\"", info->member );
  1244. }
  1245. len = strlen (buf);
  1246. sprintf ( &(buf[len]), "\nmemberinfo[%d]-[%d]:", info->c_idx, info->lu_idx );
  1247. if ( info->memberInfo )
  1248. for (i = 0; i <= info->lu_idx; i++)
  1249. {
  1250. len = strlen(buf);
  1251. sprintf ( &buf[len], "\n [%d]: ", i );
  1252. dump_member_info ( info->memberInfo[i], buf );
  1253. }
  1254. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "\n======== candidate member info in eval_info ========%s\n\n", buf );
  1255. }
  1256. else
  1257. {
  1258. sprintf (buf, "evaluated candidate [%d]=", idx);
  1259. switch (info->result)
  1260. {
  1261. case ACL_TRUE:
  1262. strcat (buf, "ACL_TRUE\n");
  1263. break;
  1264. case ACL_FALSE:
  1265. strcat (buf, "ACL_FALSE\n");
  1266. break;
  1267. case ACL_DONT_KNOW:
  1268. strcat (buf, "ACL_DONT_KNOW\n");
  1269. break;
  1270. default:
  1271. len = strlen (buf);
  1272. sprintf ( &(buf[len]), "%d\n", info->result );
  1273. break;
  1274. }
  1275. dump_member_info ( info->memberInfo[idx], buf );
  1276. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "%s\n", buf );
  1277. }
  1278. }
  1279. /***************************************************************************
  1280. *
  1281. * acllas__user_ismember_of_group
  1282. *
  1283. * Check if the user is a member of the group and nested groups..
  1284. *
  1285. * Input:
  1286. * char *groupdn - DN of the group
  1287. * char *clientDN - Dn of the client
  1288. *
  1289. * Returns:
  1290. * ACL_TRUE - the user is a member of the group.
  1291. * ACL_FALSE - Not a member
  1292. * ACL_DONT_KNOW - Any errors eg. resource limits exceeded and we could
  1293. * not compelte the evaluation.
  1294. *
  1295. * Error Handling:
  1296. * None.
  1297. *
  1298. **************************************************************************/
  1299. static int
  1300. acllas__user_ismember_of_group( struct acl_pblock *aclpb,
  1301. char* groupDN,
  1302. char* clientDN,
  1303. int cache_status,
  1304. CERTCertificate *clientCert)
  1305. {
  1306. char *attrs[5];
  1307. char *currDN;
  1308. int i,j;
  1309. int result = ACL_FALSE;
  1310. struct eval_info info;
  1311. int nesting_level;
  1312. int numOfMembersAtCurrentLevel;
  1313. int numOfMembersVisited;
  1314. int totalMembersVisited;
  1315. int numOfMembers;
  1316. int max_nestlevel;
  1317. int max_memberlimit;
  1318. aclUserGroup *u_group;
  1319. char ebuf [ BUFSIZ ];
  1320. struct member_info *groupMember = NULL;
  1321. struct member_info *parentGroup = NULL;
  1322. /*
  1323. ** First, Let's look thru the cached list and determine if the client is
  1324. ** a member of the cached list of groups.
  1325. */
  1326. if ( (u_group = aclg_get_usersGroup ( aclpb , clientDN )) == NULL) {
  1327. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1328. "Failed to find/allocate a usergroup--aborting evaluation\n", 0, 0);
  1329. return(ACL_DONT_KNOW);
  1330. }
  1331. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluating user %s in group %s?\n",
  1332. clientDN, groupDN );
  1333. /* Before I start using, get a reader lock on the group cache */
  1334. aclg_lock_groupCache ( 1 /* reader */ );
  1335. for ( i= 0; i < u_group->aclug_numof_member_group; i++) {
  1336. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "-- In %s\n",
  1337. u_group->aclug_member_groups[i] );
  1338. if ( slapi_utf8casecmp((ACLUCHP)groupDN, (ACLUCHP)u_group->aclug_member_groups[i]) == 0){
  1339. aclg_unlock_groupCache ( 1 /* reader */ );
  1340. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_TRUE\n");
  1341. return ACL_TRUE;
  1342. }
  1343. }
  1344. /* see if we know the client is not a member of a group. */
  1345. for ( i= 0; i < u_group->aclug_numof_notmember_group; i++) {
  1346. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "-- Not in %s\n",
  1347. u_group->aclug_notmember_groups[i] );
  1348. if ( slapi_utf8casecmp((ACLUCHP)groupDN, (ACLUCHP)u_group->aclug_notmember_groups[i]) == 0){
  1349. aclg_unlock_groupCache ( 1 /* reader */ );
  1350. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_FALSE\n");
  1351. return ACL_FALSE;
  1352. }
  1353. }
  1354. /*
  1355. ** That means we didn't find the the group in the cache. -- we have to add it
  1356. ** so no need for READ lock - need to get a WRITE lock. We will get it just before
  1357. ** modifying it.
  1358. */
  1359. aclg_unlock_groupCache ( 1 /* reader */ );
  1360. /* Indicate the initialization handler -- this module will be
  1361. ** called by the backend to evaluate the entry.
  1362. */
  1363. info.result = ACL_FALSE;
  1364. if (clientDN && *clientDN != '\0')
  1365. info.userDN = clientDN;
  1366. else
  1367. info.userDN = NULL;
  1368. info.c_idx = 0;
  1369. info.memberInfo = (struct member_info **) slapi_ch_malloc (ACLLAS_MAX_GRP_MEMBER * sizeof(struct member_info *));
  1370. groupMember = (struct member_info *) slapi_ch_malloc ( sizeof (struct member_info) );
  1371. groupMember->member = slapi_ch_strdup(groupDN);
  1372. groupMember->parent = NULL;
  1373. info.memberInfo[0] = groupMember;
  1374. info.lu_idx = 0;
  1375. attrs[0] = type_member;
  1376. attrs[1] = type_uniquemember;
  1377. attrs[2] = type_memberURL;
  1378. attrs[3] = type_memberCert;
  1379. attrs[4] = NULL;
  1380. currDN = groupMember->member;
  1381. /* nesting level is 0 to begin with */
  1382. nesting_level = 0;
  1383. numOfMembersVisited = 0;
  1384. totalMembersVisited = 0;
  1385. numOfMembersAtCurrentLevel = 1;
  1386. if (clientCert)
  1387. info.clientCert = clientCert;
  1388. else
  1389. info.clientCert = NULL;
  1390. info.aclpb = aclpb;
  1391. max_memberlimit = aclpb->aclpb_max_member_sizelimit;
  1392. max_nestlevel = aclpb->aclpb_max_nesting_level;
  1393. /* dump_eval_info ( "acllas__user_ismember_of_group", &info, -1 ); */
  1394. eval_another_member:
  1395. numOfMembers = info.lu_idx - info.c_idx;
  1396. /* Use new search internal API */
  1397. {
  1398. Slapi_PBlock * aPb = slapi_pblock_new ();
  1399. /*
  1400. * This search may NOT be chained--we demand that group
  1401. * definition be local.
  1402. */
  1403. slapi_search_internal_set_pb ( aPb,
  1404. currDN,
  1405. LDAP_SCOPE_BASE,
  1406. filter_groups,
  1407. &attrs[0],
  1408. 0,
  1409. NULL /* controls */,
  1410. NULL /* uniqueid */,
  1411. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  1412. SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
  1413. slapi_search_internal_callback_pb(aPb,
  1414. &info /* callback_data */,
  1415. NULL/* result_callback */,
  1416. acllas__handle_group_entry,
  1417. NULL /* referral_callback */);
  1418. if ( info.result == ACL_TRUE )
  1419. slapi_log_error( SLAPI_LOG_ACL, plugin_name,"-- In %s\n", info.memberInfo[info.c_idx]->member );
  1420. else if ( info.result == ACL_FALSE )
  1421. slapi_log_error( SLAPI_LOG_ACL, plugin_name,"-- Not in %s\n", info.memberInfo[info.c_idx]->member );
  1422. slapi_pblock_destroy (aPb);
  1423. }
  1424. if (info.result == ACL_TRUE) {
  1425. /*
  1426. ** that means the client is a member of the
  1427. ** group or one of the nested groups. We are done.
  1428. */
  1429. result = ACL_TRUE;
  1430. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_TRUE\n");
  1431. goto free_and_return;
  1432. }
  1433. numOfMembersVisited++;
  1434. if (numOfMembersVisited == numOfMembersAtCurrentLevel) {
  1435. /* This means we have looked at all the members for this level */
  1436. numOfMembersVisited = 0;
  1437. /* Now we are ready to look at the next level */
  1438. nesting_level++;
  1439. /* So, far we have visited ... */
  1440. totalMembersVisited += numOfMembersAtCurrentLevel;
  1441. /* How many members in the next level ? */
  1442. numOfMembersAtCurrentLevel =
  1443. info.lu_idx - totalMembersVisited +1;
  1444. }
  1445. if ((nesting_level > max_nestlevel)) {
  1446. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1447. "GroupEval:Member not found within the allowed nesting level (Allowed:%d Looked at:%d)\n",
  1448. max_nestlevel, nesting_level, 0);
  1449. result = ACL_DONT_KNOW; /* don't try to cache info based on this result */
  1450. goto free_and_return;
  1451. }
  1452. /* limit of -1 means "no limit */
  1453. if (info.c_idx > max_memberlimit &&
  1454. max_memberlimit != -1 ) {
  1455. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1456. "GroupEval:Looked at too many entries:(%d, %d)\n",
  1457. info.c_idx, info.lu_idx,0);
  1458. result = ACL_DONT_KNOW; /* don't try to cache info based on this result */
  1459. goto free_and_return;
  1460. }
  1461. if (info.lu_idx > info.c_idx) {
  1462. if (numOfMembers == (info.lu_idx - info.c_idx)) {
  1463. /* That means it's not a GROUP. It is just another
  1464. ** useless member which doesn't match. Remove the BAD dude.
  1465. */
  1466. groupMember = info.memberInfo[info.c_idx];
  1467. if (groupMember ) {
  1468. if ( groupMember->member ) slapi_ch_free ( (void **) &groupMember->member );
  1469. slapi_ch_free ( (void **) &groupMember );
  1470. info.memberInfo[info.c_idx] = NULL;
  1471. }
  1472. }
  1473. info.c_idx++;
  1474. /* Go thru the stack and see if we have already
  1475. ** evaluated this group. If we have, then skip it.
  1476. */
  1477. while (1) {
  1478. int evalNext=0;
  1479. int j;
  1480. if (info.c_idx > info.lu_idx) {
  1481. /* That means we have crossed the limit. We
  1482. ** may end of in this situation if we
  1483. ** have circular groups
  1484. */
  1485. info.c_idx = info.lu_idx;
  1486. goto free_and_return;
  1487. }
  1488. /* Break out of the loop if we have searched to the end */
  1489. groupMember = info.memberInfo[info.c_idx];
  1490. if ( (NULL == groupMember) || ((currDN = groupMember->member)!= NULL))
  1491. break;
  1492. for (j = 0; j < info.c_idx; j++) {
  1493. groupMember = info.memberInfo[j];
  1494. if (groupMember->member &&
  1495. (slapi_utf8casecmp((ACLUCHP)currDN, (ACLUCHP)groupMember->member) == 0)) {
  1496. /* Don't need the duplicate */
  1497. groupMember = info.memberInfo[info.c_idx];
  1498. slapi_ch_free ( (void **) &groupMember->member );
  1499. slapi_ch_free ( (void **) &groupMember );
  1500. info.memberInfo[info.c_idx] = NULL;
  1501. info.c_idx++;
  1502. evalNext=1;
  1503. break;
  1504. }
  1505. }
  1506. if (!evalNext) break;
  1507. }
  1508. /* Make sure that we have a valid DN to chug along */
  1509. groupMember = info.memberInfo[info.c_idx];
  1510. if ((info.c_idx <= info.lu_idx) && ((currDN = groupMember->member) != NULL))
  1511. goto eval_another_member;
  1512. }
  1513. free_and_return:
  1514. /* Remove the unnecessary members from the list which
  1515. ** we might have accumulated during the last execution
  1516. ** and we don't need to look at them.
  1517. */
  1518. i = info.c_idx;
  1519. i++;
  1520. while (i <= info.lu_idx) {
  1521. groupMember = info.memberInfo[i];
  1522. slapi_ch_free ( (void **) &groupMember->member );
  1523. slapi_ch_free ( (void **) &groupMember );
  1524. info.memberInfo[i] = NULL;
  1525. i++;
  1526. }
  1527. /*
  1528. ** Now we have a list which has all the groups
  1529. ** which we need to cache
  1530. */
  1531. info.lu_idx = info.c_idx;
  1532. /* since we are updating the groupcache, get a write lock */
  1533. aclg_lock_groupCache ( 2 /* writer */ );
  1534. /*
  1535. ** Keep the result of the evaluation in the cache.
  1536. ** We have 2 lists: member_of and not_member_of. We can use this
  1537. ** cached information next time we evaluate groups.
  1538. */
  1539. if (result == ACL_TRUE &&
  1540. (cache_status & ACLLAS_CACHE_MEMBER_GROUPS)) {
  1541. int ngr = 0;
  1542. /* get the last group which the user is a member of */
  1543. groupMember = info.memberInfo[info.c_idx];
  1544. while ( groupMember ) {
  1545. int already_cached = 0;
  1546. parentGroup = groupMember->parent;
  1547. for (j=0; j < u_group->aclug_numof_member_group;j++){
  1548. if (slapi_utf8casecmp( (ACLUCHP)groupMember->member,
  1549. (ACLUCHP)u_group->aclug_member_groups[j]) == 0) {
  1550. already_cached = 1;
  1551. break;
  1552. }
  1553. }
  1554. if (already_cached) {
  1555. groupMember = parentGroup;
  1556. parentGroup = NULL;
  1557. continue;
  1558. }
  1559. ngr = u_group->aclug_numof_member_group++;
  1560. if (u_group->aclug_numof_member_group >=
  1561. u_group->aclug_member_group_size){
  1562. u_group->aclug_member_groups =
  1563. (char **) slapi_ch_realloc (
  1564. (void *) u_group->aclug_member_groups,
  1565. (u_group->aclug_member_group_size +
  1566. ACLUG_INCR_GROUPS_LIST) *
  1567. sizeof (char *));
  1568. u_group->aclug_member_group_size +=
  1569. ACLUG_INCR_GROUPS_LIST;
  1570. }
  1571. u_group->aclug_member_groups[ngr] = slapi_ch_strdup ( groupMember->member );
  1572. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  1573. "Adding Group (%s) ParentGroup (%s) to the IN GROUP List\n",
  1574. groupMember->member , parentGroup ? parentGroup->member: "NULL");
  1575. groupMember = parentGroup;
  1576. parentGroup = NULL;
  1577. }
  1578. } else if (result == ACL_FALSE &&
  1579. (cache_status & ACLLAS_CACHE_NOT_MEMBER_GROUPS)) {
  1580. int ngr = 0;
  1581. /* NOT IN THE GROUP LIST */
  1582. /* get the last group which the user is a member of */
  1583. groupMember = info.memberInfo[info.c_idx];
  1584. while ( groupMember ) {
  1585. int already_cached = 0;
  1586. parentGroup = groupMember->parent;
  1587. for (j=0; j < u_group->aclug_numof_notmember_group;j++){
  1588. if (slapi_utf8casecmp( (ACLUCHP)groupMember->member,
  1589. (ACLUCHP)u_group->aclug_notmember_groups[j]) == 0) {
  1590. already_cached = 1;
  1591. break;
  1592. }
  1593. }
  1594. if (already_cached) {
  1595. groupMember = parentGroup;
  1596. parentGroup = NULL;
  1597. continue;
  1598. }
  1599. ngr = u_group->aclug_numof_notmember_group++;
  1600. if (u_group->aclug_numof_notmember_group >=
  1601. u_group->aclug_notmember_group_size){
  1602. u_group->aclug_notmember_groups =
  1603. (char **) slapi_ch_realloc (
  1604. (void *) u_group->aclug_notmember_groups,
  1605. (u_group->aclug_notmember_group_size +
  1606. ACLUG_INCR_GROUPS_LIST) *
  1607. sizeof (char *));
  1608. u_group->aclug_notmember_group_size +=
  1609. ACLUG_INCR_GROUPS_LIST;
  1610. }
  1611. u_group->aclug_notmember_groups[ngr] = slapi_ch_strdup ( groupMember->member );
  1612. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  1613. "Adding Group (%s) ParentGroup (%s) to the NOT IN GROUP List\n",
  1614. groupMember->member , parentGroup ? parentGroup->member: "NULL");
  1615. groupMember = parentGroup;
  1616. parentGroup = NULL;
  1617. }
  1618. } else if ( result == ACL_DONT_KNOW ) {
  1619. /*
  1620. * We terminated the search without reaching a conclusion--so
  1621. * don't cache any info based on this evaluation.
  1622. */
  1623. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_DONT_KNOW\n");
  1624. }
  1625. /* Unlock the group cache, we are done with updating */
  1626. aclg_unlock_groupCache ( 2 /* writer */ );
  1627. for (i=0; i <= info.lu_idx; i++) {
  1628. groupMember = info.memberInfo[i];
  1629. if ( NULL == groupMember ) continue;
  1630. slapi_ch_free ( (void **) &groupMember->member );
  1631. slapi_ch_free ( (void **) &groupMember );
  1632. }
  1633. /* free the pointer array.*/
  1634. slapi_ch_free ( (void **) &info.memberInfo);
  1635. return result;
  1636. }
  1637. /***************************************************************************
  1638. *
  1639. * acllas__handle_group_entry
  1640. *
  1641. * handler called. Compares the userdn value and determines if it's
  1642. * a member of not.
  1643. *
  1644. * Input:
  1645. *
  1646. *
  1647. * Returns:
  1648. *
  1649. * Error Handling:
  1650. *
  1651. **************************************************************************/
  1652. static int
  1653. acllas__handle_group_entry (Slapi_Entry* e, void *callback_data)
  1654. {
  1655. struct eval_info *info;
  1656. Slapi_Attr *currAttr, *nextAttr;
  1657. char *n_dn, *attrType;
  1658. short n;
  1659. int i;
  1660. info = (struct eval_info *) callback_data;
  1661. info->result = ACL_FALSE;
  1662. if (e == NULL) {
  1663. return 0;
  1664. }
  1665. slapi_entry_first_attr ( e, &currAttr);
  1666. if ( NULL == currAttr ) return 0;
  1667. slapi_attr_get_type ( currAttr, &attrType );
  1668. if (NULL == attrType ) return 0;
  1669. do {
  1670. Slapi_Value *sval=NULL;
  1671. const struct berval *attrVal;
  1672. if ((strcasecmp (attrType, type_member) == 0) ||
  1673. (strcasecmp (attrType, type_uniquemember) == 0 )) {
  1674. i = slapi_attr_first_value ( currAttr,&sval );
  1675. while ( i != -1 ) {
  1676. struct member_info *groupMember = NULL;
  1677. attrVal = slapi_value_get_berval ( sval );
  1678. n_dn = slapi_dn_normalize ( slapi_ch_strdup( attrVal->bv_val));
  1679. info->lu_idx++;
  1680. n = info->lu_idx;
  1681. if (!(n % ACLLAS_MAX_GRP_MEMBER)) {
  1682. info->memberInfo = (struct member_info **) slapi_ch_realloc(
  1683. (void *) info->memberInfo,
  1684. (n+ACLLAS_MAX_GRP_MEMBER) *
  1685. sizeof(struct eval_info *));
  1686. }
  1687. /* allocate the space for the member and attch it to the list */
  1688. groupMember = (struct member_info *) slapi_ch_malloc ( sizeof ( struct member_info ) );
  1689. groupMember->member = n_dn;
  1690. groupMember->parent = info->memberInfo[info->c_idx];
  1691. info->memberInfo[n] = groupMember;
  1692. if (info->userDN &&
  1693. slapi_utf8casecmp((ACLUCHP)n_dn, (ACLUCHP)info->userDN) == 0) {
  1694. info->result = ACL_TRUE;
  1695. return 0;
  1696. }
  1697. i = slapi_attr_next_value ( currAttr, i, &sval );
  1698. }
  1699. /* Evaluate Dynamic groups */
  1700. } else if (strcasecmp ( attrType, type_memberURL) == 0) {
  1701. char *memberURL, *savURL;
  1702. if (!info->userDN) continue;
  1703. i= slapi_attr_first_value ( currAttr,&sval );
  1704. while ( i != -1 ) {
  1705. attrVal = slapi_value_get_berval ( sval );
  1706. /*
  1707. * memberURL may start with "ldap:///" or "ldap://host:port"
  1708. * ldap://localhost:11000/o=ace industry,c=us??
  1709. * or
  1710. * ldap:///o=ace industry,c=us??
  1711. */
  1712. if (strncasecmp( attrVal->bv_val, "ldap://",7) == 0 ||
  1713. strncasecmp( attrVal->bv_val, "ldaps://",8) == 0) {
  1714. savURL = memberURL = slapi_ch_strdup ( attrVal->bv_val);
  1715. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1716. "ACL Group Eval:MemberURL:%s\n", memberURL);
  1717. info->result = acllas__client_match_URL (
  1718. info->aclpb,
  1719. info->userDN,
  1720. memberURL);
  1721. slapi_ch_free ( (void**) &savURL);
  1722. if (info->result == ACL_TRUE)
  1723. return 0;
  1724. } else {
  1725. /* This means that the URL is ill-formed */
  1726. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1727. "ACL Group Eval:Badly Formed MemberURL:%s\n", attrVal->bv_val);
  1728. }
  1729. i = slapi_attr_next_value ( currAttr, i, &sval );
  1730. }
  1731. /* Evaluate Fortezza groups */
  1732. } else if ((strcasecmp (attrType, type_memberCert) == 0) ) {
  1733. /* Do we have the certificate around */
  1734. if (!info->clientCert) {
  1735. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1736. " acllas__handle_group_entry:Client Cert missing\n" );
  1737. continue;
  1738. }
  1739. i = slapi_attr_first_value ( currAttr,&sval );
  1740. while ( i != -1 ) {
  1741. attrVal = slapi_value_get_berval ( sval );
  1742. if (ldapu_member_certificate_match (
  1743. info->clientCert,
  1744. attrVal->bv_val) == LDAP_SUCCESS) {
  1745. info->result = ACL_TRUE;
  1746. return 0;
  1747. }
  1748. i = slapi_attr_next_value ( currAttr, i, &sval );
  1749. }
  1750. }
  1751. attrType = NULL;
  1752. /* get the next attr */
  1753. slapi_entry_next_attr ( e, currAttr, &nextAttr );
  1754. if ( NULL == nextAttr ) break;
  1755. currAttr = nextAttr;
  1756. slapi_attr_get_type ( currAttr, &attrType );
  1757. } while ( NULL != attrType );
  1758. return 0;
  1759. }
  1760. /***************************************************************************
  1761. *
  1762. * DS_LASGroupDnAttrEval
  1763. *
  1764. *
  1765. * Input:
  1766. * attr_name The string "groupdnattr" - in lower case.
  1767. * comparator CMP_OP_EQ or CMP_OP_NE only
  1768. * attr_pattern A comma-separated list of users
  1769. * cachable Always set to FALSE.
  1770. * subject Subject property list
  1771. * resource Resource property list
  1772. * auth_info Authentication info, if any
  1773. *
  1774. * Returns:
  1775. * retcode The usual LAS return codes.
  1776. *
  1777. * Error Handling:
  1778. * None.
  1779. *
  1780. **************************************************************************/
  1781. struct groupdnattr_info
  1782. {
  1783. char *attrName; /* name of the attribute */
  1784. int numofGroups; /* number of groups */
  1785. char **member;
  1786. };
  1787. int
  1788. DS_LASGroupDnAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  1789. char *attr_pattern, int *cachable, void **LAS_cookie,
  1790. PList_t subject, PList_t resource, PList_t auth_info,
  1791. PList_t global_auth)
  1792. {
  1793. char *s_attrName = NULL;
  1794. char *attrName;
  1795. char *ptr;
  1796. int matched;
  1797. int rc;
  1798. int len;
  1799. Slapi_Attr *attr;
  1800. int levels[ACLLAS_MAX_LEVELS];
  1801. int numOflevels = 0;
  1802. char *n_currEntryDn = NULL;
  1803. lasInfo lasinfo;
  1804. int got_undefined = 0;
  1805. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  1806. attr_pattern,cachable,LAS_cookie,
  1807. subject, resource, auth_info,global_auth,
  1808. DS_LAS_GROUPDNATTR, "DS_LASGroupDnAttrEval",
  1809. &lasinfo )) ) {
  1810. return LAS_EVAL_FAIL;
  1811. }
  1812. /* For anonymous client, the answer is XXX come back to this */
  1813. if ( lasinfo.anomUser )
  1814. return LAS_EVAL_FALSE;
  1815. /*
  1816. ** The groupdnAttr syntax is
  1817. ** groupdnattr = <attribute>
  1818. ** Ex:
  1819. ** groupdnattr = SIEmanager;
  1820. **
  1821. ** The function of this LAS is to find out if the client belongs
  1822. ** to any group that is specified in the attr.
  1823. */
  1824. attrName = attr_pattern;
  1825. if (strstr(attrName, LDAP_URL_prefix)) {
  1826. char *s;
  1827. /* In this case "grppupdnattr="ldap:///base??attr" */
  1828. if ((s = strstr (attrName, ACL_RULE_MACRO_DN_KEY)) != NULL ||
  1829. (s = strstr (attrName, ACL_RULE_MACRO_DN_LEVELS_KEY)) != NULL ||
  1830. (s = strstr (attrName, ACL_RULE_MACRO_ATTR_KEY)) != NULL) {
  1831. matched = aclutil_evaluate_macro( attrName, &lasinfo,
  1832. ACL_EVAL_GROUPDNATTR);
  1833. } else{
  1834. matched = acllas__eval_memberGroupDnAttr(attrName,
  1835. lasinfo.resourceEntry,
  1836. lasinfo.clientDn,
  1837. lasinfo.aclpb);
  1838. }
  1839. if ( matched == ACL_DONT_KNOW) {
  1840. got_undefined = 1;
  1841. }
  1842. } else {
  1843. int i;
  1844. char *n_groupdn;
  1845. /* ignore leading/trailing whitespace */
  1846. while(ldap_utf8isspace(attrName)) LDAP_UTF8INC(attrName);
  1847. len = strlen(attrName);
  1848. ptr = attrName+len-1;
  1849. while(ldap_utf8isspace(ptr)) { *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  1850. slapi_log_error( SLAPI_LOG_ACL, plugin_name,"Attr:%s\n" , attrName, 0,0);
  1851. /* See if we have a parent[2].attr" rule */
  1852. if ( (ptr = strstr(attrName, "parent[")) != NULL) {
  1853. char *word, *str, *next;
  1854. numOflevels = 0;
  1855. n_currEntryDn = slapi_entry_get_ndn ( lasinfo.resourceEntry ) ;
  1856. s_attrName = attrName = slapi_ch_strdup ( attr_pattern );
  1857. str = attrName;
  1858. word = ldap_utf8strtok_r(str, "[],. ",&next);
  1859. /* The first word is "parent[" and so it's not important */
  1860. while ((word= ldap_utf8strtok_r(NULL, "[],.", &next)) != NULL) {
  1861. if (ldap_utf8isdigit(word)) {
  1862. while (word && ldap_utf8isspace(word)) LDAP_UTF8INC(word);
  1863. if (numOflevels < ACLLAS_MAX_LEVELS)
  1864. levels[numOflevels++] = atoi (word);
  1865. else {
  1866. /*
  1867. * Here, ignore the extra levels..it's really
  1868. * a syntax error which should have been ruled out at parse time
  1869. */
  1870. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  1871. "DS_LASGroupDnattr: Exceeded the ATTR LIMIT:%d: Ignoring extra levels\n",
  1872. ACLLAS_MAX_LEVELS,0,0);
  1873. }
  1874. } else {
  1875. /* Must be the attr name. We can goof of by
  1876. ** having parent[1,2,a] but then you have to be
  1877. ** stupid to do that.
  1878. */
  1879. char *p = word;
  1880. if (*--p == '.') {
  1881. attrName = word;
  1882. break;
  1883. }
  1884. }
  1885. }
  1886. } else {
  1887. levels[0] = 0;
  1888. numOflevels = 1;
  1889. }
  1890. matched = ACL_FALSE;
  1891. for (i=0; i < numOflevels; i++) {
  1892. if ( levels[i] == 0 ) {
  1893. Slapi_Value *sval=NULL;
  1894. const struct berval *attrVal;
  1895. int attr_i;
  1896. /*
  1897. * For the add operation, the resource itself (level 0)
  1898. * must never be allowed to grant access--
  1899. * This is because access would be granted based on a value
  1900. * of an attribute in the new entry--security hole.
  1901. * XXX is this therefore FALSE or DONT_KNOW ?
  1902. */
  1903. if ( lasinfo.aclpb->aclpb_optype == SLAPI_OPERATION_ADD) {
  1904. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1905. "ACL info: groupdnAttr does not allow ADD permission at level 0.\n");
  1906. got_undefined = 1;
  1907. continue;
  1908. }
  1909. slapi_entry_attr_find ( lasinfo.resourceEntry, attrName, &attr);
  1910. if ( !attr) continue;
  1911. attr_i= slapi_attr_first_value ( attr,&sval );
  1912. while ( attr_i != -1 ) {
  1913. attrVal = slapi_value_get_berval ( sval );
  1914. n_groupdn = slapi_dn_normalize(
  1915. slapi_ch_strdup( attrVal->bv_val));
  1916. matched = acllas__user_ismember_of_group (
  1917. lasinfo.aclpb, n_groupdn, lasinfo.clientDn,
  1918. ACLLAS_CACHE_MEMBER_GROUPS,
  1919. lasinfo.aclpb->aclpb_clientcert);
  1920. slapi_ch_free ( (void **) &n_groupdn);
  1921. if (matched == ACL_TRUE ) {
  1922. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1923. "groupdnattr matches at level (%d)\n", levels[i]);
  1924. break;
  1925. } else if ( matched == ACL_DONT_KNOW ) {
  1926. /* record this but keep going--maybe another group will evaluate to TRUE */
  1927. got_undefined = 1;
  1928. }
  1929. attr_i= slapi_attr_next_value ( attr, attr_i, &sval );
  1930. }
  1931. } else {
  1932. char *p_dn;
  1933. struct groupdnattr_info info;
  1934. char *attrs[2];
  1935. int j;
  1936. info.numofGroups = 0;
  1937. attrs[0] = info.attrName = attrName;
  1938. attrs[1] = NULL;
  1939. p_dn = acllas__dn_parent (n_currEntryDn, levels[i]);
  1940. if (p_dn == NULL) continue;
  1941. /* Use new search internal API */
  1942. {
  1943. Slapi_PBlock *aPb = slapi_pblock_new ();
  1944. /*
  1945. * This search may NOT be chained--if the user's definition is
  1946. * remote and the group is dynamic and the user entry
  1947. * changes then we would not notice--so don't go
  1948. * find the user entry in the first place.
  1949. */
  1950. slapi_search_internal_set_pb ( aPb,
  1951. p_dn,
  1952. LDAP_SCOPE_BASE,
  1953. "objectclass=*",
  1954. &attrs[0],
  1955. 0,
  1956. NULL /* controls */,
  1957. NULL /* uniqueid */,
  1958. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  1959. SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
  1960. slapi_search_internal_callback_pb(aPb,
  1961. &info /* callback_data */,
  1962. NULL/* result_callback */,
  1963. acllas__get_members,
  1964. NULL /* referral_callback */);
  1965. slapi_pblock_destroy (aPb);
  1966. }
  1967. if (info.numofGroups <= 0) {
  1968. continue;
  1969. }
  1970. for (j=0; j <info.numofGroups; j++) {
  1971. if (slapi_utf8casecmp((ACLUCHP)info.member[j],
  1972. (ACLUCHP)lasinfo.clientDn) == 0) {
  1973. matched = ACL_TRUE;
  1974. break;
  1975. }
  1976. matched = acllas__user_ismember_of_group (
  1977. lasinfo.aclpb, info.member[j],
  1978. lasinfo.clientDn, ACLLAS_CACHE_ALL_GROUPS,
  1979. lasinfo.aclpb->aclpb_clientcert);
  1980. if (matched == ACL_TRUE) {
  1981. break;
  1982. } else if ( matched == ACL_DONT_KNOW ) {
  1983. /* record this but keep going--maybe another group will evaluate to TRUE */
  1984. got_undefined = 1;
  1985. }
  1986. }
  1987. /* Deallocate the member array and the member struct */
  1988. for (j=0; j < info.numofGroups; j++)
  1989. slapi_ch_free ((void **) &info.member[j]);
  1990. slapi_ch_free ((void **) &info.member);
  1991. }
  1992. if (matched == ACL_TRUE) {
  1993. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  1994. "groupdnattr matches at level (%d)\n", levels[i]);
  1995. break;
  1996. } else if ( matched == ACL_DONT_KNOW ) {
  1997. /* record this but keep going--maybe another group at another level
  1998. * will evaluate to TRUE.
  1999. */
  2000. got_undefined = 1;
  2001. }
  2002. } /* NumofLevels */
  2003. }
  2004. if (s_attrName) slapi_ch_free ((void**) &s_attrName );
  2005. /*
  2006. * If no terms were undefined, then evaluate as normal.
  2007. * If there was an undefined term, but another one was TRUE, then we also evaluate
  2008. * as normal. Otherwise, the whole expression is UNDEFINED.
  2009. */
  2010. if ( matched == ACL_TRUE || !got_undefined ) {
  2011. if (comparator == CMP_OP_EQ) {
  2012. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  2013. } else {
  2014. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  2015. }
  2016. } else {
  2017. rc = LAS_EVAL_FAIL;
  2018. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2019. "Returning UNDEFINED for groupdnattr evaluation.\n");
  2020. }
  2021. return rc;
  2022. }
  2023. /*
  2024. * acllas__eval_memberGroupDnAttr
  2025. *
  2026. * return ACL_TRUE, ACL_FALSE or ACL_DONT_KNOW
  2027. *
  2028. * Inverse group evaluation. Find all the groups that the user is a
  2029. * member of. Find all teh groups that contain those groups. Do an
  2030. * upward nested level search. By the end of it, we will know all the
  2031. * groups that the clinet is a member of under that search scope.
  2032. *
  2033. * This model seems to be very fast if we have few groups at the
  2034. * leaf level.
  2035. *
  2036. */
  2037. static int
  2038. acllas__eval_memberGroupDnAttr (char *attrName, Slapi_Entry *e,
  2039. char *n_clientdn, struct acl_pblock *aclpb)
  2040. {
  2041. Slapi_Attr *attr;
  2042. char *s, *p;
  2043. char *str, *s_str, *base, *groupattr;
  2044. int i,j,k,matched, enumerate_groups;
  2045. aclUserGroup *u_group;
  2046. char ebuf [ BUFSIZ ];
  2047. Slapi_Value *sval=NULL;
  2048. const struct berval *attrVal;
  2049. /* Parse the URL -- We can't use the ldap_url_parse()
  2050. ** we don't follow thw complete url naming scheme
  2051. */
  2052. s_str = str = slapi_ch_strdup(attrName);
  2053. while (str && ldap_utf8isspace(str)) LDAP_UTF8INC( str );
  2054. str +=8;
  2055. s = strchr (str, '?');
  2056. if (s) {
  2057. p = s;
  2058. p++;
  2059. *s = '\0';
  2060. base = str;
  2061. s = strchr (p, '?');
  2062. if (s) *s = '\0';
  2063. groupattr = p;
  2064. } else {
  2065. slapi_ch_free ( (void **)&s_str );
  2066. return ACL_FALSE;
  2067. }
  2068. if ( (u_group = aclg_get_usersGroup ( aclpb , n_clientdn )) == NULL) {
  2069. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2070. "Failed to find/allocate a usergroup--aborting evaluation\n", 0, 0);
  2071. slapi_ch_free ( (void **)&s_str );
  2072. return(ACL_DONT_KNOW);
  2073. }
  2074. /*
  2075. ** First find out if we have already searched this base or
  2076. ** if we are searching a subtree to an already enumerated base.
  2077. */
  2078. enumerate_groups = 1;
  2079. for (j=0; j < aclpb->aclpb_numof_bases; j++) {
  2080. if (slapi_dn_issuffix(aclpb->aclpb_grpsearchbase[j], base)) {
  2081. enumerate_groups = 0;
  2082. break;
  2083. }
  2084. }
  2085. /* See if we have already enumerated all the groups which the
  2086. ** client is a member of.
  2087. */
  2088. if (enumerate_groups) {
  2089. char filter_str[BUFSIZ];
  2090. char *attrs[3];
  2091. struct eval_info info;
  2092. char *curMemberDn;
  2093. int Done = 0;
  2094. int ngr, tt;
  2095. /* Add the scope to the list of scopes */
  2096. if (aclpb->aclpb_numof_bases >= (aclpb->aclpb_grpsearchbase_size-1)) {
  2097. aclpb->aclpb_grpsearchbase = (char **)
  2098. slapi_ch_realloc (
  2099. (void *) aclpb->aclpb_grpsearchbase,
  2100. (aclpb->aclpb_grpsearchbase_size +
  2101. ACLPB_INCR_BASES) *
  2102. sizeof (char *));
  2103. aclpb->aclpb_grpsearchbase_size += ACLPB_INCR_BASES;
  2104. }
  2105. aclpb->aclpb_grpsearchbase[aclpb->aclpb_numof_bases++] =
  2106. slapi_dn_normalize(slapi_ch_strdup(base));
  2107. /* Set up info to do a search */
  2108. attrs[0] = type_member;
  2109. attrs[1] = type_uniquemember;
  2110. attrs[2] = NULL;
  2111. info.c_idx = info.lu_idx = 0;
  2112. info.member =
  2113. (char **) slapi_ch_malloc (ACLLAS_MAX_GRP_MEMBER * sizeof(char *));
  2114. curMemberDn = n_clientdn;
  2115. while (!Done) {
  2116. char *filter_str_ptr = &filter_str[0];
  2117. char *new_filter_str = NULL;
  2118. int lenf = strlen(curMemberDn)<<1;
  2119. if (lenf > (BUFSIZ - 28)) { /* 28 for "(|(uniquemember=%s)(member=%s))" */
  2120. new_filter_str = slapi_ch_malloc(lenf + 28);
  2121. filter_str_ptr = new_filter_str;
  2122. }
  2123. /*
  2124. ** Search the db for groups that the client is a member of.
  2125. ** Once found cache it. cache only unique groups.
  2126. */
  2127. tt = info.lu_idx;
  2128. sprintf (filter_str_ptr,"(|(uniquemember=%s)(member=%s))",
  2129. curMemberDn, curMemberDn);
  2130. /* Use new search internal API */
  2131. {
  2132. Slapi_PBlock *aPb = slapi_pblock_new ();
  2133. /*
  2134. * This search may NOT be chained--we demand that group
  2135. * definition be local.
  2136. */
  2137. slapi_search_internal_set_pb ( aPb,
  2138. base,
  2139. LDAP_SCOPE_SUBTREE,
  2140. filter_str_ptr,
  2141. &attrs[0],
  2142. 0,
  2143. NULL /* controls */,
  2144. NULL /* uniqueid */,
  2145. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  2146. SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
  2147. slapi_search_internal_callback_pb(aPb,
  2148. &info /* callback_data */,
  2149. NULL/* result_callback */,
  2150. acllas__add_allgroups,
  2151. NULL /* referral_callback */);
  2152. slapi_pblock_destroy (aPb);
  2153. }
  2154. if (new_filter_str) slapi_ch_free((void **) &new_filter_str);
  2155. if (tt == info.lu_idx) {
  2156. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "currDn:(%s) \n\tNO MEMBER ADDED\n",
  2157. ACL_ESCAPE_STRING_WITH_PUNCTUATION (curMemberDn, ebuf) , 0,0);
  2158. } else {
  2159. for (i=tt; i < info.lu_idx; i++)
  2160. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2161. "currDn:(%s) \n\tADDED MEMBER[%d]=%s\n",
  2162. ACL_ESCAPE_STRING_WITH_PUNCTUATION (curMemberDn, ebuf), i, info.member[i]);
  2163. }
  2164. if (info.c_idx >= info.lu_idx) {
  2165. for (i=0; i < info.lu_idx; i++) {
  2166. int already_cached = 0;
  2167. for (j=0; j < u_group->aclug_numof_member_group;
  2168. j++){
  2169. if (slapi_utf8casecmp(
  2170. (ACLUCHP)info.member[i],
  2171. (ACLUCHP)u_group->aclug_member_groups[j]) == 0) {
  2172. slapi_ch_free ((void **) &info.member[i] );
  2173. info.member[i] = NULL;
  2174. already_cached = 1;
  2175. break;
  2176. }
  2177. }
  2178. if (already_cached) continue;
  2179. ngr = u_group->aclug_numof_member_group++;
  2180. if (u_group->aclug_numof_member_group >=
  2181. u_group->aclug_member_group_size){
  2182. u_group->aclug_member_groups =
  2183. (char **) slapi_ch_realloc (
  2184. (void *) u_group->aclug_member_groups,
  2185. (u_group->aclug_member_group_size +
  2186. ACLUG_INCR_GROUPS_LIST) * sizeof(char *));
  2187. u_group->aclug_member_group_size +=
  2188. ACLUG_INCR_GROUPS_LIST;
  2189. }
  2190. u_group->aclug_member_groups[ngr] = info.member[i];
  2191. info.member[i] = NULL;
  2192. }
  2193. slapi_ch_free ((void **) &info.member);
  2194. Done = 1;
  2195. } else {
  2196. curMemberDn = info.member[info.c_idx];
  2197. info.c_idx++;
  2198. }
  2199. }
  2200. }
  2201. for (j=0; j < u_group->aclug_numof_member_group; j++)
  2202. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2203. "acllas__eval_memberGroupDnAttr:GROUP[%d] IN CACHE:%s\n",
  2204. j, ACL_ESCAPE_STRING_WITH_PUNCTUATION (u_group->aclug_member_groups[j], ebuf),0);
  2205. matched = ACL_FALSE;
  2206. slapi_entry_attr_find( e, groupattr, &attr);
  2207. if (attr == NULL) {
  2208. slapi_ch_free ( (void **)&s_str );
  2209. return ACL_FALSE;
  2210. }
  2211. {
  2212. k = slapi_attr_first_value ( attr,&sval );
  2213. while ( k != -1 ) {
  2214. char *n_attrval;
  2215. attrVal = slapi_value_get_berval ( sval );
  2216. n_attrval = slapi_ch_strdup( attrVal->bv_val);
  2217. n_attrval = slapi_dn_normalize (n_attrval);
  2218. /* We support: The attribute value can be a USER or a GROUP.
  2219. ** Let's compare with the client, thi might be just an user. If it is not
  2220. ** then we test it against the list of groups.
  2221. */
  2222. if (slapi_utf8casecmp ((ACLUCHP)n_attrval, (ACLUCHP)n_clientdn) == 0 ) {
  2223. matched = ACL_TRUE;
  2224. slapi_ch_free ( (void **)&n_attrval );
  2225. break;
  2226. }
  2227. for (j=0; j <u_group->aclug_numof_member_group; j++) {
  2228. if ( slapi_utf8casecmp((ACLUCHP)n_attrval,
  2229. (ACLUCHP)u_group->aclug_member_groups[j]) == 0) {
  2230. matched = ACL_TRUE;
  2231. break;
  2232. }
  2233. }
  2234. slapi_ch_free ( (void **)&n_attrval );
  2235. if (matched == ACL_TRUE) break;
  2236. k= slapi_attr_next_value ( attr, k, &sval );
  2237. }
  2238. }
  2239. slapi_ch_free ( (void **)&s_str );
  2240. return matched;
  2241. }
  2242. static int
  2243. acllas__add_allgroups (Slapi_Entry* e, void *callback_data)
  2244. {
  2245. int i, n, m;
  2246. struct eval_info *info;
  2247. char *n_dn;
  2248. info = (struct eval_info *) callback_data;
  2249. /*
  2250. ** Once we are here means this is a valid group. First see
  2251. ** If we have already seen this group. If not, add it to the
  2252. ** member list.
  2253. */
  2254. n_dn = slapi_ch_strdup ( slapi_entry_get_ndn ( e ) );
  2255. for (i=0; i < info->lu_idx; i++) {
  2256. if (slapi_utf8casecmp((ACLUCHP)n_dn, (ACLUCHP)info->member[i]) == 0) {
  2257. slapi_ch_free ( (void **) &n_dn);
  2258. return 0;
  2259. }
  2260. }
  2261. m = info->lu_idx;
  2262. n = info->lu_idx++;
  2263. if (!(n % ACLLAS_MAX_GRP_MEMBER)) {
  2264. info->member = (char **) slapi_ch_realloc (
  2265. (void *) info->member,
  2266. (n+ACLLAS_MAX_GRP_MEMBER) * sizeof(char *));
  2267. }
  2268. info->member[m] = n_dn;
  2269. return 0;
  2270. }
  2271. /*
  2272. *
  2273. * acllas__dn_parent
  2274. *
  2275. * This code should belong to dn.c. However this is specific to acl and I had
  2276. * 2 choices 1) create a new API or 2) reuse the slapi_dN_parent
  2277. *
  2278. * Returns a ptr to the parent based on the level.
  2279. *
  2280. */
  2281. #define DNSEPARATOR(c) (c == ',' || c == ';')
  2282. static char*
  2283. acllas__dn_parent( char *dn, int level)
  2284. {
  2285. char *s, *dnstr;
  2286. int inquote;
  2287. int curLevel;
  2288. int lastLoop = 0;
  2289. if ( dn == NULL || *dn == '\0' ) {
  2290. return( NULL );
  2291. }
  2292. /* An X.500-style name, which looks like foo=bar,sha=baz,... */
  2293. /* Do we have any dn seprator or not */
  2294. if ((strchr(dn,',') == NULL) && (strchr(dn,';') == NULL))
  2295. return (NULL);
  2296. inquote = 0;
  2297. curLevel = 1;
  2298. dnstr = dn;
  2299. while ( curLevel <= level) {
  2300. if (lastLoop) break;
  2301. if (curLevel == level) lastLoop = 1;
  2302. for ( s = dnstr; *s; s++ ) {
  2303. if ( *s == '\\' ) {
  2304. if ( *(s + 1) )
  2305. s++;
  2306. continue;
  2307. }
  2308. if ( inquote ) {
  2309. if ( *s == '"' )
  2310. inquote = 0;
  2311. } else {
  2312. if ( *s == '"' )
  2313. inquote = 1;
  2314. else if ( DNSEPARATOR( *s ) ) {
  2315. if (curLevel == level)
  2316. return( s + 1 );
  2317. dnstr = s + 1;
  2318. curLevel++;
  2319. break;
  2320. }
  2321. }
  2322. }
  2323. if ( *s == '\0') {
  2324. /* Got to the end of the string without reaching level,
  2325. * so return NULL.
  2326. */
  2327. return(NULL);
  2328. }
  2329. }
  2330. return( NULL );
  2331. }
  2332. /*
  2333. * acllas__verify_client
  2334. *
  2335. * returns 1 if the attribute exists in the entry and
  2336. * it's value is equal to the client Dn.
  2337. * If the attribute is not in the entry, or it is and the
  2338. * value differs from the clientDn then returns FALSE.
  2339. *
  2340. * Verify if client's DN is stored in the attrbute or not.
  2341. * This is a handler from a search being done at
  2342. * DS_LASUserDnAttrEval().
  2343. *
  2344. */
  2345. static int
  2346. acllas__verify_client (Slapi_Entry* e, void *callback_data)
  2347. {
  2348. Slapi_Attr *attr;
  2349. char *val;
  2350. struct userdnattr_info *info;
  2351. Slapi_Value *sval;
  2352. const struct berval *attrVal;
  2353. int i;
  2354. info = (struct userdnattr_info *) callback_data;
  2355. slapi_entry_attr_find( e, info->attr, &attr);
  2356. if (attr == NULL) return 0;
  2357. i = slapi_attr_first_value ( attr,&sval );
  2358. while ( i != -1 ) {
  2359. attrVal = slapi_value_get_berval ( sval );
  2360. val = slapi_dn_normalize (
  2361. slapi_ch_strdup(attrVal->bv_val));
  2362. if (slapi_utf8casecmp((ACLUCHP)val, (ACLUCHP)info->clientdn ) == 0) {
  2363. info->result = 1;
  2364. slapi_ch_free ( (void **) &val);
  2365. return 0;
  2366. }
  2367. slapi_ch_free ( (void **) &val);
  2368. i = slapi_attr_next_value ( attr, i, &sval );
  2369. }
  2370. return 0;
  2371. }
  2372. /*
  2373. *
  2374. * acllas__get_members
  2375. *
  2376. * Collects all the values of the specified attribute which should be group names.
  2377. */
  2378. static int
  2379. acllas__get_members (Slapi_Entry* e, void *callback_data)
  2380. {
  2381. Slapi_Attr *attr;
  2382. struct groupdnattr_info *info;
  2383. Slapi_Value *sval=NULL;
  2384. const struct berval *attrVal;
  2385. int i;
  2386. info = (struct groupdnattr_info *) callback_data;
  2387. slapi_entry_attr_find (e, info->attrName, &attr);
  2388. if ( !attr ) return 0;
  2389. slapi_attr_get_numvalues ( attr, &info->numofGroups );
  2390. info->member = (char **) slapi_ch_malloc (info->numofGroups * sizeof(char *));
  2391. i = slapi_attr_first_value ( attr,&sval );
  2392. while ( i != -1 ) {
  2393. attrVal =slapi_value_get_berval ( sval );
  2394. info->member[i] = slapi_dn_normalize ( slapi_ch_strdup(attrVal->bv_val));
  2395. i = slapi_attr_next_value ( attr, i, &sval );
  2396. }
  2397. return 0;
  2398. }
  2399. /*
  2400. * DS_LASUserAttrEval
  2401. * LAS to evaluate the userattr rule
  2402. *
  2403. * userAttr = "attrName#Type"
  2404. *
  2405. * <Type> ::= "USERDN" | "GROUPDN" | "ROLEDN" | "LDAPURL" | <value>
  2406. * <value>::== <any printable String>
  2407. *
  2408. * Example:
  2409. * userAttr = "manager#USERDN" --- same as userdnattr
  2410. * userAttr = "owner#GROUPDN" --- same as groupdnattr
  2411. * = "ldap:///o=sun.com?owner#GROUPDN
  2412. * userAttr = "attr#ROLEDN" --- The value of attr contains a roledn
  2413. * userAttr = "myattr#LDAPURL" --- The value contains a LDAP URL
  2414. * which can have scope and filter
  2415. * bits.
  2416. * userAttr = "OU#Directory Server"
  2417. * --- In this case the client's OU and the
  2418. * resource entry's OU must have
  2419. * "Directory Server" value.
  2420. *
  2421. * Returns:
  2422. * retcode The usual LAS return codes.
  2423. */
  2424. int
  2425. DS_LASUserAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  2426. char *attr_pattern, int *cachable, void **LAS_cookie,
  2427. PList_t subject, PList_t resource, PList_t auth_info,
  2428. PList_t global_auth)
  2429. {
  2430. char *attrName;
  2431. char *attrValue = NULL;
  2432. int rc;
  2433. int matched = ACL_FALSE;
  2434. char *p;
  2435. int URLAttrRule = 0;
  2436. lasInfo lasinfo;
  2437. int got_undefined = 0;
  2438. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  2439. attr_pattern,cachable,LAS_cookie,
  2440. subject, resource, auth_info,global_auth,
  2441. DS_LAS_USERATTR, "DS_LASUserAttrEval",
  2442. &lasinfo )) ) {
  2443. return LAS_EVAL_FAIL;
  2444. }
  2445. /* Which rule are we evaluating ? */
  2446. attrName = slapi_ch_strdup (attr_pattern );
  2447. if ( NULL == (p = strchr ( attrName, '#' ))) {
  2448. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2449. "DS_LASUserAttrEval:Invalid value(%s)\n", attr_pattern);
  2450. slapi_ch_free ( (void **) &attrName );
  2451. return LAS_EVAL_FAIL;
  2452. }
  2453. attrValue = p;
  2454. attrValue++; /* skip the # */
  2455. *p = '\0'; /* null terminate the attr name */
  2456. if ( 0 == strncasecmp ( attrValue, "USERDN", 6)) {
  2457. matched = DS_LASUserDnAttrEval (errp,DS_LAS_USERDNATTR, comparator,
  2458. attrName, cachable, LAS_cookie,
  2459. subject, resource, auth_info, global_auth);
  2460. goto done_las;
  2461. } else if ( 0 == strncasecmp ( attrValue, "GROUPDN", 7)) {
  2462. matched = DS_LASGroupDnAttrEval (errp,DS_LAS_GROUPDNATTR, comparator,
  2463. attrName, cachable, LAS_cookie,
  2464. subject, resource, auth_info, global_auth);
  2465. goto done_las;
  2466. } else if ( 0 == strncasecmp ( attrValue, "LDAPURL", 7) ) {
  2467. URLAttrRule = 1;
  2468. } else if ( 0 == strncasecmp ( attrValue, "ROLEDN", 6)) {
  2469. matched = DS_LASRoleDnAttrEval (errp,DS_LAS_ROLEDN, comparator,
  2470. attrName, cachable, LAS_cookie,
  2471. subject, resource, auth_info, global_auth);
  2472. goto done_las;
  2473. }
  2474. if ( lasinfo.aclpb && ( NULL == lasinfo.aclpb->aclpb_client_entry )) {
  2475. /* SD 00/16/03 pass NULL in case the req is chained */
  2476. char **attrs=NULL;
  2477. /* Use new search internal API */
  2478. Slapi_PBlock *aPb = slapi_pblock_new ();
  2479. /*
  2480. * This search may be chained if chaining for ACL is
  2481. * is enabled in the backend and the entry is in
  2482. * a chained backend.
  2483. */
  2484. slapi_search_internal_set_pb ( aPb,
  2485. lasinfo.clientDn,
  2486. LDAP_SCOPE_BASE,
  2487. "objectclass=*",
  2488. attrs,
  2489. 0,
  2490. NULL /* controls */,
  2491. NULL /* uniqueid */,
  2492. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  2493. 0 /* actions */);
  2494. slapi_search_internal_callback_pb(aPb,
  2495. lasinfo.aclpb /* callback_data */,
  2496. NULL/* result_callback */,
  2497. acllas__handle_client_search,
  2498. NULL /* referral_callback */);
  2499. slapi_pblock_destroy (aPb);
  2500. }
  2501. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  2502. "DS_LASUserAttrEval: AttrName:%s, attrVal:%s\n", attrName, attrValue );
  2503. if ( URLAttrRule ) {
  2504. Slapi_Value *sval=NULL;
  2505. const struct berval *attrVal;
  2506. Slapi_Attr *attrs;
  2507. int i;
  2508. /* Get the attr from the resouce entry */
  2509. if ( 0 == slapi_entry_attr_find (lasinfo.resourceEntry, attrName, &attrs) ) {
  2510. i= slapi_attr_first_value ( attrs, &sval );
  2511. if ( i==-1 ) {
  2512. matched = ACL_FALSE; /* Attr val not there so it's value cannot equal other one */
  2513. goto done_acl;
  2514. }
  2515. } else {
  2516. matched = ACL_FALSE; /* Not there so it cannot equal another one */
  2517. goto done_acl;
  2518. }
  2519. while( matched != ACL_TRUE && (sval != NULL)) {
  2520. attrVal = slapi_value_get_berval ( sval );
  2521. matched = acllas__client_match_URL ( lasinfo.aclpb,
  2522. lasinfo.clientDn,
  2523. attrVal->bv_val);
  2524. if ( matched != ACL_TRUE )
  2525. i = slapi_attr_next_value ( attrs, i, &sval );
  2526. if ( matched == ACL_DONT_KNOW ) {
  2527. got_undefined = 1;
  2528. }
  2529. }/* while */
  2530. } else {
  2531. /*
  2532. * Here it's the userAttr = "OU#Directory Server" case.
  2533. * Allocate the Slapi_Value on the stack and init it by reference
  2534. * to avoid having to malloc and free memory.
  2535. */
  2536. Slapi_Value v;
  2537. slapi_value_init_string_passin(&v, attrValue);
  2538. rc = slapi_entry_attr_has_syntax_value ( lasinfo.resourceEntry, attrName,
  2539. &v );
  2540. if (rc) {
  2541. rc = slapi_entry_attr_has_syntax_value (
  2542. lasinfo.aclpb->aclpb_client_entry,
  2543. attrName, &v );
  2544. if (rc) matched = ACL_TRUE;
  2545. }
  2546. /* Nothing to free--cool */
  2547. }
  2548. /*
  2549. * Find out what the result is, in
  2550. * this case matched is one of ACL_TRUE, ACL_FALSE or ACL_DONT_KNOW
  2551. * and got_undefined says whether a logical term evaluated to ACL_DONT_KNOW.
  2552. *
  2553. */
  2554. done_acl:
  2555. if ( matched == ACL_TRUE || !got_undefined) {
  2556. if (comparator == CMP_OP_EQ) {
  2557. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  2558. } else {
  2559. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  2560. }
  2561. } else {
  2562. rc = LAS_EVAL_FAIL;
  2563. }
  2564. slapi_ch_free ( (void **) &attrName );
  2565. return rc;
  2566. done_las:
  2567. /*
  2568. * In this case matched is already LAS_EVAL_TRUE or LAS_EVAL_FALSE or
  2569. * LAS_EVAL_FAIL.
  2570. */
  2571. if ( matched != LAS_EVAL_FAIL ) {
  2572. if (comparator == CMP_OP_EQ) {
  2573. rc = matched;
  2574. } else {
  2575. rc = (matched == LAS_EVAL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  2576. }
  2577. }
  2578. slapi_ch_free ( (void **) &attrName );
  2579. return rc;
  2580. }
  2581. /*
  2582. * acllas__client_match_URL
  2583. * Match a client to a URL.
  2584. *
  2585. * Returns:
  2586. * ACL_TRUE - matched the URL
  2587. * ACL_FALSE - Sorry; no match
  2588. *
  2589. */
  2590. static int
  2591. acllas__client_match_URL (struct acl_pblock *aclpb, char *n_clientdn, char *url )
  2592. {
  2593. LDAPURLDesc *ludp;
  2594. int rc;
  2595. Slapi_Filter *f = NULL;
  2596. /* Get the client's entry if we don't have already */
  2597. if ( aclpb && ( NULL == aclpb->aclpb_client_entry )) {
  2598. /* SD 00/16/03 Get every attr in case req chained */
  2599. char **attrs=NULL;
  2600. /* Use new search internal API */
  2601. Slapi_PBlock * aPb = slapi_pblock_new ();
  2602. /*
  2603. * This search may be chained if chaining for ACL is
  2604. * is enabled in the backend and the entry is in
  2605. * a chained backend.
  2606. */
  2607. slapi_search_internal_set_pb ( aPb,
  2608. n_clientdn,
  2609. LDAP_SCOPE_BASE,
  2610. "objectclass=*",
  2611. attrs,
  2612. 0,
  2613. NULL /* controls */,
  2614. NULL /* uniqueid */,
  2615. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  2616. 0 /* actions */);
  2617. slapi_search_internal_callback_pb(aPb,
  2618. aclpb /* callback_data */,
  2619. NULL/* result_callback */,
  2620. acllas__handle_client_search,
  2621. NULL /* referral_callback */);
  2622. slapi_pblock_destroy (aPb);
  2623. }
  2624. if ( NULL == aclpb->aclpb_client_entry ) {
  2625. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  2626. "DS_LASUserAttrEval: Unable to get client's entry\n",0,0,0);
  2627. return ACL_FALSE;
  2628. }
  2629. if (( rc = ldap_url_parse( url, &ludp)) != 0 ) {
  2630. return ACL_FALSE;
  2631. }
  2632. if ( ( NULL == ludp->lud_dn) || ( NULL == ludp->lud_filter) ) {
  2633. ldap_free_urldesc( ludp );
  2634. return ACL_FALSE;
  2635. }
  2636. /* Normalize in place the dn */
  2637. slapi_dn_normalize ( ludp->lud_dn );
  2638. /* Check the scope */
  2639. if ( ludp->lud_scope == LDAP_SCOPE_SUBTREE ) {
  2640. if (!slapi_dn_issuffix(n_clientdn, ludp->lud_dn)) {
  2641. ldap_free_urldesc( ludp );
  2642. return ACL_FALSE;
  2643. }
  2644. } else if ( ludp->lud_scope == LDAP_SCOPE_ONELEVEL ) {
  2645. char *parent = slapi_dn_parent (n_clientdn);
  2646. if (slapi_utf8casecmp ((ACLUCHP)parent, (ACLUCHP)ludp->lud_dn) != 0 ) {
  2647. slapi_ch_free ( (void **) &parent);
  2648. ldap_free_urldesc( ludp );
  2649. return ACL_FALSE;
  2650. }
  2651. slapi_ch_free ( (void **) &parent);
  2652. } else { /* default */
  2653. if (slapi_utf8casecmp ( (ACLUCHP)n_clientdn, (ACLUCHP)ludp->lud_dn) != 0 ) {
  2654. ldap_free_urldesc( ludp );
  2655. return ACL_FALSE;
  2656. }
  2657. }
  2658. /* Convert the filter string */
  2659. f = slapi_str2filter ( ludp->lud_filter );
  2660. rc = ACL_TRUE;
  2661. if (0 != slapi_vattr_filter_test ( aclpb->aclpb_pblock,
  2662. aclpb->aclpb_client_entry, f, 0 /* no acces chk */ ))
  2663. rc = ACL_FALSE;
  2664. ldap_free_urldesc( ludp );
  2665. slapi_filter_free ( f, 1 ) ;
  2666. return rc;
  2667. }
  2668. static int
  2669. acllas__handle_client_search ( Slapi_Entry *e, void *callback_data )
  2670. {
  2671. struct acl_pblock *aclpb = (struct acl_pblock *) callback_data;
  2672. /* If we are here means we have found the entry */
  2673. if ( NULL == aclpb-> aclpb_client_entry)
  2674. aclpb->aclpb_client_entry = slapi_entry_dup ( e );
  2675. return 0;
  2676. }
  2677. /*
  2678. *
  2679. * Do all the necessary setup for all the
  2680. * LASes.
  2681. * It will only fail if it's passed garbage (which should not happen) or
  2682. * if the data it needs to stock the lasinfo is not available, which
  2683. * also should not happen.
  2684. *
  2685. *
  2686. * Return value: 0 or one of these
  2687. * #define LAS_EVAL_TRUE -1
  2688. * #define LAS_EVAL_FALSE -2
  2689. * #define LAS_EVAL_DECLINE -3
  2690. * #define LAS_EVAL_FAIL -4
  2691. * #define LAS_EVAL_INVALID -5
  2692. */
  2693. static int
  2694. __acllas_setup ( NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  2695. char *attr_pattern, int *cachable, void **LAS_cookie,
  2696. PList_t subject, PList_t resource, PList_t auth_info,
  2697. PList_t global_auth, char *lasType, char*lasName, lasInfo *linfo)
  2698. {
  2699. int rc;
  2700. memset ( linfo, 0, sizeof ( lasInfo) );
  2701. *cachable = 0;
  2702. *LAS_cookie = (void *)0;
  2703. if (strcmp(attr_name, lasType) != 0) {
  2704. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2705. "%s:Invalid LAS(%s)\n", lasName, attr_name);
  2706. return LAS_EVAL_INVALID;
  2707. }
  2708. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  2709. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2710. "%s:Invalid comparator(%d)\n", lasName, (int)comparator);
  2711. return LAS_EVAL_INVALID;
  2712. }
  2713. /* Get the client DN */
  2714. rc = ACL_GetAttribute(errp, DS_ATTR_USERDN, (void **)&linfo->clientDn,
  2715. subject, resource, auth_info, global_auth);
  2716. if ( rc != LAS_EVAL_TRUE ) {
  2717. acl_print_acllib_err(errp, NULL);
  2718. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2719. "%s:Unable to get the clientdn attribute(%d)\n",lasName, rc);
  2720. return LAS_EVAL_FAIL;
  2721. }
  2722. /* Check if we have a user or not */
  2723. if (linfo->clientDn) {
  2724. /* See if it's a anonymous user */
  2725. if (*(linfo->clientDn) == '\0')
  2726. linfo->anomUser = ACL_TRUE;
  2727. } else {
  2728. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2729. "%s: No user\n",lasName);
  2730. return LAS_EVAL_FAIL;
  2731. }
  2732. if ((rc = PListFindValue(subject, DS_ATTR_ENTRY,
  2733. (void **)&linfo->resourceEntry, NULL)) < 0) {
  2734. acl_print_acllib_err(errp, NULL);
  2735. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2736. "%s:Unable to get the Slapi_Entry attr(%d)\n",lasName, rc);
  2737. return LAS_EVAL_FAIL;
  2738. }
  2739. /* Get ACLPB */
  2740. rc = ACL_GetAttribute(errp, DS_PROP_ACLPB, (void **)&linfo->aclpb,
  2741. subject, resource, auth_info, global_auth);
  2742. if ( rc != LAS_EVAL_TRUE ) {
  2743. acl_print_acllib_err(errp, NULL);
  2744. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2745. "%s:Unable to get the ACLPB(%d)\n", lasName, rc);
  2746. return LAS_EVAL_FAIL;
  2747. }
  2748. if (NULL == attr_pattern ) {
  2749. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2750. "%s:No rule value in the ACL\n", lasName);
  2751. return LAS_EVAL_FAIL;
  2752. }
  2753. /* get the authentication type */
  2754. if ((rc = PListFindValue(subject, DS_ATTR_AUTHTYPE,
  2755. (void **)&linfo->authType, NULL)) < 0) {
  2756. acl_print_acllib_err(errp, NULL);
  2757. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2758. "%s:Unable to get the auth type(%d)\n", rc);
  2759. return LAS_EVAL_FAIL;
  2760. }
  2761. return 0;
  2762. }
  2763. /*
  2764. * See if clientDN has role roleDN.
  2765. * Here we know the user is not anon and that the role
  2766. * is not the anyone role ie. it's actually worth invoking the roles code.
  2767. */
  2768. static int acllas__user_has_role( struct acl_pblock *aclpb,
  2769. Slapi_DN *roleDN, Slapi_DN *clientDn) {
  2770. int present = 0;
  2771. int rc = 0;
  2772. /* Get the client's entry if we don't have already */
  2773. if ( aclpb && ( NULL == aclpb->aclpb_client_entry )) {
  2774. /* SD 00/16/03 Get every attr in case req chained */
  2775. char **attrs=NULL;
  2776. /* Use new search internal API */
  2777. Slapi_PBlock * aPb = slapi_pblock_new ();
  2778. /*
  2779. * This search may NOT be chained--the user and the role definition
  2780. * must be co-located (chaining is not supported for the roles
  2781. * plugin in 5.0
  2782. */
  2783. slapi_search_internal_set_pb ( aPb,
  2784. slapi_sdn_get_ndn(clientDn),
  2785. LDAP_SCOPE_BASE,
  2786. "objectclass=*",
  2787. attrs,
  2788. 0,
  2789. NULL /* controls */,
  2790. NULL /* uniqueid */,
  2791. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  2792. SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
  2793. slapi_search_internal_callback_pb(aPb,
  2794. aclpb /* callback_data */,
  2795. NULL/* result_callback */,
  2796. acllas__handle_client_search,
  2797. NULL /* referral_callback */);
  2798. slapi_pblock_destroy (aPb);
  2799. }
  2800. if ( NULL == aclpb->aclpb_client_entry ) {
  2801. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  2802. "acllas__user_has_role: Unable to get client's entry\n",0,0,0);
  2803. return ACL_FALSE;
  2804. }
  2805. /* If the client has the role then it's a match, otherwise no */
  2806. rc = slapi_role_check( aclpb->aclpb_client_entry, roleDN, &present);
  2807. if ( present ) {
  2808. return(ACL_TRUE);
  2809. }
  2810. return(ACL_FALSE);
  2811. }
  2812. int
  2813. DS_LASRoleDnAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  2814. char *attr_pattern, int *cachable, void **LAS_cookie,
  2815. PList_t subject, PList_t resource, PList_t auth_info,
  2816. PList_t global_auth)
  2817. {
  2818. char *s_attrName = NULL;
  2819. char *attrName;
  2820. int matched;
  2821. int rc;
  2822. Slapi_Attr *attr;
  2823. int numOflevels = 0;
  2824. char *n_currEntryDn = NULL;
  2825. lasInfo lasinfo;
  2826. Slapi_Value *sval=NULL;
  2827. const struct berval *attrVal;
  2828. int k=0;
  2829. int got_undefined = 0;
  2830. if ( 0 != (rc = __acllas_setup (errp, attr_name, comparator,
  2831. attr_pattern,cachable,LAS_cookie,
  2832. subject, resource, auth_info,global_auth,
  2833. DS_LAS_ROLEDN, "DS_LASRoleDnAttrEval",
  2834. &lasinfo )) ) {
  2835. return LAS_EVAL_FAIL;
  2836. }
  2837. /* For anonymous client, they have no roles so the match is false. */
  2838. if ( lasinfo.anomUser )
  2839. return LAS_EVAL_FALSE;
  2840. /*
  2841. **
  2842. ** The function of this LAS is to find out if the client has
  2843. ** the role specified in the attr.
  2844. ** attr_pattern looks like: "ROLEDN cn=role1,o=sun.com"
  2845. */
  2846. attrName = attr_pattern;
  2847. matched = ACL_FALSE;
  2848. slapi_entry_attr_find( lasinfo.resourceEntry, attrName, &attr);
  2849. if (attr == NULL) {
  2850. /*
  2851. * Here the entry does not contain the attribute so the user
  2852. * cannot have this "null" role
  2853. */
  2854. return LAS_EVAL_FALSE;
  2855. }
  2856. if (lasinfo.aclpb->aclpb_optype == SLAPI_OPERATION_ADD) {
  2857. /*
  2858. * Here the entry does not contain the attribute so the user
  2859. * cannot have this "null" role or
  2860. * For the add operation, the resource itself
  2861. * must never be allowed to grant access--
  2862. * This is because access would be granted based on a value
  2863. * of an attribute in the new entry--security hole.
  2864. * XXX is this therefore FALSE or DONT_KNOW ?
  2865. *
  2866. *
  2867. */
  2868. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  2869. "ACL info: userattr=XXX#ROLEDN does not allow ADD permission.\n");
  2870. got_undefined = 1;
  2871. } else {
  2872. /*
  2873. * Got the first value.
  2874. * Test all the values of this attribute--if the client has _any_
  2875. * of the roles then it's a match.
  2876. */
  2877. k = slapi_attr_first_value ( attr,&sval );
  2878. while ( k != -1 ) {
  2879. char *n_attrval;
  2880. Slapi_DN *roleDN;
  2881. attrVal = slapi_value_get_berval ( sval );
  2882. n_attrval = slapi_ch_strdup( attrVal->bv_val);
  2883. n_attrval = slapi_dn_normalize (n_attrval);
  2884. roleDN = slapi_sdn_new_dn_byval(n_attrval);
  2885. /* We support: The attribute value can be a USER or a GROUP.
  2886. ** Let's compare with the client, thi might be just an user. If it is not
  2887. ** then we test it against the list of groups.
  2888. */
  2889. if ((matched = acllas__user_has_role(
  2890. lasinfo.aclpb,
  2891. roleDN,
  2892. lasinfo.aclpb->aclpb_authorization_sdn)) == ACL_TRUE){
  2893. slapi_ch_free ( (void **)&n_attrval );
  2894. slapi_sdn_free(&roleDN );
  2895. break;
  2896. }
  2897. slapi_ch_free ( (void **)&n_attrval );
  2898. slapi_sdn_free(&roleDN );
  2899. if (matched == ACL_TRUE) {
  2900. break;
  2901. } else if ( matched == ACL_DONT_KNOW ) {
  2902. /* record this but keep going--maybe another group will evaluate to TRUE */
  2903. got_undefined = 1;
  2904. }
  2905. k= slapi_attr_next_value ( attr, k, &sval );
  2906. }/* while */
  2907. }
  2908. /*
  2909. * If no terms were undefined, then evaluate as normal.
  2910. * If there was an undefined term, but another one was TRUE, then we also evaluate
  2911. * as normal. Otherwise, the whole expression is UNDEFINED.
  2912. */
  2913. if ( matched == ACL_TRUE || !got_undefined ) {
  2914. if (comparator == CMP_OP_EQ) {
  2915. rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  2916. } else {
  2917. rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  2918. }
  2919. } else {
  2920. rc = LAS_EVAL_FAIL;
  2921. }
  2922. return (rc);
  2923. }
  2924. /*
  2925. * Here, determine if lasinfo->clientDn matches user (which contains
  2926. * a ($dn) or a $attr component or both.) As defined in the aci
  2927. * lasinfo->aclpb->aclpb_curr_aci,
  2928. * which is the current aci being evaluated.
  2929. *
  2930. * returns: ACL_TRUE for matched,
  2931. * ACL_FALSE for matched.
  2932. * ACL_DONT_KNOW otherwise.
  2933. *
  2934. *
  2935. */
  2936. int
  2937. aclutil_evaluate_macro( char * rule, lasInfo *lasinfo,
  2938. acl_eval_types evalType ) {
  2939. int matched = 0;
  2940. aci_t *aci;
  2941. char *matched_val = NULL;
  2942. char **candidate_list = NULL;
  2943. char **inner_list = NULL;
  2944. char **sptr = NULL;
  2945. char **tptr = NULL;
  2946. char *t = NULL;
  2947. char *s = NULL;
  2948. char *target_dn = NULL;
  2949. struct acl_pblock *aclpb = lasinfo->aclpb;
  2950. int found_matched_val_in_ht = 0;
  2951. aci = lasinfo->aclpb->aclpb_curr_aci;
  2952. /* Get a pointer to the ndn in the resouirce */
  2953. target_dn = slapi_entry_get_ndn ( lasinfo->resourceEntry );
  2954. /*
  2955. * First, get the matched value from the target resource.
  2956. * We have alredy done this matching once beofer at tasrget match time.
  2957. */
  2958. LDAPDebug ( LDAP_DEBUG_ACL, "aclutil_evaluate_macro for aci '%s'"
  2959. "index '%d'\n",
  2960. aci->aclName, aci->aci_index,0);
  2961. if ( aci->aci_macro == NULL ) {
  2962. /* No $dn in the target, it's a $attr type subject rule */
  2963. matched_val = NULL;
  2964. } else {
  2965. /*
  2966. * Look up the matched_val value calculated
  2967. * from the target and stored judiciously there for us.
  2968. */
  2969. if ( (matched_val = (char *)acl_ht_lookup( aclpb->aclpb_macro_ht,
  2970. (PLHashNumber)aci->aci_index)) == NULL) {
  2971. LDAPDebug( LDAP_DEBUG_ACL,
  2972. "ACL info: failed to locate the calculated target"
  2973. "macro for aci '%s' index '%d'\n",
  2974. aci->aclName, aci->aci_index,0);
  2975. return(ACL_FALSE); /* Not a match */
  2976. } else {
  2977. LDAPDebug( LDAP_DEBUG_ACL,
  2978. "ACL info: found matched_val (%s) for aci index %d"
  2979. "in macro ht\n",
  2980. aci->aclName, aci->aci_index,0);
  2981. found_matched_val_in_ht = 1;
  2982. }
  2983. }
  2984. /*
  2985. * Now, make a candidate
  2986. * list of strings to match against the client.
  2987. * This involves replacing ($dn) or [$dn] by either the matched
  2988. * value, or all the suffix substrings of matched_val.
  2989. * If there is no $dn then the candidate list is just
  2990. * user itself.
  2991. *
  2992. */
  2993. candidate_list = acllas_replace_dn_macro( rule, matched_val, lasinfo);
  2994. sptr= candidate_list;
  2995. while( *sptr != NULL && !matched) {
  2996. s = *sptr;
  2997. /*
  2998. * Now s may contain some $attr macros.
  2999. * So, make a candidate list, got by replacing each occurence
  3000. * of $attr with all the values that attribute has in
  3001. * the resource entry.
  3002. */
  3003. inner_list = acllas_replace_attr_macro( s, lasinfo);
  3004. tptr = inner_list;
  3005. while( *tptr != NULL && (matched != ACL_TRUE) ){
  3006. t = *tptr;
  3007. /*
  3008. * Now, at last t is a candidate string we can
  3009. * match agains the client.
  3010. *
  3011. * $dn and $attr can appear in userdn, graoupdn and roledn
  3012. * rules, so we we need to decide which type we
  3013. * currently evaluating and evaluate that.
  3014. *
  3015. * If the string generated was undefined, eg it contained
  3016. * ($attr.ou) and the entry did not have an ou attribute,then
  3017. * the empty string is returned for this. So it we find
  3018. * an empty string in the list, skip it--it does not match.
  3019. */
  3020. if ( *t != '\0') {
  3021. if ( evalType == ACL_EVAL_USER ) {
  3022. matched = acllas_eval_one_user( lasinfo->aclpb,
  3023. lasinfo->clientDn, t);
  3024. } else if (evalType == ACL_EVAL_GROUP) {
  3025. matched = acllas_eval_one_group(t, lasinfo);
  3026. } else if (evalType == ACL_EVAL_ROLE) {
  3027. matched = acllas_eval_one_role(t, lasinfo);
  3028. } else if (evalType == ACL_EVAL_GROUPDNATTR) {
  3029. matched = acllas__eval_memberGroupDnAttr(t,
  3030. lasinfo->resourceEntry,
  3031. lasinfo->clientDn,
  3032. lasinfo->aclpb);
  3033. } else if ( evalType == ACL_EVAL_TARGET_FILTER) {
  3034. matched = acllas_eval_one_target_filter(t,
  3035. lasinfo->resourceEntry);
  3036. }
  3037. }
  3038. tptr++;
  3039. }/*inner while*/
  3040. charray_free(inner_list);
  3041. sptr++;
  3042. }/* outer while */
  3043. charray_free(candidate_list);
  3044. return(matched);
  3045. }
  3046. /*
  3047. * Here, replace the first occurrence of $(dn) with matched_val.
  3048. * replace any occurrence of $[dn] with each of the suffix substrings
  3049. * of matched_val.
  3050. * Each of these strings is returned in a NULL terminated list of strings.
  3051. *
  3052. * If there is no $dn thing then the returned list just contains rule itself.
  3053. *
  3054. * eg. rule: cn=fred,ou=*, ($dn), o=sun.com
  3055. * matched_val: ou=People,o=icnc
  3056. *
  3057. * Then we return the list
  3058. * cn=fred,ou=*,ou=People,o=icnc,o=sun.com NULL
  3059. *
  3060. * eg. rule: cn=fred,ou=*,[$dn], o=sun.com
  3061. * matched_val: ou=People,o=icnc
  3062. *
  3063. * Then we return the list
  3064. * cn=fred,ou=*,ou=People,o=icnc,o=sun.com
  3065. * cn=fred,ou=*,o=icnc,o=sun.com
  3066. * NULL
  3067. *
  3068. *
  3069. */
  3070. static char **
  3071. acllas_replace_dn_macro( char *rule, char *matched_val, lasInfo *lasinfo) {
  3072. char **a = NULL;
  3073. char *str = NULL;
  3074. char *patched_rule = NULL;
  3075. char *rule_to_use = NULL;
  3076. char *new_patched_rule = NULL;
  3077. char *rule_prefix = NULL;
  3078. char *rule_suffix = NULL;
  3079. int rule_suffix_len = 0;
  3080. char *comp = NULL;
  3081. int matched_val_len = 0;
  3082. int macro_len = 0;
  3083. int j = 0;
  3084. int has_macro_dn = 0;
  3085. int has_macro_levels = 0;
  3086. /* Determine what the rule's got once */
  3087. if ( strstr(rule, ACL_RULE_MACRO_DN_KEY) != NULL) {
  3088. has_macro_dn = 1;
  3089. }
  3090. if ( strstr(rule, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL) {
  3091. has_macro_levels = 1;
  3092. }
  3093. if ( !has_macro_dn && !has_macro_levels ) {
  3094. /*
  3095. * No $dn thing, just return a list with two elements, rule and NULL.
  3096. * charray_add will create the list and null terminate it.
  3097. */
  3098. charray_add( &a, slapi_ch_strdup(rule));
  3099. return(a);
  3100. } else {
  3101. /*
  3102. * Have an occurrence of the macro rules
  3103. *
  3104. * First, replace all occurrencers of ($dn) with the matched_val
  3105. */
  3106. if ( has_macro_dn) {
  3107. patched_rule =
  3108. acl_replace_str(rule, ACL_RULE_MACRO_DN_KEY, matched_val);
  3109. }
  3110. /* If there are no [$dn] we're done */
  3111. if ( !has_macro_levels ) {
  3112. charray_add( &a, patched_rule);
  3113. return(a);
  3114. } else {
  3115. /*
  3116. * It's a [$dn] type, so walk matched_val, splicing in all
  3117. * the suffix substrings and adding each such string to
  3118. * to the returned list.
  3119. * get_next_component() does not return the commas--the
  3120. * prefix and suffix should come with their commas.
  3121. *
  3122. * All occurrences of each [$dn] are replaced with each level.
  3123. *
  3124. * If has_macro_dn then patched_rule is the rule to strart with,
  3125. * and this needs to be freed at the end, otherwise
  3126. * just use rule.
  3127. */
  3128. if (patched_rule) {
  3129. rule_to_use = patched_rule;
  3130. } else {
  3131. rule_to_use = rule;
  3132. }
  3133. matched_val_len = strlen(matched_val);
  3134. j = 0;
  3135. while( j < matched_val_len) {
  3136. new_patched_rule =
  3137. acl_replace_str(rule_to_use, ACL_RULE_MACRO_DN_LEVELS_KEY,
  3138. &matched_val[j]);
  3139. charray_add( &a, new_patched_rule);
  3140. j += acl_find_comp_end(&matched_val[j]);
  3141. }
  3142. if (patched_rule) {
  3143. slapi_ch_free((void**)&patched_rule);
  3144. }
  3145. return(a);
  3146. }
  3147. }
  3148. }
  3149. /*
  3150. * Here, replace any occurrence of $attr.attrname with the
  3151. * value of attrname from lasinfo->resourceEntry.
  3152. *
  3153. *
  3154. * If there is no $attr thing then the returned list just contains rule
  3155. * itself.
  3156. *
  3157. * eg. rule: cn=fred,ou=*,ou=$attr.ou,o=sun.com
  3158. * ou: People
  3159. * ou: icnc
  3160. *
  3161. * Then we return the list
  3162. * cn=fred,ou=*,ou=People,o=sun.com
  3163. * cn=fred,ou=*,ou=icnc,o=sun.com
  3164. *
  3165. */
  3166. static char **
  3167. acllas_replace_attr_macro( char *rule, lasInfo *lasinfo) {
  3168. char **a = NULL;
  3169. char **working_list = NULL;
  3170. Slapi_Entry *e = lasinfo->resourceEntry;
  3171. char *str, *working_rule;
  3172. char *macro_str, *macro_attr_name;
  3173. int l;
  3174. Slapi_Attr *attr = NULL;
  3175. str = strstr(rule, ACL_RULE_MACRO_ATTR_KEY);
  3176. if ( str == NULL ) {
  3177. charray_add(&a, slapi_ch_strdup(rule));
  3178. return(a);
  3179. } else {
  3180. working_rule = slapi_ch_strdup(rule);
  3181. str = strstr(working_rule, ACL_RULE_MACRO_ATTR_KEY);
  3182. charray_add(&working_list, working_rule );
  3183. while( str != NULL) {
  3184. /*
  3185. * working_rule is the first member of working_list.
  3186. * str points to the next $attr.attrName in working_rule.
  3187. * each member of working_list needs to have each occurence of
  3188. * $attr.atrName replaced with the value of attrName in e.
  3189. * If attrName is multi valued then this generates another
  3190. * list which replaces the old one.
  3191. */
  3192. l = acl_strstr(&str[0], ")");
  3193. macro_str = slapi_ch_malloc(l+2);
  3194. strncpy( macro_str, &str[0], l+1);
  3195. macro_str[l+1] = '\0';
  3196. str = strstr(macro_str, ".");
  3197. str++; /* skip the . */
  3198. l = acl_strstr(&str[0], ")");
  3199. macro_attr_name = slapi_ch_malloc(l+1);
  3200. strncpy( macro_attr_name, &str[0], l);
  3201. macro_attr_name[l] = '\0';
  3202. slapi_entry_attr_find ( e, macro_attr_name, &attr );
  3203. if ( NULL == attr ) {
  3204. /*
  3205. * Here, if a $attr.attrName is such that the attrName
  3206. * does not occur in the entry then return a ""--
  3207. * this will go back to the matching code in
  3208. * aclutil_evaluate_macro() where "" will
  3209. * be taken as the candidate.
  3210. */
  3211. slapi_ch_free((void **)&macro_str);
  3212. slapi_ch_free((void **)&macro_attr_name);
  3213. charray_free(working_list);
  3214. charray_add(&a, slapi_ch_strdup(""));
  3215. return(a);
  3216. } else{
  3217. const struct berval *attrValue;
  3218. Slapi_Value *sval;
  3219. int i, j;
  3220. char *patched_rule;
  3221. a = NULL;
  3222. i= slapi_attr_first_value ( attr, &sval );
  3223. while(i != -1) {
  3224. attrValue = slapi_value_get_berval(sval);
  3225. j = 0;
  3226. while( working_list[j] != NULL) {
  3227. patched_rule =
  3228. acl_replace_str(working_list[j],
  3229. macro_str, attrValue->bv_val);
  3230. charray_add(&a, patched_rule);
  3231. j++;
  3232. }
  3233. i= slapi_attr_next_value( attr, i, &sval );
  3234. }/* while */
  3235. /*
  3236. * Here, a is working_list, where each member has had
  3237. * macro_str replaced with attrVal.
  3238. */
  3239. charray_free(working_list);
  3240. working_list = a;
  3241. working_rule = a[0];
  3242. }
  3243. slapi_ch_free((void **)&macro_str);
  3244. slapi_ch_free((void **)&macro_attr_name);
  3245. str = strstr(working_rule, ACL_RULE_MACRO_ATTR_KEY);
  3246. }/* while */
  3247. return(working_list);
  3248. }
  3249. }
  3250. /*
  3251. * returns ACL_TRUE, ACL_FALSE or ACL_DONT_KNOW.
  3252. *
  3253. * user is a string from the userdn keyword which may contain
  3254. * * components. This routine does the compare component by component, so
  3255. * that * behaves differently to "normal".
  3256. * Any ($dn) or $attr must have been removed from user before this is called.
  3257. */
  3258. static int
  3259. acllas_eval_one_user( struct acl_pblock *aclpb, char * clientDN, char *rule) {
  3260. int exact_match = 0;
  3261. int ret_code = 0;
  3262. const size_t LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
  3263. char *s = NULL;
  3264. /* URL format */
  3265. if ((s = strchr (rule, '?'))!= NULL) {
  3266. /* URL format */
  3267. if (acllas__client_match_URL ( aclpb, clientDN,
  3268. rule) == ACL_TRUE) {
  3269. exact_match = 1;
  3270. }
  3271. } else if ( strstr(rule, "=*") == NULL ) {
  3272. /* Just a straight compare */
  3273. /* skip the ldap:/// part */
  3274. rule += LDAP_URL_prefix_len;
  3275. exact_match = !slapi_utf8casecmp((ACLUCHP)clientDN,
  3276. (ACLUCHP)rule);
  3277. } else{
  3278. /* Here, contains a =*, so need to match comp by comp */
  3279. /* skip the ldap:/// part */
  3280. rule += LDAP_URL_prefix_len;
  3281. ret_code = acl_match_prefix( rule, clientDN, &exact_match);
  3282. }
  3283. if ( exact_match) {
  3284. return( ACL_TRUE);
  3285. } else {
  3286. return(ACL_FALSE);
  3287. }
  3288. }
  3289. /*
  3290. * returns ACL_TRUE, ACL_FALSE and ACL_DONT_KNOW.
  3291. *
  3292. * The user string has had all ($dn) and $attr replaced
  3293. * so the only dodgy thing left is a *.
  3294. *
  3295. * If * appears in such a user string, then it matches only that
  3296. * component, not .*, like it would otherwise.
  3297. *
  3298. */
  3299. static int
  3300. acllas_eval_one_group(char *groupbuf, lasInfo *lasinfo) {
  3301. if (groupbuf) {
  3302. return( acllas__user_ismember_of_group (
  3303. lasinfo->aclpb,
  3304. groupbuf,
  3305. lasinfo->clientDn,
  3306. ACLLAS_CACHE_ALL_GROUPS,
  3307. lasinfo->aclpb->aclpb_clientcert
  3308. ));
  3309. } else {
  3310. return(ACL_FALSE); /* not in the empty group */
  3311. }
  3312. }
  3313. /*
  3314. * returns ACL_TRUE for match, ACL_FALSE for not a match, ACL_DONT_KNOW otherwise.
  3315. */
  3316. static int
  3317. acllas_eval_one_role(char *role, lasInfo *lasinfo) {
  3318. Slapi_DN *roleDN = NULL;
  3319. int rc = ACL_FALSE;
  3320. char ebuf [ BUFSIZ ];
  3321. /*
  3322. * See if lasinfo.clientDn has role rolebuf.
  3323. * Here we know it's not an anom user nor
  3324. * a an anyone user--the client dn must be matched against
  3325. * a real role.
  3326. */
  3327. roleDN = slapi_sdn_new_dn_byval(role);
  3328. if (role) {
  3329. rc = acllas__user_has_role(
  3330. lasinfo->aclpb,
  3331. roleDN,
  3332. lasinfo->aclpb->aclpb_authorization_sdn);
  3333. } else { /* The user does not have the empty role */
  3334. rc = ACL_FALSE;
  3335. }
  3336. slapi_sdn_free(&roleDN );
  3337. /* Some useful logging */
  3338. if (rc == ACL_TRUE ) {
  3339. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  3340. "role evaluation: user '%s' does have role '%s'\n",
  3341. ACL_ESCAPE_STRING_WITH_PUNCTUATION (lasinfo->clientDn, ebuf),
  3342. role);
  3343. } else {
  3344. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  3345. "role evaluation: user '%s' does NOT have role '%s'\n",
  3346. ACL_ESCAPE_STRING_WITH_PUNCTUATION (lasinfo->clientDn, ebuf),
  3347. role);
  3348. }
  3349. return(rc);
  3350. }
  3351. /*
  3352. * returns ACL_TRUE if e matches the filter str, ACL_FALSE if not,
  3353. * ACL_DONT_KNOW otherwise.
  3354. */
  3355. static int acllas_eval_one_target_filter( char * str, Slapi_Entry *e) {
  3356. int rc = ACL_FALSE;
  3357. Slapi_Filter *f = NULL;
  3358. if ((f = slapi_str2filter(str)) == NULL) {
  3359. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  3360. "Warning: Bad targetfilter(%s) in aci: does not match\n", str);
  3361. return(ACL_DONT_KNOW);
  3362. }
  3363. if (slapi_vattr_filter_test(NULL, e, f, 0 /*don't do acess chk*/)!= 0) {
  3364. rc = ACL_FALSE; /* Filter does not match */
  3365. } else {
  3366. rc = ACL_TRUE; /* filter does match */
  3367. }
  3368. slapi_filter_free(f, 1);
  3369. return(rc);
  3370. }
  3371. /***************************************************************************/
  3372. /* E N D */
  3373. /***************************************************************************/