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_relax( 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 );
390 /* FIXME: experimental; use first backend rules
391 * iff there is no global_acl (ITS#3100)
393 if ( frontendDB->be_acl != NULL ) {
394 op->o_bd = frontendDB;
397 assert( op->o_bd != NULL );
399 /* this is enforced in backend_add() */
400 if ( op->o_bd->bd_info->bi_access_allowed ) {
401 /* delegate to backend */
402 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
403 desc, val, access, state, &mask );
406 /* use default (but pass through frontend
407 * for global ACL overlays) */
408 ret = frontendDB->bd_info->bi_access_allowed( op, e,
409 desc, val, access, state, &mask );
413 if ( ACL_IS_INVALID( mask ) ) {
414 Debug( LDAP_DEBUG_ACL,
415 "=> access_allowed: \"%s\" (%s) invalid!\n",
420 Debug( LDAP_DEBUG_ACL,
421 "=> access_allowed: no more rules\n", 0, 0, 0 );
427 Debug( LDAP_DEBUG_ACL,
428 "=> access_allowed: %s access %s by %s\n",
429 access2str( access ), ret ? "granted" : "denied",
430 accessmask2str( mask, accessmaskbuf, 1 ) );
433 if ( state != NULL ) {
434 /* If not value-dependent, save ACL in case of more attrs */
435 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
436 state->as_vi_acl = a;
437 state->as_result = ret;
439 state->as_recorded |= ACL_STATE_RECORDED;
440 state->as_vd_ad = desc;
442 if ( be_null ) op->o_bd = NULL;
443 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
449 * slap_acl_get - return the acl applicable to entry e, attribute
450 * attr. the acl returned is suitable for use in subsequent calls to
451 * acl_access_allowed().
454 static AccessControl *
460 AttributeDescription *desc,
464 AccessControlState *state )
471 assert( count != NULL );
472 assert( desc != NULL );
474 attr = desc->ad_cname.bv_val;
476 assert( attr != NULL );
479 if( op->o_bd == NULL ) {
480 a = frontendDB->be_acl;
482 a = op->o_bd->be_acl;
493 dnlen = e->e_nname.bv_len;
495 for ( ; a != NULL; prev = a, a = a->acl_next ) {
498 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
499 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
500 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
501 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
502 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
506 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
507 *count, a->acl_dn_pat.bv_val, 0 );
508 patlen = a->acl_dn_pat.bv_len;
509 if ( dnlen < patlen )
512 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
513 /* base dn -- entire object DN must match */
514 if ( dnlen != patlen )
517 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
518 ber_len_t rdnlen = 0;
521 if ( dnlen <= patlen )
525 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
530 rdnlen = dn_rdnlen( NULL, &e->e_nname );
531 if ( rdnlen != dnlen - patlen - sep )
534 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
535 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
538 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
539 if ( dnlen <= patlen )
541 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
545 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
549 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
553 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
554 matches[0].rm_so = matches[0].rm_eo = -1;
558 /* Is this ACL only for a specific value? */
559 if ( a->acl_attrval.bv_len ) {
564 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
565 state->as_recorded |= ACL_STATE_RECORDED_VD;
566 state->as_vd_acl = prev;
567 state->as_vd_acl_count = *count - 1;
570 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
571 Debug( LDAP_DEBUG_ACL,
572 "acl_get: valpat %s\n",
573 a->acl_attrval.bv_val, 0, 0 );
574 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
582 Debug( LDAP_DEBUG_ACL,
584 a->acl_attrval.bv_val, 0, 0 );
586 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
587 if (value_match( &match, desc,
588 a->acl_attrval_mr, 0,
589 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
596 patlen = a->acl_attrval.bv_len;
597 vdnlen = val->bv_len;
599 if ( vdnlen < patlen )
602 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
603 if ( vdnlen > patlen )
606 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
607 ber_len_t rdnlen = 0;
609 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
612 rdnlen = dn_rdnlen( NULL, val );
613 if ( rdnlen != vdnlen - patlen - 1 )
616 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
617 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
620 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
621 if ( vdnlen <= patlen )
624 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
628 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
634 if ( a->acl_filter != NULL ) {
635 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
636 if ( rc != LDAP_COMPARE_TRUE ) {
641 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
646 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
651 * Record value-dependent access control state
653 #define ACL_RECORD_VALUE_STATE do { \
654 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
655 state->as_recorded |= ACL_STATE_RECORDED_VD; \
656 state->as_vd_acl = a; \
657 state->as_vd_acl_count = count; \
665 AttributeDescription *desc,
671 struct berval *opndn )
674 * if access applies to the entry itself, and the
675 * user is bound as somebody in the same namespace as
676 * the entry, OR the given dn matches the dn pattern
679 * NOTE: styles "anonymous", "users" and "self"
680 * have been moved to enum slap_style_t, whose
681 * value is set in a_dn_style; however, the string
682 * is maintained in a_dn_pat.
685 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
686 if ( !BER_BVISEMPTY( opndn ) ) {
690 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
691 if ( BER_BVISEMPTY( opndn ) ) {
695 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
696 struct berval ndn, selfndn;
699 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
703 level = bdn->a_self_level;
711 selfndn = e->e_nname;
714 for ( ; level > 0; level-- ) {
715 if ( BER_BVISEMPTY( &ndn ) ) {
718 dnParent( &ndn, &ndn );
721 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
726 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
727 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
729 regmatch_t tmp_matches[2],
730 *tmp_matchesp = tmp_matches;
734 switch ( a->acl_dn_style ) {
735 case ACL_STYLE_REGEX:
736 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
737 tmp_matchesp = matches;
741 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
744 tmp_matches[0].rm_so = 0;
745 tmp_matches[0].rm_eo = e->e_nname.bv_len;
750 case ACL_STYLE_SUBTREE:
751 case ACL_STYLE_CHILDREN:
752 tmp_matches[0].rm_so = 0;
753 tmp_matches[0].rm_eo = e->e_nname.bv_len;
754 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
755 tmp_matches[1].rm_eo = e->e_nname.bv_len;
769 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
770 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
778 ber_len_t patlen, odnlen;
781 if ( e->e_dn == NULL )
784 if ( bdn->a_expand ) {
786 char buf[ACL_BUF_SIZE];
789 regmatch_t tmp_matches[2],
790 *tmp_matchesp = tmp_matches;
794 bv.bv_len = sizeof( buf ) - 1;
797 switch ( a->acl_dn_style ) {
798 case ACL_STYLE_REGEX:
799 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
800 tmp_matchesp = matches;
804 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
807 tmp_matches[0].rm_so = 0;
808 tmp_matches[0].rm_eo = e->e_nname.bv_len;
813 case ACL_STYLE_SUBTREE:
814 case ACL_STYLE_CHILDREN:
815 tmp_matches[0].rm_so = 0;
816 tmp_matches[0].rm_eo = e->e_nname.bv_len;
817 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
818 tmp_matches[1].rm_eo = e->e_nname.bv_len;
832 if ( acl_string_expand( &bv, &bdn->a_pat,
834 tmp_nmatch, tmp_matchesp ) )
839 if ( dnNormalize(0, NULL, NULL, &bv,
840 &pat, op->o_tmpmemctx )
843 /* did not expand to a valid dn */
852 odnlen = opndn->bv_len;
853 if ( odnlen < patlen ) {
854 goto dn_match_cleanup;
858 if ( bdn->a_style == ACL_STYLE_BASE ) {
859 /* base dn -- entire object DN must match */
860 if ( odnlen != patlen ) {
861 goto dn_match_cleanup;
864 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
865 ber_len_t rdnlen = 0;
867 if ( odnlen <= patlen ) {
868 goto dn_match_cleanup;
871 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
872 goto dn_match_cleanup;
875 rdnlen = dn_rdnlen( NULL, opndn );
876 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
877 goto dn_match_cleanup;
880 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
881 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
882 goto dn_match_cleanup;
885 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
886 if ( odnlen <= patlen ) {
887 goto dn_match_cleanup;
890 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
891 goto dn_match_cleanup;
894 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
895 int level = bdn->a_level;
898 if ( odnlen <= patlen ) {
899 goto dn_match_cleanup;
902 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
904 goto dn_match_cleanup;
908 for ( ; level > 0; level-- ) {
909 if ( BER_BVISEMPTY( &ndn ) ) {
910 goto dn_match_cleanup;
912 dnParent( &ndn, &ndn );
913 if ( ndn.bv_len < patlen ) {
914 goto dn_match_cleanup;
918 if ( ndn.bv_len != patlen ) {
919 goto dn_match_cleanup;
923 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
926 if ( pat.bv_val != bdn->a_pat.bv_val ) {
927 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
948 AccessControlState *state,
950 struct berval *opndn )
956 const char *attr = bdn->a_at->ad_cname.bv_val;
958 assert( attr != NULL );
960 if ( BER_BVISEMPTY( opndn ) ) {
964 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
967 /* see if asker is listed in dnattr */
968 for ( at = attrs_find( e->e_attrs, bdn->a_at );
970 at = attrs_find( at->a_next, bdn->a_at ) )
972 if ( value_find_ex( bdn->a_at,
973 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
974 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
976 &bv, op->o_tmpmemctx ) == 0 )
985 /* have a dnattr match. if this is a self clause then
986 * the target must also match the op dn.
989 /* check if the target is an attribute. */
990 if ( val == NULL ) return 1;
992 /* target is attribute, check if the attribute value
995 rc = value_match( &match, bdn->a_at,
996 bdn->a_at->ad_type->sat_equality, 0,
998 /* on match error or no match, fail the ACL clause */
999 if ( rc != LDAP_SUCCESS || match != 0 )
1004 /* no dnattr match, check if this is a self clause */
1005 if ( ! bdn->a_self )
1008 ACL_RECORD_VALUE_STATE;
1010 /* this is a self clause, check if the target is an
1016 /* target is attribute, check if the attribute value
1019 rc = value_match( &match, bdn->a_at,
1020 bdn->a_at->ad_type->sat_equality, 0,
1023 /* on match error or no match, fail the ACL clause */
1024 if ( rc != LDAP_SUCCESS || match != 0 )
1033 * slap_acl_mask - modifies mask based upon the given acl and the
1034 * requested access to entry e, attribute attr, value val. if val
1035 * is null, access to the whole attribute is assumed (all values).
1037 * returns 0 access NOT allowed
1041 static slap_control_t
1047 AttributeDescription *desc,
1050 regmatch_t *matches,
1052 AccessControlState *state )
1057 char accessmaskbuf[ACCESSMASK_MAXLEN];
1060 slap_mask_t a2pmask = ACL_ACCESS2PRIV( *mask );
1062 assert( a != NULL );
1063 assert( mask != NULL );
1064 assert( desc != NULL );
1066 attr = desc->ad_cname.bv_val;
1068 assert( attr != NULL );
1070 Debug( LDAP_DEBUG_ACL,
1071 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1074 Debug( LDAP_DEBUG_ACL,
1075 "=> acl_mask: to %s by \"%s\", (%s) \n",
1076 val ? "value" : "all values",
1077 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1078 accessmask2str( *mask, accessmaskbuf, 1 ) );
1084 for ( ; b != NULL; b = b->a_next, i++ ) {
1085 slap_mask_t oldmask, modmask;
1087 ACL_INVALIDATE( modmask );
1089 /* AND <who> clauses */
1090 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1091 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1092 b->a_dn_pat.bv_val, 0, 0);
1094 * if access applies to the entry itself, and the
1095 * user is bound as somebody in the same namespace as
1096 * the entry, OR the given dn matches the dn pattern
1099 * NOTE: styles "anonymous", "users" and "self"
1100 * have been moved to enum slap_style_t, whose
1101 * value is set in a_dn_style; however, the string
1102 * is maintained in a_dn_pat.
1105 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1106 &b->a_dn, &op->o_ndn ) )
1112 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1115 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1116 b->a_realdn_pat.bv_val, 0, 0);
1118 * if access applies to the entry itself, and the
1119 * user is bound as somebody in the same namespace as
1120 * the entry, OR the given dn matches the dn pattern
1123 * NOTE: styles "anonymous", "users" and "self"
1124 * have been moved to enum slap_style_t, whose
1125 * value is set in a_dn_style; however, the string
1126 * is maintained in a_dn_pat.
1129 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1131 ndn = op->o_conn->c_ndn;
1136 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1137 &b->a_realdn, &ndn ) )
1143 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1144 if ( ! op->o_conn->c_listener ) {
1147 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1148 b->a_sockurl_pat.bv_val, 0, 0 );
1150 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1151 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1152 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1153 e->e_ndn, nmatch, matches ) )
1158 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1160 char buf[ACL_BUF_SIZE];
1162 bv.bv_len = sizeof( buf ) - 1;
1164 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1165 e->e_ndn, nmatch, matches ) )
1170 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1176 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1184 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1185 if ( !op->o_conn->c_peer_domain.bv_val ) {
1188 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1189 b->a_domain_pat.bv_val, 0, 0 );
1190 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1191 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1192 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1193 e->e_ndn, nmatch, matches ) )
1198 char buf[ACL_BUF_SIZE];
1200 struct berval cmp = op->o_conn->c_peer_domain;
1201 struct berval pat = b->a_domain_pat;
1203 if ( b->a_domain_expand ) {
1206 bv.bv_len = sizeof(buf) - 1;
1209 if ( acl_string_expand(&bv, &b->a_domain_pat,
1210 e->e_ndn, nmatch, matches) )
1217 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1218 int offset = cmp.bv_len - pat.bv_len;
1223 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1227 /* trim the domain */
1228 cmp.bv_val = &cmp.bv_val[ offset ];
1229 cmp.bv_len -= offset;
1232 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1239 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1240 if ( !op->o_conn->c_peer_name.bv_val ) {
1243 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1244 b->a_peername_pat.bv_val, 0, 0 );
1245 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1246 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1247 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1248 e->e_ndn, nmatch, matches ) )
1254 /* try exact match */
1255 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1256 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1260 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1262 char buf[ACL_BUF_SIZE];
1264 bv.bv_len = sizeof( buf ) - 1;
1266 if ( acl_string_expand( &bv, &b->a_peername_pat,
1267 e->e_ndn, nmatch, matches ) )
1272 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1276 /* extract IP and try exact match */
1277 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1279 char buf[] = "255.255.255.255";
1282 int port_number = -1;
1284 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1285 acl_bv_ip_eq.bv_val,
1286 acl_bv_ip_eq.bv_len ) != 0 )
1289 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1290 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1292 port = strrchr( ip.bv_val, ':' );
1294 ip.bv_len = port - ip.bv_val;
1296 if ( lutil_atoi( &port_number, port ) != 0 )
1300 /* the port check can be anticipated here */
1301 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1304 /* address longer than expected? */
1305 if ( ip.bv_len >= sizeof(buf) )
1308 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1309 buf[ ip.bv_len ] = '\0';
1311 addr = inet_addr( buf );
1313 /* unable to convert? */
1314 if ( addr == (unsigned long)(-1) )
1317 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1320 #ifdef LDAP_PF_LOCAL
1321 /* extract path and try exact match */
1322 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1325 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1326 acl_bv_path_eq.bv_val,
1327 acl_bv_path_eq.bv_len ) != 0 )
1330 path.bv_val = op->o_conn->c_peer_name.bv_val
1331 + acl_bv_path_eq.bv_len;
1332 path.bv_len = op->o_conn->c_peer_name.bv_len
1333 - acl_bv_path_eq.bv_len;
1335 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1338 #endif /* LDAP_PF_LOCAL */
1340 /* exact match (very unlikely...) */
1341 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1348 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1349 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1352 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1353 b->a_sockname_pat.bv_val, 0, 0 );
1354 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1355 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1356 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1357 e->e_ndn, nmatch, matches ) )
1362 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1364 char buf[ACL_BUF_SIZE];
1366 bv.bv_len = sizeof( buf ) - 1;
1368 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1369 e->e_ndn, nmatch, matches ) )
1374 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1379 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1386 if ( b->a_dn_at != NULL ) {
1387 if ( acl_mask_dnattr( op, e, val, a, b, i,
1388 matches, count, state,
1389 &b->a_dn, &op->o_ndn ) )
1395 if ( b->a_realdn_at != NULL ) {
1398 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1400 ndn = op->o_conn->c_ndn;
1405 if ( acl_mask_dnattr( op, e, val, a, b, i,
1406 matches, count, state,
1407 &b->a_realdn, &ndn ) )
1413 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1415 struct berval ndn = BER_BVNULL;
1418 if ( op->o_ndn.bv_len == 0 ) {
1422 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1423 b->a_group_pat.bv_val, 0, 0 );
1425 /* b->a_group is an unexpanded entry name, expanded it should be an
1426 * entry with objectclass group* and we test to see if odn is one of
1427 * the values in the attribute group
1429 /* see if asker is listed in dnattr */
1430 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1431 char buf[ACL_BUF_SIZE];
1433 regmatch_t tmp_matches[2],
1434 *tmp_matchesp = tmp_matches;
1436 bv.bv_len = sizeof(buf) - 1;
1441 switch ( a->acl_dn_style ) {
1442 case ACL_STYLE_REGEX:
1443 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1444 tmp_matchesp = matches;
1445 tmp_nmatch = nmatch;
1449 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1450 case ACL_STYLE_BASE:
1451 tmp_matches[0].rm_so = 0;
1452 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1457 case ACL_STYLE_SUBTREE:
1458 case ACL_STYLE_CHILDREN:
1459 tmp_matches[0].rm_so = 0;
1460 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1461 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1462 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1476 if ( acl_string_expand( &bv, &b->a_group_pat,
1478 tmp_nmatch, tmp_matchesp ) )
1483 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1484 op->o_tmpmemctx ) != LDAP_SUCCESS )
1486 /* did not expand to a valid dn */
1493 bv = b->a_group_pat;
1496 rc = backend_group( op, e, &bv, &op->o_ndn,
1497 b->a_group_oc, b->a_group_at );
1500 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1508 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1510 char buf[ACL_BUF_SIZE];
1512 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1513 b->a_set_pat.bv_val, 0, 0 );
1515 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1517 regmatch_t tmp_matches[2],
1518 *tmp_matchesp = tmp_matches;
1521 bv.bv_len = sizeof( buf ) - 1;
1526 switch ( a->acl_dn_style ) {
1527 case ACL_STYLE_REGEX:
1528 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1529 tmp_matchesp = matches;
1530 tmp_nmatch = nmatch;
1534 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1535 case ACL_STYLE_BASE:
1536 tmp_matches[0].rm_so = 0;
1537 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1542 case ACL_STYLE_SUBTREE:
1543 case ACL_STYLE_CHILDREN:
1544 tmp_matches[0].rm_so = 0;
1545 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1546 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1547 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1561 if ( acl_string_expand( &bv, &b->a_set_pat,
1563 tmp_nmatch, tmp_matchesp ) )
1572 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1577 if ( b->a_authz.sai_ssf ) {
1578 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1579 b->a_authz.sai_ssf, op->o_ssf, 0 );
1580 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1585 if ( b->a_authz.sai_transport_ssf ) {
1586 Debug( LDAP_DEBUG_ACL,
1587 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1588 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1589 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1594 if ( b->a_authz.sai_tls_ssf ) {
1595 Debug( LDAP_DEBUG_ACL,
1596 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1597 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1598 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1603 if ( b->a_authz.sai_sasl_ssf ) {
1604 Debug( LDAP_DEBUG_ACL,
1605 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1606 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1607 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1612 /* check for the "self" modifier in the <access> field */
1613 if ( b->a_dn.a_self ) {
1617 ACL_RECORD_VALUE_STATE;
1619 /* must have DN syntax */
1620 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1621 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1623 /* check if the target is an attribute. */
1624 if ( val == NULL ) continue;
1626 /* a DN must be present */
1627 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1631 /* target is attribute, check if the attribute value
1634 rc = value_match( &match, desc,
1635 desc->ad_type->sat_equality, 0,
1636 val, &op->o_ndn, &dummy );
1637 /* on match error or no match, fail the ACL clause */
1638 if ( rc != LDAP_SUCCESS || match != 0 )
1643 if ( b->a_dynacl ) {
1645 slap_access_t tgrant, tdeny;
1647 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1650 /* this case works different from the others above.
1651 * since dynamic ACL's themselves give permissions, we need
1652 * to first check b->a_access_mask, the ACL's access level.
1654 /* first check if the right being requested
1655 * is allowed by the ACL clause.
1657 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1661 /* start out with nothing granted, nothing denied */
1662 ACL_INVALIDATE(tgrant);
1663 ACL_INVALIDATE(tdeny);
1665 for ( da = b->a_dynacl; da; da = da->da_next ) {
1666 slap_access_t grant,
1669 ACL_INVALIDATE(grant);
1670 ACL_INVALIDATE(deny);
1672 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1673 da->da_name, 0, 0 );
1675 (void)da->da_mask( da->da_private, op, e, desc,
1676 val, nmatch, matches, &grant, &deny );
1682 /* remove anything that the ACL clause does not allow */
1683 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1684 tdeny &= ACL_PRIV_MASK;
1686 /* see if we have anything to contribute */
1687 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1691 /* this could be improved by changing slap_acl_mask so that it can deal with
1692 * by clauses that return grant/deny pairs. Right now, it does either
1693 * additive or subtractive rights, but not both at the same time. So,
1694 * we need to combine the grant/deny pair into a single rights mask in
1695 * a smart way: if either grant or deny is "empty", then we use the
1696 * opposite as is, otherwise we remove any denied rights from the grant
1697 * rights mask and construct an additive mask.
1699 if (ACL_IS_INVALID(tdeny)) {
1700 modmask = tgrant | ACL_PRIV_ADDITIVE;
1702 } else if (ACL_IS_INVALID(tgrant)) {
1703 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1706 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1710 #endif /* SLAP_DYNACL */
1712 modmask = b->a_access_mask;
1715 Debug( LDAP_DEBUG_ACL,
1716 "<= acl_mask: [%d] applying %s (%s)\n",
1717 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1718 b->a_type == ACL_CONTINUE
1720 : b->a_type == ACL_BREAK
1726 if( ACL_IS_ADDITIVE(modmask) ) {
1728 ACL_PRIV_SET( *mask, modmask );
1731 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1733 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1734 /* substract privs */
1735 ACL_PRIV_CLR( *mask, modmask );
1738 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1747 Debug( LDAP_DEBUG_ACL,
1748 "<= acl_mask: [%d] mask: %s\n",
1749 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1751 if( b->a_type == ACL_CONTINUE ) {
1754 } else if ( b->a_type == ACL_BREAK ) {
1762 /* implicit "by * none" clause */
1765 Debug( LDAP_DEBUG_ACL,
1766 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1767 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1772 * acl_check_modlist - check access control on the given entry to see if
1773 * it allows the given modifications by the user associated with op.
1774 * returns 1 if mods allowed ok
1775 * 0 mods not allowed
1782 Modifications *mlist )
1785 AccessControlState state = ACL_STATE_INIT;
1788 int ret = 1; /* default is access allowed */
1792 be = LDAP_STAILQ_FIRST(&backendDB);
1796 assert( be != NULL );
1798 /* short circuit root database access */
1799 if ( be_isroot( op ) ) {
1800 Debug( LDAP_DEBUG_ACL,
1801 "<= acl_access_allowed: granted to database root\n",
1806 /* use backend default access if no backend acls */
1807 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1808 Debug( LDAP_DEBUG_ACL,
1809 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1810 access2str( ACL_WRITE ),
1811 op->o_bd->be_dfltaccess >= ACL_WRITE
1812 ? "granted" : "denied",
1814 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1818 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1820 * Internal mods are ignored by ACL_WRITE checking
1822 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1823 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1824 " modify access granted\n",
1825 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1830 * no-user-modification operational attributes are ignored
1831 * by ACL_WRITE checking as any found here are not provided
1834 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1835 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1837 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1838 " modify access granted\n",
1839 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1843 switch ( mlist->sml_op ) {
1844 case LDAP_MOD_REPLACE:
1845 case LDAP_MOD_INCREMENT:
1847 * We must check both permission to delete the whole
1848 * attribute and permission to add the specific attributes.
1849 * This prevents abuse from selfwriters.
1851 if ( ! access_allowed( op, e,
1852 mlist->sml_desc, NULL,
1853 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1860 if ( mlist->sml_values == NULL ) break;
1862 /* fall thru to check value to add */
1865 assert( mlist->sml_values != NULL );
1867 for ( bv = mlist->sml_nvalues
1868 ? mlist->sml_nvalues : mlist->sml_values;
1869 bv->bv_val != NULL; bv++ )
1871 if ( ! access_allowed( op, e,
1872 mlist->sml_desc, bv,
1873 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1882 case LDAP_MOD_DELETE:
1883 if ( mlist->sml_values == NULL ) {
1884 if ( ! access_allowed( op, e,
1885 mlist->sml_desc, NULL,
1886 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1894 for ( bv = mlist->sml_nvalues
1895 ? mlist->sml_nvalues : mlist->sml_values;
1896 bv->bv_val != NULL; bv++ )
1898 if ( ! access_allowed( op, e,
1899 mlist->sml_desc, bv,
1900 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1909 case SLAP_MOD_SOFTADD:
1910 /* allow adding attribute via modrdn thru */
1922 if (be_null) op->o_bd = NULL;
1928 struct berval *list,
1941 while ( len >= 0 && --ix >= 0 ) {
1942 while ( --len >= 0 && *p++ != sep )
1945 while ( len >= 0 && *p == ' ' ) {
1958 while ( --len >= 0 && *p != sep ) {
1962 while ( bv->bv_len > 0 && *--p == ' ' ) {
1969 typedef struct acl_set_gather_t {
1975 acl_set_cb_gather( Operation *op, SlapReply *rs )
1977 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
1979 if ( rs->sr_type == REP_SEARCH ) {
1980 BerValue bvals[ 2 ];
1981 BerVarray bvalsp = NULL;
1984 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1985 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
1987 if ( desc == slap_schema.si_ad_entryDN ) {
1989 bvals[ 0 ] = rs->sr_entry->e_nname;
1990 BER_BVZERO( &bvals[ 1 ] );
1995 a = attr_find( rs->sr_entry->e_attrs, desc );
1999 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2002 bvalsp = a->a_nvals;
2008 p->bvals = slap_set_join( p->cookie, p->bvals,
2009 ( '|' | SLAP_SET_RREF ), bvalsp );
2013 assert( rs->sr_type == REP_RESULT );
2020 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2022 AclSetCookie *cp = (AclSetCookie *)cookie;
2024 LDAPURLDesc *ludp = NULL;
2025 Operation op2 = { 0 };
2026 SlapReply rs = {REP_RESULT};
2027 AttributeName anlist[ 2 ], *anlistp = NULL;
2029 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2030 acl_set_gather_t p = { 0 };
2031 const char *text = NULL;
2032 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2034 /* this routine needs to return the bervals instead of
2035 * plain strings, since syntax is not known. It should
2036 * also return the syntax or some "comparison cookie".
2038 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2039 return acl_set_gather2( cookie, name, desc );
2042 rc = ldap_url_parse( name->bv_val, &ludp );
2043 if ( rc != LDAP_URL_SUCCESS ) {
2044 rc = LDAP_PROTOCOL_ERROR;
2048 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2050 /* host part must be empty */
2051 /* extensions parts must be empty */
2052 rc = LDAP_PROTOCOL_ERROR;
2056 /* Grab the searchbase and see if an appropriate database can be found */
2057 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2058 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2059 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2060 BER_BVZERO( &op2.o_req_dn );
2061 if ( rc != LDAP_SUCCESS ) {
2065 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2066 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2067 rc = LDAP_NO_SUCH_OBJECT;
2071 /* Grab the filter */
2072 if ( ludp->lud_filter ) {
2073 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2074 cp->asc_op->o_tmpmemctx );
2077 op2.ors_filterstr = defaultFilter_bv;
2080 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2081 if ( op2.ors_filter == NULL ) {
2082 rc = LDAP_PROTOCOL_ERROR;
2086 /* Grab the scope */
2087 op2.ors_scope = ludp->lud_scope;
2089 /* Grap the attributes */
2090 if ( ludp->lud_attrs ) {
2091 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2094 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2095 cp->asc_op->o_tmpmemctx );
2097 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2098 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2099 anlistp[ nattrs ].an_desc = NULL;
2100 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2101 &anlistp[ nattrs ].an_desc, &text );
2102 if ( rc != LDAP_SUCCESS ) {
2111 anlistp[ nattrs ].an_name = desc->ad_cname;
2112 anlistp[ nattrs ].an_desc = desc;
2114 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2118 op2.o_hdr = cp->asc_op->o_hdr;
2119 op2.o_tag = LDAP_REQ_SEARCH;
2120 op2.o_ndn = op2.o_bd->be_rootndn;
2121 op2.o_callback = &cb;
2122 slap_op_time( &op2.o_time, &op2.o_tincr );
2123 op2.o_do_not_cache = 1;
2124 op2.o_is_auth_check = 0;
2125 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2126 op2.ors_slimit = SLAP_NO_LIMIT;
2127 op2.ors_tlimit = SLAP_NO_LIMIT;
2128 op2.ors_attrs = anlistp;
2129 op2.ors_attrsonly = 0;
2130 op2.o_private = cp->asc_op->o_private;
2134 rc = op2.o_bd->be_search( &op2, &rs );
2140 if ( op2.ors_filter ) {
2141 filter_free_x( cp->asc_op, op2.ors_filter );
2143 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2144 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2146 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2147 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2150 ldap_free_urldesc( ludp );
2152 if ( anlistp && anlistp != anlist ) {
2153 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2160 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2162 AclSetCookie *cp = (AclSetCookie *)cookie;
2163 BerVarray bvals = NULL;
2167 /* this routine needs to return the bervals instead of
2168 * plain strings, since syntax is not known. It should
2169 * also return the syntax or some "comparison cookie".
2171 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2172 if ( rc == LDAP_SUCCESS ) {
2173 if ( desc == slap_schema.si_ad_entryDN ) {
2174 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2175 cp->asc_op->o_tmpmemctx );
2177 BER_BVZERO( &bvals[ 1 ] );
2181 backend_attribute( cp->asc_op,
2182 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2185 if ( !BER_BVISNULL( &ndn ) ) {
2186 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2195 struct berval *subj,
2198 struct berval *default_set_attribute )
2200 struct berval set = BER_BVNULL;
2202 AclSetCookie cookie;
2204 if ( default_set_attribute == NULL ) {
2205 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2208 struct berval subjdn, ndn = BER_BVNULL;
2209 struct berval setat;
2210 BerVarray bvals = NULL;
2212 AttributeDescription *desc = NULL;
2214 /* format of string is "entry/setAttrName" */
2215 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2219 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2220 setat = *default_set_attribute;
2224 * NOTE: dnNormalize honors the ber_len field
2225 * as the length of the dn to be normalized
2227 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2228 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2230 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2231 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2235 BER_BVZERO( &bvals[0] );
2236 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2238 bvals[0].bv_val = bvals[i-1].bv_val;
2239 BER_BVZERO( &bvals[i-1] );
2241 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2242 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2247 if ( !BER_BVISNULL( &set ) ) {
2250 rc = ( slap_set_filter(
2252 (SetCookie *)&cookie, &set,
2253 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2254 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2263 * dynamic ACL infrastructure
2265 static slap_dynacl_t *da_list = NULL;
2268 slap_dynacl_register( slap_dynacl_t *da )
2272 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2273 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2278 if ( tmp != NULL ) {
2282 if ( da->da_mask == NULL ) {
2286 da->da_private = NULL;
2287 da->da_next = da_list;
2293 static slap_dynacl_t *
2294 slap_dynacl_next( slap_dynacl_t *da )
2303 slap_dynacl_get( const char *name )
2307 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2308 if ( strcasecmp( da->da_name, name ) == 0 ) {
2315 #endif /* SLAP_DYNACL */
2318 * statically built-in dynamic ACL initialization
2320 static int (*acl_init_func[])( void ) = {
2322 /* TODO: remove when ACI will only be dynamic */
2323 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2325 #endif /* SLAPD_ACI_ENABLED */
2326 #endif /* SLAP_DYNACL */
2336 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2337 rc = (*(acl_init_func[ i ]))();
2352 regmatch_t *matches)
2360 bv->bv_val[0] = '\0';
2361 bv->bv_len--; /* leave space for lone $ */
2364 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2365 sp < pat->bv_val + pat->bv_len ; sp++ )
2367 /* did we previously see a $ */
2369 if ( flag == 1 && *sp == '$' ) {
2374 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2377 } else if ( *sp >= '0' && *sp <= '9' ) {
2385 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2386 if ( *sp >= '0' && *sp <= '9' ) {
2387 n = 10*n + ( *sp - '0' );
2391 if ( *sp != /*'{'*/ '}' ) {
2397 if ( n >= nmatch ) {
2403 i = matches[n].rm_so;
2404 l = matches[n].rm_eo;
2405 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2423 /* must have ended with a single $ */
2431 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2432 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2439 struct berval *pat, /* pattern to expand and match against */
2440 char *str, /* string to match against pattern */
2441 char *buf, /* buffer with $N expansion variables */
2442 int nmatch, /* size of the matches array */
2443 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2447 char newbuf[ACL_BUF_SIZE];
2451 bv.bv_len = sizeof( newbuf ) - 1;
2458 acl_string_expand( &bv, pat, buf, nmatch, matches );
2459 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2461 char error[ACL_BUF_SIZE];
2462 regerror( rc, &re, error, sizeof( error ) );
2464 Debug( LDAP_DEBUG_TRACE,
2465 "compile( \"%s\", \"%s\") failed %s\n",
2466 pat->bv_val, str, error );
2470 rc = regexec( &re, str, 0, NULL, 0 );
2473 Debug( LDAP_DEBUG_TRACE,
2474 "=> regex_matches: string: %s\n", str, 0, 0 );
2475 Debug( LDAP_DEBUG_TRACE,
2476 "=> regex_matches: rc: %d %s\n",
2477 rc, !rc ? "matches" : "no matches", 0 );