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,
70 struct berval *dn_matches, struct berval *val_matches,
71 AclRegexMatches *matches);
73 typedef struct AclSetCookie {
75 #define asc_op asc_cookie.set_op
80 SLAP_SET_GATHER acl_set_gather;
81 SLAP_SET_GATHER acl_set_gather2;
84 * access_allowed - check whether op->o_ndn is allowed the requested access
85 * to entry e, attribute attr, value val. if val is null, access to
86 * the whole attribute is assumed (all values).
88 * This routine loops through all access controls and calls
89 * slap_acl_mask() on each applicable access control.
90 * The loop exits when a definitive answer is reached or
91 * or no more controls remain.
98 * - can be legally called with op == NULL
99 * - can be legally called with op->o_bd == NULL
103 slap_access_always_allowed(
106 AttributeDescription *desc,
108 slap_access_t access,
109 AccessControlState *state,
112 assert( maskp != NULL );
115 ACL_LVL_ASSIGN_MANAGE( *maskp );
120 #define MATCHES_DNMAXCOUNT(m) \
121 ( sizeof ( (m)->dn_data ) / sizeof( *(m)->dn_data ) )
122 #define MATCHES_VALMAXCOUNT(m) \
123 ( sizeof ( (m)->val_data ) / sizeof( *(m)->val_data ) )
124 #define MATCHES_MEMSET(m) do { \
125 memset( (m)->dn_data, '\0', sizeof( (m)->dn_data ) ); \
126 memset( (m)->val_data, '\0', sizeof( (m)->val_data ) ); \
127 (m)->dn_count = MATCHES_DNMAXCOUNT( (m) ); \
128 (m)->val_count = MATCHES_VALMAXCOUNT( (m) ); \
129 } while ( 0 /* CONSTCOND */ )
135 AttributeDescription *desc,
137 slap_access_t access,
138 AccessControlState *state,
143 AccessControl *a = NULL;
146 char accessmaskbuf[ACCESSMASK_MAXLEN];
149 slap_control_t control;
150 slap_access_t access_level;
152 AclRegexMatches matches;
153 AccessControlState acl_state = ACL_STATE_INIT;
155 assert( op != NULL );
157 assert( desc != NULL );
158 assert( maskp != NULL );
160 access_level = ACL_LEVEL( access );
161 attr = desc->ad_cname.bv_val;
163 assert( attr != NULL );
167 /* grant database root access */
168 if ( be_isroot( op ) ) {
169 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
170 mask = ACL_LVL_MANAGE;
175 * no-user-modification operational attributes are ignored
176 * by ACL_WRITE checking as any found here are not provided
179 * NOTE: but they are not ignored for ACL_MANAGE, because
180 * if we get here it means a non-root user is trying to
181 * manage data, so we need to check its privileges.
183 if ( access_level == ACL_WRITE
184 && is_at_no_user_mod( desc->ad_type )
185 && desc != slap_schema.si_ad_entry
186 && desc != slap_schema.si_ad_children )
188 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
189 " %s access granted\n",
194 /* use backend default access if no backend acls */
195 if ( op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
198 Debug( LDAP_DEBUG_ACL,
199 "=> slap_access_allowed: backend default %s "
200 "access %s to \"%s\"\n",
201 access2str( access ),
202 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
203 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
204 ret = op->o_bd->be_dfltaccess >= access_level;
206 mask = ACL_PRIV_LEVEL;
207 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
208 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
219 if ( state->as_vd_ad == desc ) {
220 a = state->as_vd_acl;
221 count = state->as_vd_acl_count;
222 if ( state->as_fe_done )
225 state->as_vi_acl = NULL;
231 state->as_fe_done = 0;
233 ACL_PRIV_ASSIGN( mask, *maskp );
234 MATCHES_MEMSET( &matches );
236 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
237 &matches, state ) ) != NULL )
240 int dnmaxcount = MATCHES_DNMAXCOUNT( &matches );
241 int valmaxcount = MATCHES_VALMAXCOUNT( &matches );
242 regmatch_t *dn_data = matches.dn_data;
243 regmatch_t *val_data = matches.val_data;
246 for ( i = 0; i < dnmaxcount && dn_data[i].rm_eo > 0; i++ ) {
247 char *data = e->e_ndn;
249 Debug( LDAP_DEBUG_ACL, "=> match[dn%d]: %d %d ", i,
250 (int)dn_data[i].rm_so,
251 (int)dn_data[i].rm_eo );
252 if ( dn_data[i].rm_so <= dn_data[0].rm_eo ) {
254 for ( n = dn_data[i].rm_so;
255 n < dn_data[i].rm_eo; n++ ) {
256 Debug( LDAP_DEBUG_ACL, "%c",
260 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
264 for ( i = 0; i < valmaxcount && val_data[i].rm_eo > 0; i++ ) {
265 char *data = val->bv_val;
267 Debug( LDAP_DEBUG_ACL, "=> match[val%d]: %d %d ", i,
268 (int)val_data[i].rm_so,
269 (int)val_data[i].rm_eo );
270 if ( val_data[i].rm_so <= val_data[0].rm_eo ) {
272 for ( n = val_data[i].rm_so;
273 n < val_data[i].rm_eo; n++ ) {
274 Debug( LDAP_DEBUG_ACL, "%c",
278 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
282 if ( state->as_vi_acl == a &&
283 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
285 Debug( LDAP_DEBUG_ACL,
286 "=> slap_access_allowed: result was in cache (%s)\n",
288 ret = state->as_result;
291 Debug( LDAP_DEBUG_ACL,
292 "=> slap_access_allowed: result not in cache (%s)\n",
297 control = slap_acl_mask( a, &mask, op,
298 e, desc, val, &matches, count, state, access );
300 if ( control != ACL_BREAK ) {
304 MATCHES_MEMSET( &matches );
307 if ( ACL_IS_INVALID( mask ) ) {
308 Debug( LDAP_DEBUG_ACL,
309 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
311 ACL_PRIV_ASSIGN( mask, *maskp );
313 } else if ( control == ACL_BREAK ) {
314 Debug( LDAP_DEBUG_ACL,
315 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
320 ret = ACL_GRANT( mask, access );
322 Debug( LDAP_DEBUG_ACL,
323 "=> slap_access_allowed: %s access %s by %s\n",
324 access2str( access ), ret ? "granted" : "denied",
325 accessmask2str( mask, accessmaskbuf, 1 ) );
328 ACL_PRIV_ASSIGN( *maskp, mask );
336 AttributeDescription *desc,
338 slap_access_t access,
339 AccessControlState *state,
346 * NOTE: control gets here if FIXME
347 * if an appropriate backend cannot be selected for the operation,
348 * we assume that the frontend should handle this
349 * FIXME: should select_backend() take care of this,
350 * and return frontendDB instead of NULL? maybe for some value
355 if ( op->o_bd == NULL ) {
356 op->o_bd = select_backend( &op->o_req_ndn, 0 );
357 if ( op->o_bd == NULL )
358 op->o_bd = frontendDB;
360 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
370 AttributeDescription *desc,
372 slap_access_t access,
373 AccessControlState *state,
377 AccessControl *a = NULL;
381 char accessmaskbuf[ACCESSMASK_MAXLEN];
384 slap_access_t access_level;
386 static AccessControlState state_init = ACL_STATE_INIT;
389 assert( desc != NULL );
391 access_level = ACL_LEVEL( access );
393 assert( access_level > ACL_NONE );
396 if ( maskp ) ACL_INVALIDATE( *maskp );
398 attr = desc->ad_cname.bv_val;
400 assert( attr != NULL );
403 if ( op->o_acl_priv != ACL_NONE ) {
404 access = op->o_acl_priv;
406 } else if ( op->o_is_auth_check &&
407 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
411 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
412 desc == slap_schema.si_ad_entry )
419 if ( state->as_vd_ad == desc ) {
420 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
423 return state->as_result;
431 Debug( LDAP_DEBUG_ACL,
432 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
433 access2str( access ), e->e_dn, attr );
440 if ( op->o_bd == NULL ) {
441 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
444 /* FIXME: experimental; use first backend rules
445 * iff there is no global_acl (ITS#3100)
447 if ( frontendDB->be_acl != NULL ) {
448 op->o_bd = frontendDB;
451 assert( op->o_bd != NULL );
453 /* this is enforced in backend_add() */
454 if ( op->o_bd->bd_info->bi_access_allowed ) {
455 /* delegate to backend */
456 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
457 desc, val, access, state, &mask );
460 /* use default (but pass through frontend
461 * for global ACL overlays) */
462 ret = frontendDB->bd_info->bi_access_allowed( op, e,
463 desc, val, access, state, &mask );
467 if ( ACL_IS_INVALID( mask ) ) {
468 Debug( LDAP_DEBUG_ACL,
469 "=> access_allowed: \"%s\" (%s) invalid!\n",
474 Debug( LDAP_DEBUG_ACL,
475 "=> access_allowed: no more rules\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_ACL,
482 "=> access_allowed: %s access %s by %s\n",
483 access2str( access ), ret ? "granted" : "denied",
484 accessmask2str( mask, accessmaskbuf, 1 ) );
487 if ( state != NULL ) {
488 /* If not value-dependent, save ACL in case of more attrs */
489 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
490 state->as_vi_acl = a;
491 state->as_result = ret;
493 state->as_recorded |= ACL_STATE_RECORDED;
494 state->as_vd_ad = desc;
496 if ( be_null ) op->o_bd = NULL;
497 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
503 * slap_acl_get - return the acl applicable to entry e, attribute
504 * attr. the acl returned is suitable for use in subsequent calls to
505 * acl_access_allowed().
508 static AccessControl *
514 AttributeDescription *desc,
516 AclRegexMatches *matches,
517 AccessControlState *state )
524 assert( count != NULL );
525 assert( desc != NULL );
526 assert( state != NULL );
528 attr = desc->ad_cname.bv_val;
530 assert( attr != NULL );
533 if( op->o_bd == NULL || op->o_bd->be_acl == NULL ) {
534 a = frontendDB->be_acl;
536 a = op->o_bd->be_acl;
541 if ( a == frontendDB->be_acl )
542 state->as_fe_done = 1;
548 dnlen = e->e_nname.bv_len;
551 for ( ; a != NULL; prev = a, a = a->acl_next ) {
554 if ( a != frontendDB->be_acl && state->as_fe_done )
557 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
558 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
559 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
560 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
561 if ( regexec ( &a->acl_dn_re,
564 matches->dn_data, 0 ) )
570 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
571 *count, a->acl_dn_pat.bv_val, 0 );
572 patlen = a->acl_dn_pat.bv_len;
573 if ( dnlen < patlen )
576 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
577 /* base dn -- entire object DN must match */
578 if ( dnlen != patlen )
581 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
582 ber_len_t rdnlen = 0;
585 if ( dnlen <= patlen )
589 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
594 rdnlen = dn_rdnlen( NULL, &e->e_nname );
595 if ( rdnlen + patlen + sep != dnlen )
598 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
599 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
602 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
603 if ( dnlen <= patlen )
605 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
609 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
613 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
617 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
618 matches->dn_data[0].rm_so = -1;
619 matches->dn_data[0].rm_eo = -1;
620 matches->val_data[0].rm_so = -1;
621 matches->val_data[0].rm_eo = -1;
625 /* Is this ACL only for a specific value? */
626 if ( a->acl_attrval.bv_len ) {
631 if( !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
632 state->as_recorded |= ACL_STATE_RECORDED_VD;
633 state->as_vd_acl = prev;
634 state->as_vd_acl_count = *count - 1;
637 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
638 Debug( LDAP_DEBUG_ACL,
639 "acl_get: valpat %s\n",
640 a->acl_attrval.bv_val, 0, 0 );
641 if ( regexec ( &a->acl_attrval_re,
644 matches->val_data, 0 ) )
652 Debug( LDAP_DEBUG_ACL,
654 a->acl_attrval.bv_val, 0, 0 );
656 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
657 if (value_match( &match, desc,
658 a->acl_attrval_mr, 0,
659 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
664 ber_len_t patlen, vdnlen;
666 patlen = a->acl_attrval.bv_len;
667 vdnlen = val->bv_len;
669 if ( vdnlen < patlen )
672 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
673 if ( vdnlen > patlen )
676 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
677 ber_len_t rdnlen = 0;
679 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
682 rdnlen = dn_rdnlen( NULL, val );
683 if ( rdnlen + patlen + 1 != vdnlen )
686 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
687 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
690 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
691 if ( vdnlen <= patlen )
694 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
698 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
704 if ( a->acl_filter != NULL ) {
705 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
706 if ( rc != LDAP_COMPARE_TRUE ) {
711 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
716 if ( !state->as_fe_done ) {
717 state->as_fe_done = 1;
718 a = frontendDB->be_acl;
722 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
727 * Record value-dependent access control state
729 #define ACL_RECORD_VALUE_STATE do { \
730 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
731 state->as_recorded |= ACL_STATE_RECORDED_VD; \
732 state->as_vd_acl = a; \
733 state->as_vd_acl_count = count; \
743 AclRegexMatches *matches,
745 struct berval *opndn )
748 * if access applies to the entry itself, and the
749 * user is bound as somebody in the same namespace as
750 * the entry, OR the given dn matches the dn pattern
753 * NOTE: styles "anonymous", "users" and "self"
754 * have been moved to enum slap_style_t, whose
755 * value is set in a_dn_style; however, the string
756 * is maintained in a_dn_pat.
759 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
760 if ( !BER_BVISEMPTY( opndn ) ) {
764 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
765 if ( BER_BVISEMPTY( opndn ) ) {
769 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
770 struct berval ndn, selfndn;
773 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
777 level = bdn->a_self_level;
785 selfndn = e->e_nname;
788 for ( ; level > 0; level-- ) {
789 if ( BER_BVISEMPTY( &ndn ) ) {
792 dnParent( &ndn, &ndn );
795 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
800 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
801 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
802 AclRegexMatches tmp_matches,
803 *tmp_matchesp = &tmp_matches;
805 regmatch_t *tmp_data;
807 MATCHES_MEMSET( &tmp_matches );
808 tmp_data = &tmp_matches.dn_data[0];
810 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
811 tmp_matchesp = matches;
812 else switch ( a->acl_dn_style ) {
813 case ACL_STYLE_REGEX:
814 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
815 tmp_matchesp = matches;
818 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
821 tmp_data[0].rm_so = 0;
822 tmp_data[0].rm_eo = e->e_nname.bv_len;
823 tmp_matches.dn_count = 1;
827 case ACL_STYLE_SUBTREE:
828 case ACL_STYLE_CHILDREN:
829 tmp_data[0].rm_so = 0;
830 tmp_data[0].rm_eo = e->e_nname.bv_len;
831 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
832 tmp_data[1].rm_eo = e->e_nname.bv_len;
833 tmp_matches.dn_count = 2;
846 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
847 &e->e_nname, NULL, tmp_matchesp ) )
855 ber_len_t patlen, odnlen;
858 if ( e->e_dn == NULL )
861 if ( bdn->a_expand ) {
863 char buf[ACL_BUF_SIZE];
865 AclRegexMatches tmp_matches,
866 *tmp_matchesp = &tmp_matches;
868 regmatch_t *tmp_data;
870 MATCHES_MEMSET( &tmp_matches );
871 tmp_data = &tmp_matches.dn_data[0];
873 bv.bv_len = sizeof( buf ) - 1;
876 /* Expand value regex */
877 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
878 tmp_matchesp = matches;
879 else switch ( a->acl_dn_style ) {
880 case ACL_STYLE_REGEX:
881 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
882 tmp_matchesp = matches;
885 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
888 tmp_data[0].rm_so = 0;
889 tmp_data[0].rm_eo = e->e_nname.bv_len;
890 tmp_matches.dn_count = 1;
894 case ACL_STYLE_SUBTREE:
895 case ACL_STYLE_CHILDREN:
896 tmp_data[0].rm_so = 0;
897 tmp_data[0].rm_eo = e->e_nname.bv_len;
898 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
899 tmp_data[1].rm_eo = e->e_nname.bv_len;
900 tmp_matches.dn_count = 2;
913 if ( acl_string_expand( &bv, &bdn->a_pat,
915 val, tmp_matchesp ) )
920 if ( dnNormalize(0, NULL, NULL, &bv,
921 &pat, op->o_tmpmemctx )
924 /* did not expand to a valid dn */
933 odnlen = opndn->bv_len;
934 if ( odnlen < patlen ) {
935 goto dn_match_cleanup;
939 if ( bdn->a_style == ACL_STYLE_BASE ) {
940 /* base dn -- entire object DN must match */
941 if ( odnlen != patlen ) {
942 goto dn_match_cleanup;
945 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
946 ber_len_t rdnlen = 0;
948 if ( odnlen <= patlen ) {
949 goto dn_match_cleanup;
952 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
953 goto dn_match_cleanup;
956 rdnlen = dn_rdnlen( NULL, opndn );
957 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
958 goto dn_match_cleanup;
961 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
962 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
963 goto dn_match_cleanup;
966 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
967 if ( odnlen <= patlen ) {
968 goto dn_match_cleanup;
971 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
972 goto dn_match_cleanup;
975 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
976 int level = bdn->a_level;
979 if ( odnlen <= patlen ) {
980 goto dn_match_cleanup;
983 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
985 goto dn_match_cleanup;
989 for ( ; level > 0; level-- ) {
990 if ( BER_BVISEMPTY( &ndn ) ) {
991 goto dn_match_cleanup;
993 dnParent( &ndn, &ndn );
994 if ( ndn.bv_len < patlen ) {
995 goto dn_match_cleanup;
999 if ( ndn.bv_len != patlen ) {
1000 goto dn_match_cleanup;
1004 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1007 if ( pat.bv_val != bdn->a_pat.bv_val ) {
1008 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1026 AccessControlState *state,
1027 slap_dn_access *bdn,
1028 struct berval *opndn )
1034 const char *attr = bdn->a_at->ad_cname.bv_val;
1036 assert( attr != NULL );
1038 if ( BER_BVISEMPTY( opndn ) ) {
1042 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1045 /* see if asker is listed in dnattr */
1046 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1048 at = attrs_find( at->a_next, bdn->a_at ) )
1050 if ( attr_valfind( at,
1051 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1052 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1053 &bv, NULL, op->o_tmpmemctx ) == 0 )
1062 /* have a dnattr match. if this is a self clause then
1063 * the target must also match the op dn.
1065 if ( bdn->a_self ) {
1066 /* check if the target is an attribute. */
1067 if ( val == NULL ) return 1;
1069 /* target is attribute, check if the attribute value
1072 rc = value_match( &match, bdn->a_at,
1073 bdn->a_at->ad_type->sat_equality, 0,
1075 /* on match error or no match, fail the ACL clause */
1076 if ( rc != LDAP_SUCCESS || match != 0 )
1081 /* no dnattr match, check if this is a self clause */
1082 if ( ! bdn->a_self )
1085 ACL_RECORD_VALUE_STATE;
1087 /* this is a self clause, check if the target is an
1093 /* target is attribute, check if the attribute value
1096 rc = value_match( &match, bdn->a_at,
1097 bdn->a_at->ad_type->sat_equality, 0,
1100 /* on match error or no match, fail the ACL clause */
1101 if ( rc != LDAP_SUCCESS || match != 0 )
1110 * slap_acl_mask - modifies mask based upon the given acl and the
1111 * requested access to entry e, attribute attr, value val. if val
1112 * is null, access to the whole attribute is assumed (all values).
1114 * returns 0 access NOT allowed
1118 static slap_control_t
1124 AttributeDescription *desc,
1126 AclRegexMatches *matches,
1128 AccessControlState *state,
1129 slap_access_t access )
1134 char accessmaskbuf[ACCESSMASK_MAXLEN];
1138 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1139 #endif /* SLAP_DYNACL */
1141 assert( a != NULL );
1142 assert( mask != NULL );
1143 assert( desc != NULL );
1145 attr = desc->ad_cname.bv_val;
1147 assert( attr != NULL );
1149 Debug( LDAP_DEBUG_ACL,
1150 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1153 Debug( LDAP_DEBUG_ACL,
1154 "=> acl_mask: to %s by \"%s\", (%s) \n",
1155 val ? "value" : "all values",
1156 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1157 accessmask2str( *mask, accessmaskbuf, 1 ) );
1163 for ( ; b != NULL; b = b->a_next, i++ ) {
1164 slap_mask_t oldmask, modmask;
1166 ACL_INVALIDATE( modmask );
1168 /* AND <who> clauses */
1169 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1170 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1171 b->a_dn_pat.bv_val, 0, 0);
1173 * if access applies to the entry itself, and the
1174 * user is bound as somebody in the same namespace as
1175 * the entry, OR the given dn matches the dn pattern
1178 * NOTE: styles "anonymous", "users" and "self"
1179 * have been moved to enum slap_style_t, whose
1180 * value is set in a_dn_style; however, the string
1181 * is maintained in a_dn_pat.
1184 if ( acl_mask_dn( op, e, val, a, matches,
1185 &b->a_dn, &op->o_ndn ) )
1191 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1194 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1195 b->a_realdn_pat.bv_val, 0, 0);
1197 * if access applies to the entry itself, and the
1198 * user is bound as somebody in the same namespace as
1199 * the entry, OR the given dn matches the dn pattern
1202 * NOTE: styles "anonymous", "users" and "self"
1203 * have been moved to enum slap_style_t, whose
1204 * value is set in a_dn_style; however, the string
1205 * is maintained in a_dn_pat.
1208 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1210 ndn = op->o_conn->c_ndn;
1215 if ( acl_mask_dn( op, e, val, a, matches,
1216 &b->a_realdn, &ndn ) )
1222 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1223 if ( ! op->o_conn->c_listener ) {
1226 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1227 b->a_sockurl_pat.bv_val, 0, 0 );
1229 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1230 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1231 if ( !regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1232 &e->e_nname, val, matches ) )
1237 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1239 char buf[ACL_BUF_SIZE];
1241 bv.bv_len = sizeof( buf ) - 1;
1243 if ( acl_string_expand( &bv, &b->a_sockurl_pat, &e->e_nname, val, matches ) )
1248 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1254 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1262 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1263 if ( !op->o_conn->c_peer_domain.bv_val ) {
1266 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1267 b->a_domain_pat.bv_val, 0, 0 );
1268 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1269 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1270 if ( !regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1271 &e->e_nname, val, matches ) )
1276 char buf[ACL_BUF_SIZE];
1278 struct berval cmp = op->o_conn->c_peer_domain;
1279 struct berval pat = b->a_domain_pat;
1281 if ( b->a_domain_expand ) {
1284 bv.bv_len = sizeof(buf) - 1;
1287 if ( acl_string_expand(&bv, &b->a_domain_pat, &e->e_nname, val, matches) )
1294 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1295 int offset = cmp.bv_len - pat.bv_len;
1300 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1304 /* trim the domain */
1305 cmp.bv_val = &cmp.bv_val[ offset ];
1306 cmp.bv_len -= offset;
1309 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1316 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1317 if ( !op->o_conn->c_peer_name.bv_val ) {
1320 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1321 b->a_peername_pat.bv_val, 0, 0 );
1322 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1323 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1324 if ( !regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1325 &e->e_nname, val, matches ) )
1331 /* try exact match */
1332 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1333 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1337 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1339 char buf[ACL_BUF_SIZE];
1341 bv.bv_len = sizeof( buf ) - 1;
1343 if ( acl_string_expand( &bv, &b->a_peername_pat, &e->e_nname, val, matches ) )
1348 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1352 /* extract IP and try exact match */
1353 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1355 char buf[STRLENOF("255.255.255.255") + 1];
1358 int port_number = -1;
1360 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1361 acl_bv_ip_eq.bv_val,
1362 acl_bv_ip_eq.bv_len ) != 0 )
1365 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1366 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1368 port = strrchr( ip.bv_val, ':' );
1370 ip.bv_len = port - ip.bv_val;
1372 if ( lutil_atoi( &port_number, port ) != 0 )
1376 /* the port check can be anticipated here */
1377 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1380 /* address longer than expected? */
1381 if ( ip.bv_len >= sizeof(buf) )
1384 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1385 buf[ ip.bv_len ] = '\0';
1387 addr = inet_addr( buf );
1389 /* unable to convert? */
1390 if ( addr == (unsigned long)(-1) )
1393 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1396 #ifdef LDAP_PF_INET6
1397 /* extract IPv6 and try exact match */
1398 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1400 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1402 struct in6_addr addr;
1403 int port_number = -1;
1405 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1406 acl_bv_ipv6_eq.bv_val,
1407 acl_bv_ipv6_eq.bv_len ) != 0 )
1410 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1411 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1413 port = strrchr( ip.bv_val, ']' );
1415 ip.bv_len = port - ip.bv_val;
1417 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1421 /* the port check can be anticipated here */
1422 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1425 /* address longer than expected? */
1426 if ( ip.bv_len >= sizeof(buf) )
1429 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1430 buf[ ip.bv_len ] = '\0';
1432 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1436 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1438 #endif /* LDAP_PF_INET6 */
1440 #ifdef LDAP_PF_LOCAL
1441 /* extract path and try exact match */
1442 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1445 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1446 acl_bv_path_eq.bv_val,
1447 acl_bv_path_eq.bv_len ) != 0 )
1450 path.bv_val = op->o_conn->c_peer_name.bv_val
1451 + acl_bv_path_eq.bv_len;
1452 path.bv_len = op->o_conn->c_peer_name.bv_len
1453 - acl_bv_path_eq.bv_len;
1455 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1458 #endif /* LDAP_PF_LOCAL */
1460 /* exact match (very unlikely...) */
1461 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1468 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1469 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1472 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1473 b->a_sockname_pat.bv_val, 0, 0 );
1474 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1475 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1476 if ( !regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1477 &e->e_nname, val, matches ) )
1482 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1484 char buf[ACL_BUF_SIZE];
1486 bv.bv_len = sizeof( buf ) - 1;
1488 if ( acl_string_expand( &bv, &b->a_sockname_pat, &e->e_nname, val, matches ) )
1493 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1498 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1505 if ( b->a_dn_at != NULL ) {
1506 if ( acl_mask_dnattr( op, e, val, a,
1508 &b->a_dn, &op->o_ndn ) )
1514 if ( b->a_realdn_at != NULL ) {
1517 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1519 ndn = op->o_conn->c_ndn;
1524 if ( acl_mask_dnattr( op, e, val, a,
1526 &b->a_realdn, &ndn ) )
1532 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1534 struct berval ndn = BER_BVNULL;
1537 if ( op->o_ndn.bv_len == 0 ) {
1541 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1542 b->a_group_pat.bv_val, 0, 0 );
1544 /* b->a_group is an unexpanded entry name, expanded it should be an
1545 * entry with objectclass group* and we test to see if odn is one of
1546 * the values in the attribute group
1548 /* see if asker is listed in dnattr */
1549 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1550 char buf[ACL_BUF_SIZE];
1551 AclRegexMatches tmp_matches,
1552 *tmp_matchesp = &tmp_matches;
1553 regmatch_t *tmp_data;
1555 MATCHES_MEMSET( &tmp_matches );
1556 tmp_data = &tmp_matches.dn_data[0];
1558 bv.bv_len = sizeof(buf) - 1;
1563 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1564 tmp_matchesp = matches;
1565 else switch ( a->acl_dn_style ) {
1566 case ACL_STYLE_REGEX:
1567 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1568 tmp_matchesp = matches;
1572 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1573 case ACL_STYLE_BASE:
1574 tmp_data[0].rm_so = 0;
1575 tmp_data[0].rm_eo = e->e_nname.bv_len;
1576 tmp_matches.dn_count = 1;
1580 case ACL_STYLE_SUBTREE:
1581 case ACL_STYLE_CHILDREN:
1582 tmp_data[0].rm_so = 0;
1583 tmp_data[0].rm_eo = e->e_nname.bv_len;
1585 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1586 tmp_data[1].rm_eo = e->e_nname.bv_len;
1587 tmp_matches.dn_count = 2;
1600 if ( acl_string_expand( &bv, &b->a_group_pat,
1607 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1608 op->o_tmpmemctx ) != LDAP_SUCCESS )
1610 /* did not expand to a valid dn */
1617 bv = b->a_group_pat;
1620 rc = backend_group( op, e, &bv, &op->o_ndn,
1621 b->a_group_oc, b->a_group_at );
1624 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1632 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1634 char buf[ACL_BUF_SIZE];
1636 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1637 b->a_set_pat.bv_val, 0, 0 );
1639 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1640 AclRegexMatches tmp_matches,
1641 *tmp_matchesp = &tmp_matches;
1643 regmatch_t *tmp_data;
1645 MATCHES_MEMSET( &tmp_matches );
1646 tmp_data = &tmp_matches.dn_data[0];
1648 bv.bv_len = sizeof( buf ) - 1;
1653 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1654 tmp_matchesp = matches;
1655 else switch ( a->acl_dn_style ) {
1656 case ACL_STYLE_REGEX:
1657 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1658 tmp_matchesp = matches;
1662 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1663 case ACL_STYLE_BASE:
1664 tmp_data[0].rm_so = 0;
1665 tmp_data[0].rm_eo = e->e_nname.bv_len;
1666 tmp_matches.dn_count = 1;
1670 case ACL_STYLE_SUBTREE:
1671 case ACL_STYLE_CHILDREN:
1672 tmp_data[0].rm_so = 0;
1673 tmp_data[0].rm_eo = e->e_nname.bv_len;
1674 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1675 tmp_data[1].rm_eo = e->e_nname.bv_len; tmp_matches.dn_count = 2;
1688 if ( acl_string_expand( &bv, &b->a_set_pat,
1699 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1704 if ( b->a_authz.sai_ssf ) {
1705 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1706 b->a_authz.sai_ssf, op->o_ssf, 0 );
1707 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1712 if ( b->a_authz.sai_transport_ssf ) {
1713 Debug( LDAP_DEBUG_ACL,
1714 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1715 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1716 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1721 if ( b->a_authz.sai_tls_ssf ) {
1722 Debug( LDAP_DEBUG_ACL,
1723 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1724 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1725 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1730 if ( b->a_authz.sai_sasl_ssf ) {
1731 Debug( LDAP_DEBUG_ACL,
1732 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1733 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1734 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1739 /* check for the "self" modifier in the <access> field */
1740 if ( b->a_dn.a_self ) {
1744 ACL_RECORD_VALUE_STATE;
1746 /* must have DN syntax */
1747 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1748 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1750 /* check if the target is an attribute. */
1751 if ( val == NULL ) continue;
1753 /* a DN must be present */
1754 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1758 /* target is attribute, check if the attribute value
1761 rc = value_match( &match, desc,
1762 desc->ad_type->sat_equality, 0,
1763 val, &op->o_ndn, &dummy );
1764 /* on match error or no match, fail the ACL clause */
1765 if ( rc != LDAP_SUCCESS || match != 0 )
1770 if ( b->a_dynacl ) {
1772 slap_access_t tgrant, tdeny;
1774 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1777 /* this case works different from the others above.
1778 * since dynamic ACL's themselves give permissions, we need
1779 * to first check b->a_access_mask, the ACL's access level.
1781 /* first check if the right being requested
1782 * is allowed by the ACL clause.
1784 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1788 /* start out with nothing granted, nothing denied */
1789 ACL_INVALIDATE(tgrant);
1790 ACL_INVALIDATE(tdeny);
1792 for ( da = b->a_dynacl; da; da = da->da_next ) {
1793 slap_access_t grant,
1796 ACL_INVALIDATE(grant);
1797 ACL_INVALIDATE(deny);
1799 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1800 da->da_name, 0, 0 );
1803 * XXXmanu Only DN matches are supplied
1804 * sending attribute values matches require
1807 (void)da->da_mask( da->da_private, op, e, desc,
1808 val, matches->dn_count, matches->dn_data,
1815 /* remove anything that the ACL clause does not allow */
1816 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1817 tdeny &= ACL_PRIV_MASK;
1819 /* see if we have anything to contribute */
1820 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1824 /* this could be improved by changing slap_acl_mask so that it can deal with
1825 * by clauses that return grant/deny pairs. Right now, it does either
1826 * additive or subtractive rights, but not both at the same time. So,
1827 * we need to combine the grant/deny pair into a single rights mask in
1828 * a smart way: if either grant or deny is "empty", then we use the
1829 * opposite as is, otherwise we remove any denied rights from the grant
1830 * rights mask and construct an additive mask.
1832 if (ACL_IS_INVALID(tdeny)) {
1833 modmask = tgrant | ACL_PRIV_ADDITIVE;
1835 } else if (ACL_IS_INVALID(tgrant)) {
1836 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1839 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1843 #endif /* SLAP_DYNACL */
1845 modmask = b->a_access_mask;
1848 Debug( LDAP_DEBUG_ACL,
1849 "<= acl_mask: [%d] applying %s (%s)\n",
1850 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1851 b->a_type == ACL_CONTINUE
1853 : b->a_type == ACL_BREAK
1859 if( ACL_IS_ADDITIVE(modmask) ) {
1861 ACL_PRIV_SET( *mask, modmask );
1864 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1866 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1867 /* substract privs */
1868 ACL_PRIV_CLR( *mask, modmask );
1871 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1878 Debug( LDAP_DEBUG_ACL,
1879 "<= acl_mask: [%d] mask: %s\n",
1880 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1882 if( b->a_type == ACL_CONTINUE ) {
1885 } else if ( b->a_type == ACL_BREAK ) {
1893 /* implicit "by * none" clause */
1896 Debug( LDAP_DEBUG_ACL,
1897 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1898 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1903 * acl_check_modlist - check access control on the given entry to see if
1904 * it allows the given modifications by the user associated with op.
1905 * returns 1 if mods allowed ok
1906 * 0 mods not allowed
1913 Modifications *mlist )
1916 AccessControlState state = ACL_STATE_INIT;
1919 int ret = 1; /* default is access allowed */
1923 be = LDAP_STAILQ_FIRST(&backendDB);
1927 assert( be != NULL );
1929 /* If ADD attribute checking is not enabled, just allow it */
1930 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1933 /* short circuit root database access */
1934 if ( be_isroot( op ) ) {
1935 Debug( LDAP_DEBUG_ACL,
1936 "<= acl_access_allowed: granted to database root\n",
1941 /* use backend default access if no backend acls */
1942 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1943 Debug( LDAP_DEBUG_ACL,
1944 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1945 access2str( ACL_WRITE ),
1946 op->o_bd->be_dfltaccess >= ACL_WRITE
1947 ? "granted" : "denied",
1949 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1953 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1955 * Internal mods are ignored by ACL_WRITE checking
1957 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1958 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1959 " modify access granted\n",
1960 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1965 * no-user-modification operational attributes are ignored
1966 * by ACL_WRITE checking as any found here are not provided
1969 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1970 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1972 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1973 " modify access granted\n",
1974 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1978 switch ( mlist->sml_op ) {
1979 case LDAP_MOD_REPLACE:
1980 case LDAP_MOD_INCREMENT:
1982 * We must check both permission to delete the whole
1983 * attribute and permission to add the specific attributes.
1984 * This prevents abuse from selfwriters.
1986 if ( ! access_allowed( op, e,
1987 mlist->sml_desc, NULL,
1988 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1995 if ( mlist->sml_values == NULL ) break;
1997 /* fall thru to check value to add */
2000 assert( mlist->sml_values != NULL );
2002 for ( bv = mlist->sml_nvalues
2003 ? mlist->sml_nvalues : mlist->sml_values;
2004 bv->bv_val != NULL; bv++ )
2006 if ( ! access_allowed( op, e,
2007 mlist->sml_desc, bv,
2008 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2017 case LDAP_MOD_DELETE:
2018 if ( mlist->sml_values == NULL ) {
2019 if ( ! access_allowed( op, e,
2020 mlist->sml_desc, NULL,
2021 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2029 for ( bv = mlist->sml_nvalues
2030 ? mlist->sml_nvalues : mlist->sml_values;
2031 bv->bv_val != NULL; bv++ )
2033 if ( ! access_allowed( op, e,
2034 mlist->sml_desc, bv,
2035 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2044 case SLAP_MOD_SOFTADD:
2045 /* allow adding attribute via modrdn thru */
2057 if (be_null) op->o_bd = NULL;
2063 struct berval *list,
2076 while ( len >= 0 && --ix >= 0 ) {
2077 while ( --len >= 0 && *p++ != sep )
2080 while ( len >= 0 && *p == ' ' ) {
2093 while ( --len >= 0 && *p != sep ) {
2097 while ( bv->bv_len > 0 && *--p == ' ' ) {
2104 typedef struct acl_set_gather_t {
2110 acl_set_cb_gather( Operation *op, SlapReply *rs )
2112 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2114 if ( rs->sr_type == REP_SEARCH ) {
2115 BerValue bvals[ 2 ];
2116 BerVarray bvalsp = NULL;
2119 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2120 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2122 if ( desc == NULL ) {
2126 if ( desc == slap_schema.si_ad_entryDN ) {
2128 bvals[ 0 ] = rs->sr_entry->e_nname;
2129 BER_BVZERO( &bvals[ 1 ] );
2134 a = attr_find( rs->sr_entry->e_attrs, desc );
2136 bvalsp = a->a_nvals;
2141 p->bvals = slap_set_join( p->cookie, p->bvals,
2142 ( '|' | SLAP_SET_RREF ), bvalsp );
2147 assert( rs->sr_type == REP_RESULT );
2154 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2156 AclSetCookie *cp = (AclSetCookie *)cookie;
2158 LDAPURLDesc *ludp = NULL;
2159 Operation op2 = { 0 };
2160 SlapReply rs = {REP_RESULT};
2161 AttributeName anlist[ 2 ], *anlistp = NULL;
2163 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2164 acl_set_gather_t p = { 0 };
2166 /* this routine needs to return the bervals instead of
2167 * plain strings, since syntax is not known. It should
2168 * also return the syntax or some "comparison cookie".
2170 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2171 return acl_set_gather2( cookie, name, desc );
2174 rc = ldap_url_parse( name->bv_val, &ludp );
2175 if ( rc != LDAP_URL_SUCCESS ) {
2176 Debug( LDAP_DEBUG_TRACE,
2177 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2178 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2180 rc = LDAP_PROTOCOL_ERROR;
2184 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2186 /* host part must be empty */
2187 /* extensions parts must be empty */
2188 Debug( LDAP_DEBUG_TRACE,
2189 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2190 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2192 rc = LDAP_PROTOCOL_ERROR;
2196 /* Grab the searchbase and see if an appropriate database can be found */
2197 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2198 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2199 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2200 BER_BVZERO( &op2.o_req_dn );
2201 if ( rc != LDAP_SUCCESS ) {
2202 Debug( LDAP_DEBUG_TRACE,
2203 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2204 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2209 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2210 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2211 Debug( LDAP_DEBUG_TRACE,
2212 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2213 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2215 rc = LDAP_NO_SUCH_OBJECT;
2219 /* Grab the filter */
2220 if ( ludp->lud_filter ) {
2221 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2222 cp->asc_op->o_tmpmemctx );
2223 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2224 if ( op2.ors_filter == NULL ) {
2225 Debug( LDAP_DEBUG_TRACE,
2226 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2227 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2229 rc = LDAP_PROTOCOL_ERROR;
2234 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2235 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2239 /* Grab the scope */
2240 op2.ors_scope = ludp->lud_scope;
2242 /* Grap the attributes */
2243 if ( ludp->lud_attrs ) {
2246 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2249 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2250 cp->asc_op->o_tmpmemctx );
2252 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2254 AttributeDescription *desc = NULL;
2255 const char *text = NULL;
2257 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2258 rc = slap_bv2ad( &name, &desc, &text );
2259 if ( rc == LDAP_SUCCESS ) {
2260 anlistp[ nattrs ].an_name = name;
2261 anlistp[ nattrs ].an_desc = desc;
2270 anlistp[ nattrs ].an_name = desc->ad_cname;
2271 anlistp[ nattrs ].an_desc = desc;
2273 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2277 op2.o_hdr = cp->asc_op->o_hdr;
2278 op2.o_tag = LDAP_REQ_SEARCH;
2279 op2.o_ndn = op2.o_bd->be_rootndn;
2280 op2.o_callback = &cb;
2281 slap_op_time( &op2.o_time, &op2.o_tincr );
2282 op2.o_do_not_cache = 1;
2283 op2.o_is_auth_check = 0;
2284 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2285 op2.ors_slimit = SLAP_NO_LIMIT;
2286 op2.ors_tlimit = SLAP_NO_LIMIT;
2287 op2.ors_attrs = anlistp;
2288 op2.ors_attrsonly = 0;
2289 op2.o_private = cp->asc_op->o_private;
2290 op2.o_extra = cp->asc_op->o_extra;
2294 rc = op2.o_bd->be_search( &op2, &rs );
2300 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2301 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2303 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2304 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2306 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2307 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2310 ldap_free_urldesc( ludp );
2312 if ( anlistp && anlistp != anlist ) {
2313 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2320 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2322 AclSetCookie *cp = (AclSetCookie *)cookie;
2323 BerVarray bvals = NULL;
2327 /* this routine needs to return the bervals instead of
2328 * plain strings, since syntax is not known. It should
2329 * also return the syntax or some "comparison cookie".
2331 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2332 if ( rc == LDAP_SUCCESS ) {
2333 if ( desc == slap_schema.si_ad_entryDN ) {
2334 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2335 cp->asc_op->o_tmpmemctx );
2337 BER_BVZERO( &bvals[ 1 ] );
2341 backend_attribute( cp->asc_op,
2342 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2345 if ( !BER_BVISNULL( &ndn ) ) {
2346 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2355 struct berval *subj,
2358 struct berval *default_set_attribute )
2360 struct berval set = BER_BVNULL;
2362 AclSetCookie cookie;
2364 if ( default_set_attribute == NULL ) {
2368 struct berval subjdn, ndn = BER_BVNULL;
2369 struct berval setat;
2370 BerVarray bvals = NULL;
2372 AttributeDescription *desc = NULL;
2374 /* format of string is "entry/setAttrName" */
2375 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2379 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2380 setat = *default_set_attribute;
2384 * NOTE: dnNormalize honors the ber_len field
2385 * as the length of the dn to be normalized
2387 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2388 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2390 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2391 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2395 BER_BVZERO( &bvals[0] );
2396 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2398 bvals[0].bv_val = bvals[i-1].bv_val;
2399 BER_BVZERO( &bvals[i-1] );
2401 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2402 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2407 if ( !BER_BVISNULL( &set ) ) {
2410 rc = ( slap_set_filter(
2412 (SetCookie *)&cookie, &set,
2413 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2414 if ( set.bv_val != subj->bv_val ) {
2415 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2425 * dynamic ACL infrastructure
2427 static slap_dynacl_t *da_list = NULL;
2430 slap_dynacl_register( slap_dynacl_t *da )
2434 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2435 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2440 if ( tmp != NULL ) {
2444 if ( da->da_mask == NULL ) {
2448 da->da_private = NULL;
2449 da->da_next = da_list;
2455 static slap_dynacl_t *
2456 slap_dynacl_next( slap_dynacl_t *da )
2465 slap_dynacl_get( const char *name )
2469 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2470 if ( strcasecmp( da->da_name, name ) == 0 ) {
2477 #endif /* SLAP_DYNACL */
2480 * statically built-in dynamic ACL initialization
2482 static int (*acl_init_func[])( void ) = {
2484 /* TODO: remove when ACI will only be dynamic */
2485 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2487 #endif /* SLAPD_ACI_ENABLED */
2488 #endif /* SLAP_DYNACL */
2498 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2499 rc = (*(acl_init_func[ i ]))();
2512 struct berval *dn_matches,
2513 struct berval *val_matches,
2514 AclRegexMatches *matches)
2520 enum { DN_FLAG, VAL_FLAG } tflag;
2523 bv->bv_val[0] = '\0';
2524 bv->bv_len--; /* leave space for lone $ */
2528 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2529 sp < pat->bv_val + pat->bv_len ; sp++ )
2531 /* did we previously see a $ */
2533 if ( flag == 1 && *sp == '$' ) {
2539 } else if ( flag == 2 && *sp == 'v' /*'}'*/) {
2542 } else if ( flag == 2 && *sp == 'd' /*'}'*/) {
2545 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2548 } else if ( *sp >= '0' && *sp <= '9' ) {
2559 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2560 if ( *sp >= '0' && *sp <= '9' ) {
2561 n = 10*n + ( *sp - '0' );
2565 if ( *sp != /*'{'*/ '}' ) {
2573 nm = matches->dn_count;
2574 m = matches->dn_data;
2575 data = dn_matches ? dn_matches->bv_val : NULL;
2578 nm = matches->val_count;
2579 m = matches->val_data;
2580 data = val_matches ? val_matches->bv_val : NULL;
2589 if ( data == NULL ) {
2598 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2617 /* must have ended with a single $ */
2625 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2626 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2633 struct berval *pat, /* pattern to expand and match against */
2634 char *str, /* string to match against pattern */
2635 struct berval *dn_matches, /* buffer with $N expansion variables from DN */
2636 struct berval *val_matches, /* buffer with $N expansion variables from val */
2637 AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
2641 char newbuf[ACL_BUF_SIZE];
2645 bv.bv_len = sizeof( newbuf ) - 1;
2652 acl_string_expand( &bv, pat, dn_matches, val_matches, matches );
2653 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2655 char error[ACL_BUF_SIZE];
2656 regerror( rc, &re, error, sizeof( error ) );
2658 Debug( LDAP_DEBUG_TRACE,
2659 "compile( \"%s\", \"%s\") failed %s\n",
2660 pat->bv_val, str, error );
2664 rc = regexec( &re, str, 0, NULL, 0 );
2667 Debug( LDAP_DEBUG_TRACE,
2668 "=> regex_matches: string: %s\n", str, 0, 0 );
2669 Debug( LDAP_DEBUG_TRACE,
2670 "=> regex_matches: rc: %d %s\n",
2671 rc, !rc ? "matches" : "no matches", 0 );