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-2006 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>
40 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
42 static const struct berval acl_bv_ip_eq = BER_BVC( "IP=" );
44 static const struct berval acl_bv_path_eq = BER_BVC("PATH=");
45 #endif /* LDAP_PF_LOCAL */
47 static AccessControl * slap_acl_get(
48 AccessControl *ac, int *count,
49 Operation *op, Entry *e,
50 AttributeDescription *desc,
52 int nmatch, regmatch_t *matches,
53 AccessControlState *state );
55 static slap_control_t slap_acl_mask(
56 AccessControl *ac, slap_mask_t *mask,
57 Operation *op, Entry *e,
58 AttributeDescription *desc,
63 AccessControlState *state );
65 static int regex_matches(
66 struct berval *pat, char *str, char *buf,
67 int nmatch, regmatch_t *matches);
69 typedef struct AclSetCookie {
71 #define asc_op asc_cookie.set_op
75 SLAP_SET_GATHER acl_set_gather;
76 SLAP_SET_GATHER acl_set_gather2;
79 * access_allowed - check whether op->o_ndn is allowed the requested access
80 * to entry e, attribute attr, value val. if val is null, access to
81 * the whole attribute is assumed (all values).
83 * This routine loops through all access controls and calls
84 * slap_acl_mask() on each applicable access control.
85 * The loop exits when a definitive answer is reached or
86 * or no more controls remain.
93 * - can be legally called with op == NULL
94 * - can be legally called with op->o_bd == NULL
98 slap_access_always_allowed(
101 AttributeDescription *desc,
103 slap_access_t access,
104 AccessControlState *state,
107 assert( maskp != NULL );
110 ACL_LVL_ASSIGN_MANAGE( *maskp );
119 AttributeDescription *desc,
121 slap_access_t access,
122 AccessControlState *state,
127 AccessControl *a = NULL;
130 char accessmaskbuf[ACCESSMASK_MAXLEN];
133 slap_control_t control;
134 slap_access_t access_level;
136 regmatch_t matches[MAXREMATCHES];
137 int st_same_attr = 0;
139 assert( op != NULL );
141 assert( desc != NULL );
142 assert( maskp != NULL );
144 access_level = ACL_LEVEL( access );
145 attr = desc->ad_cname.bv_val;
147 assert( attr != NULL );
151 /* grant database root access */
152 if ( be_isroot( op ) ) {
153 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
154 mask = ACL_LVL_MANAGE;
159 * no-user-modification operational attributes are ignored
160 * by ACL_WRITE checking as any found here are not provided
163 * NOTE: but they are not ignored for ACL_MANAGE, because
164 * if we get here it means a non-root user is trying to
165 * manage data, so we need to check its privileges.
167 if ( access_level == ACL_WRITE
168 && is_at_no_user_mod( desc->ad_type )
169 && desc != slap_schema.si_ad_entry
170 && desc != slap_schema.si_ad_children )
172 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
173 " %s access granted\n",
178 /* use backend default access if no backend acls */
179 if ( op->o_bd->be_acl == NULL ) {
182 Debug( LDAP_DEBUG_ACL,
183 "=> slap_access_allowed: backend default %s "
184 "access %s to \"%s\"\n",
185 access2str( access ),
186 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
187 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
188 ret = op->o_bd->be_dfltaccess >= access_level;
190 mask = ACL_PRIV_LEVEL;
191 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
192 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
201 if ( st_same_attr ) {
202 assert( state->as_vd_acl != NULL );
204 a = state->as_vd_acl;
205 count = state->as_vd_acl_count;
206 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
207 mask = state->as_vd_acl_mask;
208 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
213 if ( state ) state->as_vi_acl = NULL;
215 ACL_PRIV_ASSIGN( mask, *maskp );
217 memset( matches, '\0', sizeof( matches ) );
220 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
221 MAXREMATCHES, matches, state ) ) != NULL )
225 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
226 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
227 (int)matches[i].rm_so, (int)matches[i].rm_eo );
228 if ( matches[i].rm_so <= matches[0].rm_eo ) {
230 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
231 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
234 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
238 if ( state->as_vi_acl == a &&
239 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
241 Debug( LDAP_DEBUG_ACL,
242 "=> slap_access_allowed: result from state (%s)\n",
244 ret = state->as_result;
247 Debug( LDAP_DEBUG_ACL,
248 "=> slap_access_allowed: no res from state (%s)\n",
254 control = slap_acl_mask( a, &mask, op,
255 e, desc, val, MAXREMATCHES, matches, count, state );
257 if ( control != ACL_BREAK ) {
261 memset( matches, '\0', sizeof( matches ) );
264 if ( ACL_IS_INVALID( mask ) ) {
265 Debug( LDAP_DEBUG_ACL,
266 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
268 ACL_PRIV_ASSIGN( mask, *maskp );
270 } else if ( control == ACL_BREAK ) {
271 Debug( LDAP_DEBUG_ACL,
272 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
277 ret = ACL_GRANT( mask, access );
279 Debug( LDAP_DEBUG_ACL,
280 "=> slap_access_allowed: %s access %s by %s\n",
281 access2str( access ), ret ? "granted" : "denied",
282 accessmask2str( mask, accessmaskbuf, 1 ) );
285 ACL_PRIV_ASSIGN( *maskp, mask );
293 AttributeDescription *desc,
295 slap_access_t access,
296 AccessControlState *state,
303 * NOTE: control gets here if FIXME
304 * if an appropriate backend cannot be selected for the operation,
305 * we assume that the frontend should handle this
306 * FIXME: should select_backend() take care of this,
307 * and return frontendDB instead of NULL? maybe for some value
312 if ( op->o_bd == NULL ) {
313 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
314 if ( op->o_bd == NULL )
315 op->o_bd = frontendDB;
317 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
327 AttributeDescription *desc,
329 slap_access_t access,
330 AccessControlState *state,
334 AccessControl *a = NULL;
338 char accessmaskbuf[ACCESSMASK_MAXLEN];
341 slap_access_t access_level;
343 int st_same_attr = 0;
344 static AccessControlState state_init = ACL_STATE_INIT;
347 assert( desc != NULL );
349 access_level = ACL_LEVEL( access );
351 assert( access_level > ACL_NONE );
354 if ( maskp ) ACL_INVALIDATE( *maskp );
356 attr = desc->ad_cname.bv_val;
358 assert( attr != NULL );
361 if ( op->o_is_auth_check &&
362 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
366 } else if ( get_manageDIT( op ) && access_level == ACL_WRITE &&
367 desc == slap_schema.si_ad_entry )
374 if ( state->as_vd_ad == desc ) {
375 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
378 return state->as_result;
380 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
381 val != NULL && state->as_vd_acl == NULL )
383 return state->as_result;
390 state->as_vd_ad = desc;
393 Debug( LDAP_DEBUG_ACL,
394 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
395 access2str( access ), e->e_dn, attr );
402 if ( op->o_bd == NULL ) {
403 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
408 * FIXME: experimental; use first backend rules
409 * iff there is no global_acl (ITS#3100) */
410 if ( frontendDB->be_acl != NULL ) {
411 op->o_bd = frontendDB;
413 #endif /* LDAP_DEVEL */
415 assert( op->o_bd != NULL );
417 /* this is enforced in backend_add() */
418 if ( op->o_bd->bd_info->bi_access_allowed ) {
419 /* delegate to backend */
420 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
421 desc, val, access, state, &mask );
424 /* use default (but pass through frontend
425 * for global ACL overlays) */
426 ret = frontendDB->bd_info->bi_access_allowed( op, e,
427 desc, val, access, state, &mask );
431 if ( ACL_IS_INVALID( mask ) ) {
432 Debug( LDAP_DEBUG_ACL,
433 "=> access_allowed: \"%s\" (%s) invalid!\n",
438 Debug( LDAP_DEBUG_ACL,
439 "=> access_allowed: no more rules\n", 0, 0, 0 );
445 Debug( LDAP_DEBUG_ACL,
446 "=> access_allowed: %s access %s by %s\n",
447 access2str( access ), ret ? "granted" : "denied",
448 accessmask2str( mask, accessmaskbuf, 1 ) );
451 if ( state != NULL ) {
452 /* If not value-dependent, save ACL in case of more attrs */
453 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
454 state->as_vi_acl = a;
455 state->as_result = ret;
457 state->as_recorded |= ACL_STATE_RECORDED;
459 if ( be_null ) op->o_bd = NULL;
460 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
466 * slap_acl_get - return the acl applicable to entry e, attribute
467 * attr. the acl returned is suitable for use in subsequent calls to
468 * acl_access_allowed().
471 static AccessControl *
477 AttributeDescription *desc,
481 AccessControlState *state )
488 assert( count != NULL );
489 assert( desc != NULL );
491 attr = desc->ad_cname.bv_val;
493 assert( attr != NULL );
496 if( op->o_bd == NULL ) {
497 a = frontendDB->be_acl;
499 a = op->o_bd->be_acl;
510 dnlen = e->e_nname.bv_len;
512 for ( ; a != NULL; a = a->acl_next ) {
515 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
516 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
517 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
518 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
519 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
523 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
524 *count, a->acl_dn_pat.bv_val, 0 );
525 patlen = a->acl_dn_pat.bv_len;
526 if ( dnlen < patlen )
529 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
530 /* base dn -- entire object DN must match */
531 if ( dnlen != patlen )
534 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
535 ber_len_t rdnlen = 0;
538 if ( dnlen <= patlen )
542 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
547 rdnlen = dn_rdnlen( NULL, &e->e_nname );
548 if ( rdnlen != dnlen - patlen - sep )
551 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
552 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
555 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
556 if ( dnlen <= patlen )
558 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
562 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
566 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
570 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
571 matches[0].rm_so = matches[0].rm_eo = -1;
575 /* Is this ACL only for a specific value? */
576 if ( a->acl_attrval.bv_len ) {
581 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
582 state->as_recorded |= ACL_STATE_RECORDED_VD;
583 state->as_vd_acl = a;
584 state->as_vd_acl_count = *count;
585 state->as_vd_access = a->acl_access;
586 state->as_vd_access_count = 1;
587 ACL_INVALIDATE( state->as_vd_acl_mask );
590 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
591 Debug( LDAP_DEBUG_ACL,
592 "acl_get: valpat %s\n",
593 a->acl_attrval.bv_val, 0, 0 );
594 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
602 Debug( LDAP_DEBUG_ACL,
604 a->acl_attrval.bv_val, 0, 0 );
606 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
607 if (value_match( &match, desc,
608 a->acl_attrval_mr, 0,
609 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
616 patlen = a->acl_attrval.bv_len;
617 vdnlen = val->bv_len;
619 if ( vdnlen < patlen )
622 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
623 if ( vdnlen > patlen )
626 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
627 ber_len_t rdnlen = 0;
629 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
632 rdnlen = dn_rdnlen( NULL, val );
633 if ( rdnlen != vdnlen - patlen - 1 )
636 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
637 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
640 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
641 if ( vdnlen <= patlen )
644 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
648 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
654 if ( a->acl_filter != NULL ) {
655 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
656 if ( rc != LDAP_COMPARE_TRUE ) {
661 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
666 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
671 * Record value-dependent access control state
673 #define ACL_RECORD_VALUE_STATE do { \
674 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
675 state->as_recorded |= ACL_STATE_RECORDED_VD; \
676 state->as_vd_acl = a; \
677 AC_MEMCPY( state->as_vd_acl_matches, matches, \
678 sizeof( state->as_vd_acl_matches )) ; \
679 state->as_vd_acl_count = count; \
680 state->as_vd_access = b; \
681 state->as_vd_access_count = i; \
689 AttributeDescription *desc,
695 struct berval *opndn )
698 * if access applies to the entry itself, and the
699 * user is bound as somebody in the same namespace as
700 * the entry, OR the given dn matches the dn pattern
703 * NOTE: styles "anonymous", "users" and "self"
704 * have been moved to enum slap_style_t, whose
705 * value is set in a_dn_style; however, the string
706 * is maintaned in a_dn_pat.
709 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
710 if ( !BER_BVISEMPTY( opndn ) ) {
714 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
715 if ( BER_BVISEMPTY( opndn ) ) {
719 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
720 struct berval ndn, selfndn;
723 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
727 level = bdn->a_self_level;
735 selfndn = e->e_nname;
738 for ( ; level > 0; level-- ) {
739 if ( BER_BVISEMPTY( &ndn ) ) {
742 dnParent( &ndn, &ndn );
745 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
750 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
751 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
753 regmatch_t tmp_matches[2],
754 *tmp_matchesp = tmp_matches;
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 ( !regex_matches( &bdn->a_pat, opndn->bv_val,
794 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
802 ber_len_t patlen, odnlen;
805 if ( e->e_dn == NULL )
808 if ( bdn->a_expand ) {
810 char buf[ACL_BUF_SIZE];
813 regmatch_t tmp_matches[2],
814 *tmp_matchesp = tmp_matches;
818 bv.bv_len = sizeof( buf ) - 1;
821 switch ( a->acl_dn_style ) {
822 case ACL_STYLE_REGEX:
823 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
824 tmp_matchesp = matches;
828 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
831 tmp_matches[0].rm_so = 0;
832 tmp_matches[0].rm_eo = e->e_nname.bv_len;
837 case ACL_STYLE_SUBTREE:
838 case ACL_STYLE_CHILDREN:
839 tmp_matches[0].rm_so = 0;
840 tmp_matches[0].rm_eo = e->e_nname.bv_len;
841 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
842 tmp_matches[1].rm_eo = e->e_nname.bv_len;
856 if ( acl_string_expand( &bv, &bdn->a_pat,
858 tmp_nmatch, tmp_matchesp ) )
863 if ( dnNormalize(0, NULL, NULL, &bv,
864 &pat, op->o_tmpmemctx )
867 /* did not expand to a valid dn */
876 odnlen = opndn->bv_len;
877 if ( odnlen < patlen ) {
878 goto dn_match_cleanup;
882 if ( bdn->a_style == ACL_STYLE_BASE ) {
883 /* base dn -- entire object DN must match */
884 if ( odnlen != patlen ) {
885 goto dn_match_cleanup;
888 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
889 ber_len_t rdnlen = 0;
891 if ( odnlen <= patlen ) {
892 goto dn_match_cleanup;
895 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
896 goto dn_match_cleanup;
899 rdnlen = dn_rdnlen( NULL, opndn );
900 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
901 goto dn_match_cleanup;
904 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
905 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
906 goto dn_match_cleanup;
909 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
910 if ( odnlen <= patlen ) {
911 goto dn_match_cleanup;
914 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
915 goto dn_match_cleanup;
918 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
919 int level = bdn->a_level;
922 if ( odnlen <= patlen ) {
923 goto dn_match_cleanup;
926 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
928 goto dn_match_cleanup;
932 for ( ; level > 0; level-- ) {
933 if ( BER_BVISEMPTY( &ndn ) ) {
934 goto dn_match_cleanup;
936 dnParent( &ndn, &ndn );
937 if ( ndn.bv_len < patlen ) {
938 goto dn_match_cleanup;
942 if ( ndn.bv_len != patlen ) {
943 goto dn_match_cleanup;
947 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
950 if ( pat.bv_val != bdn->a_pat.bv_val ) {
951 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
972 AccessControlState *state,
974 struct berval *opndn )
980 const char *attr = bdn->a_at->ad_cname.bv_val;
982 assert( attr != NULL );
984 if ( BER_BVISEMPTY( opndn ) ) {
988 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
991 /* see if asker is listed in dnattr */
992 for ( at = attrs_find( e->e_attrs, bdn->a_at );
994 at = attrs_find( at->a_next, bdn->a_at ) )
996 if ( value_find_ex( bdn->a_at,
997 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
998 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1000 &bv, op->o_tmpmemctx ) == 0 )
1009 /* have a dnattr match. if this is a self clause then
1010 * the target must also match the op dn.
1012 if ( bdn->a_self ) {
1013 /* check if the target is an attribute. */
1014 if ( val == NULL ) return 1;
1016 /* target is attribute, check if the attribute value
1019 rc = value_match( &match, bdn->a_at,
1020 bdn->a_at->ad_type->sat_equality, 0,
1022 /* on match error or no match, fail the ACL clause */
1023 if ( rc != LDAP_SUCCESS || match != 0 )
1028 /* no dnattr match, check if this is a self clause */
1029 if ( ! bdn->a_self )
1032 ACL_RECORD_VALUE_STATE;
1034 /* this is a self clause, check if the target is an
1040 /* target is attribute, check if the attribute value
1043 rc = value_match( &match, bdn->a_at,
1044 bdn->a_at->ad_type->sat_equality, 0,
1047 /* on match error or no match, fail the ACL clause */
1048 if ( rc != LDAP_SUCCESS || match != 0 )
1057 * slap_acl_mask - modifies mask based upon the given acl and the
1058 * requested access to entry e, attribute attr, value val. if val
1059 * is null, access to the whole attribute is assumed (all values).
1061 * returns 0 access NOT allowed
1065 static slap_control_t
1071 AttributeDescription *desc,
1074 regmatch_t *matches,
1076 AccessControlState *state )
1081 char accessmaskbuf[ACCESSMASK_MAXLEN];
1084 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1086 assert( a != NULL );
1087 assert( mask != NULL );
1088 assert( desc != NULL );
1090 attr = desc->ad_cname.bv_val;
1092 assert( attr != NULL );
1094 Debug( LDAP_DEBUG_ACL,
1095 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1098 Debug( LDAP_DEBUG_ACL,
1099 "=> acl_mask: to %s by \"%s\", (%s) \n",
1100 val ? "value" : "all values",
1101 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1102 accessmask2str( *mask, accessmaskbuf, 1 ) );
1105 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1106 && state->as_vd_acl == a )
1108 b = state->as_vd_access;
1109 i = state->as_vd_access_count;
1116 for ( ; b != NULL; b = b->a_next, i++ ) {
1117 slap_mask_t oldmask, modmask;
1119 ACL_INVALIDATE( modmask );
1121 /* AND <who> clauses */
1122 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1123 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1124 b->a_dn_pat.bv_val, 0, 0);
1126 * if access applies to the entry itself, and the
1127 * user is bound as somebody in the same namespace as
1128 * the entry, OR the given dn matches the dn pattern
1131 * NOTE: styles "anonymous", "users" and "self"
1132 * have been moved to enum slap_style_t, whose
1133 * value is set in a_dn_style; however, the string
1134 * is maintaned in a_dn_pat.
1137 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1138 &b->a_dn, &op->o_ndn ) )
1144 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1147 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1148 b->a_realdn_pat.bv_val, 0, 0);
1150 * if access applies to the entry itself, and the
1151 * user is bound as somebody in the same namespace as
1152 * the entry, OR the given dn matches the dn pattern
1155 * NOTE: styles "anonymous", "users" and "self"
1156 * have been moved to enum slap_style_t, whose
1157 * value is set in a_dn_style; however, the string
1158 * is maintaned in a_dn_pat.
1161 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1163 ndn = op->o_conn->c_ndn;
1168 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1169 &b->a_realdn, &ndn ) )
1175 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1176 if ( ! op->o_conn->c_listener ) {
1179 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1180 b->a_sockurl_pat.bv_val, 0, 0 );
1182 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1183 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1184 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1185 e->e_ndn, nmatch, matches ) )
1190 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1192 char buf[ACL_BUF_SIZE];
1194 bv.bv_len = sizeof( buf ) - 1;
1196 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1197 e->e_ndn, nmatch, matches ) )
1202 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1208 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1216 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1217 if ( !op->o_conn->c_peer_domain.bv_val ) {
1220 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1221 b->a_domain_pat.bv_val, 0, 0 );
1222 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1223 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1224 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1225 e->e_ndn, nmatch, matches ) )
1230 char buf[ACL_BUF_SIZE];
1232 struct berval cmp = op->o_conn->c_peer_domain;
1233 struct berval pat = b->a_domain_pat;
1235 if ( b->a_domain_expand ) {
1238 bv.bv_len = sizeof(buf) - 1;
1241 if ( acl_string_expand(&bv, &b->a_domain_pat,
1242 e->e_ndn, nmatch, matches) )
1249 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1250 int offset = cmp.bv_len - pat.bv_len;
1255 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1259 /* trim the domain */
1260 cmp.bv_val = &cmp.bv_val[ offset ];
1261 cmp.bv_len -= offset;
1264 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1271 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1272 if ( !op->o_conn->c_peer_name.bv_val ) {
1275 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1276 b->a_peername_pat.bv_val, 0, 0 );
1277 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1278 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1279 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1280 e->e_ndn, nmatch, matches ) )
1286 /* try exact match */
1287 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1288 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1292 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1294 char buf[ACL_BUF_SIZE];
1296 bv.bv_len = sizeof( buf ) - 1;
1298 if ( acl_string_expand( &bv, &b->a_peername_pat,
1299 e->e_ndn, nmatch, matches ) )
1304 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1308 /* extract IP and try exact match */
1309 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1311 char buf[] = "255.255.255.255";
1314 int port_number = -1;
1316 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1317 acl_bv_ip_eq.bv_val,
1318 acl_bv_ip_eq.bv_len ) != 0 )
1321 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1322 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1324 port = strrchr( ip.bv_val, ':' );
1326 ip.bv_len = port - ip.bv_val;
1328 if ( lutil_atoi( &port_number, port ) != 0 )
1332 /* the port check can be anticipated here */
1333 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1336 /* address longer than expected? */
1337 if ( ip.bv_len >= sizeof(buf) )
1340 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1341 buf[ ip.bv_len ] = '\0';
1343 addr = inet_addr( buf );
1345 /* unable to convert? */
1346 if ( addr == (unsigned long)(-1) )
1349 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1352 #ifdef LDAP_PF_LOCAL
1353 /* extract path and try exact match */
1354 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1357 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1358 acl_bv_path_eq.bv_val,
1359 acl_bv_path_eq.bv_len ) != 0 )
1362 path.bv_val = op->o_conn->c_peer_name.bv_val
1363 + acl_bv_path_eq.bv_len;
1364 path.bv_len = op->o_conn->c_peer_name.bv_len
1365 - acl_bv_path_eq.bv_len;
1367 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1370 #endif /* LDAP_PF_LOCAL */
1372 /* exact match (very unlikely...) */
1373 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1380 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1381 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1384 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1385 b->a_sockname_pat.bv_val, 0, 0 );
1386 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1387 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1388 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1389 e->e_ndn, nmatch, matches ) )
1394 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1396 char buf[ACL_BUF_SIZE];
1398 bv.bv_len = sizeof( buf ) - 1;
1400 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1401 e->e_ndn, nmatch, matches ) )
1406 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1411 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1418 if ( b->a_dn_at != NULL ) {
1419 if ( acl_mask_dnattr( op, e, val, a, b, i,
1420 matches, count, state,
1421 &b->a_dn, &op->o_ndn ) )
1427 if ( b->a_realdn_at != NULL ) {
1430 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1432 ndn = op->o_conn->c_ndn;
1437 if ( acl_mask_dnattr( op, e, val, a, b, i,
1438 matches, count, state,
1439 &b->a_realdn, &ndn ) )
1445 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1447 struct berval ndn = BER_BVNULL;
1450 if ( op->o_ndn.bv_len == 0 ) {
1454 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1455 b->a_group_pat.bv_val, 0, 0 );
1457 /* b->a_group is an unexpanded entry name, expanded it should be an
1458 * entry with objectclass group* and we test to see if odn is one of
1459 * the values in the attribute group
1461 /* see if asker is listed in dnattr */
1462 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1463 char buf[ACL_BUF_SIZE];
1465 regmatch_t tmp_matches[2],
1466 *tmp_matchesp = tmp_matches;
1468 bv.bv_len = sizeof(buf) - 1;
1473 switch ( a->acl_dn_style ) {
1474 case ACL_STYLE_REGEX:
1475 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1476 tmp_matchesp = matches;
1477 tmp_nmatch = nmatch;
1481 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1482 case ACL_STYLE_BASE:
1483 tmp_matches[0].rm_so = 0;
1484 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1489 case ACL_STYLE_SUBTREE:
1490 case ACL_STYLE_CHILDREN:
1491 tmp_matches[0].rm_so = 0;
1492 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1493 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1494 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1508 if ( acl_string_expand( &bv, &b->a_group_pat,
1510 tmp_nmatch, tmp_matchesp ) )
1515 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1516 op->o_tmpmemctx ) != LDAP_SUCCESS )
1518 /* did not expand to a valid dn */
1525 bv = b->a_group_pat;
1528 rc = backend_group( op, e, &bv, &op->o_ndn,
1529 b->a_group_oc, b->a_group_at );
1532 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1540 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1542 char buf[ACL_BUF_SIZE];
1544 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1545 b->a_set_pat.bv_val, 0, 0 );
1547 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1549 regmatch_t tmp_matches[2],
1550 *tmp_matchesp = tmp_matches;
1553 bv.bv_len = sizeof( buf ) - 1;
1558 switch ( a->acl_dn_style ) {
1559 case ACL_STYLE_REGEX:
1560 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1561 tmp_matchesp = matches;
1562 tmp_nmatch = nmatch;
1566 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1567 case ACL_STYLE_BASE:
1568 tmp_matches[0].rm_so = 0;
1569 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1574 case ACL_STYLE_SUBTREE:
1575 case ACL_STYLE_CHILDREN:
1576 tmp_matches[0].rm_so = 0;
1577 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1578 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1579 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1593 if ( acl_string_expand( &bv, &b->a_set_pat,
1595 tmp_nmatch, tmp_matchesp ) )
1604 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1609 if ( b->a_authz.sai_ssf ) {
1610 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1611 b->a_authz.sai_ssf, op->o_ssf, 0 );
1612 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1617 if ( b->a_authz.sai_transport_ssf ) {
1618 Debug( LDAP_DEBUG_ACL,
1619 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1620 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1621 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1626 if ( b->a_authz.sai_tls_ssf ) {
1627 Debug( LDAP_DEBUG_ACL,
1628 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1629 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1630 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1635 if ( b->a_authz.sai_sasl_ssf ) {
1636 Debug( LDAP_DEBUG_ACL,
1637 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1638 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1639 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1644 /* check for the "self" modifier in the <access> field */
1645 if ( b->a_dn.a_self ) {
1649 ACL_RECORD_VALUE_STATE;
1651 /* must have DN syntax */
1652 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) continue;
1654 /* check if the target is an attribute. */
1655 if ( val == NULL ) continue;
1657 /* a DN must be present */
1658 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1662 /* target is attribute, check if the attribute value
1665 rc = value_match( &match, desc,
1666 desc->ad_type->sat_equality, 0,
1667 val, &op->o_ndn, &dummy );
1668 /* on match error or no match, fail the ACL clause */
1669 if ( rc != LDAP_SUCCESS || match != 0 )
1674 if ( b->a_dynacl ) {
1676 slap_access_t tgrant, tdeny;
1678 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1681 /* this case works different from the others above.
1682 * since dynamic ACL's themselves give permissions, we need
1683 * to first check b->a_access_mask, the ACL's access level.
1685 /* first check if the right being requested
1686 * is allowed by the ACL clause.
1688 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1692 /* start out with nothing granted, nothing denied */
1693 ACL_INVALIDATE(tgrant);
1694 ACL_INVALIDATE(tdeny);
1696 for ( da = b->a_dynacl; da; da = da->da_next ) {
1697 slap_access_t grant,
1700 ACL_INVALIDATE(grant);
1701 ACL_INVALIDATE(deny);
1703 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1704 da->da_name, 0, 0 );
1706 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1712 /* remove anything that the ACL clause does not allow */
1713 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1714 tdeny &= ACL_PRIV_MASK;
1716 /* see if we have anything to contribute */
1717 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1721 /* this could be improved by changing slap_acl_mask so that it can deal with
1722 * by clauses that return grant/deny pairs. Right now, it does either
1723 * additive or subtractive rights, but not both at the same time. So,
1724 * we need to combine the grant/deny pair into a single rights mask in
1725 * a smart way: if either grant or deny is "empty", then we use the
1726 * opposite as is, otherwise we remove any denied rights from the grant
1727 * rights mask and construct an additive mask.
1729 if (ACL_IS_INVALID(tdeny)) {
1730 modmask = tgrant | ACL_PRIV_ADDITIVE;
1732 } else if (ACL_IS_INVALID(tgrant)) {
1733 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1736 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1740 #endif /* SLAP_DYNACL */
1742 modmask = b->a_access_mask;
1745 Debug( LDAP_DEBUG_ACL,
1746 "<= acl_mask: [%d] applying %s (%s)\n",
1747 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1748 b->a_type == ACL_CONTINUE
1750 : b->a_type == ACL_BREAK
1756 if( ACL_IS_ADDITIVE(modmask) ) {
1758 ACL_PRIV_SET( *mask, modmask );
1761 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1763 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1764 /* substract privs */
1765 ACL_PRIV_CLR( *mask, modmask );
1768 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1777 Debug( LDAP_DEBUG_ACL,
1778 "<= acl_mask: [%d] mask: %s\n",
1779 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1781 if( b->a_type == ACL_CONTINUE ) {
1784 } else if ( b->a_type == ACL_BREAK ) {
1792 /* implicit "by * none" clause */
1795 Debug( LDAP_DEBUG_ACL,
1796 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1797 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1802 * acl_check_modlist - check access control on the given entry to see if
1803 * it allows the given modifications by the user associated with op.
1804 * returns 1 if mods allowed ok
1805 * 0 mods not allowed
1812 Modifications *mlist )
1815 AccessControlState state = ACL_STATE_INIT;
1818 int ret = 1; /* default is access allowed */
1822 be = LDAP_STAILQ_FIRST(&backendDB);
1826 assert( be != NULL );
1828 /* short circuit root database access */
1829 if ( be_isroot( op ) ) {
1830 Debug( LDAP_DEBUG_ACL,
1831 "<= acl_access_allowed: granted to database root\n",
1836 /* use backend default access if no backend acls */
1837 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1838 Debug( LDAP_DEBUG_ACL,
1839 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1840 access2str( ACL_WRITE ),
1841 op->o_bd->be_dfltaccess >= ACL_WRITE
1842 ? "granted" : "denied",
1844 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1848 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1850 * Internal mods are ignored by ACL_WRITE checking
1852 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1853 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1854 " modify access granted\n",
1855 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1860 * no-user-modification operational attributes are ignored
1861 * by ACL_WRITE checking as any found here are not provided
1864 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1865 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1867 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1868 " modify access granted\n",
1869 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1873 switch ( mlist->sml_op ) {
1874 case LDAP_MOD_REPLACE:
1875 case LDAP_MOD_INCREMENT:
1877 * We must check both permission to delete the whole
1878 * attribute and permission to add the specific attributes.
1879 * This prevents abuse from selfwriters.
1881 if ( ! access_allowed( op, e,
1882 mlist->sml_desc, NULL,
1883 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1890 if ( mlist->sml_values == NULL ) break;
1892 /* fall thru to check value to add */
1895 assert( mlist->sml_values != NULL );
1897 for ( bv = mlist->sml_nvalues
1898 ? mlist->sml_nvalues : mlist->sml_values;
1899 bv->bv_val != NULL; bv++ )
1901 if ( ! access_allowed( op, e,
1902 mlist->sml_desc, bv,
1903 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1912 case LDAP_MOD_DELETE:
1913 if ( mlist->sml_values == NULL ) {
1914 if ( ! access_allowed( op, e,
1915 mlist->sml_desc, NULL,
1916 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1924 for ( bv = mlist->sml_nvalues
1925 ? mlist->sml_nvalues : mlist->sml_values;
1926 bv->bv_val != NULL; bv++ )
1928 if ( ! access_allowed( op, e,
1929 mlist->sml_desc, bv,
1930 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1939 case SLAP_MOD_SOFTADD:
1940 /* allow adding attribute via modrdn thru */
1952 if (be_null) op->o_bd = NULL;
1958 struct berval *list,
1971 while ( len >= 0 && --ix >= 0 ) {
1972 while ( --len >= 0 && *p++ != sep )
1975 while ( len >= 0 && *p == ' ' ) {
1988 while ( --len >= 0 && *p != sep ) {
1992 while ( bv->bv_len > 0 && *--p == ' ' ) {
1999 typedef struct acl_set_gather_t {
2005 acl_set_cb_gather( Operation *op, SlapReply *rs )
2007 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2009 if ( rs->sr_type == REP_SEARCH ) {
2010 BerValue bvals[ 2 ];
2011 BerVarray bvalsp = NULL;
2014 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2015 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2017 if ( desc == slap_schema.si_ad_entryDN ) {
2019 bvals[ 0 ] = rs->sr_entry->e_nname;
2020 BER_BVZERO( &bvals[ 1 ] );
2025 a = attr_find( rs->sr_entry->e_attrs, desc );
2029 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2032 bvalsp = a->a_nvals;
2038 p->bvals = slap_set_join( p->cookie, p->bvals,
2039 ( '|' | SLAP_SET_RREF ), bvalsp );
2043 assert( rs->sr_type == REP_RESULT );
2050 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2052 AclSetCookie *cp = (AclSetCookie *)cookie;
2054 LDAPURLDesc *ludp = NULL;
2055 Operation op2 = { 0 };
2056 SlapReply rs = {REP_RESULT};
2057 AttributeName anlist[ 2 ], *anlistp = NULL;
2059 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2060 acl_set_gather_t p = { 0 };
2061 const char *text = NULL;
2062 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2064 /* this routine needs to return the bervals instead of
2065 * plain strings, since syntax is not known. It should
2066 * also return the syntax or some "comparison cookie".
2068 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2069 return acl_set_gather2( cookie, name, desc );
2072 rc = ldap_url_parse( name->bv_val, &ludp );
2073 if ( rc != LDAP_URL_SUCCESS ) {
2074 rc = LDAP_PROTOCOL_ERROR;
2078 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2080 /* host part must be empty */
2081 /* extensions parts must be empty */
2082 rc = LDAP_PROTOCOL_ERROR;
2086 /* Grab the searchbase and see if an appropriate database can be found */
2087 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2088 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2089 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2090 BER_BVZERO( &op2.o_req_dn );
2091 if ( rc != LDAP_SUCCESS ) {
2095 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2096 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2097 rc = LDAP_NO_SUCH_OBJECT;
2101 /* Grab the filter */
2102 if ( ludp->lud_filter ) {
2103 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2104 cp->asc_op->o_tmpmemctx );
2107 op2.ors_filterstr = defaultFilter_bv;
2110 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2111 if ( op2.ors_filter == NULL ) {
2112 rc = LDAP_PROTOCOL_ERROR;
2116 /* Grab the scope */
2117 op2.ors_scope = ludp->lud_scope;
2119 /* Grap the attributes */
2120 if ( ludp->lud_attrs ) {
2121 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2124 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2125 cp->asc_op->o_tmpmemctx );
2127 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2128 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2129 anlistp[ nattrs ].an_desc = NULL;
2130 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2131 &anlistp[ nattrs ].an_desc, &text );
2132 if ( rc != LDAP_SUCCESS ) {
2141 anlistp[ nattrs ].an_name = desc->ad_cname;
2142 anlistp[ nattrs ].an_desc = desc;
2144 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2148 op2.o_hdr = cp->asc_op->o_hdr;
2149 op2.o_tag = LDAP_REQ_SEARCH;
2150 op2.o_ndn = op2.o_bd->be_rootndn;
2151 op2.o_callback = &cb;
2152 slap_op_time( &op2.o_time, &op2.o_tincr );
2153 op2.o_do_not_cache = 1;
2154 op2.o_is_auth_check = 0;
2155 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2156 op2.ors_slimit = SLAP_NO_LIMIT;
2157 op2.ors_tlimit = SLAP_NO_LIMIT;
2158 op2.ors_attrs = anlistp;
2159 op2.ors_attrsonly = 0;
2160 op2.o_private = cp->asc_op->o_private;
2164 rc = op2.o_bd->be_search( &op2, &rs );
2170 if ( op2.ors_filter ) {
2171 filter_free_x( cp->asc_op, op2.ors_filter );
2173 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2174 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2176 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2177 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2180 ldap_free_urldesc( ludp );
2182 if ( anlistp && anlistp != anlist ) {
2183 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2190 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2192 AclSetCookie *cp = (AclSetCookie *)cookie;
2193 BerVarray bvals = NULL;
2197 /* this routine needs to return the bervals instead of
2198 * plain strings, since syntax is not known. It should
2199 * also return the syntax or some "comparison cookie".
2201 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2202 if ( rc == LDAP_SUCCESS ) {
2203 if ( desc == slap_schema.si_ad_entryDN ) {
2204 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2205 cp->asc_op->o_tmpmemctx );
2207 BER_BVZERO( &bvals[ 1 ] );
2211 backend_attribute( cp->asc_op,
2212 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2215 if ( !BER_BVISNULL( &ndn ) ) {
2216 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2225 struct berval *subj,
2228 struct berval *default_set_attribute )
2230 struct berval set = BER_BVNULL;
2232 AclSetCookie cookie;
2234 if ( default_set_attribute == NULL ) {
2235 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2238 struct berval subjdn, ndn = BER_BVNULL;
2239 struct berval setat;
2240 BerVarray bvals = NULL;
2242 AttributeDescription *desc = NULL;
2244 /* format of string is "entry/setAttrName" */
2245 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2249 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2250 setat = *default_set_attribute;
2254 * NOTE: dnNormalize honors the ber_len field
2255 * as the length of the dn to be normalized
2257 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2258 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2260 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2261 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2265 BER_BVZERO( &bvals[0] );
2266 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2268 bvals[0].bv_val = bvals[i-1].bv_val;
2269 BER_BVZERO( &bvals[i-1] );
2271 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2272 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2277 if ( !BER_BVISNULL( &set ) ) {
2280 rc = ( slap_set_filter(
2282 (SetCookie *)&cookie, &set,
2283 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2284 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2293 * dynamic ACL infrastructure
2295 static slap_dynacl_t *da_list = NULL;
2298 slap_dynacl_register( slap_dynacl_t *da )
2302 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2303 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2308 if ( tmp != NULL ) {
2312 if ( da->da_mask == NULL ) {
2316 da->da_private = NULL;
2317 da->da_next = da_list;
2323 static slap_dynacl_t *
2324 slap_dynacl_next( slap_dynacl_t *da )
2333 slap_dynacl_get( const char *name )
2337 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2338 if ( strcasecmp( da->da_name, name ) == 0 ) {
2345 #endif /* SLAP_DYNACL */
2348 * statically built-in dynamic ACL initialization
2350 static int (*acl_init_func[])( void ) = {
2352 /* TODO: remove when ACI will only be dynamic */
2353 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2355 #endif /* SLAPD_ACI_ENABLED */
2356 #endif /* SLAP_DYNACL */
2366 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2367 rc = (*(acl_init_func[ i ]))();
2382 regmatch_t *matches)
2390 bv->bv_val[0] = '\0';
2391 bv->bv_len--; /* leave space for lone $ */
2394 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2395 sp < pat->bv_val + pat->bv_len ; sp++ )
2397 /* did we previously see a $ */
2399 if ( flag == 1 && *sp == '$' ) {
2404 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2407 } else if ( *sp >= '0' && *sp <= '9' ) {
2415 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2416 if ( *sp >= '0' && *sp <= '9' ) {
2417 n = 10*n + ( *sp - '0' );
2421 if ( *sp != /*'{'*/ '}' ) {
2427 if ( n >= nmatch ) {
2433 i = matches[n].rm_so;
2434 l = matches[n].rm_eo;
2435 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2453 /* must have ended with a single $ */
2461 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2462 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2469 struct berval *pat, /* pattern to expand and match against */
2470 char *str, /* string to match against pattern */
2471 char *buf, /* buffer with $N expansion variables */
2472 int nmatch, /* size of the matches array */
2473 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2477 char newbuf[ACL_BUF_SIZE];
2481 bv.bv_len = sizeof( newbuf ) - 1;
2488 acl_string_expand( &bv, pat, buf, nmatch, matches );
2489 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2491 char error[ACL_BUF_SIZE];
2492 regerror( rc, &re, error, sizeof( error ) );
2494 Debug( LDAP_DEBUG_TRACE,
2495 "compile( \"%s\", \"%s\") failed %s\n",
2496 pat->bv_val, str, error );
2500 rc = regexec( &re, str, 0, NULL, 0 );
2503 Debug( LDAP_DEBUG_TRACE,
2504 "=> regex_matches: string: %s\n", str, 0, 0 );
2505 Debug( LDAP_DEBUG_TRACE,
2506 "=> regex_matches: rc: %d %s\n",
2507 rc, !rc ? "matches" : "no matches", 0 );