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_onelevel = BER_BVC("onelevel"),
52 aci_bv_subtree = BER_BVC("subtree"),
53 aci_bv_br_entry = BER_BVC("[entry]"),
54 aci_bv_br_all = BER_BVC("[all]"),
55 aci_bv_access_id = BER_BVC("access-id"),
56 aci_bv_anonymous = BER_BVC("anonymous"),
57 aci_bv_public = BER_BVC("public"),
58 aci_bv_users = BER_BVC("users"),
59 aci_bv_self = BER_BVC("self"),
60 aci_bv_dnattr = BER_BVC("dnattr"),
61 aci_bv_group = BER_BVC("group"),
62 aci_bv_role = BER_BVC("role"),
63 aci_bv_set = BER_BVC("set"),
64 aci_bv_set_ref = BER_BVC("set-ref"),
65 aci_bv_grant = BER_BVC("grant"),
66 aci_bv_deny = BER_BVC("deny"),
68 aci_bv_ip_eq = BER_BVC("IP="),
70 aci_bv_path_eq = BER_BVC("PATH="),
71 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
72 #endif /* LDAP_PF_LOCAL */
74 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
75 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
76 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
77 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
78 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
80 typedef enum slap_aci_scope_t {
81 SLAP_ACI_SCOPE_ENTRY = 0x1,
82 SLAP_ACI_SCOPE_CHILDREN = 0x2,
83 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
86 static AccessControl * acl_get(
87 AccessControl *ac, int *count,
88 Operation *op, Entry *e,
89 AttributeDescription *desc,
91 int nmatch, regmatch_t *matches,
92 AccessControlState *state );
94 static slap_control_t acl_mask(
95 AccessControl *ac, slap_mask_t *mask,
96 Operation *op, Entry *e,
97 AttributeDescription *desc,
102 AccessControlState *state );
104 #ifdef SLAPD_ACI_ENABLED
106 Operation *op, Entry *e,
107 AttributeDescription *desc,
112 slap_access_t *grant,
114 slap_aci_scope_t scope);
117 static int regex_matches(
118 struct berval *pat, char *str, char *buf,
119 int nmatch, regmatch_t *matches);
120 static int string_expand(
121 struct berval *newbuf, struct berval *pattern,
122 char *match, int nmatch, regmatch_t *matches);
124 typedef struct AciSetCookie {
129 SLAP_SET_GATHER aci_set_gather;
130 SLAP_SET_GATHER aci_set_gather2;
131 static int aci_match_set ( struct berval *subj, Operation *op,
132 Entry *e, int setref );
135 * access_allowed - check whether op->o_ndn is allowed the requested access
136 * to entry e, attribute attr, value val. if val is null, access to
137 * the whole attribute is assumed (all values).
139 * This routine loops through all access controls and calls
140 * acl_mask() on each applicable access control.
141 * The loop exits when a definitive answer is reached or
142 * or no more controls remain.
149 * - can be legally called with op == NULL
150 * - can be legally called with op->o_bd == NULL
157 AttributeDescription *desc,
159 slap_access_t access,
160 AccessControlState *state,
165 AccessControl *a = NULL;
170 char accessmaskbuf[ACCESSMASK_MAXLEN];
173 slap_control_t control;
175 regmatch_t matches[MAXREMATCHES];
176 int st_same_attr = 0;
177 static AccessControlState state_init = ACL_STATE_INIT;
180 assert( desc != NULL );
181 assert( access > ACL_NONE );
182 if ( maskp ) ACL_INVALIDATE( *maskp );
184 attr = desc->ad_cname.bv_val;
186 assert( attr != NULL );
188 if( op && op->o_is_auth_check &&
189 ( access == ACL_SEARCH || access == ACL_READ ))
195 if ( state->as_vd_ad==desc) {
196 if ( state->as_recorded ) {
197 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
200 return state->as_result;
201 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
202 val != NULL && state->as_vd_acl == NULL )
204 return state->as_result;
212 state->as_vd_ad=desc;
215 Debug( LDAP_DEBUG_ACL,
216 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
217 access2str( access ), e->e_dn, attr );
230 * FIXME: experimental; use first backend rules
231 * iff there is no global_acl (ITS#3100) */
232 if ( frontendDB->be_acl == NULL )
238 assert( be != NULL );
241 if ( op->o_pb != NULL ) {
242 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
244 /* ACL plugin denied access */
248 #endif /* LDAP_SLAPI */
250 /* grant database root access */
251 if ( be != NULL && be_isroot( op ) ) {
252 Debug( LDAP_DEBUG_ACL,
253 "<= root access granted\n",
256 mask = ACL_LVL_WRITE;
263 * no-user-modification operational attributes are ignored
264 * by ACL_WRITE checking as any found here are not provided
267 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
268 && desc != slap_schema.si_ad_entry
269 && desc != slap_schema.si_ad_children )
271 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
272 " %s access granted\n",
277 /* use backend default access if no backend acls */
278 if( be != NULL && be->be_acl == NULL ) {
279 Debug( LDAP_DEBUG_ACL,
280 "=> access_allowed: backend default %s access %s to \"%s\"\n",
281 access2str( access ),
282 be->be_dfltaccess >= access ? "granted" : "denied",
283 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
284 ret = be->be_dfltaccess >= access;
289 mask = ACL_PRIV_LEVEL;
290 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
291 mask |= ACL_ACCESS2PRIV( i );
298 /* be is always non-NULL */
299 /* use global default access if no global acls */
300 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
301 Debug( LDAP_DEBUG_ACL,
302 "=> access_allowed: global default %s access %s to \"%s\"\n",
303 access2str( access ),
304 frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
305 ret = frontendDB->be_dfltaccess >= access;
310 mask = ACL_PRIV_LEVEL;
311 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
312 mask |= ACL_ACCESS2PRIV( i );
324 assert( state->as_vd_acl != NULL );
326 a = state->as_vd_acl;
327 count = state->as_vd_acl_count;
328 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
329 mask = state->as_vd_acl_mask;
330 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
335 if ( state ) state->as_vi_acl = NULL;
339 memset(matches, '\0', sizeof(matches));
342 while((a = acl_get( a, &count, op, e, desc, val,
343 MAXREMATCHES, matches, state )) != NULL)
347 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
348 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
349 (int)matches[i].rm_so, (int)matches[i].rm_eo );
350 if( matches[i].rm_so <= matches[0].rm_eo ) {
352 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
353 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
356 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
360 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
361 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
362 ret = state->as_result;
365 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
370 control = acl_mask( a, &mask, op,
371 e, desc, val, MAXREMATCHES, matches, count, state );
373 if ( control != ACL_BREAK ) {
377 memset(matches, '\0', sizeof(matches));
380 if ( ACL_IS_INVALID( mask ) ) {
381 Debug( LDAP_DEBUG_ACL,
382 "=> access_allowed: \"%s\" (%s) invalid!\n",
386 } else if ( control == ACL_BREAK ) {
387 Debug( LDAP_DEBUG_ACL,
388 "=> access_allowed: no more rules\n", 0, 0, 0);
393 Debug( LDAP_DEBUG_ACL,
394 "=> access_allowed: %s access %s by %s\n",
395 access2str( access ),
396 ACL_GRANT(mask, access) ? "granted" : "denied",
397 accessmask2str( mask, accessmaskbuf ) );
399 ret = ACL_GRANT(mask, access);
402 if( state != NULL ) {
403 /* If not value-dependent, save ACL in case of more attrs */
404 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
405 state->as_vi_acl = a;
406 state->as_result = ret;
408 state->as_recorded |= ACL_STATE_RECORDED;
410 if (be_null) op->o_bd = NULL;
411 if ( maskp ) *maskp = mask;
417 * acl_get - return the acl applicable to entry e, attribute
418 * attr. the acl returned is suitable for use in subsequent calls to
419 * acl_access_allowed().
422 static AccessControl *
428 AttributeDescription *desc,
432 AccessControlState *state )
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 ) {
448 a = frontendDB->be_acl;
450 a = op->o_bd->be_acl;
461 dnlen = e->e_nname.bv_len;
463 for ( ; a != NULL; a = a->acl_next ) {
466 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
467 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
468 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
469 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
470 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
474 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
475 *count, a->acl_dn_pat.bv_val, 0 );
476 patlen = a->acl_dn_pat.bv_len;
477 if ( dnlen < patlen )
480 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
481 /* base dn -- entire object DN must match */
482 if ( dnlen != patlen )
485 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
486 int rdnlen = -1, sep = 0;
488 if ( dnlen <= patlen )
492 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
497 rdnlen = dn_rdnlen( NULL, &e->e_nname );
498 if ( rdnlen != dnlen - patlen - sep )
501 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
502 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
505 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
506 if ( dnlen <= patlen )
508 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
512 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
516 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
520 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
521 matches[0].rm_so = matches[0].rm_eo = -1;
525 /* Is this ACL only for a specific value? */
526 if ( a->acl_attrval.bv_len ) {
531 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
532 state->as_recorded |= ACL_STATE_RECORDED_VD;
533 state->as_vd_acl = prev;
534 state->as_vd_acl_count = *count;
535 state->as_vd_access = a->acl_access;
536 state->as_vd_access_count = 1;
537 ACL_INVALIDATE( state->as_vd_acl_mask );
540 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
541 Debug( LDAP_DEBUG_ACL,
542 "acl_get: valpat %s\n",
543 a->acl_attrval.bv_val, 0, 0 );
544 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
549 Debug( LDAP_DEBUG_ACL,
551 a->acl_attrval.bv_val, 0, 0 );
553 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
554 if (value_match( &match, desc,
555 desc->ad_type->sat_equality, 0,
556 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
563 patlen = a->acl_attrval.bv_len;
564 vdnlen = val->bv_len;
566 if ( vdnlen < patlen )
569 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
570 if ( vdnlen > patlen )
573 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
576 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
579 rdnlen = dn_rdnlen( NULL, val );
580 if ( rdnlen != vdnlen - patlen - 1 )
583 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
584 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
587 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
588 if ( vdnlen <= patlen )
591 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
595 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
601 if ( a->acl_filter != NULL ) {
602 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
603 if ( rc != LDAP_COMPARE_TRUE ) {
608 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
613 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
618 * Record value-dependent access control state
620 #define ACL_RECORD_VALUE_STATE do { \
621 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
622 state->as_recorded |= ACL_STATE_RECORDED_VD; \
623 state->as_vd_acl = a; \
624 AC_MEMCPY( state->as_vd_acl_matches, matches, \
625 sizeof( state->as_vd_acl_matches )) ; \
626 state->as_vd_acl_count = count; \
627 state->as_vd_access = b; \
628 state->as_vd_access_count = i; \
633 * acl_mask - modifies mask based upon the given acl and the
634 * requested access to entry e, attribute attr, value val. if val
635 * is null, access to the whole attribute is assumed (all values).
637 * returns 0 access NOT allowed
641 static slap_control_t
647 AttributeDescription *desc,
652 AccessControlState *state )
654 int i, odnlen, patlen;
657 char accessmaskbuf[ACCESSMASK_MAXLEN];
658 char accessmaskbuf1[ACCESSMASK_MAXLEN];
663 assert( mask != NULL );
664 assert( desc != NULL );
666 attr = desc->ad_cname.bv_val;
668 assert( attr != NULL );
670 Debug( LDAP_DEBUG_ACL,
671 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
674 Debug( LDAP_DEBUG_ACL,
675 "=> acl_mask: to %s by \"%s\", (%s) \n",
676 val ? "value" : "all values",
677 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
678 accessmask2str( *mask, accessmaskbuf ) );
681 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
682 && state->as_vd_acl == a )
684 b = state->as_vd_access;
685 i = state->as_vd_access_count;
692 for ( ; b != NULL; b = b->a_next, i++ ) {
693 slap_mask_t oldmask, modmask;
695 ACL_INVALIDATE( modmask );
697 /* AND <who> clauses */
698 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
699 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
700 b->a_dn_pat.bv_val, 0, 0);
702 * if access applies to the entry itself, and the
703 * user is bound as somebody in the same namespace as
704 * the entry, OR the given dn matches the dn pattern
707 * NOTE: styles "anonymous", "users" and "self"
708 * have been moved to enum slap_style_t, whose
709 * value is set in a_dn_style; however, the string
710 * is maintaned in a_dn_pat.
712 if ( b->a_dn_style == ACL_STYLE_ANONYMOUS ) {
713 if ( op->o_ndn.bv_len != 0 ) {
717 } else if ( b->a_dn_style == ACL_STYLE_USERS ) {
718 if ( op->o_ndn.bv_len == 0 ) {
722 } else if ( b->a_dn_style == ACL_STYLE_SELF ) {
723 if ( op->o_ndn.bv_len == 0 ) {
727 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
731 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
732 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
734 regmatch_t tmp_matches[2],
735 *tmp_matchesp = tmp_matches;
739 switch ( a->acl_dn_style ) {
740 case ACL_STYLE_REGEX:
741 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
742 tmp_matchesp = matches;
746 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
749 tmp_matches[0].rm_so = 0;
750 tmp_matches[0].rm_eo = e->e_nname.bv_len;
755 case ACL_STYLE_SUBTREE:
756 case ACL_STYLE_CHILDREN:
757 tmp_matches[0].rm_so = 0;
758 tmp_matches[0].rm_eo = e->e_nname.bv_len;
759 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
760 tmp_matches[1].rm_eo = e->e_nname.bv_len;
774 if ( !regex_matches( &b->a_dn_pat,
775 op->o_ndn.bv_val, e->e_ndn,
776 tmp_nmatch, tmp_matchesp ) )
786 if ( e->e_dn == NULL )
789 if ( b->a_dn_expand ) {
791 char buf[ACL_BUF_SIZE];
794 regmatch_t tmp_matches[2],
795 *tmp_matchesp = tmp_matches;
799 bv.bv_len = sizeof( buf ) - 1;
802 switch ( a->acl_dn_style ) {
803 case ACL_STYLE_REGEX:
804 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
805 tmp_matchesp = matches;
809 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
812 tmp_matches[0].rm_so = 0;
813 tmp_matches[0].rm_eo = e->e_nname.bv_len;
818 case ACL_STYLE_SUBTREE:
819 case ACL_STYLE_CHILDREN:
820 tmp_matches[0].rm_so = 0;
821 tmp_matches[0].rm_eo = e->e_nname.bv_len;
822 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
823 tmp_matches[1].rm_eo = e->e_nname.bv_len;
837 if ( string_expand( &bv, &b->a_dn_pat,
839 tmp_nmatch, tmp_matchesp ) )
844 if ( dnNormalize(0, NULL, NULL, &bv,
845 &pat, op->o_tmpmemctx )
848 /* did not expand to a valid dn */
857 odnlen = op->o_ndn.bv_len;
858 if ( odnlen < patlen ) {
859 goto dn_match_cleanup;
863 if ( b->a_dn_style == ACL_STYLE_BASE ) {
864 /* base dn -- entire object DN must match */
865 if ( odnlen != patlen ) {
866 goto dn_match_cleanup;
869 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
872 if ( odnlen <= patlen ) {
873 goto dn_match_cleanup;
876 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
877 goto dn_match_cleanup;
880 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
881 if ( rdnlen != odnlen - patlen - 1 ) {
882 goto dn_match_cleanup;
885 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
886 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
887 goto dn_match_cleanup;
890 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
891 if ( odnlen <= patlen ) {
892 goto dn_match_cleanup;
895 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
896 goto dn_match_cleanup;
900 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
903 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
904 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
913 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
914 if ( ! op->o_conn->c_listener ) {
917 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
918 b->a_sockurl_pat.bv_val, 0, 0 );
920 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
921 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
922 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
923 e->e_ndn, nmatch, matches ) )
928 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
930 char buf[ACL_BUF_SIZE];
932 bv.bv_len = sizeof( buf ) - 1;
934 if ( string_expand( &bv, &b->a_sockurl_pat,
935 e->e_ndn, nmatch, matches ) )
940 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
946 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
954 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
955 if ( !op->o_conn->c_peer_domain.bv_val ) {
958 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
959 b->a_domain_pat.bv_val, 0, 0 );
960 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
961 if ( b->a_domain_style == ACL_STYLE_REGEX) {
962 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
963 e->e_ndn, nmatch, matches ) )
968 char buf[ACL_BUF_SIZE];
970 struct berval cmp = op->o_conn->c_peer_domain;
971 struct berval pat = b->a_domain_pat;
973 if ( b->a_domain_expand ) {
976 bv.bv_len = sizeof(buf) - 1;
979 if ( string_expand(&bv, &b->a_domain_pat,
980 e->e_ndn, nmatch, matches) )
987 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
988 int offset = cmp.bv_len - pat.bv_len;
993 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
997 /* trim the domain */
998 cmp.bv_val = &cmp.bv_val[ offset ];
999 cmp.bv_len -= offset;
1002 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1009 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1010 if ( !op->o_conn->c_peer_name.bv_val ) {
1013 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1014 b->a_peername_pat.bv_val, 0, 0 );
1015 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1016 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1017 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1018 e->e_ndn, nmatch, matches ) )
1024 /* try exact match */
1025 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1026 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1030 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1032 char buf[ACL_BUF_SIZE];
1034 bv.bv_len = sizeof( buf ) - 1;
1036 if ( string_expand( &bv, &b->a_peername_pat,
1037 e->e_ndn, nmatch, matches ) )
1042 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1046 /* extract IP and try exact match */
1047 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1049 char buf[] = "255.255.255.255";
1052 int port_number = -1;
1054 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1055 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1058 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1059 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1061 port = strrchr( ip.bv_val, ':' );
1065 ip.bv_len = port - ip.bv_val;
1067 port_number = strtol( port, &next, 10 );
1068 if ( next[0] != '\0' )
1072 /* the port check can be anticipated here */
1073 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1076 /* address longer than expected? */
1077 if ( ip.bv_len >= sizeof(buf) )
1080 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1081 buf[ ip.bv_len ] = '\0';
1083 addr = inet_addr( buf );
1085 /* unable to convert? */
1086 if ( addr == (unsigned long)(-1) )
1089 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1092 #ifdef LDAP_PF_LOCAL
1093 /* extract path and try exact match */
1094 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1097 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1098 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1101 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1102 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1104 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1107 #endif /* LDAP_PF_LOCAL */
1109 /* exact match (very unlikely...) */
1110 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1117 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1118 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1121 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1122 b->a_sockname_pat.bv_val, 0, 0 );
1123 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1124 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1125 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1126 e->e_ndn, nmatch, matches ) )
1131 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1133 char buf[ACL_BUF_SIZE];
1135 bv.bv_len = sizeof( buf ) - 1;
1137 if ( string_expand( &bv, &b->a_sockname_pat,
1138 e->e_ndn, nmatch, matches ) )
1143 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1148 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1155 if ( b->a_dn_at != NULL ) {
1160 const char *attr = b->a_dn_at->ad_cname.bv_val;
1162 assert( attr != NULL );
1164 if ( op->o_ndn.bv_len == 0 ) {
1168 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1172 /* see if asker is listed in dnattr */
1173 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1175 at = attrs_find( at->a_next, b->a_dn_at ) )
1177 if( value_find_ex( b->a_dn_at,
1178 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1179 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1181 &bv, op->o_tmpmemctx ) == 0 )
1190 /* have a dnattr match. if this is a self clause then
1191 * the target must also match the op dn.
1193 if ( b->a_dn_self ) {
1194 /* check if the target is an attribute. */
1195 if ( val == NULL ) continue;
1197 /* target is attribute, check if the attribute value
1200 rc = value_match( &match, b->a_dn_at,
1201 b->a_dn_at->ad_type->sat_equality, 0,
1203 /* on match error or no match, fail the ACL clause */
1204 if (rc != LDAP_SUCCESS || match != 0 )
1208 /* no dnattr match, check if this is a self clause */
1209 if ( ! b->a_dn_self )
1212 ACL_RECORD_VALUE_STATE;
1214 /* this is a self clause, check if the target is an
1220 /* target is attribute, check if the attribute value
1223 rc = value_match( &match, b->a_dn_at,
1224 b->a_dn_at->ad_type->sat_equality, 0,
1227 /* on match error or no match, fail the ACL clause */
1228 if (rc != LDAP_SUCCESS || match != 0 )
1233 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1235 struct berval ndn = BER_BVNULL;
1238 if ( op->o_ndn.bv_len == 0 ) {
1242 /* b->a_group is an unexpanded entry name, expanded it should be an
1243 * entry with objectclass group* and we test to see if odn is one of
1244 * the values in the attribute group
1246 /* see if asker is listed in dnattr */
1247 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1248 char buf[ACL_BUF_SIZE];
1250 regmatch_t tmp_matches[2],
1251 *tmp_matchesp = tmp_matches;
1253 bv.bv_len = sizeof(buf) - 1;
1258 switch ( a->acl_dn_style ) {
1259 case ACL_STYLE_REGEX:
1260 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1261 tmp_matchesp = matches;
1262 tmp_nmatch = nmatch;
1266 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1267 case ACL_STYLE_BASE:
1268 tmp_matches[0].rm_so = 0;
1269 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1274 case ACL_STYLE_SUBTREE:
1275 case ACL_STYLE_CHILDREN:
1276 tmp_matches[0].rm_so = 0;
1277 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1278 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1279 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1293 if ( string_expand( &bv, &b->a_group_pat,
1295 tmp_nmatch, tmp_matchesp ) )
1300 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1301 op->o_tmpmemctx ) != LDAP_SUCCESS )
1303 /* did not expand to a valid dn */
1310 bv = b->a_group_pat;
1313 rc = backend_group( op, e, &bv, &op->o_ndn,
1314 b->a_group_oc, b->a_group_at );
1317 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1325 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1327 char buf[ACL_BUF_SIZE];
1329 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1331 regmatch_t tmp_matches[2],
1332 *tmp_matchesp = tmp_matches;
1335 bv.bv_len = sizeof( buf ) - 1;
1340 switch ( a->acl_dn_style ) {
1341 case ACL_STYLE_REGEX:
1342 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1343 tmp_matchesp = matches;
1344 tmp_nmatch = nmatch;
1348 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1349 case ACL_STYLE_BASE:
1350 tmp_matches[0].rm_so = 0;
1351 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1356 case ACL_STYLE_SUBTREE:
1357 case ACL_STYLE_CHILDREN:
1358 tmp_matches[0].rm_so = 0;
1359 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1360 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1361 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1375 if ( string_expand( &bv, &b->a_set_pat,
1377 tmp_nmatch, tmp_matchesp ) )
1386 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1391 if ( b->a_authz.sai_ssf ) {
1392 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1393 b->a_authz.sai_ssf, op->o_ssf, 0 );
1394 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1399 if ( b->a_authz.sai_transport_ssf ) {
1400 Debug( LDAP_DEBUG_ACL,
1401 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1402 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1403 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1408 if ( b->a_authz.sai_tls_ssf ) {
1409 Debug( LDAP_DEBUG_ACL,
1410 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1411 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1412 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1417 if ( b->a_authz.sai_sasl_ssf ) {
1418 Debug( LDAP_DEBUG_ACL,
1419 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1420 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1421 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1427 if ( b->a_dynacl ) {
1429 slap_access_t tgrant, tdeny;
1431 /* this case works different from the others above.
1432 * since aci's themselves give permissions, we need
1433 * to first check b->a_access_mask, the ACL's access level.
1435 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1436 /* no ACIs in the root DSE */
1440 /* first check if the right being requested
1441 * is allowed by the ACL clause.
1443 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1447 /* start out with nothing granted, nothing denied */
1451 for ( da = b->a_dynacl; da; da = da->da_next ) {
1452 slap_access_t grant, deny;
1454 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1460 /* remove anything that the ACL clause does not allow */
1461 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1462 tdeny &= ACL_PRIV_MASK;
1464 /* see if we have anything to contribute */
1465 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1469 /* this could be improved by changing acl_mask so that it can deal with
1470 * by clauses that return grant/deny pairs. Right now, it does either
1471 * additive or subtractive rights, but not both at the same time. So,
1472 * we need to combine the grant/deny pair into a single rights mask in
1473 * a smart way: if either grant or deny is "empty", then we use the
1474 * opposite as is, otherwise we remove any denied rights from the grant
1475 * rights mask and construct an additive mask.
1477 if (ACL_IS_INVALID(tdeny)) {
1478 modmask = tgrant | ACL_PRIV_ADDITIVE;
1480 } else if (ACL_IS_INVALID(tgrant)) {
1481 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1484 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1488 #else /* !SLAP_DYNACL */
1490 #ifdef SLAPD_ACI_ENABLED
1491 if ( b->a_aci_at != NULL ) {
1493 slap_access_t grant, deny, tgrant, tdeny;
1494 struct berval parent_ndn,
1495 old_parent_ndn = BER_BVNULL;
1496 BerVarray bvals = NULL;
1499 /* this case works different from the others above.
1500 * since aci's themselves give permissions, we need
1501 * to first check b->a_access_mask, the ACL's access level.
1504 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1505 /* no ACIs in the root DSE */
1509 /* first check if the right being requested
1510 * is allowed by the ACL clause.
1512 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1515 /* start out with nothing granted, nothing denied */
1519 /* get the aci attribute */
1520 at = attr_find( e->e_attrs, b->a_aci_at );
1523 /* FIXME: this breaks acl caching;
1524 * see also ACL_RECORD_VALUE_STATE below */
1525 ACL_RECORD_VALUE_STATE;
1527 /* the aci is an multi-valued attribute. The
1528 * rights are determined by OR'ing the individual
1529 * rights given by the acis.
1531 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1536 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
1542 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1543 accessmask2str(tgrant,accessmaskbuf),
1544 accessmask2str(tdeny, accessmaskbuf1), 0);
1547 /* If the entry level aci didn't contain anything valid for the
1548 * current operation, climb up the tree and evaluate the
1549 * acis with scope set to subtree
1551 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
1552 dnParent(&(e->e_nname), &parent_ndn);
1553 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ) {
1554 old_parent_ndn = parent_ndn;
1555 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1556 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1564 for( i = 0; bvals[i].bv_val != NULL; i++){
1566 /* FIXME: this breaks acl caching;
1567 * see also ACL_RECORD_VALUE_STATE above */
1568 ACL_RECORD_VALUE_STATE;
1570 if (aci_mask(op, e, desc, val, &bvals[i],
1572 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
1576 /* evaluation stops as soon as either a "deny" or a
1577 * "grant" directive matches.
1579 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1583 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1584 accessmask2str(tgrant,accessmaskbuf),
1585 accessmask2str(tdeny, accessmaskbuf1), 0);
1589 case LDAP_NO_SUCH_ATTRIBUTE:
1590 /* just go on if the aci-Attribute is not present in
1593 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1597 case LDAP_NO_SUCH_OBJECT:
1598 /* We have reached the base object */
1599 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1610 dnParent(&old_parent_ndn, &parent_ndn);
1615 /* remove anything that the ACL clause does not allow */
1616 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1617 tdeny &= ACL_PRIV_MASK;
1619 /* see if we have anything to contribute */
1620 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1624 /* this could be improved by changing acl_mask so that it can deal with
1625 * by clauses that return grant/deny pairs. Right now, it does either
1626 * additive or subtractive rights, but not both at the same time. So,
1627 * we need to combine the grant/deny pair into a single rights mask in
1628 * a smart way: if either grant or deny is "empty", then we use the
1629 * opposite as is, otherwise we remove any denied rights from the grant
1630 * rights mask and construct an additive mask.
1632 if (ACL_IS_INVALID(tdeny)) {
1633 modmask = tgrant | ACL_PRIV_ADDITIVE;
1635 } else if (ACL_IS_INVALID(tgrant)) {
1636 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1639 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1643 #endif /* SLAPD_ACI_ENABLED */
1644 #endif /* !SLAP_DYNACL */
1646 modmask = b->a_access_mask;
1649 Debug( LDAP_DEBUG_ACL,
1650 "<= acl_mask: [%d] applying %s (%s)\n",
1651 i, accessmask2str( modmask, accessmaskbuf ),
1652 b->a_type == ACL_CONTINUE
1654 : b->a_type == ACL_BREAK
1660 if( ACL_IS_ADDITIVE(modmask) ) {
1662 ACL_PRIV_SET( *mask, modmask );
1665 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1667 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1668 /* substract privs */
1669 ACL_PRIV_CLR( *mask, modmask );
1672 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1679 Debug( LDAP_DEBUG_ACL,
1680 "<= acl_mask: [%d] mask: %s\n",
1681 i, accessmask2str(*mask, accessmaskbuf), 0 );
1683 if( b->a_type == ACL_CONTINUE ) {
1686 } else if ( b->a_type == ACL_BREAK ) {
1694 /* implicit "by * none" clause */
1697 Debug( LDAP_DEBUG_ACL,
1698 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1699 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1704 * acl_check_modlist - check access control on the given entry to see if
1705 * it allows the given modifications by the user associated with op.
1706 * returns 1 if mods allowed ok
1707 * 0 mods not allowed
1714 Modifications *mlist
1718 AccessControlState state = ACL_STATE_INIT;
1721 int ret = 1; /* default is access allowed */
1729 assert( be != NULL );
1731 /* short circuit root database access */
1732 if ( be_isroot( op ) ) {
1733 Debug( LDAP_DEBUG_ACL,
1734 "<= acl_access_allowed: granted to database root\n",
1739 /* use backend default access if no backend acls */
1740 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1741 Debug( LDAP_DEBUG_ACL,
1742 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1743 access2str( ACL_WRITE ),
1744 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1745 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1749 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1751 * no-user-modification operational attributes are ignored
1752 * by ACL_WRITE checking as any found here are not provided
1755 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1756 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1757 " modify access granted\n",
1758 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1762 switch ( mlist->sml_op ) {
1763 case LDAP_MOD_REPLACE:
1765 * We must check both permission to delete the whole
1766 * attribute and permission to add the specific attributes.
1767 * This prevents abuse from selfwriters.
1769 if ( ! access_allowed( op, e,
1770 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1776 if ( mlist->sml_values == NULL ) break;
1778 /* fall thru to check value to add */
1781 assert( mlist->sml_values != NULL );
1783 for ( bv = mlist->sml_nvalues
1784 ? mlist->sml_nvalues : mlist->sml_values;
1785 bv->bv_val != NULL; bv++ )
1787 if ( ! access_allowed( op, e,
1788 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1796 case LDAP_MOD_DELETE:
1797 if ( mlist->sml_values == NULL ) {
1798 if ( ! access_allowed( op, e,
1799 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1806 for ( bv = mlist->sml_nvalues
1807 ? mlist->sml_nvalues : mlist->sml_values;
1808 bv->bv_val != NULL; bv++ )
1810 if ( ! access_allowed( op, e,
1811 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1819 case SLAP_MOD_SOFTADD:
1820 /* allow adding attribute via modrdn thru */
1832 if (be_null) op->o_bd = NULL;
1838 struct berval *list,
1851 while ( len >= 0 && --ix >= 0 ) {
1852 while ( --len >= 0 && *p++ != sep )
1855 while ( len >= 0 && *p == ' ' ) {
1868 while ( --len >= 0 && *p != sep ) {
1872 while ( bv->bv_len > 0 && *--p == ' ' ) {
1879 typedef struct aci_set_gather_t {
1885 aci_set_cb_gather( Operation *op, SlapReply *rs )
1887 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
1889 if ( rs->sr_type == REP_SEARCH ) {
1890 BerValue bvals[ 2 ];
1891 BerVarray bvalsp = NULL;
1894 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1895 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
1897 if ( desc == slap_schema.si_ad_entryDN ) {
1899 bvals[ 0 ] = rs->sr_entry->e_nname;
1900 BER_BVZERO( &bvals[ 1 ] );
1905 a = attr_find( rs->sr_entry->e_attrs, desc );
1909 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1912 bvalsp = a->a_nvals;
1918 p->bvals = slap_set_join( p->cookie, p->bvals,
1919 ( '|' | SLAP_SET_RREF ), bvalsp );
1923 assert( rs->sr_type == REP_RESULT );
1930 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
1932 AciSetCookie *cp = (AciSetCookie *)cookie;
1934 LDAPURLDesc *ludp = NULL;
1935 Operation op2 = { 0 };
1936 SlapReply rs = {REP_RESULT};
1937 AttributeName anlist[ 2 ], *anlistp = NULL;
1939 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
1940 aci_set_gather_t p = { 0 };
1941 const char *text = NULL;
1942 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
1944 /* this routine needs to return the bervals instead of
1945 * plain strings, since syntax is not known. It should
1946 * also return the syntax or some "comparison cookie".
1948 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
1949 return aci_set_gather2( cookie, name, desc );
1952 rc = ldap_url_parse( name->bv_val, &ludp );
1953 if ( rc != LDAP_URL_SUCCESS ) {
1954 rc = LDAP_PROTOCOL_ERROR;
1958 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
1960 /* host part must be empty */
1961 /* extensions parts must be empty */
1962 rc = LDAP_PROTOCOL_ERROR;
1966 /* Grab the searchbase and see if an appropriate database can be found */
1967 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
1968 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
1969 &op2.o_req_ndn, cp->op->o_tmpmemctx );
1970 BER_BVZERO( &op2.o_req_dn );
1971 if ( rc != LDAP_SUCCESS ) {
1975 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
1976 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
1977 rc = LDAP_NO_SUCH_OBJECT;
1981 /* Grab the filter */
1982 if ( ludp->lud_filter ) {
1983 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
1984 cp->op->o_tmpmemctx );
1987 op2.ors_filterstr = defaultFilter_bv;
1990 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
1991 if ( op2.ors_filter == NULL ) {
1992 rc = LDAP_PROTOCOL_ERROR;
1996 /* Grab the scope */
1997 op2.ors_scope = ludp->lud_scope;
1999 /* Grap the attributes */
2000 if ( ludp->lud_attrs ) {
2001 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2004 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2005 cp->op->o_tmpmemctx );
2007 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2008 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2009 anlistp[ nattrs ].an_desc = NULL;
2010 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2011 &anlistp[ nattrs ].an_desc, &text );
2012 if ( rc != LDAP_SUCCESS ) {
2021 anlistp[ nattrs ].an_name = desc->ad_cname;
2022 anlistp[ nattrs ].an_desc = desc;
2024 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2028 op2.o_hdr = cp->op->o_hdr;
2029 op2.o_tag = LDAP_REQ_SEARCH;
2030 op2.o_ndn = op2.o_bd->be_rootndn;
2031 op2.o_callback = &cb;
2032 op2.o_time = slap_get_time();
2033 op2.o_do_not_cache = 1;
2034 op2.o_is_auth_check = 0;
2035 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2036 op2.ors_slimit = SLAP_NO_LIMIT;
2037 op2.ors_tlimit = SLAP_NO_LIMIT;
2038 op2.ors_attrs = anlistp;
2039 op2.ors_attrsonly = 0;
2043 rc = op2.o_bd->be_search( &op2, &rs );
2049 if ( op2.ors_filter ) {
2050 filter_free_x( cp->op, op2.ors_filter );
2052 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2053 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2055 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2056 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2059 ldap_free_urldesc( ludp );
2061 if ( anlistp && anlistp != anlist ) {
2062 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2069 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2071 AciSetCookie *cp = (AciSetCookie *)cookie;
2072 BerVarray bvals = NULL;
2076 /* this routine needs to return the bervals instead of
2077 * plain strings, since syntax is not known. It should
2078 * also return the syntax or some "comparison cookie".
2080 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2081 if ( rc == LDAP_SUCCESS ) {
2082 if ( desc == slap_schema.si_ad_entryDN ) {
2083 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2084 cp->op->o_tmpmemctx );
2086 BER_BVZERO( &bvals[ 1 ] );
2090 backend_attribute( cp->op,
2091 cp->e, &ndn, desc, &bvals, ACL_NONE );
2094 if ( !BER_BVISNULL( &ndn ) ) {
2095 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2104 struct berval *subj,
2110 struct berval set = BER_BVNULL;
2112 AciSetCookie cookie;
2115 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2117 struct berval subjdn, ndn = BER_BVNULL;
2118 struct berval setat;
2121 AttributeDescription *desc = NULL;
2123 /* format of string is "entry/setAttrName" */
2124 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2128 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2129 setat = aci_bv_set_attr;
2133 * NOTE: dnNormalize honors the ber_len field
2134 * as the length of the dn to be normalized
2136 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2137 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2139 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2140 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2144 BER_BVZERO( &bvals[0] );
2145 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2147 bvals[0].bv_val = bvals[i-1].bv_val;
2148 BER_BVZERO( &bvals[i-1] );
2150 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2151 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2156 if ( !BER_BVISNULL( &set ) ) {
2159 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2160 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2161 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2167 #ifdef SLAPD_ACI_ENABLED
2169 aci_list_map_rights(
2170 struct berval *list )
2177 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2180 switch (*bv.bv_val) {
2182 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2185 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2186 * the right 's' to mean "set", but in the examples states
2187 * that the right 's' means "search". The latter definition
2190 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2193 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2196 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2199 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2200 * define any equivalent to the AUTH right, so I've just used
2203 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2215 struct berval *list,
2216 const struct berval *attr,
2217 struct berval *val )
2219 struct berval bv, left, right;
2222 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2223 if (aci_get_part(&bv, 0, '=', &left) < 0
2224 || aci_get_part(&bv, 1, '=', &right) < 0)
2226 if (ber_bvstrcasecmp(attr, &bv) == 0)
2228 } else if (val == NULL) {
2229 if (ber_bvstrcasecmp(attr, &left) == 0)
2232 if (ber_bvstrcasecmp(attr, &left) == 0) {
2233 /* this is experimental code that implements a
2234 * simple (prefix) match of the attribute value.
2235 * the ACI draft does not provide for aci's that
2236 * apply to specific values, but it would be
2237 * nice to have. If the <attr> part of an aci's
2238 * rights list is of the form <attr>=<value>,
2239 * that means the aci applies only to attrs with
2240 * the given value. Furthermore, if the attr is
2241 * of the form <attr>=<value>*, then <value> is
2242 * treated as a prefix, and the aci applies to
2243 * any value with that prefix.
2245 * Ideally, this would allow r.e. matches.
2247 if (aci_get_part(&right, 0, '*', &left) < 0
2248 || right.bv_len <= left.bv_len)
2250 if (ber_bvstrcasecmp(val, &right) == 0)
2252 } else if (val->bv_len >= left.bv_len) {
2253 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2262 static slap_access_t
2263 aci_list_get_attr_rights(
2264 struct berval *list,
2265 const struct berval *attr,
2266 struct berval *val )
2272 /* loop through each rights/attr pair, skip first part (action) */
2274 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2275 if (aci_list_has_attr(&bv, attr, val) == 0)
2277 if (aci_get_part(list, i, ';', &bv) < 0)
2279 mask |= aci_list_map_rights(&bv);
2285 aci_list_get_rights(
2286 struct berval *list,
2287 const struct berval *attr,
2289 slap_access_t *grant,
2290 slap_access_t *deny )
2292 struct berval perm, actn;
2293 slap_access_t *mask;
2296 if (attr == NULL || attr->bv_len == 0
2297 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2298 attr = &aci_bv_br_entry;
2304 /* loop through each permissions clause */
2305 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2306 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2308 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2310 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2317 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2318 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2325 struct berval *subj,
2326 struct berval *defgrpoc,
2327 struct berval *defgrpat,
2334 struct berval subjdn;
2335 struct berval grpoc;
2336 struct berval grpat;
2337 ObjectClass *grp_oc = NULL;
2338 AttributeDescription *grp_ad = NULL;
2342 /* format of string is "group/objectClassValue/groupAttrName" */
2343 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2347 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2351 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2355 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2356 if( rc != LDAP_SUCCESS ) {
2362 grp_oc = oc_bvfind( &grpoc );
2364 if (grp_oc != NULL && grp_ad != NULL ) {
2365 char buf[ACL_BUF_SIZE];
2366 struct berval bv, ndn;
2367 bv.bv_len = sizeof( buf ) - 1;
2368 bv.bv_val = (char *)&buf;
2369 if ( string_expand(&bv, &subjdn,
2370 e->e_ndn, nmatch, matches) )
2375 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2376 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2377 grp_oc, grp_ad ) == 0 );
2378 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2390 AttributeDescription *desc,
2394 regmatch_t *matches,
2395 slap_access_t *grant,
2396 slap_access_t *deny,
2397 slap_aci_scope_t asserted_scope
2400 struct berval bv, scope, perms, type, sdn;
2404 assert( !BER_BVISNULL( &desc->ad_cname ) );
2406 /* parse an aci of the form:
2407 oid # scope # action;rights;attr;rights;attr
2408 $ action;rights;attr;rights;attr # type # subject
2410 [NOTE: the following comment is very outdated,
2411 as the draft version it refers to (Ando, 2004-11-20)].
2413 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2414 a full description of the format for this attribute.
2415 Differences: "this" in the draft is "self" here, and
2416 "self" and "public" is in the position of type.
2418 <scope> = {entry|children|subtree}
2419 <type> = {public|users|access-id|subtree|onelevel|children|
2420 self|dnattr|group|role|set|set-ref}
2422 This routine now supports scope={ENTRY,CHILDREN}
2424 - ENTRY applies to "entry" and "subtree";
2425 - CHILDREN aplies to "children" and "subtree"
2428 /* check that the aci has all 5 components */
2429 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2433 /* check that the aci family is supported */
2434 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2438 /* check that the scope matches */
2439 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2443 /* note: scope can be either ENTRY or CHILDREN;
2444 * they respectively match "entry" and "children" in bv
2445 * both match "subtree" */
2446 switch ( asserted_scope ) {
2447 case SLAP_ACI_SCOPE_ENTRY:
2448 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2449 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2455 case SLAP_ACI_SCOPE_CHILDREN:
2456 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2457 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2467 /* get the list of permissions clauses, bail if empty */
2468 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2472 /* check if any permissions allow desired access */
2473 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2477 /* see if we have a DN match */
2478 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2482 /* see if we have a public (i.e. anonymous) access */
2483 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2487 /* otherwise require an identity */
2488 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2492 /* see if we have a users access */
2493 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2497 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2498 * just grab all the berval up to its end (ITS#3303).
2499 * NOTE: the problem could be solved by providing the DN with
2500 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
2501 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2503 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2507 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2508 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2510 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2513 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2514 if ( rc != LDAP_SUCCESS ) {
2518 if ( dn_match( &op->o_ndn, &ndn ) ) {
2521 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2525 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2528 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2529 if ( rc != LDAP_SUCCESS ) {
2533 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2536 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2540 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2541 struct berval ndn, pndn;
2543 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2544 if ( rc != LDAP_SUCCESS ) {
2548 dnParent( &ndn, &pndn );
2550 if ( dn_match( &op->o_ndn, &pndn ) ) {
2553 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2557 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2560 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2561 if ( rc != LDAP_SUCCESS ) {
2565 if ( !dn_match( &op->o_ndn, &ndn )
2566 && dnIsSuffix( &op->o_ndn, &ndn ) )
2570 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2574 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2575 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2579 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2581 AttributeDescription *ad = NULL;
2584 rc = slap_bv2ad( &sdn, &ad, &text );
2586 if( rc != LDAP_SUCCESS ) {
2592 for ( at = attrs_find( e->e_attrs, ad );
2594 at = attrs_find( at->a_next, ad ) )
2596 if ( value_find_ex( ad,
2597 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2598 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2600 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2609 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2610 if ( aci_group_member( &sdn, &aci_bv_group_class,
2611 &aci_bv_group_attr, op, e, nmatch, matches ) )
2616 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2617 if ( aci_group_member( &sdn, &aci_bv_role_class,
2618 &aci_bv_role_attr, op, e, nmatch, matches ) )
2623 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2624 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2628 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2629 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2639 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2641 AttributeDescription *ad = NULL;
2642 const char *text = NULL;
2644 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2645 fprintf( stderr, "%s: line %d: "
2646 "inappropriate style \"%s\" in \"aci\" by clause\n",
2647 fname, lineno, sty );
2651 if ( right != NULL && *right != '\0' ) {
2652 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2654 "%s: line %d: aci \"%s\": %s\n",
2655 fname, lineno, right, text );
2660 ad = slap_schema.si_ad_aci;
2663 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2664 fprintf( stderr, "%s: line %d: "
2665 "aci \"%s\": inappropriate syntax: %s\n",
2666 fname, lineno, right,
2667 ad->ad_type->sat_syntax_oid );
2671 *privp = (void *)ad;
2677 dynacl_aci_print( void *priv )
2679 AttributeDescription *ad = ( AttributeDescription * )priv;
2683 fprintf( stderr, " aci=%s", ad->ad_cname.bv_val );
2694 AttributeDescription *desc,
2697 regmatch_t *matches,
2698 slap_access_t *grantp,
2699 slap_access_t *denyp )
2701 AttributeDescription *ad = ( AttributeDescription * )priv;
2703 slap_access_t tgrant, tdeny, grant, deny;
2705 char accessmaskbuf[ACCESSMASK_MAXLEN];
2706 char accessmaskbuf1[ACCESSMASK_MAXLEN];
2707 #endif /* LDAP_DEBUG */
2709 /* start out with nothing granted, nothing denied */
2713 /* get the aci attribute */
2714 at = attr_find( e->e_attrs, ad );
2718 /* the aci is an multi-valued attribute. The
2719 * rights are determined by OR'ing the individual
2720 * rights given by the acis.
2722 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2723 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2724 nmatch, matches, &grant, &deny,
2725 SLAP_ACI_SCOPE_ENTRY ) != 0 )
2732 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2733 accessmask2str( tgrant, accessmaskbuf ),
2734 accessmask2str( tdeny, accessmaskbuf1 ), 0 );
2737 /* If the entry level aci didn't contain anything valid for the
2738 * current operation, climb up the tree and evaluate the
2739 * acis with scope set to subtree
2741 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2742 struct berval parent_ndn;
2743 struct berval old_parent_ndn = BER_BVNULL;
2746 /* to solve the chicken'n'egg problem of accessing
2747 * the OpenLDAPaci attribute, the direct access
2748 * to the entry's attribute is unchecked; however,
2749 * further accesses to OpenLDAPaci values in the
2750 * ancestors occur through backend_attribute(), i.e.
2751 * with the identity of the operation, requiring
2752 * further access checking. For uniformity, this
2753 * makes further requests occur as the rootdn, if
2754 * any, i.e. searching for the OpenLDAPaci attribute
2755 * is considered an internal search. If this is not
2756 * acceptable, then the same check needs be performed
2757 * when accessing the entry's attribute. */
2758 Operation op2 = *op;
2760 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
2761 op2.o_dn = op->o_bd->be_rootdn;
2762 op2.o_ndn = op->o_bd->be_rootndn;
2766 dnParent( &e->e_nname, &parent_ndn );
2767 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
2769 BerVarray bvals = NULL;
2772 old_parent_ndn = parent_ndn;
2773 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
2774 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
2783 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
2784 if ( aci_mask( op, e, desc, val,
2788 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2792 /* evaluation stops as soon as either a "deny" or a
2793 * "grant" directive matches.
2795 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
2799 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2800 accessmask2str( tgrant, accessmaskbuf ),
2801 accessmask2str( tdeny, accessmaskbuf1 ), 0 );
2805 case LDAP_NO_SUCH_ATTRIBUTE:
2806 /* just go on if the aci-Attribute is not present in
2809 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
2813 case LDAP_NO_SUCH_OBJECT:
2814 /* We have reached the base object */
2815 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
2827 dnParent( &old_parent_ndn, &parent_ndn );
2837 /* need to register this at some point */
2838 static slap_dynacl_t dynacl_aci = {
2848 #endif /* SLAP_DYNACL */
2850 #endif /* SLAPD_ACI_ENABLED */
2855 * dynamic ACL infrastructure
2857 static slap_dynacl_t *da_list = NULL;
2860 slap_dynacl_register( slap_dynacl_t *da )
2864 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2865 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2870 if ( tmp != NULL ) {
2874 if ( da->da_mask == NULL ) {
2878 da->da_private = NULL;
2879 da->da_next = da_list;
2885 static slap_dynacl_t *
2886 slap_dynacl_next( slap_dynacl_t *da )
2895 slap_dynacl_get( const char *name )
2899 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2900 if ( strcasecmp( da->da_name, name ) == 0 ) {
2907 #endif /* SLAP_DYNACL */
2914 slap_dynacl_t *known_dynacl[] = {
2915 #ifdef SLAPD_ACI_ENABLED
2917 #endif /* SLAPD_ACI_ENABLED */
2921 for ( i = 0; known_dynacl[ i ]; i++ ) {
2922 rc = slap_dynacl_register( known_dynacl[ i ] );
2927 #endif /* SLAP_DYNACL */
2938 regmatch_t *matches)
2946 bv->bv_val[0] = '\0';
2947 bv->bv_len--; /* leave space for lone $ */
2950 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2951 sp < pat->bv_val + pat->bv_len ; sp++ )
2953 /* did we previously see a $ */
2955 if ( flag == 1 && *sp == '$' ) {
2960 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2963 } else if ( *sp >= '0' && *sp <= '9' ) {
2971 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2972 if ( *sp >= '0' && *sp <= '9' ) {
2973 n = 10*n + ( *sp - '0' );
2977 if ( *sp != /*'{'*/ '}' ) {
2983 if ( n >= nmatch ) {
2989 i = matches[n].rm_so;
2990 l = matches[n].rm_eo;
2991 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3009 /* must have ended with a single $ */
3017 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3018 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3025 struct berval *pat, /* pattern to expand and match against */
3026 char *str, /* string to match against pattern */
3027 char *buf, /* buffer with $N expansion variables */
3028 int nmatch, /* size of the matches array */
3029 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3033 char newbuf[ACL_BUF_SIZE];
3037 bv.bv_len = sizeof( newbuf ) - 1;
3044 string_expand( &bv, pat, buf, nmatch, matches );
3045 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3047 char error[ACL_BUF_SIZE];
3048 regerror( rc, &re, error, sizeof( error ) );
3050 Debug( LDAP_DEBUG_TRACE,
3051 "compile( \"%s\", \"%s\") failed %s\n",
3052 pat->bv_val, str, error );
3056 rc = regexec( &re, str, 0, NULL, 0 );
3059 Debug( LDAP_DEBUG_TRACE,
3060 "=> regex_matches: string: %s\n", str, 0, 0 );
3061 Debug( LDAP_DEBUG_TRACE,
3062 "=> regex_matches: rc: %d %s\n",
3063 rc, !rc ? "matches" : "no matches", 0 );