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-2005 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>
41 #include "slapi/slapi.h"
42 #endif /* LDAPI_SLAPI */
44 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
50 aci_bv_entry = BER_BVC("entry"),
51 aci_bv_children = BER_BVC("children"),
52 aci_bv_onelevel = BER_BVC("onelevel"),
53 aci_bv_subtree = BER_BVC("subtree"),
54 aci_bv_br_entry = BER_BVC("[entry]"),
55 aci_bv_br_all = BER_BVC("[all]"),
56 aci_bv_access_id = BER_BVC("access-id"),
57 aci_bv_anonymous = BER_BVC("anonymous"),
58 aci_bv_public = BER_BVC("public"),
59 aci_bv_users = BER_BVC("users"),
60 aci_bv_self = BER_BVC("self"),
61 aci_bv_dnattr = BER_BVC("dnattr"),
62 aci_bv_group = BER_BVC("group"),
63 aci_bv_role = BER_BVC("role"),
64 aci_bv_set = BER_BVC("set"),
65 aci_bv_set_ref = BER_BVC("set-ref"),
66 aci_bv_grant = BER_BVC("grant"),
67 aci_bv_deny = BER_BVC("deny"),
69 aci_bv_ip_eq = BER_BVC("IP="),
71 aci_bv_path_eq = BER_BVC("PATH="),
72 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
73 #endif /* LDAP_PF_LOCAL */
75 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
76 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
77 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
78 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
79 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
81 typedef enum slap_aci_scope_t {
82 SLAP_ACI_SCOPE_ENTRY = 0x1,
83 SLAP_ACI_SCOPE_CHILDREN = 0x2,
84 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
87 static AccessControl * acl_get(
88 AccessControl *ac, int *count,
89 Operation *op, Entry *e,
90 AttributeDescription *desc,
92 int nmatch, regmatch_t *matches,
93 AccessControlState *state );
95 static slap_control_t acl_mask(
96 AccessControl *ac, slap_mask_t *mask,
97 Operation *op, Entry *e,
98 AttributeDescription *desc,
103 AccessControlState *state );
105 #ifdef SLAPD_ACI_ENABLED
107 Operation *op, Entry *e,
108 AttributeDescription *desc,
113 slap_access_t *grant,
115 slap_aci_scope_t scope);
118 static int regex_matches(
119 struct berval *pat, char *str, char *buf,
120 int nmatch, regmatch_t *matches);
121 static int string_expand(
122 struct berval *newbuf, struct berval *pattern,
123 char *match, int nmatch, regmatch_t *matches);
125 typedef struct AciSetCookie {
130 SLAP_SET_GATHER aci_set_gather;
131 SLAP_SET_GATHER aci_set_gather2;
132 static int aci_match_set ( struct berval *subj, Operation *op,
133 Entry *e, int setref );
136 * access_allowed - check whether op->o_ndn is allowed the requested access
137 * to entry e, attribute attr, value val. if val is null, access to
138 * the whole attribute is assumed (all values).
140 * This routine loops through all access controls and calls
141 * acl_mask() on each applicable access control.
142 * The loop exits when a definitive answer is reached or
143 * or no more controls remain.
150 * - can be legally called with op == NULL
151 * - can be legally called with op->o_bd == NULL
158 AttributeDescription *desc,
160 slap_access_t access,
161 AccessControlState *state,
166 AccessControl *a = NULL;
171 char accessmaskbuf[ACCESSMASK_MAXLEN];
174 slap_control_t control;
176 regmatch_t matches[MAXREMATCHES];
177 int st_same_attr = 0;
178 static AccessControlState state_init = ACL_STATE_INIT;
181 assert( desc != NULL );
182 assert( access > ACL_NONE );
183 if ( maskp ) ACL_INVALIDATE( *maskp );
185 attr = desc->ad_cname.bv_val;
187 assert( attr != NULL );
189 if( op && op->o_is_auth_check &&
190 ( access == ACL_SEARCH || access == ACL_READ ))
196 if ( state->as_vd_ad==desc) {
197 if ( state->as_recorded ) {
198 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
201 return state->as_result;
202 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
203 val != NULL && state->as_vd_acl == NULL )
205 return state->as_result;
213 state->as_vd_ad=desc;
216 Debug( LDAP_DEBUG_ACL,
217 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
218 access2str( access ), e->e_dn, attr );
227 be = LDAP_STAILQ_FIRST(&backendDB);
231 * FIXME: experimental; use first backend rules
232 * iff there is no global_acl (ITS#3100) */
233 if ( frontendDB->be_acl == NULL )
239 assert( be != NULL );
242 if ( op->o_pb != NULL ) {
243 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
245 /* ACL plugin denied access */
249 #endif /* LDAP_SLAPI */
251 /* grant database root access */
252 if ( /* be != NULL && */ be_isroot( op ) ) {
253 Debug( LDAP_DEBUG_ACL,
254 "<= root access granted\n",
257 mask = ACL_LVL_MANAGE;
264 * no-user-modification operational attributes are ignored
265 * by ACL_WRITE checking as any found here are not provided
268 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
269 && desc != slap_schema.si_ad_entry
270 && desc != slap_schema.si_ad_children )
272 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
273 " %s access granted\n",
278 /* use backend default access if no backend acls */
279 if( be != NULL && be->be_acl == NULL ) {
280 Debug( LDAP_DEBUG_ACL,
281 "=> access_allowed: backend default %s access %s to \"%s\"\n",
282 access2str( access ),
283 be->be_dfltaccess >= access ? "granted" : "denied",
284 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
285 ret = be->be_dfltaccess >= access;
290 mask = ACL_PRIV_LEVEL;
291 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
292 mask |= ACL_ACCESS2PRIV( i );
299 /* be is always non-NULL */
300 /* use global default access if no global acls */
301 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
302 Debug( LDAP_DEBUG_ACL,
303 "=> access_allowed: global default %s access %s to \"%s\"\n",
304 access2str( access ),
305 frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
306 ret = frontendDB->be_dfltaccess >= access;
311 mask = ACL_PRIV_LEVEL;
312 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
313 mask |= ACL_ACCESS2PRIV( i );
325 assert( state->as_vd_acl != NULL );
327 a = state->as_vd_acl;
328 count = state->as_vd_acl_count;
329 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
330 mask = state->as_vd_acl_mask;
331 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
336 if ( state ) state->as_vi_acl = NULL;
340 memset(matches, '\0', sizeof(matches));
343 while((a = acl_get( a, &count, op, e, desc, val,
344 MAXREMATCHES, matches, state )) != NULL)
348 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
349 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
350 (int)matches[i].rm_so, (int)matches[i].rm_eo );
351 if( matches[i].rm_so <= matches[0].rm_eo ) {
353 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
354 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
357 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
361 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
362 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
363 ret = state->as_result;
366 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
371 control = acl_mask( a, &mask, op,
372 e, desc, val, MAXREMATCHES, matches, count, state );
374 if ( control != ACL_BREAK ) {
378 memset(matches, '\0', sizeof(matches));
381 if ( ACL_IS_INVALID( mask ) ) {
382 Debug( LDAP_DEBUG_ACL,
383 "=> access_allowed: \"%s\" (%s) invalid!\n",
387 } else if ( control == ACL_BREAK ) {
388 Debug( LDAP_DEBUG_ACL,
389 "=> access_allowed: no more rules\n", 0, 0, 0);
394 Debug( LDAP_DEBUG_ACL,
395 "=> access_allowed: %s access %s by %s\n",
396 access2str( access ),
397 ACL_GRANT(mask, access) ? "granted" : "denied",
398 accessmask2str( mask, accessmaskbuf, 1 ) );
400 ret = ACL_GRANT(mask, access);
403 if( state != NULL ) {
404 /* If not value-dependent, save ACL in case of more attrs */
405 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
406 state->as_vi_acl = a;
407 state->as_result = ret;
409 state->as_recorded |= ACL_STATE_RECORDED;
411 if (be_null) op->o_bd = NULL;
412 if ( maskp ) *maskp = mask;
418 * acl_get - return the acl applicable to entry e, attribute
419 * attr. the acl returned is suitable for use in subsequent calls to
420 * acl_access_allowed().
423 static AccessControl *
429 AttributeDescription *desc,
433 AccessControlState *state )
440 assert( count != NULL );
441 assert( desc != NULL );
443 attr = desc->ad_cname.bv_val;
445 assert( attr != NULL );
448 if( op->o_bd == NULL ) {
449 a = frontendDB->be_acl;
451 a = op->o_bd->be_acl;
462 dnlen = e->e_nname.bv_len;
464 for ( ; a != NULL; a = a->acl_next ) {
467 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
468 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
469 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
470 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
471 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
475 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
476 *count, a->acl_dn_pat.bv_val, 0 );
477 patlen = a->acl_dn_pat.bv_len;
478 if ( dnlen < patlen )
481 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
482 /* base dn -- entire object DN must match */
483 if ( dnlen != patlen )
486 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
487 int rdnlen = -1, sep = 0;
489 if ( dnlen <= patlen )
493 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
498 rdnlen = dn_rdnlen( NULL, &e->e_nname );
499 if ( rdnlen != dnlen - patlen - sep )
502 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
503 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
506 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
507 if ( dnlen <= patlen )
509 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
513 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
517 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
521 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
522 matches[0].rm_so = matches[0].rm_eo = -1;
526 /* Is this ACL only for a specific value? */
527 if ( a->acl_attrval.bv_len ) {
532 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
533 state->as_recorded |= ACL_STATE_RECORDED_VD;
534 state->as_vd_acl = prev;
535 state->as_vd_acl_count = *count;
536 state->as_vd_access = a->acl_access;
537 state->as_vd_access_count = 1;
538 ACL_INVALIDATE( state->as_vd_acl_mask );
541 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
542 Debug( LDAP_DEBUG_ACL,
543 "acl_get: valpat %s\n",
544 a->acl_attrval.bv_val, 0, 0 );
545 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
553 Debug( LDAP_DEBUG_ACL,
555 a->acl_attrval.bv_val, 0, 0 );
557 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
558 if (value_match( &match, desc,
559 desc->ad_type->sat_equality, 0,
560 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
567 patlen = a->acl_attrval.bv_len;
568 vdnlen = val->bv_len;
570 if ( vdnlen < patlen )
573 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
574 if ( vdnlen > patlen )
577 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
580 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
583 rdnlen = dn_rdnlen( NULL, val );
584 if ( rdnlen != vdnlen - patlen - 1 )
587 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
588 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
591 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
592 if ( vdnlen <= patlen )
595 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
599 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
605 if ( a->acl_filter != NULL ) {
606 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
607 if ( rc != LDAP_COMPARE_TRUE ) {
612 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
617 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
629 struct berval *opndn )
632 * if access applies to the entry itself, and the
633 * user is bound as somebody in the same namespace as
634 * the entry, OR the given dn matches the dn pattern
637 * NOTE: styles "anonymous", "users" and "self"
638 * have been moved to enum slap_style_t, whose
639 * value is set in a_dn_style; however, the string
640 * is maintaned in a_dn_pat.
642 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
643 if ( !BER_BVISEMPTY( opndn ) ) {
647 } else if ( b->a_style == ACL_STYLE_USERS ) {
648 if ( BER_BVISEMPTY( opndn ) ) {
652 } else if ( b->a_style == ACL_STYLE_SELF ) {
653 struct berval ndn, selfndn;
656 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
660 level = b->a_self_level;
668 selfndn = e->e_nname;
671 for ( ; level > 0; level-- ) {
672 if ( BER_BVISEMPTY( &ndn ) ) {
675 dnParent( &ndn, &ndn );
678 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
683 } else if ( b->a_style == ACL_STYLE_REGEX ) {
684 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
686 regmatch_t tmp_matches[2],
687 *tmp_matchesp = tmp_matches;
691 switch ( a->acl_dn_style ) {
692 case ACL_STYLE_REGEX:
693 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
694 tmp_matchesp = matches;
698 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
701 tmp_matches[0].rm_so = 0;
702 tmp_matches[0].rm_eo = e->e_nname.bv_len;
707 case ACL_STYLE_SUBTREE:
708 case ACL_STYLE_CHILDREN:
709 tmp_matches[0].rm_so = 0;
710 tmp_matches[0].rm_eo = e->e_nname.bv_len;
711 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
712 tmp_matches[1].rm_eo = e->e_nname.bv_len;
726 if ( !regex_matches( &b->a_pat, opndn->bv_val,
727 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
735 ber_len_t patlen, odnlen;
738 if ( e->e_dn == NULL )
743 char buf[ACL_BUF_SIZE];
746 regmatch_t tmp_matches[2],
747 *tmp_matchesp = tmp_matches;
751 bv.bv_len = sizeof( buf ) - 1;
754 switch ( a->acl_dn_style ) {
755 case ACL_STYLE_REGEX:
756 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
757 tmp_matchesp = matches;
761 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
764 tmp_matches[0].rm_so = 0;
765 tmp_matches[0].rm_eo = e->e_nname.bv_len;
770 case ACL_STYLE_SUBTREE:
771 case ACL_STYLE_CHILDREN:
772 tmp_matches[0].rm_so = 0;
773 tmp_matches[0].rm_eo = e->e_nname.bv_len;
774 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
775 tmp_matches[1].rm_eo = e->e_nname.bv_len;
789 if ( string_expand( &bv, &b->a_pat,
791 tmp_nmatch, tmp_matchesp ) )
796 if ( dnNormalize(0, NULL, NULL, &bv,
797 &pat, op->o_tmpmemctx )
800 /* did not expand to a valid dn */
809 odnlen = opndn->bv_len;
810 if ( odnlen < patlen ) {
811 goto dn_match_cleanup;
815 if ( b->a_style == ACL_STYLE_BASE ) {
816 /* base dn -- entire object DN must match */
817 if ( odnlen != patlen ) {
818 goto dn_match_cleanup;
821 } else if ( b->a_style == ACL_STYLE_ONE ) {
824 if ( odnlen <= patlen ) {
825 goto dn_match_cleanup;
828 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
829 goto dn_match_cleanup;
832 rdnlen = dn_rdnlen( NULL, opndn );
833 if ( rdnlen != odnlen - patlen - 1 ) {
834 goto dn_match_cleanup;
837 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
838 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
839 goto dn_match_cleanup;
842 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
843 if ( odnlen <= patlen ) {
844 goto dn_match_cleanup;
847 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
848 goto dn_match_cleanup;
851 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
855 if ( odnlen <= patlen ) {
856 goto dn_match_cleanup;
859 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
861 goto dn_match_cleanup;
866 for ( ; level > 0; level-- ) {
867 if ( BER_BVISEMPTY( &ndn ) ) {
868 goto dn_match_cleanup;
870 dnParent( &ndn, &ndn );
871 if ( ndn.bv_len < patlen ) {
872 goto dn_match_cleanup;
876 if ( ndn.bv_len != patlen ) {
877 goto dn_match_cleanup;
881 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
884 if ( pat.bv_val != b->a_pat.bv_val ) {
885 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
897 * Record value-dependent access control state
899 #define ACL_RECORD_VALUE_STATE do { \
900 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
901 state->as_recorded |= ACL_STATE_RECORDED_VD; \
902 state->as_vd_acl = a; \
903 AC_MEMCPY( state->as_vd_acl_matches, matches, \
904 sizeof( state->as_vd_acl_matches )) ; \
905 state->as_vd_acl_count = count; \
906 state->as_vd_access = b; \
907 state->as_vd_access_count = i; \
921 AccessControlState *state,
923 struct berval *opndn )
929 const char *attr = bdn->a_at->ad_cname.bv_val;
931 assert( attr != NULL );
933 if ( BER_BVISEMPTY( opndn ) ) {
937 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
940 /* see if asker is listed in dnattr */
941 for ( at = attrs_find( e->e_attrs, bdn->a_at );
943 at = attrs_find( at->a_next, bdn->a_at ) )
945 if ( value_find_ex( bdn->a_at,
946 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
947 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
949 &bv, op->o_tmpmemctx ) == 0 )
958 /* have a dnattr match. if this is a self clause then
959 * the target must also match the op dn.
962 /* check if the target is an attribute. */
963 if ( val == NULL ) return 1;
965 /* target is attribute, check if the attribute value
968 rc = value_match( &match, bdn->a_at,
969 bdn->a_at->ad_type->sat_equality, 0,
971 /* on match error or no match, fail the ACL clause */
972 if ( rc != LDAP_SUCCESS || match != 0 )
977 /* no dnattr match, check if this is a self clause */
981 ACL_RECORD_VALUE_STATE;
983 /* this is a self clause, check if the target is an
989 /* target is attribute, check if the attribute value
992 rc = value_match( &match, bdn->a_at,
993 bdn->a_at->ad_type->sat_equality, 0,
996 /* on match error or no match, fail the ACL clause */
997 if ( rc != LDAP_SUCCESS || match != 0 )
1006 * acl_mask - modifies mask based upon the given acl and the
1007 * requested access to entry e, attribute attr, value val. if val
1008 * is null, access to the whole attribute is assumed (all values).
1010 * returns 0 access NOT allowed
1014 static slap_control_t
1020 AttributeDescription *desc,
1023 regmatch_t *matches,
1025 AccessControlState *state )
1030 char accessmaskbuf[ACCESSMASK_MAXLEN];
1031 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1032 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1033 #endif /* SLAPD_ACI_ENABLED */
1037 assert( a != NULL );
1038 assert( mask != NULL );
1039 assert( desc != NULL );
1041 attr = desc->ad_cname.bv_val;
1043 assert( attr != NULL );
1045 Debug( LDAP_DEBUG_ACL,
1046 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1049 Debug( LDAP_DEBUG_ACL,
1050 "=> acl_mask: to %s by \"%s\", (%s) \n",
1051 val ? "value" : "all values",
1052 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1053 accessmask2str( *mask, accessmaskbuf, 1) );
1056 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1057 && state->as_vd_acl == a )
1059 b = state->as_vd_access;
1060 i = state->as_vd_access_count;
1067 for ( ; b != NULL; b = b->a_next, i++ ) {
1068 slap_mask_t oldmask, modmask;
1070 ACL_INVALIDATE( modmask );
1072 /* AND <who> clauses */
1073 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1074 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1075 b->a_dn_pat.bv_val, 0, 0);
1077 * if access applies to the entry itself, and the
1078 * user is bound as somebody in the same namespace as
1079 * the entry, OR the given dn matches the dn pattern
1082 * NOTE: styles "anonymous", "users" and "self"
1083 * have been moved to enum slap_style_t, whose
1084 * value is set in a_dn_style; however, the string
1085 * is maintaned in a_dn_pat.
1088 if ( acl_mask_dn( op, e, a, nmatch, matches,
1089 &b->a_dn, &op->o_ndn ) )
1095 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1098 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1099 b->a_realdn_pat.bv_val, 0, 0);
1101 * if access applies to the entry itself, and the
1102 * user is bound as somebody in the same namespace as
1103 * the entry, OR the given dn matches the dn pattern
1106 * NOTE: styles "anonymous", "users" and "self"
1107 * have been moved to enum slap_style_t, whose
1108 * value is set in a_dn_style; however, the string
1109 * is maintaned in a_dn_pat.
1112 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1113 ndn = op->o_conn->c_ndn;
1118 if ( acl_mask_dn( op, e, a, nmatch, matches,
1119 &b->a_realdn, &ndn ) )
1125 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1126 if ( ! op->o_conn->c_listener ) {
1129 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1130 b->a_sockurl_pat.bv_val, 0, 0 );
1132 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1133 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1134 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1135 e->e_ndn, nmatch, matches ) )
1140 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1142 char buf[ACL_BUF_SIZE];
1144 bv.bv_len = sizeof( buf ) - 1;
1146 if ( string_expand( &bv, &b->a_sockurl_pat,
1147 e->e_ndn, nmatch, matches ) )
1152 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1158 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1166 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1167 if ( !op->o_conn->c_peer_domain.bv_val ) {
1170 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1171 b->a_domain_pat.bv_val, 0, 0 );
1172 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1173 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1174 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1175 e->e_ndn, nmatch, matches ) )
1180 char buf[ACL_BUF_SIZE];
1182 struct berval cmp = op->o_conn->c_peer_domain;
1183 struct berval pat = b->a_domain_pat;
1185 if ( b->a_domain_expand ) {
1188 bv.bv_len = sizeof(buf) - 1;
1191 if ( string_expand(&bv, &b->a_domain_pat,
1192 e->e_ndn, nmatch, matches) )
1199 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1200 int offset = cmp.bv_len - pat.bv_len;
1205 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1209 /* trim the domain */
1210 cmp.bv_val = &cmp.bv_val[ offset ];
1211 cmp.bv_len -= offset;
1214 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1221 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1222 if ( !op->o_conn->c_peer_name.bv_val ) {
1225 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1226 b->a_peername_pat.bv_val, 0, 0 );
1227 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1228 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1229 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1230 e->e_ndn, nmatch, matches ) )
1236 /* try exact match */
1237 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1238 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1242 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1244 char buf[ACL_BUF_SIZE];
1246 bv.bv_len = sizeof( buf ) - 1;
1248 if ( string_expand( &bv, &b->a_peername_pat,
1249 e->e_ndn, nmatch, matches ) )
1254 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1258 /* extract IP and try exact match */
1259 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1261 char buf[] = "255.255.255.255";
1264 int port_number = -1;
1266 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1267 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1270 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1271 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1273 port = strrchr( ip.bv_val, ':' );
1277 ip.bv_len = port - ip.bv_val;
1279 port_number = strtol( port, &next, 10 );
1280 if ( next[0] != '\0' )
1284 /* the port check can be anticipated here */
1285 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1288 /* address longer than expected? */
1289 if ( ip.bv_len >= sizeof(buf) )
1292 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1293 buf[ ip.bv_len ] = '\0';
1295 addr = inet_addr( buf );
1297 /* unable to convert? */
1298 if ( addr == (unsigned long)(-1) )
1301 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1304 #ifdef LDAP_PF_LOCAL
1305 /* extract path and try exact match */
1306 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1309 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1310 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1313 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1314 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1316 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1319 #endif /* LDAP_PF_LOCAL */
1321 /* exact match (very unlikely...) */
1322 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1329 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1330 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1333 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1334 b->a_sockname_pat.bv_val, 0, 0 );
1335 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1336 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1337 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1338 e->e_ndn, nmatch, matches ) )
1343 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1345 char buf[ACL_BUF_SIZE];
1347 bv.bv_len = sizeof( buf ) - 1;
1349 if ( string_expand( &bv, &b->a_sockname_pat,
1350 e->e_ndn, nmatch, matches ) )
1355 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1360 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1367 if ( b->a_dn_at != NULL ) {
1368 if ( acl_mask_dnattr( op, e, val, a, b, i,
1369 matches, count, state,
1370 &b->a_dn, &op->o_ndn ) )
1376 if ( b->a_realdn_at != NULL ) {
1379 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1380 ndn = op->o_conn->c_ndn;
1385 if ( acl_mask_dnattr( op, e, val, a, b, i,
1386 matches, count, state,
1387 &b->a_realdn, &ndn ) )
1394 if ( b->a_dn_at != NULL ) {
1399 const char *attr = b->a_dn_at->ad_cname.bv_val;
1401 assert( attr != NULL );
1403 if ( op->o_ndn.bv_len == 0 ) {
1407 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1411 /* see if asker is listed in dnattr */
1412 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1414 at = attrs_find( at->a_next, b->a_dn_at ) )
1416 if( value_find_ex( b->a_dn_at,
1417 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1418 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1420 &bv, op->o_tmpmemctx ) == 0 )
1429 /* have a dnattr match. if this is a self clause then
1430 * the target must also match the op dn.
1432 if ( b->a_dn_self ) {
1433 /* check if the target is an attribute. */
1434 if ( val == NULL ) continue;
1436 /* target is attribute, check if the attribute value
1439 rc = value_match( &match, b->a_dn_at,
1440 b->a_dn_at->ad_type->sat_equality, 0,
1442 /* on match error or no match, fail the ACL clause */
1443 if (rc != LDAP_SUCCESS || match != 0 )
1448 /* no dnattr match, check if this is a self clause */
1449 if ( ! b->a_dn_self )
1452 ACL_RECORD_VALUE_STATE;
1454 /* this is a self clause, check if the target is an
1460 /* target is attribute, check if the attribute value
1463 rc = value_match( &match, b->a_dn_at,
1464 b->a_dn_at->ad_type->sat_equality, 0,
1467 /* on match error or no match, fail the ACL clause */
1468 if (rc != LDAP_SUCCESS || match != 0 )
1474 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1476 struct berval ndn = BER_BVNULL;
1479 if ( op->o_ndn.bv_len == 0 ) {
1483 /* b->a_group is an unexpanded entry name, expanded it should be an
1484 * entry with objectclass group* and we test to see if odn is one of
1485 * the values in the attribute group
1487 /* see if asker is listed in dnattr */
1488 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1489 char buf[ACL_BUF_SIZE];
1491 regmatch_t tmp_matches[2],
1492 *tmp_matchesp = tmp_matches;
1494 bv.bv_len = sizeof(buf) - 1;
1499 switch ( a->acl_dn_style ) {
1500 case ACL_STYLE_REGEX:
1501 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1502 tmp_matchesp = matches;
1503 tmp_nmatch = nmatch;
1507 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1508 case ACL_STYLE_BASE:
1509 tmp_matches[0].rm_so = 0;
1510 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1515 case ACL_STYLE_SUBTREE:
1516 case ACL_STYLE_CHILDREN:
1517 tmp_matches[0].rm_so = 0;
1518 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1519 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1520 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1534 if ( string_expand( &bv, &b->a_group_pat,
1536 tmp_nmatch, tmp_matchesp ) )
1541 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1542 op->o_tmpmemctx ) != LDAP_SUCCESS )
1544 /* did not expand to a valid dn */
1551 bv = b->a_group_pat;
1554 rc = backend_group( op, e, &bv, &op->o_ndn,
1555 b->a_group_oc, b->a_group_at );
1558 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1566 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1568 char buf[ACL_BUF_SIZE];
1570 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1572 regmatch_t tmp_matches[2],
1573 *tmp_matchesp = tmp_matches;
1576 bv.bv_len = sizeof( buf ) - 1;
1581 switch ( a->acl_dn_style ) {
1582 case ACL_STYLE_REGEX:
1583 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1584 tmp_matchesp = matches;
1585 tmp_nmatch = nmatch;
1589 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1590 case ACL_STYLE_BASE:
1591 tmp_matches[0].rm_so = 0;
1592 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1597 case ACL_STYLE_SUBTREE:
1598 case ACL_STYLE_CHILDREN:
1599 tmp_matches[0].rm_so = 0;
1600 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1601 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1602 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1616 if ( string_expand( &bv, &b->a_set_pat,
1618 tmp_nmatch, tmp_matchesp ) )
1627 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1632 if ( b->a_authz.sai_ssf ) {
1633 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1634 b->a_authz.sai_ssf, op->o_ssf, 0 );
1635 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1640 if ( b->a_authz.sai_transport_ssf ) {
1641 Debug( LDAP_DEBUG_ACL,
1642 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1643 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1644 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1649 if ( b->a_authz.sai_tls_ssf ) {
1650 Debug( LDAP_DEBUG_ACL,
1651 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1652 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1653 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1658 if ( b->a_authz.sai_sasl_ssf ) {
1659 Debug( LDAP_DEBUG_ACL,
1660 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1661 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1662 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1668 if ( b->a_dynacl ) {
1670 slap_access_t tgrant, tdeny;
1672 /* this case works different from the others above.
1673 * since aci's themselves give permissions, we need
1674 * to first check b->a_access_mask, the ACL's access level.
1676 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1677 /* no ACIs in the root DSE */
1681 /* first check if the right being requested
1682 * is allowed by the ACL clause.
1684 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1688 /* start out with nothing granted, nothing denied */
1692 for ( da = b->a_dynacl; da; da = da->da_next ) {
1693 slap_access_t grant, deny;
1695 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1701 /* remove anything that the ACL clause does not allow */
1702 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1703 tdeny &= ACL_PRIV_MASK;
1705 /* see if we have anything to contribute */
1706 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1710 /* this could be improved by changing acl_mask so that it can deal with
1711 * by clauses that return grant/deny pairs. Right now, it does either
1712 * additive or subtractive rights, but not both at the same time. So,
1713 * we need to combine the grant/deny pair into a single rights mask in
1714 * a smart way: if either grant or deny is "empty", then we use the
1715 * opposite as is, otherwise we remove any denied rights from the grant
1716 * rights mask and construct an additive mask.
1718 if (ACL_IS_INVALID(tdeny)) {
1719 modmask = tgrant | ACL_PRIV_ADDITIVE;
1721 } else if (ACL_IS_INVALID(tgrant)) {
1722 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1725 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1729 #else /* !SLAP_DYNACL */
1731 #ifdef SLAPD_ACI_ENABLED
1732 if ( b->a_aci_at != NULL ) {
1734 slap_access_t grant, deny, tgrant, tdeny;
1735 struct berval parent_ndn;
1736 BerVarray bvals = NULL;
1739 /* this case works different from the others above.
1740 * since aci's themselves give permissions, we need
1741 * to first check b->a_access_mask, the ACL's access level.
1744 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1745 /* no ACIs in the root DSE */
1749 /* first check if the right being requested
1750 * is allowed by the ACL clause.
1752 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1755 /* start out with nothing granted, nothing denied */
1759 /* get the aci attribute */
1760 at = attr_find( e->e_attrs, b->a_aci_at );
1763 /* FIXME: this breaks acl caching;
1764 * see also ACL_RECORD_VALUE_STATE below */
1765 ACL_RECORD_VALUE_STATE;
1767 /* the aci is an multi-valued attribute. The
1768 * rights are determined by OR'ing the individual
1769 * rights given by the acis.
1771 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1776 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
1782 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1783 accessmask2str(tgrant, accessmaskbuf, 1),
1784 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1787 /* If the entry level aci didn't contain anything valid for the
1788 * current operation, climb up the tree and evaluate the
1789 * acis with scope set to subtree
1791 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
1792 dnParent( &e->e_nname, &parent_ndn );
1793 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
1794 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1795 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1803 for( i = 0; bvals[i].bv_val != NULL; i++){
1805 /* FIXME: this breaks acl caching;
1806 * see also ACL_RECORD_VALUE_STATE above */
1807 ACL_RECORD_VALUE_STATE;
1809 if (aci_mask(op, e, desc, val, &bvals[i],
1811 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
1815 /* evaluation stops as soon as either a "deny" or a
1816 * "grant" directive matches.
1818 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1822 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1823 accessmask2str(tgrant, accessmaskbuf, 1),
1824 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1828 case LDAP_NO_SUCH_ATTRIBUTE:
1829 /* just go on if the aci-Attribute is not present in
1832 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1836 case LDAP_NO_SUCH_OBJECT:
1837 /* We have reached the base object */
1838 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1849 dnParent( &parent_ndn, &parent_ndn );
1854 /* remove anything that the ACL clause does not allow */
1855 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1856 tdeny &= ACL_PRIV_MASK;
1858 /* see if we have anything to contribute */
1859 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1863 /* this could be improved by changing acl_mask so that it can deal with
1864 * by clauses that return grant/deny pairs. Right now, it does either
1865 * additive or subtractive rights, but not both at the same time. So,
1866 * we need to combine the grant/deny pair into a single rights mask in
1867 * a smart way: if either grant or deny is "empty", then we use the
1868 * opposite as is, otherwise we remove any denied rights from the grant
1869 * rights mask and construct an additive mask.
1871 if (ACL_IS_INVALID(tdeny)) {
1872 modmask = tgrant | ACL_PRIV_ADDITIVE;
1874 } else if (ACL_IS_INVALID(tgrant)) {
1875 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1878 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1882 #endif /* SLAPD_ACI_ENABLED */
1883 #endif /* !SLAP_DYNACL */
1885 modmask = b->a_access_mask;
1888 Debug( LDAP_DEBUG_ACL,
1889 "<= acl_mask: [%d] applying %s (%s)\n",
1890 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1891 b->a_type == ACL_CONTINUE
1893 : b->a_type == ACL_BREAK
1899 if( ACL_IS_ADDITIVE(modmask) ) {
1901 ACL_PRIV_SET( *mask, modmask );
1904 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1906 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1907 /* substract privs */
1908 ACL_PRIV_CLR( *mask, modmask );
1911 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1918 Debug( LDAP_DEBUG_ACL,
1919 "<= acl_mask: [%d] mask: %s\n",
1920 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1922 if( b->a_type == ACL_CONTINUE ) {
1925 } else if ( b->a_type == ACL_BREAK ) {
1933 /* implicit "by * none" clause */
1936 Debug( LDAP_DEBUG_ACL,
1937 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1938 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1943 * acl_check_modlist - check access control on the given entry to see if
1944 * it allows the given modifications by the user associated with op.
1945 * returns 1 if mods allowed ok
1946 * 0 mods not allowed
1953 Modifications *mlist
1957 AccessControlState state = ACL_STATE_INIT;
1960 int ret = 1; /* default is access allowed */
1964 be = LDAP_STAILQ_FIRST(&backendDB);
1968 assert( be != NULL );
1970 /* short circuit root database access */
1971 if ( be_isroot( op ) ) {
1972 Debug( LDAP_DEBUG_ACL,
1973 "<= acl_access_allowed: granted to database root\n",
1978 /* use backend default access if no backend acls */
1979 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1980 Debug( LDAP_DEBUG_ACL,
1981 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1982 access2str( ACL_WRITE ),
1983 op->o_bd->be_dfltaccess >= ACL_WRITE
1984 ? "granted" : "denied",
1986 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1990 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1992 * no-user-modification operational attributes are ignored
1993 * by ACL_WRITE checking as any found here are not provided
1996 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1997 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1998 " modify access granted\n",
1999 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2003 switch ( mlist->sml_op ) {
2004 case LDAP_MOD_REPLACE:
2006 * We must check both permission to delete the whole
2007 * attribute and permission to add the specific attributes.
2008 * This prevents abuse from selfwriters.
2010 if ( ! access_allowed( op, e,
2011 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
2017 if ( mlist->sml_values == NULL ) break;
2019 /* fall thru to check value to add */
2022 assert( mlist->sml_values != NULL );
2024 for ( bv = mlist->sml_nvalues
2025 ? mlist->sml_nvalues : mlist->sml_values;
2026 bv->bv_val != NULL; bv++ )
2028 if ( ! access_allowed( op, e,
2029 mlist->sml_desc, bv, ACL_WRITE, &state ) )
2037 case LDAP_MOD_DELETE:
2038 if ( mlist->sml_values == NULL ) {
2039 if ( ! access_allowed( op, e,
2040 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
2047 for ( bv = mlist->sml_nvalues
2048 ? mlist->sml_nvalues : mlist->sml_values;
2049 bv->bv_val != NULL; bv++ )
2051 if ( ! access_allowed( op, e,
2052 mlist->sml_desc, bv, ACL_WRITE, &state ) )
2060 case SLAP_MOD_SOFTADD:
2061 /* allow adding attribute via modrdn thru */
2073 if (be_null) op->o_bd = NULL;
2079 struct berval *list,
2092 while ( len >= 0 && --ix >= 0 ) {
2093 while ( --len >= 0 && *p++ != sep )
2096 while ( len >= 0 && *p == ' ' ) {
2109 while ( --len >= 0 && *p != sep ) {
2113 while ( bv->bv_len > 0 && *--p == ' ' ) {
2120 typedef struct aci_set_gather_t {
2126 aci_set_cb_gather( Operation *op, SlapReply *rs )
2128 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2130 if ( rs->sr_type == REP_SEARCH ) {
2131 BerValue bvals[ 2 ];
2132 BerVarray bvalsp = NULL;
2135 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2136 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2138 if ( desc == slap_schema.si_ad_entryDN ) {
2140 bvals[ 0 ] = rs->sr_entry->e_nname;
2141 BER_BVZERO( &bvals[ 1 ] );
2146 a = attr_find( rs->sr_entry->e_attrs, desc );
2150 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2153 bvalsp = a->a_nvals;
2159 p->bvals = slap_set_join( p->cookie, p->bvals,
2160 ( '|' | SLAP_SET_RREF ), bvalsp );
2164 assert( rs->sr_type == REP_RESULT );
2171 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2173 AciSetCookie *cp = (AciSetCookie *)cookie;
2175 LDAPURLDesc *ludp = NULL;
2176 Operation op2 = { 0 };
2177 SlapReply rs = {REP_RESULT};
2178 AttributeName anlist[ 2 ], *anlistp = NULL;
2180 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2181 aci_set_gather_t p = { 0 };
2182 const char *text = NULL;
2183 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2185 /* this routine needs to return the bervals instead of
2186 * plain strings, since syntax is not known. It should
2187 * also return the syntax or some "comparison cookie".
2189 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2190 return aci_set_gather2( cookie, name, desc );
2193 rc = ldap_url_parse( name->bv_val, &ludp );
2194 if ( rc != LDAP_URL_SUCCESS ) {
2195 rc = LDAP_PROTOCOL_ERROR;
2199 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2201 /* host part must be empty */
2202 /* extensions parts must be empty */
2203 rc = LDAP_PROTOCOL_ERROR;
2207 /* Grab the searchbase and see if an appropriate database can be found */
2208 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2209 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2210 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2211 BER_BVZERO( &op2.o_req_dn );
2212 if ( rc != LDAP_SUCCESS ) {
2216 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2217 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2218 rc = LDAP_NO_SUCH_OBJECT;
2222 /* Grab the filter */
2223 if ( ludp->lud_filter ) {
2224 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2225 cp->op->o_tmpmemctx );
2228 op2.ors_filterstr = defaultFilter_bv;
2231 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2232 if ( op2.ors_filter == NULL ) {
2233 rc = LDAP_PROTOCOL_ERROR;
2237 /* Grab the scope */
2238 op2.ors_scope = ludp->lud_scope;
2240 /* Grap the attributes */
2241 if ( ludp->lud_attrs ) {
2242 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2245 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2246 cp->op->o_tmpmemctx );
2248 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2249 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2250 anlistp[ nattrs ].an_desc = NULL;
2251 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2252 &anlistp[ nattrs ].an_desc, &text );
2253 if ( rc != LDAP_SUCCESS ) {
2262 anlistp[ nattrs ].an_name = desc->ad_cname;
2263 anlistp[ nattrs ].an_desc = desc;
2265 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2269 op2.o_hdr = cp->op->o_hdr;
2270 op2.o_tag = LDAP_REQ_SEARCH;
2271 op2.o_ndn = op2.o_bd->be_rootndn;
2272 op2.o_callback = &cb;
2273 op2.o_time = slap_get_time();
2274 op2.o_do_not_cache = 1;
2275 op2.o_is_auth_check = 0;
2276 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2277 op2.ors_slimit = SLAP_NO_LIMIT;
2278 op2.ors_tlimit = SLAP_NO_LIMIT;
2279 op2.ors_attrs = anlistp;
2280 op2.ors_attrsonly = 0;
2281 op2.o_private = cp->op->o_private;
2285 rc = op2.o_bd->be_search( &op2, &rs );
2291 if ( op2.ors_filter ) {
2292 filter_free_x( cp->op, op2.ors_filter );
2294 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2295 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2297 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2298 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2301 ldap_free_urldesc( ludp );
2303 if ( anlistp && anlistp != anlist ) {
2304 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2311 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2313 AciSetCookie *cp = (AciSetCookie *)cookie;
2314 BerVarray bvals = NULL;
2318 /* this routine needs to return the bervals instead of
2319 * plain strings, since syntax is not known. It should
2320 * also return the syntax or some "comparison cookie".
2322 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2323 if ( rc == LDAP_SUCCESS ) {
2324 if ( desc == slap_schema.si_ad_entryDN ) {
2325 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2326 cp->op->o_tmpmemctx );
2328 BER_BVZERO( &bvals[ 1 ] );
2332 backend_attribute( cp->op,
2333 cp->e, &ndn, desc, &bvals, ACL_NONE );
2336 if ( !BER_BVISNULL( &ndn ) ) {
2337 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2346 struct berval *subj,
2352 struct berval set = BER_BVNULL;
2354 AciSetCookie cookie;
2356 if ( setref == 0 ) {
2357 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2360 struct berval subjdn, ndn = BER_BVNULL;
2361 struct berval setat;
2364 AttributeDescription *desc = NULL;
2366 /* format of string is "entry/setAttrName" */
2367 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2371 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2372 setat = aci_bv_set_attr;
2376 * NOTE: dnNormalize honors the ber_len field
2377 * as the length of the dn to be normalized
2379 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2380 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2382 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2383 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2387 BER_BVZERO( &bvals[0] );
2388 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2390 bvals[0].bv_val = bvals[i-1].bv_val;
2391 BER_BVZERO( &bvals[i-1] );
2393 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2394 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2399 if ( !BER_BVISNULL( &set ) ) {
2402 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2403 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2404 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2410 #ifdef SLAPD_ACI_ENABLED
2412 aci_list_map_rights(
2413 struct berval *list )
2420 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2423 switch (*bv.bv_val) {
2425 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2428 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2429 * the right 's' to mean "set", but in the examples states
2430 * that the right 's' means "search". The latter definition
2433 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2436 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2439 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2442 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2443 * define any equivalent to the AUTH right, so I've just used
2446 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2458 struct berval *list,
2459 const struct berval *attr,
2460 struct berval *val )
2462 struct berval bv, left, right;
2465 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2466 if (aci_get_part(&bv, 0, '=', &left) < 0
2467 || aci_get_part(&bv, 1, '=', &right) < 0)
2469 if (ber_bvstrcasecmp(attr, &bv) == 0)
2471 } else if (val == NULL) {
2472 if (ber_bvstrcasecmp(attr, &left) == 0)
2475 if (ber_bvstrcasecmp(attr, &left) == 0) {
2476 /* this is experimental code that implements a
2477 * simple (prefix) match of the attribute value.
2478 * the ACI draft does not provide for aci's that
2479 * apply to specific values, but it would be
2480 * nice to have. If the <attr> part of an aci's
2481 * rights list is of the form <attr>=<value>,
2482 * that means the aci applies only to attrs with
2483 * the given value. Furthermore, if the attr is
2484 * of the form <attr>=<value>*, then <value> is
2485 * treated as a prefix, and the aci applies to
2486 * any value with that prefix.
2488 * Ideally, this would allow r.e. matches.
2490 if (aci_get_part(&right, 0, '*', &left) < 0
2491 || right.bv_len <= left.bv_len)
2493 if (ber_bvstrcasecmp(val, &right) == 0)
2495 } else if (val->bv_len >= left.bv_len) {
2496 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2505 static slap_access_t
2506 aci_list_get_attr_rights(
2507 struct berval *list,
2508 const struct berval *attr,
2509 struct berval *val )
2515 /* loop through each rights/attr pair, skip first part (action) */
2517 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2518 if (aci_list_has_attr(&bv, attr, val) == 0)
2520 if (aci_get_part(list, i, ';', &bv) < 0)
2522 mask |= aci_list_map_rights(&bv);
2528 aci_list_get_rights(
2529 struct berval *list,
2530 const struct berval *attr,
2532 slap_access_t *grant,
2533 slap_access_t *deny )
2535 struct berval perm, actn;
2536 slap_access_t *mask;
2539 if (attr == NULL || attr->bv_len == 0
2540 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2541 attr = &aci_bv_br_entry;
2547 /* loop through each permissions clause */
2548 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2549 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2551 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2553 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2560 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2561 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2568 struct berval *subj,
2569 struct berval *defgrpoc,
2570 struct berval *defgrpat,
2577 struct berval subjdn;
2578 struct berval grpoc;
2579 struct berval grpat;
2580 ObjectClass *grp_oc = NULL;
2581 AttributeDescription *grp_ad = NULL;
2585 /* format of string is "group/objectClassValue/groupAttrName" */
2586 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2590 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2594 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2598 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2599 if( rc != LDAP_SUCCESS ) {
2605 grp_oc = oc_bvfind( &grpoc );
2607 if (grp_oc != NULL && grp_ad != NULL ) {
2608 char buf[ACL_BUF_SIZE];
2609 struct berval bv, ndn;
2610 bv.bv_len = sizeof( buf ) - 1;
2611 bv.bv_val = (char *)&buf;
2612 if ( string_expand(&bv, &subjdn,
2613 e->e_ndn, nmatch, matches) )
2618 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2619 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2620 grp_oc, grp_ad ) == 0 );
2621 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2633 AttributeDescription *desc,
2637 regmatch_t *matches,
2638 slap_access_t *grant,
2639 slap_access_t *deny,
2640 slap_aci_scope_t asserted_scope
2643 struct berval bv, scope, perms, type, sdn;
2647 assert( !BER_BVISNULL( &desc->ad_cname ) );
2649 /* parse an aci of the form:
2650 oid # scope # action;rights;attr;rights;attr
2651 $ action;rights;attr;rights;attr # type # subject
2653 [NOTE: the following comment is very outdated,
2654 as the draft version it refers to (Ando, 2004-11-20)].
2656 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2657 a full description of the format for this attribute.
2658 Differences: "this" in the draft is "self" here, and
2659 "self" and "public" is in the position of type.
2661 <scope> = {entry|children|subtree}
2662 <type> = {public|users|access-id|subtree|onelevel|children|
2663 self|dnattr|group|role|set|set-ref}
2665 This routine now supports scope={ENTRY,CHILDREN}
2667 - ENTRY applies to "entry" and "subtree";
2668 - CHILDREN aplies to "children" and "subtree"
2671 /* check that the aci has all 5 components */
2672 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2676 /* check that the aci family is supported */
2677 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2681 /* check that the scope matches */
2682 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2686 /* note: scope can be either ENTRY or CHILDREN;
2687 * they respectively match "entry" and "children" in bv
2688 * both match "subtree" */
2689 switch ( asserted_scope ) {
2690 case SLAP_ACI_SCOPE_ENTRY:
2691 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2692 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2698 case SLAP_ACI_SCOPE_CHILDREN:
2699 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2700 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2710 /* get the list of permissions clauses, bail if empty */
2711 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2715 /* check if any permissions allow desired access */
2716 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2720 /* see if we have a DN match */
2721 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2725 /* see if we have a public (i.e. anonymous) access */
2726 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2730 /* otherwise require an identity */
2731 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2735 /* see if we have a users access */
2736 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2740 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2741 * just grab all the berval up to its end (ITS#3303).
2742 * NOTE: the problem could be solved by providing the DN with
2743 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
2744 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2746 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2750 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2751 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2753 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2756 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2757 if ( rc != LDAP_SUCCESS ) {
2761 if ( dn_match( &op->o_ndn, &ndn ) ) {
2764 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2768 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2771 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2772 if ( rc != LDAP_SUCCESS ) {
2776 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2779 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2783 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2784 struct berval ndn, pndn;
2786 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2787 if ( rc != LDAP_SUCCESS ) {
2791 dnParent( &ndn, &pndn );
2793 if ( dn_match( &op->o_ndn, &pndn ) ) {
2796 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2800 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2803 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2804 if ( rc != LDAP_SUCCESS ) {
2808 if ( !dn_match( &op->o_ndn, &ndn )
2809 && dnIsSuffix( &op->o_ndn, &ndn ) )
2813 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2817 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2818 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2822 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2824 AttributeDescription *ad = NULL;
2827 rc = slap_bv2ad( &sdn, &ad, &text );
2829 if( rc != LDAP_SUCCESS ) {
2835 for ( at = attrs_find( e->e_attrs, ad );
2837 at = attrs_find( at->a_next, ad ) )
2839 if ( value_find_ex( ad,
2840 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2841 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2843 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2852 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2853 if ( aci_group_member( &sdn, &aci_bv_group_class,
2854 &aci_bv_group_attr, op, e, nmatch, matches ) )
2859 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2860 if ( aci_group_member( &sdn, &aci_bv_role_class,
2861 &aci_bv_role_attr, op, e, nmatch, matches ) )
2866 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2867 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2871 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2872 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2882 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2884 AttributeDescription *ad = NULL;
2885 const char *text = NULL;
2887 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2888 fprintf( stderr, "%s: line %d: "
2889 "inappropriate style \"%s\" in \"aci\" by clause\n",
2890 fname, lineno, sty );
2894 if ( right != NULL && *right != '\0' ) {
2895 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2897 "%s: line %d: aci \"%s\": %s\n",
2898 fname, lineno, right, text );
2903 ad = slap_schema.si_ad_aci;
2906 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2907 fprintf( stderr, "%s: line %d: "
2908 "aci \"%s\": inappropriate syntax: %s\n",
2909 fname, lineno, right,
2910 ad->ad_type->sat_syntax_oid );
2914 *privp = (void *)ad;
2920 dynacl_aci_unparse( void *priv, struct berval *bv )
2922 AttributeDescription *ad = ( AttributeDescription * )priv;
2927 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
2928 ptr = lutil_strcopy( bv->bv_val, " aci=" );
2929 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
2930 bv->bv_len = ptr - bv->bv_val;
2941 AttributeDescription *desc,
2944 regmatch_t *matches,
2945 slap_access_t *grantp,
2946 slap_access_t *denyp )
2948 AttributeDescription *ad = ( AttributeDescription * )priv;
2950 slap_access_t tgrant, tdeny, grant, deny;
2952 char accessmaskbuf[ACCESSMASK_MAXLEN];
2953 char accessmaskbuf1[ACCESSMASK_MAXLEN];
2954 #endif /* LDAP_DEBUG */
2956 /* start out with nothing granted, nothing denied */
2960 /* get the aci attribute */
2961 at = attr_find( e->e_attrs, ad );
2965 /* the aci is an multi-valued attribute. The
2966 * rights are determined by OR'ing the individual
2967 * rights given by the acis.
2969 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2970 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2971 nmatch, matches, &grant, &deny,
2972 SLAP_ACI_SCOPE_ENTRY ) != 0 )
2979 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2980 accessmask2str( tgrant, accessmaskbuf, 1 ),
2981 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2984 /* If the entry level aci didn't contain anything valid for the
2985 * current operation, climb up the tree and evaluate the
2986 * acis with scope set to subtree
2988 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2989 struct berval parent_ndn;
2992 /* to solve the chicken'n'egg problem of accessing
2993 * the OpenLDAPaci attribute, the direct access
2994 * to the entry's attribute is unchecked; however,
2995 * further accesses to OpenLDAPaci values in the
2996 * ancestors occur through backend_attribute(), i.e.
2997 * with the identity of the operation, requiring
2998 * further access checking. For uniformity, this
2999 * makes further requests occur as the rootdn, if
3000 * any, i.e. searching for the OpenLDAPaci attribute
3001 * is considered an internal search. If this is not
3002 * acceptable, then the same check needs be performed
3003 * when accessing the entry's attribute. */
3004 Operation op2 = *op;
3006 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3007 op2.o_dn = op->o_bd->be_rootdn;
3008 op2.o_ndn = op->o_bd->be_rootndn;
3012 dnParent( &e->e_nname, &parent_ndn );
3013 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3015 BerVarray bvals = NULL;
3018 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3019 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3028 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3029 if ( aci_mask( op, e, desc, val,
3033 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3037 /* evaluation stops as soon as either a "deny" or a
3038 * "grant" directive matches.
3040 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3044 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3045 accessmask2str( tgrant, accessmaskbuf, 1 ),
3046 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3050 case LDAP_NO_SUCH_ATTRIBUTE:
3051 /* just go on if the aci-Attribute is not present in
3054 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3058 case LDAP_NO_SUCH_OBJECT:
3059 /* We have reached the base object */
3060 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3072 dnParent( &parent_ndn, &parent_ndn );
3082 /* need to register this at some point */
3083 static slap_dynacl_t dynacl_aci = {
3093 #endif /* SLAP_DYNACL */
3095 #endif /* SLAPD_ACI_ENABLED */
3100 * dynamic ACL infrastructure
3102 static slap_dynacl_t *da_list = NULL;
3105 slap_dynacl_register( slap_dynacl_t *da )
3109 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3110 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3115 if ( tmp != NULL ) {
3119 if ( da->da_mask == NULL ) {
3123 da->da_private = NULL;
3124 da->da_next = da_list;
3130 static slap_dynacl_t *
3131 slap_dynacl_next( slap_dynacl_t *da )
3140 slap_dynacl_get( const char *name )
3144 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3145 if ( strcasecmp( da->da_name, name ) == 0 ) {
3152 #endif /* SLAP_DYNACL */
3159 slap_dynacl_t *known_dynacl[] = {
3160 #ifdef SLAPD_ACI_ENABLED
3162 #endif /* SLAPD_ACI_ENABLED */
3166 for ( i = 0; known_dynacl[ i ]; i++ ) {
3167 rc = slap_dynacl_register( known_dynacl[ i ] );
3172 #endif /* SLAP_DYNACL */
3183 regmatch_t *matches)
3191 bv->bv_val[0] = '\0';
3192 bv->bv_len--; /* leave space for lone $ */
3195 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3196 sp < pat->bv_val + pat->bv_len ; sp++ )
3198 /* did we previously see a $ */
3200 if ( flag == 1 && *sp == '$' ) {
3205 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3208 } else if ( *sp >= '0' && *sp <= '9' ) {
3216 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3217 if ( *sp >= '0' && *sp <= '9' ) {
3218 n = 10*n + ( *sp - '0' );
3222 if ( *sp != /*'{'*/ '}' ) {
3228 if ( n >= nmatch ) {
3234 i = matches[n].rm_so;
3235 l = matches[n].rm_eo;
3236 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3254 /* must have ended with a single $ */
3262 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3263 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3270 struct berval *pat, /* pattern to expand and match against */
3271 char *str, /* string to match against pattern */
3272 char *buf, /* buffer with $N expansion variables */
3273 int nmatch, /* size of the matches array */
3274 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3278 char newbuf[ACL_BUF_SIZE];
3282 bv.bv_len = sizeof( newbuf ) - 1;
3289 string_expand( &bv, pat, buf, nmatch, matches );
3290 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3292 char error[ACL_BUF_SIZE];
3293 regerror( rc, &re, error, sizeof( error ) );
3295 Debug( LDAP_DEBUG_TRACE,
3296 "compile( \"%s\", \"%s\") failed %s\n",
3297 pat->bv_val, str, error );
3301 rc = regexec( &re, str, 0, NULL, 0 );
3304 Debug( LDAP_DEBUG_TRACE,
3305 "=> regex_matches: string: %s\n", str, 0, 0 );
3306 Debug( LDAP_DEBUG_TRACE,
3307 "=> regex_matches: rc: %d %s\n",
3308 rc, !rc ? "matches" : "no matches", 0 );