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-2004 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 #include "slapi/slapi.h"
41 #endif /* LDAPI_SLAPI */
43 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
49 aci_bv_entry = BER_BVC("entry"),
50 aci_bv_children = BER_BVC("children"),
51 aci_bv_br_entry = BER_BVC("[entry]"),
52 aci_bv_br_all = BER_BVC("[all]"),
53 aci_bv_access_id = BER_BVC("access-id"),
54 aci_bv_anonymous = BER_BVC("anonymous"),
55 aci_bv_public = BER_BVC("public"),
56 aci_bv_users = BER_BVC("users"),
57 aci_bv_self = BER_BVC("self"),
58 aci_bv_dnattr = BER_BVC("dnattr"),
59 aci_bv_group = BER_BVC("group"),
60 aci_bv_role = BER_BVC("role"),
61 aci_bv_set = BER_BVC("set"),
62 aci_bv_set_ref = BER_BVC("set-ref"),
63 aci_bv_grant = BER_BVC("grant"),
64 aci_bv_deny = BER_BVC("deny"),
66 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
67 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
68 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
69 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR);
72 static AccessControl * acl_get(
73 AccessControl *ac, int *count,
74 Operation *op, Entry *e,
75 AttributeDescription *desc,
77 int nmatches, regmatch_t *matches );
79 static slap_control_t acl_mask(
80 AccessControl *ac, slap_mask_t *mask,
81 Operation *op, Entry *e,
82 AttributeDescription *desc,
86 AccessControlState *state );
88 #ifdef SLAPD_ACI_ENABLED
90 Operation *op, Entry *e,
91 AttributeDescription *desc,
97 struct berval *scope);
100 static int regex_matches(
101 struct berval *pat, char *str, char *buf, regmatch_t *matches);
102 static void string_expand(
103 struct berval *newbuf, struct berval *pattern,
104 char *match, regmatch_t *matches);
106 typedef struct AciSetCookie {
111 SLAP_SET_GATHER aci_set_gather;
112 static int aci_match_set ( struct berval *subj, Operation *op,
113 Entry *e, int setref );
116 * access_allowed - check whether op->o_ndn is allowed the requested access
117 * to entry e, attribute attr, value val. if val is null, access to
118 * the whole attribute is assumed (all values).
120 * This routine loops through all access controls and calls
121 * acl_mask() on each applicable access control.
122 * The loop exits when a definitive answer is reached or
123 * or no more controls remain.
134 AttributeDescription *desc,
136 slap_access_t access,
137 AccessControlState *state )
141 AccessControl *a = NULL;
146 char accessmaskbuf[ACCESSMASK_MAXLEN];
149 slap_control_t control;
151 regmatch_t matches[MAXREMATCHES];
152 int st_same_attr = 0;
153 int st_initialized = 0;
154 static AccessControlState state_init = ACL_STATE_INIT;
157 assert( desc != NULL );
158 assert( access > ACL_NONE );
160 attr = desc->ad_cname.bv_val;
162 assert( attr != NULL );
164 if( op && op->o_is_auth_check &&
165 ( access == ACL_SEARCH || access == ACL_READ ))
170 if( state && state->as_recorded && state->as_vd_ad==desc) {
171 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
174 return state->as_result;
176 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
177 val != NULL && state->as_vd_acl == NULL )
179 return state->as_result;
185 state->as_vd_ad=desc;
189 LDAP_LOG( ACL, ENTRY,
190 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
191 access2str( access ), e->e_dn, attr );
193 Debug( LDAP_DEBUG_ACL,
194 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
195 access2str( access ), e->e_dn, attr );
209 assert( be != NULL );
212 if ( op->o_pb != NULL ) {
213 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
215 /* ACL plugin denied access */
219 #endif /* LDAP_SLAPI */
221 /* grant database root access */
222 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
225 "access_allowed: conn %lu root access granted\n",
226 op->o_connid, 0, 0 );
228 Debug( LDAP_DEBUG_ACL,
229 "<= root access granted\n",
236 * no-user-modification operational attributes are ignored
237 * by ACL_WRITE checking as any found here are not provided
240 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
241 && desc != slap_schema.si_ad_entry
242 && desc != slap_schema.si_ad_children )
245 LDAP_LOG( ACL, DETAIL1,
246 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
247 "access granted\n", op->o_connid, attr , 0 );
249 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
250 " %s access granted\n",
256 /* use backend default access if no backend acls */
257 if( be != NULL && be->be_acl == NULL ) {
259 LDAP_LOG( ACL, DETAIL1,
260 "access_allowed: backend default %s access %s to \"%s\"\n",
261 access2str( access ),
262 be->be_dfltaccess >= access ? "granted" : "denied",
263 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
265 Debug( LDAP_DEBUG_ACL,
266 "=> access_allowed: backend default %s access %s to \"%s\"\n",
267 access2str( access ),
268 be->be_dfltaccess >= access ? "granted" : "denied",
269 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
271 ret = be->be_dfltaccess >= access;
275 /* be is always non-NULL */
276 /* use global default access if no global acls */
277 } else if ( be == NULL && global_acl == NULL ) {
279 LDAP_LOG( ACL, DETAIL1,
280 "access_allowed: global default %s access %s to \"%s\"\n",
281 access2str( access ),
282 global_default_access >= access ? "granted" : "denied",
285 Debug( LDAP_DEBUG_ACL,
286 "=> access_allowed: global default %s access %s to \"%s\"\n",
287 access2str( access ),
288 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
290 ret = global_default_access >= access;
299 assert( state->as_vd_acl != NULL );
301 a = state->as_vd_acl;
302 mask = state->as_vd_acl_mask;
303 count = state->as_vd_acl_count;
304 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
308 if ( state ) state->as_vi_acl = NULL;
312 memset(matches, '\0', sizeof(matches));
315 while((a = acl_get( a, &count, op, e, desc, val,
316 MAXREMATCHES, matches )) != NULL)
320 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
322 LDAP_LOG( ACL, DETAIL1,
323 "access_allowed: match[%d]: %d %d ",
324 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
326 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
327 (int)matches[i].rm_so, (int)matches[i].rm_eo );
329 if( matches[i].rm_so <= matches[0].rm_eo ) {
331 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
332 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
336 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
338 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
343 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
344 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
345 ret = state->as_result;
347 } else if (!st_initialized) {
348 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
350 state->as_vd_ad=desc;
356 control = acl_mask( a, &mask, op,
357 e, desc, val, matches, count, state );
359 if ( control != ACL_BREAK ) {
363 memset(matches, '\0', sizeof(matches));
366 if ( ACL_IS_INVALID( mask ) ) {
368 LDAP_LOG( ACL, DETAIL1,
369 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
370 op->o_connid, e->e_dn, attr );
372 Debug( LDAP_DEBUG_ACL,
373 "=> access_allowed: \"%s\" (%s) invalid!\n",
378 } else if ( control == ACL_BREAK ) {
380 LDAP_LOG( ACL, DETAIL1,
381 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
383 Debug( LDAP_DEBUG_ACL,
384 "=> access_allowed: no more rules\n", 0, 0, 0);
391 LDAP_LOG( ACL, ENTRY,
392 "access_allowed: %s access %s by %s\n",
393 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
394 accessmask2str( mask, accessmaskbuf ) );
396 Debug( LDAP_DEBUG_ACL,
397 "=> access_allowed: %s access %s by %s\n",
398 access2str( access ),
399 ACL_GRANT(mask, access) ? "granted" : "denied",
400 accessmask2str( mask, accessmaskbuf ) );
403 ret = ACL_GRANT(mask, access);
406 if( state != NULL ) {
407 /* If not value-dependent, save ACL in case of more attrs */
408 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
409 state->as_vi_acl = a;
410 state->as_result = ret;
412 state->as_recorded |= ACL_STATE_RECORDED;
414 if (be_null) op->o_bd = NULL;
419 * acl_get - return the acl applicable to entry e, attribute
420 * attr. the acl returned is suitable for use in subsequent calls to
421 * acl_access_allowed().
424 static AccessControl *
430 AttributeDescription *desc,
433 regmatch_t *matches )
439 assert( count != NULL );
440 assert( desc != NULL );
442 attr = desc->ad_cname.bv_val;
444 assert( attr != NULL );
447 if( op->o_bd == NULL ) {
450 a = op->o_bd->be_acl;
459 dnlen = e->e_nname.bv_len;
461 for ( ; a != NULL; a = a->acl_next ) {
464 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
465 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
467 LDAP_LOG( ACL, DETAIL1,
468 "acl_get: dnpat [%d] %s nsub: %d\n",
469 *count, a->acl_dn_pat.bv_val,
470 (int) a->acl_dn_re.re_nsub );
472 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
473 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
475 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
480 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
481 *count, a->acl_dn_pat.bv_val, 0 );
483 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
484 *count, a->acl_dn_pat.bv_val, 0 );
486 patlen = a->acl_dn_pat.bv_len;
487 if ( dnlen < patlen )
490 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
491 /* base dn -- entire object DN must match */
492 if ( dnlen != patlen )
495 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
498 if ( dnlen <= patlen )
501 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
504 rdnlen = dn_rdnlen( NULL, &e->e_nname );
505 if ( rdnlen != dnlen - patlen - 1 )
508 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
509 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
512 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
513 if ( dnlen <= patlen )
515 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
519 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
524 LDAP_LOG( ACL, DETAIL1,
525 "acl_get: [%d] matched\n", *count, 0, 0 );
527 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
532 if ( a->acl_filter != NULL ) {
533 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
534 if ( rc != LDAP_COMPARE_TRUE ) {
540 LDAP_LOG( ACL, DETAIL1,
541 "acl_get: [%d] check attr %s\n", *count, attr ,0 );
543 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
546 if ( a->acl_attrs == NULL ||
547 ad_inlist( desc, a->acl_attrs ) )
550 LDAP_LOG( ACL, DETAIL1,
551 "acl_get: [%d] acl %s attr: %s\n", *count, e->e_dn, attr );
553 Debug( LDAP_DEBUG_ACL,
554 "<= acl_get: [%d] acl %s attr: %s\n",
555 *count, e->e_dn, attr );
559 matches[0].rm_so = matches[0].rm_eo = -1;
563 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
565 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
571 * Record value-dependent access control state
573 #define ACL_RECORD_VALUE_STATE do { \
574 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
575 state->as_recorded |= ACL_STATE_RECORDED_VD; \
576 state->as_vd_acl = a; \
577 AC_MEMCPY( state->as_vd_acl_matches, matches, \
578 sizeof( state->as_vd_acl_matches )) ; \
579 state->as_vd_acl_count = count; \
580 state->as_vd_access = b; \
581 state->as_vd_access_count = i; \
586 * acl_mask - modifies mask based upon the given acl and the
587 * requested access to entry e, attribute attr, value val. if val
588 * is null, access to the whole attribute is assumed (all values).
590 * returns 0 access NOT allowed
594 static slap_control_t
600 AttributeDescription *desc,
604 AccessControlState *state )
606 int i, odnlen, patlen;
609 char accessmaskbuf[ACCESSMASK_MAXLEN];
610 char accessmaskbuf1[ACCESSMASK_MAXLEN];
615 assert( mask != NULL );
616 assert( desc != NULL );
618 attr = desc->ad_cname.bv_val;
620 assert( attr != NULL );
623 LDAP_LOG( ACL, ENTRY,
624 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
625 op->o_connid, e->e_dn, attr );
628 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
629 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
630 accessmask2str( *mask, accessmaskbuf ) );
632 Debug( LDAP_DEBUG_ACL,
633 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
636 Debug( LDAP_DEBUG_ACL,
637 "=> acl_mask: to %s by \"%s\", (%s) \n",
638 val ? "value" : "all values",
639 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
640 accessmask2str( *mask, accessmaskbuf ) );
643 /* Is this ACL only for a specific value? */
644 if ( a->acl_attrval.bv_len ) {
645 if ( state && !state->as_vd_acl ) {
646 state->as_vd_acl = a;
647 state->as_vd_access = a->acl_access;
648 state->as_vd_access_count = 1;
653 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
655 LDAP_LOG( ACL, DETAIL1,
656 "acl_get: valpat %s\n",
657 a->acl_attrval.bv_val, 0, 0 );
659 Debug( LDAP_DEBUG_ACL,
660 "acl_get: valpat %s\n",
661 a->acl_attrval.bv_val, 0, 0 );
663 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
669 LDAP_LOG( ACL, DETAIL1,
671 a->acl_attrval.bv_val, 0, 0 );
673 Debug( LDAP_DEBUG_ACL,
675 a->acl_attrval.bv_val, 0, 0 );
678 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
679 if (value_match( &match, desc,
680 desc->ad_type->sat_equality, 0,
681 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
686 int patlen, vdnlen, rc, got_match = 0;
687 struct berval vdn = { 0, NULL };
690 assert( a->acl_attrs[0].an_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName );
692 rc = dnNormalize( 0, NULL, NULL, val, &vdn,
694 if ( rc != LDAP_SUCCESS ) {
699 patlen = a->acl_attrval.bv_len;
702 if ( vdnlen < patlen )
703 goto attrval_cleanup;
705 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
706 if ( vdnlen > patlen )
707 goto attrval_cleanup;
709 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
712 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
713 goto attrval_cleanup;
715 rdnlen = dn_rdnlen( NULL, &vdn );
716 if ( rdnlen != vdnlen - patlen - 1 )
717 goto attrval_cleanup;
719 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
720 if ( vdnlen > patlen && !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
721 goto attrval_cleanup;
723 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
724 if ( vdnlen <= patlen )
725 goto attrval_cleanup;
727 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
728 goto attrval_cleanup;
731 got_match = strcmp( a->acl_attrval.bv_val, vdn.bv_val + vdnlen - patlen );
744 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
745 && state->as_vd_acl == a )
747 b = state->as_vd_access;
748 i = state->as_vd_access_count;
755 for ( ; b != NULL; b = b->a_next, i++ ) {
756 slap_mask_t oldmask, modmask;
758 ACL_INVALIDATE( modmask );
760 /* AND <who> clauses */
761 if ( b->a_dn_pat.bv_len != 0 ) {
763 LDAP_LOG( ACL, DETAIL1,
764 "acl_mask: conn %lu check a_dn_pat: %s\n",
765 op->o_connid, b->a_dn_pat.bv_val ,0 );
767 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
768 b->a_dn_pat.bv_val, 0, 0);
771 * if access applies to the entry itself, and the
772 * user is bound as somebody in the same namespace as
773 * the entry, OR the given dn matches the dn pattern
775 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
776 if ( op->o_ndn.bv_len != 0 ) {
780 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
781 if ( op->o_ndn.bv_len == 0 ) {
785 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
786 if ( op->o_ndn.bv_len == 0 ) {
790 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
794 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
795 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
796 int ret = regex_matches( &b->a_dn_pat,
797 op->o_ndn.bv_val, e->e_ndn, matches );
808 if ( e->e_dn == NULL )
811 if ( b->a_dn_expand ) {
813 char buf[ACL_BUF_SIZE];
815 bv.bv_len = sizeof( buf ) - 1;
818 string_expand(&bv, &b->a_dn_pat,
820 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
821 /* did not expand to a valid dn */
829 odnlen = op->o_ndn.bv_len;
830 if ( odnlen < patlen ) {
831 goto dn_match_cleanup;
835 if ( b->a_dn_style == ACL_STYLE_BASE ) {
836 /* base dn -- entire object DN must match */
837 if ( odnlen != patlen ) {
838 goto dn_match_cleanup;
841 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
844 if ( odnlen <= patlen ) {
845 goto dn_match_cleanup;
848 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
849 goto dn_match_cleanup;
852 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
853 if ( rdnlen != odnlen - patlen - 1 ) {
854 goto dn_match_cleanup;
857 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
858 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
859 goto dn_match_cleanup;
862 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
863 if ( odnlen <= patlen ) {
864 goto dn_match_cleanup;
867 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
868 goto dn_match_cleanup;
872 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
875 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
885 if ( b->a_sockurl_pat.bv_len ) {
886 if ( ! op->o_conn->c_listener ) {
890 LDAP_LOG( ACL, DETAIL1,
891 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
892 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
894 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
895 b->a_sockurl_pat.bv_val, 0, 0 );
898 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
899 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
900 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
901 e->e_ndn, matches ) )
906 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
912 if ( b->a_domain_pat.bv_len ) {
913 if ( !op->o_conn->c_peer_domain.bv_val ) {
917 LDAP_LOG( ACL, DETAIL1,
918 "acl_mask: conn %lu check a_domain_pat: %s\n",
919 op->o_connid, b->a_domain_pat.bv_val , 0 );
921 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
922 b->a_domain_pat.bv_val, 0, 0 );
924 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
925 if ( b->a_domain_style == ACL_STYLE_REGEX) {
926 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
927 e->e_ndn, matches ) )
932 char buf[ACL_BUF_SIZE];
934 struct berval cmp = op->o_conn->c_peer_domain;
935 struct berval pat = b->a_domain_pat;
937 if ( b->a_domain_expand ) {
940 bv.bv_len = sizeof(buf) - 1;
943 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
947 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
948 int offset = cmp.bv_len - pat.bv_len;
953 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
957 /* trim the domain */
958 cmp.bv_val = &cmp.bv_val[ offset ];
959 cmp.bv_len -= offset;
962 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
969 if ( b->a_peername_pat.bv_len ) {
970 if ( !op->o_conn->c_peer_name.bv_val ) {
974 LDAP_LOG( ACL, DETAIL1,
975 "acl_mask: conn %lu check a_peername_path: %s\n",
976 op->o_connid, b->a_peername_pat.bv_val , 0 );
978 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
979 b->a_peername_pat.bv_val, 0, 0 );
981 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
982 if ( b->a_peername_style == ACL_STYLE_REGEX) {
983 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
984 e->e_ndn, matches ) )
989 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 )
995 if ( b->a_sockname_pat.bv_len ) {
996 if ( !op->o_conn->c_sock_name.bv_val ) {
1000 LDAP_LOG( ACL, DETAIL1,
1001 "acl_mask: conn %lu check a_sockname_path: %s\n",
1002 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1004 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1005 b->a_sockname_pat.bv_val, 0, 0 );
1007 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1008 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1009 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1010 e->e_ndn, matches ) )
1015 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1021 if ( b->a_dn_at != NULL ) {
1026 const char *attr = b->a_dn_at->ad_cname.bv_val;
1028 assert( attr != NULL );
1030 if ( op->o_ndn.bv_len == 0 ) {
1035 LDAP_LOG( ACL, DETAIL1,
1036 "acl_mask: conn %lu check a_dn_pat: %s\n",
1037 op->o_connid, attr , 0 );
1039 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1044 /* see if asker is listed in dnattr */
1045 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1047 at = attrs_find( at->a_next, b->a_dn_at ) )
1049 if( value_find_ex( b->a_dn_at,
1050 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1051 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1053 &bv, op->o_tmpmemctx ) == 0 )
1062 /* have a dnattr match. if this is a self clause then
1063 * the target must also match the op dn.
1065 if ( b->a_dn_self ) {
1066 /* check if the target is an attribute. */
1067 if ( val == NULL ) continue;
1069 /* target is attribute, check if the attribute value
1072 rc = value_match( &match, b->a_dn_at,
1073 b->a_dn_at->ad_type->sat_equality, 0,
1075 /* on match error or no match, fail the ACL clause */
1076 if (rc != LDAP_SUCCESS || match != 0 )
1080 /* no dnattr match, check if this is a self clause */
1081 if ( ! b->a_dn_self )
1084 ACL_RECORD_VALUE_STATE;
1086 /* this is a self clause, check if the target is an
1092 /* target is attribute, check if the attribute value
1095 rc = value_match( &match, b->a_dn_at,
1096 b->a_dn_at->ad_type->sat_equality, 0,
1099 /* on match error or no match, fail the ACL clause */
1100 if (rc != LDAP_SUCCESS || match != 0 )
1105 if ( b->a_group_pat.bv_len ) {
1107 struct berval ndn = { 0, NULL };
1110 if ( op->o_ndn.bv_len == 0 ) {
1114 /* b->a_group is an unexpanded entry name, expanded it should be an
1115 * entry with objectclass group* and we test to see if odn is one of
1116 * the values in the attribute group
1118 /* see if asker is listed in dnattr */
1119 if ( b->a_group_style == ACL_STYLE_REGEX ) {
1120 char buf[ACL_BUF_SIZE];
1121 bv.bv_len = sizeof(buf) - 1;
1124 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1125 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1126 /* did not expand to a valid dn */
1133 bv = b->a_group_pat;
1136 rc = backend_group( op, e, &bv, &op->o_ndn,
1137 b->a_group_oc, b->a_group_at );
1139 if ( ndn.bv_val ) free( ndn.bv_val );
1146 if ( b->a_set_pat.bv_len != 0 ) {
1148 char buf[ACL_BUF_SIZE];
1149 if( b->a_set_style == ACL_STYLE_REGEX ){
1150 bv.bv_len = sizeof(buf) - 1;
1152 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1156 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1161 if ( b->a_authz.sai_ssf ) {
1163 LDAP_LOG( ACL, DETAIL1,
1164 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1165 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1167 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1168 b->a_authz.sai_ssf, op->o_ssf, 0 );
1170 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1175 if ( b->a_authz.sai_transport_ssf ) {
1177 LDAP_LOG( ACL, DETAIL1,
1178 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1180 op->o_connid, b->a_authz.sai_transport_ssf,
1181 op->o_transport_ssf );
1183 Debug( LDAP_DEBUG_ACL,
1184 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1185 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1187 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1192 if ( b->a_authz.sai_tls_ssf ) {
1194 LDAP_LOG( ACL, DETAIL1,
1195 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1197 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1199 Debug( LDAP_DEBUG_ACL,
1200 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1201 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1203 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1208 if ( b->a_authz.sai_sasl_ssf ) {
1210 LDAP_LOG( ACL, DETAIL1,
1211 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1213 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1215 Debug( LDAP_DEBUG_ACL,
1216 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1217 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1219 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1224 #ifdef SLAPD_ACI_ENABLED
1225 if ( b->a_aci_at != NULL ) {
1227 slap_access_t grant, deny, tgrant, tdeny;
1228 struct berval parent_ndn, old_parent_ndn;
1229 BerVarray bvals = NULL;
1232 /* this case works different from the others above.
1233 * since aci's themselves give permissions, we need
1234 * to first check b->a_access_mask, the ACL's access level.
1237 if ( e->e_nname.bv_len == 0 ) {
1238 /* no ACIs in the root DSE */
1242 /* first check if the right being requested
1243 * is allowed by the ACL clause.
1245 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1248 /* start out with nothing granted, nothing denied */
1252 /* get the aci attribute */
1253 at = attr_find( e->e_attrs, b->a_aci_at );
1255 ACL_RECORD_VALUE_STATE;
1256 /* the aci is an multi-valued attribute. The
1257 * rights are determined by OR'ing the individual
1258 * rights given by the acis.
1260 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1264 matches, &grant, &deny, &aci_bv_entry ) != 0)
1270 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1271 accessmask2str(tgrant,accessmaskbuf),
1272 accessmask2str(tdeny, accessmaskbuf1), 0);
1275 /* If the entry level aci didn't contain anything valid for the
1276 * current operation, climb up the tree and evaluate the
1277 * acis with scope set to subtree
1279 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1280 dnParent(&(e->e_nname), &parent_ndn);
1281 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1282 old_parent_ndn = parent_ndn;
1283 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1284 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1288 for( i = 0; bvals[i].bv_val != NULL; i++){
1289 ACL_RECORD_VALUE_STATE;
1290 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1291 &grant, &deny, &aci_bv_children) != 0) {
1294 /* evaluation stops as soon as either a "deny" or a
1295 * "grant" directive matches.
1297 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1301 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1302 accessmask2str(tgrant,accessmaskbuf),
1303 accessmask2str(tdeny, accessmaskbuf1), 0);
1308 case LDAP_NO_SUCH_ATTRIBUTE:
1309 /* just go on if the aci-Attribute is not present in
1312 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1315 case LDAP_NO_SUCH_OBJECT:
1316 /* We have reached the base object */
1317 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1327 dnParent(&old_parent_ndn, &parent_ndn);
1332 /* remove anything that the ACL clause does not allow */
1333 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1334 tdeny &= ACL_PRIV_MASK;
1336 /* see if we have anything to contribute */
1337 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1341 /* this could be improved by changing acl_mask so that it can deal with
1342 * by clauses that return grant/deny pairs. Right now, it does either
1343 * additive or subtractive rights, but not both at the same time. So,
1344 * we need to combine the grant/deny pair into a single rights mask in
1345 * a smart way: if either grant or deny is "empty", then we use the
1346 * opposite as is, otherwise we remove any denied rights from the grant
1347 * rights mask and construct an additive mask.
1349 if (ACL_IS_INVALID(tdeny)) {
1350 modmask = tgrant | ACL_PRIV_ADDITIVE;
1352 } else if (ACL_IS_INVALID(tgrant)) {
1353 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1356 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1362 modmask = b->a_access_mask;
1366 LDAP_LOG( ACL, RESULTS,
1367 "acl_mask: [%d] applying %s (%s)\n",
1368 i, accessmask2str( modmask, accessmaskbuf),
1369 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1370 ? "break" : "stop" );
1372 Debug( LDAP_DEBUG_ACL,
1373 "<= acl_mask: [%d] applying %s (%s)\n",
1374 i, accessmask2str( modmask, accessmaskbuf ),
1375 b->a_type == ACL_CONTINUE
1377 : b->a_type == ACL_BREAK
1384 if( ACL_IS_ADDITIVE(modmask) ) {
1386 ACL_PRIV_SET( *mask, modmask );
1389 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1391 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1392 /* substract privs */
1393 ACL_PRIV_CLR( *mask, modmask );
1396 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1404 LDAP_LOG( ACL, DETAIL1,
1405 "acl_mask: conn %lu [%d] mask: %s\n",
1406 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1408 Debug( LDAP_DEBUG_ACL,
1409 "<= acl_mask: [%d] mask: %s\n",
1410 i, accessmask2str(*mask, accessmaskbuf), 0 );
1413 if( b->a_type == ACL_CONTINUE ) {
1416 } else if ( b->a_type == ACL_BREAK ) {
1424 /* implicit "by * none" clause */
1428 LDAP_LOG( ACL, RESULTS,
1429 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1430 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1432 Debug( LDAP_DEBUG_ACL,
1433 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1434 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1440 * acl_check_modlist - check access control on the given entry to see if
1441 * it allows the given modifications by the user associated with op.
1442 * returns 1 if mods allowed ok
1443 * 0 mods not allowed
1450 Modifications *mlist
1454 AccessControlState state = ACL_STATE_INIT;
1457 int ret = 1; /* default is access allowed */
1465 assert( be != NULL );
1467 /* short circuit root database access */
1468 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1470 LDAP_LOG( ACL, DETAIL1,
1471 "acl_check_modlist: conn %lu access granted to root user\n",
1472 op->o_connid, 0, 0 );
1474 Debug( LDAP_DEBUG_ACL,
1475 "<= acl_access_allowed: granted to database root\n",
1481 /* use backend default access if no backend acls */
1482 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1484 LDAP_LOG( ACL, DETAIL1,
1485 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1486 access2str( ACL_WRITE ),
1487 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1490 Debug( LDAP_DEBUG_ACL,
1491 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1492 access2str( ACL_WRITE ),
1493 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1495 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1499 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1501 * no-user-modification operational attributes are ignored
1502 * by ACL_WRITE checking as any found here are not provided
1505 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1507 LDAP_LOG( ACL, DETAIL1,
1508 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1509 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1511 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1512 " modify access granted\n",
1513 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1518 switch ( mlist->sml_op ) {
1519 case LDAP_MOD_REPLACE:
1521 * We must check both permission to delete the whole
1522 * attribute and permission to add the specific attributes.
1523 * This prevents abuse from selfwriters.
1525 if ( ! access_allowed( op, e,
1526 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1532 if ( mlist->sml_bvalues == NULL ) break;
1534 /* fall thru to check value to add */
1537 assert( mlist->sml_bvalues != NULL );
1539 for ( bv = mlist->sml_nvalues
1540 ? mlist->sml_nvalues : mlist->sml_values;
1541 bv->bv_val != NULL; bv++ )
1543 if ( ! access_allowed( op, e,
1544 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1552 case LDAP_MOD_DELETE:
1553 if ( mlist->sml_bvalues == NULL ) {
1554 if ( ! access_allowed( op, e,
1555 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1562 for ( bv = mlist->sml_nvalues
1563 ? mlist->sml_nvalues : mlist->sml_values;
1564 bv->bv_val != NULL; bv++ )
1566 if ( ! access_allowed( op, e,
1567 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1575 case SLAP_MOD_SOFTADD:
1576 /* allow adding attribute via modrdn thru */
1588 if (be_null) op->o_bd = NULL;
1594 struct berval *list,
1608 while (len >= 0 && --ix >= 0) {
1609 while (--len >= 0 && *p++ != sep) ;
1611 while (len >= 0 && *p == ' ') {
1622 while (--len >= 0 && *p != sep) {
1626 while (bv->bv_len > 0 && *--p == ' ')
1632 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1634 AciSetCookie *cp = (AciSetCookie *)cookie;
1635 BerVarray bvals = NULL;
1638 /* this routine needs to return the bervals instead of
1639 * plain strings, since syntax is not known. It should
1640 * also return the syntax or some "comparison cookie".
1643 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1645 AttributeDescription *desc = NULL;
1646 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1647 backend_attribute(cp->op,
1648 cp->e, &ndn, desc, &bvals);
1650 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1657 struct berval *subj,
1663 struct berval set = { 0, NULL };
1665 AciSetCookie cookie;
1668 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1670 struct berval subjdn, ndn = { 0, NULL };
1671 struct berval setat;
1674 AttributeDescription *desc = NULL;
1676 /* format of string is "entry/setAttrName" */
1677 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1681 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1682 setat.bv_val = SLAPD_ACI_SET_ATTR;
1683 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1686 if ( setat.bv_val != NULL ) {
1688 * NOTE: dnNormalize honors the ber_len field
1689 * as the length of the dn to be normalized
1691 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1692 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1694 backend_attribute(op, e,
1695 &ndn, desc, &bvals);
1696 if ( bvals != NULL ) {
1697 if ( bvals[0].bv_val != NULL ) {
1700 bvals[0].bv_val = NULL;
1701 for (i=1;bvals[i].bv_val;i++);
1702 bvals[0].bv_val = bvals[i-1].bv_val;
1703 bvals[i-1].bv_val = NULL;
1705 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1713 if (set.bv_val != NULL) {
1716 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1717 &op->o_ndn, &e->e_nname, NULL) > 0);
1718 sl_free(set.bv_val, op->o_tmpmemctx);
1723 #ifdef SLAPD_ACI_ENABLED
1725 aci_list_map_rights(
1726 struct berval *list )
1733 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1736 switch (*bv.bv_val) {
1738 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1741 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1742 * the right 's' to mean "set", but in the examples states
1743 * that the right 's' means "search". The latter definition
1746 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1749 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1752 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1755 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1756 * define any equivalent to the AUTH right, so I've just used
1759 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1771 struct berval *list,
1772 const struct berval *attr,
1773 struct berval *val )
1775 struct berval bv, left, right;
1778 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1779 if (aci_get_part(&bv, 0, '=', &left) < 0
1780 || aci_get_part(&bv, 1, '=', &right) < 0)
1782 if (ber_bvstrcasecmp(attr, &bv) == 0)
1784 } else if (val == NULL) {
1785 if (ber_bvstrcasecmp(attr, &left) == 0)
1788 if (ber_bvstrcasecmp(attr, &left) == 0) {
1789 /* this is experimental code that implements a
1790 * simple (prefix) match of the attribute value.
1791 * the ACI draft does not provide for aci's that
1792 * apply to specific values, but it would be
1793 * nice to have. If the <attr> part of an aci's
1794 * rights list is of the form <attr>=<value>,
1795 * that means the aci applies only to attrs with
1796 * the given value. Furthermore, if the attr is
1797 * of the form <attr>=<value>*, then <value> is
1798 * treated as a prefix, and the aci applies to
1799 * any value with that prefix.
1801 * Ideally, this would allow r.e. matches.
1803 if (aci_get_part(&right, 0, '*', &left) < 0
1804 || right.bv_len <= left.bv_len)
1806 if (ber_bvstrcasecmp(val, &right) == 0)
1808 } else if (val->bv_len >= left.bv_len) {
1809 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1818 static slap_access_t
1819 aci_list_get_attr_rights(
1820 struct berval *list,
1821 const struct berval *attr,
1822 struct berval *val )
1828 /* loop through each rights/attr pair, skip first part (action) */
1830 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1831 if (aci_list_has_attr(&bv, attr, val) == 0)
1833 if (aci_get_part(list, i, ';', &bv) < 0)
1835 mask |= aci_list_map_rights(&bv);
1841 aci_list_get_rights(
1842 struct berval *list,
1843 const struct berval *attr,
1845 slap_access_t *grant,
1846 slap_access_t *deny )
1848 struct berval perm, actn;
1849 slap_access_t *mask;
1852 if (attr == NULL || attr->bv_len == 0
1853 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1854 attr = &aci_bv_br_entry;
1860 /* loop through each permissions clause */
1861 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1862 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1864 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1866 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1873 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1874 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1881 struct berval *subj,
1882 struct berval *defgrpoc,
1883 struct berval *defgrpat,
1889 struct berval subjdn;
1890 struct berval grpoc;
1891 struct berval grpat;
1892 ObjectClass *grp_oc = NULL;
1893 AttributeDescription *grp_ad = NULL;
1897 /* format of string is "group/objectClassValue/groupAttrName" */
1898 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1902 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1906 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1910 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1911 if( rc != LDAP_SUCCESS ) {
1917 grp_oc = oc_bvfind( &grpoc );
1919 if (grp_oc != NULL && grp_ad != NULL ) {
1920 char buf[ACL_BUF_SIZE];
1921 struct berval bv, ndn;
1922 bv.bv_len = sizeof( buf ) - 1;
1923 bv.bv_val = (char *)&buf;
1924 string_expand(&bv, &subjdn, e->e_ndn, matches);
1925 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1926 rc = (backend_group(op, e, &ndn, &op->o_ndn,
1927 grp_oc, grp_ad) == 0);
1940 AttributeDescription *desc,
1943 regmatch_t *matches,
1944 slap_access_t *grant,
1945 slap_access_t *deny,
1946 struct berval *scope
1949 struct berval bv, perms, sdn;
1953 assert( desc->ad_cname.bv_val != NULL );
1955 /* parse an aci of the form:
1956 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1958 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1959 a full description of the format for this attribute.
1960 Differences: "this" in the draft is "self" here, and
1961 "self" and "public" is in the position of dnType.
1963 For now, this routine only supports scope=entry.
1965 /* check that the aci has all 5 components */
1966 if (aci_get_part(aci, 4, '#', NULL) < 0)
1969 /* check that the aci family is supported */
1970 if (aci_get_part(aci, 0, '#', &bv) < 0)
1973 /* check that the scope matches */
1974 if (aci_get_part(aci, 1, '#', &bv) < 0
1975 || ber_bvstrcasecmp( scope, &bv ) != 0)
1980 /* get the list of permissions clauses, bail if empty */
1981 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1984 /* check if any permissions allow desired access */
1985 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1988 /* see if we have a DN match */
1989 if (aci_get_part(aci, 3, '#', &bv) < 0)
1992 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1995 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1998 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1999 if (dn_match( &op->o_ndn, &ndn))
2005 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2008 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2009 if (dn_match(&op->o_ndn, &e->e_nname))
2012 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2014 AttributeDescription *ad = NULL;
2017 rc = slap_bv2ad( &sdn, &ad, &text );
2019 if( rc != LDAP_SUCCESS ) {
2027 for(at = attrs_find( e->e_attrs, ad );
2029 at = attrs_find( at->a_next, ad ) )
2031 if (value_find_ex( ad,
2032 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2033 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2035 &bv, op->o_tmpmemctx) == 0 )
2045 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2046 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2049 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2050 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2053 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2054 if (aci_match_set(&sdn, op, e, 0))
2057 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2058 if (aci_match_set(&sdn, op, e, 1))
2066 #endif /* SLAPD_ACI_ENABLED */
2073 regmatch_t *matches)
2081 bv->bv_val[0] = '\0';
2082 bv->bv_len--; /* leave space for lone $ */
2085 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2086 sp < pat->bv_val + pat->bv_len ; sp++ )
2088 /* did we previously see a $ */
2090 if ( flag == 1 && *sp == '$' ) {
2095 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2098 } else if ( *sp >= '0' && *sp <= '9' ) {
2106 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2107 if ( *sp >= '0' && *sp <= '9' ) {
2108 n = 10*n + ( *sp - '0' );
2112 if ( *sp != /*'{'*/ '}' ) {
2117 if ( n >= MAXREMATCHES ) {
2122 i = matches[n].rm_so;
2123 l = matches[n].rm_eo;
2124 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2142 /* must have ended with a single $ */
2151 LDAP_LOG( ACL, DETAIL1,
2152 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2153 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2155 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2156 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2162 struct berval *pat, /* pattern to expand and match against */
2163 char *str, /* string to match against pattern */
2164 char *buf, /* buffer with $N expansion variables */
2165 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2169 char newbuf[ACL_BUF_SIZE];
2173 bv.bv_len = sizeof(newbuf) - 1;
2176 if(str == NULL) str = "";
2178 string_expand(&bv, pat, buf, matches);
2179 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2180 char error[ACL_BUF_SIZE];
2181 regerror(rc, &re, error, sizeof(error));
2185 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2186 pat->bv_val, str, error );
2188 Debug( LDAP_DEBUG_TRACE,
2189 "compile( \"%s\", \"%s\") failed %s\n",
2190 pat->bv_val, str, error );
2195 rc = regexec(&re, str, 0, NULL, 0);
2199 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2200 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2201 rc, rc ? "matches" : "no matches", 0 );
2203 Debug( LDAP_DEBUG_TRACE,
2204 "=> regex_matches: string: %s\n", str, 0, 0 );
2205 Debug( LDAP_DEBUG_TRACE,
2206 "=> regex_matches: rc: %d %s\n",
2207 rc, !rc ? "matches" : "no matches", 0 );