acllas.c 133 KB

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