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-2008 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,
56 AccessControlState *state );
58 static slap_control_t slap_acl_mask(
59 AccessControl *ac, slap_mask_t *mask,
60 Operation *op, Entry *e,
61 AttributeDescription *desc,
63 AclRegexMatches *matches,
65 AccessControlState *state,
66 slap_access_t access );
68 static int regex_matches(
69 struct berval *pat, char *str, char *buf,
70 AclRegexMatches *matches);
72 typedef struct AclSetCookie {
74 #define asc_op asc_cookie.set_op
79 SLAP_SET_GATHER acl_set_gather;
80 SLAP_SET_GATHER acl_set_gather2;
83 * access_allowed - check whether op->o_ndn is allowed the requested access
84 * to entry e, attribute attr, value val. if val is null, access to
85 * the whole attribute is assumed (all values).
87 * This routine loops through all access controls and calls
88 * slap_acl_mask() on each applicable access control.
89 * The loop exits when a definitive answer is reached or
90 * or no more controls remain.
97 * - can be legally called with op == NULL
98 * - can be legally called with op->o_bd == NULL
102 slap_access_always_allowed(
105 AttributeDescription *desc,
107 slap_access_t access,
108 AccessControlState *state,
111 assert( maskp != NULL );
114 ACL_LVL_ASSIGN_MANAGE( *maskp );
119 #define MATCHES_DNMAXCOUNT(m) \
120 ( sizeof ( (m)->dn_data ) / sizeof( *(m)->dn_data ) )
121 #define MATCHES_VALMAXCOUNT(m) \
122 ( sizeof ( (m)->val_data ) / sizeof( *(m)->val_data ) )
123 #define MATCHES_MEMSET(m) do { \
124 memset( (m)->dn_data, '\0', sizeof( (m)->dn_data ) ); \
125 memset( (m)->val_data, '\0', sizeof( (m)->val_data ) ); \
126 (m)->dn_count = MATCHES_DNMAXCOUNT( (m) ); \
127 (m)->val_count = MATCHES_VALMAXCOUNT( (m) ); \
128 } while ( 0 /* CONSTCOND */ )
134 AttributeDescription *desc,
136 slap_access_t access,
137 AccessControlState *state,
142 AccessControl *a = NULL;
145 char accessmaskbuf[ACCESSMASK_MAXLEN];
148 slap_control_t control;
149 slap_access_t access_level;
151 AclRegexMatches matches;
152 AccessControlState acl_state = ACL_STATE_INIT;
154 assert( op != NULL );
156 assert( desc != NULL );
157 assert( maskp != NULL );
159 access_level = ACL_LEVEL( access );
160 attr = desc->ad_cname.bv_val;
162 assert( attr != NULL );
166 /* grant database root access */
167 if ( be_isroot( op ) ) {
168 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
169 mask = ACL_LVL_MANAGE;
174 * no-user-modification operational attributes are ignored
175 * by ACL_WRITE checking as any found here are not provided
178 * NOTE: but they are not ignored for ACL_MANAGE, because
179 * if we get here it means a non-root user is trying to
180 * manage data, so we need to check its privileges.
182 if ( access_level == ACL_WRITE
183 && is_at_no_user_mod( desc->ad_type )
184 && desc != slap_schema.si_ad_entry
185 && desc != slap_schema.si_ad_children )
187 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
188 " %s access granted\n",
193 /* use backend default access if no backend acls */
194 if ( op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
197 Debug( LDAP_DEBUG_ACL,
198 "=> slap_access_allowed: backend default %s "
199 "access %s to \"%s\"\n",
200 access2str( access ),
201 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
202 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
203 ret = op->o_bd->be_dfltaccess >= access_level;
205 mask = ACL_PRIV_LEVEL;
206 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
207 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
218 if ( state->as_vd_ad == desc ) {
219 a = state->as_vd_acl;
220 count = state->as_vd_acl_count;
221 if ( state->as_fe_done )
224 state->as_vi_acl = NULL;
230 state->as_fe_done = 0;
232 ACL_PRIV_ASSIGN( mask, *maskp );
233 MATCHES_MEMSET( &matches );
235 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
236 &matches, state ) ) != NULL )
239 int dnmaxcount = MATCHES_DNMAXCOUNT( &matches );
240 int valmaxcount = MATCHES_VALMAXCOUNT( &matches );
241 regmatch_t *dn_data = matches.dn_data;
242 regmatch_t *val_data = matches.val_data;
245 for ( i = 0; i < dnmaxcount && dn_data[i].rm_eo > 0; i++ ) {
246 char *data = e->e_ndn;
248 Debug( LDAP_DEBUG_ACL, "=> match[dn%d]: %d %d ", i,
249 (int)dn_data[i].rm_so,
250 (int)dn_data[i].rm_eo );
251 if ( dn_data[i].rm_so <= dn_data[0].rm_eo ) {
253 for ( n = dn_data[i].rm_so;
254 n < dn_data[i].rm_eo; n++ ) {
255 Debug( LDAP_DEBUG_ACL, "%c",
259 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
263 for ( i = 0; i < valmaxcount && val_data[i].rm_eo > 0; i++ ) {
264 char *data = val->bv_val;
266 Debug( LDAP_DEBUG_ACL, "=> match[val%d]: %d %d ", i,
267 (int)val_data[i].rm_so,
268 (int)val_data[i].rm_eo );
269 if ( val_data[i].rm_so <= val_data[0].rm_eo ) {
271 for ( n = val_data[i].rm_so;
272 n < val_data[i].rm_eo; n++ ) {
273 Debug( LDAP_DEBUG_ACL, "%c",
277 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
281 if ( state->as_vi_acl == a &&
282 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
284 Debug( LDAP_DEBUG_ACL,
285 "=> slap_access_allowed: result was in cache (%s)\n",
287 ret = state->as_result;
290 Debug( LDAP_DEBUG_ACL,
291 "=> slap_access_allowed: result not in cache (%s)\n",
296 control = slap_acl_mask( a, &mask, op,
297 e, desc, val, &matches, count, state, access );
299 if ( control != ACL_BREAK ) {
303 MATCHES_MEMSET( &matches );
306 if ( ACL_IS_INVALID( mask ) ) {
307 Debug( LDAP_DEBUG_ACL,
308 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
310 ACL_PRIV_ASSIGN( mask, *maskp );
312 } else if ( control == ACL_BREAK ) {
313 Debug( LDAP_DEBUG_ACL,
314 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
319 ret = ACL_GRANT( mask, access );
321 Debug( LDAP_DEBUG_ACL,
322 "=> slap_access_allowed: %s access %s by %s\n",
323 access2str( access ), ret ? "granted" : "denied",
324 accessmask2str( mask, accessmaskbuf, 1 ) );
327 ACL_PRIV_ASSIGN( *maskp, mask );
335 AttributeDescription *desc,
337 slap_access_t access,
338 AccessControlState *state,
345 * NOTE: control gets here if FIXME
346 * if an appropriate backend cannot be selected for the operation,
347 * we assume that the frontend should handle this
348 * FIXME: should select_backend() take care of this,
349 * and return frontendDB instead of NULL? maybe for some value
354 if ( op->o_bd == NULL ) {
355 op->o_bd = select_backend( &op->o_req_ndn, 0 );
356 if ( op->o_bd == NULL )
357 op->o_bd = frontendDB;
359 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
369 AttributeDescription *desc,
371 slap_access_t access,
372 AccessControlState *state,
376 AccessControl *a = NULL;
380 char accessmaskbuf[ACCESSMASK_MAXLEN];
383 slap_access_t access_level;
385 static AccessControlState state_init = ACL_STATE_INIT;
388 assert( desc != NULL );
390 access_level = ACL_LEVEL( access );
392 assert( access_level > ACL_NONE );
395 if ( maskp ) ACL_INVALIDATE( *maskp );
397 attr = desc->ad_cname.bv_val;
399 assert( attr != NULL );
402 if ( op->o_acl_priv != ACL_NONE ) {
403 access = op->o_acl_priv;
405 } else if ( op->o_is_auth_check &&
406 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
410 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
411 desc == slap_schema.si_ad_entry )
418 if ( state->as_vd_ad == desc ) {
419 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
422 return state->as_result;
430 Debug( LDAP_DEBUG_ACL,
431 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
432 access2str( access ), e->e_dn, attr );
439 if ( op->o_bd == NULL ) {
440 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
443 /* FIXME: experimental; use first backend rules
444 * iff there is no global_acl (ITS#3100)
446 if ( frontendDB->be_acl != NULL ) {
447 op->o_bd = frontendDB;
450 assert( op->o_bd != NULL );
452 /* this is enforced in backend_add() */
453 if ( op->o_bd->bd_info->bi_access_allowed ) {
454 /* delegate to backend */
455 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
456 desc, val, access, state, &mask );
459 /* use default (but pass through frontend
460 * for global ACL overlays) */
461 ret = frontendDB->bd_info->bi_access_allowed( op, e,
462 desc, val, access, state, &mask );
466 if ( ACL_IS_INVALID( mask ) ) {
467 Debug( LDAP_DEBUG_ACL,
468 "=> access_allowed: \"%s\" (%s) invalid!\n",
473 Debug( LDAP_DEBUG_ACL,
474 "=> access_allowed: no more rules\n", 0, 0, 0 );
480 Debug( LDAP_DEBUG_ACL,
481 "=> access_allowed: %s access %s by %s\n",
482 access2str( access ), ret ? "granted" : "denied",
483 accessmask2str( mask, accessmaskbuf, 1 ) );
486 if ( state != NULL ) {
487 /* If not value-dependent, save ACL in case of more attrs */
488 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
489 state->as_vi_acl = a;
490 state->as_result = ret;
492 state->as_recorded |= ACL_STATE_RECORDED;
493 state->as_vd_ad = desc;
495 if ( be_null ) op->o_bd = NULL;
496 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
502 * slap_acl_get - return the acl applicable to entry e, attribute
503 * attr. the acl returned is suitable for use in subsequent calls to
504 * acl_access_allowed().
507 static AccessControl *
513 AttributeDescription *desc,
515 AclRegexMatches *matches,
516 AccessControlState *state )
523 assert( count != NULL );
524 assert( desc != NULL );
525 assert( state != NULL );
527 attr = desc->ad_cname.bv_val;
529 assert( attr != NULL );
532 if( op->o_bd == NULL || op->o_bd->be_acl == NULL ) {
533 a = frontendDB->be_acl;
535 a = op->o_bd->be_acl;
540 if ( a == frontendDB->be_acl )
541 state->as_fe_done = 1;
547 dnlen = e->e_nname.bv_len;
550 for ( ; a != NULL; prev = a, a = a->acl_next ) {
553 if ( a != frontendDB->be_acl && state->as_fe_done )
556 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
557 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
558 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
559 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
560 if ( regexec ( &a->acl_dn_re,
563 matches->dn_data, 0 ) )
569 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
570 *count, a->acl_dn_pat.bv_val, 0 );
571 patlen = a->acl_dn_pat.bv_len;
572 if ( dnlen < patlen )
575 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
576 /* base dn -- entire object DN must match */
577 if ( dnlen != patlen )
580 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
581 ber_len_t rdnlen = 0;
584 if ( dnlen <= patlen )
588 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
593 rdnlen = dn_rdnlen( NULL, &e->e_nname );
594 if ( rdnlen + patlen + sep != dnlen )
597 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
598 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
601 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
602 if ( dnlen <= patlen )
604 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
608 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
612 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
616 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
617 matches->dn_data[0].rm_so = -1;
618 matches->dn_data[0].rm_eo = -1;
619 matches->val_data[0].rm_so = -1;
620 matches->val_data[0].rm_eo = -1;
624 /* Is this ACL only for a specific value? */
625 if ( a->acl_attrval.bv_len ) {
630 if( !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
631 state->as_recorded |= ACL_STATE_RECORDED_VD;
632 state->as_vd_acl = prev;
633 state->as_vd_acl_count = *count - 1;
636 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
637 Debug( LDAP_DEBUG_ACL,
638 "acl_get: valpat %s\n",
639 a->acl_attrval.bv_val, 0, 0 );
640 if ( regexec ( &a->acl_attrval_re,
643 matches->val_data, 0 ) )
651 Debug( LDAP_DEBUG_ACL,
653 a->acl_attrval.bv_val, 0, 0 );
655 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
656 if (value_match( &match, desc,
657 a->acl_attrval_mr, 0,
658 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
663 ber_len_t patlen, vdnlen;
665 patlen = a->acl_attrval.bv_len;
666 vdnlen = val->bv_len;
668 if ( vdnlen < patlen )
671 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
672 if ( vdnlen > patlen )
675 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
676 ber_len_t rdnlen = 0;
678 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
681 rdnlen = dn_rdnlen( NULL, val );
682 if ( rdnlen + patlen + 1 != vdnlen )
685 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
686 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
689 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
690 if ( vdnlen <= patlen )
693 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
697 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
703 if ( a->acl_filter != NULL ) {
704 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
705 if ( rc != LDAP_COMPARE_TRUE ) {
710 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
715 if ( !state->as_fe_done ) {
716 state->as_fe_done = 1;
717 a = frontendDB->be_acl;
721 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
726 * Record value-dependent access control state
728 #define ACL_RECORD_VALUE_STATE do { \
729 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
730 state->as_recorded |= ACL_STATE_RECORDED_VD; \
731 state->as_vd_acl = a; \
732 state->as_vd_acl_count = count; \
742 AclRegexMatches *matches,
744 struct berval *opndn )
747 * if access applies to the entry itself, and the
748 * user is bound as somebody in the same namespace as
749 * the entry, OR the given dn matches the dn pattern
752 * NOTE: styles "anonymous", "users" and "self"
753 * have been moved to enum slap_style_t, whose
754 * value is set in a_dn_style; however, the string
755 * is maintained in a_dn_pat.
758 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
759 if ( !BER_BVISEMPTY( opndn ) ) {
763 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
764 if ( BER_BVISEMPTY( opndn ) ) {
768 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
769 struct berval ndn, selfndn;
772 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
776 level = bdn->a_self_level;
784 selfndn = e->e_nname;
787 for ( ; level > 0; level-- ) {
788 if ( BER_BVISEMPTY( &ndn ) ) {
791 dnParent( &ndn, &ndn );
794 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
799 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
800 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
801 AclRegexMatches tmp_matches,
802 *tmp_matchesp = &tmp_matches;
804 regmatch_t *tmp_data;
806 MATCHES_MEMSET( &tmp_matches );
807 tmp_data = &tmp_matches.dn_data[0];
809 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
810 tmp_matchesp = matches;
811 else switch ( a->acl_dn_style ) {
812 case ACL_STYLE_REGEX:
813 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
814 tmp_matchesp = matches;
817 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
820 tmp_data[0].rm_so = 0;
821 tmp_data[0].rm_eo = e->e_nname.bv_len;
822 tmp_matches.dn_count = 1;
826 case ACL_STYLE_SUBTREE:
827 case ACL_STYLE_CHILDREN:
828 tmp_data[0].rm_so = 0;
829 tmp_data[0].rm_eo = e->e_nname.bv_len;
830 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
831 tmp_data[1].rm_eo = e->e_nname.bv_len;
832 tmp_matches.dn_count = 2;
845 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
846 e->e_ndn, tmp_matchesp ) )
854 ber_len_t patlen, odnlen;
857 if ( e->e_dn == NULL )
860 if ( bdn->a_expand ) {
862 char buf[ACL_BUF_SIZE];
864 AclRegexMatches tmp_matches,
865 *tmp_matchesp = &tmp_matches;
867 regmatch_t *tmp_data;
869 MATCHES_MEMSET( &tmp_matches );
870 tmp_data = &tmp_matches.dn_data[0];
872 bv.bv_len = sizeof( buf ) - 1;
875 /* Expand value regex */
876 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
877 tmp_matchesp = matches;
878 else switch ( a->acl_dn_style ) {
879 case ACL_STYLE_REGEX:
880 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
881 tmp_matchesp = matches;
884 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
887 tmp_data[0].rm_so = 0;
888 tmp_data[0].rm_eo = e->e_nname.bv_len;
889 tmp_matches.dn_count = 1;
893 case ACL_STYLE_SUBTREE:
894 case ACL_STYLE_CHILDREN:
895 tmp_data[0].rm_so = 0;
896 tmp_data[0].rm_eo = e->e_nname.bv_len;
897 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
898 tmp_data[1].rm_eo = e->e_nname.bv_len;
899 tmp_matches.dn_count = 2;
912 if ( acl_string_expand( &bv, &bdn->a_pat,
914 val->bv_val, tmp_matchesp ) )
919 if ( dnNormalize(0, NULL, NULL, &bv,
920 &pat, op->o_tmpmemctx )
923 /* did not expand to a valid dn */
932 odnlen = opndn->bv_len;
933 if ( odnlen < patlen ) {
934 goto dn_match_cleanup;
938 if ( bdn->a_style == ACL_STYLE_BASE ) {
939 /* base dn -- entire object DN must match */
940 if ( odnlen != patlen ) {
941 goto dn_match_cleanup;
944 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
945 ber_len_t rdnlen = 0;
947 if ( odnlen <= patlen ) {
948 goto dn_match_cleanup;
951 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
952 goto dn_match_cleanup;
955 rdnlen = dn_rdnlen( NULL, opndn );
956 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
957 goto dn_match_cleanup;
960 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
961 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
962 goto dn_match_cleanup;
965 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
966 if ( odnlen <= patlen ) {
967 goto dn_match_cleanup;
970 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
971 goto dn_match_cleanup;
974 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
975 int level = bdn->a_level;
978 if ( odnlen <= patlen ) {
979 goto dn_match_cleanup;
982 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
984 goto dn_match_cleanup;
988 for ( ; level > 0; level-- ) {
989 if ( BER_BVISEMPTY( &ndn ) ) {
990 goto dn_match_cleanup;
992 dnParent( &ndn, &ndn );
993 if ( ndn.bv_len < patlen ) {
994 goto dn_match_cleanup;
998 if ( ndn.bv_len != patlen ) {
999 goto dn_match_cleanup;
1003 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1006 if ( pat.bv_val != bdn->a_pat.bv_val ) {
1007 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1025 AccessControlState *state,
1026 slap_dn_access *bdn,
1027 struct berval *opndn )
1033 const char *attr = bdn->a_at->ad_cname.bv_val;
1035 assert( attr != NULL );
1037 if ( BER_BVISEMPTY( opndn ) ) {
1041 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1044 /* see if asker is listed in dnattr */
1045 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1047 at = attrs_find( at->a_next, bdn->a_at ) )
1049 if ( attr_valfind( at,
1050 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1051 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1052 &bv, NULL, op->o_tmpmemctx ) == 0 )
1061 /* have a dnattr match. if this is a self clause then
1062 * the target must also match the op dn.
1064 if ( bdn->a_self ) {
1065 /* check if the target is an attribute. */
1066 if ( val == NULL ) return 1;
1068 /* target is attribute, check if the attribute value
1071 rc = value_match( &match, bdn->a_at,
1072 bdn->a_at->ad_type->sat_equality, 0,
1074 /* on match error or no match, fail the ACL clause */
1075 if ( rc != LDAP_SUCCESS || match != 0 )
1080 /* no dnattr match, check if this is a self clause */
1081 if ( ! bdn->a_self )
1084 ACL_RECORD_VALUE_STATE;
1086 /* this is a self clause, check if the target is an
1092 /* target is attribute, check if the attribute value
1095 rc = value_match( &match, bdn->a_at,
1096 bdn->a_at->ad_type->sat_equality, 0,
1099 /* on match error or no match, fail the ACL clause */
1100 if ( rc != LDAP_SUCCESS || match != 0 )
1109 * slap_acl_mask - modifies mask based upon the given acl and the
1110 * requested access to entry e, attribute attr, value val. if val
1111 * is null, access to the whole attribute is assumed (all values).
1113 * returns 0 access NOT allowed
1117 static slap_control_t
1123 AttributeDescription *desc,
1125 AclRegexMatches *matches,
1127 AccessControlState *state,
1128 slap_access_t access )
1133 char accessmaskbuf[ACCESSMASK_MAXLEN];
1137 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1138 #endif /* SLAP_DYNACL */
1140 assert( a != NULL );
1141 assert( mask != NULL );
1142 assert( desc != NULL );
1144 attr = desc->ad_cname.bv_val;
1146 assert( attr != NULL );
1148 Debug( LDAP_DEBUG_ACL,
1149 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1152 Debug( LDAP_DEBUG_ACL,
1153 "=> acl_mask: to %s by \"%s\", (%s) \n",
1154 val ? "value" : "all values",
1155 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1156 accessmask2str( *mask, accessmaskbuf, 1 ) );
1162 for ( ; b != NULL; b = b->a_next, i++ ) {
1163 slap_mask_t oldmask, modmask;
1165 ACL_INVALIDATE( modmask );
1167 /* AND <who> clauses */
1168 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1169 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1170 b->a_dn_pat.bv_val, 0, 0);
1172 * if access applies to the entry itself, and the
1173 * user is bound as somebody in the same namespace as
1174 * the entry, OR the given dn matches the dn pattern
1177 * NOTE: styles "anonymous", "users" and "self"
1178 * have been moved to enum slap_style_t, whose
1179 * value is set in a_dn_style; however, the string
1180 * is maintained in a_dn_pat.
1183 if ( acl_mask_dn( op, e, val, a, matches,
1184 &b->a_dn, &op->o_ndn ) )
1190 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1193 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1194 b->a_realdn_pat.bv_val, 0, 0);
1196 * if access applies to the entry itself, and the
1197 * user is bound as somebody in the same namespace as
1198 * the entry, OR the given dn matches the dn pattern
1201 * NOTE: styles "anonymous", "users" and "self"
1202 * have been moved to enum slap_style_t, whose
1203 * value is set in a_dn_style; however, the string
1204 * is maintained in a_dn_pat.
1207 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1209 ndn = op->o_conn->c_ndn;
1214 if ( acl_mask_dn( op, e, val, a, matches,
1215 &b->a_realdn, &ndn ) )
1221 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1222 if ( ! op->o_conn->c_listener ) {
1225 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1226 b->a_sockurl_pat.bv_val, 0, 0 );
1228 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1229 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1230 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1231 e->e_ndn, matches ) )
1236 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1238 char buf[ACL_BUF_SIZE];
1240 bv.bv_len = sizeof( buf ) - 1;
1242 if ( acl_string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, val->bv_val, matches ) )
1247 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1253 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1261 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1262 if ( !op->o_conn->c_peer_domain.bv_val ) {
1265 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1266 b->a_domain_pat.bv_val, 0, 0 );
1267 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1268 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1269 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1270 e->e_ndn, matches ) )
1275 char buf[ACL_BUF_SIZE];
1277 struct berval cmp = op->o_conn->c_peer_domain;
1278 struct berval pat = b->a_domain_pat;
1280 if ( b->a_domain_expand ) {
1283 bv.bv_len = sizeof(buf) - 1;
1286 if ( acl_string_expand(&bv, &b->a_domain_pat, e->e_ndn, val->bv_val, matches) )
1293 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1294 int offset = cmp.bv_len - pat.bv_len;
1299 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1303 /* trim the domain */
1304 cmp.bv_val = &cmp.bv_val[ offset ];
1305 cmp.bv_len -= offset;
1308 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1315 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1316 if ( !op->o_conn->c_peer_name.bv_val ) {
1319 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1320 b->a_peername_pat.bv_val, 0, 0 );
1321 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1322 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1323 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1324 e->e_ndn, matches ) )
1330 /* try exact match */
1331 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1332 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1336 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1338 char buf[ACL_BUF_SIZE];
1340 bv.bv_len = sizeof( buf ) - 1;
1342 if ( acl_string_expand( &bv, &b->a_peername_pat, e->e_ndn, val->bv_val, matches ) )
1347 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1351 /* extract IP and try exact match */
1352 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1354 char buf[STRLENOF("255.255.255.255") + 1];
1357 int port_number = -1;
1359 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1360 acl_bv_ip_eq.bv_val,
1361 acl_bv_ip_eq.bv_len ) != 0 )
1364 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1365 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1367 port = strrchr( ip.bv_val, ':' );
1369 ip.bv_len = port - ip.bv_val;
1371 if ( lutil_atoi( &port_number, port ) != 0 )
1375 /* the port check can be anticipated here */
1376 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1379 /* address longer than expected? */
1380 if ( ip.bv_len >= sizeof(buf) )
1383 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1384 buf[ ip.bv_len ] = '\0';
1386 addr = inet_addr( buf );
1388 /* unable to convert? */
1389 if ( addr == (unsigned long)(-1) )
1392 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1395 #ifdef LDAP_PF_INET6
1396 /* extract IPv6 and try exact match */
1397 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1399 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1401 struct in6_addr addr;
1402 int port_number = -1;
1404 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1405 acl_bv_ipv6_eq.bv_val,
1406 acl_bv_ipv6_eq.bv_len ) != 0 )
1409 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1410 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1412 port = strrchr( ip.bv_val, ']' );
1414 ip.bv_len = port - ip.bv_val;
1416 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1420 /* the port check can be anticipated here */
1421 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1424 /* address longer than expected? */
1425 if ( ip.bv_len >= sizeof(buf) )
1428 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1429 buf[ ip.bv_len ] = '\0';
1431 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1435 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1437 #endif /* LDAP_PF_INET6 */
1439 #ifdef LDAP_PF_LOCAL
1440 /* extract path and try exact match */
1441 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1444 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1445 acl_bv_path_eq.bv_val,
1446 acl_bv_path_eq.bv_len ) != 0 )
1449 path.bv_val = op->o_conn->c_peer_name.bv_val
1450 + acl_bv_path_eq.bv_len;
1451 path.bv_len = op->o_conn->c_peer_name.bv_len
1452 - acl_bv_path_eq.bv_len;
1454 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1457 #endif /* LDAP_PF_LOCAL */
1459 /* exact match (very unlikely...) */
1460 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1467 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1468 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1471 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1472 b->a_sockname_pat.bv_val, 0, 0 );
1473 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1474 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1475 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1476 e->e_ndn, matches ) )
1481 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1483 char buf[ACL_BUF_SIZE];
1485 bv.bv_len = sizeof( buf ) - 1;
1487 if ( acl_string_expand( &bv, &b->a_sockname_pat, e->e_ndn, val->bv_val, matches ) )
1492 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1497 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1504 if ( b->a_dn_at != NULL ) {
1505 if ( acl_mask_dnattr( op, e, val, a,
1507 &b->a_dn, &op->o_ndn ) )
1513 if ( b->a_realdn_at != NULL ) {
1516 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1518 ndn = op->o_conn->c_ndn;
1523 if ( acl_mask_dnattr( op, e, val, a,
1525 &b->a_realdn, &ndn ) )
1531 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1533 struct berval ndn = BER_BVNULL;
1536 if ( op->o_ndn.bv_len == 0 ) {
1540 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1541 b->a_group_pat.bv_val, 0, 0 );
1543 /* b->a_group is an unexpanded entry name, expanded it should be an
1544 * entry with objectclass group* and we test to see if odn is one of
1545 * the values in the attribute group
1547 /* see if asker is listed in dnattr */
1548 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1549 char buf[ACL_BUF_SIZE];
1550 AclRegexMatches tmp_matches,
1551 *tmp_matchesp = &tmp_matches;
1552 regmatch_t *tmp_data;
1554 MATCHES_MEMSET( &tmp_matches );
1555 tmp_data = &tmp_matches.dn_data[0];
1557 bv.bv_len = sizeof(buf) - 1;
1562 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1563 tmp_matchesp = matches;
1564 else switch ( a->acl_dn_style ) {
1565 case ACL_STYLE_REGEX:
1566 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1567 tmp_matchesp = matches;
1571 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1572 case ACL_STYLE_BASE:
1573 tmp_data[0].rm_so = 0;
1574 tmp_data[0].rm_eo = e->e_nname.bv_len;
1575 tmp_matches.dn_count = 1;
1579 case ACL_STYLE_SUBTREE:
1580 case ACL_STYLE_CHILDREN:
1581 tmp_data[0].rm_so = 0;
1582 tmp_data[0].rm_eo = e->e_nname.bv_len;
1584 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1585 tmp_data[1].rm_eo = e->e_nname.bv_len;
1586 tmp_matches.dn_count = 2;
1599 if ( acl_string_expand( &bv, &b->a_group_pat,
1600 e->e_nname.bv_val, val->bv_val,
1606 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1607 op->o_tmpmemctx ) != LDAP_SUCCESS )
1609 /* did not expand to a valid dn */
1616 bv = b->a_group_pat;
1619 rc = backend_group( op, e, &bv, &op->o_ndn,
1620 b->a_group_oc, b->a_group_at );
1623 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1631 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1633 char buf[ACL_BUF_SIZE];
1635 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1636 b->a_set_pat.bv_val, 0, 0 );
1638 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1639 AclRegexMatches tmp_matches,
1640 *tmp_matchesp = &tmp_matches;
1642 regmatch_t *tmp_data;
1644 MATCHES_MEMSET( &tmp_matches );
1645 tmp_data = &tmp_matches.dn_data[0];
1647 bv.bv_len = sizeof( buf ) - 1;
1652 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1653 tmp_matchesp = matches;
1654 else switch ( a->acl_dn_style ) {
1655 case ACL_STYLE_REGEX:
1656 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1657 tmp_matchesp = matches;
1661 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1662 case ACL_STYLE_BASE:
1663 tmp_data[0].rm_so = 0;
1664 tmp_data[0].rm_eo = e->e_nname.bv_len;
1665 tmp_matches.dn_count = 1;
1669 case ACL_STYLE_SUBTREE:
1670 case ACL_STYLE_CHILDREN:
1671 tmp_data[0].rm_so = 0;
1672 tmp_data[0].rm_eo = e->e_nname.bv_len;
1673 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1674 tmp_data[1].rm_eo = e->e_nname.bv_len; tmp_matches.dn_count = 2;
1687 if ( acl_string_expand( &bv, &b->a_set_pat,
1688 e->e_nname.bv_val, val->bv_val,
1698 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1703 if ( b->a_authz.sai_ssf ) {
1704 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1705 b->a_authz.sai_ssf, op->o_ssf, 0 );
1706 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1711 if ( b->a_authz.sai_transport_ssf ) {
1712 Debug( LDAP_DEBUG_ACL,
1713 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1714 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1715 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1720 if ( b->a_authz.sai_tls_ssf ) {
1721 Debug( LDAP_DEBUG_ACL,
1722 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1723 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1724 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1729 if ( b->a_authz.sai_sasl_ssf ) {
1730 Debug( LDAP_DEBUG_ACL,
1731 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1732 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1733 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1738 /* check for the "self" modifier in the <access> field */
1739 if ( b->a_dn.a_self ) {
1743 ACL_RECORD_VALUE_STATE;
1745 /* must have DN syntax */
1746 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1747 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1749 /* check if the target is an attribute. */
1750 if ( val == NULL ) continue;
1752 /* a DN must be present */
1753 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1757 /* target is attribute, check if the attribute value
1760 rc = value_match( &match, desc,
1761 desc->ad_type->sat_equality, 0,
1762 val, &op->o_ndn, &dummy );
1763 /* on match error or no match, fail the ACL clause */
1764 if ( rc != LDAP_SUCCESS || match != 0 )
1769 if ( b->a_dynacl ) {
1771 slap_access_t tgrant, tdeny;
1773 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1776 /* this case works different from the others above.
1777 * since dynamic ACL's themselves give permissions, we need
1778 * to first check b->a_access_mask, the ACL's access level.
1780 /* first check if the right being requested
1781 * is allowed by the ACL clause.
1783 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1787 /* start out with nothing granted, nothing denied */
1788 ACL_INVALIDATE(tgrant);
1789 ACL_INVALIDATE(tdeny);
1791 for ( da = b->a_dynacl; da; da = da->da_next ) {
1792 slap_access_t grant,
1795 ACL_INVALIDATE(grant);
1796 ACL_INVALIDATE(deny);
1798 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1799 da->da_name, 0, 0 );
1802 * XXXmanu Only DN matches are supplied
1803 * sending attribute values matches require
1806 (void)da->da_mask( da->da_private, op, e, desc,
1807 val, matches->dn_count, matches->dn_data,
1814 /* remove anything that the ACL clause does not allow */
1815 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1816 tdeny &= ACL_PRIV_MASK;
1818 /* see if we have anything to contribute */
1819 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1823 /* this could be improved by changing slap_acl_mask so that it can deal with
1824 * by clauses that return grant/deny pairs. Right now, it does either
1825 * additive or subtractive rights, but not both at the same time. So,
1826 * we need to combine the grant/deny pair into a single rights mask in
1827 * a smart way: if either grant or deny is "empty", then we use the
1828 * opposite as is, otherwise we remove any denied rights from the grant
1829 * rights mask and construct an additive mask.
1831 if (ACL_IS_INVALID(tdeny)) {
1832 modmask = tgrant | ACL_PRIV_ADDITIVE;
1834 } else if (ACL_IS_INVALID(tgrant)) {
1835 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1838 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1842 #endif /* SLAP_DYNACL */
1844 modmask = b->a_access_mask;
1847 Debug( LDAP_DEBUG_ACL,
1848 "<= acl_mask: [%d] applying %s (%s)\n",
1849 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1850 b->a_type == ACL_CONTINUE
1852 : b->a_type == ACL_BREAK
1858 if( ACL_IS_ADDITIVE(modmask) ) {
1860 ACL_PRIV_SET( *mask, modmask );
1863 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1865 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1866 /* substract privs */
1867 ACL_PRIV_CLR( *mask, modmask );
1870 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1877 Debug( LDAP_DEBUG_ACL,
1878 "<= acl_mask: [%d] mask: %s\n",
1879 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1881 if( b->a_type == ACL_CONTINUE ) {
1884 } else if ( b->a_type == ACL_BREAK ) {
1892 /* implicit "by * none" clause */
1895 Debug( LDAP_DEBUG_ACL,
1896 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1897 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1902 * acl_check_modlist - check access control on the given entry to see if
1903 * it allows the given modifications by the user associated with op.
1904 * returns 1 if mods allowed ok
1905 * 0 mods not allowed
1912 Modifications *mlist )
1915 AccessControlState state = ACL_STATE_INIT;
1918 int ret = 1; /* default is access allowed */
1922 be = LDAP_STAILQ_FIRST(&backendDB);
1926 assert( be != NULL );
1928 /* If ADD attribute checking is not enabled, just allow it */
1929 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1932 /* short circuit root database access */
1933 if ( be_isroot( op ) ) {
1934 Debug( LDAP_DEBUG_ACL,
1935 "<= acl_access_allowed: granted to database root\n",
1940 /* use backend default access if no backend acls */
1941 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1942 Debug( LDAP_DEBUG_ACL,
1943 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1944 access2str( ACL_WRITE ),
1945 op->o_bd->be_dfltaccess >= ACL_WRITE
1946 ? "granted" : "denied",
1948 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1952 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1954 * Internal mods are ignored by ACL_WRITE checking
1956 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1957 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1958 " modify access granted\n",
1959 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1964 * no-user-modification operational attributes are ignored
1965 * by ACL_WRITE checking as any found here are not provided
1968 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1969 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1971 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1972 " modify access granted\n",
1973 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1977 switch ( mlist->sml_op ) {
1978 case LDAP_MOD_REPLACE:
1979 case LDAP_MOD_INCREMENT:
1981 * We must check both permission to delete the whole
1982 * attribute and permission to add the specific attributes.
1983 * This prevents abuse from selfwriters.
1985 if ( ! access_allowed( op, e,
1986 mlist->sml_desc, NULL,
1987 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1994 if ( mlist->sml_values == NULL ) break;
1996 /* fall thru to check value to add */
1999 assert( mlist->sml_values != NULL );
2001 for ( bv = mlist->sml_nvalues
2002 ? mlist->sml_nvalues : mlist->sml_values;
2003 bv->bv_val != NULL; bv++ )
2005 if ( ! access_allowed( op, e,
2006 mlist->sml_desc, bv,
2007 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2016 case LDAP_MOD_DELETE:
2017 if ( mlist->sml_values == NULL ) {
2018 if ( ! access_allowed( op, e,
2019 mlist->sml_desc, NULL,
2020 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2028 for ( bv = mlist->sml_nvalues
2029 ? mlist->sml_nvalues : mlist->sml_values;
2030 bv->bv_val != NULL; bv++ )
2032 if ( ! access_allowed( op, e,
2033 mlist->sml_desc, bv,
2034 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2043 case SLAP_MOD_SOFTADD:
2044 /* allow adding attribute via modrdn thru */
2056 if (be_null) op->o_bd = NULL;
2062 struct berval *list,
2075 while ( len >= 0 && --ix >= 0 ) {
2076 while ( --len >= 0 && *p++ != sep )
2079 while ( len >= 0 && *p == ' ' ) {
2092 while ( --len >= 0 && *p != sep ) {
2096 while ( bv->bv_len > 0 && *--p == ' ' ) {
2103 typedef struct acl_set_gather_t {
2109 acl_set_cb_gather( Operation *op, SlapReply *rs )
2111 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2113 if ( rs->sr_type == REP_SEARCH ) {
2114 BerValue bvals[ 2 ];
2115 BerVarray bvalsp = NULL;
2118 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2119 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2121 if ( desc == NULL ) {
2125 if ( desc == slap_schema.si_ad_entryDN ) {
2127 bvals[ 0 ] = rs->sr_entry->e_nname;
2128 BER_BVZERO( &bvals[ 1 ] );
2133 a = attr_find( rs->sr_entry->e_attrs, desc );
2135 bvalsp = a->a_nvals;
2140 p->bvals = slap_set_join( p->cookie, p->bvals,
2141 ( '|' | SLAP_SET_RREF ), bvalsp );
2146 assert( rs->sr_type == REP_RESULT );
2153 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2155 AclSetCookie *cp = (AclSetCookie *)cookie;
2157 LDAPURLDesc *ludp = NULL;
2158 Operation op2 = { 0 };
2159 SlapReply rs = {REP_RESULT};
2160 AttributeName anlist[ 2 ], *anlistp = NULL;
2162 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2163 acl_set_gather_t p = { 0 };
2165 /* this routine needs to return the bervals instead of
2166 * plain strings, since syntax is not known. It should
2167 * also return the syntax or some "comparison cookie".
2169 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2170 return acl_set_gather2( cookie, name, desc );
2173 rc = ldap_url_parse( name->bv_val, &ludp );
2174 if ( rc != LDAP_URL_SUCCESS ) {
2175 Debug( LDAP_DEBUG_TRACE,
2176 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2177 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2179 rc = LDAP_PROTOCOL_ERROR;
2183 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2185 /* host part must be empty */
2186 /* extensions parts must be empty */
2187 Debug( LDAP_DEBUG_TRACE,
2188 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2189 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2191 rc = LDAP_PROTOCOL_ERROR;
2195 /* Grab the searchbase and see if an appropriate database can be found */
2196 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2197 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2198 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2199 BER_BVZERO( &op2.o_req_dn );
2200 if ( rc != LDAP_SUCCESS ) {
2201 Debug( LDAP_DEBUG_TRACE,
2202 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2203 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2208 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2209 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2210 Debug( LDAP_DEBUG_TRACE,
2211 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2212 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2214 rc = LDAP_NO_SUCH_OBJECT;
2218 /* Grab the filter */
2219 if ( ludp->lud_filter ) {
2220 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2221 cp->asc_op->o_tmpmemctx );
2222 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2223 if ( op2.ors_filter == NULL ) {
2224 Debug( LDAP_DEBUG_TRACE,
2225 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2226 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2228 rc = LDAP_PROTOCOL_ERROR;
2233 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2234 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2238 /* Grab the scope */
2239 op2.ors_scope = ludp->lud_scope;
2241 /* Grap the attributes */
2242 if ( ludp->lud_attrs ) {
2245 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2248 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2249 cp->asc_op->o_tmpmemctx );
2251 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2253 AttributeDescription *desc = NULL;
2254 const char *text = NULL;
2256 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2257 rc = slap_bv2ad( &name, &desc, &text );
2258 if ( rc == LDAP_SUCCESS ) {
2259 anlistp[ nattrs ].an_name = name;
2260 anlistp[ nattrs ].an_desc = desc;
2269 anlistp[ nattrs ].an_name = desc->ad_cname;
2270 anlistp[ nattrs ].an_desc = desc;
2272 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2276 op2.o_hdr = cp->asc_op->o_hdr;
2277 op2.o_tag = LDAP_REQ_SEARCH;
2278 op2.o_ndn = op2.o_bd->be_rootndn;
2279 op2.o_callback = &cb;
2280 slap_op_time( &op2.o_time, &op2.o_tincr );
2281 op2.o_do_not_cache = 1;
2282 op2.o_is_auth_check = 0;
2283 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2284 op2.ors_slimit = SLAP_NO_LIMIT;
2285 op2.ors_tlimit = SLAP_NO_LIMIT;
2286 op2.ors_attrs = anlistp;
2287 op2.ors_attrsonly = 0;
2288 op2.o_private = cp->asc_op->o_private;
2289 op2.o_extra = cp->asc_op->o_extra;
2293 rc = op2.o_bd->be_search( &op2, &rs );
2299 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2300 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2302 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2303 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2305 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2306 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2309 ldap_free_urldesc( ludp );
2311 if ( anlistp && anlistp != anlist ) {
2312 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2319 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2321 AclSetCookie *cp = (AclSetCookie *)cookie;
2322 BerVarray bvals = NULL;
2326 /* this routine needs to return the bervals instead of
2327 * plain strings, since syntax is not known. It should
2328 * also return the syntax or some "comparison cookie".
2330 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2331 if ( rc == LDAP_SUCCESS ) {
2332 if ( desc == slap_schema.si_ad_entryDN ) {
2333 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2334 cp->asc_op->o_tmpmemctx );
2336 BER_BVZERO( &bvals[ 1 ] );
2340 backend_attribute( cp->asc_op,
2341 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2344 if ( !BER_BVISNULL( &ndn ) ) {
2345 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2354 struct berval *subj,
2357 struct berval *default_set_attribute )
2359 struct berval set = BER_BVNULL;
2361 AclSetCookie cookie;
2363 if ( default_set_attribute == NULL ) {
2367 struct berval subjdn, ndn = BER_BVNULL;
2368 struct berval setat;
2369 BerVarray bvals = NULL;
2371 AttributeDescription *desc = NULL;
2373 /* format of string is "entry/setAttrName" */
2374 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2378 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2379 setat = *default_set_attribute;
2383 * NOTE: dnNormalize honors the ber_len field
2384 * as the length of the dn to be normalized
2386 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2387 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2389 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2390 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2394 BER_BVZERO( &bvals[0] );
2395 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2397 bvals[0].bv_val = bvals[i-1].bv_val;
2398 BER_BVZERO( &bvals[i-1] );
2400 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2401 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2406 if ( !BER_BVISNULL( &set ) ) {
2409 rc = ( slap_set_filter(
2411 (SetCookie *)&cookie, &set,
2412 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2413 if ( set.bv_val != subj->bv_val ) {
2414 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2424 * dynamic ACL infrastructure
2426 static slap_dynacl_t *da_list = NULL;
2429 slap_dynacl_register( slap_dynacl_t *da )
2433 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2434 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2439 if ( tmp != NULL ) {
2443 if ( da->da_mask == NULL ) {
2447 da->da_private = NULL;
2448 da->da_next = da_list;
2454 static slap_dynacl_t *
2455 slap_dynacl_next( slap_dynacl_t *da )
2464 slap_dynacl_get( const char *name )
2468 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2469 if ( strcasecmp( da->da_name, name ) == 0 ) {
2476 #endif /* SLAP_DYNACL */
2479 * statically built-in dynamic ACL initialization
2481 static int (*acl_init_func[])( void ) = {
2483 /* TODO: remove when ACI will only be dynamic */
2484 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2486 #endif /* SLAPD_ACI_ENABLED */
2487 #endif /* SLAP_DYNACL */
2497 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2498 rc = (*(acl_init_func[ i ]))();
2513 AclRegexMatches *matches)
2519 enum { DN_FLAG, VAL_FLAG } tflag;
2522 bv->bv_val[0] = '\0';
2523 bv->bv_len--; /* leave space for lone $ */
2527 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2528 sp < pat->bv_val + pat->bv_len ; sp++ )
2530 /* did we previously see a $ */
2532 if ( flag == 1 && *sp == '$' ) {
2538 } else if ( flag == 2 && *sp == 'v' /*'}'*/) {
2541 } else if ( flag == 2 && *sp == 'd' /*'}'*/) {
2544 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2547 } else if ( *sp >= '0' && *sp <= '9' ) {
2558 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2559 if ( *sp >= '0' && *sp <= '9' ) {
2560 n = 10*n + ( *sp - '0' );
2564 if ( *sp != /*'{'*/ '}' ) {
2572 nm = matches->dn_count;
2573 m = matches->dn_data;
2577 nm = matches->val_count;
2578 m = matches->val_data;
2586 if ( data == NULL ) {
2595 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2614 /* must have ended with a single $ */
2622 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2623 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2630 struct berval *pat, /* pattern to expand and match against */
2631 char *str, /* string to match against pattern */
2632 char *buf, /* buffer with $N expansion variables */
2633 AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
2637 char newbuf[ACL_BUF_SIZE];
2641 bv.bv_len = sizeof( newbuf ) - 1;
2648 acl_string_expand( &bv, pat, buf, NULL, matches );
2649 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2651 char error[ACL_BUF_SIZE];
2652 regerror( rc, &re, error, sizeof( error ) );
2654 Debug( LDAP_DEBUG_TRACE,
2655 "compile( \"%s\", \"%s\") failed %s\n",
2656 pat->bv_val, str, error );
2660 rc = regexec( &re, str, 0, NULL, 0 );
2663 Debug( LDAP_DEBUG_TRACE,
2664 "=> regex_matches: string: %s\n", str, 0, 0 );
2665 Debug( LDAP_DEBUG_TRACE,
2666 "=> regex_matches: rc: %d %s\n",
2667 rc, !rc ? "matches" : "no matches", 0 );