acllas.c 109 KB

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