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_ip_eq = BER_BVC("IP="),
68 aci_bv_path_eq = BER_BVC("PATH="),
69 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
70 #endif /* LDAP_PF_LOCAL */
72 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
73 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
74 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
75 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
76 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
79 static AccessControl * acl_get(
80 AccessControl *ac, int *count,
81 Operation *op, Entry *e,
82 AttributeDescription *desc,
84 int nmatches, regmatch_t *matches,
85 AccessControlState *state );
87 static slap_control_t acl_mask(
88 AccessControl *ac, slap_mask_t *mask,
89 Operation *op, Entry *e,
90 AttributeDescription *desc,
94 AccessControlState *state );
96 #ifdef SLAPD_ACI_ENABLED
98 Operation *op, Entry *e,
99 AttributeDescription *desc,
103 slap_access_t *grant,
105 struct berval *scope);
108 static int regex_matches(
109 struct berval *pat, char *str, char *buf, regmatch_t *matches);
110 static int string_expand(
111 struct berval *newbuf, struct berval *pattern,
112 char *match, regmatch_t *matches);
114 typedef struct AciSetCookie {
119 SLAP_SET_GATHER aci_set_gather;
120 static int aci_match_set ( struct berval *subj, Operation *op,
121 Entry *e, int setref );
124 * access_allowed - check whether op->o_ndn is allowed the requested access
125 * to entry e, attribute attr, value val. if val is null, access to
126 * the whole attribute is assumed (all values).
128 * This routine loops through all access controls and calls
129 * acl_mask() on each applicable access control.
130 * The loop exits when a definitive answer is reached or
131 * or no more controls remain.
138 * - can be legally called with op == NULL
139 * - can be legally called with op->o_bd == NULL
146 AttributeDescription *desc,
148 slap_access_t access,
149 AccessControlState *state,
154 AccessControl *a = NULL;
159 char accessmaskbuf[ACCESSMASK_MAXLEN];
162 slap_control_t control;
164 regmatch_t matches[MAXREMATCHES];
165 int st_same_attr = 0;
166 static AccessControlState state_init = ACL_STATE_INIT;
169 assert( desc != NULL );
170 assert( access > ACL_NONE );
171 if ( maskp ) ACL_INVALIDATE( *maskp );
173 attr = desc->ad_cname.bv_val;
175 assert( attr != NULL );
177 if( op && op->o_is_auth_check &&
178 ( access == ACL_SEARCH || access == ACL_READ ))
184 if ( state->as_vd_ad==desc) {
185 if ( state->as_recorded ) {
186 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
189 return state->as_result;
190 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
191 val != NULL && state->as_vd_acl == NULL )
193 return state->as_result;
201 state->as_vd_ad=desc;
204 Debug( LDAP_DEBUG_ACL,
205 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
206 access2str( access ), e->e_dn, attr );
219 * FIXME: experimental; use first backend rules
220 * iff there is no global_acl (ITS#3100) */
221 if ( frontendDB->be_acl == NULL )
227 assert( be != NULL );
230 if ( op->o_pb != NULL ) {
231 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
233 /* ACL plugin denied access */
237 #endif /* LDAP_SLAPI */
239 /* grant database root access */
240 if ( be != NULL && be_isroot( op ) ) {
241 Debug( LDAP_DEBUG_ACL,
242 "<= root access granted\n",
245 mask = ACL_LVL_WRITE;
252 * no-user-modification operational attributes are ignored
253 * by ACL_WRITE checking as any found here are not provided
256 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
257 && desc != slap_schema.si_ad_entry
258 && desc != slap_schema.si_ad_children )
260 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
261 " %s access granted\n",
266 /* use backend default access if no backend acls */
267 if( be != NULL && be->be_acl == NULL ) {
268 Debug( LDAP_DEBUG_ACL,
269 "=> access_allowed: backend default %s access %s to \"%s\"\n",
270 access2str( access ),
271 be->be_dfltaccess >= access ? "granted" : "denied",
272 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
273 ret = be->be_dfltaccess >= access;
278 mask = ACL_PRIV_LEVEL;
279 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
280 mask |= ACL_ACCESS2PRIV( i );
287 /* be is always non-NULL */
288 /* use global default access if no global acls */
289 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
290 Debug( LDAP_DEBUG_ACL,
291 "=> access_allowed: global default %s access %s to \"%s\"\n",
292 access2str( access ),
293 frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
294 ret = frontendDB->be_dfltaccess >= access;
299 mask = ACL_PRIV_LEVEL;
300 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
301 mask |= ACL_ACCESS2PRIV( i );
313 assert( state->as_vd_acl != NULL );
315 a = state->as_vd_acl;
316 count = state->as_vd_acl_count;
317 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
318 mask = state->as_vd_acl_mask;
319 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
324 if ( state ) state->as_vi_acl = NULL;
328 memset(matches, '\0', sizeof(matches));
331 while((a = acl_get( a, &count, op, e, desc, val,
332 MAXREMATCHES, matches, state )) != NULL)
336 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
337 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
338 (int)matches[i].rm_so, (int)matches[i].rm_eo );
339 if( matches[i].rm_so <= matches[0].rm_eo ) {
341 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
342 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
345 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
349 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
350 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
351 ret = state->as_result;
354 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
359 control = acl_mask( a, &mask, op,
360 e, desc, val, matches, count, state );
362 if ( control != ACL_BREAK ) {
366 memset(matches, '\0', sizeof(matches));
369 if ( ACL_IS_INVALID( mask ) ) {
370 Debug( LDAP_DEBUG_ACL,
371 "=> access_allowed: \"%s\" (%s) invalid!\n",
375 } else if ( control == ACL_BREAK ) {
376 Debug( LDAP_DEBUG_ACL,
377 "=> access_allowed: no more rules\n", 0, 0, 0);
382 Debug( LDAP_DEBUG_ACL,
383 "=> access_allowed: %s access %s by %s\n",
384 access2str( access ),
385 ACL_GRANT(mask, access) ? "granted" : "denied",
386 accessmask2str( mask, accessmaskbuf ) );
388 ret = ACL_GRANT(mask, access);
391 if( state != NULL ) {
392 /* If not value-dependent, save ACL in case of more attrs */
393 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
394 state->as_vi_acl = a;
395 state->as_result = ret;
397 state->as_recorded |= ACL_STATE_RECORDED;
399 if (be_null) op->o_bd = NULL;
400 if ( maskp ) *maskp = mask;
406 * acl_get - return the acl applicable to entry e, attribute
407 * attr. the acl returned is suitable for use in subsequent calls to
408 * acl_access_allowed().
411 static AccessControl *
417 AttributeDescription *desc,
421 AccessControlState *state )
428 assert( count != NULL );
429 assert( desc != NULL );
431 attr = desc->ad_cname.bv_val;
433 assert( attr != NULL );
436 if( op->o_bd == NULL ) {
437 a = frontendDB->be_acl;
439 a = op->o_bd->be_acl;
450 dnlen = e->e_nname.bv_len;
452 for ( ; a != NULL; a = a->acl_next ) {
455 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
456 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
457 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
458 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
459 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
463 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
464 *count, a->acl_dn_pat.bv_val, 0 );
465 patlen = a->acl_dn_pat.bv_len;
466 if ( dnlen < patlen )
469 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
470 /* base dn -- entire object DN must match */
471 if ( dnlen != patlen )
474 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
475 int rdnlen = -1, sep = 0;
477 if ( dnlen <= patlen )
481 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
486 rdnlen = dn_rdnlen( NULL, &e->e_nname );
487 if ( rdnlen != dnlen - patlen - sep )
490 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
491 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
494 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
495 if ( dnlen <= patlen )
497 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
501 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
505 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
509 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
510 matches[0].rm_so = matches[0].rm_eo = -1;
514 /* Is this ACL only for a specific value? */
515 if ( a->acl_attrval.bv_len ) {
520 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
521 state->as_recorded |= ACL_STATE_RECORDED_VD;
522 state->as_vd_acl = prev;
523 state->as_vd_acl_count = *count;
524 state->as_vd_access = a->acl_access;
525 state->as_vd_access_count = 1;
526 ACL_INVALIDATE( state->as_vd_acl_mask );
529 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
530 Debug( LDAP_DEBUG_ACL,
531 "acl_get: valpat %s\n",
532 a->acl_attrval.bv_val, 0, 0 );
533 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
538 Debug( LDAP_DEBUG_ACL,
540 a->acl_attrval.bv_val, 0, 0 );
542 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
543 if (value_match( &match, desc,
544 desc->ad_type->sat_equality, 0,
545 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
552 patlen = a->acl_attrval.bv_len;
553 vdnlen = val->bv_len;
555 if ( vdnlen < patlen )
558 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
559 if ( vdnlen > patlen )
562 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
565 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
568 rdnlen = dn_rdnlen( NULL, val );
569 if ( rdnlen != vdnlen - patlen - 1 )
572 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
573 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
576 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
577 if ( vdnlen <= patlen )
580 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
584 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
590 if ( a->acl_filter != NULL ) {
591 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
592 if ( rc != LDAP_COMPARE_TRUE ) {
597 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
602 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
607 * Record value-dependent access control state
609 #define ACL_RECORD_VALUE_STATE do { \
610 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
611 state->as_recorded |= ACL_STATE_RECORDED_VD; \
612 state->as_vd_acl = a; \
613 AC_MEMCPY( state->as_vd_acl_matches, matches, \
614 sizeof( state->as_vd_acl_matches )) ; \
615 state->as_vd_acl_count = count; \
616 state->as_vd_access = b; \
617 state->as_vd_access_count = i; \
622 * acl_mask - modifies mask based upon the given acl and the
623 * requested access to entry e, attribute attr, value val. if val
624 * is null, access to the whole attribute is assumed (all values).
626 * returns 0 access NOT allowed
630 static slap_control_t
636 AttributeDescription *desc,
640 AccessControlState *state )
642 int i, odnlen, patlen;
645 char accessmaskbuf[ACCESSMASK_MAXLEN];
646 char accessmaskbuf1[ACCESSMASK_MAXLEN];
651 assert( mask != NULL );
652 assert( desc != NULL );
654 attr = desc->ad_cname.bv_val;
656 assert( attr != NULL );
658 Debug( LDAP_DEBUG_ACL,
659 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
662 Debug( LDAP_DEBUG_ACL,
663 "=> acl_mask: to %s by \"%s\", (%s) \n",
664 val ? "value" : "all values",
665 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
666 accessmask2str( *mask, accessmaskbuf ) );
669 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
670 && state->as_vd_acl == a )
672 b = state->as_vd_access;
673 i = state->as_vd_access_count;
680 for ( ; b != NULL; b = b->a_next, i++ ) {
681 slap_mask_t oldmask, modmask;
683 ACL_INVALIDATE( modmask );
685 /* AND <who> clauses */
686 if ( b->a_dn_pat.bv_len != 0 ) {
687 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
688 b->a_dn_pat.bv_val, 0, 0);
690 * if access applies to the entry itself, and the
691 * user is bound as somebody in the same namespace as
692 * the entry, OR the given dn matches the dn pattern
694 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
695 if ( op->o_ndn.bv_len != 0 ) {
699 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
700 if ( op->o_ndn.bv_len == 0 ) {
704 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
705 if ( op->o_ndn.bv_len == 0 ) {
709 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
713 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
714 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
715 int ret = regex_matches( &b->a_dn_pat,
716 op->o_ndn.bv_val, e->e_ndn, matches );
727 if ( e->e_dn == NULL )
730 if ( b->a_dn_expand ) {
732 char buf[ACL_BUF_SIZE];
734 bv.bv_len = sizeof( buf ) - 1;
737 if ( string_expand(&bv, &b->a_dn_pat,
742 if ( dnNormalize(0, NULL, NULL, &bv,
743 &pat, op->o_tmpmemctx )
746 /* did not expand to a valid dn */
754 odnlen = op->o_ndn.bv_len;
755 if ( odnlen < patlen ) {
756 goto dn_match_cleanup;
760 if ( b->a_dn_style == ACL_STYLE_BASE ) {
761 /* base dn -- entire object DN must match */
762 if ( odnlen != patlen ) {
763 goto dn_match_cleanup;
766 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
769 if ( odnlen <= patlen ) {
770 goto dn_match_cleanup;
773 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
774 goto dn_match_cleanup;
777 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
778 if ( rdnlen != odnlen - patlen - 1 ) {
779 goto dn_match_cleanup;
782 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
783 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
784 goto dn_match_cleanup;
787 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
788 if ( odnlen <= patlen ) {
789 goto dn_match_cleanup;
792 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
793 goto dn_match_cleanup;
797 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
800 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
810 if ( b->a_sockurl_pat.bv_len ) {
811 if ( ! op->o_conn->c_listener ) {
814 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
815 b->a_sockurl_pat.bv_val, 0, 0 );
817 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
818 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
819 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
820 e->e_ndn, matches ) )
825 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
827 char buf[ACL_BUF_SIZE];
829 bv.bv_len = sizeof( buf ) - 1;
831 if ( string_expand( &bv, &b->a_sockurl_pat,
832 e->e_ndn, matches ) )
837 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
843 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
851 if ( b->a_domain_pat.bv_len ) {
852 if ( !op->o_conn->c_peer_domain.bv_val ) {
855 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
856 b->a_domain_pat.bv_val, 0, 0 );
857 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
858 if ( b->a_domain_style == ACL_STYLE_REGEX) {
859 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
860 e->e_ndn, matches ) )
865 char buf[ACL_BUF_SIZE];
867 struct berval cmp = op->o_conn->c_peer_domain;
868 struct berval pat = b->a_domain_pat;
870 if ( b->a_domain_expand ) {
873 bv.bv_len = sizeof(buf) - 1;
876 if ( string_expand(&bv, &b->a_domain_pat,
884 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
885 int offset = cmp.bv_len - pat.bv_len;
890 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
894 /* trim the domain */
895 cmp.bv_val = &cmp.bv_val[ offset ];
896 cmp.bv_len -= offset;
899 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
906 if ( b->a_peername_pat.bv_len ) {
907 if ( !op->o_conn->c_peer_name.bv_val ) {
910 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
911 b->a_peername_pat.bv_val, 0, 0 );
912 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
913 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
914 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
915 e->e_ndn, matches ) )
921 /* try exact match */
922 if ( b->a_peername_style == ACL_STYLE_BASE ) {
923 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
927 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
929 char buf[ACL_BUF_SIZE];
931 bv.bv_len = sizeof( buf ) - 1;
933 if ( string_expand( &bv, &b->a_peername_pat,
934 e->e_ndn, matches ) )
939 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
943 /* extract IP and try exact match */
944 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
946 char buf[] = "255.255.255.255";
949 int port_number = -1;
951 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
952 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
955 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
956 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
958 port = strrchr( ip.bv_val, ':' );
962 ip.bv_len = port - ip.bv_val;
964 port_number = strtol( port, &next, 10 );
965 if ( next[0] != '\0' )
969 /* the port check can be anticipated here */
970 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
973 /* address longer than expected? */
974 if ( ip.bv_len >= sizeof(buf) )
977 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
978 buf[ ip.bv_len ] = '\0';
980 addr = inet_addr( buf );
982 /* unable to convert? */
983 if ( addr == (unsigned long)(-1) )
986 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
990 /* extract path and try exact match */
991 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
994 if ( strncmp( op->o_conn->c_peer_name.bv_val,
995 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
998 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
999 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1001 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1004 #endif /* LDAP_PF_LOCAL */
1006 /* exact match (very unlikely...) */
1007 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1014 if ( b->a_sockname_pat.bv_len ) {
1015 if ( !op->o_conn->c_sock_name.bv_val ) {
1018 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1019 b->a_sockname_pat.bv_val, 0, 0 );
1020 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1021 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1022 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1023 e->e_ndn, matches ) )
1028 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1030 char buf[ACL_BUF_SIZE];
1032 bv.bv_len = sizeof( buf ) - 1;
1034 if ( string_expand( &bv, &b->a_sockname_pat,
1035 e->e_ndn, matches ) )
1040 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1045 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1052 if ( b->a_dn_at != NULL ) {
1057 const char *attr = b->a_dn_at->ad_cname.bv_val;
1059 assert( attr != NULL );
1061 if ( op->o_ndn.bv_len == 0 ) {
1065 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1069 /* see if asker is listed in dnattr */
1070 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1072 at = attrs_find( at->a_next, b->a_dn_at ) )
1074 if( value_find_ex( b->a_dn_at,
1075 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1076 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1078 &bv, op->o_tmpmemctx ) == 0 )
1087 /* have a dnattr match. if this is a self clause then
1088 * the target must also match the op dn.
1090 if ( b->a_dn_self ) {
1091 /* check if the target is an attribute. */
1092 if ( val == NULL ) continue;
1094 /* target is attribute, check if the attribute value
1097 rc = value_match( &match, b->a_dn_at,
1098 b->a_dn_at->ad_type->sat_equality, 0,
1100 /* on match error or no match, fail the ACL clause */
1101 if (rc != LDAP_SUCCESS || match != 0 )
1105 /* no dnattr match, check if this is a self clause */
1106 if ( ! b->a_dn_self )
1109 ACL_RECORD_VALUE_STATE;
1111 /* this is a self clause, check if the target is an
1117 /* target is attribute, check if the attribute value
1120 rc = value_match( &match, b->a_dn_at,
1121 b->a_dn_at->ad_type->sat_equality, 0,
1124 /* on match error or no match, fail the ACL clause */
1125 if (rc != LDAP_SUCCESS || match != 0 )
1130 if ( b->a_group_pat.bv_len ) {
1132 struct berval ndn = BER_BVNULL;
1135 if ( op->o_ndn.bv_len == 0 ) {
1139 /* b->a_group is an unexpanded entry name, expanded it should be an
1140 * entry with objectclass group* and we test to see if odn is one of
1141 * the values in the attribute group
1143 /* see if asker is listed in dnattr */
1144 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1145 char buf[ACL_BUF_SIZE];
1146 bv.bv_len = sizeof(buf) - 1;
1149 if ( string_expand( &bv, &b->a_group_pat,
1150 e->e_ndn, matches ) )
1154 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1155 /* did not expand to a valid dn */
1162 bv = b->a_group_pat;
1165 rc = backend_group( op, e, &bv, &op->o_ndn,
1166 b->a_group_oc, b->a_group_at );
1168 if ( ndn.bv_val ) free( ndn.bv_val );
1175 if ( b->a_set_pat.bv_len != 0 ) {
1177 char buf[ACL_BUF_SIZE];
1178 if( b->a_set_style == ACL_STYLE_REGEX ){
1179 bv.bv_len = sizeof(buf) - 1;
1181 if ( string_expand( &bv, &b->a_set_pat,
1182 e->e_ndn, matches ) )
1189 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1194 if ( b->a_authz.sai_ssf ) {
1195 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1196 b->a_authz.sai_ssf, op->o_ssf, 0 );
1197 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1202 if ( b->a_authz.sai_transport_ssf ) {
1203 Debug( LDAP_DEBUG_ACL,
1204 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1205 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1206 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1211 if ( b->a_authz.sai_tls_ssf ) {
1212 Debug( LDAP_DEBUG_ACL,
1213 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1214 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1215 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1220 if ( b->a_authz.sai_sasl_ssf ) {
1221 Debug( LDAP_DEBUG_ACL,
1222 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1223 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1224 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1229 #ifdef SLAPD_ACI_ENABLED
1230 if ( b->a_aci_at != NULL ) {
1232 slap_access_t grant, deny, tgrant, tdeny;
1233 struct berval parent_ndn, old_parent_ndn;
1234 BerVarray bvals = NULL;
1237 /* this case works different from the others above.
1238 * since aci's themselves give permissions, we need
1239 * to first check b->a_access_mask, the ACL's access level.
1242 if ( e->e_nname.bv_len == 0 ) {
1243 /* no ACIs in the root DSE */
1247 /* first check if the right being requested
1248 * is allowed by the ACL clause.
1250 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1253 /* start out with nothing granted, nothing denied */
1257 /* get the aci attribute */
1258 at = attr_find( e->e_attrs, b->a_aci_at );
1261 /* FIXME: this breaks acl caching;
1262 * see also ACL_RECORD_VALUE_STATE below */
1263 ACL_RECORD_VALUE_STATE;
1265 /* the aci is an multi-valued attribute. The
1266 * rights are determined by OR'ing the individual
1267 * rights given by the acis.
1269 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1273 matches, &grant, &deny, &aci_bv_entry ) != 0)
1279 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1280 accessmask2str(tgrant,accessmaskbuf),
1281 accessmask2str(tdeny, accessmaskbuf1), 0);
1284 /* If the entry level aci didn't contain anything valid for the
1285 * current operation, climb up the tree and evaluate the
1286 * acis with scope set to subtree
1288 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1289 dnParent(&(e->e_nname), &parent_ndn);
1290 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1291 old_parent_ndn = parent_ndn;
1292 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1293 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1301 for( i = 0; bvals[i].bv_val != NULL; i++){
1303 /* FIXME: this breaks acl caching;
1304 * see also ACL_RECORD_VALUE_STATE above */
1305 ACL_RECORD_VALUE_STATE;
1307 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1308 &grant, &deny, &aci_bv_children) != 0) {
1311 /* evaluation stops as soon as either a "deny" or a
1312 * "grant" directive matches.
1314 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1318 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1319 accessmask2str(tgrant,accessmaskbuf),
1320 accessmask2str(tdeny, accessmaskbuf1), 0);
1324 case LDAP_NO_SUCH_ATTRIBUTE:
1325 /* just go on if the aci-Attribute is not present in
1328 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1332 case LDAP_NO_SUCH_OBJECT:
1333 /* We have reached the base object */
1334 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1345 dnParent(&old_parent_ndn, &parent_ndn);
1350 /* remove anything that the ACL clause does not allow */
1351 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1352 tdeny &= ACL_PRIV_MASK;
1354 /* see if we have anything to contribute */
1355 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1359 /* this could be improved by changing acl_mask so that it can deal with
1360 * by clauses that return grant/deny pairs. Right now, it does either
1361 * additive or subtractive rights, but not both at the same time. So,
1362 * we need to combine the grant/deny pair into a single rights mask in
1363 * a smart way: if either grant or deny is "empty", then we use the
1364 * opposite as is, otherwise we remove any denied rights from the grant
1365 * rights mask and construct an additive mask.
1367 if (ACL_IS_INVALID(tdeny)) {
1368 modmask = tgrant | ACL_PRIV_ADDITIVE;
1370 } else if (ACL_IS_INVALID(tgrant)) {
1371 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1374 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1380 modmask = b->a_access_mask;
1383 Debug( LDAP_DEBUG_ACL,
1384 "<= acl_mask: [%d] applying %s (%s)\n",
1385 i, accessmask2str( modmask, accessmaskbuf ),
1386 b->a_type == ACL_CONTINUE
1388 : b->a_type == ACL_BREAK
1394 if( ACL_IS_ADDITIVE(modmask) ) {
1396 ACL_PRIV_SET( *mask, modmask );
1399 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1401 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1402 /* substract privs */
1403 ACL_PRIV_CLR( *mask, modmask );
1406 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1413 Debug( LDAP_DEBUG_ACL,
1414 "<= acl_mask: [%d] mask: %s\n",
1415 i, accessmask2str(*mask, accessmaskbuf), 0 );
1417 if( b->a_type == ACL_CONTINUE ) {
1420 } else if ( b->a_type == ACL_BREAK ) {
1428 /* implicit "by * none" clause */
1431 Debug( LDAP_DEBUG_ACL,
1432 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1433 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1438 * acl_check_modlist - check access control on the given entry to see if
1439 * it allows the given modifications by the user associated with op.
1440 * returns 1 if mods allowed ok
1441 * 0 mods not allowed
1448 Modifications *mlist
1452 AccessControlState state = ACL_STATE_INIT;
1455 int ret = 1; /* default is access allowed */
1463 assert( be != NULL );
1465 /* short circuit root database access */
1466 if ( be_isroot( op ) ) {
1467 Debug( LDAP_DEBUG_ACL,
1468 "<= acl_access_allowed: granted to database root\n",
1473 /* use backend default access if no backend acls */
1474 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1475 Debug( LDAP_DEBUG_ACL,
1476 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1477 access2str( ACL_WRITE ),
1478 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1479 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1483 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1485 * no-user-modification operational attributes are ignored
1486 * by ACL_WRITE checking as any found here are not provided
1489 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1490 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1491 " modify access granted\n",
1492 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1496 switch ( mlist->sml_op ) {
1497 case LDAP_MOD_REPLACE:
1499 * We must check both permission to delete the whole
1500 * attribute and permission to add the specific attributes.
1501 * This prevents abuse from selfwriters.
1503 if ( ! access_allowed( op, e,
1504 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1510 if ( mlist->sml_values == NULL ) break;
1512 /* fall thru to check value to add */
1515 assert( mlist->sml_values != NULL );
1517 for ( bv = mlist->sml_nvalues
1518 ? mlist->sml_nvalues : mlist->sml_values;
1519 bv->bv_val != NULL; bv++ )
1521 if ( ! access_allowed( op, e,
1522 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1530 case LDAP_MOD_DELETE:
1531 if ( mlist->sml_values == NULL ) {
1532 if ( ! access_allowed( op, e,
1533 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1540 for ( bv = mlist->sml_nvalues
1541 ? mlist->sml_nvalues : mlist->sml_values;
1542 bv->bv_val != NULL; bv++ )
1544 if ( ! access_allowed( op, e,
1545 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1553 case SLAP_MOD_SOFTADD:
1554 /* allow adding attribute via modrdn thru */
1566 if (be_null) op->o_bd = NULL;
1572 struct berval *list,
1585 while (len >= 0 && --ix >= 0) {
1586 while (--len >= 0 && *p++ != sep) ;
1588 while (len >= 0 && *p == ' ') {
1599 while (--len >= 0 && *p != sep) {
1603 while (bv->bv_len > 0 && *--p == ' ')
1609 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1611 AciSetCookie *cp = (AciSetCookie *)cookie;
1612 BerVarray bvals = NULL;
1615 /* this routine needs to return the bervals instead of
1616 * plain strings, since syntax is not known. It should
1617 * also return the syntax or some "comparison cookie".
1620 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1622 AttributeDescription *desc = NULL;
1623 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1624 backend_attribute(cp->op,
1625 cp->e, &ndn, desc, &bvals, ACL_NONE);
1627 slap_sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1634 struct berval *subj,
1640 struct berval set = BER_BVNULL;
1642 AciSetCookie cookie;
1645 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1647 struct berval subjdn, ndn = BER_BVNULL;
1648 struct berval setat;
1651 AttributeDescription *desc = NULL;
1653 /* format of string is "entry/setAttrName" */
1654 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
1658 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
1659 setat = aci_bv_set_attr;
1663 * NOTE: dnNormalize honors the ber_len field
1664 * as the length of the dn to be normalized
1666 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
1667 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
1669 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
1670 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
1674 BER_BVZERO( &bvals[0] );
1675 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
1677 bvals[0].bv_val = bvals[i-1].bv_val;
1678 BER_BVZERO( &bvals[i-1] );
1680 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
1681 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1686 if ( !BER_BVISNULL( &set ) ) {
1689 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
1690 &op->o_ndn, &e->e_nname, NULL ) > 0 );
1691 slap_sl_free( set.bv_val, op->o_tmpmemctx );
1697 #ifdef SLAPD_ACI_ENABLED
1699 aci_list_map_rights(
1700 struct berval *list )
1707 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1710 switch (*bv.bv_val) {
1712 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1715 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1716 * the right 's' to mean "set", but in the examples states
1717 * that the right 's' means "search". The latter definition
1720 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1723 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1726 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1729 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1730 * define any equivalent to the AUTH right, so I've just used
1733 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1745 struct berval *list,
1746 const struct berval *attr,
1747 struct berval *val )
1749 struct berval bv, left, right;
1752 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1753 if (aci_get_part(&bv, 0, '=', &left) < 0
1754 || aci_get_part(&bv, 1, '=', &right) < 0)
1756 if (ber_bvstrcasecmp(attr, &bv) == 0)
1758 } else if (val == NULL) {
1759 if (ber_bvstrcasecmp(attr, &left) == 0)
1762 if (ber_bvstrcasecmp(attr, &left) == 0) {
1763 /* this is experimental code that implements a
1764 * simple (prefix) match of the attribute value.
1765 * the ACI draft does not provide for aci's that
1766 * apply to specific values, but it would be
1767 * nice to have. If the <attr> part of an aci's
1768 * rights list is of the form <attr>=<value>,
1769 * that means the aci applies only to attrs with
1770 * the given value. Furthermore, if the attr is
1771 * of the form <attr>=<value>*, then <value> is
1772 * treated as a prefix, and the aci applies to
1773 * any value with that prefix.
1775 * Ideally, this would allow r.e. matches.
1777 if (aci_get_part(&right, 0, '*', &left) < 0
1778 || right.bv_len <= left.bv_len)
1780 if (ber_bvstrcasecmp(val, &right) == 0)
1782 } else if (val->bv_len >= left.bv_len) {
1783 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1792 static slap_access_t
1793 aci_list_get_attr_rights(
1794 struct berval *list,
1795 const struct berval *attr,
1796 struct berval *val )
1802 /* loop through each rights/attr pair, skip first part (action) */
1804 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1805 if (aci_list_has_attr(&bv, attr, val) == 0)
1807 if (aci_get_part(list, i, ';', &bv) < 0)
1809 mask |= aci_list_map_rights(&bv);
1815 aci_list_get_rights(
1816 struct berval *list,
1817 const struct berval *attr,
1819 slap_access_t *grant,
1820 slap_access_t *deny )
1822 struct berval perm, actn;
1823 slap_access_t *mask;
1826 if (attr == NULL || attr->bv_len == 0
1827 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1828 attr = &aci_bv_br_entry;
1834 /* loop through each permissions clause */
1835 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1836 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1838 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1840 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1847 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1848 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1855 struct berval *subj,
1856 struct berval *defgrpoc,
1857 struct berval *defgrpat,
1863 struct berval subjdn;
1864 struct berval grpoc;
1865 struct berval grpat;
1866 ObjectClass *grp_oc = NULL;
1867 AttributeDescription *grp_ad = NULL;
1871 /* format of string is "group/objectClassValue/groupAttrName" */
1872 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1876 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1880 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1884 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1885 if( rc != LDAP_SUCCESS ) {
1891 grp_oc = oc_bvfind( &grpoc );
1893 if (grp_oc != NULL && grp_ad != NULL ) {
1894 char buf[ACL_BUF_SIZE];
1895 struct berval bv, ndn;
1896 bv.bv_len = sizeof( buf ) - 1;
1897 bv.bv_val = (char *)&buf;
1898 if ( string_expand(&bv, &subjdn,
1899 e->e_ndn, matches) )
1904 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1905 rc = (backend_group(op, e, &ndn, &op->o_ndn,
1906 grp_oc, grp_ad) == 0);
1919 AttributeDescription *desc,
1922 regmatch_t *matches,
1923 slap_access_t *grant,
1924 slap_access_t *deny,
1925 struct berval *scope
1928 struct berval bv, perms, sdn;
1932 assert( desc->ad_cname.bv_val != NULL );
1934 /* parse an aci of the form:
1935 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1937 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1938 a full description of the format for this attribute.
1939 Differences: "this" in the draft is "self" here, and
1940 "self" and "public" is in the position of dnType.
1942 For now, this routine only supports scope=entry.
1944 /* check that the aci has all 5 components */
1945 if (aci_get_part(aci, 4, '#', NULL) < 0)
1948 /* check that the aci family is supported */
1949 if (aci_get_part(aci, 0, '#', &bv) < 0)
1952 /* check that the scope matches */
1953 if (aci_get_part(aci, 1, '#', &bv) < 0
1954 || ber_bvstrcasecmp( scope, &bv ) != 0)
1959 /* get the list of permissions clauses, bail if empty */
1960 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1963 /* check if any permissions allow desired access */
1964 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1967 /* see if we have a DN match */
1968 if (aci_get_part(aci, 3, '#', &bv) < 0)
1971 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1974 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1977 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1978 if (dn_match( &op->o_ndn, &ndn))
1984 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1987 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1988 if (dn_match(&op->o_ndn, &e->e_nname))
1991 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1993 AttributeDescription *ad = NULL;
1996 rc = slap_bv2ad( &sdn, &ad, &text );
1998 if( rc != LDAP_SUCCESS ) {
2006 for(at = attrs_find( e->e_attrs, ad );
2008 at = attrs_find( at->a_next, ad ) )
2010 if (value_find_ex( ad,
2011 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2012 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2014 &bv, op->o_tmpmemctx) == 0 )
2024 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2025 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2028 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2029 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2032 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2033 if (aci_match_set(&sdn, op, e, 0))
2036 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2037 if (aci_match_set(&sdn, op, e, 1))
2045 #endif /* SLAPD_ACI_ENABLED */
2052 regmatch_t *matches)
2060 bv->bv_val[0] = '\0';
2061 bv->bv_len--; /* leave space for lone $ */
2064 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2065 sp < pat->bv_val + pat->bv_len ; sp++ )
2067 /* did we previously see a $ */
2069 if ( flag == 1 && *sp == '$' ) {
2074 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2077 } else if ( *sp >= '0' && *sp <= '9' ) {
2085 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2086 if ( *sp >= '0' && *sp <= '9' ) {
2087 n = 10*n + ( *sp - '0' );
2091 if ( *sp != /*'{'*/ '}' ) {
2097 if ( n >= MAXREMATCHES ) {
2103 i = matches[n].rm_so;
2104 l = matches[n].rm_eo;
2105 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2123 /* must have ended with a single $ */
2131 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2132 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2139 struct berval *pat, /* pattern to expand and match against */
2140 char *str, /* string to match against pattern */
2141 char *buf, /* buffer with $N expansion variables */
2142 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2146 char newbuf[ACL_BUF_SIZE];
2150 bv.bv_len = sizeof(newbuf) - 1;
2153 if(str == NULL) str = "";
2155 string_expand(&bv, pat, buf, matches);
2156 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2157 char error[ACL_BUF_SIZE];
2158 regerror(rc, &re, error, sizeof(error));
2160 Debug( LDAP_DEBUG_TRACE,
2161 "compile( \"%s\", \"%s\") failed %s\n",
2162 pat->bv_val, str, error );
2166 rc = regexec(&re, str, 0, NULL, 0);
2169 Debug( LDAP_DEBUG_TRACE,
2170 "=> regex_matches: string: %s\n", str, 0, 0 );
2171 Debug( LDAP_DEBUG_TRACE,
2172 "=> regex_matches: rc: %d %s\n",
2173 rc, !rc ? "matches" : "no matches", 0 );