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 int nmatch, regmatch_t *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,
66 AccessControlState *state,
67 slap_access_t access );
69 static int regex_matches(
70 struct berval *pat, char *str, char *buf,
71 int nmatch, regmatch_t *matches);
73 typedef struct AclSetCookie {
75 #define asc_op asc_cookie.set_op
79 SLAP_SET_GATHER acl_set_gather;
80 SLAP_SET_GATHER acl_set_gather2;
83 * access_allowed - check whether op->o_ndn is allowed the requested access
84 * to entry e, attribute attr, value val. if val is null, access to
85 * the whole attribute is assumed (all values).
87 * This routine loops through all access controls and calls
88 * slap_acl_mask() on each applicable access control.
89 * The loop exits when a definitive answer is reached or
90 * or no more controls remain.
97 * - can be legally called with op == NULL
98 * - can be legally called with op->o_bd == NULL
102 slap_access_always_allowed(
105 AttributeDescription *desc,
107 slap_access_t access,
108 AccessControlState *state,
111 assert( maskp != NULL );
114 ACL_LVL_ASSIGN_MANAGE( *maskp );
123 AttributeDescription *desc,
125 slap_access_t access,
126 AccessControlState *state,
131 AccessControl *a = NULL;
134 char accessmaskbuf[ACCESSMASK_MAXLEN];
137 slap_control_t control;
138 slap_access_t access_level;
140 regmatch_t matches[MAXREMATCHES];
141 AccessControlState acl_state = ACL_STATE_INIT;
143 assert( op != NULL );
145 assert( desc != NULL );
146 assert( maskp != NULL );
148 access_level = ACL_LEVEL( access );
149 attr = desc->ad_cname.bv_val;
151 assert( attr != NULL );
155 /* grant database root access */
156 if ( be_isroot( op ) ) {
157 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
158 mask = ACL_LVL_MANAGE;
163 * no-user-modification operational attributes are ignored
164 * by ACL_WRITE checking as any found here are not provided
167 * NOTE: but they are not ignored for ACL_MANAGE, because
168 * if we get here it means a non-root user is trying to
169 * manage data, so we need to check its privileges.
171 if ( access_level == ACL_WRITE
172 && is_at_no_user_mod( desc->ad_type )
173 && desc != slap_schema.si_ad_entry
174 && desc != slap_schema.si_ad_children )
176 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
177 " %s access granted\n",
182 /* use backend default access if no backend acls */
183 if ( op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
186 Debug( LDAP_DEBUG_ACL,
187 "=> slap_access_allowed: backend default %s "
188 "access %s to \"%s\"\n",
189 access2str( access ),
190 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
191 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
192 ret = op->o_bd->be_dfltaccess >= access_level;
194 mask = ACL_PRIV_LEVEL;
195 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
196 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
207 if ( state->as_vd_ad == desc ) {
208 a = state->as_vd_acl;
209 count = state->as_vd_acl_count;
210 if ( state->as_fe_done )
213 state->as_vi_acl = NULL;
219 state->as_fe_done = 0;
221 ACL_PRIV_ASSIGN( mask, *maskp );
222 memset( matches, '\0', sizeof( matches ) );
224 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
225 MAXREMATCHES, matches, state ) ) != NULL )
229 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
230 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
231 (int)matches[i].rm_so, (int)matches[i].rm_eo );
232 if ( matches[i].rm_so <= matches[0].rm_eo ) {
234 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
235 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
238 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
242 if ( state->as_vi_acl == a &&
243 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
245 Debug( LDAP_DEBUG_ACL,
246 "=> slap_access_allowed: result was in cache (%s)\n",
248 ret = state->as_result;
251 Debug( LDAP_DEBUG_ACL,
252 "=> slap_access_allowed: result not in cache (%s)\n",
257 control = slap_acl_mask( a, &mask, op,
258 e, desc, val, MAXREMATCHES, matches, count, state, access );
260 if ( control != ACL_BREAK ) {
264 memset( matches, '\0', sizeof( matches ) );
267 if ( ACL_IS_INVALID( mask ) ) {
268 Debug( LDAP_DEBUG_ACL,
269 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
271 ACL_PRIV_ASSIGN( mask, *maskp );
273 } else if ( control == ACL_BREAK ) {
274 Debug( LDAP_DEBUG_ACL,
275 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
280 ret = ACL_GRANT( mask, access );
282 Debug( LDAP_DEBUG_ACL,
283 "=> slap_access_allowed: %s access %s by %s\n",
284 access2str( access ), ret ? "granted" : "denied",
285 accessmask2str( mask, accessmaskbuf, 1 ) );
288 ACL_PRIV_ASSIGN( *maskp, mask );
296 AttributeDescription *desc,
298 slap_access_t access,
299 AccessControlState *state,
306 * NOTE: control gets here if FIXME
307 * if an appropriate backend cannot be selected for the operation,
308 * we assume that the frontend should handle this
309 * FIXME: should select_backend() take care of this,
310 * and return frontendDB instead of NULL? maybe for some value
315 if ( op->o_bd == NULL ) {
316 op->o_bd = select_backend( &op->o_req_ndn, 0 );
317 if ( op->o_bd == NULL )
318 op->o_bd = frontendDB;
320 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
330 AttributeDescription *desc,
332 slap_access_t access,
333 AccessControlState *state,
337 AccessControl *a = NULL;
341 char accessmaskbuf[ACCESSMASK_MAXLEN];
344 slap_access_t access_level;
346 static AccessControlState state_init = ACL_STATE_INIT;
349 assert( desc != NULL );
351 access_level = ACL_LEVEL( access );
353 assert( access_level > ACL_NONE );
356 if ( maskp ) ACL_INVALIDATE( *maskp );
358 attr = desc->ad_cname.bv_val;
360 assert( attr != NULL );
363 if ( op->o_acl_priv != ACL_NONE ) {
364 access = op->o_acl_priv;
366 } else if ( op->o_is_auth_check &&
367 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
371 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
372 desc == slap_schema.si_ad_entry )
379 if ( state->as_vd_ad == desc ) {
380 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
383 return state->as_result;
391 Debug( LDAP_DEBUG_ACL,
392 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
393 access2str( access ), e->e_dn, attr );
400 if ( op->o_bd == NULL ) {
401 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
404 /* FIXME: experimental; use first backend rules
405 * iff there is no global_acl (ITS#3100)
407 if ( frontendDB->be_acl != NULL ) {
408 op->o_bd = frontendDB;
411 assert( op->o_bd != NULL );
413 /* this is enforced in backend_add() */
414 if ( op->o_bd->bd_info->bi_access_allowed ) {
415 /* delegate to backend */
416 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
417 desc, val, access, state, &mask );
420 /* use default (but pass through frontend
421 * for global ACL overlays) */
422 ret = frontendDB->bd_info->bi_access_allowed( op, e,
423 desc, val, access, state, &mask );
427 if ( ACL_IS_INVALID( mask ) ) {
428 Debug( LDAP_DEBUG_ACL,
429 "=> access_allowed: \"%s\" (%s) invalid!\n",
434 Debug( LDAP_DEBUG_ACL,
435 "=> access_allowed: no more rules\n", 0, 0, 0 );
441 Debug( LDAP_DEBUG_ACL,
442 "=> access_allowed: %s access %s by %s\n",
443 access2str( access ), ret ? "granted" : "denied",
444 accessmask2str( mask, accessmaskbuf, 1 ) );
447 if ( state != NULL ) {
448 /* If not value-dependent, save ACL in case of more attrs */
449 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
450 state->as_vi_acl = a;
451 state->as_result = ret;
453 state->as_recorded |= ACL_STATE_RECORDED;
454 state->as_vd_ad = desc;
456 if ( be_null ) op->o_bd = NULL;
457 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
463 * slap_acl_get - return the acl applicable to entry e, attribute
464 * attr. the acl returned is suitable for use in subsequent calls to
465 * acl_access_allowed().
468 static AccessControl *
474 AttributeDescription *desc,
478 AccessControlState *state )
485 assert( count != NULL );
486 assert( desc != NULL );
487 assert( state != NULL );
489 attr = desc->ad_cname.bv_val;
491 assert( attr != NULL );
494 if( op->o_bd == NULL || op->o_bd->be_acl == NULL ) {
495 a = frontendDB->be_acl;
497 a = op->o_bd->be_acl;
502 if ( a == frontendDB->be_acl )
503 state->as_fe_done = 1;
509 dnlen = e->e_nname.bv_len;
512 for ( ; a != NULL; prev = a, a = a->acl_next ) {
515 if ( a != frontendDB->be_acl && state->as_fe_done )
518 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
519 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
520 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
521 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
522 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
526 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
527 *count, a->acl_dn_pat.bv_val, 0 );
528 patlen = a->acl_dn_pat.bv_len;
529 if ( dnlen < patlen )
532 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
533 /* base dn -- entire object DN must match */
534 if ( dnlen != patlen )
537 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
538 ber_len_t rdnlen = 0;
541 if ( dnlen <= patlen )
545 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
550 rdnlen = dn_rdnlen( NULL, &e->e_nname );
551 if ( rdnlen != dnlen - patlen - sep )
554 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
555 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
558 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
559 if ( dnlen <= patlen )
561 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
565 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
569 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
573 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
574 matches[0].rm_so = matches[0].rm_eo = -1;
578 /* Is this ACL only for a specific value? */
579 if ( a->acl_attrval.bv_len ) {
584 if( !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
585 state->as_recorded |= ACL_STATE_RECORDED_VD;
586 state->as_vd_acl = prev;
587 state->as_vd_acl_count = *count - 1;
590 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
591 Debug( LDAP_DEBUG_ACL,
592 "acl_get: valpat %s\n",
593 a->acl_attrval.bv_val, 0, 0 );
594 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
602 Debug( LDAP_DEBUG_ACL,
604 a->acl_attrval.bv_val, 0, 0 );
606 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
607 if (value_match( &match, desc,
608 a->acl_attrval_mr, 0,
609 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
616 patlen = a->acl_attrval.bv_len;
617 vdnlen = val->bv_len;
619 if ( vdnlen < patlen )
622 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
623 if ( vdnlen > patlen )
626 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
627 ber_len_t rdnlen = 0;
629 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
632 rdnlen = dn_rdnlen( NULL, val );
633 if ( rdnlen != vdnlen - patlen - 1 )
636 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
637 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
640 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
641 if ( vdnlen <= patlen )
644 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
648 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
654 if ( a->acl_filter != NULL ) {
655 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
656 if ( rc != LDAP_COMPARE_TRUE ) {
661 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
666 if ( !state->as_fe_done ) {
667 state->as_fe_done = 1;
668 a = frontendDB->be_acl;
672 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
677 * Record value-dependent access control state
679 #define ACL_RECORD_VALUE_STATE do { \
680 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
681 state->as_recorded |= ACL_STATE_RECORDED_VD; \
682 state->as_vd_acl = a; \
683 state->as_vd_acl_count = count; \
691 AttributeDescription *desc,
697 struct berval *opndn )
700 * if access applies to the entry itself, and the
701 * user is bound as somebody in the same namespace as
702 * the entry, OR the given dn matches the dn pattern
705 * NOTE: styles "anonymous", "users" and "self"
706 * have been moved to enum slap_style_t, whose
707 * value is set in a_dn_style; however, the string
708 * is maintained in a_dn_pat.
711 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
712 if ( !BER_BVISEMPTY( opndn ) ) {
716 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
717 if ( BER_BVISEMPTY( opndn ) ) {
721 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
722 struct berval ndn, selfndn;
725 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
729 level = bdn->a_self_level;
737 selfndn = e->e_nname;
740 for ( ; level > 0; level-- ) {
741 if ( BER_BVISEMPTY( &ndn ) ) {
744 dnParent( &ndn, &ndn );
747 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
752 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
753 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
755 regmatch_t tmp_matches[2],
756 *tmp_matchesp = tmp_matches;
760 switch ( a->acl_dn_style ) {
761 case ACL_STYLE_REGEX:
762 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
763 tmp_matchesp = matches;
767 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
770 tmp_matches[0].rm_so = 0;
771 tmp_matches[0].rm_eo = e->e_nname.bv_len;
776 case ACL_STYLE_SUBTREE:
777 case ACL_STYLE_CHILDREN:
778 tmp_matches[0].rm_so = 0;
779 tmp_matches[0].rm_eo = e->e_nname.bv_len;
780 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
781 tmp_matches[1].rm_eo = e->e_nname.bv_len;
795 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
796 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
804 ber_len_t patlen, odnlen;
807 if ( e->e_dn == NULL )
810 if ( bdn->a_expand ) {
812 char buf[ACL_BUF_SIZE];
815 regmatch_t tmp_matches[2],
816 *tmp_matchesp = tmp_matches;
820 bv.bv_len = sizeof( buf ) - 1;
823 switch ( a->acl_dn_style ) {
824 case ACL_STYLE_REGEX:
825 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
826 tmp_matchesp = matches;
830 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
833 tmp_matches[0].rm_so = 0;
834 tmp_matches[0].rm_eo = e->e_nname.bv_len;
839 case ACL_STYLE_SUBTREE:
840 case ACL_STYLE_CHILDREN:
841 tmp_matches[0].rm_so = 0;
842 tmp_matches[0].rm_eo = e->e_nname.bv_len;
843 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
844 tmp_matches[1].rm_eo = e->e_nname.bv_len;
858 if ( acl_string_expand( &bv, &bdn->a_pat,
860 tmp_nmatch, tmp_matchesp ) )
865 if ( dnNormalize(0, NULL, NULL, &bv,
866 &pat, op->o_tmpmemctx )
869 /* did not expand to a valid dn */
878 odnlen = opndn->bv_len;
879 if ( odnlen < patlen ) {
880 goto dn_match_cleanup;
884 if ( bdn->a_style == ACL_STYLE_BASE ) {
885 /* base dn -- entire object DN must match */
886 if ( odnlen != patlen ) {
887 goto dn_match_cleanup;
890 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
891 ber_len_t rdnlen = 0;
893 if ( odnlen <= patlen ) {
894 goto dn_match_cleanup;
897 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
898 goto dn_match_cleanup;
901 rdnlen = dn_rdnlen( NULL, opndn );
902 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
903 goto dn_match_cleanup;
906 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
907 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
908 goto dn_match_cleanup;
911 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
912 if ( odnlen <= patlen ) {
913 goto dn_match_cleanup;
916 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
917 goto dn_match_cleanup;
920 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
921 int level = bdn->a_level;
924 if ( odnlen <= patlen ) {
925 goto dn_match_cleanup;
928 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
930 goto dn_match_cleanup;
934 for ( ; level > 0; level-- ) {
935 if ( BER_BVISEMPTY( &ndn ) ) {
936 goto dn_match_cleanup;
938 dnParent( &ndn, &ndn );
939 if ( ndn.bv_len < patlen ) {
940 goto dn_match_cleanup;
944 if ( ndn.bv_len != patlen ) {
945 goto dn_match_cleanup;
949 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
952 if ( pat.bv_val != bdn->a_pat.bv_val ) {
953 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
974 AccessControlState *state,
976 struct berval *opndn )
982 const char *attr = bdn->a_at->ad_cname.bv_val;
984 assert( attr != NULL );
986 if ( BER_BVISEMPTY( opndn ) ) {
990 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
993 /* see if asker is listed in dnattr */
994 for ( at = attrs_find( e->e_attrs, bdn->a_at );
996 at = attrs_find( at->a_next, bdn->a_at ) )
998 if ( attr_valfind( at,
999 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1000 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1001 &bv, NULL, op->o_tmpmemctx ) == 0 )
1010 /* have a dnattr match. if this is a self clause then
1011 * the target must also match the op dn.
1013 if ( bdn->a_self ) {
1014 /* check if the target is an attribute. */
1015 if ( val == NULL ) return 1;
1017 /* target is attribute, check if the attribute value
1020 rc = value_match( &match, bdn->a_at,
1021 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 )
1029 /* no dnattr match, check if this is a self clause */
1030 if ( ! bdn->a_self )
1033 ACL_RECORD_VALUE_STATE;
1035 /* this is a self clause, check if the target is an
1041 /* target is attribute, check if the attribute value
1044 rc = value_match( &match, bdn->a_at,
1045 bdn->a_at->ad_type->sat_equality, 0,
1048 /* on match error or no match, fail the ACL clause */
1049 if ( rc != LDAP_SUCCESS || match != 0 )
1058 * slap_acl_mask - modifies mask based upon the given acl and the
1059 * requested access to entry e, attribute attr, value val. if val
1060 * is null, access to the whole attribute is assumed (all values).
1062 * returns 0 access NOT allowed
1066 static slap_control_t
1072 AttributeDescription *desc,
1075 regmatch_t *matches,
1077 AccessControlState *state,
1078 slap_access_t access )
1083 char accessmaskbuf[ACCESSMASK_MAXLEN];
1086 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1088 assert( a != NULL );
1089 assert( mask != NULL );
1090 assert( desc != NULL );
1092 attr = desc->ad_cname.bv_val;
1094 assert( attr != NULL );
1096 Debug( LDAP_DEBUG_ACL,
1097 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1100 Debug( LDAP_DEBUG_ACL,
1101 "=> acl_mask: to %s by \"%s\", (%s) \n",
1102 val ? "value" : "all values",
1103 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1104 accessmask2str( *mask, accessmaskbuf, 1 ) );
1110 for ( ; b != NULL; b = b->a_next, i++ ) {
1111 slap_mask_t oldmask, modmask;
1113 ACL_INVALIDATE( modmask );
1115 /* AND <who> clauses */
1116 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1117 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1118 b->a_dn_pat.bv_val, 0, 0);
1120 * if access applies to the entry itself, and the
1121 * user is bound as somebody in the same namespace as
1122 * the entry, OR the given dn matches the dn pattern
1125 * NOTE: styles "anonymous", "users" and "self"
1126 * have been moved to enum slap_style_t, whose
1127 * value is set in a_dn_style; however, the string
1128 * is maintained in a_dn_pat.
1131 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1132 &b->a_dn, &op->o_ndn ) )
1138 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1141 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1142 b->a_realdn_pat.bv_val, 0, 0);
1144 * if access applies to the entry itself, and the
1145 * user is bound as somebody in the same namespace as
1146 * the entry, OR the given dn matches the dn pattern
1149 * NOTE: styles "anonymous", "users" and "self"
1150 * have been moved to enum slap_style_t, whose
1151 * value is set in a_dn_style; however, the string
1152 * is maintained in a_dn_pat.
1155 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1157 ndn = op->o_conn->c_ndn;
1162 if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1163 &b->a_realdn, &ndn ) )
1169 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1170 if ( ! op->o_conn->c_listener ) {
1173 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1174 b->a_sockurl_pat.bv_val, 0, 0 );
1176 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1177 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1178 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1179 e->e_ndn, nmatch, matches ) )
1184 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1186 char buf[ACL_BUF_SIZE];
1188 bv.bv_len = sizeof( buf ) - 1;
1190 if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1191 e->e_ndn, nmatch, matches ) )
1196 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1202 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1210 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1211 if ( !op->o_conn->c_peer_domain.bv_val ) {
1214 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1215 b->a_domain_pat.bv_val, 0, 0 );
1216 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1217 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1218 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1219 e->e_ndn, nmatch, matches ) )
1224 char buf[ACL_BUF_SIZE];
1226 struct berval cmp = op->o_conn->c_peer_domain;
1227 struct berval pat = b->a_domain_pat;
1229 if ( b->a_domain_expand ) {
1232 bv.bv_len = sizeof(buf) - 1;
1235 if ( acl_string_expand(&bv, &b->a_domain_pat,
1236 e->e_ndn, nmatch, matches) )
1243 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1244 int offset = cmp.bv_len - pat.bv_len;
1249 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1253 /* trim the domain */
1254 cmp.bv_val = &cmp.bv_val[ offset ];
1255 cmp.bv_len -= offset;
1258 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1265 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1266 if ( !op->o_conn->c_peer_name.bv_val ) {
1269 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1270 b->a_peername_pat.bv_val, 0, 0 );
1271 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1272 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1273 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1274 e->e_ndn, nmatch, matches ) )
1280 /* try exact match */
1281 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1282 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1286 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1288 char buf[ACL_BUF_SIZE];
1290 bv.bv_len = sizeof( buf ) - 1;
1292 if ( acl_string_expand( &bv, &b->a_peername_pat,
1293 e->e_ndn, nmatch, matches ) )
1298 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1302 /* extract IP and try exact match */
1303 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1305 char buf[STRLENOF("255.255.255.255") + 1];
1308 int port_number = -1;
1310 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1311 acl_bv_ip_eq.bv_val,
1312 acl_bv_ip_eq.bv_len ) != 0 )
1315 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1316 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1318 port = strrchr( ip.bv_val, ':' );
1320 ip.bv_len = port - ip.bv_val;
1322 if ( lutil_atoi( &port_number, port ) != 0 )
1326 /* the port check can be anticipated here */
1327 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1330 /* address longer than expected? */
1331 if ( ip.bv_len >= sizeof(buf) )
1334 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1335 buf[ ip.bv_len ] = '\0';
1337 addr = inet_addr( buf );
1339 /* unable to convert? */
1340 if ( addr == (unsigned long)(-1) )
1343 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1346 #ifdef LDAP_PF_INET6
1347 /* extract IPv6 and try exact match */
1348 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1350 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1352 struct in6_addr addr;
1353 int port_number = -1;
1355 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1356 acl_bv_ipv6_eq.bv_val,
1357 acl_bv_ipv6_eq.bv_len ) != 0 )
1360 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1361 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1363 port = strrchr( ip.bv_val, ']' );
1365 ip.bv_len = port - ip.bv_val;
1367 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1371 /* the port check can be anticipated here */
1372 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1375 /* address longer than expected? */
1376 if ( ip.bv_len >= sizeof(buf) )
1379 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1380 buf[ ip.bv_len ] = '\0';
1382 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1386 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1388 #endif /* LDAP_PF_INET6 */
1390 #ifdef LDAP_PF_LOCAL
1391 /* extract path and try exact match */
1392 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1395 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1396 acl_bv_path_eq.bv_val,
1397 acl_bv_path_eq.bv_len ) != 0 )
1400 path.bv_val = op->o_conn->c_peer_name.bv_val
1401 + acl_bv_path_eq.bv_len;
1402 path.bv_len = op->o_conn->c_peer_name.bv_len
1403 - acl_bv_path_eq.bv_len;
1405 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1408 #endif /* LDAP_PF_LOCAL */
1410 /* exact match (very unlikely...) */
1411 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1418 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1419 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1422 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1423 b->a_sockname_pat.bv_val, 0, 0 );
1424 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1425 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1426 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1427 e->e_ndn, nmatch, matches ) )
1432 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1434 char buf[ACL_BUF_SIZE];
1436 bv.bv_len = sizeof( buf ) - 1;
1438 if ( acl_string_expand( &bv, &b->a_sockname_pat,
1439 e->e_ndn, nmatch, matches ) )
1444 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1449 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1456 if ( b->a_dn_at != NULL ) {
1457 if ( acl_mask_dnattr( op, e, val, a, b, i,
1458 matches, count, state,
1459 &b->a_dn, &op->o_ndn ) )
1465 if ( b->a_realdn_at != NULL ) {
1468 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1470 ndn = op->o_conn->c_ndn;
1475 if ( acl_mask_dnattr( op, e, val, a, b, i,
1476 matches, count, state,
1477 &b->a_realdn, &ndn ) )
1483 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1485 struct berval ndn = BER_BVNULL;
1488 if ( op->o_ndn.bv_len == 0 ) {
1492 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1493 b->a_group_pat.bv_val, 0, 0 );
1495 /* b->a_group is an unexpanded entry name, expanded it should be an
1496 * entry with objectclass group* and we test to see if odn is one of
1497 * the values in the attribute group
1499 /* see if asker is listed in dnattr */
1500 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1501 char buf[ACL_BUF_SIZE];
1503 regmatch_t tmp_matches[2],
1504 *tmp_matchesp = tmp_matches;
1506 bv.bv_len = sizeof(buf) - 1;
1511 switch ( a->acl_dn_style ) {
1512 case ACL_STYLE_REGEX:
1513 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1514 tmp_matchesp = matches;
1515 tmp_nmatch = nmatch;
1519 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1520 case ACL_STYLE_BASE:
1521 tmp_matches[0].rm_so = 0;
1522 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1527 case ACL_STYLE_SUBTREE:
1528 case ACL_STYLE_CHILDREN:
1529 tmp_matches[0].rm_so = 0;
1530 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1531 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1532 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1546 if ( acl_string_expand( &bv, &b->a_group_pat,
1548 tmp_nmatch, tmp_matchesp ) )
1553 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1554 op->o_tmpmemctx ) != LDAP_SUCCESS )
1556 /* did not expand to a valid dn */
1563 bv = b->a_group_pat;
1566 rc = backend_group( op, e, &bv, &op->o_ndn,
1567 b->a_group_oc, b->a_group_at );
1570 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1578 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1580 char buf[ACL_BUF_SIZE];
1582 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1583 b->a_set_pat.bv_val, 0, 0 );
1585 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1587 regmatch_t tmp_matches[2],
1588 *tmp_matchesp = tmp_matches;
1591 bv.bv_len = sizeof( buf ) - 1;
1596 switch ( a->acl_dn_style ) {
1597 case ACL_STYLE_REGEX:
1598 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1599 tmp_matchesp = matches;
1600 tmp_nmatch = nmatch;
1604 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1605 case ACL_STYLE_BASE:
1606 tmp_matches[0].rm_so = 0;
1607 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1612 case ACL_STYLE_SUBTREE:
1613 case ACL_STYLE_CHILDREN:
1614 tmp_matches[0].rm_so = 0;
1615 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1616 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1617 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1631 if ( acl_string_expand( &bv, &b->a_set_pat,
1633 tmp_nmatch, tmp_matchesp ) )
1642 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1647 if ( b->a_authz.sai_ssf ) {
1648 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1649 b->a_authz.sai_ssf, op->o_ssf, 0 );
1650 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1655 if ( b->a_authz.sai_transport_ssf ) {
1656 Debug( LDAP_DEBUG_ACL,
1657 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1658 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1659 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1664 if ( b->a_authz.sai_tls_ssf ) {
1665 Debug( LDAP_DEBUG_ACL,
1666 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1667 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1668 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1673 if ( b->a_authz.sai_sasl_ssf ) {
1674 Debug( LDAP_DEBUG_ACL,
1675 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1676 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1677 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1682 /* check for the "self" modifier in the <access> field */
1683 if ( b->a_dn.a_self ) {
1687 ACL_RECORD_VALUE_STATE;
1689 /* must have DN syntax */
1690 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1691 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1693 /* check if the target is an attribute. */
1694 if ( val == NULL ) continue;
1696 /* a DN must be present */
1697 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1701 /* target is attribute, check if the attribute value
1704 rc = value_match( &match, desc,
1705 desc->ad_type->sat_equality, 0,
1706 val, &op->o_ndn, &dummy );
1707 /* on match error or no match, fail the ACL clause */
1708 if ( rc != LDAP_SUCCESS || match != 0 )
1713 if ( b->a_dynacl ) {
1715 slap_access_t tgrant, tdeny;
1717 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1720 /* this case works different from the others above.
1721 * since dynamic ACL's themselves give permissions, we need
1722 * to first check b->a_access_mask, the ACL's access level.
1724 /* first check if the right being requested
1725 * is allowed by the ACL clause.
1727 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1731 /* start out with nothing granted, nothing denied */
1732 ACL_INVALIDATE(tgrant);
1733 ACL_INVALIDATE(tdeny);
1735 for ( da = b->a_dynacl; da; da = da->da_next ) {
1736 slap_access_t grant,
1739 ACL_INVALIDATE(grant);
1740 ACL_INVALIDATE(deny);
1742 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1743 da->da_name, 0, 0 );
1745 (void)da->da_mask( da->da_private, op, e, desc,
1746 val, nmatch, matches, &grant, &deny );
1752 /* remove anything that the ACL clause does not allow */
1753 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1754 tdeny &= ACL_PRIV_MASK;
1756 /* see if we have anything to contribute */
1757 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1761 /* this could be improved by changing slap_acl_mask so that it can deal with
1762 * by clauses that return grant/deny pairs. Right now, it does either
1763 * additive or subtractive rights, but not both at the same time. So,
1764 * we need to combine the grant/deny pair into a single rights mask in
1765 * a smart way: if either grant or deny is "empty", then we use the
1766 * opposite as is, otherwise we remove any denied rights from the grant
1767 * rights mask and construct an additive mask.
1769 if (ACL_IS_INVALID(tdeny)) {
1770 modmask = tgrant | ACL_PRIV_ADDITIVE;
1772 } else if (ACL_IS_INVALID(tgrant)) {
1773 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1776 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1780 #endif /* SLAP_DYNACL */
1782 modmask = b->a_access_mask;
1785 Debug( LDAP_DEBUG_ACL,
1786 "<= acl_mask: [%d] applying %s (%s)\n",
1787 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1788 b->a_type == ACL_CONTINUE
1790 : b->a_type == ACL_BREAK
1796 if( ACL_IS_ADDITIVE(modmask) ) {
1798 ACL_PRIV_SET( *mask, modmask );
1801 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1803 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1804 /* substract privs */
1805 ACL_PRIV_CLR( *mask, modmask );
1808 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1815 Debug( LDAP_DEBUG_ACL,
1816 "<= acl_mask: [%d] mask: %s\n",
1817 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1819 if( b->a_type == ACL_CONTINUE ) {
1822 } else if ( b->a_type == ACL_BREAK ) {
1830 /* implicit "by * none" clause */
1833 Debug( LDAP_DEBUG_ACL,
1834 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1835 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1840 * acl_check_modlist - check access control on the given entry to see if
1841 * it allows the given modifications by the user associated with op.
1842 * returns 1 if mods allowed ok
1843 * 0 mods not allowed
1850 Modifications *mlist )
1853 AccessControlState state = ACL_STATE_INIT;
1856 int ret = 1; /* default is access allowed */
1860 be = LDAP_STAILQ_FIRST(&backendDB);
1864 assert( be != NULL );
1866 /* If ADD attribute checking is not enabled, just allow it */
1867 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1870 /* short circuit root database access */
1871 if ( be_isroot( op ) ) {
1872 Debug( LDAP_DEBUG_ACL,
1873 "<= acl_access_allowed: granted to database root\n",
1878 /* use backend default access if no backend acls */
1879 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1880 Debug( LDAP_DEBUG_ACL,
1881 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1882 access2str( ACL_WRITE ),
1883 op->o_bd->be_dfltaccess >= ACL_WRITE
1884 ? "granted" : "denied",
1886 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1890 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1892 * Internal mods are ignored by ACL_WRITE checking
1894 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1895 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1896 " modify access granted\n",
1897 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1902 * no-user-modification operational attributes are ignored
1903 * by ACL_WRITE checking as any found here are not provided
1906 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1907 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1909 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1910 " modify access granted\n",
1911 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1915 switch ( mlist->sml_op ) {
1916 case LDAP_MOD_REPLACE:
1917 case LDAP_MOD_INCREMENT:
1919 * We must check both permission to delete the whole
1920 * attribute and permission to add the specific attributes.
1921 * This prevents abuse from selfwriters.
1923 if ( ! access_allowed( op, e,
1924 mlist->sml_desc, NULL,
1925 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1932 if ( mlist->sml_values == NULL ) break;
1934 /* fall thru to check value to add */
1937 assert( mlist->sml_values != NULL );
1939 for ( bv = mlist->sml_nvalues
1940 ? mlist->sml_nvalues : mlist->sml_values;
1941 bv->bv_val != NULL; bv++ )
1943 if ( ! access_allowed( op, e,
1944 mlist->sml_desc, bv,
1945 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1954 case LDAP_MOD_DELETE:
1955 if ( mlist->sml_values == NULL ) {
1956 if ( ! access_allowed( op, e,
1957 mlist->sml_desc, NULL,
1958 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1966 for ( bv = mlist->sml_nvalues
1967 ? mlist->sml_nvalues : mlist->sml_values;
1968 bv->bv_val != NULL; bv++ )
1970 if ( ! access_allowed( op, e,
1971 mlist->sml_desc, bv,
1972 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1981 case SLAP_MOD_SOFTADD:
1982 /* allow adding attribute via modrdn thru */
1994 if (be_null) op->o_bd = NULL;
2000 struct berval *list,
2013 while ( len >= 0 && --ix >= 0 ) {
2014 while ( --len >= 0 && *p++ != sep )
2017 while ( len >= 0 && *p == ' ' ) {
2030 while ( --len >= 0 && *p != sep ) {
2034 while ( bv->bv_len > 0 && *--p == ' ' ) {
2041 typedef struct acl_set_gather_t {
2047 acl_set_cb_gather( Operation *op, SlapReply *rs )
2049 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2051 if ( rs->sr_type == REP_SEARCH ) {
2052 BerValue bvals[ 2 ];
2053 BerVarray bvalsp = NULL;
2056 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2057 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2059 if ( desc == NULL ) {
2063 if ( desc == slap_schema.si_ad_entryDN ) {
2065 bvals[ 0 ] = rs->sr_entry->e_nname;
2066 BER_BVZERO( &bvals[ 1 ] );
2071 a = attr_find( rs->sr_entry->e_attrs, desc );
2073 bvalsp = a->a_nvals;
2078 p->bvals = slap_set_join( p->cookie, p->bvals,
2079 ( '|' | SLAP_SET_RREF ), bvalsp );
2084 assert( rs->sr_type == REP_RESULT );
2091 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2093 AclSetCookie *cp = (AclSetCookie *)cookie;
2095 LDAPURLDesc *ludp = NULL;
2096 Operation op2 = { 0 };
2097 SlapReply rs = {REP_RESULT};
2098 AttributeName anlist[ 2 ], *anlistp = NULL;
2100 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2101 acl_set_gather_t p = { 0 };
2103 /* this routine needs to return the bervals instead of
2104 * plain strings, since syntax is not known. It should
2105 * also return the syntax or some "comparison cookie".
2107 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2108 return acl_set_gather2( cookie, name, desc );
2111 rc = ldap_url_parse( name->bv_val, &ludp );
2112 if ( rc != LDAP_URL_SUCCESS ) {
2113 Debug( LDAP_DEBUG_TRACE,
2114 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2115 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2117 rc = LDAP_PROTOCOL_ERROR;
2121 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2123 /* host part must be empty */
2124 /* extensions parts must be empty */
2125 Debug( LDAP_DEBUG_TRACE,
2126 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2127 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2129 rc = LDAP_PROTOCOL_ERROR;
2133 /* Grab the searchbase and see if an appropriate database can be found */
2134 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2135 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2136 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2137 BER_BVZERO( &op2.o_req_dn );
2138 if ( rc != LDAP_SUCCESS ) {
2139 Debug( LDAP_DEBUG_TRACE,
2140 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2141 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2146 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2147 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2148 Debug( LDAP_DEBUG_TRACE,
2149 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2150 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2152 rc = LDAP_NO_SUCH_OBJECT;
2156 /* Grab the filter */
2157 if ( ludp->lud_filter ) {
2158 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2159 cp->asc_op->o_tmpmemctx );
2160 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2161 if ( op2.ors_filter == NULL ) {
2162 Debug( LDAP_DEBUG_TRACE,
2163 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2164 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2166 rc = LDAP_PROTOCOL_ERROR;
2171 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2172 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2176 /* Grab the scope */
2177 op2.ors_scope = ludp->lud_scope;
2179 /* Grap the attributes */
2180 if ( ludp->lud_attrs ) {
2183 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2186 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2187 cp->asc_op->o_tmpmemctx );
2189 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2191 AttributeDescription *desc = NULL;
2192 const char *text = NULL;
2194 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2195 rc = slap_bv2ad( &name, &desc, &text );
2196 if ( rc == LDAP_SUCCESS ) {
2197 anlistp[ nattrs ].an_name = name;
2198 anlistp[ nattrs ].an_desc = desc;
2207 anlistp[ nattrs ].an_name = desc->ad_cname;
2208 anlistp[ nattrs ].an_desc = desc;
2210 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2214 op2.o_hdr = cp->asc_op->o_hdr;
2215 op2.o_tag = LDAP_REQ_SEARCH;
2216 op2.o_ndn = op2.o_bd->be_rootndn;
2217 op2.o_callback = &cb;
2218 slap_op_time( &op2.o_time, &op2.o_tincr );
2219 op2.o_do_not_cache = 1;
2220 op2.o_is_auth_check = 0;
2221 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2222 op2.ors_slimit = SLAP_NO_LIMIT;
2223 op2.ors_tlimit = SLAP_NO_LIMIT;
2224 op2.ors_attrs = anlistp;
2225 op2.ors_attrsonly = 0;
2226 op2.o_private = cp->asc_op->o_private;
2227 op2.o_extra = cp->asc_op->o_extra;
2231 rc = op2.o_bd->be_search( &op2, &rs );
2237 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2238 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2240 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2241 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2243 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2244 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2247 ldap_free_urldesc( ludp );
2249 if ( anlistp && anlistp != anlist ) {
2250 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2257 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2259 AclSetCookie *cp = (AclSetCookie *)cookie;
2260 BerVarray bvals = NULL;
2264 /* this routine needs to return the bervals instead of
2265 * plain strings, since syntax is not known. It should
2266 * also return the syntax or some "comparison cookie".
2268 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2269 if ( rc == LDAP_SUCCESS ) {
2270 if ( desc == slap_schema.si_ad_entryDN ) {
2271 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2272 cp->asc_op->o_tmpmemctx );
2274 BER_BVZERO( &bvals[ 1 ] );
2278 backend_attribute( cp->asc_op,
2279 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2282 if ( !BER_BVISNULL( &ndn ) ) {
2283 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2292 struct berval *subj,
2295 struct berval *default_set_attribute )
2297 struct berval set = BER_BVNULL;
2299 AclSetCookie cookie;
2301 if ( default_set_attribute == NULL ) {
2305 struct berval subjdn, ndn = BER_BVNULL;
2306 struct berval setat;
2307 BerVarray bvals = NULL;
2309 AttributeDescription *desc = NULL;
2311 /* format of string is "entry/setAttrName" */
2312 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2316 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2317 setat = *default_set_attribute;
2321 * NOTE: dnNormalize honors the ber_len field
2322 * as the length of the dn to be normalized
2324 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2325 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2327 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2328 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2332 BER_BVZERO( &bvals[0] );
2333 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2335 bvals[0].bv_val = bvals[i-1].bv_val;
2336 BER_BVZERO( &bvals[i-1] );
2338 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2339 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2344 if ( !BER_BVISNULL( &set ) ) {
2347 rc = ( slap_set_filter(
2349 (SetCookie *)&cookie, &set,
2350 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2351 if ( set.bv_val != subj->bv_val ) {
2352 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2362 * dynamic ACL infrastructure
2364 static slap_dynacl_t *da_list = NULL;
2367 slap_dynacl_register( slap_dynacl_t *da )
2371 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2372 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2377 if ( tmp != NULL ) {
2381 if ( da->da_mask == NULL ) {
2385 da->da_private = NULL;
2386 da->da_next = da_list;
2392 static slap_dynacl_t *
2393 slap_dynacl_next( slap_dynacl_t *da )
2402 slap_dynacl_get( const char *name )
2406 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2407 if ( strcasecmp( da->da_name, name ) == 0 ) {
2414 #endif /* SLAP_DYNACL */
2417 * statically built-in dynamic ACL initialization
2419 static int (*acl_init_func[])( void ) = {
2421 /* TODO: remove when ACI will only be dynamic */
2422 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2424 #endif /* SLAPD_ACI_ENABLED */
2425 #endif /* SLAP_DYNACL */
2435 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2436 rc = (*(acl_init_func[ i ]))();
2451 regmatch_t *matches)
2459 bv->bv_val[0] = '\0';
2460 bv->bv_len--; /* leave space for lone $ */
2463 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2464 sp < pat->bv_val + pat->bv_len ; sp++ )
2466 /* did we previously see a $ */
2468 if ( flag == 1 && *sp == '$' ) {
2473 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2476 } else if ( *sp >= '0' && *sp <= '9' ) {
2484 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2485 if ( *sp >= '0' && *sp <= '9' ) {
2486 n = 10*n + ( *sp - '0' );
2490 if ( *sp != /*'{'*/ '}' ) {
2496 if ( n >= nmatch ) {
2502 i = matches[n].rm_so;
2503 l = matches[n].rm_eo;
2504 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2522 /* must have ended with a single $ */
2530 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2531 Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2538 struct berval *pat, /* pattern to expand and match against */
2539 char *str, /* string to match against pattern */
2540 char *buf, /* buffer with $N expansion variables */
2541 int nmatch, /* size of the matches array */
2542 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2546 char newbuf[ACL_BUF_SIZE];
2550 bv.bv_len = sizeof( newbuf ) - 1;
2557 acl_string_expand( &bv, pat, buf, nmatch, matches );
2558 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2560 char error[ACL_BUF_SIZE];
2561 regerror( rc, &re, error, sizeof( error ) );
2563 Debug( LDAP_DEBUG_TRACE,
2564 "compile( \"%s\", \"%s\") failed %s\n",
2565 pat->bv_val, str, error );
2569 rc = regexec( &re, str, 0, NULL, 0 );
2572 Debug( LDAP_DEBUG_TRACE,
2573 "=> regex_matches: string: %s\n", str, 0, 0 );
2574 Debug( LDAP_DEBUG_TRACE,
2575 "=> regex_matches: rc: %d %s\n",
2576 rc, !rc ? "matches" : "no matches", 0 );