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 );
674 AttributeDescription *desc,
680 struct berval *opndn )
683 * if access applies to the entry itself, and the
684 * user is bound as somebody in the same namespace as
685 * the entry, OR the given dn matches the dn pattern
688 * NOTE: styles "anonymous", "users" and "self"
689 * have been moved to enum slap_style_t, whose
690 * value is set in a_dn_style; however, the string
691 * is maintaned in a_dn_pat.
693 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
694 if ( !BER_BVISEMPTY( opndn ) ) {
698 } else if ( b->a_style == ACL_STYLE_USERS ) {
699 if ( BER_BVISEMPTY( opndn ) ) {
707 /* must have DN syntax */
708 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) return 1;
710 /* check if the target is an attribute. */
711 if ( val == NULL ) return 1;
713 /* target is attribute, check if the attribute value
716 rc = value_match( &match, desc,
717 desc->ad_type->sat_equality, 0,
718 val, opndn, &dummy );
719 /* on match error or no match, fail the ACL clause */
720 if ( rc != LDAP_SUCCESS || match != 0 )
724 } else if ( b->a_style == ACL_STYLE_SELF ) {
725 struct berval ndn, selfndn;
728 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
732 level = b->a_self_level;
740 selfndn = e->e_nname;
743 for ( ; level > 0; level-- ) {
744 if ( BER_BVISEMPTY( &ndn ) ) {
747 dnParent( &ndn, &ndn );
750 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
755 } else if ( b->a_style == ACL_STYLE_REGEX ) {
756 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
758 regmatch_t tmp_matches[2],
759 *tmp_matchesp = tmp_matches;
763 switch ( a->acl_dn_style ) {
764 case ACL_STYLE_REGEX:
765 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
766 tmp_matchesp = matches;
770 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
773 tmp_matches[0].rm_so = 0;
774 tmp_matches[0].rm_eo = e->e_nname.bv_len;
779 case ACL_STYLE_SUBTREE:
780 case ACL_STYLE_CHILDREN:
781 tmp_matches[0].rm_so = 0;
782 tmp_matches[0].rm_eo = e->e_nname.bv_len;
783 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
784 tmp_matches[1].rm_eo = e->e_nname.bv_len;
798 if ( !regex_matches( &b->a_pat, opndn->bv_val,
799 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
807 ber_len_t patlen, odnlen;
810 if ( e->e_dn == NULL )
815 char buf[ACL_BUF_SIZE];
818 regmatch_t tmp_matches[2],
819 *tmp_matchesp = tmp_matches;
823 bv.bv_len = sizeof( buf ) - 1;
826 switch ( a->acl_dn_style ) {
827 case ACL_STYLE_REGEX:
828 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
829 tmp_matchesp = matches;
833 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
836 tmp_matches[0].rm_so = 0;
837 tmp_matches[0].rm_eo = e->e_nname.bv_len;
842 case ACL_STYLE_SUBTREE:
843 case ACL_STYLE_CHILDREN:
844 tmp_matches[0].rm_so = 0;
845 tmp_matches[0].rm_eo = e->e_nname.bv_len;
846 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
847 tmp_matches[1].rm_eo = e->e_nname.bv_len;
861 if ( acl_string_expand( &bv, &b->a_pat,
863 tmp_nmatch, tmp_matchesp ) )
868 if ( dnNormalize(0, NULL, NULL, &bv,
869 &pat, op->o_tmpmemctx )
872 /* did not expand to a valid dn */
881 odnlen = opndn->bv_len;
882 if ( odnlen < patlen ) {
883 goto dn_match_cleanup;
887 if ( b->a_style == ACL_STYLE_BASE ) {
888 /* base dn -- entire object DN must match */
889 if ( odnlen != patlen ) {
890 goto dn_match_cleanup;
893 } else if ( b->a_style == ACL_STYLE_ONE ) {
894 ber_len_t rdnlen = 0;
896 if ( odnlen <= patlen ) {
897 goto dn_match_cleanup;
900 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
901 goto dn_match_cleanup;
904 rdnlen = dn_rdnlen( NULL, opndn );
905 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
906 goto dn_match_cleanup;
909 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
910 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
911 goto dn_match_cleanup;
914 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
915 if ( odnlen <= patlen ) {
916 goto dn_match_cleanup;
919 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
920 goto dn_match_cleanup;
923 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
924 int level = b->a_level;
927 if ( odnlen <= patlen ) {
928 goto dn_match_cleanup;
931 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
933 goto dn_match_cleanup;
937 for ( ; level > 0; level-- ) {
938 if ( BER_BVISEMPTY( &ndn ) ) {
939 goto dn_match_cleanup;
941 dnParent( &ndn, &ndn );
942 if ( ndn.bv_len < patlen ) {
943 goto dn_match_cleanup;
947 if ( ndn.bv_len != patlen ) {
948 goto dn_match_cleanup;
952 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
955 if ( pat.bv_val != b->a_pat.bv_val ) {
956 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
968 * Record value-dependent access control state
970 #define ACL_RECORD_VALUE_STATE do { \
971 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
972 state->as_recorded |= ACL_STATE_RECORDED_VD; \
973 state->as_vd_acl = a; \
974 AC_MEMCPY( state->as_vd_acl_matches, matches, \
975 sizeof( state->as_vd_acl_matches )) ; \
976 state->as_vd_acl_count = count; \
977 state->as_vd_access = b; \
978 state->as_vd_access_count = i; \
992 AccessControlState *state,
994 struct berval *opndn )
1000 const char *attr = bdn->a_at->ad_cname.bv_val;
1002 assert( attr != NULL );
1004 if ( BER_BVISEMPTY( opndn ) ) {
1008 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1011 /* see if asker is listed in dnattr */
1012 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1014 at = attrs_find( at->a_next, bdn->a_at ) )
1016 if ( value_find_ex( bdn->a_at,
1017 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1018 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1020 &bv, op->o_tmpmemctx ) == 0 )
1029 /* have a dnattr match. if this is a self clause then
1030 * the target must also match the op dn.
1032 if ( bdn->a_self ) {
1033 /* check if the target is an attribute. */
1034 if ( val == NULL ) return 1;
1036 /* target is attribute, check if the attribute value
1039 rc = value_match( &match, bdn->a_at,
1040 bdn->a_at->ad_type->sat_equality, 0,
1042 /* on match error or no match, fail the ACL clause */
1043 if ( rc != LDAP_SUCCESS || match != 0 )
1048 /* no dnattr match, check if this is a self clause */
1049 if ( ! bdn->a_self )
1052 ACL_RECORD_VALUE_STATE;
1054 /* this is a self clause, check if the target is an
1060 /* target is attribute, check if the attribute value
1063 rc = value_match( &match, bdn->a_at,
1064 bdn->a_at->ad_type->sat_equality, 0,
1067 /* on match error or no match, fail the ACL clause */
1068 if ( rc != LDAP_SUCCESS || match != 0 )
1077 * slap_acl_mask - modifies mask based upon the given acl and the
1078 * requested access to entry e, attribute attr, value val. if val
1079 * is null, access to the whole attribute is assumed (all values).
1081 * returns 0 access NOT allowed
1085 static slap_control_t
1091 AttributeDescription *desc,
1094 regmatch_t *matches,
1096 AccessControlState *state )
1101 char accessmaskbuf[ACCESSMASK_MAXLEN];
1104 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1106 assert( a != NULL );
1107 assert( mask != NULL );
1108 assert( desc != NULL );
1110 attr = desc->ad_cname.bv_val;
1112 assert( attr != NULL );
1114 Debug( LDAP_DEBUG_ACL,
1115 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1118 Debug( LDAP_DEBUG_ACL,
1119 "=> acl_mask: to %s by \"%s\", (%s) \n",
1120 val ? "value" : "all values",
1121 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1122 accessmask2str( *mask, accessmaskbuf, 1 ) );
1125 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1126 && state->as_vd_acl == a )
1128 b = state->as_vd_access;
1129 i = state->as_vd_access_count;
1136 for ( ; b != NULL; b = b->a_next, i++ ) {
1137 slap_mask_t oldmask, modmask;
1139 ACL_INVALIDATE( modmask );
1141 /* AND <who> clauses */
1142 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1143 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1144 b->a_dn_pat.bv_val, 0, 0);
1146 * if access applies to the entry itself, and the
1147 * user is bound as somebody in the same namespace as
1148 * the entry, OR the given dn matches the dn pattern
1151 * NOTE: styles "anonymous", "users" and "self"
1152 * have been moved to enum slap_style_t, whose
1153 * value is set in a_dn_style; however, the string
1154 * is maintaned in a_dn_pat.
1157 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1158 &b->a_dn, &op->o_ndn ) )
1164 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1167 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1168 b->a_realdn_pat.bv_val, 0, 0);
1170 * if access applies to the entry itself, and the
1171 * user is bound as somebody in the same namespace as
1172 * the entry, OR the given dn matches the dn pattern
1175 * NOTE: styles "anonymous", "users" and "self"
1176 * have been moved to enum slap_style_t, whose
1177 * value is set in a_dn_style; however, the string
1178 * is maintaned in a_dn_pat.
1181 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1183 ndn = op->o_conn->c_ndn;
1188 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1189 &b->a_realdn, &ndn ) )
1195 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1196 if ( ! op->o_conn->c_listener ) {
1199 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1200 b->a_sockurl_pat.bv_val, 0, 0 );
1202 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1203 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1204 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1205 e->e_ndn, nmatch, matches ) )
1210 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1212 char buf[ACL_BUF_SIZE];
1214 bv.bv_len = sizeof( buf ) - 1;
1216 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1217 e->e_ndn, nmatch, matches ) )
1222 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1228 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1236 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1237 if ( !op->o_conn->c_peer_domain.bv_val ) {
1240 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1241 b->a_domain_pat.bv_val, 0, 0 );
1242 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1243 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1244 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1245 e->e_ndn, nmatch, matches ) )
1250 char buf[ACL_BUF_SIZE];
1252 struct berval cmp = op->o_conn->c_peer_domain;
1253 struct berval pat = b->a_domain_pat;
1255 if ( b->a_domain_expand ) {
1258 bv.bv_len = sizeof(buf) - 1;
1261 if ( acl_string_expand(&bv, &b->a_domain_pat,
1262 e->e_ndn, nmatch, matches) )
1269 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1270 int offset = cmp.bv_len - pat.bv_len;
1275 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1279 /* trim the domain */
1280 cmp.bv_val = &cmp.bv_val[ offset ];
1281 cmp.bv_len -= offset;
1284 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1291 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1292 if ( !op->o_conn->c_peer_name.bv_val ) {
1295 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1296 b->a_peername_pat.bv_val, 0, 0 );
1297 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1298 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1299 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1300 e->e_ndn, nmatch, matches ) )
1306 /* try exact match */
1307 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1308 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1312 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1314 char buf[ACL_BUF_SIZE];
1316 bv.bv_len = sizeof( buf ) - 1;
1318 if ( acl_string_expand( &bv, &b->a_peername_pat,
1319 e->e_ndn, nmatch, matches ) )
1324 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1328 /* extract IP and try exact match */
1329 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1331 char buf[] = "255.255.255.255";
1334 int port_number = -1;
1336 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1337 acl_bv_ip_eq.bv_val,
1338 acl_bv_ip_eq.bv_len ) != 0 )
1341 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1342 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1344 port = strrchr( ip.bv_val, ':' );
1346 ip.bv_len = port - ip.bv_val;
1348 if ( lutil_atoi( &port_number, port ) != 0 )
1352 /* the port check can be anticipated here */
1353 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1356 /* address longer than expected? */
1357 if ( ip.bv_len >= sizeof(buf) )
1360 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1361 buf[ ip.bv_len ] = '\0';
1363 addr = inet_addr( buf );
1365 /* unable to convert? */
1366 if ( addr == (unsigned long)(-1) )
1369 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1372 #ifdef LDAP_PF_LOCAL
1373 /* extract path and try exact match */
1374 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1377 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1378 acl_bv_path_eq.bv_val,
1379 acl_bv_path_eq.bv_len ) != 0 )
1382 path.bv_val = op->o_conn->c_peer_name.bv_val
1383 + acl_bv_path_eq.bv_len;
1384 path.bv_len = op->o_conn->c_peer_name.bv_len
1385 - acl_bv_path_eq.bv_len;
1387 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1390 #endif /* LDAP_PF_LOCAL */
1392 /* exact match (very unlikely...) */
1393 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1400 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1401 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1404 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1405 b->a_sockname_pat.bv_val, 0, 0 );
1406 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1407 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1408 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1409 e->e_ndn, nmatch, matches ) )
1414 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1416 char buf[ACL_BUF_SIZE];
1418 bv.bv_len = sizeof( buf ) - 1;
1420 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1421 e->e_ndn, nmatch, matches ) )
1426 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1431 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1438 if ( b->a_dn_at != NULL ) {
1439 if ( acl_mask_dnattr( op, e, val, a, b, i,
1440 matches, count, state,
1441 &b->a_dn, &op->o_ndn ) )
1447 if ( b->a_realdn_at != NULL ) {
1450 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1452 ndn = op->o_conn->c_ndn;
1457 if ( acl_mask_dnattr( op, e, val, a, b, i,
1458 matches, count, state,
1459 &b->a_realdn, &ndn ) )
1465 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1467 struct berval ndn = BER_BVNULL;
1470 if ( op->o_ndn.bv_len == 0 ) {
1474 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1475 b->a_group_pat.bv_val, 0, 0 );
1477 /* b->a_group is an unexpanded entry name, expanded it should be an
1478 * entry with objectclass group* and we test to see if odn is one of
1479 * the values in the attribute group
1481 /* see if asker is listed in dnattr */
1482 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1483 char buf[ACL_BUF_SIZE];
1485 regmatch_t tmp_matches[2],
1486 *tmp_matchesp = tmp_matches;
1488 bv.bv_len = sizeof(buf) - 1;
1493 switch ( a->acl_dn_style ) {
1494 case ACL_STYLE_REGEX:
1495 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1496 tmp_matchesp = matches;
1497 tmp_nmatch = nmatch;
1501 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1502 case ACL_STYLE_BASE:
1503 tmp_matches[0].rm_so = 0;
1504 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1509 case ACL_STYLE_SUBTREE:
1510 case ACL_STYLE_CHILDREN:
1511 tmp_matches[0].rm_so = 0;
1512 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1513 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1514 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1528 if ( acl_string_expand( &bv, &b->a_group_pat,
1530 tmp_nmatch, tmp_matchesp ) )
1535 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1536 op->o_tmpmemctx ) != LDAP_SUCCESS )
1538 /* did not expand to a valid dn */
1545 bv = b->a_group_pat;
1548 rc = backend_group( op, e, &bv, &op->o_ndn,
1549 b->a_group_oc, b->a_group_at );
1552 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1560 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1562 char buf[ACL_BUF_SIZE];
1564 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1565 b->a_set_pat.bv_val, 0, 0 );
1567 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1569 regmatch_t tmp_matches[2],
1570 *tmp_matchesp = tmp_matches;
1573 bv.bv_len = sizeof( buf ) - 1;
1578 switch ( a->acl_dn_style ) {
1579 case ACL_STYLE_REGEX:
1580 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1581 tmp_matchesp = matches;
1582 tmp_nmatch = nmatch;
1586 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1587 case ACL_STYLE_BASE:
1588 tmp_matches[0].rm_so = 0;
1589 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1594 case ACL_STYLE_SUBTREE:
1595 case ACL_STYLE_CHILDREN:
1596 tmp_matches[0].rm_so = 0;
1597 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1598 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1599 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1613 if ( acl_string_expand( &bv, &b->a_set_pat,
1615 tmp_nmatch, tmp_matchesp ) )
1624 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1629 if ( b->a_authz.sai_ssf ) {
1630 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1631 b->a_authz.sai_ssf, op->o_ssf, 0 );
1632 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1637 if ( b->a_authz.sai_transport_ssf ) {
1638 Debug( LDAP_DEBUG_ACL,
1639 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1640 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1641 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1646 if ( b->a_authz.sai_tls_ssf ) {
1647 Debug( LDAP_DEBUG_ACL,
1648 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1649 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1650 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1655 if ( b->a_authz.sai_sasl_ssf ) {
1656 Debug( LDAP_DEBUG_ACL,
1657 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1658 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1659 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1665 if ( b->a_dynacl ) {
1667 slap_access_t tgrant, tdeny;
1669 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1672 /* this case works different from the others above.
1673 * since dynamic ACL's themselves give permissions, we need
1674 * to first check b->a_access_mask, the ACL's access level.
1676 /* first check if the right being requested
1677 * is allowed by the ACL clause.
1679 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1683 /* start out with nothing granted, nothing denied */
1684 ACL_INVALIDATE(tgrant);
1685 ACL_INVALIDATE(tdeny);
1687 for ( da = b->a_dynacl; da; da = da->da_next ) {
1688 slap_access_t grant,
1691 ACL_INVALIDATE(grant);
1692 ACL_INVALIDATE(deny);
1694 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1695 da->da_name, 0, 0 );
1697 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1703 /* remove anything that the ACL clause does not allow */
1704 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1705 tdeny &= ACL_PRIV_MASK;
1707 /* see if we have anything to contribute */
1708 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1712 /* this could be improved by changing slap_acl_mask so that it can deal with
1713 * by clauses that return grant/deny pairs. Right now, it does either
1714 * additive or subtractive rights, but not both at the same time. So,
1715 * we need to combine the grant/deny pair into a single rights mask in
1716 * a smart way: if either grant or deny is "empty", then we use the
1717 * opposite as is, otherwise we remove any denied rights from the grant
1718 * rights mask and construct an additive mask.
1720 if (ACL_IS_INVALID(tdeny)) {
1721 modmask = tgrant | ACL_PRIV_ADDITIVE;
1723 } else if (ACL_IS_INVALID(tgrant)) {
1724 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1727 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1731 #endif /* SLAP_DYNACL */
1733 modmask = b->a_access_mask;
1736 Debug( LDAP_DEBUG_ACL,
1737 "<= acl_mask: [%d] applying %s (%s)\n",
1738 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1739 b->a_type == ACL_CONTINUE
1741 : b->a_type == ACL_BREAK
1747 if( ACL_IS_ADDITIVE(modmask) ) {
1749 ACL_PRIV_SET( *mask, modmask );
1752 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1754 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1755 /* substract privs */
1756 ACL_PRIV_CLR( *mask, modmask );
1759 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1768 Debug( LDAP_DEBUG_ACL,
1769 "<= acl_mask: [%d] mask: %s\n",
1770 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1772 if( b->a_type == ACL_CONTINUE ) {
1775 } else if ( b->a_type == ACL_BREAK ) {
1783 /* implicit "by * none" clause */
1786 Debug( LDAP_DEBUG_ACL,
1787 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1788 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1793 * acl_check_modlist - check access control on the given entry to see if
1794 * it allows the given modifications by the user associated with op.
1795 * returns 1 if mods allowed ok
1796 * 0 mods not allowed
1803 Modifications *mlist )
1806 AccessControlState state = ACL_STATE_INIT;
1809 int ret = 1; /* default is access allowed */
1813 be = LDAP_STAILQ_FIRST(&backendDB);
1817 assert( be != NULL );
1819 /* short circuit root database access */
1820 if ( be_isroot( op ) ) {
1821 Debug( LDAP_DEBUG_ACL,
1822 "<= acl_access_allowed: granted to database root\n",
1827 /* use backend default access if no backend acls */
1828 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1829 Debug( LDAP_DEBUG_ACL,
1830 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1831 access2str( ACL_WRITE ),
1832 op->o_bd->be_dfltaccess >= ACL_WRITE
1833 ? "granted" : "denied",
1835 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1839 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1841 * Internal mods are ignored by ACL_WRITE checking
1843 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1844 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1845 " modify access granted\n",
1846 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1851 * no-user-modification operational attributes are ignored
1852 * by ACL_WRITE checking as any found here are not provided
1855 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1856 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1858 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1859 " modify access granted\n",
1860 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1864 switch ( mlist->sml_op ) {
1865 case LDAP_MOD_REPLACE:
1866 case LDAP_MOD_INCREMENT:
1868 * We must check both permission to delete the whole
1869 * attribute and permission to add the specific attributes.
1870 * This prevents abuse from selfwriters.
1872 if ( ! access_allowed( op, e,
1873 mlist->sml_desc, NULL,
1874 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1881 if ( mlist->sml_values == NULL ) break;
1883 /* fall thru to check value to add */
1886 assert( mlist->sml_values != NULL );
1888 for ( bv = mlist->sml_nvalues
1889 ? mlist->sml_nvalues : mlist->sml_values;
1890 bv->bv_val != NULL; bv++ )
1892 if ( ! access_allowed( op, e,
1893 mlist->sml_desc, bv,
1894 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1903 case LDAP_MOD_DELETE:
1904 if ( mlist->sml_values == NULL ) {
1905 if ( ! access_allowed( op, e,
1906 mlist->sml_desc, NULL,
1907 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1915 for ( bv = mlist->sml_nvalues
1916 ? mlist->sml_nvalues : mlist->sml_values;
1917 bv->bv_val != NULL; bv++ )
1919 if ( ! access_allowed( op, e,
1920 mlist->sml_desc, bv,
1921 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1930 case SLAP_MOD_SOFTADD:
1931 /* allow adding attribute via modrdn thru */
1943 if (be_null) op->o_bd = NULL;
1949 struct berval *list,
1962 while ( len >= 0 && --ix >= 0 ) {
1963 while ( --len >= 0 && *p++ != sep )
1966 while ( len >= 0 && *p == ' ' ) {
1979 while ( --len >= 0 && *p != sep ) {
1983 while ( bv->bv_len > 0 && *--p == ' ' ) {
1990 typedef struct acl_set_gather_t {
1996 acl_set_cb_gather( Operation *op, SlapReply *rs )
1998 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2000 if ( rs->sr_type == REP_SEARCH ) {
2001 BerValue bvals[ 2 ];
2002 BerVarray bvalsp = NULL;
2005 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2006 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2008 if ( desc == slap_schema.si_ad_entryDN ) {
2010 bvals[ 0 ] = rs->sr_entry->e_nname;
2011 BER_BVZERO( &bvals[ 1 ] );
2016 a = attr_find( rs->sr_entry->e_attrs, desc );
2020 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2023 bvalsp = a->a_nvals;
2029 p->bvals = slap_set_join( p->cookie, p->bvals,
2030 ( '|' | SLAP_SET_RREF ), bvalsp );
2034 assert( rs->sr_type == REP_RESULT );
2041 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2043 AclSetCookie *cp = (AclSetCookie *)cookie;
2045 LDAPURLDesc *ludp = NULL;
2046 Operation op2 = { 0 };
2047 SlapReply rs = {REP_RESULT};
2048 AttributeName anlist[ 2 ], *anlistp = NULL;
2050 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2051 acl_set_gather_t p = { 0 };
2052 const char *text = NULL;
2053 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2055 /* this routine needs to return the bervals instead of
2056 * plain strings, since syntax is not known. It should
2057 * also return the syntax or some "comparison cookie".
2059 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2060 return acl_set_gather2( cookie, name, desc );
2063 rc = ldap_url_parse( name->bv_val, &ludp );
2064 if ( rc != LDAP_URL_SUCCESS ) {
2065 rc = LDAP_PROTOCOL_ERROR;
2069 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2071 /* host part must be empty */
2072 /* extensions parts must be empty */
2073 rc = LDAP_PROTOCOL_ERROR;
2077 /* Grab the searchbase and see if an appropriate database can be found */
2078 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2079 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2080 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2081 BER_BVZERO( &op2.o_req_dn );
2082 if ( rc != LDAP_SUCCESS ) {
2086 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2087 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2088 rc = LDAP_NO_SUCH_OBJECT;
2092 /* Grab the filter */
2093 if ( ludp->lud_filter ) {
2094 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2095 cp->asc_op->o_tmpmemctx );
2098 op2.ors_filterstr = defaultFilter_bv;
2101 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2102 if ( op2.ors_filter == NULL ) {
2103 rc = LDAP_PROTOCOL_ERROR;
2107 /* Grab the scope */
2108 op2.ors_scope = ludp->lud_scope;
2110 /* Grap the attributes */
2111 if ( ludp->lud_attrs ) {
2112 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2115 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2116 cp->asc_op->o_tmpmemctx );
2118 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2119 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2120 anlistp[ nattrs ].an_desc = NULL;
2121 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2122 &anlistp[ nattrs ].an_desc, &text );
2123 if ( rc != LDAP_SUCCESS ) {
2132 anlistp[ nattrs ].an_name = desc->ad_cname;
2133 anlistp[ nattrs ].an_desc = desc;
2135 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2139 op2.o_hdr = cp->asc_op->o_hdr;
2140 op2.o_tag = LDAP_REQ_SEARCH;
2141 op2.o_ndn = op2.o_bd->be_rootndn;
2142 op2.o_callback = &cb;
2143 slap_op_time( &op2.o_time, &op2.o_tincr );
2144 op2.o_do_not_cache = 1;
2145 op2.o_is_auth_check = 0;
2146 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2147 op2.ors_slimit = SLAP_NO_LIMIT;
2148 op2.ors_tlimit = SLAP_NO_LIMIT;
2149 op2.ors_attrs = anlistp;
2150 op2.ors_attrsonly = 0;
2151 op2.o_private = cp->asc_op->o_private;
2155 rc = op2.o_bd->be_search( &op2, &rs );
2161 if ( op2.ors_filter ) {
2162 filter_free_x( cp->asc_op, op2.ors_filter );
2164 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2165 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2167 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2168 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2171 ldap_free_urldesc( ludp );
2173 if ( anlistp && anlistp != anlist ) {
2174 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2181 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2183 AclSetCookie *cp = (AclSetCookie *)cookie;
2184 BerVarray bvals = NULL;
2188 /* this routine needs to return the bervals instead of
2189 * plain strings, since syntax is not known. It should
2190 * also return the syntax or some "comparison cookie".
2192 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2193 if ( rc == LDAP_SUCCESS ) {
2194 if ( desc == slap_schema.si_ad_entryDN ) {
2195 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2196 cp->asc_op->o_tmpmemctx );
2198 BER_BVZERO( &bvals[ 1 ] );
2202 backend_attribute( cp->asc_op,
2203 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2206 if ( !BER_BVISNULL( &ndn ) ) {
2207 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2216 struct berval *subj,
2219 struct berval *default_set_attribute )
2221 struct berval set = BER_BVNULL;
2223 AclSetCookie cookie;
2225 if ( default_set_attribute == NULL ) {
2226 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2229 struct berval subjdn, ndn = BER_BVNULL;
2230 struct berval setat;
2231 BerVarray bvals = NULL;
2233 AttributeDescription *desc = NULL;
2235 /* format of string is "entry/setAttrName" */
2236 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2240 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2241 setat = *default_set_attribute;
2245 * NOTE: dnNormalize honors the ber_len field
2246 * as the length of the dn to be normalized
2248 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2249 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2251 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2252 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2256 BER_BVZERO( &bvals[0] );
2257 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2259 bvals[0].bv_val = bvals[i-1].bv_val;
2260 BER_BVZERO( &bvals[i-1] );
2262 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2263 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2268 if ( !BER_BVISNULL( &set ) ) {
2271 rc = ( slap_set_filter(
2273 (SetCookie *)&cookie, &set,
2274 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2275 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2284 * dynamic ACL infrastructure
2286 static slap_dynacl_t *da_list = NULL;
2289 slap_dynacl_register( slap_dynacl_t *da )
2293 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2294 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2299 if ( tmp != NULL ) {
2303 if ( da->da_mask == NULL ) {
2307 da->da_private = NULL;
2308 da->da_next = da_list;
2314 static slap_dynacl_t *
2315 slap_dynacl_next( slap_dynacl_t *da )
2324 slap_dynacl_get( const char *name )
2328 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2329 if ( strcasecmp( da->da_name, name ) == 0 ) {
2336 #endif /* SLAP_DYNACL */
2339 * statically built-in dynamic ACL initialization
2341 static int (*acl_init_func[])( void ) = {
2343 /* TODO: remove when ACI will only be dynamic */
2344 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2346 #endif /* SLAPD_ACI_ENABLED */
2347 #endif /* SLAP_DYNACL */
2357 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2358 rc = (*(acl_init_func[ i ]))();
2373 regmatch_t *matches)
2381 bv->bv_val[0] = '\0';
2382 bv->bv_len--; /* leave space for lone $ */
2385 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2386 sp < pat->bv_val + pat->bv_len ; sp++ )
2388 /* did we previously see a $ */
2390 if ( flag == 1 && *sp == '$' ) {
2395 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2398 } else if ( *sp >= '0' && *sp <= '9' ) {
2406 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2407 if ( *sp >= '0' && *sp <= '9' ) {
2408 n = 10*n + ( *sp - '0' );
2412 if ( *sp != /*'{'*/ '}' ) {
2418 if ( n >= nmatch ) {
2424 i = matches[n].rm_so;
2425 l = matches[n].rm_eo;
2426 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2444 /* must have ended with a single $ */
2452 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2453 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2460 struct berval *pat, /* pattern to expand and match against */
2461 char *str, /* string to match against pattern */
2462 char *buf, /* buffer with $N expansion variables */
2463 int nmatch, /* size of the matches array */
2464 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2468 char newbuf[ACL_BUF_SIZE];
2472 bv.bv_len = sizeof( newbuf ) - 1;
2479 acl_string_expand( &bv, pat, buf, nmatch, matches );
2480 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2482 char error[ACL_BUF_SIZE];
2483 regerror( rc, &re, error, sizeof( error ) );
2485 Debug( LDAP_DEBUG_TRACE,
2486 "compile( \"%s\", \"%s\") failed %s\n",
2487 pat->bv_val, str, error );
2491 rc = regexec( &re, str, 0, NULL, 0 );
2494 Debug( LDAP_DEBUG_TRACE,
2495 "=> regex_matches: string: %s\n", str, 0, 0 );
2496 Debug( LDAP_DEBUG_TRACE,
2497 "=> regex_matches: rc: %d %s\n",
2498 rc, !rc ? "matches" : "no matches", 0 );