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-2010 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_present )
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_present )
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_present ) {
619 state->as_vd_acl_present = 1;
620 state->as_vd_acl = prev;
621 state->as_vd_acl_count = *count - 1;
622 ACL_PRIV_ASSIGN ( state->as_vd_mask, *mask );
625 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
626 Debug( LDAP_DEBUG_ACL,
627 "acl_get: valpat %s\n",
628 a->acl_attrval.bv_val, 0, 0 );
629 if ( regexec ( &a->acl_attrval_re,
632 matches->val_data, 0 ) )
640 Debug( LDAP_DEBUG_ACL,
642 a->acl_attrval.bv_val, 0, 0 );
644 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
645 if (value_match( &match, desc,
646 a->acl_attrval_mr, 0,
647 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
652 ber_len_t patlen, vdnlen;
654 patlen = a->acl_attrval.bv_len;
655 vdnlen = val->bv_len;
657 if ( vdnlen < patlen )
660 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
661 if ( vdnlen > patlen )
664 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
665 ber_len_t rdnlen = 0;
667 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
670 rdnlen = dn_rdnlen( NULL, val );
671 if ( rdnlen + patlen + 1 != vdnlen )
674 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
675 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
678 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
679 if ( vdnlen <= patlen )
682 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
686 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
692 if ( a->acl_filter != NULL ) {
693 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
694 if ( rc != LDAP_COMPARE_TRUE ) {
699 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
704 if ( !state->as_fe_done ) {
705 state->as_fe_done = 1;
706 a = frontendDB->be_acl;
710 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
715 * Record value-dependent access control state
717 #define ACL_RECORD_VALUE_STATE do { \
718 if( state && !state->as_vd_acl_present ) { \
719 state->as_vd_acl_present = 1; \
720 state->as_vd_acl = a; \
721 state->as_vd_acl_count = count; \
722 ACL_PRIV_ASSIGN( state->as_vd_mask, *mask ); \
732 AclRegexMatches *matches,
734 struct berval *opndn )
737 * if access applies to the entry itself, and the
738 * user is bound as somebody in the same namespace as
739 * the entry, OR the given dn matches the dn pattern
742 * NOTE: styles "anonymous", "users" and "self"
743 * have been moved to enum slap_style_t, whose
744 * value is set in a_dn_style; however, the string
745 * is maintained in a_dn_pat.
748 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
749 if ( !BER_BVISEMPTY( opndn ) ) {
753 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
754 if ( BER_BVISEMPTY( opndn ) ) {
758 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
759 struct berval ndn, selfndn;
762 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
766 level = bdn->a_self_level;
774 selfndn = e->e_nname;
777 for ( ; level > 0; level-- ) {
778 if ( BER_BVISEMPTY( &ndn ) ) {
781 dnParent( &ndn, &ndn );
784 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
789 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
790 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
791 AclRegexMatches tmp_matches,
792 *tmp_matchesp = &tmp_matches;
794 regmatch_t *tmp_data;
796 MATCHES_MEMSET( &tmp_matches );
797 tmp_data = &tmp_matches.dn_data[0];
799 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
800 tmp_matchesp = matches;
801 else switch ( a->acl_dn_style ) {
802 case ACL_STYLE_REGEX:
803 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
804 tmp_matchesp = matches;
807 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
810 tmp_data[0].rm_so = 0;
811 tmp_data[0].rm_eo = e->e_nname.bv_len;
812 tmp_matches.dn_count = 1;
816 case ACL_STYLE_SUBTREE:
817 case ACL_STYLE_CHILDREN:
818 tmp_data[0].rm_so = 0;
819 tmp_data[0].rm_eo = e->e_nname.bv_len;
820 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
821 tmp_data[1].rm_eo = e->e_nname.bv_len;
822 tmp_matches.dn_count = 2;
835 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
836 &e->e_nname, NULL, tmp_matchesp ) )
844 ber_len_t patlen, odnlen;
847 if ( e->e_dn == NULL )
850 if ( bdn->a_expand ) {
852 char buf[ACL_BUF_SIZE];
854 AclRegexMatches tmp_matches,
855 *tmp_matchesp = &tmp_matches;
857 regmatch_t *tmp_data;
859 MATCHES_MEMSET( &tmp_matches );
860 tmp_data = &tmp_matches.dn_data[0];
862 bv.bv_len = sizeof( buf ) - 1;
865 /* Expand value regex */
866 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
867 tmp_matchesp = matches;
868 else switch ( a->acl_dn_style ) {
869 case ACL_STYLE_REGEX:
870 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
871 tmp_matchesp = matches;
874 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
877 tmp_data[0].rm_so = 0;
878 tmp_data[0].rm_eo = e->e_nname.bv_len;
879 tmp_matches.dn_count = 1;
883 case ACL_STYLE_SUBTREE:
884 case ACL_STYLE_CHILDREN:
885 tmp_data[0].rm_so = 0;
886 tmp_data[0].rm_eo = e->e_nname.bv_len;
887 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
888 tmp_data[1].rm_eo = e->e_nname.bv_len;
889 tmp_matches.dn_count = 2;
902 if ( acl_string_expand( &bv, &bdn->a_pat,
904 val, tmp_matchesp ) )
909 if ( dnNormalize(0, NULL, NULL, &bv,
910 &pat, op->o_tmpmemctx )
913 /* did not expand to a valid dn */
922 odnlen = opndn->bv_len;
923 if ( odnlen < patlen ) {
924 goto dn_match_cleanup;
928 if ( bdn->a_style == ACL_STYLE_BASE ) {
929 /* base dn -- entire object DN must match */
930 if ( odnlen != patlen ) {
931 goto dn_match_cleanup;
934 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
935 ber_len_t rdnlen = 0;
937 if ( odnlen <= patlen ) {
938 goto dn_match_cleanup;
941 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
942 goto dn_match_cleanup;
945 rdnlen = dn_rdnlen( NULL, opndn );
946 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
947 goto dn_match_cleanup;
950 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
951 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
952 goto dn_match_cleanup;
955 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
956 if ( odnlen <= patlen ) {
957 goto dn_match_cleanup;
960 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
961 goto dn_match_cleanup;
964 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
965 int level = bdn->a_level;
968 if ( odnlen <= patlen ) {
969 goto dn_match_cleanup;
972 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
974 goto dn_match_cleanup;
978 for ( ; level > 0; level-- ) {
979 if ( BER_BVISEMPTY( &ndn ) ) {
980 goto dn_match_cleanup;
982 dnParent( &ndn, &ndn );
983 if ( ndn.bv_len < patlen ) {
984 goto dn_match_cleanup;
988 if ( ndn.bv_len != patlen ) {
989 goto dn_match_cleanup;
993 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
996 if ( pat.bv_val != bdn->a_pat.bv_val ) {
997 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1015 AccessControlState *state,
1017 slap_dn_access *bdn,
1018 struct berval *opndn )
1024 const char *attr = bdn->a_at->ad_cname.bv_val;
1026 assert( attr != NULL );
1028 if ( BER_BVISEMPTY( opndn ) ) {
1032 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1035 /* see if asker is listed in dnattr */
1036 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1038 at = attrs_find( at->a_next, bdn->a_at ) )
1040 if ( attr_valfind( at,
1041 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1042 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1043 &bv, NULL, op->o_tmpmemctx ) == 0 )
1052 /* have a dnattr match. if this is a self clause then
1053 * the target must also match the op dn.
1055 if ( bdn->a_self ) {
1056 /* check if the target is an attribute. */
1057 if ( val == NULL ) return 1;
1059 /* target is attribute, check if the attribute value
1062 rc = value_match( &match, bdn->a_at,
1063 bdn->a_at->ad_type->sat_equality, 0,
1065 /* on match error or no match, fail the ACL clause */
1066 if ( rc != LDAP_SUCCESS || match != 0 )
1071 /* no dnattr match, check if this is a self clause */
1072 if ( ! bdn->a_self )
1075 ACL_RECORD_VALUE_STATE;
1077 /* this is a self clause, check if the target is an
1083 /* target is attribute, check if the attribute value
1086 rc = value_match( &match, bdn->a_at,
1087 bdn->a_at->ad_type->sat_equality, 0,
1090 /* on match error or no match, fail the ACL clause */
1091 if ( rc != LDAP_SUCCESS || match != 0 )
1100 * slap_acl_mask - modifies mask based upon the given acl and the
1101 * requested access to entry e, attribute attr, value val. if val
1102 * is null, access to the whole attribute is assumed (all values).
1104 * returns 0 access NOT allowed
1108 static slap_control_t
1114 AttributeDescription *desc,
1116 AclRegexMatches *matches,
1118 AccessControlState *state,
1119 slap_access_t access )
1124 char accessmaskbuf[ACCESSMASK_MAXLEN];
1128 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1129 #endif /* SLAP_DYNACL */
1131 assert( a != NULL );
1132 assert( mask != NULL );
1133 assert( desc != NULL );
1135 attr = desc->ad_cname.bv_val;
1137 assert( attr != NULL );
1139 Debug( LDAP_DEBUG_ACL,
1140 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1143 Debug( LDAP_DEBUG_ACL,
1144 "=> acl_mask: to %s by \"%s\", (%s) \n",
1145 val ? "value" : "all values",
1146 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1147 accessmask2str( *mask, accessmaskbuf, 1 ) );
1153 for ( ; b != NULL; b = b->a_next, i++ ) {
1154 slap_mask_t oldmask, modmask;
1156 ACL_INVALIDATE( modmask );
1158 /* AND <who> clauses */
1159 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1160 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1161 b->a_dn_pat.bv_val, 0, 0);
1163 * if access applies to the entry itself, and the
1164 * user is bound as somebody in the same namespace as
1165 * the entry, OR the given dn matches the dn pattern
1168 * NOTE: styles "anonymous", "users" and "self"
1169 * have been moved to enum slap_style_t, whose
1170 * value is set in a_dn_style; however, the string
1171 * is maintained in a_dn_pat.
1174 if ( acl_mask_dn( op, e, val, a, matches,
1175 &b->a_dn, &op->o_ndn ) )
1181 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1184 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1185 b->a_realdn_pat.bv_val, 0, 0);
1187 * if access applies to the entry itself, and the
1188 * user is bound as somebody in the same namespace as
1189 * the entry, OR the given dn matches the dn pattern
1192 * NOTE: styles "anonymous", "users" and "self"
1193 * have been moved to enum slap_style_t, whose
1194 * value is set in a_dn_style; however, the string
1195 * is maintained in a_dn_pat.
1198 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1200 ndn = op->o_conn->c_ndn;
1205 if ( acl_mask_dn( op, e, val, a, matches,
1206 &b->a_realdn, &ndn ) )
1212 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1213 if ( ! op->o_conn->c_listener ) {
1216 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1217 b->a_sockurl_pat.bv_val, 0, 0 );
1219 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1220 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1221 if ( !regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1222 &e->e_nname, val, matches ) )
1227 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1229 char buf[ACL_BUF_SIZE];
1231 bv.bv_len = sizeof( buf ) - 1;
1233 if ( acl_string_expand( &bv, &b->a_sockurl_pat, &e->e_nname, val, matches ) )
1238 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1244 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1252 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1253 if ( !op->o_conn->c_peer_domain.bv_val ) {
1256 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1257 b->a_domain_pat.bv_val, 0, 0 );
1258 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1259 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1260 if ( !regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1261 &e->e_nname, val, matches ) )
1266 char buf[ACL_BUF_SIZE];
1268 struct berval cmp = op->o_conn->c_peer_domain;
1269 struct berval pat = b->a_domain_pat;
1271 if ( b->a_domain_expand ) {
1274 bv.bv_len = sizeof(buf) - 1;
1277 if ( acl_string_expand(&bv, &b->a_domain_pat, &e->e_nname, val, matches) )
1284 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1285 int offset = cmp.bv_len - pat.bv_len;
1290 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1294 /* trim the domain */
1295 cmp.bv_val = &cmp.bv_val[ offset ];
1296 cmp.bv_len -= offset;
1299 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1306 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1307 if ( !op->o_conn->c_peer_name.bv_val ) {
1310 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1311 b->a_peername_pat.bv_val, 0, 0 );
1312 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1313 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1314 if ( !regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1315 &e->e_nname, val, matches ) )
1321 /* try exact match */
1322 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1323 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1327 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1329 char buf[ACL_BUF_SIZE];
1331 bv.bv_len = sizeof( buf ) - 1;
1333 if ( acl_string_expand( &bv, &b->a_peername_pat, &e->e_nname, val, matches ) )
1338 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1342 /* extract IP and try exact match */
1343 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1345 char buf[STRLENOF("255.255.255.255") + 1];
1348 int port_number = -1;
1350 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1351 acl_bv_ip_eq.bv_val,
1352 acl_bv_ip_eq.bv_len ) != 0 )
1355 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1356 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1358 port = strrchr( ip.bv_val, ':' );
1360 ip.bv_len = port - ip.bv_val;
1362 if ( lutil_atoi( &port_number, port ) != 0 )
1366 /* the port check can be anticipated here */
1367 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1370 /* address longer than expected? */
1371 if ( ip.bv_len >= sizeof(buf) )
1374 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1375 buf[ ip.bv_len ] = '\0';
1377 addr = inet_addr( buf );
1379 /* unable to convert? */
1380 if ( addr == (unsigned long)(-1) )
1383 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1386 #ifdef LDAP_PF_INET6
1387 /* extract IPv6 and try exact match */
1388 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1390 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1392 struct in6_addr addr;
1393 int port_number = -1;
1395 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1396 acl_bv_ipv6_eq.bv_val,
1397 acl_bv_ipv6_eq.bv_len ) != 0 )
1400 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1401 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1403 port = strrchr( ip.bv_val, ']' );
1405 ip.bv_len = port - ip.bv_val;
1407 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1411 /* the port check can be anticipated here */
1412 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1415 /* address longer than expected? */
1416 if ( ip.bv_len >= sizeof(buf) )
1419 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1420 buf[ ip.bv_len ] = '\0';
1422 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1426 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1428 #endif /* LDAP_PF_INET6 */
1430 #ifdef LDAP_PF_LOCAL
1431 /* extract path and try exact match */
1432 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1435 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1436 acl_bv_path_eq.bv_val,
1437 acl_bv_path_eq.bv_len ) != 0 )
1440 path.bv_val = op->o_conn->c_peer_name.bv_val
1441 + acl_bv_path_eq.bv_len;
1442 path.bv_len = op->o_conn->c_peer_name.bv_len
1443 - acl_bv_path_eq.bv_len;
1445 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1448 #endif /* LDAP_PF_LOCAL */
1450 /* exact match (very unlikely...) */
1451 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1458 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1459 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1462 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1463 b->a_sockname_pat.bv_val, 0, 0 );
1464 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1465 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1466 if ( !regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1467 &e->e_nname, val, matches ) )
1472 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1474 char buf[ACL_BUF_SIZE];
1476 bv.bv_len = sizeof( buf ) - 1;
1478 if ( acl_string_expand( &bv, &b->a_sockname_pat, &e->e_nname, val, matches ) )
1483 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1488 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1495 if ( b->a_dn_at != NULL ) {
1496 if ( acl_mask_dnattr( op, e, val, a,
1498 &b->a_dn, &op->o_ndn ) )
1504 if ( b->a_realdn_at != NULL ) {
1507 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1509 ndn = op->o_conn->c_ndn;
1514 if ( acl_mask_dnattr( op, e, val, a,
1516 &b->a_realdn, &ndn ) )
1522 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1524 struct berval ndn = BER_BVNULL;
1527 if ( op->o_ndn.bv_len == 0 ) {
1531 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1532 b->a_group_pat.bv_val, 0, 0 );
1534 /* b->a_group is an unexpanded entry name, expanded it should be an
1535 * entry with objectclass group* and we test to see if odn is one of
1536 * the values in the attribute group
1538 /* see if asker is listed in dnattr */
1539 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1540 char buf[ACL_BUF_SIZE];
1541 AclRegexMatches tmp_matches,
1542 *tmp_matchesp = &tmp_matches;
1543 regmatch_t *tmp_data;
1545 MATCHES_MEMSET( &tmp_matches );
1546 tmp_data = &tmp_matches.dn_data[0];
1548 bv.bv_len = sizeof(buf) - 1;
1553 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1554 tmp_matchesp = matches;
1555 else switch ( a->acl_dn_style ) {
1556 case ACL_STYLE_REGEX:
1557 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1558 tmp_matchesp = matches;
1562 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1563 case ACL_STYLE_BASE:
1564 tmp_data[0].rm_so = 0;
1565 tmp_data[0].rm_eo = e->e_nname.bv_len;
1566 tmp_matches.dn_count = 1;
1570 case ACL_STYLE_SUBTREE:
1571 case ACL_STYLE_CHILDREN:
1572 tmp_data[0].rm_so = 0;
1573 tmp_data[0].rm_eo = e->e_nname.bv_len;
1575 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1576 tmp_data[1].rm_eo = e->e_nname.bv_len;
1577 tmp_matches.dn_count = 2;
1590 if ( acl_string_expand( &bv, &b->a_group_pat,
1597 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1598 op->o_tmpmemctx ) != LDAP_SUCCESS )
1600 /* did not expand to a valid dn */
1607 bv = b->a_group_pat;
1610 rc = backend_group( op, e, &bv, &op->o_ndn,
1611 b->a_group_oc, b->a_group_at );
1614 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1622 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1624 char buf[ACL_BUF_SIZE];
1626 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1627 b->a_set_pat.bv_val, 0, 0 );
1629 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1630 AclRegexMatches tmp_matches,
1631 *tmp_matchesp = &tmp_matches;
1633 regmatch_t *tmp_data;
1635 MATCHES_MEMSET( &tmp_matches );
1636 tmp_data = &tmp_matches.dn_data[0];
1638 bv.bv_len = sizeof( buf ) - 1;
1643 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1644 tmp_matchesp = matches;
1645 else switch ( a->acl_dn_style ) {
1646 case ACL_STYLE_REGEX:
1647 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1648 tmp_matchesp = matches;
1652 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1653 case ACL_STYLE_BASE:
1654 tmp_data[0].rm_so = 0;
1655 tmp_data[0].rm_eo = e->e_nname.bv_len;
1656 tmp_matches.dn_count = 1;
1660 case ACL_STYLE_SUBTREE:
1661 case ACL_STYLE_CHILDREN:
1662 tmp_data[0].rm_so = 0;
1663 tmp_data[0].rm_eo = e->e_nname.bv_len;
1664 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1665 tmp_data[1].rm_eo = e->e_nname.bv_len; tmp_matches.dn_count = 2;
1678 if ( acl_string_expand( &bv, &b->a_set_pat,
1689 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1694 if ( b->a_authz.sai_ssf ) {
1695 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1696 b->a_authz.sai_ssf, op->o_ssf, 0 );
1697 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1702 if ( b->a_authz.sai_transport_ssf ) {
1703 Debug( LDAP_DEBUG_ACL,
1704 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1705 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1706 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1711 if ( b->a_authz.sai_tls_ssf ) {
1712 Debug( LDAP_DEBUG_ACL,
1713 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1714 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1715 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1720 if ( b->a_authz.sai_sasl_ssf ) {
1721 Debug( LDAP_DEBUG_ACL,
1722 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1723 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1724 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1729 /* check for the "self" modifier in the <access> field */
1730 if ( b->a_dn.a_self ) {
1734 ACL_RECORD_VALUE_STATE;
1736 /* must have DN syntax */
1737 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1738 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1740 /* check if the target is an attribute. */
1741 if ( val == NULL ) continue;
1743 /* a DN must be present */
1744 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1748 /* target is attribute, check if the attribute value
1751 rc = value_match( &match, desc,
1752 desc->ad_type->sat_equality, 0,
1753 val, &op->o_ndn, &dummy );
1754 /* on match error or no match, fail the ACL clause */
1755 if ( rc != LDAP_SUCCESS || match != 0 )
1760 if ( b->a_dynacl ) {
1762 slap_access_t tgrant, tdeny;
1764 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1767 /* this case works different from the others above.
1768 * since dynamic ACL's themselves give permissions, we need
1769 * to first check b->a_access_mask, the ACL's access level.
1771 /* first check if the right being requested
1772 * is allowed by the ACL clause.
1774 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1778 /* start out with nothing granted, nothing denied */
1779 ACL_INVALIDATE(tgrant);
1780 ACL_INVALIDATE(tdeny);
1782 for ( da = b->a_dynacl; da; da = da->da_next ) {
1783 slap_access_t grant,
1786 ACL_INVALIDATE(grant);
1787 ACL_INVALIDATE(deny);
1789 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1790 da->da_name, 0, 0 );
1793 * XXXmanu Only DN matches are supplied
1794 * sending attribute values matches require
1797 (void)da->da_mask( da->da_private, op, e, desc,
1798 val, matches->dn_count, matches->dn_data,
1805 /* remove anything that the ACL clause does not allow */
1806 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1807 tdeny &= ACL_PRIV_MASK;
1809 /* see if we have anything to contribute */
1810 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1814 /* this could be improved by changing slap_acl_mask so that it can deal with
1815 * by clauses that return grant/deny pairs. Right now, it does either
1816 * additive or subtractive rights, but not both at the same time. So,
1817 * we need to combine the grant/deny pair into a single rights mask in
1818 * a smart way: if either grant or deny is "empty", then we use the
1819 * opposite as is, otherwise we remove any denied rights from the grant
1820 * rights mask and construct an additive mask.
1822 if (ACL_IS_INVALID(tdeny)) {
1823 modmask = tgrant | ACL_PRIV_ADDITIVE;
1825 } else if (ACL_IS_INVALID(tgrant)) {
1826 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1829 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1833 #endif /* SLAP_DYNACL */
1835 modmask = b->a_access_mask;
1838 Debug( LDAP_DEBUG_ACL,
1839 "<= acl_mask: [%d] applying %s (%s)\n",
1840 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1841 b->a_type == ACL_CONTINUE
1843 : b->a_type == ACL_BREAK
1849 if( ACL_IS_ADDITIVE(modmask) ) {
1851 ACL_PRIV_SET( *mask, modmask );
1854 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1856 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1857 /* substract privs */
1858 ACL_PRIV_CLR( *mask, modmask );
1861 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1868 Debug( LDAP_DEBUG_ACL,
1869 "<= acl_mask: [%d] mask: %s\n",
1870 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1872 if( b->a_type == ACL_CONTINUE ) {
1875 } else if ( b->a_type == ACL_BREAK ) {
1883 /* implicit "by * none" clause */
1886 Debug( LDAP_DEBUG_ACL,
1887 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1888 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1893 * acl_check_modlist - check access control on the given entry to see if
1894 * it allows the given modifications by the user associated with op.
1895 * returns 1 if mods allowed ok
1896 * 0 mods not allowed
1903 Modifications *mlist )
1906 AccessControlState state = ACL_STATE_INIT;
1909 int ret = 1; /* default is access allowed */
1913 be = LDAP_STAILQ_FIRST(&backendDB);
1917 assert( be != NULL );
1919 /* If ADD attribute checking is not enabled, just allow it */
1920 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1923 /* short circuit root database access */
1924 if ( be_isroot( op ) ) {
1925 Debug( LDAP_DEBUG_ACL,
1926 "<= acl_access_allowed: granted to database root\n",
1931 /* use backend default access if no backend acls */
1932 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1933 Debug( LDAP_DEBUG_ACL,
1934 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1935 access2str( ACL_WRITE ),
1936 op->o_bd->be_dfltaccess >= ACL_WRITE
1937 ? "granted" : "denied",
1939 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1943 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1945 * Internal mods are ignored by ACL_WRITE checking
1947 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1948 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1949 " modify access granted\n",
1950 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1955 * no-user-modification operational attributes are ignored
1956 * by ACL_WRITE checking as any found here are not provided
1959 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1960 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1962 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1963 " modify access granted\n",
1964 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1968 switch ( mlist->sml_op ) {
1969 case LDAP_MOD_REPLACE:
1970 case LDAP_MOD_INCREMENT:
1972 * We must check both permission to delete the whole
1973 * attribute and permission to add the specific attributes.
1974 * This prevents abuse from selfwriters.
1976 if ( ! access_allowed( op, e,
1977 mlist->sml_desc, NULL,
1978 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1985 if ( mlist->sml_values == NULL ) break;
1987 /* fall thru to check value to add */
1990 case SLAP_MOD_ADD_IF_NOT_PRESENT:
1991 assert( mlist->sml_values != NULL );
1993 if ( mlist->sml_op == SLAP_MOD_ADD_IF_NOT_PRESENT
1994 && attr_find( e->e_attrs, mlist->sml_desc ) )
1999 for ( bv = mlist->sml_nvalues
2000 ? mlist->sml_nvalues : mlist->sml_values;
2001 bv->bv_val != NULL; bv++ )
2003 if ( ! access_allowed( op, e,
2004 mlist->sml_desc, bv,
2005 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2014 case LDAP_MOD_DELETE:
2015 case SLAP_MOD_SOFTDEL:
2016 if ( mlist->sml_values == NULL ) {
2017 if ( ! access_allowed( op, e,
2018 mlist->sml_desc, NULL,
2019 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2027 for ( bv = mlist->sml_nvalues
2028 ? mlist->sml_nvalues : mlist->sml_values;
2029 bv->bv_val != NULL; bv++ )
2031 if ( ! access_allowed( op, e,
2032 mlist->sml_desc, bv,
2033 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2042 case SLAP_MOD_SOFTADD:
2043 /* allow adding attribute via modrdn thru */
2055 if (be_null) op->o_bd = NULL;
2061 struct berval *list,
2074 while ( len >= 0 && --ix >= 0 ) {
2075 while ( --len >= 0 && *p++ != sep )
2078 while ( len >= 0 && *p == ' ' ) {
2091 while ( --len >= 0 && *p != sep ) {
2095 while ( bv->bv_len > 0 && *--p == ' ' ) {
2102 typedef struct acl_set_gather_t {
2108 acl_set_cb_gather( Operation *op, SlapReply *rs )
2110 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2112 if ( rs->sr_type == REP_SEARCH ) {
2113 BerValue bvals[ 2 ];
2114 BerVarray bvalsp = NULL;
2117 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2118 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2120 if ( desc == NULL ) {
2124 if ( desc == slap_schema.si_ad_entryDN ) {
2126 bvals[ 0 ] = rs->sr_entry->e_nname;
2127 BER_BVZERO( &bvals[ 1 ] );
2132 a = attr_find( rs->sr_entry->e_attrs, desc );
2134 bvalsp = a->a_nvals;
2139 p->bvals = slap_set_join( p->cookie, p->bvals,
2140 ( '|' | SLAP_SET_RREF ), bvalsp );
2145 switch ( rs->sr_type ) {
2147 case REP_INTERMEDIATE:
2152 assert( rs->sr_type == REP_RESULT );
2161 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2163 AclSetCookie *cp = (AclSetCookie *)cookie;
2165 LDAPURLDesc *ludp = NULL;
2166 Operation op2 = { 0 };
2167 SlapReply rs = {REP_RESULT};
2168 AttributeName anlist[ 2 ], *anlistp = NULL;
2170 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2171 acl_set_gather_t p = { 0 };
2173 /* this routine needs to return the bervals instead of
2174 * plain strings, since syntax is not known. It should
2175 * also return the syntax or some "comparison cookie".
2177 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2178 return acl_set_gather2( cookie, name, desc );
2181 rc = ldap_url_parse( name->bv_val, &ludp );
2182 if ( rc != LDAP_URL_SUCCESS ) {
2183 Debug( LDAP_DEBUG_TRACE,
2184 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2185 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2187 rc = LDAP_PROTOCOL_ERROR;
2191 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2193 /* host part must be empty */
2194 /* extensions parts must be empty */
2195 Debug( LDAP_DEBUG_TRACE,
2196 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2197 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2199 rc = LDAP_PROTOCOL_ERROR;
2203 /* Grab the searchbase and see if an appropriate database can be found */
2204 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2205 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2206 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2207 BER_BVZERO( &op2.o_req_dn );
2208 if ( rc != LDAP_SUCCESS ) {
2209 Debug( LDAP_DEBUG_TRACE,
2210 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2211 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2216 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2217 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2218 Debug( LDAP_DEBUG_TRACE,
2219 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2220 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2222 rc = LDAP_NO_SUCH_OBJECT;
2226 /* Grab the filter */
2227 if ( ludp->lud_filter ) {
2228 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2229 cp->asc_op->o_tmpmemctx );
2230 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2231 if ( op2.ors_filter == NULL ) {
2232 Debug( LDAP_DEBUG_TRACE,
2233 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2234 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2236 rc = LDAP_PROTOCOL_ERROR;
2241 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2242 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2246 /* Grab the scope */
2247 op2.ors_scope = ludp->lud_scope;
2249 /* Grap the attributes */
2250 if ( ludp->lud_attrs ) {
2253 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2256 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2257 cp->asc_op->o_tmpmemctx );
2259 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2261 AttributeDescription *desc = NULL;
2262 const char *text = NULL;
2264 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2265 rc = slap_bv2ad( &name, &desc, &text );
2266 if ( rc == LDAP_SUCCESS ) {
2267 anlistp[ nattrs ].an_name = name;
2268 anlistp[ nattrs ].an_desc = desc;
2277 anlistp[ nattrs ].an_name = desc->ad_cname;
2278 anlistp[ nattrs ].an_desc = desc;
2280 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2284 op2.o_hdr = cp->asc_op->o_hdr;
2285 op2.o_tag = LDAP_REQ_SEARCH;
2286 op2.o_ndn = op2.o_bd->be_rootndn;
2287 op2.o_callback = &cb;
2288 slap_op_time( &op2.o_time, &op2.o_tincr );
2289 op2.o_do_not_cache = 1;
2290 op2.o_is_auth_check = 0;
2291 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2292 op2.ors_slimit = SLAP_NO_LIMIT;
2293 op2.ors_tlimit = SLAP_NO_LIMIT;
2294 op2.ors_attrs = anlistp;
2295 op2.ors_attrsonly = 0;
2296 op2.o_private = cp->asc_op->o_private;
2297 op2.o_extra = cp->asc_op->o_extra;
2301 rc = op2.o_bd->be_search( &op2, &rs );
2307 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2308 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2310 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2311 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2313 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2314 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2317 ldap_free_urldesc( ludp );
2319 if ( anlistp && anlistp != anlist ) {
2320 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2327 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2329 AclSetCookie *cp = (AclSetCookie *)cookie;
2330 BerVarray bvals = NULL;
2334 /* this routine needs to return the bervals instead of
2335 * plain strings, since syntax is not known. It should
2336 * also return the syntax or some "comparison cookie".
2338 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2339 if ( rc == LDAP_SUCCESS ) {
2340 if ( desc == slap_schema.si_ad_entryDN ) {
2341 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2342 cp->asc_op->o_tmpmemctx );
2344 BER_BVZERO( &bvals[ 1 ] );
2348 backend_attribute( cp->asc_op,
2349 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2352 if ( !BER_BVISNULL( &ndn ) ) {
2353 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2362 struct berval *subj,
2365 struct berval *default_set_attribute )
2367 struct berval set = BER_BVNULL;
2369 AclSetCookie cookie;
2371 if ( default_set_attribute == NULL ) {
2375 struct berval subjdn, ndn = BER_BVNULL;
2376 struct berval setat;
2377 BerVarray bvals = NULL;
2379 AttributeDescription *desc = NULL;
2381 /* format of string is "entry/setAttrName" */
2382 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2386 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2387 setat = *default_set_attribute;
2391 * NOTE: dnNormalize honors the ber_len field
2392 * as the length of the dn to be normalized
2394 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2395 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2397 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2398 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2402 BER_BVZERO( &bvals[0] );
2403 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2405 bvals[0].bv_val = bvals[i-1].bv_val;
2406 BER_BVZERO( &bvals[i-1] );
2408 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2409 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2414 if ( !BER_BVISNULL( &set ) ) {
2417 rc = ( slap_set_filter(
2419 (SetCookie *)&cookie, &set,
2420 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2421 if ( set.bv_val != subj->bv_val ) {
2422 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2432 * dynamic ACL infrastructure
2434 static slap_dynacl_t *da_list = NULL;
2437 slap_dynacl_register( slap_dynacl_t *da )
2441 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2442 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2447 if ( tmp != NULL ) {
2451 if ( da->da_mask == NULL ) {
2455 da->da_private = NULL;
2456 da->da_next = da_list;
2462 static slap_dynacl_t *
2463 slap_dynacl_next( slap_dynacl_t *da )
2472 slap_dynacl_get( const char *name )
2476 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2477 if ( strcasecmp( da->da_name, name ) == 0 ) {
2484 #endif /* SLAP_DYNACL */
2487 * statically built-in dynamic ACL initialization
2489 static int (*acl_init_func[])( void ) = {
2491 /* TODO: remove when ACI will only be dynamic */
2492 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2494 #endif /* SLAPD_ACI_ENABLED */
2495 #endif /* SLAP_DYNACL */
2505 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2506 rc = (*(acl_init_func[ i ]))();
2519 struct berval *dn_matches,
2520 struct berval *val_matches,
2521 AclRegexMatches *matches)
2527 enum { DN_FLAG, VAL_FLAG } tflag;
2530 bv->bv_val[0] = '\0';
2531 bv->bv_len--; /* leave space for lone $ */
2535 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2536 sp < pat->bv_val + pat->bv_len ; sp++ )
2538 /* did we previously see a $ */
2540 if ( flag == 1 && *sp == '$' ) {
2546 } else if ( flag == 2 && *sp == 'v' /*'}'*/) {
2549 } else if ( flag == 2 && *sp == 'd' /*'}'*/) {
2552 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2555 } else if ( *sp >= '0' && *sp <= '9' ) {
2566 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2567 if ( *sp >= '0' && *sp <= '9' ) {
2568 n = 10*n + ( *sp - '0' );
2572 if ( *sp != /*'{'*/ '}' ) {
2580 nm = matches->dn_count;
2581 m = matches->dn_data;
2582 data = dn_matches ? dn_matches->bv_val : NULL;
2585 nm = matches->val_count;
2586 m = matches->val_data;
2587 data = val_matches ? val_matches->bv_val : NULL;
2596 if ( data == NULL ) {
2605 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2624 /* must have ended with a single $ */
2632 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2633 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2640 struct berval *pat, /* pattern to expand and match against */
2641 char *str, /* string to match against pattern */
2642 struct berval *dn_matches, /* buffer with $N expansion variables from DN */
2643 struct berval *val_matches, /* buffer with $N expansion variables from val */
2644 AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
2648 char newbuf[ACL_BUF_SIZE];
2652 bv.bv_len = sizeof( newbuf ) - 1;
2659 acl_string_expand( &bv, pat, dn_matches, val_matches, matches );
2660 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2662 char error[ACL_BUF_SIZE];
2663 regerror( rc, &re, error, sizeof( error ) );
2665 Debug( LDAP_DEBUG_TRACE,
2666 "compile( \"%s\", \"%s\") failed %s\n",
2667 pat->bv_val, str, error );
2671 rc = regexec( &re, str, 0, NULL, 0 );
2674 Debug( LDAP_DEBUG_TRACE,
2675 "=> regex_matches: string: %s\n", str, 0, 0 );
2676 Debug( LDAP_DEBUG_TRACE,
2677 "=> regex_matches: rc: %d %s\n",
2678 rc, !rc ? "matches" : "no matches", 0 );