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-2007 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;
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 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1364 #endif /* LDAP_PF_INET6 */
1366 #ifdef LDAP_PF_LOCAL
1367 /* extract path and try exact match */
1368 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1371 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1372 acl_bv_path_eq.bv_val,
1373 acl_bv_path_eq.bv_len ) != 0 )
1376 path.bv_val = op->o_conn->c_peer_name.bv_val
1377 + acl_bv_path_eq.bv_len;
1378 path.bv_len = op->o_conn->c_peer_name.bv_len
1379 - acl_bv_path_eq.bv_len;
1381 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1384 #endif /* LDAP_PF_LOCAL */
1386 /* exact match (very unlikely...) */
1387 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1394 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1395 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1398 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1399 b->a_sockname_pat.bv_val, 0, 0 );
1400 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1401 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1402 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1403 e->e_ndn, nmatch, matches ) )
1408 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1410 char buf[ACL_BUF_SIZE];
1412 bv.bv_len = sizeof( buf ) - 1;
1414 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1415 e->e_ndn, nmatch, matches ) )
1420 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1425 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1432 if ( b->a_dn_at != NULL ) {
1433 if ( acl_mask_dnattr( op, e, val, a, b, i,
1434 matches, count, state,
1435 &b->a_dn, &op->o_ndn ) )
1441 if ( b->a_realdn_at != NULL ) {
1444 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1446 ndn = op->o_conn->c_ndn;
1451 if ( acl_mask_dnattr( op, e, val, a, b, i,
1452 matches, count, state,
1453 &b->a_realdn, &ndn ) )
1459 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1461 struct berval ndn = BER_BVNULL;
1464 if ( op->o_ndn.bv_len == 0 ) {
1468 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1469 b->a_group_pat.bv_val, 0, 0 );
1471 /* b->a_group is an unexpanded entry name, expanded it should be an
1472 * entry with objectclass group* and we test to see if odn is one of
1473 * the values in the attribute group
1475 /* see if asker is listed in dnattr */
1476 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1477 char buf[ACL_BUF_SIZE];
1479 regmatch_t tmp_matches[2],
1480 *tmp_matchesp = tmp_matches;
1482 bv.bv_len = sizeof(buf) - 1;
1487 switch ( a->acl_dn_style ) {
1488 case ACL_STYLE_REGEX:
1489 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1490 tmp_matchesp = matches;
1491 tmp_nmatch = nmatch;
1495 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1496 case ACL_STYLE_BASE:
1497 tmp_matches[0].rm_so = 0;
1498 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1503 case ACL_STYLE_SUBTREE:
1504 case ACL_STYLE_CHILDREN:
1505 tmp_matches[0].rm_so = 0;
1506 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1507 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1508 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1522 if ( acl_string_expand( &bv, &b->a_group_pat,
1524 tmp_nmatch, tmp_matchesp ) )
1529 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1530 op->o_tmpmemctx ) != LDAP_SUCCESS )
1532 /* did not expand to a valid dn */
1539 bv = b->a_group_pat;
1542 rc = backend_group( op, e, &bv, &op->o_ndn,
1543 b->a_group_oc, b->a_group_at );
1546 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1554 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1556 char buf[ACL_BUF_SIZE];
1558 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1559 b->a_set_pat.bv_val, 0, 0 );
1561 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1563 regmatch_t tmp_matches[2],
1564 *tmp_matchesp = tmp_matches;
1567 bv.bv_len = sizeof( buf ) - 1;
1572 switch ( a->acl_dn_style ) {
1573 case ACL_STYLE_REGEX:
1574 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1575 tmp_matchesp = matches;
1576 tmp_nmatch = nmatch;
1580 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1581 case ACL_STYLE_BASE:
1582 tmp_matches[0].rm_so = 0;
1583 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1588 case ACL_STYLE_SUBTREE:
1589 case ACL_STYLE_CHILDREN:
1590 tmp_matches[0].rm_so = 0;
1591 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1592 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1593 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1607 if ( acl_string_expand( &bv, &b->a_set_pat,
1609 tmp_nmatch, tmp_matchesp ) )
1618 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1623 if ( b->a_authz.sai_ssf ) {
1624 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1625 b->a_authz.sai_ssf, op->o_ssf, 0 );
1626 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1631 if ( b->a_authz.sai_transport_ssf ) {
1632 Debug( LDAP_DEBUG_ACL,
1633 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1634 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1635 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1640 if ( b->a_authz.sai_tls_ssf ) {
1641 Debug( LDAP_DEBUG_ACL,
1642 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1643 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1644 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1649 if ( b->a_authz.sai_sasl_ssf ) {
1650 Debug( LDAP_DEBUG_ACL,
1651 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1652 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1653 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1658 /* check for the "self" modifier in the <access> field */
1659 if ( b->a_dn.a_self ) {
1663 ACL_RECORD_VALUE_STATE;
1665 /* must have DN syntax */
1666 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1667 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1669 /* check if the target is an attribute. */
1670 if ( val == NULL ) continue;
1672 /* a DN must be present */
1673 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1677 /* target is attribute, check if the attribute value
1680 rc = value_match( &match, desc,
1681 desc->ad_type->sat_equality, 0,
1682 val, &op->o_ndn, &dummy );
1683 /* on match error or no match, fail the ACL clause */
1684 if ( rc != LDAP_SUCCESS || match != 0 )
1689 if ( b->a_dynacl ) {
1691 slap_access_t tgrant, tdeny;
1693 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1696 /* this case works different from the others above.
1697 * since dynamic ACL's themselves give permissions, we need
1698 * to first check b->a_access_mask, the ACL's access level.
1700 /* first check if the right being requested
1701 * is allowed by the ACL clause.
1703 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1707 /* start out with nothing granted, nothing denied */
1708 ACL_INVALIDATE(tgrant);
1709 ACL_INVALIDATE(tdeny);
1711 for ( da = b->a_dynacl; da; da = da->da_next ) {
1712 slap_access_t grant,
1715 ACL_INVALIDATE(grant);
1716 ACL_INVALIDATE(deny);
1718 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1719 da->da_name, 0, 0 );
1721 (void)da->da_mask( da->da_private, op, e, desc,
1722 val, nmatch, matches, &grant, &deny );
1728 /* remove anything that the ACL clause does not allow */
1729 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1730 tdeny &= ACL_PRIV_MASK;
1732 /* see if we have anything to contribute */
1733 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1737 /* this could be improved by changing slap_acl_mask so that it can deal with
1738 * by clauses that return grant/deny pairs. Right now, it does either
1739 * additive or subtractive rights, but not both at the same time. So,
1740 * we need to combine the grant/deny pair into a single rights mask in
1741 * a smart way: if either grant or deny is "empty", then we use the
1742 * opposite as is, otherwise we remove any denied rights from the grant
1743 * rights mask and construct an additive mask.
1745 if (ACL_IS_INVALID(tdeny)) {
1746 modmask = tgrant | ACL_PRIV_ADDITIVE;
1748 } else if (ACL_IS_INVALID(tgrant)) {
1749 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1752 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1756 #endif /* SLAP_DYNACL */
1758 modmask = b->a_access_mask;
1761 Debug( LDAP_DEBUG_ACL,
1762 "<= acl_mask: [%d] applying %s (%s)\n",
1763 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1764 b->a_type == ACL_CONTINUE
1766 : b->a_type == ACL_BREAK
1772 if( ACL_IS_ADDITIVE(modmask) ) {
1774 ACL_PRIV_SET( *mask, modmask );
1777 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1779 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1780 /* substract privs */
1781 ACL_PRIV_CLR( *mask, modmask );
1784 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1793 Debug( LDAP_DEBUG_ACL,
1794 "<= acl_mask: [%d] mask: %s\n",
1795 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1797 if( b->a_type == ACL_CONTINUE ) {
1800 } else if ( b->a_type == ACL_BREAK ) {
1808 /* implicit "by * none" clause */
1811 Debug( LDAP_DEBUG_ACL,
1812 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1813 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1818 * acl_check_modlist - check access control on the given entry to see if
1819 * it allows the given modifications by the user associated with op.
1820 * returns 1 if mods allowed ok
1821 * 0 mods not allowed
1828 Modifications *mlist )
1831 AccessControlState state = ACL_STATE_INIT;
1834 int ret = 1; /* default is access allowed */
1838 be = LDAP_STAILQ_FIRST(&backendDB);
1842 assert( be != NULL );
1844 /* short circuit root database access */
1845 if ( be_isroot( op ) ) {
1846 Debug( LDAP_DEBUG_ACL,
1847 "<= acl_access_allowed: granted to database root\n",
1852 /* use backend default access if no backend acls */
1853 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1854 Debug( LDAP_DEBUG_ACL,
1855 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1856 access2str( ACL_WRITE ),
1857 op->o_bd->be_dfltaccess >= ACL_WRITE
1858 ? "granted" : "denied",
1860 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1864 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1866 * Internal mods are ignored by ACL_WRITE checking
1868 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1869 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1870 " modify access granted\n",
1871 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1876 * no-user-modification operational attributes are ignored
1877 * by ACL_WRITE checking as any found here are not provided
1880 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1881 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1883 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1884 " modify access granted\n",
1885 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1889 switch ( mlist->sml_op ) {
1890 case LDAP_MOD_REPLACE:
1891 case LDAP_MOD_INCREMENT:
1893 * We must check both permission to delete the whole
1894 * attribute and permission to add the specific attributes.
1895 * This prevents abuse from selfwriters.
1897 if ( ! access_allowed( op, e,
1898 mlist->sml_desc, NULL,
1899 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1906 if ( mlist->sml_values == NULL ) break;
1908 /* fall thru to check value to add */
1911 assert( mlist->sml_values != NULL );
1913 for ( bv = mlist->sml_nvalues
1914 ? mlist->sml_nvalues : mlist->sml_values;
1915 bv->bv_val != NULL; bv++ )
1917 if ( ! access_allowed( op, e,
1918 mlist->sml_desc, bv,
1919 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1928 case LDAP_MOD_DELETE:
1929 if ( mlist->sml_values == NULL ) {
1930 if ( ! access_allowed( op, e,
1931 mlist->sml_desc, NULL,
1932 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1940 for ( bv = mlist->sml_nvalues
1941 ? mlist->sml_nvalues : mlist->sml_values;
1942 bv->bv_val != NULL; bv++ )
1944 if ( ! access_allowed( op, e,
1945 mlist->sml_desc, bv,
1946 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1955 case SLAP_MOD_SOFTADD:
1956 /* allow adding attribute via modrdn thru */
1968 if (be_null) op->o_bd = NULL;
1974 struct berval *list,
1987 while ( len >= 0 && --ix >= 0 ) {
1988 while ( --len >= 0 && *p++ != sep )
1991 while ( len >= 0 && *p == ' ' ) {
2004 while ( --len >= 0 && *p != sep ) {
2008 while ( bv->bv_len > 0 && *--p == ' ' ) {
2015 typedef struct acl_set_gather_t {
2021 acl_set_cb_gather( Operation *op, SlapReply *rs )
2023 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2025 if ( rs->sr_type == REP_SEARCH ) {
2026 BerValue bvals[ 2 ];
2027 BerVarray bvalsp = NULL;
2030 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2031 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2033 if ( desc == slap_schema.si_ad_entryDN ) {
2035 bvals[ 0 ] = rs->sr_entry->e_nname;
2036 BER_BVZERO( &bvals[ 1 ] );
2041 a = attr_find( rs->sr_entry->e_attrs, desc );
2045 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2048 bvalsp = a->a_nvals;
2054 p->bvals = slap_set_join( p->cookie, p->bvals,
2055 ( '|' | SLAP_SET_RREF ), bvalsp );
2059 assert( rs->sr_type == REP_RESULT );
2066 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2068 AclSetCookie *cp = (AclSetCookie *)cookie;
2070 LDAPURLDesc *ludp = NULL;
2071 Operation op2 = { 0 };
2072 SlapReply rs = {REP_RESULT};
2073 AttributeName anlist[ 2 ], *anlistp = NULL;
2075 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2076 acl_set_gather_t p = { 0 };
2077 const char *text = NULL;
2078 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2080 /* this routine needs to return the bervals instead of
2081 * plain strings, since syntax is not known. It should
2082 * also return the syntax or some "comparison cookie".
2084 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2085 return acl_set_gather2( cookie, name, desc );
2088 rc = ldap_url_parse( name->bv_val, &ludp );
2089 if ( rc != LDAP_URL_SUCCESS ) {
2090 rc = LDAP_PROTOCOL_ERROR;
2094 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2096 /* host part must be empty */
2097 /* extensions parts must be empty */
2098 rc = LDAP_PROTOCOL_ERROR;
2102 /* Grab the searchbase and see if an appropriate database can be found */
2103 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2104 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2105 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2106 BER_BVZERO( &op2.o_req_dn );
2107 if ( rc != LDAP_SUCCESS ) {
2111 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2112 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2113 rc = LDAP_NO_SUCH_OBJECT;
2117 /* Grab the filter */
2118 if ( ludp->lud_filter ) {
2119 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2120 cp->asc_op->o_tmpmemctx );
2123 op2.ors_filterstr = defaultFilter_bv;
2126 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2127 if ( op2.ors_filter == NULL ) {
2128 rc = LDAP_PROTOCOL_ERROR;
2132 /* Grab the scope */
2133 op2.ors_scope = ludp->lud_scope;
2135 /* Grap the attributes */
2136 if ( ludp->lud_attrs ) {
2137 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2140 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2141 cp->asc_op->o_tmpmemctx );
2143 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2144 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2145 anlistp[ nattrs ].an_desc = NULL;
2146 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2147 &anlistp[ nattrs ].an_desc, &text );
2148 if ( rc != LDAP_SUCCESS ) {
2157 anlistp[ nattrs ].an_name = desc->ad_cname;
2158 anlistp[ nattrs ].an_desc = desc;
2160 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2164 op2.o_hdr = cp->asc_op->o_hdr;
2165 op2.o_tag = LDAP_REQ_SEARCH;
2166 op2.o_ndn = op2.o_bd->be_rootndn;
2167 op2.o_callback = &cb;
2168 slap_op_time( &op2.o_time, &op2.o_tincr );
2169 op2.o_do_not_cache = 1;
2170 op2.o_is_auth_check = 0;
2171 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2172 op2.ors_slimit = SLAP_NO_LIMIT;
2173 op2.ors_tlimit = SLAP_NO_LIMIT;
2174 op2.ors_attrs = anlistp;
2175 op2.ors_attrsonly = 0;
2176 op2.o_private = cp->asc_op->o_private;
2180 rc = op2.o_bd->be_search( &op2, &rs );
2186 if ( op2.ors_filter ) {
2187 filter_free_x( cp->asc_op, op2.ors_filter );
2189 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2190 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2192 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2193 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2196 ldap_free_urldesc( ludp );
2198 if ( anlistp && anlistp != anlist ) {
2199 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2206 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2208 AclSetCookie *cp = (AclSetCookie *)cookie;
2209 BerVarray bvals = NULL;
2213 /* this routine needs to return the bervals instead of
2214 * plain strings, since syntax is not known. It should
2215 * also return the syntax or some "comparison cookie".
2217 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2218 if ( rc == LDAP_SUCCESS ) {
2219 if ( desc == slap_schema.si_ad_entryDN ) {
2220 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2221 cp->asc_op->o_tmpmemctx );
2223 BER_BVZERO( &bvals[ 1 ] );
2227 backend_attribute( cp->asc_op,
2228 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2231 if ( !BER_BVISNULL( &ndn ) ) {
2232 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2241 struct berval *subj,
2244 struct berval *default_set_attribute )
2246 struct berval set = BER_BVNULL;
2248 AclSetCookie cookie;
2250 if ( default_set_attribute == NULL ) {
2251 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2254 struct berval subjdn, ndn = BER_BVNULL;
2255 struct berval setat;
2256 BerVarray bvals = NULL;
2258 AttributeDescription *desc = NULL;
2260 /* format of string is "entry/setAttrName" */
2261 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2265 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2266 setat = *default_set_attribute;
2270 * NOTE: dnNormalize honors the ber_len field
2271 * as the length of the dn to be normalized
2273 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2274 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2276 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2277 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2281 BER_BVZERO( &bvals[0] );
2282 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2284 bvals[0].bv_val = bvals[i-1].bv_val;
2285 BER_BVZERO( &bvals[i-1] );
2287 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2288 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2293 if ( !BER_BVISNULL( &set ) ) {
2296 rc = ( slap_set_filter(
2298 (SetCookie *)&cookie, &set,
2299 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2300 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2309 * dynamic ACL infrastructure
2311 static slap_dynacl_t *da_list = NULL;
2314 slap_dynacl_register( slap_dynacl_t *da )
2318 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2319 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2324 if ( tmp != NULL ) {
2328 if ( da->da_mask == NULL ) {
2332 da->da_private = NULL;
2333 da->da_next = da_list;
2339 static slap_dynacl_t *
2340 slap_dynacl_next( slap_dynacl_t *da )
2349 slap_dynacl_get( const char *name )
2353 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2354 if ( strcasecmp( da->da_name, name ) == 0 ) {
2361 #endif /* SLAP_DYNACL */
2364 * statically built-in dynamic ACL initialization
2366 static int (*acl_init_func[])( void ) = {
2368 /* TODO: remove when ACI will only be dynamic */
2369 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2371 #endif /* SLAPD_ACI_ENABLED */
2372 #endif /* SLAP_DYNACL */
2382 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2383 rc = (*(acl_init_func[ i ]))();
2398 regmatch_t *matches)
2406 bv->bv_val[0] = '\0';
2407 bv->bv_len--; /* leave space for lone $ */
2410 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2411 sp < pat->bv_val + pat->bv_len ; sp++ )
2413 /* did we previously see a $ */
2415 if ( flag == 1 && *sp == '$' ) {
2420 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2423 } else if ( *sp >= '0' && *sp <= '9' ) {
2431 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2432 if ( *sp >= '0' && *sp <= '9' ) {
2433 n = 10*n + ( *sp - '0' );
2437 if ( *sp != /*'{'*/ '}' ) {
2443 if ( n >= nmatch ) {
2449 i = matches[n].rm_so;
2450 l = matches[n].rm_eo;
2451 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2469 /* must have ended with a single $ */
2477 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2478 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2485 struct berval *pat, /* pattern to expand and match against */
2486 char *str, /* string to match against pattern */
2487 char *buf, /* buffer with $N expansion variables */
2488 int nmatch, /* size of the matches array */
2489 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2493 char newbuf[ACL_BUF_SIZE];
2497 bv.bv_len = sizeof( newbuf ) - 1;
2504 acl_string_expand( &bv, pat, buf, nmatch, matches );
2505 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2507 char error[ACL_BUF_SIZE];
2508 regerror( rc, &re, error, sizeof( error ) );
2510 Debug( LDAP_DEBUG_TRACE,
2511 "compile( \"%s\", \"%s\") failed %s\n",
2512 pat->bv_val, str, error );
2516 rc = regexec( &re, str, 0, NULL, 0 );
2519 Debug( LDAP_DEBUG_TRACE,
2520 "=> regex_matches: string: %s\n", str, 0, 0 );
2521 Debug( LDAP_DEBUG_TRACE,
2522 "=> regex_matches: rc: %d %s\n",
2523 rc, !rc ? "matches" : "no matches", 0 );