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
97 #ifdef SLAP_OVERLAY_ACCESS
99 slap_access_always_allowed(
102 AttributeDescription *desc,
104 slap_access_t access,
105 AccessControlState *state,
108 assert( maskp != NULL );
111 ACL_LVL_ASSIGN_MANAGE( *maskp );
120 AttributeDescription *desc,
122 slap_access_t access,
123 AccessControlState *state,
128 AccessControl *a = NULL;
131 char accessmaskbuf[ACCESSMASK_MAXLEN];
134 slap_control_t control;
135 slap_access_t access_level;
137 regmatch_t matches[MAXREMATCHES];
138 int st_same_attr = 0;
140 assert( op != NULL );
142 assert( desc != NULL );
143 assert( maskp != NULL );
145 access_level = ACL_LEVEL( access );
146 attr = desc->ad_cname.bv_val;
148 assert( attr != NULL );
152 /* grant database root access */
153 if ( be_isroot( op ) ) {
154 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
155 mask = ACL_LVL_MANAGE;
160 * no-user-modification operational attributes are ignored
161 * by ACL_WRITE checking as any found here are not provided
164 * NOTE: but they are not ignored for ACL_MANAGE, because
165 * if we get here it means a non-root user is trying to
166 * manage data, so we need to check its privileges.
168 if ( access_level == ACL_WRITE
169 && is_at_no_user_mod( desc->ad_type )
170 && desc != slap_schema.si_ad_entry
171 && desc != slap_schema.si_ad_children )
173 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
174 " %s access granted\n",
179 /* use backend default access if no backend acls */
180 if ( op->o_bd->be_acl == NULL ) {
183 Debug( LDAP_DEBUG_ACL,
184 "=> slap_access_allowed: backend default %s "
185 "access %s to \"%s\"\n",
186 access2str( access ),
187 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
188 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
189 ret = op->o_bd->be_dfltaccess >= access_level;
191 mask = ACL_PRIV_LEVEL;
192 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
193 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
202 if ( st_same_attr ) {
203 assert( state->as_vd_acl != NULL );
205 a = state->as_vd_acl;
206 count = state->as_vd_acl_count;
207 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
208 mask = state->as_vd_acl_mask;
209 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
214 if ( state ) state->as_vi_acl = NULL;
216 ACL_PRIV_ASSIGN( mask, *maskp );
218 memset( matches, '\0', sizeof( matches ) );
221 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
222 MAXREMATCHES, matches, state ) ) != NULL )
226 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
227 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
228 (int)matches[i].rm_so, (int)matches[i].rm_eo );
229 if ( matches[i].rm_so <= matches[0].rm_eo ) {
231 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
232 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
235 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
239 if ( state->as_vi_acl == a &&
240 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
242 Debug( LDAP_DEBUG_ACL,
243 "=> slap_access_allowed: result from state (%s)\n",
245 ret = state->as_result;
248 Debug( LDAP_DEBUG_ACL,
249 "=> slap_access_allowed: no res from state (%s)\n",
255 control = slap_acl_mask( a, &mask, op,
256 e, desc, val, MAXREMATCHES, matches, count, state );
258 if ( control != ACL_BREAK ) {
262 memset( matches, '\0', sizeof( matches ) );
265 if ( ACL_IS_INVALID( mask ) ) {
266 Debug( LDAP_DEBUG_ACL,
267 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
269 ACL_PRIV_ASSIGN( mask, *maskp );
271 } else if ( control == ACL_BREAK ) {
272 Debug( LDAP_DEBUG_ACL,
273 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
278 ret = ACL_GRANT( mask, access );
280 Debug( LDAP_DEBUG_ACL,
281 "=> slap_access_allowed: %s access %s by %s\n",
282 access2str( access ), ret ? "granted" : "denied",
283 accessmask2str( mask, accessmaskbuf, 1 ) );
286 ACL_PRIV_ASSIGN( *maskp, mask );
294 AttributeDescription *desc,
296 slap_access_t access,
297 AccessControlState *state,
304 * NOTE: control gets here if FIXME
305 * if an appropriate backend cannot be selected for the operation,
306 * we assume that the frontend should handle this
307 * FIXME: should select_backend() take care of this,
308 * and return frontendDB instead of NULL? maybe for some value
313 if ( op->o_bd == NULL ) {
314 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
315 if ( op->o_bd == NULL )
316 op->o_bd = frontendDB;
318 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
328 AttributeDescription *desc,
330 slap_access_t access,
331 AccessControlState *state,
335 AccessControl *a = NULL;
339 char accessmaskbuf[ACCESSMASK_MAXLEN];
342 slap_access_t access_level;
344 int st_same_attr = 0;
345 static AccessControlState state_init = ACL_STATE_INIT;
348 assert( desc != NULL );
350 access_level = ACL_LEVEL( access );
352 assert( access_level > ACL_NONE );
355 if ( maskp ) ACL_INVALIDATE( *maskp );
357 attr = desc->ad_cname.bv_val;
359 assert( attr != NULL );
362 if ( op->o_is_auth_check &&
363 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
367 } else if ( get_manageDIT( op ) && access_level == ACL_WRITE &&
368 desc == slap_schema.si_ad_entry )
375 if ( state->as_vd_ad == desc ) {
376 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
379 return state->as_result;
381 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
382 val != NULL && state->as_vd_acl == NULL )
384 return state->as_result;
391 state->as_vd_ad = desc;
394 Debug( LDAP_DEBUG_ACL,
395 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
396 access2str( access ), e->e_dn, attr );
403 if ( op->o_bd == NULL ) {
404 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
409 * FIXME: experimental; use first backend rules
410 * iff there is no global_acl (ITS#3100) */
411 if ( frontendDB->be_acl != NULL ) {
412 op->o_bd = frontendDB;
414 #endif /* LDAP_DEVEL */
416 assert( op->o_bd != NULL );
418 /* this is enforced in backend_add() */
419 if ( op->o_bd->bd_info->bi_access_allowed ) {
420 /* delegate to backend */
421 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
422 desc, val, access, state, &mask );
425 /* use default (but pass through frontend
426 * for global ACL overlays) */
427 ret = frontendDB->bd_info->bi_access_allowed( op, e,
428 desc, val, access, state, &mask );
432 if ( ACL_IS_INVALID( mask ) ) {
433 Debug( LDAP_DEBUG_ACL,
434 "=> access_allowed: \"%s\" (%s) invalid!\n",
439 Debug( LDAP_DEBUG_ACL,
440 "=> access_allowed: no more rules\n", 0, 0, 0 );
446 Debug( LDAP_DEBUG_ACL,
447 "=> access_allowed: %s access %s by %s\n",
448 access2str( access ), ret ? "granted" : "denied",
449 accessmask2str( mask, accessmaskbuf, 1 ) );
452 if ( state != NULL ) {
453 /* If not value-dependent, save ACL in case of more attrs */
454 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
455 state->as_vi_acl = a;
456 state->as_result = ret;
458 state->as_recorded |= ACL_STATE_RECORDED;
460 if ( be_null ) op->o_bd = NULL;
461 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
465 #else /* !SLAP_OVERLAY_ACCESS */
471 AttributeDescription *desc,
473 slap_access_t access,
474 AccessControlState *state,
479 AccessControl *a = NULL;
484 char accessmaskbuf[ACCESSMASK_MAXLEN];
487 slap_control_t control;
488 slap_access_t access_level;
490 regmatch_t matches[MAXREMATCHES];
491 int st_same_attr = 0;
492 static AccessControlState state_init = ACL_STATE_INIT;
495 assert( desc != NULL );
497 access_level = ACL_LEVEL( access );
499 assert( access_level > ACL_NONE );
500 if ( maskp ) ACL_INVALIDATE( *maskp );
502 attr = desc->ad_cname.bv_val;
504 assert( attr != NULL );
507 if ( op->o_is_auth_check &&
508 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
512 } else if ( get_manageDIT( op ) && access_level == ACL_WRITE &&
513 desc == slap_schema.si_ad_entry )
520 if ( state->as_vd_ad == desc ) {
521 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
524 return state->as_result;
526 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
527 val != NULL && state->as_vd_acl == NULL )
529 return state->as_result;
536 state->as_vd_ad = desc;
539 Debug( LDAP_DEBUG_ACL,
540 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
541 access2str( access ), e->e_dn, attr );
550 be = LDAP_STAILQ_FIRST(&backendDB);
554 * FIXME: experimental; use first backend rules
555 * iff there is no global_acl (ITS#3100) */
556 if ( frontendDB->be_acl == NULL )
562 assert( be != NULL );
564 /* grant database root access */
565 if ( be_isroot( op ) ) {
566 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
568 mask = ACL_LVL_MANAGE;
575 * no-user-modification operational attributes are ignored
576 * by ACL_WRITE checking as any found here are not provided
579 * NOTE: but they are not ignored for ACL_MANAGE, because
580 * if we get here it means a non-root user is trying to
581 * manage data, so we need to check its privileges.
583 if ( access_level == ACL_WRITE && is_at_no_user_mod( desc->ad_type )
584 && desc != slap_schema.si_ad_entry
585 && desc != slap_schema.si_ad_children )
587 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
588 " %s access granted\n",
593 /* use backend default access if no backend acls */
594 if ( be->be_acl == NULL ) {
595 Debug( LDAP_DEBUG_ACL,
596 "=> access_allowed: backend default %s "
597 "access %s to \"%s\"\n",
598 access2str( access ),
599 be->be_dfltaccess >= access_level ? "granted" : "denied",
600 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
601 ret = be->be_dfltaccess >= access_level;
606 mask = ACL_PRIV_LEVEL;
607 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
608 mask |= ACL_ACCESS2PRIV( i );
615 /* be is always non-NULL */
616 /* use global default access if no global acls */
617 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
618 Debug( LDAP_DEBUG_ACL,
619 "=> access_allowed: global default %s access %s to \"%s\"\n",
620 access2str( access ),
621 frontendDB->be_dfltaccess >= access_level ?
622 "granted" : "denied", op->o_dn.bv_val );
623 ret = frontendDB->be_dfltaccess >= access_level;
628 mask = ACL_PRIV_LEVEL;
629 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
630 mask |= ACL_ACCESS2PRIV( i );
641 if ( st_same_attr ) {
642 assert( state->as_vd_acl != NULL );
644 a = state->as_vd_acl;
645 count = state->as_vd_acl_count;
646 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
647 mask = state->as_vd_acl_mask;
648 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
653 if ( state ) state->as_vi_acl = NULL;
657 memset( matches, '\0', sizeof(matches) );
660 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
661 MAXREMATCHES, matches, state ) ) != NULL )
665 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
666 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
667 (int)matches[i].rm_so, (int)matches[i].rm_eo );
668 if ( matches[i].rm_so <= matches[0].rm_eo ) {
670 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
671 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
674 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
678 if ( state->as_vi_acl == a &&
679 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
681 Debug( LDAP_DEBUG_ACL,
682 "access_allowed: result from state (%s)\n",
684 ret = state->as_result;
687 Debug( LDAP_DEBUG_ACL,
688 "access_allowed: no res from state (%s)\n",
694 control = slap_acl_mask( a, &mask, op,
695 e, desc, val, MAXREMATCHES, matches, count, state );
697 if ( control != ACL_BREAK ) {
701 memset( matches, '\0', sizeof(matches) );
704 if ( ACL_IS_INVALID( mask ) ) {
705 Debug( LDAP_DEBUG_ACL,
706 "=> access_allowed: \"%s\" (%s) invalid!\n",
710 } else if ( control == ACL_BREAK ) {
711 Debug( LDAP_DEBUG_ACL,
712 "=> access_allowed: no more rules\n", 0, 0, 0 );
717 Debug( LDAP_DEBUG_ACL,
718 "=> access_allowed: %s access %s by %s\n",
719 access2str( access ),
720 ACL_GRANT(mask, access) ? "granted" : "denied",
721 accessmask2str( mask, accessmaskbuf, 1 ) );
723 ret = ACL_GRANT(mask, access);
726 if ( state != NULL ) {
727 /* If not value-dependent, save ACL in case of more attrs */
728 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
729 state->as_vi_acl = a;
730 state->as_result = ret;
732 state->as_recorded |= ACL_STATE_RECORDED;
734 if ( be_null ) op->o_bd = NULL;
735 if ( maskp ) *maskp = mask;
739 #endif /* !SLAP_OVERLAY_ACCESS */
742 * slap_acl_get - return the acl applicable to entry e, attribute
743 * attr. the acl returned is suitable for use in subsequent calls to
744 * acl_access_allowed().
747 static AccessControl *
753 AttributeDescription *desc,
757 AccessControlState *state )
764 assert( count != NULL );
765 assert( desc != NULL );
767 attr = desc->ad_cname.bv_val;
769 assert( attr != NULL );
772 if( op->o_bd == NULL ) {
773 a = frontendDB->be_acl;
775 a = op->o_bd->be_acl;
786 dnlen = e->e_nname.bv_len;
788 for ( ; a != NULL; a = a->acl_next ) {
791 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
792 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
793 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
794 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
795 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
799 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
800 *count, a->acl_dn_pat.bv_val, 0 );
801 patlen = a->acl_dn_pat.bv_len;
802 if ( dnlen < patlen )
805 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
806 /* base dn -- entire object DN must match */
807 if ( dnlen != patlen )
810 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
811 ber_len_t rdnlen = 0;
814 if ( dnlen <= patlen )
818 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
823 rdnlen = dn_rdnlen( NULL, &e->e_nname );
824 if ( rdnlen != dnlen - patlen - sep )
827 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
828 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
831 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
832 if ( dnlen <= patlen )
834 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
838 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
842 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
846 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
847 matches[0].rm_so = matches[0].rm_eo = -1;
851 /* Is this ACL only for a specific value? */
852 if ( a->acl_attrval.bv_len ) {
857 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
858 state->as_recorded |= ACL_STATE_RECORDED_VD;
859 state->as_vd_acl = a;
860 state->as_vd_acl_count = *count;
861 state->as_vd_access = a->acl_access;
862 state->as_vd_access_count = 1;
863 ACL_INVALIDATE( state->as_vd_acl_mask );
866 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
867 Debug( LDAP_DEBUG_ACL,
868 "acl_get: valpat %s\n",
869 a->acl_attrval.bv_val, 0, 0 );
870 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
878 Debug( LDAP_DEBUG_ACL,
880 a->acl_attrval.bv_val, 0, 0 );
882 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
883 if (value_match( &match, desc,
884 a->acl_attrval_mr, 0,
885 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
892 patlen = a->acl_attrval.bv_len;
893 vdnlen = val->bv_len;
895 if ( vdnlen < patlen )
898 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
899 if ( vdnlen > patlen )
902 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
903 ber_len_t rdnlen = 0;
905 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
908 rdnlen = dn_rdnlen( NULL, val );
909 if ( rdnlen != vdnlen - patlen - 1 )
912 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
913 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
916 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
917 if ( vdnlen <= patlen )
920 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
924 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
930 if ( a->acl_filter != NULL ) {
931 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
932 if ( rc != LDAP_COMPARE_TRUE ) {
937 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
942 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
950 AttributeDescription *desc,
956 struct berval *opndn )
959 * if access applies to the entry itself, and the
960 * user is bound as somebody in the same namespace as
961 * the entry, OR the given dn matches the dn pattern
964 * NOTE: styles "anonymous", "users" and "self"
965 * have been moved to enum slap_style_t, whose
966 * value is set in a_dn_style; however, the string
967 * is maintaned in a_dn_pat.
969 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
970 if ( !BER_BVISEMPTY( opndn ) ) {
974 } else if ( b->a_style == ACL_STYLE_USERS ) {
975 if ( BER_BVISEMPTY( opndn ) ) {
983 /* must have DN syntax */
984 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) return 1;
986 /* check if the target is an attribute. */
987 if ( val == NULL ) return 1;
989 /* target is attribute, check if the attribute value
992 rc = value_match( &match, desc,
993 desc->ad_type->sat_equality, 0,
994 val, opndn, &dummy );
995 /* on match error or no match, fail the ACL clause */
996 if ( rc != LDAP_SUCCESS || match != 0 )
1000 } else if ( b->a_style == ACL_STYLE_SELF ) {
1001 struct berval ndn, selfndn;
1004 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1008 level = b->a_self_level;
1016 selfndn = e->e_nname;
1019 for ( ; level > 0; level-- ) {
1020 if ( BER_BVISEMPTY( &ndn ) ) {
1023 dnParent( &ndn, &ndn );
1026 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1031 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1032 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1034 regmatch_t tmp_matches[2],
1035 *tmp_matchesp = tmp_matches;
1039 switch ( a->acl_dn_style ) {
1040 case ACL_STYLE_REGEX:
1041 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1042 tmp_matchesp = matches;
1043 tmp_nmatch = nmatch;
1046 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1048 case ACL_STYLE_BASE:
1049 tmp_matches[0].rm_so = 0;
1050 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1055 case ACL_STYLE_SUBTREE:
1056 case ACL_STYLE_CHILDREN:
1057 tmp_matches[0].rm_so = 0;
1058 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1059 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1060 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1074 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1075 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1083 ber_len_t patlen, odnlen;
1086 if ( e->e_dn == NULL )
1089 if ( b->a_expand ) {
1091 char buf[ACL_BUF_SIZE];
1094 regmatch_t tmp_matches[2],
1095 *tmp_matchesp = tmp_matches;
1099 bv.bv_len = sizeof( buf ) - 1;
1102 switch ( a->acl_dn_style ) {
1103 case ACL_STYLE_REGEX:
1104 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1105 tmp_matchesp = matches;
1106 tmp_nmatch = nmatch;
1109 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1111 case ACL_STYLE_BASE:
1112 tmp_matches[0].rm_so = 0;
1113 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1118 case ACL_STYLE_SUBTREE:
1119 case ACL_STYLE_CHILDREN:
1120 tmp_matches[0].rm_so = 0;
1121 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1122 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1123 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1137 if ( acl_string_expand( &bv, &b->a_pat,
1139 tmp_nmatch, tmp_matchesp ) )
1144 if ( dnNormalize(0, NULL, NULL, &bv,
1145 &pat, op->o_tmpmemctx )
1148 /* did not expand to a valid dn */
1156 patlen = pat.bv_len;
1157 odnlen = opndn->bv_len;
1158 if ( odnlen < patlen ) {
1159 goto dn_match_cleanup;
1163 if ( b->a_style == ACL_STYLE_BASE ) {
1164 /* base dn -- entire object DN must match */
1165 if ( odnlen != patlen ) {
1166 goto dn_match_cleanup;
1169 } else if ( b->a_style == ACL_STYLE_ONE ) {
1170 ber_len_t rdnlen = 0;
1172 if ( odnlen <= patlen ) {
1173 goto dn_match_cleanup;
1176 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1177 goto dn_match_cleanup;
1180 rdnlen = dn_rdnlen( NULL, opndn );
1181 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
1182 goto dn_match_cleanup;
1185 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1186 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1187 goto dn_match_cleanup;
1190 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1191 if ( odnlen <= patlen ) {
1192 goto dn_match_cleanup;
1195 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1196 goto dn_match_cleanup;
1199 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1200 int level = b->a_level;
1203 if ( odnlen <= patlen ) {
1204 goto dn_match_cleanup;
1207 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1209 goto dn_match_cleanup;
1213 for ( ; level > 0; level-- ) {
1214 if ( BER_BVISEMPTY( &ndn ) ) {
1215 goto dn_match_cleanup;
1217 dnParent( &ndn, &ndn );
1218 if ( ndn.bv_len < patlen ) {
1219 goto dn_match_cleanup;
1223 if ( ndn.bv_len != patlen ) {
1224 goto dn_match_cleanup;
1228 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1231 if ( pat.bv_val != b->a_pat.bv_val ) {
1232 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1244 * Record value-dependent access control state
1246 #define ACL_RECORD_VALUE_STATE do { \
1247 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1248 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1249 state->as_vd_acl = a; \
1250 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1251 sizeof( state->as_vd_acl_matches )) ; \
1252 state->as_vd_acl_count = count; \
1253 state->as_vd_access = b; \
1254 state->as_vd_access_count = i; \
1266 regmatch_t *matches,
1268 AccessControlState *state,
1269 slap_dn_access *bdn,
1270 struct berval *opndn )
1276 const char *attr = bdn->a_at->ad_cname.bv_val;
1278 assert( attr != NULL );
1280 if ( BER_BVISEMPTY( opndn ) ) {
1284 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1287 /* see if asker is listed in dnattr */
1288 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1290 at = attrs_find( at->a_next, bdn->a_at ) )
1292 if ( value_find_ex( bdn->a_at,
1293 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1294 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1296 &bv, op->o_tmpmemctx ) == 0 )
1305 /* have a dnattr match. if this is a self clause then
1306 * the target must also match the op dn.
1308 if ( bdn->a_self ) {
1309 /* check if the target is an attribute. */
1310 if ( val == NULL ) return 1;
1312 /* target is attribute, check if the attribute value
1315 rc = value_match( &match, bdn->a_at,
1316 bdn->a_at->ad_type->sat_equality, 0,
1318 /* on match error or no match, fail the ACL clause */
1319 if ( rc != LDAP_SUCCESS || match != 0 )
1324 /* no dnattr match, check if this is a self clause */
1325 if ( ! bdn->a_self )
1328 ACL_RECORD_VALUE_STATE;
1330 /* this is a self clause, check if the target is an
1336 /* target is attribute, check if the attribute value
1339 rc = value_match( &match, bdn->a_at,
1340 bdn->a_at->ad_type->sat_equality, 0,
1343 /* on match error or no match, fail the ACL clause */
1344 if ( rc != LDAP_SUCCESS || match != 0 )
1353 * slap_acl_mask - modifies mask based upon the given acl and the
1354 * requested access to entry e, attribute attr, value val. if val
1355 * is null, access to the whole attribute is assumed (all values).
1357 * returns 0 access NOT allowed
1361 static slap_control_t
1367 AttributeDescription *desc,
1370 regmatch_t *matches,
1372 AccessControlState *state )
1377 char accessmaskbuf[ACCESSMASK_MAXLEN];
1380 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1382 assert( a != NULL );
1383 assert( mask != NULL );
1384 assert( desc != NULL );
1386 attr = desc->ad_cname.bv_val;
1388 assert( attr != NULL );
1390 Debug( LDAP_DEBUG_ACL,
1391 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1394 Debug( LDAP_DEBUG_ACL,
1395 "=> acl_mask: to %s by \"%s\", (%s) \n",
1396 val ? "value" : "all values",
1397 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1398 accessmask2str( *mask, accessmaskbuf, 1 ) );
1401 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1402 && state->as_vd_acl == a )
1404 b = state->as_vd_access;
1405 i = state->as_vd_access_count;
1412 for ( ; b != NULL; b = b->a_next, i++ ) {
1413 slap_mask_t oldmask, modmask;
1415 ACL_INVALIDATE( modmask );
1417 /* AND <who> clauses */
1418 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1419 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1420 b->a_dn_pat.bv_val, 0, 0);
1422 * if access applies to the entry itself, and the
1423 * user is bound as somebody in the same namespace as
1424 * the entry, OR the given dn matches the dn pattern
1427 * NOTE: styles "anonymous", "users" and "self"
1428 * have been moved to enum slap_style_t, whose
1429 * value is set in a_dn_style; however, the string
1430 * is maintaned in a_dn_pat.
1433 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1434 &b->a_dn, &op->o_ndn ) )
1440 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1443 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1444 b->a_realdn_pat.bv_val, 0, 0);
1446 * if access applies to the entry itself, and the
1447 * user is bound as somebody in the same namespace as
1448 * the entry, OR the given dn matches the dn pattern
1451 * NOTE: styles "anonymous", "users" and "self"
1452 * have been moved to enum slap_style_t, whose
1453 * value is set in a_dn_style; however, the string
1454 * is maintaned in a_dn_pat.
1457 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1459 ndn = op->o_conn->c_ndn;
1464 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1465 &b->a_realdn, &ndn ) )
1471 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1472 if ( ! op->o_conn->c_listener ) {
1475 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1476 b->a_sockurl_pat.bv_val, 0, 0 );
1478 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1479 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1480 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1481 e->e_ndn, nmatch, matches ) )
1486 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1488 char buf[ACL_BUF_SIZE];
1490 bv.bv_len = sizeof( buf ) - 1;
1492 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1493 e->e_ndn, nmatch, matches ) )
1498 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1504 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1512 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1513 if ( !op->o_conn->c_peer_domain.bv_val ) {
1516 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1517 b->a_domain_pat.bv_val, 0, 0 );
1518 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1519 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1520 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1521 e->e_ndn, nmatch, matches ) )
1526 char buf[ACL_BUF_SIZE];
1528 struct berval cmp = op->o_conn->c_peer_domain;
1529 struct berval pat = b->a_domain_pat;
1531 if ( b->a_domain_expand ) {
1534 bv.bv_len = sizeof(buf) - 1;
1537 if ( acl_string_expand(&bv, &b->a_domain_pat,
1538 e->e_ndn, nmatch, matches) )
1545 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1546 int offset = cmp.bv_len - pat.bv_len;
1551 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1555 /* trim the domain */
1556 cmp.bv_val = &cmp.bv_val[ offset ];
1557 cmp.bv_len -= offset;
1560 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1567 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1568 if ( !op->o_conn->c_peer_name.bv_val ) {
1571 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1572 b->a_peername_pat.bv_val, 0, 0 );
1573 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1574 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1575 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1576 e->e_ndn, nmatch, matches ) )
1582 /* try exact match */
1583 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1584 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1588 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1590 char buf[ACL_BUF_SIZE];
1592 bv.bv_len = sizeof( buf ) - 1;
1594 if ( acl_string_expand( &bv, &b->a_peername_pat,
1595 e->e_ndn, nmatch, matches ) )
1600 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1604 /* extract IP and try exact match */
1605 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1607 char buf[] = "255.255.255.255";
1610 int port_number = -1;
1612 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1613 acl_bv_ip_eq.bv_val,
1614 acl_bv_ip_eq.bv_len ) != 0 )
1617 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1618 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1620 port = strrchr( ip.bv_val, ':' );
1622 ip.bv_len = port - ip.bv_val;
1624 if ( lutil_atoi( &port_number, port ) != 0 )
1628 /* the port check can be anticipated here */
1629 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1632 /* address longer than expected? */
1633 if ( ip.bv_len >= sizeof(buf) )
1636 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1637 buf[ ip.bv_len ] = '\0';
1639 addr = inet_addr( buf );
1641 /* unable to convert? */
1642 if ( addr == (unsigned long)(-1) )
1645 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1648 #ifdef LDAP_PF_LOCAL
1649 /* extract path and try exact match */
1650 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1653 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1654 acl_bv_path_eq.bv_val,
1655 acl_bv_path_eq.bv_len ) != 0 )
1658 path.bv_val = op->o_conn->c_peer_name.bv_val
1659 + acl_bv_path_eq.bv_len;
1660 path.bv_len = op->o_conn->c_peer_name.bv_len
1661 - acl_bv_path_eq.bv_len;
1663 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1666 #endif /* LDAP_PF_LOCAL */
1668 /* exact match (very unlikely...) */
1669 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1676 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1677 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1680 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1681 b->a_sockname_pat.bv_val, 0, 0 );
1682 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1683 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1684 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1685 e->e_ndn, nmatch, matches ) )
1690 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1692 char buf[ACL_BUF_SIZE];
1694 bv.bv_len = sizeof( buf ) - 1;
1696 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1697 e->e_ndn, nmatch, matches ) )
1702 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1707 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1714 if ( b->a_dn_at != NULL ) {
1715 if ( acl_mask_dnattr( op, e, val, a, b, i,
1716 matches, count, state,
1717 &b->a_dn, &op->o_ndn ) )
1723 if ( b->a_realdn_at != NULL ) {
1726 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1728 ndn = op->o_conn->c_ndn;
1733 if ( acl_mask_dnattr( op, e, val, a, b, i,
1734 matches, count, state,
1735 &b->a_realdn, &ndn ) )
1741 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1743 struct berval ndn = BER_BVNULL;
1746 if ( op->o_ndn.bv_len == 0 ) {
1750 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1751 b->a_group_pat.bv_val, 0, 0 );
1753 /* b->a_group is an unexpanded entry name, expanded it should be an
1754 * entry with objectclass group* and we test to see if odn is one of
1755 * the values in the attribute group
1757 /* see if asker is listed in dnattr */
1758 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1759 char buf[ACL_BUF_SIZE];
1761 regmatch_t tmp_matches[2],
1762 *tmp_matchesp = tmp_matches;
1764 bv.bv_len = sizeof(buf) - 1;
1769 switch ( a->acl_dn_style ) {
1770 case ACL_STYLE_REGEX:
1771 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1772 tmp_matchesp = matches;
1773 tmp_nmatch = nmatch;
1777 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1778 case ACL_STYLE_BASE:
1779 tmp_matches[0].rm_so = 0;
1780 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1785 case ACL_STYLE_SUBTREE:
1786 case ACL_STYLE_CHILDREN:
1787 tmp_matches[0].rm_so = 0;
1788 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1789 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1790 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1804 if ( acl_string_expand( &bv, &b->a_group_pat,
1806 tmp_nmatch, tmp_matchesp ) )
1811 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1812 op->o_tmpmemctx ) != LDAP_SUCCESS )
1814 /* did not expand to a valid dn */
1821 bv = b->a_group_pat;
1824 rc = backend_group( op, e, &bv, &op->o_ndn,
1825 b->a_group_oc, b->a_group_at );
1828 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1836 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1838 char buf[ACL_BUF_SIZE];
1840 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1841 b->a_set_pat.bv_val, 0, 0 );
1843 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1845 regmatch_t tmp_matches[2],
1846 *tmp_matchesp = tmp_matches;
1849 bv.bv_len = sizeof( buf ) - 1;
1854 switch ( a->acl_dn_style ) {
1855 case ACL_STYLE_REGEX:
1856 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1857 tmp_matchesp = matches;
1858 tmp_nmatch = nmatch;
1862 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1863 case ACL_STYLE_BASE:
1864 tmp_matches[0].rm_so = 0;
1865 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1870 case ACL_STYLE_SUBTREE:
1871 case ACL_STYLE_CHILDREN:
1872 tmp_matches[0].rm_so = 0;
1873 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1874 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1875 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1889 if ( acl_string_expand( &bv, &b->a_set_pat,
1891 tmp_nmatch, tmp_matchesp ) )
1900 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1905 if ( b->a_authz.sai_ssf ) {
1906 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1907 b->a_authz.sai_ssf, op->o_ssf, 0 );
1908 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1913 if ( b->a_authz.sai_transport_ssf ) {
1914 Debug( LDAP_DEBUG_ACL,
1915 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1916 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1917 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1922 if ( b->a_authz.sai_tls_ssf ) {
1923 Debug( LDAP_DEBUG_ACL,
1924 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1925 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1926 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1931 if ( b->a_authz.sai_sasl_ssf ) {
1932 Debug( LDAP_DEBUG_ACL,
1933 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1934 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1935 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1941 if ( b->a_dynacl ) {
1943 slap_access_t tgrant, tdeny;
1945 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1948 /* this case works different from the others above.
1949 * since dynamic ACL's themselves give permissions, we need
1950 * to first check b->a_access_mask, the ACL's access level.
1952 /* first check if the right being requested
1953 * is allowed by the ACL clause.
1955 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1959 /* start out with nothing granted, nothing denied */
1963 for ( da = b->a_dynacl; da; da = da->da_next ) {
1964 slap_access_t grant,
1970 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1971 da->da_name, 0, 0 );
1973 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1979 /* remove anything that the ACL clause does not allow */
1980 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1981 tdeny &= ACL_PRIV_MASK;
1983 /* see if we have anything to contribute */
1984 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1988 /* this could be improved by changing slap_acl_mask so that it can deal with
1989 * by clauses that return grant/deny pairs. Right now, it does either
1990 * additive or subtractive rights, but not both at the same time. So,
1991 * we need to combine the grant/deny pair into a single rights mask in
1992 * a smart way: if either grant or deny is "empty", then we use the
1993 * opposite as is, otherwise we remove any denied rights from the grant
1994 * rights mask and construct an additive mask.
1996 if (ACL_IS_INVALID(tdeny)) {
1997 modmask = tgrant | ACL_PRIV_ADDITIVE;
1999 } else if (ACL_IS_INVALID(tgrant)) {
2000 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2003 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2007 #endif /* SLAP_DYNACL */
2009 modmask = b->a_access_mask;
2012 Debug( LDAP_DEBUG_ACL,
2013 "<= acl_mask: [%d] applying %s (%s)\n",
2014 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2015 b->a_type == ACL_CONTINUE
2017 : b->a_type == ACL_BREAK
2023 if( ACL_IS_ADDITIVE(modmask) ) {
2025 ACL_PRIV_SET( *mask, modmask );
2028 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2030 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2031 /* substract privs */
2032 ACL_PRIV_CLR( *mask, modmask );
2035 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2044 Debug( LDAP_DEBUG_ACL,
2045 "<= acl_mask: [%d] mask: %s\n",
2046 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2048 if( b->a_type == ACL_CONTINUE ) {
2051 } else if ( b->a_type == ACL_BREAK ) {
2059 /* implicit "by * none" clause */
2062 Debug( LDAP_DEBUG_ACL,
2063 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2064 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2069 * acl_check_modlist - check access control on the given entry to see if
2070 * it allows the given modifications by the user associated with op.
2071 * returns 1 if mods allowed ok
2072 * 0 mods not allowed
2079 Modifications *mlist )
2082 AccessControlState state = ACL_STATE_INIT;
2085 int ret = 1; /* default is access allowed */
2089 be = LDAP_STAILQ_FIRST(&backendDB);
2093 assert( be != NULL );
2095 /* short circuit root database access */
2096 if ( be_isroot( op ) ) {
2097 Debug( LDAP_DEBUG_ACL,
2098 "<= acl_access_allowed: granted to database root\n",
2103 /* use backend default access if no backend acls */
2104 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2105 Debug( LDAP_DEBUG_ACL,
2106 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2107 access2str( ACL_WRITE ),
2108 op->o_bd->be_dfltaccess >= ACL_WRITE
2109 ? "granted" : "denied",
2111 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2115 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2117 * Internal mods are ignored by ACL_WRITE checking
2119 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2120 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2121 " modify access granted\n",
2122 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2127 * no-user-modification operational attributes are ignored
2128 * by ACL_WRITE checking as any found here are not provided
2131 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
2132 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
2134 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2135 " modify access granted\n",
2136 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2140 switch ( mlist->sml_op ) {
2141 case LDAP_MOD_REPLACE:
2142 case LDAP_MOD_INCREMENT:
2144 * We must check both permission to delete the whole
2145 * attribute and permission to add the specific attributes.
2146 * This prevents abuse from selfwriters.
2148 if ( ! access_allowed( op, e,
2149 mlist->sml_desc, NULL,
2150 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2157 if ( mlist->sml_values == NULL ) break;
2159 /* fall thru to check value to add */
2162 assert( mlist->sml_values != NULL );
2164 for ( bv = mlist->sml_nvalues
2165 ? mlist->sml_nvalues : mlist->sml_values;
2166 bv->bv_val != NULL; bv++ )
2168 if ( ! access_allowed( op, e,
2169 mlist->sml_desc, bv,
2170 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2179 case LDAP_MOD_DELETE:
2180 if ( mlist->sml_values == NULL ) {
2181 if ( ! access_allowed( op, e,
2182 mlist->sml_desc, NULL,
2183 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2191 for ( bv = mlist->sml_nvalues
2192 ? mlist->sml_nvalues : mlist->sml_values;
2193 bv->bv_val != NULL; bv++ )
2195 if ( ! access_allowed( op, e,
2196 mlist->sml_desc, bv,
2197 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2206 case SLAP_MOD_SOFTADD:
2207 /* allow adding attribute via modrdn thru */
2219 if (be_null) op->o_bd = NULL;
2225 struct berval *list,
2238 while ( len >= 0 && --ix >= 0 ) {
2239 while ( --len >= 0 && *p++ != sep )
2242 while ( len >= 0 && *p == ' ' ) {
2255 while ( --len >= 0 && *p != sep ) {
2259 while ( bv->bv_len > 0 && *--p == ' ' ) {
2266 typedef struct acl_set_gather_t {
2272 acl_set_cb_gather( Operation *op, SlapReply *rs )
2274 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2276 if ( rs->sr_type == REP_SEARCH ) {
2277 BerValue bvals[ 2 ];
2278 BerVarray bvalsp = NULL;
2281 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2282 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2284 if ( desc == slap_schema.si_ad_entryDN ) {
2286 bvals[ 0 ] = rs->sr_entry->e_nname;
2287 BER_BVZERO( &bvals[ 1 ] );
2292 a = attr_find( rs->sr_entry->e_attrs, desc );
2296 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2299 bvalsp = a->a_nvals;
2305 p->bvals = slap_set_join( p->cookie, p->bvals,
2306 ( '|' | SLAP_SET_RREF ), bvalsp );
2310 assert( rs->sr_type == REP_RESULT );
2317 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2319 AclSetCookie *cp = (AclSetCookie *)cookie;
2321 LDAPURLDesc *ludp = NULL;
2322 Operation op2 = { 0 };
2323 SlapReply rs = {REP_RESULT};
2324 AttributeName anlist[ 2 ], *anlistp = NULL;
2326 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2327 acl_set_gather_t p = { 0 };
2328 const char *text = NULL;
2329 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2331 /* this routine needs to return the bervals instead of
2332 * plain strings, since syntax is not known. It should
2333 * also return the syntax or some "comparison cookie".
2335 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2336 return acl_set_gather2( cookie, name, desc );
2339 rc = ldap_url_parse( name->bv_val, &ludp );
2340 if ( rc != LDAP_URL_SUCCESS ) {
2341 rc = LDAP_PROTOCOL_ERROR;
2345 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2347 /* host part must be empty */
2348 /* extensions parts must be empty */
2349 rc = LDAP_PROTOCOL_ERROR;
2353 /* Grab the searchbase and see if an appropriate database can be found */
2354 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2355 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2356 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2357 BER_BVZERO( &op2.o_req_dn );
2358 if ( rc != LDAP_SUCCESS ) {
2362 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2363 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2364 rc = LDAP_NO_SUCH_OBJECT;
2368 /* Grab the filter */
2369 if ( ludp->lud_filter ) {
2370 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2371 cp->asc_op->o_tmpmemctx );
2374 op2.ors_filterstr = defaultFilter_bv;
2377 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2378 if ( op2.ors_filter == NULL ) {
2379 rc = LDAP_PROTOCOL_ERROR;
2383 /* Grab the scope */
2384 op2.ors_scope = ludp->lud_scope;
2386 /* Grap the attributes */
2387 if ( ludp->lud_attrs ) {
2388 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2391 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2392 cp->asc_op->o_tmpmemctx );
2394 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2395 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2396 anlistp[ nattrs ].an_desc = NULL;
2397 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2398 &anlistp[ nattrs ].an_desc, &text );
2399 if ( rc != LDAP_SUCCESS ) {
2408 anlistp[ nattrs ].an_name = desc->ad_cname;
2409 anlistp[ nattrs ].an_desc = desc;
2411 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2415 op2.o_hdr = cp->asc_op->o_hdr;
2416 op2.o_tag = LDAP_REQ_SEARCH;
2417 op2.o_ndn = op2.o_bd->be_rootndn;
2418 op2.o_callback = &cb;
2419 slap_op_time( &op2.o_time, &op2.o_tincr );
2420 op2.o_do_not_cache = 1;
2421 op2.o_is_auth_check = 0;
2422 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2423 op2.ors_slimit = SLAP_NO_LIMIT;
2424 op2.ors_tlimit = SLAP_NO_LIMIT;
2425 op2.ors_attrs = anlistp;
2426 op2.ors_attrsonly = 0;
2427 op2.o_private = cp->asc_op->o_private;
2431 rc = op2.o_bd->be_search( &op2, &rs );
2437 if ( op2.ors_filter ) {
2438 filter_free_x( cp->asc_op, op2.ors_filter );
2440 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2441 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2443 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2444 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2447 ldap_free_urldesc( ludp );
2449 if ( anlistp && anlistp != anlist ) {
2450 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2457 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2459 AclSetCookie *cp = (AclSetCookie *)cookie;
2460 BerVarray bvals = NULL;
2464 /* this routine needs to return the bervals instead of
2465 * plain strings, since syntax is not known. It should
2466 * also return the syntax or some "comparison cookie".
2468 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2469 if ( rc == LDAP_SUCCESS ) {
2470 if ( desc == slap_schema.si_ad_entryDN ) {
2471 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2472 cp->asc_op->o_tmpmemctx );
2474 BER_BVZERO( &bvals[ 1 ] );
2478 backend_attribute( cp->asc_op,
2479 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2482 if ( !BER_BVISNULL( &ndn ) ) {
2483 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2492 struct berval *subj,
2495 struct berval *default_set_attribute )
2497 struct berval set = BER_BVNULL;
2499 AclSetCookie cookie;
2501 if ( default_set_attribute == NULL ) {
2502 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2505 struct berval subjdn, ndn = BER_BVNULL;
2506 struct berval setat;
2509 AttributeDescription *desc = NULL;
2511 /* format of string is "entry/setAttrName" */
2512 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2516 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2517 setat = *default_set_attribute;
2521 * NOTE: dnNormalize honors the ber_len field
2522 * as the length of the dn to be normalized
2524 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2525 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2527 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2528 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2532 BER_BVZERO( &bvals[0] );
2533 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2535 bvals[0].bv_val = bvals[i-1].bv_val;
2536 BER_BVZERO( &bvals[i-1] );
2538 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2539 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2544 if ( !BER_BVISNULL( &set ) ) {
2547 rc = ( slap_set_filter(
2549 (SetCookie *)&cookie, &set,
2550 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2551 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2560 * dynamic ACL infrastructure
2562 static slap_dynacl_t *da_list = NULL;
2565 slap_dynacl_register( slap_dynacl_t *da )
2569 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2570 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2575 if ( tmp != NULL ) {
2579 if ( da->da_mask == NULL ) {
2583 da->da_private = NULL;
2584 da->da_next = da_list;
2590 static slap_dynacl_t *
2591 slap_dynacl_next( slap_dynacl_t *da )
2600 slap_dynacl_get( const char *name )
2604 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2605 if ( strcasecmp( da->da_name, name ) == 0 ) {
2612 #endif /* SLAP_DYNACL */
2615 * statically built-in dynamic ACL initialization
2617 static int (*acl_init_func[])( void ) = {
2619 /* TODO: remove when ACI will only be dynamic */
2620 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2622 #endif /* SLAPD_ACI_ENABLED */
2623 #endif /* SLAP_DYNACL */
2633 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2634 rc = (*(acl_init_func[ i ]))();
2649 regmatch_t *matches)
2657 bv->bv_val[0] = '\0';
2658 bv->bv_len--; /* leave space for lone $ */
2661 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2662 sp < pat->bv_val + pat->bv_len ; sp++ )
2664 /* did we previously see a $ */
2666 if ( flag == 1 && *sp == '$' ) {
2671 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2674 } else if ( *sp >= '0' && *sp <= '9' ) {
2682 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2683 if ( *sp >= '0' && *sp <= '9' ) {
2684 n = 10*n + ( *sp - '0' );
2688 if ( *sp != /*'{'*/ '}' ) {
2694 if ( n >= nmatch ) {
2700 i = matches[n].rm_so;
2701 l = matches[n].rm_eo;
2702 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2720 /* must have ended with a single $ */
2728 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2729 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2736 struct berval *pat, /* pattern to expand and match against */
2737 char *str, /* string to match against pattern */
2738 char *buf, /* buffer with $N expansion variables */
2739 int nmatch, /* size of the matches array */
2740 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2744 char newbuf[ACL_BUF_SIZE];
2748 bv.bv_len = sizeof( newbuf ) - 1;
2755 acl_string_expand( &bv, pat, buf, nmatch, matches );
2756 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2758 char error[ACL_BUF_SIZE];
2759 regerror( rc, &re, error, sizeof( error ) );
2761 Debug( LDAP_DEBUG_TRACE,
2762 "compile( \"%s\", \"%s\") failed %s\n",
2763 pat->bv_val, str, error );
2767 rc = regexec( &re, str, 0, NULL, 0 );
2770 Debug( LDAP_DEBUG_TRACE,
2771 "=> regex_matches: string: %s\n", str, 0, 0 );
2772 Debug( LDAP_DEBUG_TRACE,
2773 "=> regex_matches: rc: %d %s\n",
2774 rc, !rc ? "matches" : "no matches", 0 );