acllas.c 111 KB

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