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-2006 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_path_eq = BER_BVC("PATH=");
45 #endif /* LDAP_PF_LOCAL */
47 static AccessControl * slap_acl_get(
48 AccessControl *ac, int *count,
49 Operation *op, Entry *e,
50 AttributeDescription *desc,
52 int nmatch, regmatch_t *matches,
53 AccessControlState *state );
55 static slap_control_t slap_acl_mask(
56 AccessControl *ac, slap_mask_t *mask,
57 Operation *op, Entry *e,
58 AttributeDescription *desc,
63 AccessControlState *state );
65 static int regex_matches(
66 struct berval *pat, char *str, char *buf,
67 int nmatch, regmatch_t *matches);
69 typedef struct AclSetCookie {
71 #define asc_op asc_cookie.set_op
75 SLAP_SET_GATHER acl_set_gather;
76 SLAP_SET_GATHER acl_set_gather2;
79 * access_allowed - check whether op->o_ndn is allowed the requested access
80 * to entry e, attribute attr, value val. if val is null, access to
81 * the whole attribute is assumed (all values).
83 * This routine loops through all access controls and calls
84 * slap_acl_mask() on each applicable access control.
85 * The loop exits when a definitive answer is reached or
86 * or no more controls remain.
93 * - can be legally called with op == NULL
94 * - can be legally called with op->o_bd == NULL
98 slap_access_always_allowed(
101 AttributeDescription *desc,
103 slap_access_t access,
104 AccessControlState *state,
107 assert( maskp != NULL );
110 ACL_LVL_ASSIGN_MANAGE( *maskp );
119 AttributeDescription *desc,
121 slap_access_t access,
122 AccessControlState *state,
127 AccessControl *a = NULL;
130 char accessmaskbuf[ACCESSMASK_MAXLEN];
133 slap_control_t control;
134 slap_access_t access_level;
136 regmatch_t matches[MAXREMATCHES];
138 assert( op != NULL );
140 assert( desc != NULL );
141 assert( maskp != NULL );
143 access_level = ACL_LEVEL( access );
144 attr = desc->ad_cname.bv_val;
146 assert( attr != NULL );
150 /* grant database root access */
151 if ( be_isroot( op ) ) {
152 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
153 mask = ACL_LVL_MANAGE;
158 * no-user-modification operational attributes are ignored
159 * by ACL_WRITE checking as any found here are not provided
162 * NOTE: but they are not ignored for ACL_MANAGE, because
163 * if we get here it means a non-root user is trying to
164 * manage data, so we need to check its privileges.
166 if ( access_level == ACL_WRITE
167 && is_at_no_user_mod( desc->ad_type )
168 && desc != slap_schema.si_ad_entry
169 && desc != slap_schema.si_ad_children )
171 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
172 " %s access granted\n",
177 /* use backend default access if no backend acls */
178 if ( op->o_bd->be_acl == NULL ) {
181 Debug( LDAP_DEBUG_ACL,
182 "=> slap_access_allowed: backend default %s "
183 "access %s to \"%s\"\n",
184 access2str( access ),
185 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
186 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
187 ret = op->o_bd->be_dfltaccess >= access_level;
189 mask = ACL_PRIV_LEVEL;
190 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
191 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
200 if ( state && state->as_vd_ad == desc ) {
201 a = state->as_vd_acl;
202 count = state->as_vd_acl_count;
205 if ( state ) state->as_vi_acl = NULL;
209 ACL_PRIV_ASSIGN( mask, *maskp );
210 memset( matches, '\0', sizeof( matches ) );
212 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
213 MAXREMATCHES, matches, state ) ) != NULL )
217 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
218 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
219 (int)matches[i].rm_so, (int)matches[i].rm_eo );
220 if ( matches[i].rm_so <= matches[0].rm_eo ) {
222 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
223 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
226 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
230 if ( state->as_vi_acl == a &&
231 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
233 Debug( LDAP_DEBUG_ACL,
234 "=> slap_access_allowed: result from state (%s)\n",
236 ret = state->as_result;
239 Debug( LDAP_DEBUG_ACL,
240 "=> slap_access_allowed: no res from state (%s)\n",
246 control = slap_acl_mask( a, &mask, op,
247 e, desc, val, MAXREMATCHES, matches, count, state );
249 if ( control != ACL_BREAK ) {
253 memset( matches, '\0', sizeof( matches ) );
256 if ( ACL_IS_INVALID( mask ) ) {
257 Debug( LDAP_DEBUG_ACL,
258 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
260 ACL_PRIV_ASSIGN( mask, *maskp );
262 } else if ( control == ACL_BREAK ) {
263 Debug( LDAP_DEBUG_ACL,
264 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
269 ret = ACL_GRANT( mask, access );
271 Debug( LDAP_DEBUG_ACL,
272 "=> slap_access_allowed: %s access %s by %s\n",
273 access2str( access ), ret ? "granted" : "denied",
274 accessmask2str( mask, accessmaskbuf, 1 ) );
277 ACL_PRIV_ASSIGN( *maskp, mask );
285 AttributeDescription *desc,
287 slap_access_t access,
288 AccessControlState *state,
295 * NOTE: control gets here if FIXME
296 * if an appropriate backend cannot be selected for the operation,
297 * we assume that the frontend should handle this
298 * FIXME: should select_backend() take care of this,
299 * and return frontendDB instead of NULL? maybe for some value
304 if ( op->o_bd == NULL ) {
305 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
306 if ( op->o_bd == NULL )
307 op->o_bd = frontendDB;
309 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
319 AttributeDescription *desc,
321 slap_access_t access,
322 AccessControlState *state,
326 AccessControl *a = NULL;
330 char accessmaskbuf[ACCESSMASK_MAXLEN];
333 slap_access_t access_level;
335 static AccessControlState state_init = ACL_STATE_INIT;
338 assert( desc != NULL );
340 access_level = ACL_LEVEL( access );
342 assert( access_level > ACL_NONE );
345 if ( maskp ) ACL_INVALIDATE( *maskp );
347 attr = desc->ad_cname.bv_val;
349 assert( attr != NULL );
352 if ( op->o_is_auth_check &&
353 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
357 } else if ( get_manageDIT( op ) && access_level == ACL_WRITE &&
358 desc == slap_schema.si_ad_entry )
365 if ( state->as_vd_ad == desc ) {
366 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
369 return state->as_result;
377 Debug( LDAP_DEBUG_ACL,
378 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
379 access2str( access ), e->e_dn, attr );
386 if ( op->o_bd == NULL ) {
387 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
392 * FIXME: experimental; use first backend rules
393 * iff there is no global_acl (ITS#3100) */
394 if ( frontendDB->be_acl != NULL ) {
395 op->o_bd = frontendDB;
397 #endif /* LDAP_DEVEL */
399 assert( op->o_bd != NULL );
401 /* this is enforced in backend_add() */
402 if ( op->o_bd->bd_info->bi_access_allowed ) {
403 /* delegate to backend */
404 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
405 desc, val, access, state, &mask );
408 /* use default (but pass through frontend
409 * for global ACL overlays) */
410 ret = frontendDB->bd_info->bi_access_allowed( op, e,
411 desc, val, access, state, &mask );
415 if ( ACL_IS_INVALID( mask ) ) {
416 Debug( LDAP_DEBUG_ACL,
417 "=> access_allowed: \"%s\" (%s) invalid!\n",
422 Debug( LDAP_DEBUG_ACL,
423 "=> access_allowed: no more rules\n", 0, 0, 0 );
429 Debug( LDAP_DEBUG_ACL,
430 "=> access_allowed: %s access %s by %s\n",
431 access2str( access ), ret ? "granted" : "denied",
432 accessmask2str( mask, accessmaskbuf, 1 ) );
435 if ( state != NULL ) {
436 /* If not value-dependent, save ACL in case of more attrs */
437 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
438 state->as_vi_acl = a;
439 state->as_result = ret;
441 state->as_recorded |= ACL_STATE_RECORDED;
442 state->as_vd_ad = desc;
444 if ( be_null ) op->o_bd = NULL;
445 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
451 * slap_acl_get - return the acl applicable to entry e, attribute
452 * attr. the acl returned is suitable for use in subsequent calls to
453 * acl_access_allowed().
456 static AccessControl *
462 AttributeDescription *desc,
466 AccessControlState *state )
473 assert( count != NULL );
474 assert( desc != NULL );
476 attr = desc->ad_cname.bv_val;
478 assert( attr != NULL );
481 if( op->o_bd == NULL ) {
482 a = frontendDB->be_acl;
484 a = op->o_bd->be_acl;
495 dnlen = e->e_nname.bv_len;
497 for ( ; a != NULL; prev = a, a = a->acl_next ) {
500 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
501 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
502 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
503 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
504 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
508 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
509 *count, a->acl_dn_pat.bv_val, 0 );
510 patlen = a->acl_dn_pat.bv_len;
511 if ( dnlen < patlen )
514 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
515 /* base dn -- entire object DN must match */
516 if ( dnlen != patlen )
519 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
520 ber_len_t rdnlen = 0;
523 if ( dnlen <= patlen )
527 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
532 rdnlen = dn_rdnlen( NULL, &e->e_nname );
533 if ( rdnlen != dnlen - patlen - sep )
536 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
537 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
540 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
541 if ( dnlen <= patlen )
543 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
547 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
551 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
555 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
556 matches[0].rm_so = matches[0].rm_eo = -1;
560 /* Is this ACL only for a specific value? */
561 if ( a->acl_attrval.bv_len ) {
566 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
567 state->as_recorded |= ACL_STATE_RECORDED_VD;
568 state->as_vd_acl = prev;
569 state->as_vd_acl_count = *count - 1;
572 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
573 Debug( LDAP_DEBUG_ACL,
574 "acl_get: valpat %s\n",
575 a->acl_attrval.bv_val, 0, 0 );
576 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
584 Debug( LDAP_DEBUG_ACL,
586 a->acl_attrval.bv_val, 0, 0 );
588 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
589 if (value_match( &match, desc,
590 a->acl_attrval_mr, 0,
591 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
598 patlen = a->acl_attrval.bv_len;
599 vdnlen = val->bv_len;
601 if ( vdnlen < patlen )
604 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
605 if ( vdnlen > patlen )
608 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
609 ber_len_t rdnlen = 0;
611 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
614 rdnlen = dn_rdnlen( NULL, val );
615 if ( rdnlen != vdnlen - patlen - 1 )
618 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
619 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
622 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
623 if ( vdnlen <= patlen )
626 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
630 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
636 if ( a->acl_filter != NULL ) {
637 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
638 if ( rc != LDAP_COMPARE_TRUE ) {
643 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
648 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
656 AttributeDescription *desc,
662 struct berval *opndn )
665 * if access applies to the entry itself, and the
666 * user is bound as somebody in the same namespace as
667 * the entry, OR the given dn matches the dn pattern
670 * NOTE: styles "anonymous", "users" and "self"
671 * have been moved to enum slap_style_t, whose
672 * value is set in a_dn_style; however, the string
673 * is maintaned in a_dn_pat.
676 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
677 if ( !BER_BVISEMPTY( opndn ) ) {
681 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
682 if ( BER_BVISEMPTY( opndn ) ) {
686 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
687 struct berval ndn, selfndn;
690 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
694 level = bdn->a_self_level;
702 selfndn = e->e_nname;
705 for ( ; level > 0; level-- ) {
706 if ( BER_BVISEMPTY( &ndn ) ) {
709 dnParent( &ndn, &ndn );
712 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
717 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
718 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
720 regmatch_t tmp_matches[2],
721 *tmp_matchesp = tmp_matches;
725 switch ( a->acl_dn_style ) {
726 case ACL_STYLE_REGEX:
727 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
728 tmp_matchesp = matches;
732 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
735 tmp_matches[0].rm_so = 0;
736 tmp_matches[0].rm_eo = e->e_nname.bv_len;
741 case ACL_STYLE_SUBTREE:
742 case ACL_STYLE_CHILDREN:
743 tmp_matches[0].rm_so = 0;
744 tmp_matches[0].rm_eo = e->e_nname.bv_len;
745 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
746 tmp_matches[1].rm_eo = e->e_nname.bv_len;
760 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
761 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
769 ber_len_t patlen, odnlen;
772 if ( e->e_dn == NULL )
775 if ( bdn->a_expand ) {
777 char buf[ACL_BUF_SIZE];
780 regmatch_t tmp_matches[2],
781 *tmp_matchesp = tmp_matches;
785 bv.bv_len = sizeof( buf ) - 1;
788 switch ( a->acl_dn_style ) {
789 case ACL_STYLE_REGEX:
790 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
791 tmp_matchesp = matches;
795 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
798 tmp_matches[0].rm_so = 0;
799 tmp_matches[0].rm_eo = e->e_nname.bv_len;
804 case ACL_STYLE_SUBTREE:
805 case ACL_STYLE_CHILDREN:
806 tmp_matches[0].rm_so = 0;
807 tmp_matches[0].rm_eo = e->e_nname.bv_len;
808 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
809 tmp_matches[1].rm_eo = e->e_nname.bv_len;
823 if ( acl_string_expand( &bv, &bdn->a_pat,
825 tmp_nmatch, tmp_matchesp ) )
830 if ( dnNormalize(0, NULL, NULL, &bv,
831 &pat, op->o_tmpmemctx )
834 /* did not expand to a valid dn */
843 odnlen = opndn->bv_len;
844 if ( odnlen < patlen ) {
845 goto dn_match_cleanup;
849 if ( bdn->a_style == ACL_STYLE_BASE ) {
850 /* base dn -- entire object DN must match */
851 if ( odnlen != patlen ) {
852 goto dn_match_cleanup;
855 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
856 ber_len_t rdnlen = 0;
858 if ( odnlen <= patlen ) {
859 goto dn_match_cleanup;
862 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
863 goto dn_match_cleanup;
866 rdnlen = dn_rdnlen( NULL, opndn );
867 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
868 goto dn_match_cleanup;
871 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
872 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
873 goto dn_match_cleanup;
876 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
877 if ( odnlen <= patlen ) {
878 goto dn_match_cleanup;
881 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
882 goto dn_match_cleanup;
885 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
886 int level = bdn->a_level;
889 if ( odnlen <= patlen ) {
890 goto dn_match_cleanup;
893 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
895 goto dn_match_cleanup;
899 for ( ; level > 0; level-- ) {
900 if ( BER_BVISEMPTY( &ndn ) ) {
901 goto dn_match_cleanup;
903 dnParent( &ndn, &ndn );
904 if ( ndn.bv_len < patlen ) {
905 goto dn_match_cleanup;
909 if ( ndn.bv_len != patlen ) {
910 goto dn_match_cleanup;
914 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
917 if ( pat.bv_val != bdn->a_pat.bv_val ) {
918 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
939 AccessControlState *state,
941 struct berval *opndn )
947 const char *attr = bdn->a_at->ad_cname.bv_val;
949 assert( attr != NULL );
951 if ( BER_BVISEMPTY( opndn ) ) {
955 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
958 /* see if asker is listed in dnattr */
959 for ( at = attrs_find( e->e_attrs, bdn->a_at );
961 at = attrs_find( at->a_next, bdn->a_at ) )
963 if ( value_find_ex( bdn->a_at,
964 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
965 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
967 &bv, op->o_tmpmemctx ) == 0 )
976 /* have a dnattr match. if this is a self clause then
977 * the target must also match the op dn.
980 /* check if the target is an attribute. */
981 if ( val == NULL ) return 1;
983 /* target is attribute, check if the attribute value
986 rc = value_match( &match, bdn->a_at,
987 bdn->a_at->ad_type->sat_equality, 0,
989 /* on match error or no match, fail the ACL clause */
990 if ( rc != LDAP_SUCCESS || match != 0 )
995 /* no dnattr match, check if this is a self clause */
999 /* this is a self clause, check if the target is an
1005 /* target is attribute, check if the attribute value
1008 rc = value_match( &match, bdn->a_at,
1009 bdn->a_at->ad_type->sat_equality, 0,
1012 /* on match error or no match, fail the ACL clause */
1013 if ( rc != LDAP_SUCCESS || match != 0 )
1022 * slap_acl_mask - modifies mask based upon the given acl and the
1023 * requested access to entry e, attribute attr, value val. if val
1024 * is null, access to the whole attribute is assumed (all values).
1026 * returns 0 access NOT allowed
1030 static slap_control_t
1036 AttributeDescription *desc,
1039 regmatch_t *matches,
1041 AccessControlState *state )
1046 char accessmaskbuf[ACCESSMASK_MAXLEN];
1049 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1051 assert( a != NULL );
1052 assert( mask != NULL );
1053 assert( desc != NULL );
1055 attr = desc->ad_cname.bv_val;
1057 assert( attr != NULL );
1059 Debug( LDAP_DEBUG_ACL,
1060 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1063 Debug( LDAP_DEBUG_ACL,
1064 "=> acl_mask: to %s by \"%s\", (%s) \n",
1065 val ? "value" : "all values",
1066 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1067 accessmask2str( *mask, accessmaskbuf, 1 ) );
1073 for ( ; b != NULL; b = b->a_next, i++ ) {
1074 slap_mask_t oldmask, modmask;
1076 ACL_INVALIDATE( modmask );
1078 /* AND <who> clauses */
1079 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1080 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1081 b->a_dn_pat.bv_val, 0, 0);
1083 * if access applies to the entry itself, and the
1084 * user is bound as somebody in the same namespace as
1085 * the entry, OR the given dn matches the dn pattern
1088 * NOTE: styles "anonymous", "users" and "self"
1089 * have been moved to enum slap_style_t, whose
1090 * value is set in a_dn_style; however, the string
1091 * is maintaned in a_dn_pat.
1094 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1095 &b->a_dn, &op->o_ndn ) )
1101 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1104 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1105 b->a_realdn_pat.bv_val, 0, 0);
1107 * if access applies to the entry itself, and the
1108 * user is bound as somebody in the same namespace as
1109 * the entry, OR the given dn matches the dn pattern
1112 * NOTE: styles "anonymous", "users" and "self"
1113 * have been moved to enum slap_style_t, whose
1114 * value is set in a_dn_style; however, the string
1115 * is maintaned in a_dn_pat.
1118 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1120 ndn = op->o_conn->c_ndn;
1125 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1126 &b->a_realdn, &ndn ) )
1132 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1133 if ( ! op->o_conn->c_listener ) {
1136 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1137 b->a_sockurl_pat.bv_val, 0, 0 );
1139 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1140 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1141 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1142 e->e_ndn, nmatch, matches ) )
1147 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1149 char buf[ACL_BUF_SIZE];
1151 bv.bv_len = sizeof( buf ) - 1;
1153 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1154 e->e_ndn, nmatch, matches ) )
1159 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1165 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1173 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1174 if ( !op->o_conn->c_peer_domain.bv_val ) {
1177 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1178 b->a_domain_pat.bv_val, 0, 0 );
1179 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1180 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1181 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1182 e->e_ndn, nmatch, matches ) )
1187 char buf[ACL_BUF_SIZE];
1189 struct berval cmp = op->o_conn->c_peer_domain;
1190 struct berval pat = b->a_domain_pat;
1192 if ( b->a_domain_expand ) {
1195 bv.bv_len = sizeof(buf) - 1;
1198 if ( acl_string_expand(&bv, &b->a_domain_pat,
1199 e->e_ndn, nmatch, matches) )
1206 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1207 int offset = cmp.bv_len - pat.bv_len;
1212 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1216 /* trim the domain */
1217 cmp.bv_val = &cmp.bv_val[ offset ];
1218 cmp.bv_len -= offset;
1221 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1228 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1229 if ( !op->o_conn->c_peer_name.bv_val ) {
1232 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1233 b->a_peername_pat.bv_val, 0, 0 );
1234 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1235 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1236 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1237 e->e_ndn, nmatch, matches ) )
1243 /* try exact match */
1244 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1245 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1249 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1251 char buf[ACL_BUF_SIZE];
1253 bv.bv_len = sizeof( buf ) - 1;
1255 if ( acl_string_expand( &bv, &b->a_peername_pat,
1256 e->e_ndn, nmatch, matches ) )
1261 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1265 /* extract IP and try exact match */
1266 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1268 char buf[] = "255.255.255.255";
1271 int port_number = -1;
1273 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1274 acl_bv_ip_eq.bv_val,
1275 acl_bv_ip_eq.bv_len ) != 0 )
1278 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1279 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1281 port = strrchr( ip.bv_val, ':' );
1283 ip.bv_len = port - ip.bv_val;
1285 if ( lutil_atoi( &port_number, port ) != 0 )
1289 /* the port check can be anticipated here */
1290 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1293 /* address longer than expected? */
1294 if ( ip.bv_len >= sizeof(buf) )
1297 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1298 buf[ ip.bv_len ] = '\0';
1300 addr = inet_addr( buf );
1302 /* unable to convert? */
1303 if ( addr == (unsigned long)(-1) )
1306 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1309 #ifdef LDAP_PF_LOCAL
1310 /* extract path and try exact match */
1311 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1314 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1315 acl_bv_path_eq.bv_val,
1316 acl_bv_path_eq.bv_len ) != 0 )
1319 path.bv_val = op->o_conn->c_peer_name.bv_val
1320 + acl_bv_path_eq.bv_len;
1321 path.bv_len = op->o_conn->c_peer_name.bv_len
1322 - acl_bv_path_eq.bv_len;
1324 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1327 #endif /* LDAP_PF_LOCAL */
1329 /* exact match (very unlikely...) */
1330 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1337 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1338 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1341 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1342 b->a_sockname_pat.bv_val, 0, 0 );
1343 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1344 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1345 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1346 e->e_ndn, nmatch, matches ) )
1351 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1353 char buf[ACL_BUF_SIZE];
1355 bv.bv_len = sizeof( buf ) - 1;
1357 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1358 e->e_ndn, nmatch, matches ) )
1363 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1368 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1375 if ( b->a_dn_at != NULL ) {
1376 if ( acl_mask_dnattr( op, e, val, a, b, i,
1377 matches, count, state,
1378 &b->a_dn, &op->o_ndn ) )
1384 if ( b->a_realdn_at != NULL ) {
1387 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1389 ndn = op->o_conn->c_ndn;
1394 if ( acl_mask_dnattr( op, e, val, a, b, i,
1395 matches, count, state,
1396 &b->a_realdn, &ndn ) )
1402 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1404 struct berval ndn = BER_BVNULL;
1407 if ( op->o_ndn.bv_len == 0 ) {
1411 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1412 b->a_group_pat.bv_val, 0, 0 );
1414 /* b->a_group is an unexpanded entry name, expanded it should be an
1415 * entry with objectclass group* and we test to see if odn is one of
1416 * the values in the attribute group
1418 /* see if asker is listed in dnattr */
1419 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1420 char buf[ACL_BUF_SIZE];
1422 regmatch_t tmp_matches[2],
1423 *tmp_matchesp = tmp_matches;
1425 bv.bv_len = sizeof(buf) - 1;
1430 switch ( a->acl_dn_style ) {
1431 case ACL_STYLE_REGEX:
1432 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1433 tmp_matchesp = matches;
1434 tmp_nmatch = nmatch;
1438 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1439 case ACL_STYLE_BASE:
1440 tmp_matches[0].rm_so = 0;
1441 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1446 case ACL_STYLE_SUBTREE:
1447 case ACL_STYLE_CHILDREN:
1448 tmp_matches[0].rm_so = 0;
1449 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1450 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1451 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1465 if ( acl_string_expand( &bv, &b->a_group_pat,
1467 tmp_nmatch, tmp_matchesp ) )
1472 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1473 op->o_tmpmemctx ) != LDAP_SUCCESS )
1475 /* did not expand to a valid dn */
1482 bv = b->a_group_pat;
1485 rc = backend_group( op, e, &bv, &op->o_ndn,
1486 b->a_group_oc, b->a_group_at );
1489 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1497 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1499 char buf[ACL_BUF_SIZE];
1501 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1502 b->a_set_pat.bv_val, 0, 0 );
1504 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1506 regmatch_t tmp_matches[2],
1507 *tmp_matchesp = tmp_matches;
1510 bv.bv_len = sizeof( buf ) - 1;
1515 switch ( a->acl_dn_style ) {
1516 case ACL_STYLE_REGEX:
1517 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1518 tmp_matchesp = matches;
1519 tmp_nmatch = nmatch;
1523 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1524 case ACL_STYLE_BASE:
1525 tmp_matches[0].rm_so = 0;
1526 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1531 case ACL_STYLE_SUBTREE:
1532 case ACL_STYLE_CHILDREN:
1533 tmp_matches[0].rm_so = 0;
1534 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1535 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1536 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1550 if ( acl_string_expand( &bv, &b->a_set_pat,
1552 tmp_nmatch, tmp_matchesp ) )
1561 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1566 if ( b->a_authz.sai_ssf ) {
1567 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1568 b->a_authz.sai_ssf, op->o_ssf, 0 );
1569 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1574 if ( b->a_authz.sai_transport_ssf ) {
1575 Debug( LDAP_DEBUG_ACL,
1576 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1577 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1578 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1583 if ( b->a_authz.sai_tls_ssf ) {
1584 Debug( LDAP_DEBUG_ACL,
1585 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1586 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1587 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1592 if ( b->a_authz.sai_sasl_ssf ) {
1593 Debug( LDAP_DEBUG_ACL,
1594 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1595 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1596 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1601 /* check for the "self" modifier in the <access> field */
1602 if ( b->a_dn.a_self ) {
1606 /* must have DN syntax */
1607 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1608 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1610 /* check if the target is an attribute. */
1611 if ( val == NULL ) continue;
1613 /* a DN must be present */
1614 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1618 /* target is attribute, check if the attribute value
1621 rc = value_match( &match, desc,
1622 desc->ad_type->sat_equality, 0,
1623 val, &op->o_ndn, &dummy );
1624 /* on match error or no match, fail the ACL clause */
1625 if ( rc != LDAP_SUCCESS || match != 0 )
1630 if ( b->a_dynacl ) {
1632 slap_access_t tgrant, tdeny;
1634 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1637 /* this case works different from the others above.
1638 * since dynamic ACL's themselves give permissions, we need
1639 * to first check b->a_access_mask, the ACL's access level.
1641 /* first check if the right being requested
1642 * is allowed by the ACL clause.
1644 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1648 /* start out with nothing granted, nothing denied */
1649 ACL_INVALIDATE(tgrant);
1650 ACL_INVALIDATE(tdeny);
1652 for ( da = b->a_dynacl; da; da = da->da_next ) {
1653 slap_access_t grant,
1656 ACL_INVALIDATE(grant);
1657 ACL_INVALIDATE(deny);
1659 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1660 da->da_name, 0, 0 );
1662 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1668 /* remove anything that the ACL clause does not allow */
1669 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1670 tdeny &= ACL_PRIV_MASK;
1672 /* see if we have anything to contribute */
1673 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1677 /* this could be improved by changing slap_acl_mask so that it can deal with
1678 * by clauses that return grant/deny pairs. Right now, it does either
1679 * additive or subtractive rights, but not both at the same time. So,
1680 * we need to combine the grant/deny pair into a single rights mask in
1681 * a smart way: if either grant or deny is "empty", then we use the
1682 * opposite as is, otherwise we remove any denied rights from the grant
1683 * rights mask and construct an additive mask.
1685 if (ACL_IS_INVALID(tdeny)) {
1686 modmask = tgrant | ACL_PRIV_ADDITIVE;
1688 } else if (ACL_IS_INVALID(tgrant)) {
1689 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1692 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1696 #endif /* SLAP_DYNACL */
1698 modmask = b->a_access_mask;
1701 Debug( LDAP_DEBUG_ACL,
1702 "<= acl_mask: [%d] applying %s (%s)\n",
1703 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1704 b->a_type == ACL_CONTINUE
1706 : b->a_type == ACL_BREAK
1712 if( ACL_IS_ADDITIVE(modmask) ) {
1714 ACL_PRIV_SET( *mask, modmask );
1717 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1719 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1720 /* substract privs */
1721 ACL_PRIV_CLR( *mask, modmask );
1724 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1733 Debug( LDAP_DEBUG_ACL,
1734 "<= acl_mask: [%d] mask: %s\n",
1735 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1737 if( b->a_type == ACL_CONTINUE ) {
1740 } else if ( b->a_type == ACL_BREAK ) {
1748 /* implicit "by * none" clause */
1751 Debug( LDAP_DEBUG_ACL,
1752 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1753 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1758 * acl_check_modlist - check access control on the given entry to see if
1759 * it allows the given modifications by the user associated with op.
1760 * returns 1 if mods allowed ok
1761 * 0 mods not allowed
1768 Modifications *mlist )
1771 AccessControlState state = ACL_STATE_INIT;
1774 int ret = 1; /* default is access allowed */
1778 be = LDAP_STAILQ_FIRST(&backendDB);
1782 assert( be != NULL );
1784 /* short circuit root database access */
1785 if ( be_isroot( op ) ) {
1786 Debug( LDAP_DEBUG_ACL,
1787 "<= acl_access_allowed: granted to database root\n",
1792 /* use backend default access if no backend acls */
1793 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1794 Debug( LDAP_DEBUG_ACL,
1795 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1796 access2str( ACL_WRITE ),
1797 op->o_bd->be_dfltaccess >= ACL_WRITE
1798 ? "granted" : "denied",
1800 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1804 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1806 * Internal mods are ignored by ACL_WRITE checking
1808 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1809 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1810 " modify access granted\n",
1811 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1816 * no-user-modification operational attributes are ignored
1817 * by ACL_WRITE checking as any found here are not provided
1820 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1821 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1823 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1824 " modify access granted\n",
1825 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1829 switch ( mlist->sml_op ) {
1830 case LDAP_MOD_REPLACE:
1831 case LDAP_MOD_INCREMENT:
1833 * We must check both permission to delete the whole
1834 * attribute and permission to add the specific attributes.
1835 * This prevents abuse from selfwriters.
1837 if ( ! access_allowed( op, e,
1838 mlist->sml_desc, NULL,
1839 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1846 if ( mlist->sml_values == NULL ) break;
1848 /* fall thru to check value to add */
1851 assert( mlist->sml_values != NULL );
1853 for ( bv = mlist->sml_nvalues
1854 ? mlist->sml_nvalues : mlist->sml_values;
1855 bv->bv_val != NULL; bv++ )
1857 if ( ! access_allowed( op, e,
1858 mlist->sml_desc, bv,
1859 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1868 case LDAP_MOD_DELETE:
1869 if ( mlist->sml_values == NULL ) {
1870 if ( ! access_allowed( op, e,
1871 mlist->sml_desc, NULL,
1872 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1880 for ( bv = mlist->sml_nvalues
1881 ? mlist->sml_nvalues : mlist->sml_values;
1882 bv->bv_val != NULL; bv++ )
1884 if ( ! access_allowed( op, e,
1885 mlist->sml_desc, bv,
1886 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1895 case SLAP_MOD_SOFTADD:
1896 /* allow adding attribute via modrdn thru */
1908 if (be_null) op->o_bd = NULL;
1914 struct berval *list,
1927 while ( len >= 0 && --ix >= 0 ) {
1928 while ( --len >= 0 && *p++ != sep )
1931 while ( len >= 0 && *p == ' ' ) {
1944 while ( --len >= 0 && *p != sep ) {
1948 while ( bv->bv_len > 0 && *--p == ' ' ) {
1955 typedef struct acl_set_gather_t {
1961 acl_set_cb_gather( Operation *op, SlapReply *rs )
1963 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
1965 if ( rs->sr_type == REP_SEARCH ) {
1966 BerValue bvals[ 2 ];
1967 BerVarray bvalsp = NULL;
1970 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1971 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
1973 if ( desc == slap_schema.si_ad_entryDN ) {
1975 bvals[ 0 ] = rs->sr_entry->e_nname;
1976 BER_BVZERO( &bvals[ 1 ] );
1981 a = attr_find( rs->sr_entry->e_attrs, desc );
1985 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1988 bvalsp = a->a_nvals;
1994 p->bvals = slap_set_join( p->cookie, p->bvals,
1995 ( '|' | SLAP_SET_RREF ), bvalsp );
1999 assert( rs->sr_type == REP_RESULT );
2006 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2008 AclSetCookie *cp = (AclSetCookie *)cookie;
2010 LDAPURLDesc *ludp = NULL;
2011 Operation op2 = { 0 };
2012 SlapReply rs = {REP_RESULT};
2013 AttributeName anlist[ 2 ], *anlistp = NULL;
2015 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2016 acl_set_gather_t p = { 0 };
2017 const char *text = NULL;
2018 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2020 /* this routine needs to return the bervals instead of
2021 * plain strings, since syntax is not known. It should
2022 * also return the syntax or some "comparison cookie".
2024 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2025 return acl_set_gather2( cookie, name, desc );
2028 rc = ldap_url_parse( name->bv_val, &ludp );
2029 if ( rc != LDAP_URL_SUCCESS ) {
2030 rc = LDAP_PROTOCOL_ERROR;
2034 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2036 /* host part must be empty */
2037 /* extensions parts must be empty */
2038 rc = LDAP_PROTOCOL_ERROR;
2042 /* Grab the searchbase and see if an appropriate database can be found */
2043 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2044 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2045 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2046 BER_BVZERO( &op2.o_req_dn );
2047 if ( rc != LDAP_SUCCESS ) {
2051 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2052 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2053 rc = LDAP_NO_SUCH_OBJECT;
2057 /* Grab the filter */
2058 if ( ludp->lud_filter ) {
2059 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2060 cp->asc_op->o_tmpmemctx );
2063 op2.ors_filterstr = defaultFilter_bv;
2066 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2067 if ( op2.ors_filter == NULL ) {
2068 rc = LDAP_PROTOCOL_ERROR;
2072 /* Grab the scope */
2073 op2.ors_scope = ludp->lud_scope;
2075 /* Grap the attributes */
2076 if ( ludp->lud_attrs ) {
2077 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2080 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2081 cp->asc_op->o_tmpmemctx );
2083 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2084 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2085 anlistp[ nattrs ].an_desc = NULL;
2086 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2087 &anlistp[ nattrs ].an_desc, &text );
2088 if ( rc != LDAP_SUCCESS ) {
2097 anlistp[ nattrs ].an_name = desc->ad_cname;
2098 anlistp[ nattrs ].an_desc = desc;
2100 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2104 op2.o_hdr = cp->asc_op->o_hdr;
2105 op2.o_tag = LDAP_REQ_SEARCH;
2106 op2.o_ndn = op2.o_bd->be_rootndn;
2107 op2.o_callback = &cb;
2108 slap_op_time( &op2.o_time, &op2.o_tincr );
2109 op2.o_do_not_cache = 1;
2110 op2.o_is_auth_check = 0;
2111 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2112 op2.ors_slimit = SLAP_NO_LIMIT;
2113 op2.ors_tlimit = SLAP_NO_LIMIT;
2114 op2.ors_attrs = anlistp;
2115 op2.ors_attrsonly = 0;
2116 op2.o_private = cp->asc_op->o_private;
2120 rc = op2.o_bd->be_search( &op2, &rs );
2126 if ( op2.ors_filter ) {
2127 filter_free_x( cp->asc_op, op2.ors_filter );
2129 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2130 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2132 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2133 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2136 ldap_free_urldesc( ludp );
2138 if ( anlistp && anlistp != anlist ) {
2139 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2146 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2148 AclSetCookie *cp = (AclSetCookie *)cookie;
2149 BerVarray bvals = NULL;
2153 /* this routine needs to return the bervals instead of
2154 * plain strings, since syntax is not known. It should
2155 * also return the syntax or some "comparison cookie".
2157 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2158 if ( rc == LDAP_SUCCESS ) {
2159 if ( desc == slap_schema.si_ad_entryDN ) {
2160 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2161 cp->asc_op->o_tmpmemctx );
2163 BER_BVZERO( &bvals[ 1 ] );
2167 backend_attribute( cp->asc_op,
2168 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2171 if ( !BER_BVISNULL( &ndn ) ) {
2172 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2181 struct berval *subj,
2184 struct berval *default_set_attribute )
2186 struct berval set = BER_BVNULL;
2188 AclSetCookie cookie;
2190 if ( default_set_attribute == NULL ) {
2191 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2194 struct berval subjdn, ndn = BER_BVNULL;
2195 struct berval setat;
2196 BerVarray bvals = NULL;
2198 AttributeDescription *desc = NULL;
2200 /* format of string is "entry/setAttrName" */
2201 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2205 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2206 setat = *default_set_attribute;
2210 * NOTE: dnNormalize honors the ber_len field
2211 * as the length of the dn to be normalized
2213 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2214 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2216 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2217 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2221 BER_BVZERO( &bvals[0] );
2222 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2224 bvals[0].bv_val = bvals[i-1].bv_val;
2225 BER_BVZERO( &bvals[i-1] );
2227 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2228 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2233 if ( !BER_BVISNULL( &set ) ) {
2236 rc = ( slap_set_filter(
2238 (SetCookie *)&cookie, &set,
2239 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2240 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2249 * dynamic ACL infrastructure
2251 static slap_dynacl_t *da_list = NULL;
2254 slap_dynacl_register( slap_dynacl_t *da )
2258 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2259 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2264 if ( tmp != NULL ) {
2268 if ( da->da_mask == NULL ) {
2272 da->da_private = NULL;
2273 da->da_next = da_list;
2279 static slap_dynacl_t *
2280 slap_dynacl_next( slap_dynacl_t *da )
2289 slap_dynacl_get( const char *name )
2293 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2294 if ( strcasecmp( da->da_name, name ) == 0 ) {
2301 #endif /* SLAP_DYNACL */
2304 * statically built-in dynamic ACL initialization
2306 static int (*acl_init_func[])( void ) = {
2308 /* TODO: remove when ACI will only be dynamic */
2309 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2311 #endif /* SLAPD_ACI_ENABLED */
2312 #endif /* SLAP_DYNACL */
2322 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2323 rc = (*(acl_init_func[ i ]))();
2338 regmatch_t *matches)
2346 bv->bv_val[0] = '\0';
2347 bv->bv_len--; /* leave space for lone $ */
2350 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2351 sp < pat->bv_val + pat->bv_len ; sp++ )
2353 /* did we previously see a $ */
2355 if ( flag == 1 && *sp == '$' ) {
2360 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2363 } else if ( *sp >= '0' && *sp <= '9' ) {
2371 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2372 if ( *sp >= '0' && *sp <= '9' ) {
2373 n = 10*n + ( *sp - '0' );
2377 if ( *sp != /*'{'*/ '}' ) {
2383 if ( n >= nmatch ) {
2389 i = matches[n].rm_so;
2390 l = matches[n].rm_eo;
2391 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2409 /* must have ended with a single $ */
2417 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2418 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2425 struct berval *pat, /* pattern to expand and match against */
2426 char *str, /* string to match against pattern */
2427 char *buf, /* buffer with $N expansion variables */
2428 int nmatch, /* size of the matches array */
2429 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2433 char newbuf[ACL_BUF_SIZE];
2437 bv.bv_len = sizeof( newbuf ) - 1;
2444 acl_string_expand( &bv, pat, buf, nmatch, matches );
2445 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2447 char error[ACL_BUF_SIZE];
2448 regerror( rc, &re, error, sizeof( error ) );
2450 Debug( LDAP_DEBUG_TRACE,
2451 "compile( \"%s\", \"%s\") failed %s\n",
2452 pat->bv_val, str, error );
2456 rc = regexec( &re, str, 0, NULL, 0 );
2459 Debug( LDAP_DEBUG_TRACE,
2460 "=> regex_matches: string: %s\n", str, 0, 0 );
2461 Debug( LDAP_DEBUG_TRACE,
2462 "=> regex_matches: rc: %d %s\n",
2463 rc, !rc ? "matches" : "no matches", 0 );