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-2009 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 AclRegexMatches *matches,
57 AccessControlState *state );
59 static slap_control_t slap_acl_mask(
60 AccessControl *ac, slap_mask_t *mask,
61 Operation *op, Entry *e,
62 AttributeDescription *desc,
64 AclRegexMatches *matches,
66 AccessControlState *state,
67 slap_access_t access );
69 static int regex_matches(
70 struct berval *pat, char *str,
71 struct berval *dn_matches, struct berval *val_matches,
72 AclRegexMatches *matches);
74 typedef struct AclSetCookie {
76 #define asc_op asc_cookie.set_op
81 SLAP_SET_GATHER acl_set_gather;
82 SLAP_SET_GATHER acl_set_gather2;
85 * access_allowed - check whether op->o_ndn is allowed the requested access
86 * to entry e, attribute attr, value val. if val is null, access to
87 * the whole attribute is assumed (all values).
89 * This routine loops through all access controls and calls
90 * slap_acl_mask() on each applicable access control.
91 * The loop exits when a definitive answer is reached or
92 * or no more controls remain.
99 * - can be legally called with op == NULL
100 * - can be legally called with op->o_bd == NULL
104 slap_access_always_allowed(
107 AttributeDescription *desc,
109 slap_access_t access,
110 AccessControlState *state,
113 assert( maskp != NULL );
116 ACL_LVL_ASSIGN_MANAGE( *maskp );
121 #define MATCHES_DNMAXCOUNT(m) \
122 ( sizeof ( (m)->dn_data ) / sizeof( *(m)->dn_data ) )
123 #define MATCHES_VALMAXCOUNT(m) \
124 ( sizeof ( (m)->val_data ) / sizeof( *(m)->val_data ) )
125 #define MATCHES_MEMSET(m) do { \
126 memset( (m)->dn_data, '\0', sizeof( (m)->dn_data ) ); \
127 memset( (m)->val_data, '\0', sizeof( (m)->val_data ) ); \
128 (m)->dn_count = MATCHES_DNMAXCOUNT( (m) ); \
129 (m)->val_count = MATCHES_VALMAXCOUNT( (m) ); \
130 } while ( 0 /* CONSTCOND */ )
136 AttributeDescription *desc,
138 slap_access_t access,
139 AccessControlState *state,
144 AccessControl *a = NULL;
147 char accessmaskbuf[ACCESSMASK_MAXLEN];
150 slap_control_t control;
151 slap_access_t access_level;
153 AclRegexMatches matches;
154 AccessControlState acl_state = ACL_STATE_INIT;
155 static AccessControlState state_init = ACL_STATE_INIT;
157 assert( op != NULL );
159 assert( desc != NULL );
160 assert( maskp != NULL );
162 access_level = ACL_LEVEL( access );
163 attr = desc->ad_cname.bv_val;
165 assert( attr != NULL );
169 /* grant database root access */
170 if ( be_isroot( op ) ) {
171 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
172 mask = ACL_LVL_MANAGE;
177 * no-user-modification operational attributes are ignored
178 * by ACL_WRITE checking as any found here are not provided
181 * NOTE: but they are not ignored for ACL_MANAGE, because
182 * if we get here it means a non-root user is trying to
183 * manage data, so we need to check its privileges.
185 if ( access_level == ACL_WRITE
186 && is_at_no_user_mod( desc->ad_type )
187 && desc != slap_schema.si_ad_entry
188 && desc != slap_schema.si_ad_children )
190 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
191 " %s access granted\n",
196 /* use backend default access if no backend acls */
197 if ( op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
200 Debug( LDAP_DEBUG_ACL,
201 "=> slap_access_allowed: backend default %s "
202 "access %s to \"%s\"\n",
203 access2str( access ),
204 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
205 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
206 ret = op->o_bd->be_dfltaccess >= access_level;
208 mask = ACL_PRIV_LEVEL;
209 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
210 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
221 if ( state->as_desc == desc &&
222 state->as_access == access &&
223 state->as_vd_acl != NULL )
225 a = state->as_vd_acl;
226 count = state->as_vd_acl_count;
227 if ( state->as_fe_done )
229 ACL_PRIV_ASSIGN( mask, state->as_vd_mask );
235 ACL_PRIV_ASSIGN( mask, *maskp );
238 MATCHES_MEMSET( &matches );
240 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
241 &matches, &mask, state ) ) != NULL )
244 int dnmaxcount = MATCHES_DNMAXCOUNT( &matches );
245 int valmaxcount = MATCHES_VALMAXCOUNT( &matches );
246 regmatch_t *dn_data = matches.dn_data;
247 regmatch_t *val_data = matches.val_data;
250 for ( i = 0; i < dnmaxcount && dn_data[i].rm_eo > 0; i++ ) {
251 char *data = e->e_ndn;
253 Debug( LDAP_DEBUG_ACL, "=> match[dn%d]: %d %d ", i,
254 (int)dn_data[i].rm_so,
255 (int)dn_data[i].rm_eo );
256 if ( dn_data[i].rm_so <= dn_data[0].rm_eo ) {
258 for ( n = dn_data[i].rm_so;
259 n < dn_data[i].rm_eo; n++ ) {
260 Debug( LDAP_DEBUG_ACL, "%c",
264 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
268 for ( i = 0; i < valmaxcount && val_data[i].rm_eo > 0; i++ ) {
269 char *data = val->bv_val;
271 Debug( LDAP_DEBUG_ACL, "=> match[val%d]: %d %d ", i,
272 (int)val_data[i].rm_so,
273 (int)val_data[i].rm_eo );
274 if ( val_data[i].rm_so <= val_data[0].rm_eo ) {
276 for ( n = val_data[i].rm_so;
277 n < val_data[i].rm_eo; n++ ) {
278 Debug( LDAP_DEBUG_ACL, "%c",
282 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
285 control = slap_acl_mask( a, &mask, op,
286 e, desc, val, &matches, count, state, access );
288 if ( control != ACL_BREAK ) {
292 MATCHES_MEMSET( &matches );
295 if ( ACL_IS_INVALID( mask ) ) {
296 Debug( LDAP_DEBUG_ACL,
297 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
299 ACL_PRIV_ASSIGN( mask, *maskp );
301 } else if ( control == ACL_BREAK ) {
302 Debug( LDAP_DEBUG_ACL,
303 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
308 ret = ACL_GRANT( mask, access );
310 Debug( LDAP_DEBUG_ACL,
311 "=> slap_access_allowed: %s access %s by %s\n",
312 access2str( access ), ret ? "granted" : "denied",
313 accessmask2str( mask, accessmaskbuf, 1 ) );
316 ACL_PRIV_ASSIGN( *maskp, mask );
324 AttributeDescription *desc,
326 slap_access_t access,
327 AccessControlState *state,
334 * NOTE: control gets here if FIXME
335 * if an appropriate backend cannot be selected for the operation,
336 * we assume that the frontend should handle this
337 * FIXME: should select_backend() take care of this,
338 * and return frontendDB instead of NULL? maybe for some value
343 if ( op->o_bd == NULL ) {
344 op->o_bd = select_backend( &op->o_req_ndn, 0 );
345 if ( op->o_bd == NULL )
346 op->o_bd = frontendDB;
348 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
358 AttributeDescription *desc,
360 slap_access_t access,
361 AccessControlState *state,
368 char accessmaskbuf[ACCESSMASK_MAXLEN];
371 slap_access_t access_level;
375 assert( desc != NULL );
377 access_level = ACL_LEVEL( access );
379 assert( access_level > ACL_NONE );
382 if ( maskp ) ACL_INVALIDATE( *maskp );
384 attr = desc->ad_cname.bv_val;
386 assert( attr != NULL );
389 if ( op->o_acl_priv != ACL_NONE ) {
390 access = op->o_acl_priv;
392 } else if ( op->o_is_auth_check &&
393 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
397 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
398 desc == slap_schema.si_ad_entry )
404 if ( state != NULL ) {
405 if ( state->as_desc == desc &&
406 state->as_access == access &&
407 state->as_result != -1 &&
408 state->as_vd_acl == NULL )
410 Debug( LDAP_DEBUG_ACL,
411 "=> access_allowed: result was in cache (%s)\n",
413 return state->as_result;
415 Debug( LDAP_DEBUG_ACL,
416 "=> access_allowed: result not in cache (%s)\n",
421 Debug( LDAP_DEBUG_ACL,
422 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
423 access2str( access ), e->e_dn, attr );
430 if ( op->o_bd == NULL ) {
431 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
434 /* FIXME: experimental; use first backend rules
435 * iff there is no global_acl (ITS#3100)
437 if ( frontendDB->be_acl != NULL ) {
438 op->o_bd = frontendDB;
441 assert( op->o_bd != NULL );
443 /* this is enforced in backend_add() */
444 if ( op->o_bd->bd_info->bi_access_allowed ) {
445 /* delegate to backend */
446 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
447 desc, val, access, state, &mask );
450 /* use default (but pass through frontend
451 * for global ACL overlays) */
452 ret = frontendDB->bd_info->bi_access_allowed( op, e,
453 desc, val, access, state, &mask );
457 if ( ACL_IS_INVALID( mask ) ) {
458 Debug( LDAP_DEBUG_ACL,
459 "=> access_allowed: \"%s\" (%s) invalid!\n",
464 Debug( LDAP_DEBUG_ACL,
465 "=> access_allowed: no more rules\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_ACL,
472 "=> access_allowed: %s access %s by %s\n",
473 access2str( access ), ret ? "granted" : "denied",
474 accessmask2str( mask, accessmaskbuf, 1 ) );
477 if ( state != NULL ) {
478 state->as_access = access;
479 state->as_result = ret;
480 state->as_desc = desc;
482 if ( be_null ) op->o_bd = NULL;
483 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
489 * slap_acl_get - return the acl applicable to entry e, attribute
490 * attr. the acl returned is suitable for use in subsequent calls to
491 * acl_access_allowed().
494 static AccessControl *
500 AttributeDescription *desc,
502 AclRegexMatches *matches,
504 AccessControlState *state )
511 assert( count != NULL );
512 assert( desc != NULL );
513 assert( state != NULL );
515 attr = desc->ad_cname.bv_val;
517 assert( attr != NULL );
520 if( op->o_bd == NULL || op->o_bd->be_acl == NULL ) {
521 a = frontendDB->be_acl;
523 a = op->o_bd->be_acl;
528 if ( a == frontendDB->be_acl )
529 state->as_fe_done = 1;
535 dnlen = e->e_nname.bv_len;
538 for ( ; a != NULL; prev = a, a = a->acl_next ) {
541 if ( a != frontendDB->be_acl && state->as_fe_done )
544 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
545 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
546 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
547 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
548 if ( regexec ( &a->acl_dn_re,
551 matches->dn_data, 0 ) )
557 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
558 *count, a->acl_dn_pat.bv_val, 0 );
559 patlen = a->acl_dn_pat.bv_len;
560 if ( dnlen < patlen )
563 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
564 /* base dn -- entire object DN must match */
565 if ( dnlen != patlen )
568 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
569 ber_len_t rdnlen = 0;
572 if ( dnlen <= patlen )
576 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
581 rdnlen = dn_rdnlen( NULL, &e->e_nname );
582 if ( rdnlen + patlen + sep != dnlen )
585 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
586 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
589 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
590 if ( dnlen <= patlen )
592 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
596 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
600 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
604 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
605 matches->dn_data[0].rm_so = -1;
606 matches->dn_data[0].rm_eo = -1;
607 matches->val_data[0].rm_so = -1;
608 matches->val_data[0].rm_eo = -1;
612 /* Is this ACL only for a specific value? */
613 if ( a->acl_attrval.bv_len ) {
618 if ( state->as_vd_acl == NULL ) {
619 state->as_vd_acl = prev;
620 state->as_vd_acl_count = *count - 1;
621 ACL_PRIV_ASSIGN ( state->as_vd_mask, *mask );
624 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
625 Debug( LDAP_DEBUG_ACL,
626 "acl_get: valpat %s\n",
627 a->acl_attrval.bv_val, 0, 0 );
628 if ( regexec ( &a->acl_attrval_re,
631 matches->val_data, 0 ) )
639 Debug( LDAP_DEBUG_ACL,
641 a->acl_attrval.bv_val, 0, 0 );
643 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
644 if (value_match( &match, desc,
645 a->acl_attrval_mr, 0,
646 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
651 ber_len_t patlen, vdnlen;
653 patlen = a->acl_attrval.bv_len;
654 vdnlen = val->bv_len;
656 if ( vdnlen < patlen )
659 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
660 if ( vdnlen > patlen )
663 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
664 ber_len_t rdnlen = 0;
666 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
669 rdnlen = dn_rdnlen( NULL, val );
670 if ( rdnlen + patlen + 1 != vdnlen )
673 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
674 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
677 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
678 if ( vdnlen <= patlen )
681 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
685 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
691 if ( a->acl_filter != NULL ) {
692 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
693 if ( rc != LDAP_COMPARE_TRUE ) {
698 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
703 if ( !state->as_fe_done ) {
704 state->as_fe_done = 1;
705 a = frontendDB->be_acl;
709 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
714 * Record value-dependent access control state
716 #define ACL_RECORD_VALUE_STATE do { \
717 if( state && state->as_vd_acl == NULL ) { \
718 state->as_vd_acl = a; \
719 state->as_vd_acl_count = count; \
720 ACL_PRIV_ASSIGN( state->as_vd_mask, *mask ); \
730 AclRegexMatches *matches,
732 struct berval *opndn )
735 * if access applies to the entry itself, and the
736 * user is bound as somebody in the same namespace as
737 * the entry, OR the given dn matches the dn pattern
740 * NOTE: styles "anonymous", "users" and "self"
741 * have been moved to enum slap_style_t, whose
742 * value is set in a_dn_style; however, the string
743 * is maintained in a_dn_pat.
746 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
747 if ( !BER_BVISEMPTY( opndn ) ) {
751 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
752 if ( BER_BVISEMPTY( opndn ) ) {
756 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
757 struct berval ndn, selfndn;
760 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
764 level = bdn->a_self_level;
772 selfndn = e->e_nname;
775 for ( ; level > 0; level-- ) {
776 if ( BER_BVISEMPTY( &ndn ) ) {
779 dnParent( &ndn, &ndn );
782 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
787 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
788 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
789 AclRegexMatches tmp_matches,
790 *tmp_matchesp = &tmp_matches;
792 regmatch_t *tmp_data;
794 MATCHES_MEMSET( &tmp_matches );
795 tmp_data = &tmp_matches.dn_data[0];
797 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
798 tmp_matchesp = matches;
799 else switch ( a->acl_dn_style ) {
800 case ACL_STYLE_REGEX:
801 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
802 tmp_matchesp = matches;
805 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
808 tmp_data[0].rm_so = 0;
809 tmp_data[0].rm_eo = e->e_nname.bv_len;
810 tmp_matches.dn_count = 1;
814 case ACL_STYLE_SUBTREE:
815 case ACL_STYLE_CHILDREN:
816 tmp_data[0].rm_so = 0;
817 tmp_data[0].rm_eo = e->e_nname.bv_len;
818 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
819 tmp_data[1].rm_eo = e->e_nname.bv_len;
820 tmp_matches.dn_count = 2;
833 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
834 &e->e_nname, NULL, tmp_matchesp ) )
842 ber_len_t patlen, odnlen;
845 if ( e->e_dn == NULL )
848 if ( bdn->a_expand ) {
850 char buf[ACL_BUF_SIZE];
852 AclRegexMatches tmp_matches,
853 *tmp_matchesp = &tmp_matches;
855 regmatch_t *tmp_data;
857 MATCHES_MEMSET( &tmp_matches );
858 tmp_data = &tmp_matches.dn_data[0];
860 bv.bv_len = sizeof( buf ) - 1;
863 /* Expand value regex */
864 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
865 tmp_matchesp = matches;
866 else switch ( a->acl_dn_style ) {
867 case ACL_STYLE_REGEX:
868 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
869 tmp_matchesp = matches;
872 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
875 tmp_data[0].rm_so = 0;
876 tmp_data[0].rm_eo = e->e_nname.bv_len;
877 tmp_matches.dn_count = 1;
881 case ACL_STYLE_SUBTREE:
882 case ACL_STYLE_CHILDREN:
883 tmp_data[0].rm_so = 0;
884 tmp_data[0].rm_eo = e->e_nname.bv_len;
885 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
886 tmp_data[1].rm_eo = e->e_nname.bv_len;
887 tmp_matches.dn_count = 2;
900 if ( acl_string_expand( &bv, &bdn->a_pat,
902 val, tmp_matchesp ) )
907 if ( dnNormalize(0, NULL, NULL, &bv,
908 &pat, op->o_tmpmemctx )
911 /* did not expand to a valid dn */
920 odnlen = opndn->bv_len;
921 if ( odnlen < patlen ) {
922 goto dn_match_cleanup;
926 if ( bdn->a_style == ACL_STYLE_BASE ) {
927 /* base dn -- entire object DN must match */
928 if ( odnlen != patlen ) {
929 goto dn_match_cleanup;
932 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
933 ber_len_t rdnlen = 0;
935 if ( odnlen <= patlen ) {
936 goto dn_match_cleanup;
939 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
940 goto dn_match_cleanup;
943 rdnlen = dn_rdnlen( NULL, opndn );
944 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
945 goto dn_match_cleanup;
948 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
949 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
950 goto dn_match_cleanup;
953 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
954 if ( odnlen <= patlen ) {
955 goto dn_match_cleanup;
958 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
959 goto dn_match_cleanup;
962 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
963 int level = bdn->a_level;
966 if ( odnlen <= patlen ) {
967 goto dn_match_cleanup;
970 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
972 goto dn_match_cleanup;
976 for ( ; level > 0; level-- ) {
977 if ( BER_BVISEMPTY( &ndn ) ) {
978 goto dn_match_cleanup;
980 dnParent( &ndn, &ndn );
981 if ( ndn.bv_len < patlen ) {
982 goto dn_match_cleanup;
986 if ( ndn.bv_len != patlen ) {
987 goto dn_match_cleanup;
991 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
994 if ( pat.bv_val != bdn->a_pat.bv_val ) {
995 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1013 AccessControlState *state,
1015 slap_dn_access *bdn,
1016 struct berval *opndn )
1022 const char *attr = bdn->a_at->ad_cname.bv_val;
1024 assert( attr != NULL );
1026 if ( BER_BVISEMPTY( opndn ) ) {
1030 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1033 /* see if asker is listed in dnattr */
1034 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1036 at = attrs_find( at->a_next, bdn->a_at ) )
1038 if ( attr_valfind( at,
1039 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1040 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1041 &bv, NULL, op->o_tmpmemctx ) == 0 )
1050 /* have a dnattr match. if this is a self clause then
1051 * the target must also match the op dn.
1053 if ( bdn->a_self ) {
1054 /* check if the target is an attribute. */
1055 if ( val == NULL ) return 1;
1057 /* target is attribute, check if the attribute value
1060 rc = value_match( &match, bdn->a_at,
1061 bdn->a_at->ad_type->sat_equality, 0,
1063 /* on match error or no match, fail the ACL clause */
1064 if ( rc != LDAP_SUCCESS || match != 0 )
1069 /* no dnattr match, check if this is a self clause */
1070 if ( ! bdn->a_self )
1073 ACL_RECORD_VALUE_STATE;
1075 /* this is a self clause, check if the target is an
1081 /* target is attribute, check if the attribute value
1084 rc = value_match( &match, bdn->a_at,
1085 bdn->a_at->ad_type->sat_equality, 0,
1088 /* on match error or no match, fail the ACL clause */
1089 if ( rc != LDAP_SUCCESS || match != 0 )
1098 * slap_acl_mask - modifies mask based upon the given acl and the
1099 * requested access to entry e, attribute attr, value val. if val
1100 * is null, access to the whole attribute is assumed (all values).
1102 * returns 0 access NOT allowed
1106 static slap_control_t
1112 AttributeDescription *desc,
1114 AclRegexMatches *matches,
1116 AccessControlState *state,
1117 slap_access_t access )
1122 char accessmaskbuf[ACCESSMASK_MAXLEN];
1126 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1127 #endif /* SLAP_DYNACL */
1129 assert( a != NULL );
1130 assert( mask != NULL );
1131 assert( desc != NULL );
1133 attr = desc->ad_cname.bv_val;
1135 assert( attr != NULL );
1137 Debug( LDAP_DEBUG_ACL,
1138 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1141 Debug( LDAP_DEBUG_ACL,
1142 "=> acl_mask: to %s by \"%s\", (%s) \n",
1143 val ? "value" : "all values",
1144 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1145 accessmask2str( *mask, accessmaskbuf, 1 ) );
1151 for ( ; b != NULL; b = b->a_next, i++ ) {
1152 slap_mask_t oldmask, modmask;
1154 ACL_INVALIDATE( modmask );
1156 /* AND <who> clauses */
1157 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1158 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1159 b->a_dn_pat.bv_val, 0, 0);
1161 * if access applies to the entry itself, and the
1162 * user is bound as somebody in the same namespace as
1163 * the entry, OR the given dn matches the dn pattern
1166 * NOTE: styles "anonymous", "users" and "self"
1167 * have been moved to enum slap_style_t, whose
1168 * value is set in a_dn_style; however, the string
1169 * is maintained in a_dn_pat.
1172 if ( acl_mask_dn( op, e, val, a, matches,
1173 &b->a_dn, &op->o_ndn ) )
1179 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1182 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1183 b->a_realdn_pat.bv_val, 0, 0);
1185 * if access applies to the entry itself, and the
1186 * user is bound as somebody in the same namespace as
1187 * the entry, OR the given dn matches the dn pattern
1190 * NOTE: styles "anonymous", "users" and "self"
1191 * have been moved to enum slap_style_t, whose
1192 * value is set in a_dn_style; however, the string
1193 * is maintained in a_dn_pat.
1196 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1198 ndn = op->o_conn->c_ndn;
1203 if ( acl_mask_dn( op, e, val, a, matches,
1204 &b->a_realdn, &ndn ) )
1210 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1211 if ( ! op->o_conn->c_listener ) {
1214 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1215 b->a_sockurl_pat.bv_val, 0, 0 );
1217 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1218 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1219 if ( !regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1220 &e->e_nname, val, matches ) )
1225 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1227 char buf[ACL_BUF_SIZE];
1229 bv.bv_len = sizeof( buf ) - 1;
1231 if ( acl_string_expand( &bv, &b->a_sockurl_pat, &e->e_nname, val, matches ) )
1236 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1242 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1250 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1251 if ( !op->o_conn->c_peer_domain.bv_val ) {
1254 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1255 b->a_domain_pat.bv_val, 0, 0 );
1256 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1257 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1258 if ( !regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1259 &e->e_nname, val, matches ) )
1264 char buf[ACL_BUF_SIZE];
1266 struct berval cmp = op->o_conn->c_peer_domain;
1267 struct berval pat = b->a_domain_pat;
1269 if ( b->a_domain_expand ) {
1272 bv.bv_len = sizeof(buf) - 1;
1275 if ( acl_string_expand(&bv, &b->a_domain_pat, &e->e_nname, val, matches) )
1282 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1283 int offset = cmp.bv_len - pat.bv_len;
1288 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1292 /* trim the domain */
1293 cmp.bv_val = &cmp.bv_val[ offset ];
1294 cmp.bv_len -= offset;
1297 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1304 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1305 if ( !op->o_conn->c_peer_name.bv_val ) {
1308 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1309 b->a_peername_pat.bv_val, 0, 0 );
1310 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1311 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1312 if ( !regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1313 &e->e_nname, val, matches ) )
1319 /* try exact match */
1320 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1321 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1325 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1327 char buf[ACL_BUF_SIZE];
1329 bv.bv_len = sizeof( buf ) - 1;
1331 if ( acl_string_expand( &bv, &b->a_peername_pat, &e->e_nname, val, matches ) )
1336 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1340 /* extract IP and try exact match */
1341 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1343 char buf[STRLENOF("255.255.255.255") + 1];
1346 int port_number = -1;
1348 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1349 acl_bv_ip_eq.bv_val,
1350 acl_bv_ip_eq.bv_len ) != 0 )
1353 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1354 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1356 port = strrchr( ip.bv_val, ':' );
1358 ip.bv_len = port - ip.bv_val;
1360 if ( lutil_atoi( &port_number, port ) != 0 )
1364 /* the port check can be anticipated here */
1365 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1368 /* address longer than expected? */
1369 if ( ip.bv_len >= sizeof(buf) )
1372 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1373 buf[ ip.bv_len ] = '\0';
1375 addr = inet_addr( buf );
1377 /* unable to convert? */
1378 if ( addr == (unsigned long)(-1) )
1381 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1384 #ifdef LDAP_PF_INET6
1385 /* extract IPv6 and try exact match */
1386 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1388 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1390 struct in6_addr addr;
1391 int port_number = -1;
1393 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1394 acl_bv_ipv6_eq.bv_val,
1395 acl_bv_ipv6_eq.bv_len ) != 0 )
1398 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1399 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1401 port = strrchr( ip.bv_val, ']' );
1403 ip.bv_len = port - ip.bv_val;
1405 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1409 /* the port check can be anticipated here */
1410 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1413 /* address longer than expected? */
1414 if ( ip.bv_len >= sizeof(buf) )
1417 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1418 buf[ ip.bv_len ] = '\0';
1420 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1424 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1426 #endif /* LDAP_PF_INET6 */
1428 #ifdef LDAP_PF_LOCAL
1429 /* extract path and try exact match */
1430 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1433 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1434 acl_bv_path_eq.bv_val,
1435 acl_bv_path_eq.bv_len ) != 0 )
1438 path.bv_val = op->o_conn->c_peer_name.bv_val
1439 + acl_bv_path_eq.bv_len;
1440 path.bv_len = op->o_conn->c_peer_name.bv_len
1441 - acl_bv_path_eq.bv_len;
1443 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1446 #endif /* LDAP_PF_LOCAL */
1448 /* exact match (very unlikely...) */
1449 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1456 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1457 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1460 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1461 b->a_sockname_pat.bv_val, 0, 0 );
1462 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1463 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1464 if ( !regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1465 &e->e_nname, val, matches ) )
1470 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1472 char buf[ACL_BUF_SIZE];
1474 bv.bv_len = sizeof( buf ) - 1;
1476 if ( acl_string_expand( &bv, &b->a_sockname_pat, &e->e_nname, val, matches ) )
1481 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1486 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1493 if ( b->a_dn_at != NULL ) {
1494 if ( acl_mask_dnattr( op, e, val, a,
1496 &b->a_dn, &op->o_ndn ) )
1502 if ( b->a_realdn_at != NULL ) {
1505 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1507 ndn = op->o_conn->c_ndn;
1512 if ( acl_mask_dnattr( op, e, val, a,
1514 &b->a_realdn, &ndn ) )
1520 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1522 struct berval ndn = BER_BVNULL;
1525 if ( op->o_ndn.bv_len == 0 ) {
1529 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1530 b->a_group_pat.bv_val, 0, 0 );
1532 /* b->a_group is an unexpanded entry name, expanded it should be an
1533 * entry with objectclass group* and we test to see if odn is one of
1534 * the values in the attribute group
1536 /* see if asker is listed in dnattr */
1537 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1538 char buf[ACL_BUF_SIZE];
1539 AclRegexMatches tmp_matches,
1540 *tmp_matchesp = &tmp_matches;
1541 regmatch_t *tmp_data;
1543 MATCHES_MEMSET( &tmp_matches );
1544 tmp_data = &tmp_matches.dn_data[0];
1546 bv.bv_len = sizeof(buf) - 1;
1551 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1552 tmp_matchesp = matches;
1553 else switch ( a->acl_dn_style ) {
1554 case ACL_STYLE_REGEX:
1555 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1556 tmp_matchesp = matches;
1560 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1561 case ACL_STYLE_BASE:
1562 tmp_data[0].rm_so = 0;
1563 tmp_data[0].rm_eo = e->e_nname.bv_len;
1564 tmp_matches.dn_count = 1;
1568 case ACL_STYLE_SUBTREE:
1569 case ACL_STYLE_CHILDREN:
1570 tmp_data[0].rm_so = 0;
1571 tmp_data[0].rm_eo = e->e_nname.bv_len;
1573 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1574 tmp_data[1].rm_eo = e->e_nname.bv_len;
1575 tmp_matches.dn_count = 2;
1588 if ( acl_string_expand( &bv, &b->a_group_pat,
1595 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1596 op->o_tmpmemctx ) != LDAP_SUCCESS )
1598 /* did not expand to a valid dn */
1605 bv = b->a_group_pat;
1608 rc = backend_group( op, e, &bv, &op->o_ndn,
1609 b->a_group_oc, b->a_group_at );
1612 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1620 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1622 char buf[ACL_BUF_SIZE];
1624 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1625 b->a_set_pat.bv_val, 0, 0 );
1627 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1628 AclRegexMatches tmp_matches,
1629 *tmp_matchesp = &tmp_matches;
1631 regmatch_t *tmp_data;
1633 MATCHES_MEMSET( &tmp_matches );
1634 tmp_data = &tmp_matches.dn_data[0];
1636 bv.bv_len = sizeof( buf ) - 1;
1641 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1642 tmp_matchesp = matches;
1643 else switch ( a->acl_dn_style ) {
1644 case ACL_STYLE_REGEX:
1645 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1646 tmp_matchesp = matches;
1650 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1651 case ACL_STYLE_BASE:
1652 tmp_data[0].rm_so = 0;
1653 tmp_data[0].rm_eo = e->e_nname.bv_len;
1654 tmp_matches.dn_count = 1;
1658 case ACL_STYLE_SUBTREE:
1659 case ACL_STYLE_CHILDREN:
1660 tmp_data[0].rm_so = 0;
1661 tmp_data[0].rm_eo = e->e_nname.bv_len;
1662 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1663 tmp_data[1].rm_eo = e->e_nname.bv_len; tmp_matches.dn_count = 2;
1676 if ( acl_string_expand( &bv, &b->a_set_pat,
1687 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1692 if ( b->a_authz.sai_ssf ) {
1693 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1694 b->a_authz.sai_ssf, op->o_ssf, 0 );
1695 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1700 if ( b->a_authz.sai_transport_ssf ) {
1701 Debug( LDAP_DEBUG_ACL,
1702 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1703 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1704 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1709 if ( b->a_authz.sai_tls_ssf ) {
1710 Debug( LDAP_DEBUG_ACL,
1711 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1712 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1713 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1718 if ( b->a_authz.sai_sasl_ssf ) {
1719 Debug( LDAP_DEBUG_ACL,
1720 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1721 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1722 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1727 /* check for the "self" modifier in the <access> field */
1728 if ( b->a_dn.a_self ) {
1732 ACL_RECORD_VALUE_STATE;
1734 /* must have DN syntax */
1735 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1736 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1738 /* check if the target is an attribute. */
1739 if ( val == NULL ) continue;
1741 /* a DN must be present */
1742 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1746 /* target is attribute, check if the attribute value
1749 rc = value_match( &match, desc,
1750 desc->ad_type->sat_equality, 0,
1751 val, &op->o_ndn, &dummy );
1752 /* on match error or no match, fail the ACL clause */
1753 if ( rc != LDAP_SUCCESS || match != 0 )
1758 if ( b->a_dynacl ) {
1760 slap_access_t tgrant, tdeny;
1762 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1765 /* this case works different from the others above.
1766 * since dynamic ACL's themselves give permissions, we need
1767 * to first check b->a_access_mask, the ACL's access level.
1769 /* first check if the right being requested
1770 * is allowed by the ACL clause.
1772 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1776 /* start out with nothing granted, nothing denied */
1777 ACL_INVALIDATE(tgrant);
1778 ACL_INVALIDATE(tdeny);
1780 for ( da = b->a_dynacl; da; da = da->da_next ) {
1781 slap_access_t grant,
1784 ACL_INVALIDATE(grant);
1785 ACL_INVALIDATE(deny);
1787 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1788 da->da_name, 0, 0 );
1791 * XXXmanu Only DN matches are supplied
1792 * sending attribute values matches require
1795 (void)da->da_mask( da->da_private, op, e, desc,
1796 val, matches->dn_count, matches->dn_data,
1803 /* remove anything that the ACL clause does not allow */
1804 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1805 tdeny &= ACL_PRIV_MASK;
1807 /* see if we have anything to contribute */
1808 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1812 /* this could be improved by changing slap_acl_mask so that it can deal with
1813 * by clauses that return grant/deny pairs. Right now, it does either
1814 * additive or subtractive rights, but not both at the same time. So,
1815 * we need to combine the grant/deny pair into a single rights mask in
1816 * a smart way: if either grant or deny is "empty", then we use the
1817 * opposite as is, otherwise we remove any denied rights from the grant
1818 * rights mask and construct an additive mask.
1820 if (ACL_IS_INVALID(tdeny)) {
1821 modmask = tgrant | ACL_PRIV_ADDITIVE;
1823 } else if (ACL_IS_INVALID(tgrant)) {
1824 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1827 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1831 #endif /* SLAP_DYNACL */
1833 modmask = b->a_access_mask;
1836 Debug( LDAP_DEBUG_ACL,
1837 "<= acl_mask: [%d] applying %s (%s)\n",
1838 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1839 b->a_type == ACL_CONTINUE
1841 : b->a_type == ACL_BREAK
1847 if( ACL_IS_ADDITIVE(modmask) ) {
1849 ACL_PRIV_SET( *mask, modmask );
1852 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1854 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1855 /* substract privs */
1856 ACL_PRIV_CLR( *mask, modmask );
1859 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1866 Debug( LDAP_DEBUG_ACL,
1867 "<= acl_mask: [%d] mask: %s\n",
1868 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1870 if( b->a_type == ACL_CONTINUE ) {
1873 } else if ( b->a_type == ACL_BREAK ) {
1881 /* implicit "by * none" clause */
1884 Debug( LDAP_DEBUG_ACL,
1885 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1886 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1891 * acl_check_modlist - check access control on the given entry to see if
1892 * it allows the given modifications by the user associated with op.
1893 * returns 1 if mods allowed ok
1894 * 0 mods not allowed
1901 Modifications *mlist )
1904 AccessControlState state = ACL_STATE_INIT;
1907 int ret = 1; /* default is access allowed */
1911 be = LDAP_STAILQ_FIRST(&backendDB);
1915 assert( be != NULL );
1917 /* If ADD attribute checking is not enabled, just allow it */
1918 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1921 /* short circuit root database access */
1922 if ( be_isroot( op ) ) {
1923 Debug( LDAP_DEBUG_ACL,
1924 "<= acl_access_allowed: granted to database root\n",
1929 /* use backend default access if no backend acls */
1930 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1931 Debug( LDAP_DEBUG_ACL,
1932 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1933 access2str( ACL_WRITE ),
1934 op->o_bd->be_dfltaccess >= ACL_WRITE
1935 ? "granted" : "denied",
1937 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1941 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1943 * Internal mods are ignored by ACL_WRITE checking
1945 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1946 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1947 " modify access granted\n",
1948 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1953 * no-user-modification operational attributes are ignored
1954 * by ACL_WRITE checking as any found here are not provided
1957 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1958 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1960 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1961 " modify access granted\n",
1962 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1966 switch ( mlist->sml_op ) {
1967 case LDAP_MOD_REPLACE:
1968 case LDAP_MOD_INCREMENT:
1970 * We must check both permission to delete the whole
1971 * attribute and permission to add the specific attributes.
1972 * This prevents abuse from selfwriters.
1974 if ( ! access_allowed( op, e,
1975 mlist->sml_desc, NULL,
1976 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1983 if ( mlist->sml_values == NULL ) break;
1985 /* fall thru to check value to add */
1988 assert( mlist->sml_values != NULL );
1990 for ( bv = mlist->sml_nvalues
1991 ? mlist->sml_nvalues : mlist->sml_values;
1992 bv->bv_val != NULL; bv++ )
1994 if ( ! access_allowed( op, e,
1995 mlist->sml_desc, bv,
1996 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2005 case LDAP_MOD_DELETE:
2006 if ( mlist->sml_values == NULL ) {
2007 if ( ! access_allowed( op, e,
2008 mlist->sml_desc, NULL,
2009 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2017 for ( bv = mlist->sml_nvalues
2018 ? mlist->sml_nvalues : mlist->sml_values;
2019 bv->bv_val != NULL; bv++ )
2021 if ( ! access_allowed( op, e,
2022 mlist->sml_desc, bv,
2023 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2032 case SLAP_MOD_SOFTADD:
2033 /* allow adding attribute via modrdn thru */
2045 if (be_null) op->o_bd = NULL;
2051 struct berval *list,
2064 while ( len >= 0 && --ix >= 0 ) {
2065 while ( --len >= 0 && *p++ != sep )
2068 while ( len >= 0 && *p == ' ' ) {
2081 while ( --len >= 0 && *p != sep ) {
2085 while ( bv->bv_len > 0 && *--p == ' ' ) {
2092 typedef struct acl_set_gather_t {
2098 acl_set_cb_gather( Operation *op, SlapReply *rs )
2100 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2102 if ( rs->sr_type == REP_SEARCH ) {
2103 BerValue bvals[ 2 ];
2104 BerVarray bvalsp = NULL;
2107 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2108 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2110 if ( desc == NULL ) {
2114 if ( desc == slap_schema.si_ad_entryDN ) {
2116 bvals[ 0 ] = rs->sr_entry->e_nname;
2117 BER_BVZERO( &bvals[ 1 ] );
2122 a = attr_find( rs->sr_entry->e_attrs, desc );
2124 bvalsp = a->a_nvals;
2129 p->bvals = slap_set_join( p->cookie, p->bvals,
2130 ( '|' | SLAP_SET_RREF ), bvalsp );
2135 assert( rs->sr_type == REP_RESULT );
2142 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2144 AclSetCookie *cp = (AclSetCookie *)cookie;
2146 LDAPURLDesc *ludp = NULL;
2147 Operation op2 = { 0 };
2148 SlapReply rs = {REP_RESULT};
2149 AttributeName anlist[ 2 ], *anlistp = NULL;
2151 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2152 acl_set_gather_t p = { 0 };
2154 /* this routine needs to return the bervals instead of
2155 * plain strings, since syntax is not known. It should
2156 * also return the syntax or some "comparison cookie".
2158 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2159 return acl_set_gather2( cookie, name, desc );
2162 rc = ldap_url_parse( name->bv_val, &ludp );
2163 if ( rc != LDAP_URL_SUCCESS ) {
2164 Debug( LDAP_DEBUG_TRACE,
2165 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2166 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2168 rc = LDAP_PROTOCOL_ERROR;
2172 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2174 /* host part must be empty */
2175 /* extensions parts must be empty */
2176 Debug( LDAP_DEBUG_TRACE,
2177 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2178 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2180 rc = LDAP_PROTOCOL_ERROR;
2184 /* Grab the searchbase and see if an appropriate database can be found */
2185 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2186 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2187 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2188 BER_BVZERO( &op2.o_req_dn );
2189 if ( rc != LDAP_SUCCESS ) {
2190 Debug( LDAP_DEBUG_TRACE,
2191 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2192 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2197 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2198 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2199 Debug( LDAP_DEBUG_TRACE,
2200 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2201 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2203 rc = LDAP_NO_SUCH_OBJECT;
2207 /* Grab the filter */
2208 if ( ludp->lud_filter ) {
2209 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2210 cp->asc_op->o_tmpmemctx );
2211 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2212 if ( op2.ors_filter == NULL ) {
2213 Debug( LDAP_DEBUG_TRACE,
2214 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2215 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2217 rc = LDAP_PROTOCOL_ERROR;
2222 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2223 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2227 /* Grab the scope */
2228 op2.ors_scope = ludp->lud_scope;
2230 /* Grap the attributes */
2231 if ( ludp->lud_attrs ) {
2234 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2237 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2238 cp->asc_op->o_tmpmemctx );
2240 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2242 AttributeDescription *desc = NULL;
2243 const char *text = NULL;
2245 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2246 rc = slap_bv2ad( &name, &desc, &text );
2247 if ( rc == LDAP_SUCCESS ) {
2248 anlistp[ nattrs ].an_name = name;
2249 anlistp[ nattrs ].an_desc = desc;
2258 anlistp[ nattrs ].an_name = desc->ad_cname;
2259 anlistp[ nattrs ].an_desc = desc;
2261 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2265 op2.o_hdr = cp->asc_op->o_hdr;
2266 op2.o_tag = LDAP_REQ_SEARCH;
2267 op2.o_ndn = op2.o_bd->be_rootndn;
2268 op2.o_callback = &cb;
2269 slap_op_time( &op2.o_time, &op2.o_tincr );
2270 op2.o_do_not_cache = 1;
2271 op2.o_is_auth_check = 0;
2272 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2273 op2.ors_slimit = SLAP_NO_LIMIT;
2274 op2.ors_tlimit = SLAP_NO_LIMIT;
2275 op2.ors_attrs = anlistp;
2276 op2.ors_attrsonly = 0;
2277 op2.o_private = cp->asc_op->o_private;
2278 op2.o_extra = cp->asc_op->o_extra;
2282 rc = op2.o_bd->be_search( &op2, &rs );
2288 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2289 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2291 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2292 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2294 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2295 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2298 ldap_free_urldesc( ludp );
2300 if ( anlistp && anlistp != anlist ) {
2301 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2308 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2310 AclSetCookie *cp = (AclSetCookie *)cookie;
2311 BerVarray bvals = NULL;
2315 /* this routine needs to return the bervals instead of
2316 * plain strings, since syntax is not known. It should
2317 * also return the syntax or some "comparison cookie".
2319 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2320 if ( rc == LDAP_SUCCESS ) {
2321 if ( desc == slap_schema.si_ad_entryDN ) {
2322 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2323 cp->asc_op->o_tmpmemctx );
2325 BER_BVZERO( &bvals[ 1 ] );
2329 backend_attribute( cp->asc_op,
2330 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2333 if ( !BER_BVISNULL( &ndn ) ) {
2334 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2343 struct berval *subj,
2346 struct berval *default_set_attribute )
2348 struct berval set = BER_BVNULL;
2350 AclSetCookie cookie;
2352 if ( default_set_attribute == NULL ) {
2356 struct berval subjdn, ndn = BER_BVNULL;
2357 struct berval setat;
2358 BerVarray bvals = NULL;
2360 AttributeDescription *desc = NULL;
2362 /* format of string is "entry/setAttrName" */
2363 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2367 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2368 setat = *default_set_attribute;
2372 * NOTE: dnNormalize honors the ber_len field
2373 * as the length of the dn to be normalized
2375 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2376 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2378 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2379 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2383 BER_BVZERO( &bvals[0] );
2384 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2386 bvals[0].bv_val = bvals[i-1].bv_val;
2387 BER_BVZERO( &bvals[i-1] );
2389 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2390 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2395 if ( !BER_BVISNULL( &set ) ) {
2398 rc = ( slap_set_filter(
2400 (SetCookie *)&cookie, &set,
2401 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2402 if ( set.bv_val != subj->bv_val ) {
2403 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2413 * dynamic ACL infrastructure
2415 static slap_dynacl_t *da_list = NULL;
2418 slap_dynacl_register( slap_dynacl_t *da )
2422 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2423 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2428 if ( tmp != NULL ) {
2432 if ( da->da_mask == NULL ) {
2436 da->da_private = NULL;
2437 da->da_next = da_list;
2443 static slap_dynacl_t *
2444 slap_dynacl_next( slap_dynacl_t *da )
2453 slap_dynacl_get( const char *name )
2457 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2458 if ( strcasecmp( da->da_name, name ) == 0 ) {
2465 #endif /* SLAP_DYNACL */
2468 * statically built-in dynamic ACL initialization
2470 static int (*acl_init_func[])( void ) = {
2472 /* TODO: remove when ACI will only be dynamic */
2473 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2475 #endif /* SLAPD_ACI_ENABLED */
2476 #endif /* SLAP_DYNACL */
2486 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2487 rc = (*(acl_init_func[ i ]))();
2500 struct berval *dn_matches,
2501 struct berval *val_matches,
2502 AclRegexMatches *matches)
2508 enum { DN_FLAG, VAL_FLAG } tflag;
2511 bv->bv_val[0] = '\0';
2512 bv->bv_len--; /* leave space for lone $ */
2516 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2517 sp < pat->bv_val + pat->bv_len ; sp++ )
2519 /* did we previously see a $ */
2521 if ( flag == 1 && *sp == '$' ) {
2527 } else if ( flag == 2 && *sp == 'v' /*'}'*/) {
2530 } else if ( flag == 2 && *sp == 'd' /*'}'*/) {
2533 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2536 } else if ( *sp >= '0' && *sp <= '9' ) {
2547 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2548 if ( *sp >= '0' && *sp <= '9' ) {
2549 n = 10*n + ( *sp - '0' );
2553 if ( *sp != /*'{'*/ '}' ) {
2561 nm = matches->dn_count;
2562 m = matches->dn_data;
2563 data = dn_matches ? dn_matches->bv_val : NULL;
2566 nm = matches->val_count;
2567 m = matches->val_data;
2568 data = val_matches ? val_matches->bv_val : NULL;
2577 if ( data == NULL ) {
2586 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2605 /* must have ended with a single $ */
2613 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2614 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2621 struct berval *pat, /* pattern to expand and match against */
2622 char *str, /* string to match against pattern */
2623 struct berval *dn_matches, /* buffer with $N expansion variables from DN */
2624 struct berval *val_matches, /* buffer with $N expansion variables from val */
2625 AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
2629 char newbuf[ACL_BUF_SIZE];
2633 bv.bv_len = sizeof( newbuf ) - 1;
2640 acl_string_expand( &bv, pat, dn_matches, val_matches, matches );
2641 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2643 char error[ACL_BUF_SIZE];
2644 regerror( rc, &re, error, sizeof( error ) );
2646 Debug( LDAP_DEBUG_TRACE,
2647 "compile( \"%s\", \"%s\") failed %s\n",
2648 pat->bv_val, str, error );
2652 rc = regexec( &re, str, 0, NULL, 0 );
2655 Debug( LDAP_DEBUG_TRACE,
2656 "=> regex_matches: string: %s\n", str, 0, 0 );
2657 Debug( LDAP_DEBUG_TRACE,
2658 "=> regex_matches: rc: %d %s\n",
2659 rc, !rc ? "matches" : "no matches", 0 );