1
0

acllas.c 132 KB

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