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,
697 AccessControlState *state,
699 struct berval *opndn )
702 * if access applies to the entry itself, and the
703 * user is bound as somebody in the same namespace as
704 * the entry, OR the given dn matches the dn pattern
707 * NOTE: styles "anonymous", "users" and "self"
708 * have been moved to enum slap_style_t, whose
709 * value is set in a_dn_style; however, the string
710 * is maintaned in a_dn_pat.
716 ACL_RECORD_VALUE_STATE;
718 /* must have DN syntax */
719 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) return 1;
721 /* check if the target is an attribute. */
722 if ( val == NULL ) return 1;
724 /* a DN must be present */
725 if ( BER_BVISEMPTY( opndn ) ) {
729 /* target is attribute, check if the attribute value
732 rc = value_match( &match, desc,
733 desc->ad_type->sat_equality, 0,
734 val, opndn, &dummy );
735 /* on match error or no match, fail the ACL clause */
736 if ( rc != LDAP_SUCCESS || match != 0 )
740 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
741 if ( !BER_BVISEMPTY( opndn ) ) {
745 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
746 if ( BER_BVISEMPTY( opndn ) ) {
750 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
751 struct berval ndn, selfndn;
754 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
758 level = bdn->a_self_level;
766 selfndn = e->e_nname;
769 for ( ; level > 0; level-- ) {
770 if ( BER_BVISEMPTY( &ndn ) ) {
773 dnParent( &ndn, &ndn );
776 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
781 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
782 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
784 regmatch_t tmp_matches[2],
785 *tmp_matchesp = tmp_matches;
789 switch ( a->acl_dn_style ) {
790 case ACL_STYLE_REGEX:
791 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
792 tmp_matchesp = matches;
796 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
799 tmp_matches[0].rm_so = 0;
800 tmp_matches[0].rm_eo = e->e_nname.bv_len;
805 case ACL_STYLE_SUBTREE:
806 case ACL_STYLE_CHILDREN:
807 tmp_matches[0].rm_so = 0;
808 tmp_matches[0].rm_eo = e->e_nname.bv_len;
809 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
810 tmp_matches[1].rm_eo = e->e_nname.bv_len;
824 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
825 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
833 ber_len_t patlen, odnlen;
836 if ( e->e_dn == NULL )
839 if ( bdn->a_expand ) {
841 char buf[ACL_BUF_SIZE];
844 regmatch_t tmp_matches[2],
845 *tmp_matchesp = tmp_matches;
849 bv.bv_len = sizeof( buf ) - 1;
852 switch ( a->acl_dn_style ) {
853 case ACL_STYLE_REGEX:
854 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
855 tmp_matchesp = matches;
859 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
862 tmp_matches[0].rm_so = 0;
863 tmp_matches[0].rm_eo = e->e_nname.bv_len;
868 case ACL_STYLE_SUBTREE:
869 case ACL_STYLE_CHILDREN:
870 tmp_matches[0].rm_so = 0;
871 tmp_matches[0].rm_eo = e->e_nname.bv_len;
872 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
873 tmp_matches[1].rm_eo = e->e_nname.bv_len;
887 if ( acl_string_expand( &bv, &bdn->a_pat,
889 tmp_nmatch, tmp_matchesp ) )
894 if ( dnNormalize(0, NULL, NULL, &bv,
895 &pat, op->o_tmpmemctx )
898 /* did not expand to a valid dn */
907 odnlen = opndn->bv_len;
908 if ( odnlen < patlen ) {
909 goto dn_match_cleanup;
913 if ( bdn->a_style == ACL_STYLE_BASE ) {
914 /* base dn -- entire object DN must match */
915 if ( odnlen != patlen ) {
916 goto dn_match_cleanup;
919 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
920 ber_len_t rdnlen = 0;
922 if ( odnlen <= patlen ) {
923 goto dn_match_cleanup;
926 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
927 goto dn_match_cleanup;
930 rdnlen = dn_rdnlen( NULL, opndn );
931 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
932 goto dn_match_cleanup;
935 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
936 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
937 goto dn_match_cleanup;
940 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
941 if ( odnlen <= patlen ) {
942 goto dn_match_cleanup;
945 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
946 goto dn_match_cleanup;
949 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
950 int level = bdn->a_level;
953 if ( odnlen <= patlen ) {
954 goto dn_match_cleanup;
957 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
959 goto dn_match_cleanup;
963 for ( ; level > 0; level-- ) {
964 if ( BER_BVISEMPTY( &ndn ) ) {
965 goto dn_match_cleanup;
967 dnParent( &ndn, &ndn );
968 if ( ndn.bv_len < patlen ) {
969 goto dn_match_cleanup;
973 if ( ndn.bv_len != patlen ) {
974 goto dn_match_cleanup;
978 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
981 if ( pat.bv_val != bdn->a_pat.bv_val ) {
982 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1001 regmatch_t *matches,
1003 AccessControlState *state,
1004 slap_dn_access *bdn,
1005 struct berval *opndn )
1011 const char *attr = bdn->a_at->ad_cname.bv_val;
1013 assert( attr != NULL );
1015 if ( BER_BVISEMPTY( opndn ) ) {
1019 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1022 /* see if asker is listed in dnattr */
1023 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1025 at = attrs_find( at->a_next, bdn->a_at ) )
1027 if ( value_find_ex( bdn->a_at,
1028 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1029 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1031 &bv, op->o_tmpmemctx ) == 0 )
1040 /* have a dnattr match. if this is a self clause then
1041 * the target must also match the op dn.
1043 if ( bdn->a_self ) {
1044 /* check if the target is an attribute. */
1045 if ( val == NULL ) return 1;
1047 /* target is attribute, check if the attribute value
1050 rc = value_match( &match, bdn->a_at,
1051 bdn->a_at->ad_type->sat_equality, 0,
1053 /* on match error or no match, fail the ACL clause */
1054 if ( rc != LDAP_SUCCESS || match != 0 )
1059 /* no dnattr match, check if this is a self clause */
1060 if ( ! bdn->a_self )
1063 ACL_RECORD_VALUE_STATE;
1065 /* this is a self clause, check if the target is an
1071 /* target is attribute, check if the attribute value
1074 rc = value_match( &match, bdn->a_at,
1075 bdn->a_at->ad_type->sat_equality, 0,
1078 /* on match error or no match, fail the ACL clause */
1079 if ( rc != LDAP_SUCCESS || match != 0 )
1088 * slap_acl_mask - modifies mask based upon the given acl and the
1089 * requested access to entry e, attribute attr, value val. if val
1090 * is null, access to the whole attribute is assumed (all values).
1092 * returns 0 access NOT allowed
1096 static slap_control_t
1102 AttributeDescription *desc,
1105 regmatch_t *matches,
1107 AccessControlState *state )
1112 char accessmaskbuf[ACCESSMASK_MAXLEN];
1115 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1117 assert( a != NULL );
1118 assert( mask != NULL );
1119 assert( desc != NULL );
1121 attr = desc->ad_cname.bv_val;
1123 assert( attr != NULL );
1125 Debug( LDAP_DEBUG_ACL,
1126 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1129 Debug( LDAP_DEBUG_ACL,
1130 "=> acl_mask: to %s by \"%s\", (%s) \n",
1131 val ? "value" : "all values",
1132 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1133 accessmask2str( *mask, accessmaskbuf, 1 ) );
1136 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1137 && state->as_vd_acl == a )
1139 b = state->as_vd_access;
1140 i = state->as_vd_access_count;
1147 for ( ; b != NULL; b = b->a_next, i++ ) {
1148 slap_mask_t oldmask, modmask;
1150 ACL_INVALIDATE( modmask );
1152 /* AND <who> clauses */
1153 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1154 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1155 b->a_dn_pat.bv_val, 0, 0);
1157 * if access applies to the entry itself, and the
1158 * user is bound as somebody in the same namespace as
1159 * the entry, OR the given dn matches the dn pattern
1162 * NOTE: styles "anonymous", "users" and "self"
1163 * have been moved to enum slap_style_t, whose
1164 * value is set in a_dn_style; however, the string
1165 * is maintaned in a_dn_pat.
1168 if ( acl_mask_dn( op, e, desc, val, a, b, i, nmatch, matches,
1169 count, state, &b->a_dn, &op->o_ndn ) )
1175 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1178 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1179 b->a_realdn_pat.bv_val, 0, 0);
1181 * if access applies to the entry itself, and the
1182 * user is bound as somebody in the same namespace as
1183 * the entry, OR the given dn matches the dn pattern
1186 * NOTE: styles "anonymous", "users" and "self"
1187 * have been moved to enum slap_style_t, whose
1188 * value is set in a_dn_style; however, the string
1189 * is maintaned in a_dn_pat.
1192 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1194 ndn = op->o_conn->c_ndn;
1199 if ( acl_mask_dn( op, e, desc, val, a, b, i, nmatch, matches,
1200 count, state, &b->a_realdn, &ndn ) )
1206 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1207 if ( ! op->o_conn->c_listener ) {
1210 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1211 b->a_sockurl_pat.bv_val, 0, 0 );
1213 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1214 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1215 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1216 e->e_ndn, nmatch, matches ) )
1221 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1223 char buf[ACL_BUF_SIZE];
1225 bv.bv_len = sizeof( buf ) - 1;
1227 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1228 e->e_ndn, nmatch, matches ) )
1233 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1239 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1247 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1248 if ( !op->o_conn->c_peer_domain.bv_val ) {
1251 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1252 b->a_domain_pat.bv_val, 0, 0 );
1253 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1254 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1255 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1256 e->e_ndn, nmatch, matches ) )
1261 char buf[ACL_BUF_SIZE];
1263 struct berval cmp = op->o_conn->c_peer_domain;
1264 struct berval pat = b->a_domain_pat;
1266 if ( b->a_domain_expand ) {
1269 bv.bv_len = sizeof(buf) - 1;
1272 if ( acl_string_expand(&bv, &b->a_domain_pat,
1273 e->e_ndn, nmatch, matches) )
1280 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1281 int offset = cmp.bv_len - pat.bv_len;
1286 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1290 /* trim the domain */
1291 cmp.bv_val = &cmp.bv_val[ offset ];
1292 cmp.bv_len -= offset;
1295 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1302 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1303 if ( !op->o_conn->c_peer_name.bv_val ) {
1306 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1307 b->a_peername_pat.bv_val, 0, 0 );
1308 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1309 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1310 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1311 e->e_ndn, nmatch, matches ) )
1317 /* try exact match */
1318 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1319 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1323 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1325 char buf[ACL_BUF_SIZE];
1327 bv.bv_len = sizeof( buf ) - 1;
1329 if ( acl_string_expand( &bv, &b->a_peername_pat,
1330 e->e_ndn, nmatch, matches ) )
1335 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1339 /* extract IP and try exact match */
1340 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1342 char buf[] = "255.255.255.255";
1345 int port_number = -1;
1347 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1348 acl_bv_ip_eq.bv_val,
1349 acl_bv_ip_eq.bv_len ) != 0 )
1352 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1353 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1355 port = strrchr( ip.bv_val, ':' );
1357 ip.bv_len = port - ip.bv_val;
1359 if ( lutil_atoi( &port_number, port ) != 0 )
1363 /* the port check can be anticipated here */
1364 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1367 /* address longer than expected? */
1368 if ( ip.bv_len >= sizeof(buf) )
1371 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1372 buf[ ip.bv_len ] = '\0';
1374 addr = inet_addr( buf );
1376 /* unable to convert? */
1377 if ( addr == (unsigned long)(-1) )
1380 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1383 #ifdef LDAP_PF_LOCAL
1384 /* extract path and try exact match */
1385 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1388 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1389 acl_bv_path_eq.bv_val,
1390 acl_bv_path_eq.bv_len ) != 0 )
1393 path.bv_val = op->o_conn->c_peer_name.bv_val
1394 + acl_bv_path_eq.bv_len;
1395 path.bv_len = op->o_conn->c_peer_name.bv_len
1396 - acl_bv_path_eq.bv_len;
1398 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1401 #endif /* LDAP_PF_LOCAL */
1403 /* exact match (very unlikely...) */
1404 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1411 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1412 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1415 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1416 b->a_sockname_pat.bv_val, 0, 0 );
1417 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1418 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1419 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1420 e->e_ndn, nmatch, matches ) )
1425 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1427 char buf[ACL_BUF_SIZE];
1429 bv.bv_len = sizeof( buf ) - 1;
1431 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1432 e->e_ndn, nmatch, matches ) )
1437 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1442 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1449 if ( b->a_dn_at != NULL ) {
1450 if ( acl_mask_dnattr( op, e, val, a, b, i,
1451 matches, count, state,
1452 &b->a_dn, &op->o_ndn ) )
1458 if ( b->a_realdn_at != NULL ) {
1461 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1463 ndn = op->o_conn->c_ndn;
1468 if ( acl_mask_dnattr( op, e, val, a, b, i,
1469 matches, count, state,
1470 &b->a_realdn, &ndn ) )
1476 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1478 struct berval ndn = BER_BVNULL;
1481 if ( op->o_ndn.bv_len == 0 ) {
1485 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1486 b->a_group_pat.bv_val, 0, 0 );
1488 /* b->a_group is an unexpanded entry name, expanded it should be an
1489 * entry with objectclass group* and we test to see if odn is one of
1490 * the values in the attribute group
1492 /* see if asker is listed in dnattr */
1493 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1494 char buf[ACL_BUF_SIZE];
1496 regmatch_t tmp_matches[2],
1497 *tmp_matchesp = tmp_matches;
1499 bv.bv_len = sizeof(buf) - 1;
1504 switch ( a->acl_dn_style ) {
1505 case ACL_STYLE_REGEX:
1506 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1507 tmp_matchesp = matches;
1508 tmp_nmatch = nmatch;
1512 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1513 case ACL_STYLE_BASE:
1514 tmp_matches[0].rm_so = 0;
1515 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1520 case ACL_STYLE_SUBTREE:
1521 case ACL_STYLE_CHILDREN:
1522 tmp_matches[0].rm_so = 0;
1523 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1524 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1525 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1539 if ( acl_string_expand( &bv, &b->a_group_pat,
1541 tmp_nmatch, tmp_matchesp ) )
1546 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1547 op->o_tmpmemctx ) != LDAP_SUCCESS )
1549 /* did not expand to a valid dn */
1556 bv = b->a_group_pat;
1559 rc = backend_group( op, e, &bv, &op->o_ndn,
1560 b->a_group_oc, b->a_group_at );
1563 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1571 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1573 char buf[ACL_BUF_SIZE];
1575 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1576 b->a_set_pat.bv_val, 0, 0 );
1578 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1580 regmatch_t tmp_matches[2],
1581 *tmp_matchesp = tmp_matches;
1584 bv.bv_len = sizeof( buf ) - 1;
1589 switch ( a->acl_dn_style ) {
1590 case ACL_STYLE_REGEX:
1591 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1592 tmp_matchesp = matches;
1593 tmp_nmatch = nmatch;
1597 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1598 case ACL_STYLE_BASE:
1599 tmp_matches[0].rm_so = 0;
1600 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1605 case ACL_STYLE_SUBTREE:
1606 case ACL_STYLE_CHILDREN:
1607 tmp_matches[0].rm_so = 0;
1608 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1609 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1610 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1624 if ( acl_string_expand( &bv, &b->a_set_pat,
1626 tmp_nmatch, tmp_matchesp ) )
1635 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1640 if ( b->a_authz.sai_ssf ) {
1641 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1642 b->a_authz.sai_ssf, op->o_ssf, 0 );
1643 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1648 if ( b->a_authz.sai_transport_ssf ) {
1649 Debug( LDAP_DEBUG_ACL,
1650 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1651 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1652 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1657 if ( b->a_authz.sai_tls_ssf ) {
1658 Debug( LDAP_DEBUG_ACL,
1659 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1660 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1661 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1666 if ( b->a_authz.sai_sasl_ssf ) {
1667 Debug( LDAP_DEBUG_ACL,
1668 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1669 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1670 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1676 if ( b->a_dynacl ) {
1678 slap_access_t tgrant, tdeny;
1680 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1683 /* this case works different from the others above.
1684 * since dynamic ACL's themselves give permissions, we need
1685 * to first check b->a_access_mask, the ACL's access level.
1687 /* first check if the right being requested
1688 * is allowed by the ACL clause.
1690 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1694 /* start out with nothing granted, nothing denied */
1695 ACL_INVALIDATE(tgrant);
1696 ACL_INVALIDATE(tdeny);
1698 for ( da = b->a_dynacl; da; da = da->da_next ) {
1699 slap_access_t grant,
1702 ACL_INVALIDATE(grant);
1703 ACL_INVALIDATE(deny);
1705 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1706 da->da_name, 0, 0 );
1708 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1714 /* remove anything that the ACL clause does not allow */
1715 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1716 tdeny &= ACL_PRIV_MASK;
1718 /* see if we have anything to contribute */
1719 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1723 /* this could be improved by changing slap_acl_mask so that it can deal with
1724 * by clauses that return grant/deny pairs. Right now, it does either
1725 * additive or subtractive rights, but not both at the same time. So,
1726 * we need to combine the grant/deny pair into a single rights mask in
1727 * a smart way: if either grant or deny is "empty", then we use the
1728 * opposite as is, otherwise we remove any denied rights from the grant
1729 * rights mask and construct an additive mask.
1731 if (ACL_IS_INVALID(tdeny)) {
1732 modmask = tgrant | ACL_PRIV_ADDITIVE;
1734 } else if (ACL_IS_INVALID(tgrant)) {
1735 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1738 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1742 #endif /* SLAP_DYNACL */
1744 modmask = b->a_access_mask;
1747 Debug( LDAP_DEBUG_ACL,
1748 "<= acl_mask: [%d] applying %s (%s)\n",
1749 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1750 b->a_type == ACL_CONTINUE
1752 : b->a_type == ACL_BREAK
1758 if( ACL_IS_ADDITIVE(modmask) ) {
1760 ACL_PRIV_SET( *mask, modmask );
1763 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1765 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1766 /* substract privs */
1767 ACL_PRIV_CLR( *mask, modmask );
1770 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1779 Debug( LDAP_DEBUG_ACL,
1780 "<= acl_mask: [%d] mask: %s\n",
1781 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1783 if( b->a_type == ACL_CONTINUE ) {
1786 } else if ( b->a_type == ACL_BREAK ) {
1794 /* implicit "by * none" clause */
1797 Debug( LDAP_DEBUG_ACL,
1798 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1799 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1804 * acl_check_modlist - check access control on the given entry to see if
1805 * it allows the given modifications by the user associated with op.
1806 * returns 1 if mods allowed ok
1807 * 0 mods not allowed
1814 Modifications *mlist )
1817 AccessControlState state = ACL_STATE_INIT;
1820 int ret = 1; /* default is access allowed */
1824 be = LDAP_STAILQ_FIRST(&backendDB);
1828 assert( be != NULL );
1830 /* short circuit root database access */
1831 if ( be_isroot( op ) ) {
1832 Debug( LDAP_DEBUG_ACL,
1833 "<= acl_access_allowed: granted to database root\n",
1838 /* use backend default access if no backend acls */
1839 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1840 Debug( LDAP_DEBUG_ACL,
1841 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1842 access2str( ACL_WRITE ),
1843 op->o_bd->be_dfltaccess >= ACL_WRITE
1844 ? "granted" : "denied",
1846 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1850 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1852 * Internal mods are ignored by ACL_WRITE checking
1854 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1855 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1856 " modify access granted\n",
1857 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1862 * no-user-modification operational attributes are ignored
1863 * by ACL_WRITE checking as any found here are not provided
1866 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1867 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1869 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1870 " modify access granted\n",
1871 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1875 switch ( mlist->sml_op ) {
1876 case LDAP_MOD_REPLACE:
1877 case LDAP_MOD_INCREMENT:
1879 * We must check both permission to delete the whole
1880 * attribute and permission to add the specific attributes.
1881 * This prevents abuse from selfwriters.
1883 if ( ! access_allowed( op, e,
1884 mlist->sml_desc, NULL,
1885 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1892 if ( mlist->sml_values == NULL ) break;
1894 /* fall thru to check value to add */
1897 assert( mlist->sml_values != NULL );
1899 for ( bv = mlist->sml_nvalues
1900 ? mlist->sml_nvalues : mlist->sml_values;
1901 bv->bv_val != NULL; bv++ )
1903 if ( ! access_allowed( op, e,
1904 mlist->sml_desc, bv,
1905 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1914 case LDAP_MOD_DELETE:
1915 if ( mlist->sml_values == NULL ) {
1916 if ( ! access_allowed( op, e,
1917 mlist->sml_desc, NULL,
1918 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1926 for ( bv = mlist->sml_nvalues
1927 ? mlist->sml_nvalues : mlist->sml_values;
1928 bv->bv_val != NULL; bv++ )
1930 if ( ! access_allowed( op, e,
1931 mlist->sml_desc, bv,
1932 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1941 case SLAP_MOD_SOFTADD:
1942 /* allow adding attribute via modrdn thru */
1954 if (be_null) op->o_bd = NULL;
1960 struct berval *list,
1973 while ( len >= 0 && --ix >= 0 ) {
1974 while ( --len >= 0 && *p++ != sep )
1977 while ( len >= 0 && *p == ' ' ) {
1990 while ( --len >= 0 && *p != sep ) {
1994 while ( bv->bv_len > 0 && *--p == ' ' ) {
2001 typedef struct acl_set_gather_t {
2007 acl_set_cb_gather( Operation *op, SlapReply *rs )
2009 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2011 if ( rs->sr_type == REP_SEARCH ) {
2012 BerValue bvals[ 2 ];
2013 BerVarray bvalsp = NULL;
2016 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2017 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2019 if ( desc == slap_schema.si_ad_entryDN ) {
2021 bvals[ 0 ] = rs->sr_entry->e_nname;
2022 BER_BVZERO( &bvals[ 1 ] );
2027 a = attr_find( rs->sr_entry->e_attrs, desc );
2031 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2034 bvalsp = a->a_nvals;
2040 p->bvals = slap_set_join( p->cookie, p->bvals,
2041 ( '|' | SLAP_SET_RREF ), bvalsp );
2045 assert( rs->sr_type == REP_RESULT );
2052 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2054 AclSetCookie *cp = (AclSetCookie *)cookie;
2056 LDAPURLDesc *ludp = NULL;
2057 Operation op2 = { 0 };
2058 SlapReply rs = {REP_RESULT};
2059 AttributeName anlist[ 2 ], *anlistp = NULL;
2061 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2062 acl_set_gather_t p = { 0 };
2063 const char *text = NULL;
2064 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2066 /* this routine needs to return the bervals instead of
2067 * plain strings, since syntax is not known. It should
2068 * also return the syntax or some "comparison cookie".
2070 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2071 return acl_set_gather2( cookie, name, desc );
2074 rc = ldap_url_parse( name->bv_val, &ludp );
2075 if ( rc != LDAP_URL_SUCCESS ) {
2076 rc = LDAP_PROTOCOL_ERROR;
2080 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2082 /* host part must be empty */
2083 /* extensions parts must be empty */
2084 rc = LDAP_PROTOCOL_ERROR;
2088 /* Grab the searchbase and see if an appropriate database can be found */
2089 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2090 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2091 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2092 BER_BVZERO( &op2.o_req_dn );
2093 if ( rc != LDAP_SUCCESS ) {
2097 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2098 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2099 rc = LDAP_NO_SUCH_OBJECT;
2103 /* Grab the filter */
2104 if ( ludp->lud_filter ) {
2105 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2106 cp->asc_op->o_tmpmemctx );
2109 op2.ors_filterstr = defaultFilter_bv;
2112 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2113 if ( op2.ors_filter == NULL ) {
2114 rc = LDAP_PROTOCOL_ERROR;
2118 /* Grab the scope */
2119 op2.ors_scope = ludp->lud_scope;
2121 /* Grap the attributes */
2122 if ( ludp->lud_attrs ) {
2123 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2126 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2127 cp->asc_op->o_tmpmemctx );
2129 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2130 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2131 anlistp[ nattrs ].an_desc = NULL;
2132 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2133 &anlistp[ nattrs ].an_desc, &text );
2134 if ( rc != LDAP_SUCCESS ) {
2143 anlistp[ nattrs ].an_name = desc->ad_cname;
2144 anlistp[ nattrs ].an_desc = desc;
2146 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2150 op2.o_hdr = cp->asc_op->o_hdr;
2151 op2.o_tag = LDAP_REQ_SEARCH;
2152 op2.o_ndn = op2.o_bd->be_rootndn;
2153 op2.o_callback = &cb;
2154 slap_op_time( &op2.o_time, &op2.o_tincr );
2155 op2.o_do_not_cache = 1;
2156 op2.o_is_auth_check = 0;
2157 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2158 op2.ors_slimit = SLAP_NO_LIMIT;
2159 op2.ors_tlimit = SLAP_NO_LIMIT;
2160 op2.ors_attrs = anlistp;
2161 op2.ors_attrsonly = 0;
2162 op2.o_private = cp->asc_op->o_private;
2166 rc = op2.o_bd->be_search( &op2, &rs );
2172 if ( op2.ors_filter ) {
2173 filter_free_x( cp->asc_op, op2.ors_filter );
2175 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2176 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2178 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2179 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2182 ldap_free_urldesc( ludp );
2184 if ( anlistp && anlistp != anlist ) {
2185 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2192 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2194 AclSetCookie *cp = (AclSetCookie *)cookie;
2195 BerVarray bvals = NULL;
2199 /* this routine needs to return the bervals instead of
2200 * plain strings, since syntax is not known. It should
2201 * also return the syntax or some "comparison cookie".
2203 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2204 if ( rc == LDAP_SUCCESS ) {
2205 if ( desc == slap_schema.si_ad_entryDN ) {
2206 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2207 cp->asc_op->o_tmpmemctx );
2209 BER_BVZERO( &bvals[ 1 ] );
2213 backend_attribute( cp->asc_op,
2214 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2217 if ( !BER_BVISNULL( &ndn ) ) {
2218 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2227 struct berval *subj,
2230 struct berval *default_set_attribute )
2232 struct berval set = BER_BVNULL;
2234 AclSetCookie cookie;
2236 if ( default_set_attribute == NULL ) {
2237 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2240 struct berval subjdn, ndn = BER_BVNULL;
2241 struct berval setat;
2242 BerVarray bvals = NULL;
2244 AttributeDescription *desc = NULL;
2246 /* format of string is "entry/setAttrName" */
2247 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2251 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2252 setat = *default_set_attribute;
2256 * NOTE: dnNormalize honors the ber_len field
2257 * as the length of the dn to be normalized
2259 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2260 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2262 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2263 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2267 BER_BVZERO( &bvals[0] );
2268 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2270 bvals[0].bv_val = bvals[i-1].bv_val;
2271 BER_BVZERO( &bvals[i-1] );
2273 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2274 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2279 if ( !BER_BVISNULL( &set ) ) {
2282 rc = ( slap_set_filter(
2284 (SetCookie *)&cookie, &set,
2285 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2286 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2295 * dynamic ACL infrastructure
2297 static slap_dynacl_t *da_list = NULL;
2300 slap_dynacl_register( slap_dynacl_t *da )
2304 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2305 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2310 if ( tmp != NULL ) {
2314 if ( da->da_mask == NULL ) {
2318 da->da_private = NULL;
2319 da->da_next = da_list;
2325 static slap_dynacl_t *
2326 slap_dynacl_next( slap_dynacl_t *da )
2335 slap_dynacl_get( const char *name )
2339 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2340 if ( strcasecmp( da->da_name, name ) == 0 ) {
2347 #endif /* SLAP_DYNACL */
2350 * statically built-in dynamic ACL initialization
2352 static int (*acl_init_func[])( void ) = {
2354 /* TODO: remove when ACI will only be dynamic */
2355 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2357 #endif /* SLAPD_ACI_ENABLED */
2358 #endif /* SLAP_DYNACL */
2368 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2369 rc = (*(acl_init_func[ i ]))();
2384 regmatch_t *matches)
2392 bv->bv_val[0] = '\0';
2393 bv->bv_len--; /* leave space for lone $ */
2396 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2397 sp < pat->bv_val + pat->bv_len ; sp++ )
2399 /* did we previously see a $ */
2401 if ( flag == 1 && *sp == '$' ) {
2406 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2409 } else if ( *sp >= '0' && *sp <= '9' ) {
2417 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2418 if ( *sp >= '0' && *sp <= '9' ) {
2419 n = 10*n + ( *sp - '0' );
2423 if ( *sp != /*'{'*/ '}' ) {
2429 if ( n >= nmatch ) {
2435 i = matches[n].rm_so;
2436 l = matches[n].rm_eo;
2437 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2455 /* must have ended with a single $ */
2463 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2464 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2471 struct berval *pat, /* pattern to expand and match against */
2472 char *str, /* string to match against pattern */
2473 char *buf, /* buffer with $N expansion variables */
2474 int nmatch, /* size of the matches array */
2475 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2479 char newbuf[ACL_BUF_SIZE];
2483 bv.bv_len = sizeof( newbuf ) - 1;
2490 acl_string_expand( &bv, pat, buf, nmatch, matches );
2491 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2493 char error[ACL_BUF_SIZE];
2494 regerror( rc, &re, error, sizeof( error ) );
2496 Debug( LDAP_DEBUG_TRACE,
2497 "compile( \"%s\", \"%s\") failed %s\n",
2498 pat->bv_val, str, error );
2502 rc = regexec( &re, str, 0, NULL, 0 );
2505 Debug( LDAP_DEBUG_TRACE,
2506 "=> regex_matches: string: %s\n", str, 0, 0 );
2507 Debug( LDAP_DEBUG_TRACE,
2508 "=> regex_matches: rc: %d %s\n",
2509 rc, !rc ? "matches" : "no matches", 0 );