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_ipv6_eq = BER_BVC( "IP=[" );
45 #endif /* LDAP_PF_INET6 */
47 static const struct berval acl_bv_path_eq = BER_BVC("PATH=");
48 #endif /* LDAP_PF_LOCAL */
50 static AccessControl * slap_acl_get(
51 AccessControl *ac, int *count,
52 Operation *op, Entry *e,
53 AttributeDescription *desc,
55 int nmatch, regmatch_t *matches,
56 AccessControlState *state );
58 static slap_control_t slap_acl_mask(
59 AccessControl *ac, slap_mask_t *mask,
60 Operation *op, Entry *e,
61 AttributeDescription *desc,
66 AccessControlState *state );
68 static int regex_matches(
69 struct berval *pat, char *str, char *buf,
70 int nmatch, regmatch_t *matches);
72 typedef struct AclSetCookie {
74 #define asc_op asc_cookie.set_op
78 SLAP_SET_GATHER acl_set_gather;
79 SLAP_SET_GATHER acl_set_gather2;
82 * access_allowed - check whether op->o_ndn is allowed the requested access
83 * to entry e, attribute attr, value val. if val is null, access to
84 * the whole attribute is assumed (all values).
86 * This routine loops through all access controls and calls
87 * slap_acl_mask() on each applicable access control.
88 * The loop exits when a definitive answer is reached or
89 * or no more controls remain.
96 * - can be legally called with op == NULL
97 * - can be legally called with op->o_bd == NULL
101 slap_access_always_allowed(
104 AttributeDescription *desc,
106 slap_access_t access,
107 AccessControlState *state,
110 assert( maskp != NULL );
113 ACL_LVL_ASSIGN_MANAGE( *maskp );
122 AttributeDescription *desc,
124 slap_access_t access,
125 AccessControlState *state,
130 AccessControl *a = NULL;
133 char accessmaskbuf[ACCESSMASK_MAXLEN];
136 slap_control_t control;
137 slap_access_t access_level;
139 regmatch_t matches[MAXREMATCHES];
141 assert( op != NULL );
143 assert( desc != NULL );
144 assert( maskp != NULL );
146 access_level = ACL_LEVEL( access );
147 attr = desc->ad_cname.bv_val;
149 assert( attr != NULL );
153 /* grant database root access */
154 if ( be_isroot( op ) ) {
155 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
156 mask = ACL_LVL_MANAGE;
161 * no-user-modification operational attributes are ignored
162 * by ACL_WRITE checking as any found here are not provided
165 * NOTE: but they are not ignored for ACL_MANAGE, because
166 * if we get here it means a non-root user is trying to
167 * manage data, so we need to check its privileges.
169 if ( access_level == ACL_WRITE
170 && is_at_no_user_mod( desc->ad_type )
171 && desc != slap_schema.si_ad_entry
172 && desc != slap_schema.si_ad_children )
174 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
175 " %s access granted\n",
180 /* use backend default access if no backend acls */
181 if ( op->o_bd->be_acl == NULL ) {
184 Debug( LDAP_DEBUG_ACL,
185 "=> slap_access_allowed: backend default %s "
186 "access %s to \"%s\"\n",
187 access2str( access ),
188 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
189 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
190 ret = op->o_bd->be_dfltaccess >= access_level;
192 mask = ACL_PRIV_LEVEL;
193 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
194 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
203 if ( state && state->as_vd_ad == desc ) {
204 a = state->as_vd_acl;
205 count = state->as_vd_acl_count;
208 if ( state ) state->as_vi_acl = NULL;
212 ACL_PRIV_ASSIGN( mask, *maskp );
213 memset( matches, '\0', sizeof( matches ) );
215 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
216 MAXREMATCHES, matches, state ) ) != NULL )
220 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
221 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
222 (int)matches[i].rm_so, (int)matches[i].rm_eo );
223 if ( matches[i].rm_so <= matches[0].rm_eo ) {
225 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
226 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
229 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
233 if ( state->as_vi_acl == a &&
234 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
236 Debug( LDAP_DEBUG_ACL,
237 "=> slap_access_allowed: result from state (%s)\n",
239 ret = state->as_result;
242 Debug( LDAP_DEBUG_ACL,
243 "=> slap_access_allowed: no res from state (%s)\n",
248 control = slap_acl_mask( a, &mask, op,
249 e, desc, val, MAXREMATCHES, matches, count, state );
251 if ( control != ACL_BREAK ) {
255 memset( matches, '\0', sizeof( matches ) );
258 if ( ACL_IS_INVALID( mask ) ) {
259 Debug( LDAP_DEBUG_ACL,
260 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
262 ACL_PRIV_ASSIGN( mask, *maskp );
264 } else if ( control == ACL_BREAK ) {
265 Debug( LDAP_DEBUG_ACL,
266 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
271 ret = ACL_GRANT( mask, access );
273 Debug( LDAP_DEBUG_ACL,
274 "=> slap_access_allowed: %s access %s by %s\n",
275 access2str( access ), ret ? "granted" : "denied",
276 accessmask2str( mask, accessmaskbuf, 1 ) );
279 ACL_PRIV_ASSIGN( *maskp, mask );
287 AttributeDescription *desc,
289 slap_access_t access,
290 AccessControlState *state,
297 * NOTE: control gets here if FIXME
298 * if an appropriate backend cannot be selected for the operation,
299 * we assume that the frontend should handle this
300 * FIXME: should select_backend() take care of this,
301 * and return frontendDB instead of NULL? maybe for some value
306 if ( op->o_bd == NULL ) {
307 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
308 if ( op->o_bd == NULL )
309 op->o_bd = frontendDB;
311 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
321 AttributeDescription *desc,
323 slap_access_t access,
324 AccessControlState *state,
328 AccessControl *a = NULL;
332 char accessmaskbuf[ACCESSMASK_MAXLEN];
335 slap_access_t access_level;
337 static AccessControlState state_init = ACL_STATE_INIT;
340 assert( desc != NULL );
342 access_level = ACL_LEVEL( access );
344 assert( access_level > ACL_NONE );
347 if ( maskp ) ACL_INVALIDATE( *maskp );
349 attr = desc->ad_cname.bv_val;
351 assert( attr != NULL );
354 if ( op->o_is_auth_check &&
355 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
359 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
360 desc == slap_schema.si_ad_entry )
367 if ( state->as_vd_ad == desc ) {
368 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
371 return state->as_result;
379 Debug( LDAP_DEBUG_ACL,
380 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
381 access2str( access ), e->e_dn, attr );
388 if ( op->o_bd == NULL ) {
389 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
392 /* FIXME: experimental; use first backend rules
393 * iff there is no global_acl (ITS#3100)
395 if ( frontendDB->be_acl != NULL ) {
396 op->o_bd = frontendDB;
399 assert( op->o_bd != NULL );
401 /* this is enforced in backend_add() */
402 if ( op->o_bd->bd_info->bi_access_allowed ) {
403 /* delegate to backend */
404 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
405 desc, val, access, state, &mask );
408 /* use default (but pass through frontend
409 * for global ACL overlays) */
410 ret = frontendDB->bd_info->bi_access_allowed( op, e,
411 desc, val, access, state, &mask );
415 if ( ACL_IS_INVALID( mask ) ) {
416 Debug( LDAP_DEBUG_ACL,
417 "=> access_allowed: \"%s\" (%s) invalid!\n",
422 Debug( LDAP_DEBUG_ACL,
423 "=> access_allowed: no more rules\n", 0, 0, 0 );
429 Debug( LDAP_DEBUG_ACL,
430 "=> access_allowed: %s access %s by %s\n",
431 access2str( access ), ret ? "granted" : "denied",
432 accessmask2str( mask, accessmaskbuf, 1 ) );
435 if ( state != NULL ) {
436 /* If not value-dependent, save ACL in case of more attrs */
437 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
438 state->as_vi_acl = a;
439 state->as_result = ret;
441 state->as_recorded |= ACL_STATE_RECORDED;
442 state->as_vd_ad = desc;
444 if ( be_null ) op->o_bd = NULL;
445 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
451 * slap_acl_get - return the acl applicable to entry e, attribute
452 * attr. the acl returned is suitable for use in subsequent calls to
453 * acl_access_allowed().
456 static AccessControl *
462 AttributeDescription *desc,
466 AccessControlState *state )
473 assert( count != NULL );
474 assert( desc != NULL );
476 attr = desc->ad_cname.bv_val;
478 assert( attr != NULL );
481 if( op->o_bd == NULL ) {
482 a = frontendDB->be_acl;
484 a = op->o_bd->be_acl;
495 dnlen = e->e_nname.bv_len;
497 for ( ; a != NULL; prev = a, a = a->acl_next ) {
500 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
501 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
502 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
503 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
504 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
508 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
509 *count, a->acl_dn_pat.bv_val, 0 );
510 patlen = a->acl_dn_pat.bv_len;
511 if ( dnlen < patlen )
514 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
515 /* base dn -- entire object DN must match */
516 if ( dnlen != patlen )
519 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
520 ber_len_t rdnlen = 0;
523 if ( dnlen <= patlen )
527 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
532 rdnlen = dn_rdnlen( NULL, &e->e_nname );
533 if ( rdnlen != dnlen - patlen - sep )
536 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
537 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
540 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
541 if ( dnlen <= patlen )
543 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
547 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
551 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
555 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
556 matches[0].rm_so = matches[0].rm_eo = -1;
560 /* Is this ACL only for a specific value? */
561 if ( a->acl_attrval.bv_len ) {
566 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
567 state->as_recorded |= ACL_STATE_RECORDED_VD;
568 state->as_vd_acl = prev;
569 state->as_vd_acl_count = *count - 1;
572 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
573 Debug( LDAP_DEBUG_ACL,
574 "acl_get: valpat %s\n",
575 a->acl_attrval.bv_val, 0, 0 );
576 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
584 Debug( LDAP_DEBUG_ACL,
586 a->acl_attrval.bv_val, 0, 0 );
588 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
589 if (value_match( &match, desc,
590 a->acl_attrval_mr, 0,
591 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
598 patlen = a->acl_attrval.bv_len;
599 vdnlen = val->bv_len;
601 if ( vdnlen < patlen )
604 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
605 if ( vdnlen > patlen )
608 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
609 ber_len_t rdnlen = 0;
611 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
614 rdnlen = dn_rdnlen( NULL, val );
615 if ( rdnlen != vdnlen - patlen - 1 )
618 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
619 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
622 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
623 if ( vdnlen <= patlen )
626 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
630 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
636 if ( a->acl_filter != NULL ) {
637 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
638 if ( rc != LDAP_COMPARE_TRUE ) {
643 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
648 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
653 * Record value-dependent access control state
655 #define ACL_RECORD_VALUE_STATE do { \
656 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
657 state->as_recorded |= ACL_STATE_RECORDED_VD; \
658 state->as_vd_acl = a; \
659 state->as_vd_acl_count = count; \
667 AttributeDescription *desc,
673 struct berval *opndn )
676 * if access applies to the entry itself, and the
677 * user is bound as somebody in the same namespace as
678 * the entry, OR the given dn matches the dn pattern
681 * NOTE: styles "anonymous", "users" and "self"
682 * have been moved to enum slap_style_t, whose
683 * value is set in a_dn_style; however, the string
684 * is maintained in a_dn_pat.
687 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
688 if ( !BER_BVISEMPTY( opndn ) ) {
692 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
693 if ( BER_BVISEMPTY( opndn ) ) {
697 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
698 struct berval ndn, selfndn;
701 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
705 level = bdn->a_self_level;
713 selfndn = e->e_nname;
716 for ( ; level > 0; level-- ) {
717 if ( BER_BVISEMPTY( &ndn ) ) {
720 dnParent( &ndn, &ndn );
723 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
728 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
729 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
731 regmatch_t tmp_matches[2],
732 *tmp_matchesp = tmp_matches;
736 switch ( a->acl_dn_style ) {
737 case ACL_STYLE_REGEX:
738 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
739 tmp_matchesp = matches;
743 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
746 tmp_matches[0].rm_so = 0;
747 tmp_matches[0].rm_eo = e->e_nname.bv_len;
752 case ACL_STYLE_SUBTREE:
753 case ACL_STYLE_CHILDREN:
754 tmp_matches[0].rm_so = 0;
755 tmp_matches[0].rm_eo = e->e_nname.bv_len;
756 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
757 tmp_matches[1].rm_eo = e->e_nname.bv_len;
771 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
772 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
780 ber_len_t patlen, odnlen;
783 if ( e->e_dn == NULL )
786 if ( bdn->a_expand ) {
788 char buf[ACL_BUF_SIZE];
791 regmatch_t tmp_matches[2],
792 *tmp_matchesp = tmp_matches;
796 bv.bv_len = sizeof( buf ) - 1;
799 switch ( a->acl_dn_style ) {
800 case ACL_STYLE_REGEX:
801 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
802 tmp_matchesp = matches;
806 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
809 tmp_matches[0].rm_so = 0;
810 tmp_matches[0].rm_eo = e->e_nname.bv_len;
815 case ACL_STYLE_SUBTREE:
816 case ACL_STYLE_CHILDREN:
817 tmp_matches[0].rm_so = 0;
818 tmp_matches[0].rm_eo = e->e_nname.bv_len;
819 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
820 tmp_matches[1].rm_eo = e->e_nname.bv_len;
834 if ( acl_string_expand( &bv, &bdn->a_pat,
836 tmp_nmatch, tmp_matchesp ) )
841 if ( dnNormalize(0, NULL, NULL, &bv,
842 &pat, op->o_tmpmemctx )
845 /* did not expand to a valid dn */
854 odnlen = opndn->bv_len;
855 if ( odnlen < patlen ) {
856 goto dn_match_cleanup;
860 if ( bdn->a_style == ACL_STYLE_BASE ) {
861 /* base dn -- entire object DN must match */
862 if ( odnlen != patlen ) {
863 goto dn_match_cleanup;
866 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
867 ber_len_t rdnlen = 0;
869 if ( odnlen <= patlen ) {
870 goto dn_match_cleanup;
873 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
874 goto dn_match_cleanup;
877 rdnlen = dn_rdnlen( NULL, opndn );
878 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
879 goto dn_match_cleanup;
882 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
883 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
884 goto dn_match_cleanup;
887 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
888 if ( odnlen <= patlen ) {
889 goto dn_match_cleanup;
892 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
893 goto dn_match_cleanup;
896 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
897 int level = bdn->a_level;
900 if ( odnlen <= patlen ) {
901 goto dn_match_cleanup;
904 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
906 goto dn_match_cleanup;
910 for ( ; level > 0; level-- ) {
911 if ( BER_BVISEMPTY( &ndn ) ) {
912 goto dn_match_cleanup;
914 dnParent( &ndn, &ndn );
915 if ( ndn.bv_len < patlen ) {
916 goto dn_match_cleanup;
920 if ( ndn.bv_len != patlen ) {
921 goto dn_match_cleanup;
925 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
928 if ( pat.bv_val != bdn->a_pat.bv_val ) {
929 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
950 AccessControlState *state,
952 struct berval *opndn )
958 const char *attr = bdn->a_at->ad_cname.bv_val;
960 assert( attr != NULL );
962 if ( BER_BVISEMPTY( opndn ) ) {
966 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
969 /* see if asker is listed in dnattr */
970 for ( at = attrs_find( e->e_attrs, bdn->a_at );
972 at = attrs_find( at->a_next, bdn->a_at ) )
974 if ( value_find_ex( bdn->a_at,
975 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
976 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
978 &bv, op->o_tmpmemctx ) == 0 )
987 /* have a dnattr match. if this is a self clause then
988 * the target must also match the op dn.
991 /* check if the target is an attribute. */
992 if ( val == NULL ) return 1;
994 /* target is attribute, check if the attribute value
997 rc = value_match( &match, bdn->a_at,
998 bdn->a_at->ad_type->sat_equality, 0,
1000 /* on match error or no match, fail the ACL clause */
1001 if ( rc != LDAP_SUCCESS || match != 0 )
1006 /* no dnattr match, check if this is a self clause */
1007 if ( ! bdn->a_self )
1010 ACL_RECORD_VALUE_STATE;
1012 /* this is a self clause, check if the target is an
1018 /* target is attribute, check if the attribute value
1021 rc = value_match( &match, bdn->a_at,
1022 bdn->a_at->ad_type->sat_equality, 0,
1025 /* on match error or no match, fail the ACL clause */
1026 if ( rc != LDAP_SUCCESS || match != 0 )
1035 * slap_acl_mask - modifies mask based upon the given acl and the
1036 * requested access to entry e, attribute attr, value val. if val
1037 * is null, access to the whole attribute is assumed (all values).
1039 * returns 0 access NOT allowed
1043 static slap_control_t
1049 AttributeDescription *desc,
1052 regmatch_t *matches,
1054 AccessControlState *state )
1059 char accessmaskbuf[ACCESSMASK_MAXLEN];
1062 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1064 assert( a != NULL );
1065 assert( mask != NULL );
1066 assert( desc != NULL );
1068 attr = desc->ad_cname.bv_val;
1070 assert( attr != NULL );
1072 Debug( LDAP_DEBUG_ACL,
1073 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1076 Debug( LDAP_DEBUG_ACL,
1077 "=> acl_mask: to %s by \"%s\", (%s) \n",
1078 val ? "value" : "all values",
1079 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1080 accessmask2str( *mask, accessmaskbuf, 1 ) );
1086 for ( ; b != NULL; b = b->a_next, i++ ) {
1087 slap_mask_t oldmask, modmask;
1089 ACL_INVALIDATE( modmask );
1091 /* AND <who> clauses */
1092 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1093 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1094 b->a_dn_pat.bv_val, 0, 0);
1096 * if access applies to the entry itself, and the
1097 * user is bound as somebody in the same namespace as
1098 * the entry, OR the given dn matches the dn pattern
1101 * NOTE: styles "anonymous", "users" and "self"
1102 * have been moved to enum slap_style_t, whose
1103 * value is set in a_dn_style; however, the string
1104 * is maintained in a_dn_pat.
1107 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1108 &b->a_dn, &op->o_ndn ) )
1114 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1117 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1118 b->a_realdn_pat.bv_val, 0, 0);
1120 * if access applies to the entry itself, and the
1121 * user is bound as somebody in the same namespace as
1122 * the entry, OR the given dn matches the dn pattern
1125 * NOTE: styles "anonymous", "users" and "self"
1126 * have been moved to enum slap_style_t, whose
1127 * value is set in a_dn_style; however, the string
1128 * is maintained in a_dn_pat.
1131 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1133 ndn = op->o_conn->c_ndn;
1138 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1139 &b->a_realdn, &ndn ) )
1145 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1146 if ( ! op->o_conn->c_listener ) {
1149 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1150 b->a_sockurl_pat.bv_val, 0, 0 );
1152 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1153 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1154 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1155 e->e_ndn, nmatch, matches ) )
1160 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1162 char buf[ACL_BUF_SIZE];
1164 bv.bv_len = sizeof( buf ) - 1;
1166 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1167 e->e_ndn, nmatch, matches ) )
1172 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1178 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1186 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1187 if ( !op->o_conn->c_peer_domain.bv_val ) {
1190 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1191 b->a_domain_pat.bv_val, 0, 0 );
1192 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1193 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1194 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1195 e->e_ndn, nmatch, matches ) )
1200 char buf[ACL_BUF_SIZE];
1202 struct berval cmp = op->o_conn->c_peer_domain;
1203 struct berval pat = b->a_domain_pat;
1205 if ( b->a_domain_expand ) {
1208 bv.bv_len = sizeof(buf) - 1;
1211 if ( acl_string_expand(&bv, &b->a_domain_pat,
1212 e->e_ndn, nmatch, matches) )
1219 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1220 int offset = cmp.bv_len - pat.bv_len;
1225 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1229 /* trim the domain */
1230 cmp.bv_val = &cmp.bv_val[ offset ];
1231 cmp.bv_len -= offset;
1234 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1241 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1242 if ( !op->o_conn->c_peer_name.bv_val ) {
1245 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1246 b->a_peername_pat.bv_val, 0, 0 );
1247 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1248 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1249 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1250 e->e_ndn, nmatch, matches ) )
1256 /* try exact match */
1257 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1258 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1262 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1264 char buf[ACL_BUF_SIZE];
1266 bv.bv_len = sizeof( buf ) - 1;
1268 if ( acl_string_expand( &bv, &b->a_peername_pat,
1269 e->e_ndn, nmatch, matches ) )
1274 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1278 /* extract IP and try exact match */
1279 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1281 char buf[] = "255.255.255.255";
1284 int port_number = -1;
1286 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1287 acl_bv_ip_eq.bv_val,
1288 acl_bv_ip_eq.bv_len ) != 0 )
1291 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1292 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1294 port = strrchr( ip.bv_val, ':' );
1296 ip.bv_len = port - ip.bv_val;
1298 if ( lutil_atoi( &port_number, port ) != 0 )
1302 /* the port check can be anticipated here */
1303 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1306 /* address longer than expected? */
1307 if ( ip.bv_len >= sizeof(buf) )
1310 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1311 buf[ ip.bv_len ] = '\0';
1313 addr = inet_addr( buf );
1315 /* unable to convert? */
1316 if ( addr == (unsigned long)(-1) )
1319 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1322 #ifdef LDAP_PF_INET6
1323 /* extract IPv6 and try exact match */
1324 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1326 char buf[] = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
1328 struct in6_addr addr;
1329 int port_number = -1, i;
1331 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1332 acl_bv_ipv6_eq.bv_val,
1333 acl_bv_ipv6_eq.bv_len ) != 0 )
1336 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1337 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1339 port = strrchr( ip.bv_val, ']' );
1341 ip.bv_len = port - ip.bv_val;
1343 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1347 /* the port check can be anticipated here */
1348 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1351 /* address longer than expected? */
1352 if ( ip.bv_len >= sizeof(buf) )
1355 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1356 buf[ ip.bv_len ] = '\0';
1358 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1362 for ( i = 0; i < 4; i++ ) {
1363 if ( ( addr.s6_addr32[i] & b->a_peername_mask6.s6_addr32[i] )
1364 != b->a_peername_addr6.s6_addr32[i] )
1372 #endif /* LDAP_PF_INET6 */
1374 #ifdef LDAP_PF_LOCAL
1375 /* extract path and try exact match */
1376 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1379 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1380 acl_bv_path_eq.bv_val,
1381 acl_bv_path_eq.bv_len ) != 0 )
1384 path.bv_val = op->o_conn->c_peer_name.bv_val
1385 + acl_bv_path_eq.bv_len;
1386 path.bv_len = op->o_conn->c_peer_name.bv_len
1387 - acl_bv_path_eq.bv_len;
1389 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1392 #endif /* LDAP_PF_LOCAL */
1394 /* exact match (very unlikely...) */
1395 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1402 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1403 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1406 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1407 b->a_sockname_pat.bv_val, 0, 0 );
1408 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1409 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1410 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1411 e->e_ndn, nmatch, matches ) )
1416 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1418 char buf[ACL_BUF_SIZE];
1420 bv.bv_len = sizeof( buf ) - 1;
1422 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1423 e->e_ndn, nmatch, matches ) )
1428 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1433 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1440 if ( b->a_dn_at != NULL ) {
1441 if ( acl_mask_dnattr( op, e, val, a, b, i,
1442 matches, count, state,
1443 &b->a_dn, &op->o_ndn ) )
1449 if ( b->a_realdn_at != NULL ) {
1452 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1454 ndn = op->o_conn->c_ndn;
1459 if ( acl_mask_dnattr( op, e, val, a, b, i,
1460 matches, count, state,
1461 &b->a_realdn, &ndn ) )
1467 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1469 struct berval ndn = BER_BVNULL;
1472 if ( op->o_ndn.bv_len == 0 ) {
1476 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1477 b->a_group_pat.bv_val, 0, 0 );
1479 /* b->a_group is an unexpanded entry name, expanded it should be an
1480 * entry with objectclass group* and we test to see if odn is one of
1481 * the values in the attribute group
1483 /* see if asker is listed in dnattr */
1484 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1485 char buf[ACL_BUF_SIZE];
1487 regmatch_t tmp_matches[2],
1488 *tmp_matchesp = tmp_matches;
1490 bv.bv_len = sizeof(buf) - 1;
1495 switch ( a->acl_dn_style ) {
1496 case ACL_STYLE_REGEX:
1497 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1498 tmp_matchesp = matches;
1499 tmp_nmatch = nmatch;
1503 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1504 case ACL_STYLE_BASE:
1505 tmp_matches[0].rm_so = 0;
1506 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1511 case ACL_STYLE_SUBTREE:
1512 case ACL_STYLE_CHILDREN:
1513 tmp_matches[0].rm_so = 0;
1514 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1515 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1516 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1530 if ( acl_string_expand( &bv, &b->a_group_pat,
1532 tmp_nmatch, tmp_matchesp ) )
1537 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1538 op->o_tmpmemctx ) != LDAP_SUCCESS )
1540 /* did not expand to a valid dn */
1547 bv = b->a_group_pat;
1550 rc = backend_group( op, e, &bv, &op->o_ndn,
1551 b->a_group_oc, b->a_group_at );
1554 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1562 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1564 char buf[ACL_BUF_SIZE];
1566 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1567 b->a_set_pat.bv_val, 0, 0 );
1569 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1571 regmatch_t tmp_matches[2],
1572 *tmp_matchesp = tmp_matches;
1575 bv.bv_len = sizeof( buf ) - 1;
1580 switch ( a->acl_dn_style ) {
1581 case ACL_STYLE_REGEX:
1582 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1583 tmp_matchesp = matches;
1584 tmp_nmatch = nmatch;
1588 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1589 case ACL_STYLE_BASE:
1590 tmp_matches[0].rm_so = 0;
1591 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1596 case ACL_STYLE_SUBTREE:
1597 case ACL_STYLE_CHILDREN:
1598 tmp_matches[0].rm_so = 0;
1599 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1600 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1601 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1615 if ( acl_string_expand( &bv, &b->a_set_pat,
1617 tmp_nmatch, tmp_matchesp ) )
1626 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1631 if ( b->a_authz.sai_ssf ) {
1632 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1633 b->a_authz.sai_ssf, op->o_ssf, 0 );
1634 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1639 if ( b->a_authz.sai_transport_ssf ) {
1640 Debug( LDAP_DEBUG_ACL,
1641 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1642 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1643 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1648 if ( b->a_authz.sai_tls_ssf ) {
1649 Debug( LDAP_DEBUG_ACL,
1650 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1651 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1652 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1657 if ( b->a_authz.sai_sasl_ssf ) {
1658 Debug( LDAP_DEBUG_ACL,
1659 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1660 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1661 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1666 /* check for the "self" modifier in the <access> field */
1667 if ( b->a_dn.a_self ) {
1671 ACL_RECORD_VALUE_STATE;
1673 /* must have DN syntax */
1674 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1675 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1677 /* check if the target is an attribute. */
1678 if ( val == NULL ) continue;
1680 /* a DN must be present */
1681 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1685 /* target is attribute, check if the attribute value
1688 rc = value_match( &match, desc,
1689 desc->ad_type->sat_equality, 0,
1690 val, &op->o_ndn, &dummy );
1691 /* on match error or no match, fail the ACL clause */
1692 if ( rc != LDAP_SUCCESS || match != 0 )
1697 if ( b->a_dynacl ) {
1699 slap_access_t tgrant, tdeny;
1701 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1704 /* this case works different from the others above.
1705 * since dynamic ACL's themselves give permissions, we need
1706 * to first check b->a_access_mask, the ACL's access level.
1708 /* first check if the right being requested
1709 * is allowed by the ACL clause.
1711 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1715 /* start out with nothing granted, nothing denied */
1716 ACL_INVALIDATE(tgrant);
1717 ACL_INVALIDATE(tdeny);
1719 for ( da = b->a_dynacl; da; da = da->da_next ) {
1720 slap_access_t grant,
1723 ACL_INVALIDATE(grant);
1724 ACL_INVALIDATE(deny);
1726 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1727 da->da_name, 0, 0 );
1729 (void)da->da_mask( da->da_private, op, e, desc,
1730 val, nmatch, matches, &grant, &deny );
1736 /* remove anything that the ACL clause does not allow */
1737 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1738 tdeny &= ACL_PRIV_MASK;
1740 /* see if we have anything to contribute */
1741 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1745 /* this could be improved by changing slap_acl_mask so that it can deal with
1746 * by clauses that return grant/deny pairs. Right now, it does either
1747 * additive or subtractive rights, but not both at the same time. So,
1748 * we need to combine the grant/deny pair into a single rights mask in
1749 * a smart way: if either grant or deny is "empty", then we use the
1750 * opposite as is, otherwise we remove any denied rights from the grant
1751 * rights mask and construct an additive mask.
1753 if (ACL_IS_INVALID(tdeny)) {
1754 modmask = tgrant | ACL_PRIV_ADDITIVE;
1756 } else if (ACL_IS_INVALID(tgrant)) {
1757 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1760 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1764 #endif /* SLAP_DYNACL */
1766 modmask = b->a_access_mask;
1769 Debug( LDAP_DEBUG_ACL,
1770 "<= acl_mask: [%d] applying %s (%s)\n",
1771 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1772 b->a_type == ACL_CONTINUE
1774 : b->a_type == ACL_BREAK
1780 if( ACL_IS_ADDITIVE(modmask) ) {
1782 ACL_PRIV_SET( *mask, modmask );
1785 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1787 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1788 /* substract privs */
1789 ACL_PRIV_CLR( *mask, modmask );
1792 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1801 Debug( LDAP_DEBUG_ACL,
1802 "<= acl_mask: [%d] mask: %s\n",
1803 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1805 if( b->a_type == ACL_CONTINUE ) {
1808 } else if ( b->a_type == ACL_BREAK ) {
1816 /* implicit "by * none" clause */
1819 Debug( LDAP_DEBUG_ACL,
1820 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1821 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1826 * acl_check_modlist - check access control on the given entry to see if
1827 * it allows the given modifications by the user associated with op.
1828 * returns 1 if mods allowed ok
1829 * 0 mods not allowed
1836 Modifications *mlist )
1839 AccessControlState state = ACL_STATE_INIT;
1842 int ret = 1; /* default is access allowed */
1846 be = LDAP_STAILQ_FIRST(&backendDB);
1850 assert( be != NULL );
1852 /* short circuit root database access */
1853 if ( be_isroot( op ) ) {
1854 Debug( LDAP_DEBUG_ACL,
1855 "<= acl_access_allowed: granted to database root\n",
1860 /* use backend default access if no backend acls */
1861 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1862 Debug( LDAP_DEBUG_ACL,
1863 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1864 access2str( ACL_WRITE ),
1865 op->o_bd->be_dfltaccess >= ACL_WRITE
1866 ? "granted" : "denied",
1868 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1872 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1874 * Internal mods are ignored by ACL_WRITE checking
1876 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1877 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1878 " modify access granted\n",
1879 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1884 * no-user-modification operational attributes are ignored
1885 * by ACL_WRITE checking as any found here are not provided
1888 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1889 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1891 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1892 " modify access granted\n",
1893 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1897 switch ( mlist->sml_op ) {
1898 case LDAP_MOD_REPLACE:
1899 case LDAP_MOD_INCREMENT:
1901 * We must check both permission to delete the whole
1902 * attribute and permission to add the specific attributes.
1903 * This prevents abuse from selfwriters.
1905 if ( ! access_allowed( op, e,
1906 mlist->sml_desc, NULL,
1907 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1914 if ( mlist->sml_values == NULL ) break;
1916 /* fall thru to check value to add */
1919 assert( mlist->sml_values != NULL );
1921 for ( bv = mlist->sml_nvalues
1922 ? mlist->sml_nvalues : mlist->sml_values;
1923 bv->bv_val != NULL; bv++ )
1925 if ( ! access_allowed( op, e,
1926 mlist->sml_desc, bv,
1927 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1936 case LDAP_MOD_DELETE:
1937 if ( mlist->sml_values == NULL ) {
1938 if ( ! access_allowed( op, e,
1939 mlist->sml_desc, NULL,
1940 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1948 for ( bv = mlist->sml_nvalues
1949 ? mlist->sml_nvalues : mlist->sml_values;
1950 bv->bv_val != NULL; bv++ )
1952 if ( ! access_allowed( op, e,
1953 mlist->sml_desc, bv,
1954 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1963 case SLAP_MOD_SOFTADD:
1964 /* allow adding attribute via modrdn thru */
1976 if (be_null) op->o_bd = NULL;
1982 struct berval *list,
1995 while ( len >= 0 && --ix >= 0 ) {
1996 while ( --len >= 0 && *p++ != sep )
1999 while ( len >= 0 && *p == ' ' ) {
2012 while ( --len >= 0 && *p != sep ) {
2016 while ( bv->bv_len > 0 && *--p == ' ' ) {
2023 typedef struct acl_set_gather_t {
2029 acl_set_cb_gather( Operation *op, SlapReply *rs )
2031 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2033 if ( rs->sr_type == REP_SEARCH ) {
2034 BerValue bvals[ 2 ];
2035 BerVarray bvalsp = NULL;
2038 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2039 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2041 if ( desc == slap_schema.si_ad_entryDN ) {
2043 bvals[ 0 ] = rs->sr_entry->e_nname;
2044 BER_BVZERO( &bvals[ 1 ] );
2049 a = attr_find( rs->sr_entry->e_attrs, desc );
2053 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2056 bvalsp = a->a_nvals;
2062 p->bvals = slap_set_join( p->cookie, p->bvals,
2063 ( '|' | SLAP_SET_RREF ), bvalsp );
2067 assert( rs->sr_type == REP_RESULT );
2074 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2076 AclSetCookie *cp = (AclSetCookie *)cookie;
2078 LDAPURLDesc *ludp = NULL;
2079 Operation op2 = { 0 };
2080 SlapReply rs = {REP_RESULT};
2081 AttributeName anlist[ 2 ], *anlistp = NULL;
2083 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2084 acl_set_gather_t p = { 0 };
2085 const char *text = NULL;
2086 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2088 /* this routine needs to return the bervals instead of
2089 * plain strings, since syntax is not known. It should
2090 * also return the syntax or some "comparison cookie".
2092 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2093 return acl_set_gather2( cookie, name, desc );
2096 rc = ldap_url_parse( name->bv_val, &ludp );
2097 if ( rc != LDAP_URL_SUCCESS ) {
2098 rc = LDAP_PROTOCOL_ERROR;
2102 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2104 /* host part must be empty */
2105 /* extensions parts must be empty */
2106 rc = LDAP_PROTOCOL_ERROR;
2110 /* Grab the searchbase and see if an appropriate database can be found */
2111 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2112 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2113 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2114 BER_BVZERO( &op2.o_req_dn );
2115 if ( rc != LDAP_SUCCESS ) {
2119 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2120 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2121 rc = LDAP_NO_SUCH_OBJECT;
2125 /* Grab the filter */
2126 if ( ludp->lud_filter ) {
2127 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2128 cp->asc_op->o_tmpmemctx );
2131 op2.ors_filterstr = defaultFilter_bv;
2134 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2135 if ( op2.ors_filter == NULL ) {
2136 rc = LDAP_PROTOCOL_ERROR;
2140 /* Grab the scope */
2141 op2.ors_scope = ludp->lud_scope;
2143 /* Grap the attributes */
2144 if ( ludp->lud_attrs ) {
2145 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2148 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2149 cp->asc_op->o_tmpmemctx );
2151 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2152 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2153 anlistp[ nattrs ].an_desc = NULL;
2154 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2155 &anlistp[ nattrs ].an_desc, &text );
2156 if ( rc != LDAP_SUCCESS ) {
2165 anlistp[ nattrs ].an_name = desc->ad_cname;
2166 anlistp[ nattrs ].an_desc = desc;
2168 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2172 op2.o_hdr = cp->asc_op->o_hdr;
2173 op2.o_tag = LDAP_REQ_SEARCH;
2174 op2.o_ndn = op2.o_bd->be_rootndn;
2175 op2.o_callback = &cb;
2176 slap_op_time( &op2.o_time, &op2.o_tincr );
2177 op2.o_do_not_cache = 1;
2178 op2.o_is_auth_check = 0;
2179 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2180 op2.ors_slimit = SLAP_NO_LIMIT;
2181 op2.ors_tlimit = SLAP_NO_LIMIT;
2182 op2.ors_attrs = anlistp;
2183 op2.ors_attrsonly = 0;
2184 op2.o_private = cp->asc_op->o_private;
2188 rc = op2.o_bd->be_search( &op2, &rs );
2194 if ( op2.ors_filter ) {
2195 filter_free_x( cp->asc_op, op2.ors_filter );
2197 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2198 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2200 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2201 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2204 ldap_free_urldesc( ludp );
2206 if ( anlistp && anlistp != anlist ) {
2207 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2214 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2216 AclSetCookie *cp = (AclSetCookie *)cookie;
2217 BerVarray bvals = NULL;
2221 /* this routine needs to return the bervals instead of
2222 * plain strings, since syntax is not known. It should
2223 * also return the syntax or some "comparison cookie".
2225 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2226 if ( rc == LDAP_SUCCESS ) {
2227 if ( desc == slap_schema.si_ad_entryDN ) {
2228 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2229 cp->asc_op->o_tmpmemctx );
2231 BER_BVZERO( &bvals[ 1 ] );
2235 backend_attribute( cp->asc_op,
2236 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2239 if ( !BER_BVISNULL( &ndn ) ) {
2240 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2249 struct berval *subj,
2252 struct berval *default_set_attribute )
2254 struct berval set = BER_BVNULL;
2256 AclSetCookie cookie;
2258 if ( default_set_attribute == NULL ) {
2259 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2262 struct berval subjdn, ndn = BER_BVNULL;
2263 struct berval setat;
2264 BerVarray bvals = NULL;
2266 AttributeDescription *desc = NULL;
2268 /* format of string is "entry/setAttrName" */
2269 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2273 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2274 setat = *default_set_attribute;
2278 * NOTE: dnNormalize honors the ber_len field
2279 * as the length of the dn to be normalized
2281 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2282 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2284 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2285 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2289 BER_BVZERO( &bvals[0] );
2290 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2292 bvals[0].bv_val = bvals[i-1].bv_val;
2293 BER_BVZERO( &bvals[i-1] );
2295 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2296 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2301 if ( !BER_BVISNULL( &set ) ) {
2304 rc = ( slap_set_filter(
2306 (SetCookie *)&cookie, &set,
2307 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2308 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2317 * dynamic ACL infrastructure
2319 static slap_dynacl_t *da_list = NULL;
2322 slap_dynacl_register( slap_dynacl_t *da )
2326 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2327 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2332 if ( tmp != NULL ) {
2336 if ( da->da_mask == NULL ) {
2340 da->da_private = NULL;
2341 da->da_next = da_list;
2347 static slap_dynacl_t *
2348 slap_dynacl_next( slap_dynacl_t *da )
2357 slap_dynacl_get( const char *name )
2361 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2362 if ( strcasecmp( da->da_name, name ) == 0 ) {
2369 #endif /* SLAP_DYNACL */
2372 * statically built-in dynamic ACL initialization
2374 static int (*acl_init_func[])( void ) = {
2376 /* TODO: remove when ACI will only be dynamic */
2377 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2379 #endif /* SLAPD_ACI_ENABLED */
2380 #endif /* SLAP_DYNACL */
2390 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2391 rc = (*(acl_init_func[ i ]))();
2406 regmatch_t *matches)
2414 bv->bv_val[0] = '\0';
2415 bv->bv_len--; /* leave space for lone $ */
2418 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2419 sp < pat->bv_val + pat->bv_len ; sp++ )
2421 /* did we previously see a $ */
2423 if ( flag == 1 && *sp == '$' ) {
2428 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2431 } else if ( *sp >= '0' && *sp <= '9' ) {
2439 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2440 if ( *sp >= '0' && *sp <= '9' ) {
2441 n = 10*n + ( *sp - '0' );
2445 if ( *sp != /*'{'*/ '}' ) {
2451 if ( n >= nmatch ) {
2457 i = matches[n].rm_so;
2458 l = matches[n].rm_eo;
2459 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2477 /* must have ended with a single $ */
2485 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2486 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2493 struct berval *pat, /* pattern to expand and match against */
2494 char *str, /* string to match against pattern */
2495 char *buf, /* buffer with $N expansion variables */
2496 int nmatch, /* size of the matches array */
2497 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2501 char newbuf[ACL_BUF_SIZE];
2505 bv.bv_len = sizeof( newbuf ) - 1;
2512 acl_string_expand( &bv, pat, buf, nmatch, matches );
2513 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2515 char error[ACL_BUF_SIZE];
2516 regerror( rc, &re, error, sizeof( error ) );
2518 Debug( LDAP_DEBUG_TRACE,
2519 "compile( \"%s\", \"%s\") failed %s\n",
2520 pat->bv_val, str, error );
2524 rc = regexec( &re, str, 0, NULL, 0 );
2527 Debug( LDAP_DEBUG_TRACE,
2528 "=> regex_matches: string: %s\n", str, 0, 0 );
2529 Debug( LDAP_DEBUG_TRACE,
2530 "=> regex_matches: rc: %d %s\n",
2531 rc, !rc ? "matches" : "no matches", 0 );