1 /* acl.c - routines to parse and check acl's */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
41 #include "slapi/slapi.h"
42 #endif /* LDAPI_SLAPI */
44 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
50 aci_bv_entry = BER_BVC("entry"),
51 aci_bv_children = BER_BVC("children"),
52 aci_bv_onelevel = BER_BVC("onelevel"),
53 aci_bv_subtree = BER_BVC("subtree"),
54 aci_bv_br_entry = BER_BVC("[entry]"),
55 aci_bv_br_all = BER_BVC("[all]"),
56 aci_bv_access_id = BER_BVC("access-id"),
57 aci_bv_anonymous = BER_BVC("anonymous"),
58 aci_bv_public = BER_BVC("public"),
59 aci_bv_users = BER_BVC("users"),
60 aci_bv_self = BER_BVC("self"),
61 aci_bv_dnattr = BER_BVC("dnattr"),
62 aci_bv_group = BER_BVC("group"),
63 aci_bv_role = BER_BVC("role"),
64 aci_bv_set = BER_BVC("set"),
65 aci_bv_set_ref = BER_BVC("set-ref"),
66 aci_bv_grant = BER_BVC("grant"),
67 aci_bv_deny = BER_BVC("deny"),
69 aci_bv_ip_eq = BER_BVC("IP="),
71 aci_bv_path_eq = BER_BVC("PATH="),
72 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
73 #endif /* LDAP_PF_LOCAL */
75 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
76 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
77 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
78 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
79 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
81 typedef enum slap_aci_scope_t {
82 SLAP_ACI_SCOPE_ENTRY = 0x1,
83 SLAP_ACI_SCOPE_CHILDREN = 0x2,
84 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
87 static AccessControl * acl_get(
88 AccessControl *ac, int *count,
89 Operation *op, Entry *e,
90 AttributeDescription *desc,
92 int nmatch, regmatch_t *matches,
93 AccessControlState *state );
95 static slap_control_t acl_mask(
96 AccessControl *ac, slap_mask_t *mask,
97 Operation *op, Entry *e,
98 AttributeDescription *desc,
103 AccessControlState *state );
105 #ifdef SLAPD_ACI_ENABLED
107 Operation *op, Entry *e,
108 AttributeDescription *desc,
113 slap_access_t *grant,
115 slap_aci_scope_t scope);
116 #endif /* SLAPD_ACI_ENABLED */
118 static int regex_matches(
119 struct berval *pat, char *str, char *buf,
120 int nmatch, regmatch_t *matches);
121 static int string_expand(
122 struct berval *newbuf, struct berval *pattern,
123 char *match, int nmatch, regmatch_t *matches);
125 typedef struct AciSetCookie {
130 SLAP_SET_GATHER aci_set_gather;
131 SLAP_SET_GATHER aci_set_gather2;
132 static int aci_match_set ( struct berval *subj, Operation *op,
133 Entry *e, int setref );
136 * access_allowed - check whether op->o_ndn is allowed the requested access
137 * to entry e, attribute attr, value val. if val is null, access to
138 * the whole attribute is assumed (all values).
140 * This routine loops through all access controls and calls
141 * acl_mask() on each applicable access control.
142 * The loop exits when a definitive answer is reached or
143 * or no more controls remain.
150 * - can be legally called with op == NULL
151 * - can be legally called with op->o_bd == NULL
158 AttributeDescription *desc,
160 slap_access_t access,
161 AccessControlState *state,
166 AccessControl *a = NULL;
171 char accessmaskbuf[ACCESSMASK_MAXLEN];
174 slap_control_t control;
175 slap_access_t access_level;
177 regmatch_t matches[MAXREMATCHES];
178 int st_same_attr = 0;
179 static AccessControlState state_init = ACL_STATE_INIT;
182 assert( desc != NULL );
184 access_level = ACL_LEVEL( access );
186 assert( access_level > ACL_NONE );
187 if ( maskp ) ACL_INVALIDATE( *maskp );
189 attr = desc->ad_cname.bv_val;
191 assert( attr != NULL );
193 if( op && op->o_is_auth_check &&
194 ( access_level == ACL_SEARCH || access_level == ACL_READ ))
200 if ( state->as_vd_ad==desc) {
201 if ( state->as_recorded ) {
202 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
205 return state->as_result;
206 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
207 val != NULL && state->as_vd_acl == NULL )
209 return state->as_result;
217 state->as_vd_ad=desc;
220 Debug( LDAP_DEBUG_ACL,
221 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
222 access2str( access ), e->e_dn, attr );
231 be = LDAP_STAILQ_FIRST(&backendDB);
235 * FIXME: experimental; use first backend rules
236 * iff there is no global_acl (ITS#3100) */
237 if ( frontendDB->be_acl == NULL )
243 assert( be != NULL );
246 if ( op->o_pb != NULL ) {
247 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
249 /* ACL plugin denied access */
253 #endif /* LDAP_SLAPI */
255 /* grant database root access */
256 if ( /* be != NULL && */ be_isroot( op ) ) {
257 Debug( LDAP_DEBUG_ACL,
258 "<= root access granted\n",
261 mask = ACL_LVL_MANAGE;
268 * no-user-modification operational attributes are ignored
269 * by ACL_WRITE checking as any found here are not provided
272 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
273 && desc != slap_schema.si_ad_entry
274 && desc != slap_schema.si_ad_children )
276 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
277 " %s access granted\n",
282 /* use backend default access if no backend acls */
283 if( be != NULL && be->be_acl == NULL ) {
284 Debug( LDAP_DEBUG_ACL,
285 "=> access_allowed: backend default %s access %s to \"%s\"\n",
286 access2str( access ),
287 be->be_dfltaccess >= access_level ? "granted" : "denied",
288 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
289 ret = be->be_dfltaccess >= access_level;
294 mask = ACL_PRIV_LEVEL;
295 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
296 mask |= ACL_ACCESS2PRIV( i );
303 /* be is always non-NULL */
304 /* use global default access if no global acls */
305 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
306 Debug( LDAP_DEBUG_ACL,
307 "=> access_allowed: global default %s access %s to \"%s\"\n",
308 access2str( access ),
309 frontendDB->be_dfltaccess >= access_level ? "granted" : "denied", op->o_dn.bv_val );
310 ret = frontendDB->be_dfltaccess >= access_level;
315 mask = ACL_PRIV_LEVEL;
316 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
317 mask |= ACL_ACCESS2PRIV( i );
329 assert( state->as_vd_acl != NULL );
331 a = state->as_vd_acl;
332 count = state->as_vd_acl_count;
333 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
334 mask = state->as_vd_acl_mask;
335 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
340 if ( state ) state->as_vi_acl = NULL;
344 memset(matches, '\0', sizeof(matches));
347 while((a = acl_get( a, &count, op, e, desc, val,
348 MAXREMATCHES, matches, state )) != NULL)
352 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
353 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
354 (int)matches[i].rm_so, (int)matches[i].rm_eo );
355 if( matches[i].rm_so <= matches[0].rm_eo ) {
357 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
358 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
361 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
365 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
366 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
367 ret = state->as_result;
370 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
375 control = acl_mask( a, &mask, op,
376 e, desc, val, MAXREMATCHES, matches, count, state );
378 if ( control != ACL_BREAK ) {
382 memset(matches, '\0', sizeof(matches));
385 if ( ACL_IS_INVALID( mask ) ) {
386 Debug( LDAP_DEBUG_ACL,
387 "=> access_allowed: \"%s\" (%s) invalid!\n",
391 } else if ( control == ACL_BREAK ) {
392 Debug( LDAP_DEBUG_ACL,
393 "=> access_allowed: no more rules\n", 0, 0, 0);
398 Debug( LDAP_DEBUG_ACL,
399 "=> access_allowed: %s access %s by %s\n",
400 access2str( access ),
401 ACL_GRANT(mask, access) ? "granted" : "denied",
402 accessmask2str( mask, accessmaskbuf, 1 ) );
404 ret = ACL_GRANT(mask, access);
407 if( state != NULL ) {
408 /* If not value-dependent, save ACL in case of more attrs */
409 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
410 state->as_vi_acl = a;
411 state->as_result = ret;
413 state->as_recorded |= ACL_STATE_RECORDED;
415 if (be_null) op->o_bd = NULL;
416 if ( maskp ) *maskp = mask;
422 * acl_get - return the acl applicable to entry e, attribute
423 * attr. the acl returned is suitable for use in subsequent calls to
424 * acl_access_allowed().
427 static AccessControl *
433 AttributeDescription *desc,
437 AccessControlState *state )
444 assert( count != NULL );
445 assert( desc != NULL );
447 attr = desc->ad_cname.bv_val;
449 assert( attr != NULL );
452 if( op->o_bd == NULL ) {
453 a = frontendDB->be_acl;
455 a = op->o_bd->be_acl;
466 dnlen = e->e_nname.bv_len;
468 for ( ; a != NULL; a = a->acl_next ) {
471 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
472 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
473 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
474 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
475 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
479 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
480 *count, a->acl_dn_pat.bv_val, 0 );
481 patlen = a->acl_dn_pat.bv_len;
482 if ( dnlen < patlen )
485 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
486 /* base dn -- entire object DN must match */
487 if ( dnlen != patlen )
490 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
491 int rdnlen = -1, sep = 0;
493 if ( dnlen <= patlen )
497 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
502 rdnlen = dn_rdnlen( NULL, &e->e_nname );
503 if ( rdnlen != dnlen - patlen - sep )
506 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
507 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
510 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
511 if ( dnlen <= patlen )
513 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
517 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
521 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
525 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
526 matches[0].rm_so = matches[0].rm_eo = -1;
530 /* Is this ACL only for a specific value? */
531 if ( a->acl_attrval.bv_len ) {
536 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
537 state->as_recorded |= ACL_STATE_RECORDED_VD;
538 state->as_vd_acl = prev;
539 state->as_vd_acl_count = *count;
540 state->as_vd_access = a->acl_access;
541 state->as_vd_access_count = 1;
542 ACL_INVALIDATE( state->as_vd_acl_mask );
545 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
546 Debug( LDAP_DEBUG_ACL,
547 "acl_get: valpat %s\n",
548 a->acl_attrval.bv_val, 0, 0 );
549 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
557 Debug( LDAP_DEBUG_ACL,
559 a->acl_attrval.bv_val, 0, 0 );
561 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
562 if (value_match( &match, desc,
563 desc->ad_type->sat_equality, 0,
564 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
571 patlen = a->acl_attrval.bv_len;
572 vdnlen = val->bv_len;
574 if ( vdnlen < patlen )
577 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
578 if ( vdnlen > patlen )
581 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
584 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
587 rdnlen = dn_rdnlen( NULL, val );
588 if ( rdnlen != vdnlen - patlen - 1 )
591 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
592 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
595 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
596 if ( vdnlen <= patlen )
599 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
603 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
609 if ( a->acl_filter != NULL ) {
610 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
611 if ( rc != LDAP_COMPARE_TRUE ) {
616 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
621 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
633 struct berval *opndn )
636 * if access applies to the entry itself, and the
637 * user is bound as somebody in the same namespace as
638 * the entry, OR the given dn matches the dn pattern
641 * NOTE: styles "anonymous", "users" and "self"
642 * have been moved to enum slap_style_t, whose
643 * value is set in a_dn_style; however, the string
644 * is maintaned in a_dn_pat.
646 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
647 if ( !BER_BVISEMPTY( opndn ) ) {
651 } else if ( b->a_style == ACL_STYLE_USERS ) {
652 if ( BER_BVISEMPTY( opndn ) ) {
656 } else if ( b->a_style == ACL_STYLE_SELF ) {
657 struct berval ndn, selfndn;
660 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
664 level = b->a_self_level;
672 selfndn = e->e_nname;
675 for ( ; level > 0; level-- ) {
676 if ( BER_BVISEMPTY( &ndn ) ) {
679 dnParent( &ndn, &ndn );
682 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
687 } else if ( b->a_style == ACL_STYLE_REGEX ) {
688 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
690 regmatch_t tmp_matches[2],
691 *tmp_matchesp = tmp_matches;
695 switch ( a->acl_dn_style ) {
696 case ACL_STYLE_REGEX:
697 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
698 tmp_matchesp = matches;
702 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
705 tmp_matches[0].rm_so = 0;
706 tmp_matches[0].rm_eo = e->e_nname.bv_len;
711 case ACL_STYLE_SUBTREE:
712 case ACL_STYLE_CHILDREN:
713 tmp_matches[0].rm_so = 0;
714 tmp_matches[0].rm_eo = e->e_nname.bv_len;
715 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
716 tmp_matches[1].rm_eo = e->e_nname.bv_len;
730 if ( !regex_matches( &b->a_pat, opndn->bv_val,
731 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
739 ber_len_t patlen, odnlen;
742 if ( e->e_dn == NULL )
747 char buf[ACL_BUF_SIZE];
750 regmatch_t tmp_matches[2],
751 *tmp_matchesp = tmp_matches;
755 bv.bv_len = sizeof( buf ) - 1;
758 switch ( a->acl_dn_style ) {
759 case ACL_STYLE_REGEX:
760 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
761 tmp_matchesp = matches;
765 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
768 tmp_matches[0].rm_so = 0;
769 tmp_matches[0].rm_eo = e->e_nname.bv_len;
774 case ACL_STYLE_SUBTREE:
775 case ACL_STYLE_CHILDREN:
776 tmp_matches[0].rm_so = 0;
777 tmp_matches[0].rm_eo = e->e_nname.bv_len;
778 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
779 tmp_matches[1].rm_eo = e->e_nname.bv_len;
793 if ( string_expand( &bv, &b->a_pat,
795 tmp_nmatch, tmp_matchesp ) )
800 if ( dnNormalize(0, NULL, NULL, &bv,
801 &pat, op->o_tmpmemctx )
804 /* did not expand to a valid dn */
813 odnlen = opndn->bv_len;
814 if ( odnlen < patlen ) {
815 goto dn_match_cleanup;
819 if ( b->a_style == ACL_STYLE_BASE ) {
820 /* base dn -- entire object DN must match */
821 if ( odnlen != patlen ) {
822 goto dn_match_cleanup;
825 } else if ( b->a_style == ACL_STYLE_ONE ) {
828 if ( odnlen <= patlen ) {
829 goto dn_match_cleanup;
832 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
833 goto dn_match_cleanup;
836 rdnlen = dn_rdnlen( NULL, opndn );
837 if ( rdnlen != odnlen - patlen - 1 ) {
838 goto dn_match_cleanup;
841 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
842 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
843 goto dn_match_cleanup;
846 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
847 if ( odnlen <= patlen ) {
848 goto dn_match_cleanup;
851 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
852 goto dn_match_cleanup;
855 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
859 if ( odnlen <= patlen ) {
860 goto dn_match_cleanup;
863 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
865 goto dn_match_cleanup;
870 for ( ; level > 0; level-- ) {
871 if ( BER_BVISEMPTY( &ndn ) ) {
872 goto dn_match_cleanup;
874 dnParent( &ndn, &ndn );
875 if ( ndn.bv_len < patlen ) {
876 goto dn_match_cleanup;
880 if ( ndn.bv_len != patlen ) {
881 goto dn_match_cleanup;
885 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
888 if ( pat.bv_val != b->a_pat.bv_val ) {
889 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
901 * Record value-dependent access control state
903 #define ACL_RECORD_VALUE_STATE do { \
904 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
905 state->as_recorded |= ACL_STATE_RECORDED_VD; \
906 state->as_vd_acl = a; \
907 AC_MEMCPY( state->as_vd_acl_matches, matches, \
908 sizeof( state->as_vd_acl_matches )) ; \
909 state->as_vd_acl_count = count; \
910 state->as_vd_access = b; \
911 state->as_vd_access_count = i; \
925 AccessControlState *state,
927 struct berval *opndn )
933 const char *attr = bdn->a_at->ad_cname.bv_val;
935 assert( attr != NULL );
937 if ( BER_BVISEMPTY( opndn ) ) {
941 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
944 /* see if asker is listed in dnattr */
945 for ( at = attrs_find( e->e_attrs, bdn->a_at );
947 at = attrs_find( at->a_next, bdn->a_at ) )
949 if ( value_find_ex( bdn->a_at,
950 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
951 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
953 &bv, op->o_tmpmemctx ) == 0 )
962 /* have a dnattr match. if this is a self clause then
963 * the target must also match the op dn.
966 /* check if the target is an attribute. */
967 if ( val == NULL ) return 1;
969 /* target is attribute, check if the attribute value
972 rc = value_match( &match, bdn->a_at,
973 bdn->a_at->ad_type->sat_equality, 0,
975 /* on match error or no match, fail the ACL clause */
976 if ( rc != LDAP_SUCCESS || match != 0 )
981 /* no dnattr match, check if this is a self clause */
985 ACL_RECORD_VALUE_STATE;
987 /* this is a self clause, check if the target is an
993 /* target is attribute, check if the attribute value
996 rc = value_match( &match, bdn->a_at,
997 bdn->a_at->ad_type->sat_equality, 0,
1000 /* on match error or no match, fail the ACL clause */
1001 if ( rc != LDAP_SUCCESS || match != 0 )
1010 * acl_mask - modifies mask based upon the given acl and the
1011 * requested access to entry e, attribute attr, value val. if val
1012 * is null, access to the whole attribute is assumed (all values).
1014 * returns 0 access NOT allowed
1018 static slap_control_t
1024 AttributeDescription *desc,
1027 regmatch_t *matches,
1029 AccessControlState *state )
1034 char accessmaskbuf[ACCESSMASK_MAXLEN];
1035 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1036 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1037 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1041 assert( a != NULL );
1042 assert( mask != NULL );
1043 assert( desc != NULL );
1045 attr = desc->ad_cname.bv_val;
1047 assert( attr != NULL );
1049 Debug( LDAP_DEBUG_ACL,
1050 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1053 Debug( LDAP_DEBUG_ACL,
1054 "=> acl_mask: to %s by \"%s\", (%s) \n",
1055 val ? "value" : "all values",
1056 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1057 accessmask2str( *mask, accessmaskbuf, 1) );
1060 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1061 && state->as_vd_acl == a )
1063 b = state->as_vd_access;
1064 i = state->as_vd_access_count;
1071 for ( ; b != NULL; b = b->a_next, i++ ) {
1072 slap_mask_t oldmask, modmask;
1074 ACL_INVALIDATE( modmask );
1076 /* AND <who> clauses */
1077 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1078 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1079 b->a_dn_pat.bv_val, 0, 0);
1081 * if access applies to the entry itself, and the
1082 * user is bound as somebody in the same namespace as
1083 * the entry, OR the given dn matches the dn pattern
1086 * NOTE: styles "anonymous", "users" and "self"
1087 * have been moved to enum slap_style_t, whose
1088 * value is set in a_dn_style; however, the string
1089 * is maintaned in a_dn_pat.
1092 if ( acl_mask_dn( op, e, a, nmatch, matches,
1093 &b->a_dn, &op->o_ndn ) )
1099 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1102 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1103 b->a_realdn_pat.bv_val, 0, 0);
1105 * if access applies to the entry itself, and the
1106 * user is bound as somebody in the same namespace as
1107 * the entry, OR the given dn matches the dn pattern
1110 * NOTE: styles "anonymous", "users" and "self"
1111 * have been moved to enum slap_style_t, whose
1112 * value is set in a_dn_style; however, the string
1113 * is maintaned in a_dn_pat.
1116 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1118 ndn = op->o_conn->c_ndn;
1123 if ( acl_mask_dn( op, e, a, nmatch, matches,
1124 &b->a_realdn, &ndn ) )
1130 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1131 if ( ! op->o_conn->c_listener ) {
1134 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1135 b->a_sockurl_pat.bv_val, 0, 0 );
1137 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1138 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1139 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1140 e->e_ndn, nmatch, matches ) )
1145 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1147 char buf[ACL_BUF_SIZE];
1149 bv.bv_len = sizeof( buf ) - 1;
1151 if ( string_expand( &bv, &b->a_sockurl_pat,
1152 e->e_ndn, nmatch, matches ) )
1157 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1163 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1171 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1172 if ( !op->o_conn->c_peer_domain.bv_val ) {
1175 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1176 b->a_domain_pat.bv_val, 0, 0 );
1177 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1178 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1179 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1180 e->e_ndn, nmatch, matches ) )
1185 char buf[ACL_BUF_SIZE];
1187 struct berval cmp = op->o_conn->c_peer_domain;
1188 struct berval pat = b->a_domain_pat;
1190 if ( b->a_domain_expand ) {
1193 bv.bv_len = sizeof(buf) - 1;
1196 if ( string_expand(&bv, &b->a_domain_pat,
1197 e->e_ndn, nmatch, matches) )
1204 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1205 int offset = cmp.bv_len - pat.bv_len;
1210 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1214 /* trim the domain */
1215 cmp.bv_val = &cmp.bv_val[ offset ];
1216 cmp.bv_len -= offset;
1219 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1226 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1227 if ( !op->o_conn->c_peer_name.bv_val ) {
1230 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1231 b->a_peername_pat.bv_val, 0, 0 );
1232 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1233 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1234 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1235 e->e_ndn, nmatch, matches ) )
1241 /* try exact match */
1242 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1243 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1247 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1249 char buf[ACL_BUF_SIZE];
1251 bv.bv_len = sizeof( buf ) - 1;
1253 if ( string_expand( &bv, &b->a_peername_pat,
1254 e->e_ndn, nmatch, matches ) )
1259 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1263 /* extract IP and try exact match */
1264 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1266 char buf[] = "255.255.255.255";
1269 int port_number = -1;
1271 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1272 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1275 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1276 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1278 port = strrchr( ip.bv_val, ':' );
1282 ip.bv_len = port - ip.bv_val;
1284 port_number = strtol( port, &next, 10 );
1285 if ( next[0] != '\0' )
1289 /* the port check can be anticipated here */
1290 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1293 /* address longer than expected? */
1294 if ( ip.bv_len >= sizeof(buf) )
1297 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1298 buf[ ip.bv_len ] = '\0';
1300 addr = inet_addr( buf );
1302 /* unable to convert? */
1303 if ( addr == (unsigned long)(-1) )
1306 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1309 #ifdef LDAP_PF_LOCAL
1310 /* extract path and try exact match */
1311 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1314 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1315 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1318 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1319 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1321 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1324 #endif /* LDAP_PF_LOCAL */
1326 /* exact match (very unlikely...) */
1327 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1334 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1335 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1338 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1339 b->a_sockname_pat.bv_val, 0, 0 );
1340 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1341 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1342 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1343 e->e_ndn, nmatch, matches ) )
1348 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1350 char buf[ACL_BUF_SIZE];
1352 bv.bv_len = sizeof( buf ) - 1;
1354 if ( string_expand( &bv, &b->a_sockname_pat,
1355 e->e_ndn, nmatch, matches ) )
1360 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1365 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1372 if ( b->a_dn_at != NULL ) {
1373 if ( acl_mask_dnattr( op, e, val, a, b, i,
1374 matches, count, state,
1375 &b->a_dn, &op->o_ndn ) )
1381 if ( b->a_realdn_at != NULL ) {
1384 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1386 ndn = op->o_conn->c_ndn;
1391 if ( acl_mask_dnattr( op, e, val, a, b, i,
1392 matches, count, state,
1393 &b->a_realdn, &ndn ) )
1399 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1401 struct berval ndn = BER_BVNULL;
1404 if ( op->o_ndn.bv_len == 0 ) {
1408 /* b->a_group is an unexpanded entry name, expanded it should be an
1409 * entry with objectclass group* and we test to see if odn is one of
1410 * the values in the attribute group
1412 /* see if asker is listed in dnattr */
1413 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1414 char buf[ACL_BUF_SIZE];
1416 regmatch_t tmp_matches[2],
1417 *tmp_matchesp = tmp_matches;
1419 bv.bv_len = sizeof(buf) - 1;
1424 switch ( a->acl_dn_style ) {
1425 case ACL_STYLE_REGEX:
1426 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1427 tmp_matchesp = matches;
1428 tmp_nmatch = nmatch;
1432 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1433 case ACL_STYLE_BASE:
1434 tmp_matches[0].rm_so = 0;
1435 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1440 case ACL_STYLE_SUBTREE:
1441 case ACL_STYLE_CHILDREN:
1442 tmp_matches[0].rm_so = 0;
1443 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1444 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1445 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1459 if ( string_expand( &bv, &b->a_group_pat,
1461 tmp_nmatch, tmp_matchesp ) )
1466 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1467 op->o_tmpmemctx ) != LDAP_SUCCESS )
1469 /* did not expand to a valid dn */
1476 bv = b->a_group_pat;
1479 rc = backend_group( op, e, &bv, &op->o_ndn,
1480 b->a_group_oc, b->a_group_at );
1483 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1491 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1493 char buf[ACL_BUF_SIZE];
1495 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1497 regmatch_t tmp_matches[2],
1498 *tmp_matchesp = tmp_matches;
1501 bv.bv_len = sizeof( buf ) - 1;
1506 switch ( a->acl_dn_style ) {
1507 case ACL_STYLE_REGEX:
1508 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1509 tmp_matchesp = matches;
1510 tmp_nmatch = nmatch;
1514 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1515 case ACL_STYLE_BASE:
1516 tmp_matches[0].rm_so = 0;
1517 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1522 case ACL_STYLE_SUBTREE:
1523 case ACL_STYLE_CHILDREN:
1524 tmp_matches[0].rm_so = 0;
1525 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1526 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1527 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1541 if ( string_expand( &bv, &b->a_set_pat,
1543 tmp_nmatch, tmp_matchesp ) )
1552 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1557 if ( b->a_authz.sai_ssf ) {
1558 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1559 b->a_authz.sai_ssf, op->o_ssf, 0 );
1560 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1565 if ( b->a_authz.sai_transport_ssf ) {
1566 Debug( LDAP_DEBUG_ACL,
1567 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1568 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1569 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1574 if ( b->a_authz.sai_tls_ssf ) {
1575 Debug( LDAP_DEBUG_ACL,
1576 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1577 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1578 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1583 if ( b->a_authz.sai_sasl_ssf ) {
1584 Debug( LDAP_DEBUG_ACL,
1585 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1586 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1587 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1593 if ( b->a_dynacl ) {
1595 slap_access_t tgrant, tdeny;
1597 /* this case works different from the others above.
1598 * since aci's themselves give permissions, we need
1599 * to first check b->a_access_mask, the ACL's access level.
1601 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1602 /* no ACIs in the root DSE */
1606 /* first check if the right being requested
1607 * is allowed by the ACL clause.
1609 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1613 /* start out with nothing granted, nothing denied */
1617 for ( da = b->a_dynacl; da; da = da->da_next ) {
1618 slap_access_t grant, deny;
1620 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1626 /* remove anything that the ACL clause does not allow */
1627 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1628 tdeny &= ACL_PRIV_MASK;
1630 /* see if we have anything to contribute */
1631 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1635 /* this could be improved by changing acl_mask so that it can deal with
1636 * by clauses that return grant/deny pairs. Right now, it does either
1637 * additive or subtractive rights, but not both at the same time. So,
1638 * we need to combine the grant/deny pair into a single rights mask in
1639 * a smart way: if either grant or deny is "empty", then we use the
1640 * opposite as is, otherwise we remove any denied rights from the grant
1641 * rights mask and construct an additive mask.
1643 if (ACL_IS_INVALID(tdeny)) {
1644 modmask = tgrant | ACL_PRIV_ADDITIVE;
1646 } else if (ACL_IS_INVALID(tgrant)) {
1647 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1650 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1654 #else /* !SLAP_DYNACL */
1656 #ifdef SLAPD_ACI_ENABLED
1657 if ( b->a_aci_at != NULL ) {
1659 slap_access_t grant, deny, tgrant, tdeny;
1660 struct berval parent_ndn;
1661 BerVarray bvals = NULL;
1664 /* this case works different from the others above.
1665 * since aci's themselves give permissions, we need
1666 * to first check b->a_access_mask, the ACL's access level.
1669 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1670 /* no ACIs in the root DSE */
1674 /* first check if the right being requested
1675 * is allowed by the ACL clause.
1677 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1680 /* start out with nothing granted, nothing denied */
1684 /* get the aci attribute */
1685 at = attr_find( e->e_attrs, b->a_aci_at );
1688 /* FIXME: this breaks acl caching;
1689 * see also ACL_RECORD_VALUE_STATE below */
1690 ACL_RECORD_VALUE_STATE;
1692 /* the aci is an multi-valued attribute. The
1693 * rights are determined by OR'ing the individual
1694 * rights given by the acis.
1696 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1701 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
1707 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1708 accessmask2str(tgrant, accessmaskbuf, 1),
1709 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1712 /* If the entry level aci didn't contain anything valid for the
1713 * current operation, climb up the tree and evaluate the
1714 * acis with scope set to subtree
1716 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
1717 dnParent( &e->e_nname, &parent_ndn );
1718 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
1719 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1720 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1728 for( i = 0; bvals[i].bv_val != NULL; i++){
1730 /* FIXME: this breaks acl caching;
1731 * see also ACL_RECORD_VALUE_STATE above */
1732 ACL_RECORD_VALUE_STATE;
1734 if (aci_mask(op, e, desc, val, &bvals[i],
1736 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
1740 /* evaluation stops as soon as either a "deny" or a
1741 * "grant" directive matches.
1743 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1747 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1748 accessmask2str(tgrant, accessmaskbuf, 1),
1749 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1753 case LDAP_NO_SUCH_ATTRIBUTE:
1754 /* just go on if the aci-Attribute is not present in
1757 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1761 case LDAP_NO_SUCH_OBJECT:
1762 /* We have reached the base object */
1763 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1774 dnParent( &parent_ndn, &parent_ndn );
1779 /* remove anything that the ACL clause does not allow */
1780 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1781 tdeny &= ACL_PRIV_MASK;
1783 /* see if we have anything to contribute */
1784 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1788 /* this could be improved by changing acl_mask so that it can deal with
1789 * by clauses that return grant/deny pairs. Right now, it does either
1790 * additive or subtractive rights, but not both at the same time. So,
1791 * we need to combine the grant/deny pair into a single rights mask in
1792 * a smart way: if either grant or deny is "empty", then we use the
1793 * opposite as is, otherwise we remove any denied rights from the grant
1794 * rights mask and construct an additive mask.
1796 if (ACL_IS_INVALID(tdeny)) {
1797 modmask = tgrant | ACL_PRIV_ADDITIVE;
1799 } else if (ACL_IS_INVALID(tgrant)) {
1800 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1803 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1807 #endif /* SLAPD_ACI_ENABLED */
1808 #endif /* !SLAP_DYNACL */
1810 modmask = b->a_access_mask;
1813 Debug( LDAP_DEBUG_ACL,
1814 "<= acl_mask: [%d] applying %s (%s)\n",
1815 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1816 b->a_type == ACL_CONTINUE
1818 : b->a_type == ACL_BREAK
1824 if( ACL_IS_ADDITIVE(modmask) ) {
1826 ACL_PRIV_SET( *mask, modmask );
1829 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1831 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1832 /* substract privs */
1833 ACL_PRIV_CLR( *mask, modmask );
1836 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1843 Debug( LDAP_DEBUG_ACL,
1844 "<= acl_mask: [%d] mask: %s\n",
1845 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1847 if( b->a_type == ACL_CONTINUE ) {
1850 } else if ( b->a_type == ACL_BREAK ) {
1858 /* implicit "by * none" clause */
1861 Debug( LDAP_DEBUG_ACL,
1862 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1863 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1868 * acl_check_modlist - check access control on the given entry to see if
1869 * it allows the given modifications by the user associated with op.
1870 * returns 1 if mods allowed ok
1871 * 0 mods not allowed
1878 Modifications *mlist
1882 AccessControlState state = ACL_STATE_INIT;
1885 int ret = 1; /* default is access allowed */
1889 be = LDAP_STAILQ_FIRST(&backendDB);
1893 assert( be != NULL );
1895 /* short circuit root database access */
1896 if ( be_isroot( op ) ) {
1897 Debug( LDAP_DEBUG_ACL,
1898 "<= acl_access_allowed: granted to database root\n",
1903 /* use backend default access if no backend acls */
1904 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1905 Debug( LDAP_DEBUG_ACL,
1906 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1907 access2str( ACL_WRITE ),
1908 op->o_bd->be_dfltaccess >= ACL_WRITE
1909 ? "granted" : "denied",
1911 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1915 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1917 * no-user-modification operational attributes are ignored
1918 * by ACL_WRITE checking as any found here are not provided
1921 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1922 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1923 " modify access granted\n",
1924 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1928 switch ( mlist->sml_op ) {
1929 case LDAP_MOD_REPLACE:
1931 * We must check both permission to delete the whole
1932 * attribute and permission to add the specific attributes.
1933 * This prevents abuse from selfwriters.
1935 if ( ! access_allowed( op, e,
1936 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
1942 if ( mlist->sml_values == NULL ) break;
1944 /* fall thru to check value to add */
1947 assert( mlist->sml_values != NULL );
1949 for ( bv = mlist->sml_nvalues
1950 ? mlist->sml_nvalues : mlist->sml_values;
1951 bv->bv_val != NULL; bv++ )
1953 if ( ! access_allowed( op, e,
1954 mlist->sml_desc, bv, ACL_WADD, &state ) )
1962 case LDAP_MOD_DELETE:
1963 if ( mlist->sml_values == NULL ) {
1964 if ( ! access_allowed( op, e,
1965 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
1972 for ( bv = mlist->sml_nvalues
1973 ? mlist->sml_nvalues : mlist->sml_values;
1974 bv->bv_val != NULL; bv++ )
1976 if ( ! access_allowed( op, e,
1977 mlist->sml_desc, bv, ACL_WDEL, &state ) )
1985 case SLAP_MOD_SOFTADD:
1986 /* allow adding attribute via modrdn thru */
1998 if (be_null) op->o_bd = NULL;
2004 struct berval *list,
2017 while ( len >= 0 && --ix >= 0 ) {
2018 while ( --len >= 0 && *p++ != sep )
2021 while ( len >= 0 && *p == ' ' ) {
2034 while ( --len >= 0 && *p != sep ) {
2038 while ( bv->bv_len > 0 && *--p == ' ' ) {
2045 typedef struct aci_set_gather_t {
2051 aci_set_cb_gather( Operation *op, SlapReply *rs )
2053 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2055 if ( rs->sr_type == REP_SEARCH ) {
2056 BerValue bvals[ 2 ];
2057 BerVarray bvalsp = NULL;
2060 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2061 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2063 if ( desc == slap_schema.si_ad_entryDN ) {
2065 bvals[ 0 ] = rs->sr_entry->e_nname;
2066 BER_BVZERO( &bvals[ 1 ] );
2071 a = attr_find( rs->sr_entry->e_attrs, desc );
2075 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2078 bvalsp = a->a_nvals;
2084 p->bvals = slap_set_join( p->cookie, p->bvals,
2085 ( '|' | SLAP_SET_RREF ), bvalsp );
2089 assert( rs->sr_type == REP_RESULT );
2096 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2098 AciSetCookie *cp = (AciSetCookie *)cookie;
2100 LDAPURLDesc *ludp = NULL;
2101 Operation op2 = { 0 };
2102 SlapReply rs = {REP_RESULT};
2103 AttributeName anlist[ 2 ], *anlistp = NULL;
2105 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2106 aci_set_gather_t p = { 0 };
2107 const char *text = NULL;
2108 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2110 /* this routine needs to return the bervals instead of
2111 * plain strings, since syntax is not known. It should
2112 * also return the syntax or some "comparison cookie".
2114 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2115 return aci_set_gather2( cookie, name, desc );
2118 rc = ldap_url_parse( name->bv_val, &ludp );
2119 if ( rc != LDAP_URL_SUCCESS ) {
2120 rc = LDAP_PROTOCOL_ERROR;
2124 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2126 /* host part must be empty */
2127 /* extensions parts must be empty */
2128 rc = LDAP_PROTOCOL_ERROR;
2132 /* Grab the searchbase and see if an appropriate database can be found */
2133 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2134 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2135 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2136 BER_BVZERO( &op2.o_req_dn );
2137 if ( rc != LDAP_SUCCESS ) {
2141 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2142 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2143 rc = LDAP_NO_SUCH_OBJECT;
2147 /* Grab the filter */
2148 if ( ludp->lud_filter ) {
2149 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2150 cp->op->o_tmpmemctx );
2153 op2.ors_filterstr = defaultFilter_bv;
2156 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2157 if ( op2.ors_filter == NULL ) {
2158 rc = LDAP_PROTOCOL_ERROR;
2162 /* Grab the scope */
2163 op2.ors_scope = ludp->lud_scope;
2165 /* Grap the attributes */
2166 if ( ludp->lud_attrs ) {
2167 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2170 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2171 cp->op->o_tmpmemctx );
2173 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2174 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2175 anlistp[ nattrs ].an_desc = NULL;
2176 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2177 &anlistp[ nattrs ].an_desc, &text );
2178 if ( rc != LDAP_SUCCESS ) {
2187 anlistp[ nattrs ].an_name = desc->ad_cname;
2188 anlistp[ nattrs ].an_desc = desc;
2190 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2194 op2.o_hdr = cp->op->o_hdr;
2195 op2.o_tag = LDAP_REQ_SEARCH;
2196 op2.o_ndn = op2.o_bd->be_rootndn;
2197 op2.o_callback = &cb;
2198 op2.o_time = slap_get_time();
2199 op2.o_do_not_cache = 1;
2200 op2.o_is_auth_check = 0;
2201 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2202 op2.ors_slimit = SLAP_NO_LIMIT;
2203 op2.ors_tlimit = SLAP_NO_LIMIT;
2204 op2.ors_attrs = anlistp;
2205 op2.ors_attrsonly = 0;
2206 op2.o_private = cp->op->o_private;
2210 rc = op2.o_bd->be_search( &op2, &rs );
2216 if ( op2.ors_filter ) {
2217 filter_free_x( cp->op, op2.ors_filter );
2219 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2220 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2222 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2223 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2226 ldap_free_urldesc( ludp );
2228 if ( anlistp && anlistp != anlist ) {
2229 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2236 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2238 AciSetCookie *cp = (AciSetCookie *)cookie;
2239 BerVarray bvals = NULL;
2243 /* this routine needs to return the bervals instead of
2244 * plain strings, since syntax is not known. It should
2245 * also return the syntax or some "comparison cookie".
2247 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2248 if ( rc == LDAP_SUCCESS ) {
2249 if ( desc == slap_schema.si_ad_entryDN ) {
2250 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2251 cp->op->o_tmpmemctx );
2253 BER_BVZERO( &bvals[ 1 ] );
2257 backend_attribute( cp->op,
2258 cp->e, &ndn, desc, &bvals, ACL_NONE );
2261 if ( !BER_BVISNULL( &ndn ) ) {
2262 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2271 struct berval *subj,
2277 struct berval set = BER_BVNULL;
2279 AciSetCookie cookie;
2281 if ( setref == 0 ) {
2282 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2285 struct berval subjdn, ndn = BER_BVNULL;
2286 struct berval setat;
2289 AttributeDescription *desc = NULL;
2291 /* format of string is "entry/setAttrName" */
2292 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2296 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2297 setat = aci_bv_set_attr;
2301 * NOTE: dnNormalize honors the ber_len field
2302 * as the length of the dn to be normalized
2304 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2305 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2307 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2308 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2312 BER_BVZERO( &bvals[0] );
2313 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2315 bvals[0].bv_val = bvals[i-1].bv_val;
2316 BER_BVZERO( &bvals[i-1] );
2318 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2319 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2324 if ( !BER_BVISNULL( &set ) ) {
2327 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2328 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2329 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2335 #ifdef SLAPD_ACI_ENABLED
2337 aci_list_map_rights(
2338 struct berval *list )
2345 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2348 switch (*bv.bv_val) {
2350 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2353 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2354 * the right 's' to mean "set", but in the examples states
2355 * that the right 's' means "search". The latter definition
2358 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2361 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2364 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2367 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2368 * define any equivalent to the AUTH right, so I've just used
2371 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2383 struct berval *list,
2384 const struct berval *attr,
2385 struct berval *val )
2387 struct berval bv, left, right;
2390 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2391 if (aci_get_part(&bv, 0, '=', &left) < 0
2392 || aci_get_part(&bv, 1, '=', &right) < 0)
2394 if (ber_bvstrcasecmp(attr, &bv) == 0)
2396 } else if (val == NULL) {
2397 if (ber_bvstrcasecmp(attr, &left) == 0)
2400 if (ber_bvstrcasecmp(attr, &left) == 0) {
2401 /* this is experimental code that implements a
2402 * simple (prefix) match of the attribute value.
2403 * the ACI draft does not provide for aci's that
2404 * apply to specific values, but it would be
2405 * nice to have. If the <attr> part of an aci's
2406 * rights list is of the form <attr>=<value>,
2407 * that means the aci applies only to attrs with
2408 * the given value. Furthermore, if the attr is
2409 * of the form <attr>=<value>*, then <value> is
2410 * treated as a prefix, and the aci applies to
2411 * any value with that prefix.
2413 * Ideally, this would allow r.e. matches.
2415 if (aci_get_part(&right, 0, '*', &left) < 0
2416 || right.bv_len <= left.bv_len)
2418 if (ber_bvstrcasecmp(val, &right) == 0)
2420 } else if (val->bv_len >= left.bv_len) {
2421 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2430 static slap_access_t
2431 aci_list_get_attr_rights(
2432 struct berval *list,
2433 const struct berval *attr,
2434 struct berval *val )
2440 /* loop through each rights/attr pair, skip first part (action) */
2442 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2443 if (aci_list_has_attr(&bv, attr, val) == 0)
2445 if (aci_get_part(list, i, ';', &bv) < 0)
2447 mask |= aci_list_map_rights(&bv);
2453 aci_list_get_rights(
2454 struct berval *list,
2455 const struct berval *attr,
2457 slap_access_t *grant,
2458 slap_access_t *deny )
2460 struct berval perm, actn;
2461 slap_access_t *mask;
2464 if (attr == NULL || attr->bv_len == 0
2465 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2466 attr = &aci_bv_br_entry;
2472 /* loop through each permissions clause */
2473 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2474 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2476 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2478 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2485 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2486 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2493 struct berval *subj,
2494 struct berval *defgrpoc,
2495 struct berval *defgrpat,
2502 struct berval subjdn;
2503 struct berval grpoc;
2504 struct berval grpat;
2505 ObjectClass *grp_oc = NULL;
2506 AttributeDescription *grp_ad = NULL;
2510 /* format of string is "group/objectClassValue/groupAttrName" */
2511 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2515 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2519 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2523 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2524 if( rc != LDAP_SUCCESS ) {
2530 grp_oc = oc_bvfind( &grpoc );
2532 if (grp_oc != NULL && grp_ad != NULL ) {
2533 char buf[ACL_BUF_SIZE];
2534 struct berval bv, ndn;
2535 bv.bv_len = sizeof( buf ) - 1;
2536 bv.bv_val = (char *)&buf;
2537 if ( string_expand(&bv, &subjdn,
2538 e->e_ndn, nmatch, matches) )
2543 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2544 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2545 grp_oc, grp_ad ) == 0 );
2546 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2558 AttributeDescription *desc,
2562 regmatch_t *matches,
2563 slap_access_t *grant,
2564 slap_access_t *deny,
2565 slap_aci_scope_t asserted_scope
2568 struct berval bv, scope, perms, type, sdn;
2572 assert( !BER_BVISNULL( &desc->ad_cname ) );
2574 /* parse an aci of the form:
2575 oid # scope # action;rights;attr;rights;attr
2576 $ action;rights;attr;rights;attr # type # subject
2578 [NOTE: the following comment is very outdated,
2579 as the draft version it refers to (Ando, 2004-11-20)].
2581 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2582 a full description of the format for this attribute.
2583 Differences: "this" in the draft is "self" here, and
2584 "self" and "public" is in the position of type.
2586 <scope> = {entry|children|subtree}
2587 <type> = {public|users|access-id|subtree|onelevel|children|
2588 self|dnattr|group|role|set|set-ref}
2590 This routine now supports scope={ENTRY,CHILDREN}
2592 - ENTRY applies to "entry" and "subtree";
2593 - CHILDREN aplies to "children" and "subtree"
2596 /* check that the aci has all 5 components */
2597 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2601 /* check that the aci family is supported */
2602 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2606 /* check that the scope matches */
2607 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2611 /* note: scope can be either ENTRY or CHILDREN;
2612 * they respectively match "entry" and "children" in bv
2613 * both match "subtree" */
2614 switch ( asserted_scope ) {
2615 case SLAP_ACI_SCOPE_ENTRY:
2616 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2617 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2623 case SLAP_ACI_SCOPE_CHILDREN:
2624 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2625 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2635 /* get the list of permissions clauses, bail if empty */
2636 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2640 /* check if any permissions allow desired access */
2641 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2645 /* see if we have a DN match */
2646 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2650 /* see if we have a public (i.e. anonymous) access */
2651 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2655 /* otherwise require an identity */
2656 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2660 /* see if we have a users access */
2661 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2665 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2666 * just grab all the berval up to its end (ITS#3303).
2667 * NOTE: the problem could be solved by providing the DN with
2668 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
2669 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2671 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2675 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2676 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2678 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2681 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2682 if ( rc != LDAP_SUCCESS ) {
2686 if ( dn_match( &op->o_ndn, &ndn ) ) {
2689 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2693 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2696 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2697 if ( rc != LDAP_SUCCESS ) {
2701 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2704 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2708 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2709 struct berval ndn, pndn;
2711 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2712 if ( rc != LDAP_SUCCESS ) {
2716 dnParent( &ndn, &pndn );
2718 if ( dn_match( &op->o_ndn, &pndn ) ) {
2721 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2725 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2728 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2729 if ( rc != LDAP_SUCCESS ) {
2733 if ( !dn_match( &op->o_ndn, &ndn )
2734 && dnIsSuffix( &op->o_ndn, &ndn ) )
2738 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2742 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2743 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2747 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2749 AttributeDescription *ad = NULL;
2752 rc = slap_bv2ad( &sdn, &ad, &text );
2754 if( rc != LDAP_SUCCESS ) {
2760 for ( at = attrs_find( e->e_attrs, ad );
2762 at = attrs_find( at->a_next, ad ) )
2764 if ( value_find_ex( ad,
2765 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2766 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2768 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2777 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2778 if ( aci_group_member( &sdn, &aci_bv_group_class,
2779 &aci_bv_group_attr, op, e, nmatch, matches ) )
2784 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2785 if ( aci_group_member( &sdn, &aci_bv_role_class,
2786 &aci_bv_role_attr, op, e, nmatch, matches ) )
2791 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2792 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2796 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2797 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2807 * FIXME: there is a silly dependence that makes it difficult
2808 * to move ACIs in a run-time loadable module under the "dynacl"
2809 * umbrella, because sets share some helpers with ACIs.
2812 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2814 AttributeDescription *ad = NULL;
2815 const char *text = NULL;
2817 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2818 fprintf( stderr, "%s: line %d: "
2819 "inappropriate style \"%s\" in \"aci\" by clause\n",
2820 fname, lineno, sty );
2824 if ( right != NULL && *right != '\0' ) {
2825 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2827 "%s: line %d: aci \"%s\": %s\n",
2828 fname, lineno, right, text );
2833 ad = slap_schema.si_ad_aci;
2836 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2837 fprintf( stderr, "%s: line %d: "
2838 "aci \"%s\": inappropriate syntax: %s\n",
2839 fname, lineno, right,
2840 ad->ad_type->sat_syntax_oid );
2844 *privp = (void *)ad;
2850 dynacl_aci_unparse( void *priv, struct berval *bv )
2852 AttributeDescription *ad = ( AttributeDescription * )priv;
2857 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
2858 ptr = lutil_strcopy( bv->bv_val, " aci=" );
2859 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
2860 bv->bv_len = ptr - bv->bv_val;
2871 AttributeDescription *desc,
2874 regmatch_t *matches,
2875 slap_access_t *grantp,
2876 slap_access_t *denyp )
2878 AttributeDescription *ad = ( AttributeDescription * )priv;
2880 slap_access_t tgrant, tdeny, grant, deny;
2882 char accessmaskbuf[ACCESSMASK_MAXLEN];
2883 char accessmaskbuf1[ACCESSMASK_MAXLEN];
2884 #endif /* LDAP_DEBUG */
2886 /* start out with nothing granted, nothing denied */
2890 /* get the aci attribute */
2891 at = attr_find( e->e_attrs, ad );
2895 /* the aci is an multi-valued attribute. The
2896 * rights are determined by OR'ing the individual
2897 * rights given by the acis.
2899 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2900 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2901 nmatch, matches, &grant, &deny,
2902 SLAP_ACI_SCOPE_ENTRY ) != 0 )
2909 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2910 accessmask2str( tgrant, accessmaskbuf, 1 ),
2911 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2914 /* If the entry level aci didn't contain anything valid for the
2915 * current operation, climb up the tree and evaluate the
2916 * acis with scope set to subtree
2918 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2919 struct berval parent_ndn;
2922 /* to solve the chicken'n'egg problem of accessing
2923 * the OpenLDAPaci attribute, the direct access
2924 * to the entry's attribute is unchecked; however,
2925 * further accesses to OpenLDAPaci values in the
2926 * ancestors occur through backend_attribute(), i.e.
2927 * with the identity of the operation, requiring
2928 * further access checking. For uniformity, this
2929 * makes further requests occur as the rootdn, if
2930 * any, i.e. searching for the OpenLDAPaci attribute
2931 * is considered an internal search. If this is not
2932 * acceptable, then the same check needs be performed
2933 * when accessing the entry's attribute. */
2934 Operation op2 = *op;
2936 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
2937 op2.o_dn = op->o_bd->be_rootdn;
2938 op2.o_ndn = op->o_bd->be_rootndn;
2942 dnParent( &e->e_nname, &parent_ndn );
2943 while ( !BER_BVISEMPTY( &parent_ndn ) ){
2945 BerVarray bvals = NULL;
2948 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
2949 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
2958 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
2959 if ( aci_mask( op, e, desc, val,
2963 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2967 /* evaluation stops as soon as either a "deny" or a
2968 * "grant" directive matches.
2970 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
2974 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2975 accessmask2str( tgrant, accessmaskbuf, 1 ),
2976 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2980 case LDAP_NO_SUCH_ATTRIBUTE:
2981 /* just go on if the aci-Attribute is not present in
2984 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
2988 case LDAP_NO_SUCH_OBJECT:
2989 /* We have reached the base object */
2990 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3002 dnParent( &parent_ndn, &parent_ndn );
3012 /* need to register this at some point */
3013 static slap_dynacl_t dynacl_aci = {
3023 #endif /* SLAP_DYNACL */
3025 #endif /* SLAPD_ACI_ENABLED */
3030 * dynamic ACL infrastructure
3032 static slap_dynacl_t *da_list = NULL;
3035 slap_dynacl_register( slap_dynacl_t *da )
3039 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3040 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3045 if ( tmp != NULL ) {
3049 if ( da->da_mask == NULL ) {
3053 da->da_private = NULL;
3054 da->da_next = da_list;
3060 static slap_dynacl_t *
3061 slap_dynacl_next( slap_dynacl_t *da )
3070 slap_dynacl_get( const char *name )
3074 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3075 if ( strcasecmp( da->da_name, name ) == 0 ) {
3082 #endif /* SLAP_DYNACL */
3089 slap_dynacl_t *known_dynacl[] = {
3090 #ifdef SLAPD_ACI_ENABLED
3092 #endif /* SLAPD_ACI_ENABLED */
3096 for ( i = 0; known_dynacl[ i ]; i++ ) {
3097 rc = slap_dynacl_register( known_dynacl[ i ] );
3102 #endif /* SLAP_DYNACL */
3113 regmatch_t *matches)
3121 bv->bv_val[0] = '\0';
3122 bv->bv_len--; /* leave space for lone $ */
3125 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3126 sp < pat->bv_val + pat->bv_len ; sp++ )
3128 /* did we previously see a $ */
3130 if ( flag == 1 && *sp == '$' ) {
3135 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3138 } else if ( *sp >= '0' && *sp <= '9' ) {
3146 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3147 if ( *sp >= '0' && *sp <= '9' ) {
3148 n = 10*n + ( *sp - '0' );
3152 if ( *sp != /*'{'*/ '}' ) {
3158 if ( n >= nmatch ) {
3164 i = matches[n].rm_so;
3165 l = matches[n].rm_eo;
3166 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3184 /* must have ended with a single $ */
3192 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3193 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3200 struct berval *pat, /* pattern to expand and match against */
3201 char *str, /* string to match against pattern */
3202 char *buf, /* buffer with $N expansion variables */
3203 int nmatch, /* size of the matches array */
3204 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3208 char newbuf[ACL_BUF_SIZE];
3212 bv.bv_len = sizeof( newbuf ) - 1;
3219 string_expand( &bv, pat, buf, nmatch, matches );
3220 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3222 char error[ACL_BUF_SIZE];
3223 regerror( rc, &re, error, sizeof( error ) );
3225 Debug( LDAP_DEBUG_TRACE,
3226 "compile( \"%s\", \"%s\") failed %s\n",
3227 pat->bv_val, str, error );
3231 rc = regexec( &re, str, 0, NULL, 0 );
3234 Debug( LDAP_DEBUG_TRACE,
3235 "=> regex_matches: string: %s\n", str, 0, 0 );
3236 Debug( LDAP_DEBUG_TRACE,
3237 "=> regex_matches: rc: %d %s\n",
3238 rc, !rc ? "matches" : "no matches", 0 );