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);
116 #endif /* SLAPD_ACI_ENABLED */
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 /* !SLAP_DYNACL && 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 ) )
1114 ndn = op->o_conn->c_ndn;
1119 if ( acl_mask_dn( op, e, a, nmatch, matches,
1120 &b->a_realdn, &ndn ) )
1126 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1127 if ( ! op->o_conn->c_listener ) {
1130 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1131 b->a_sockurl_pat.bv_val, 0, 0 );
1133 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1134 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1135 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1136 e->e_ndn, nmatch, matches ) )
1141 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1143 char buf[ACL_BUF_SIZE];
1145 bv.bv_len = sizeof( buf ) - 1;
1147 if ( string_expand( &bv, &b->a_sockurl_pat,
1148 e->e_ndn, nmatch, matches ) )
1153 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1159 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1167 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1168 if ( !op->o_conn->c_peer_domain.bv_val ) {
1171 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1172 b->a_domain_pat.bv_val, 0, 0 );
1173 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1174 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1175 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1176 e->e_ndn, nmatch, matches ) )
1181 char buf[ACL_BUF_SIZE];
1183 struct berval cmp = op->o_conn->c_peer_domain;
1184 struct berval pat = b->a_domain_pat;
1186 if ( b->a_domain_expand ) {
1189 bv.bv_len = sizeof(buf) - 1;
1192 if ( string_expand(&bv, &b->a_domain_pat,
1193 e->e_ndn, nmatch, matches) )
1200 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1201 int offset = cmp.bv_len - pat.bv_len;
1206 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1210 /* trim the domain */
1211 cmp.bv_val = &cmp.bv_val[ offset ];
1212 cmp.bv_len -= offset;
1215 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1222 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1223 if ( !op->o_conn->c_peer_name.bv_val ) {
1226 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1227 b->a_peername_pat.bv_val, 0, 0 );
1228 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1229 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1230 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1231 e->e_ndn, nmatch, matches ) )
1237 /* try exact match */
1238 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1239 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1243 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1245 char buf[ACL_BUF_SIZE];
1247 bv.bv_len = sizeof( buf ) - 1;
1249 if ( string_expand( &bv, &b->a_peername_pat,
1250 e->e_ndn, nmatch, matches ) )
1255 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1259 /* extract IP and try exact match */
1260 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1262 char buf[] = "255.255.255.255";
1265 int port_number = -1;
1267 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1268 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1271 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1272 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1274 port = strrchr( ip.bv_val, ':' );
1278 ip.bv_len = port - ip.bv_val;
1280 port_number = strtol( port, &next, 10 );
1281 if ( next[0] != '\0' )
1285 /* the port check can be anticipated here */
1286 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1289 /* address longer than expected? */
1290 if ( ip.bv_len >= sizeof(buf) )
1293 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1294 buf[ ip.bv_len ] = '\0';
1296 addr = inet_addr( buf );
1298 /* unable to convert? */
1299 if ( addr == (unsigned long)(-1) )
1302 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1305 #ifdef LDAP_PF_LOCAL
1306 /* extract path and try exact match */
1307 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1310 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1311 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1314 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1315 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1317 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1320 #endif /* LDAP_PF_LOCAL */
1322 /* exact match (very unlikely...) */
1323 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1330 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1331 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1334 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1335 b->a_sockname_pat.bv_val, 0, 0 );
1336 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1337 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1338 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1339 e->e_ndn, nmatch, matches ) )
1344 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1346 char buf[ACL_BUF_SIZE];
1348 bv.bv_len = sizeof( buf ) - 1;
1350 if ( string_expand( &bv, &b->a_sockname_pat,
1351 e->e_ndn, nmatch, matches ) )
1356 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1361 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1368 if ( b->a_dn_at != NULL ) {
1369 if ( acl_mask_dnattr( op, e, val, a, b, i,
1370 matches, count, state,
1371 &b->a_dn, &op->o_ndn ) )
1377 if ( b->a_realdn_at != NULL ) {
1380 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1382 ndn = op->o_conn->c_ndn;
1387 if ( acl_mask_dnattr( op, e, val, a, b, i,
1388 matches, count, state,
1389 &b->a_realdn, &ndn ) )
1395 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1397 struct berval ndn = BER_BVNULL;
1400 if ( op->o_ndn.bv_len == 0 ) {
1404 /* b->a_group is an unexpanded entry name, expanded it should be an
1405 * entry with objectclass group* and we test to see if odn is one of
1406 * the values in the attribute group
1408 /* see if asker is listed in dnattr */
1409 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1410 char buf[ACL_BUF_SIZE];
1412 regmatch_t tmp_matches[2],
1413 *tmp_matchesp = tmp_matches;
1415 bv.bv_len = sizeof(buf) - 1;
1420 switch ( a->acl_dn_style ) {
1421 case ACL_STYLE_REGEX:
1422 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1423 tmp_matchesp = matches;
1424 tmp_nmatch = nmatch;
1428 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1429 case ACL_STYLE_BASE:
1430 tmp_matches[0].rm_so = 0;
1431 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1436 case ACL_STYLE_SUBTREE:
1437 case ACL_STYLE_CHILDREN:
1438 tmp_matches[0].rm_so = 0;
1439 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1440 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1441 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1455 if ( string_expand( &bv, &b->a_group_pat,
1457 tmp_nmatch, tmp_matchesp ) )
1462 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1463 op->o_tmpmemctx ) != LDAP_SUCCESS )
1465 /* did not expand to a valid dn */
1472 bv = b->a_group_pat;
1475 rc = backend_group( op, e, &bv, &op->o_ndn,
1476 b->a_group_oc, b->a_group_at );
1479 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1487 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1489 char buf[ACL_BUF_SIZE];
1491 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1493 regmatch_t tmp_matches[2],
1494 *tmp_matchesp = tmp_matches;
1497 bv.bv_len = sizeof( buf ) - 1;
1502 switch ( a->acl_dn_style ) {
1503 case ACL_STYLE_REGEX:
1504 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1505 tmp_matchesp = matches;
1506 tmp_nmatch = nmatch;
1510 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1511 case ACL_STYLE_BASE:
1512 tmp_matches[0].rm_so = 0;
1513 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1518 case ACL_STYLE_SUBTREE:
1519 case ACL_STYLE_CHILDREN:
1520 tmp_matches[0].rm_so = 0;
1521 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1522 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1523 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1537 if ( string_expand( &bv, &b->a_set_pat,
1539 tmp_nmatch, tmp_matchesp ) )
1548 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1553 if ( b->a_authz.sai_ssf ) {
1554 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1555 b->a_authz.sai_ssf, op->o_ssf, 0 );
1556 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1561 if ( b->a_authz.sai_transport_ssf ) {
1562 Debug( LDAP_DEBUG_ACL,
1563 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1564 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1565 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1570 if ( b->a_authz.sai_tls_ssf ) {
1571 Debug( LDAP_DEBUG_ACL,
1572 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1573 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1574 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1579 if ( b->a_authz.sai_sasl_ssf ) {
1580 Debug( LDAP_DEBUG_ACL,
1581 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1582 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1583 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1589 if ( b->a_dynacl ) {
1591 slap_access_t tgrant, tdeny;
1593 /* this case works different from the others above.
1594 * since aci's themselves give permissions, we need
1595 * to first check b->a_access_mask, the ACL's access level.
1597 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1598 /* no ACIs in the root DSE */
1602 /* first check if the right being requested
1603 * is allowed by the ACL clause.
1605 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1609 /* start out with nothing granted, nothing denied */
1613 for ( da = b->a_dynacl; da; da = da->da_next ) {
1614 slap_access_t grant, deny;
1616 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1622 /* remove anything that the ACL clause does not allow */
1623 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1624 tdeny &= ACL_PRIV_MASK;
1626 /* see if we have anything to contribute */
1627 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1631 /* this could be improved by changing acl_mask so that it can deal with
1632 * by clauses that return grant/deny pairs. Right now, it does either
1633 * additive or subtractive rights, but not both at the same time. So,
1634 * we need to combine the grant/deny pair into a single rights mask in
1635 * a smart way: if either grant or deny is "empty", then we use the
1636 * opposite as is, otherwise we remove any denied rights from the grant
1637 * rights mask and construct an additive mask.
1639 if (ACL_IS_INVALID(tdeny)) {
1640 modmask = tgrant | ACL_PRIV_ADDITIVE;
1642 } else if (ACL_IS_INVALID(tgrant)) {
1643 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1646 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1650 #else /* !SLAP_DYNACL */
1652 #ifdef SLAPD_ACI_ENABLED
1653 if ( b->a_aci_at != NULL ) {
1655 slap_access_t grant, deny, tgrant, tdeny;
1656 struct berval parent_ndn;
1657 BerVarray bvals = NULL;
1660 /* this case works different from the others above.
1661 * since aci's themselves give permissions, we need
1662 * to first check b->a_access_mask, the ACL's access level.
1665 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1666 /* no ACIs in the root DSE */
1670 /* first check if the right being requested
1671 * is allowed by the ACL clause.
1673 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1676 /* start out with nothing granted, nothing denied */
1680 /* get the aci attribute */
1681 at = attr_find( e->e_attrs, b->a_aci_at );
1684 /* FIXME: this breaks acl caching;
1685 * see also ACL_RECORD_VALUE_STATE below */
1686 ACL_RECORD_VALUE_STATE;
1688 /* the aci is an multi-valued attribute. The
1689 * rights are determined by OR'ing the individual
1690 * rights given by the acis.
1692 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1697 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
1703 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1704 accessmask2str(tgrant, accessmaskbuf, 1),
1705 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1708 /* If the entry level aci didn't contain anything valid for the
1709 * current operation, climb up the tree and evaluate the
1710 * acis with scope set to subtree
1712 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
1713 dnParent( &e->e_nname, &parent_ndn );
1714 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
1715 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1716 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1724 for( i = 0; bvals[i].bv_val != NULL; i++){
1726 /* FIXME: this breaks acl caching;
1727 * see also ACL_RECORD_VALUE_STATE above */
1728 ACL_RECORD_VALUE_STATE;
1730 if (aci_mask(op, e, desc, val, &bvals[i],
1732 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
1736 /* evaluation stops as soon as either a "deny" or a
1737 * "grant" directive matches.
1739 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1743 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1744 accessmask2str(tgrant, accessmaskbuf, 1),
1745 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1749 case LDAP_NO_SUCH_ATTRIBUTE:
1750 /* just go on if the aci-Attribute is not present in
1753 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1757 case LDAP_NO_SUCH_OBJECT:
1758 /* We have reached the base object */
1759 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1770 dnParent( &parent_ndn, &parent_ndn );
1775 /* remove anything that the ACL clause does not allow */
1776 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1777 tdeny &= ACL_PRIV_MASK;
1779 /* see if we have anything to contribute */
1780 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1784 /* this could be improved by changing acl_mask so that it can deal with
1785 * by clauses that return grant/deny pairs. Right now, it does either
1786 * additive or subtractive rights, but not both at the same time. So,
1787 * we need to combine the grant/deny pair into a single rights mask in
1788 * a smart way: if either grant or deny is "empty", then we use the
1789 * opposite as is, otherwise we remove any denied rights from the grant
1790 * rights mask and construct an additive mask.
1792 if (ACL_IS_INVALID(tdeny)) {
1793 modmask = tgrant | ACL_PRIV_ADDITIVE;
1795 } else if (ACL_IS_INVALID(tgrant)) {
1796 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1799 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1803 #endif /* SLAPD_ACI_ENABLED */
1804 #endif /* !SLAP_DYNACL */
1806 modmask = b->a_access_mask;
1809 Debug( LDAP_DEBUG_ACL,
1810 "<= acl_mask: [%d] applying %s (%s)\n",
1811 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1812 b->a_type == ACL_CONTINUE
1814 : b->a_type == ACL_BREAK
1820 if( ACL_IS_ADDITIVE(modmask) ) {
1822 ACL_PRIV_SET( *mask, modmask );
1825 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1827 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1828 /* substract privs */
1829 ACL_PRIV_CLR( *mask, modmask );
1832 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1839 Debug( LDAP_DEBUG_ACL,
1840 "<= acl_mask: [%d] mask: %s\n",
1841 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1843 if( b->a_type == ACL_CONTINUE ) {
1846 } else if ( b->a_type == ACL_BREAK ) {
1854 /* implicit "by * none" clause */
1857 Debug( LDAP_DEBUG_ACL,
1858 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1859 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1864 * acl_check_modlist - check access control on the given entry to see if
1865 * it allows the given modifications by the user associated with op.
1866 * returns 1 if mods allowed ok
1867 * 0 mods not allowed
1874 Modifications *mlist
1878 AccessControlState state = ACL_STATE_INIT;
1881 int ret = 1; /* default is access allowed */
1885 be = LDAP_STAILQ_FIRST(&backendDB);
1889 assert( be != NULL );
1891 /* short circuit root database access */
1892 if ( be_isroot( op ) ) {
1893 Debug( LDAP_DEBUG_ACL,
1894 "<= acl_access_allowed: granted to database root\n",
1899 /* use backend default access if no backend acls */
1900 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1901 Debug( LDAP_DEBUG_ACL,
1902 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1903 access2str( ACL_WRITE ),
1904 op->o_bd->be_dfltaccess >= ACL_WRITE
1905 ? "granted" : "denied",
1907 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1911 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1913 * no-user-modification operational attributes are ignored
1914 * by ACL_WRITE checking as any found here are not provided
1917 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1918 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1919 " modify access granted\n",
1920 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1924 switch ( mlist->sml_op ) {
1925 case LDAP_MOD_REPLACE:
1927 * We must check both permission to delete the whole
1928 * attribute and permission to add the specific attributes.
1929 * This prevents abuse from selfwriters.
1931 if ( ! access_allowed( op, e,
1932 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1938 if ( mlist->sml_values == NULL ) break;
1940 /* fall thru to check value to add */
1943 assert( mlist->sml_values != NULL );
1945 for ( bv = mlist->sml_nvalues
1946 ? mlist->sml_nvalues : mlist->sml_values;
1947 bv->bv_val != NULL; bv++ )
1949 if ( ! access_allowed( op, e,
1950 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1958 case LDAP_MOD_DELETE:
1959 if ( mlist->sml_values == NULL ) {
1960 if ( ! access_allowed( op, e,
1961 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1968 for ( bv = mlist->sml_nvalues
1969 ? mlist->sml_nvalues : mlist->sml_values;
1970 bv->bv_val != NULL; bv++ )
1972 if ( ! access_allowed( op, e,
1973 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1981 case SLAP_MOD_SOFTADD:
1982 /* allow adding attribute via modrdn thru */
1994 if (be_null) op->o_bd = NULL;
2000 struct berval *list,
2013 while ( len >= 0 && --ix >= 0 ) {
2014 while ( --len >= 0 && *p++ != sep )
2017 while ( len >= 0 && *p == ' ' ) {
2030 while ( --len >= 0 && *p != sep ) {
2034 while ( bv->bv_len > 0 && *--p == ' ' ) {
2041 typedef struct aci_set_gather_t {
2047 aci_set_cb_gather( Operation *op, SlapReply *rs )
2049 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2051 if ( rs->sr_type == REP_SEARCH ) {
2052 BerValue bvals[ 2 ];
2053 BerVarray bvalsp = NULL;
2056 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2057 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2059 if ( desc == slap_schema.si_ad_entryDN ) {
2061 bvals[ 0 ] = rs->sr_entry->e_nname;
2062 BER_BVZERO( &bvals[ 1 ] );
2067 a = attr_find( rs->sr_entry->e_attrs, desc );
2071 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2074 bvalsp = a->a_nvals;
2080 p->bvals = slap_set_join( p->cookie, p->bvals,
2081 ( '|' | SLAP_SET_RREF ), bvalsp );
2085 assert( rs->sr_type == REP_RESULT );
2092 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2094 AciSetCookie *cp = (AciSetCookie *)cookie;
2096 LDAPURLDesc *ludp = NULL;
2097 Operation op2 = { 0 };
2098 SlapReply rs = {REP_RESULT};
2099 AttributeName anlist[ 2 ], *anlistp = NULL;
2101 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2102 aci_set_gather_t p = { 0 };
2103 const char *text = NULL;
2104 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2106 /* this routine needs to return the bervals instead of
2107 * plain strings, since syntax is not known. It should
2108 * also return the syntax or some "comparison cookie".
2110 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2111 return aci_set_gather2( cookie, name, desc );
2114 rc = ldap_url_parse( name->bv_val, &ludp );
2115 if ( rc != LDAP_URL_SUCCESS ) {
2116 rc = LDAP_PROTOCOL_ERROR;
2120 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2122 /* host part must be empty */
2123 /* extensions parts must be empty */
2124 rc = LDAP_PROTOCOL_ERROR;
2128 /* Grab the searchbase and see if an appropriate database can be found */
2129 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2130 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2131 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2132 BER_BVZERO( &op2.o_req_dn );
2133 if ( rc != LDAP_SUCCESS ) {
2137 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2138 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2139 rc = LDAP_NO_SUCH_OBJECT;
2143 /* Grab the filter */
2144 if ( ludp->lud_filter ) {
2145 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2146 cp->op->o_tmpmemctx );
2149 op2.ors_filterstr = defaultFilter_bv;
2152 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2153 if ( op2.ors_filter == NULL ) {
2154 rc = LDAP_PROTOCOL_ERROR;
2158 /* Grab the scope */
2159 op2.ors_scope = ludp->lud_scope;
2161 /* Grap the attributes */
2162 if ( ludp->lud_attrs ) {
2163 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2166 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2167 cp->op->o_tmpmemctx );
2169 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2170 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2171 anlistp[ nattrs ].an_desc = NULL;
2172 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2173 &anlistp[ nattrs ].an_desc, &text );
2174 if ( rc != LDAP_SUCCESS ) {
2183 anlistp[ nattrs ].an_name = desc->ad_cname;
2184 anlistp[ nattrs ].an_desc = desc;
2186 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2190 op2.o_hdr = cp->op->o_hdr;
2191 op2.o_tag = LDAP_REQ_SEARCH;
2192 op2.o_ndn = op2.o_bd->be_rootndn;
2193 op2.o_callback = &cb;
2194 op2.o_time = slap_get_time();
2195 op2.o_do_not_cache = 1;
2196 op2.o_is_auth_check = 0;
2197 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2198 op2.ors_slimit = SLAP_NO_LIMIT;
2199 op2.ors_tlimit = SLAP_NO_LIMIT;
2200 op2.ors_attrs = anlistp;
2201 op2.ors_attrsonly = 0;
2202 op2.o_private = cp->op->o_private;
2206 rc = op2.o_bd->be_search( &op2, &rs );
2212 if ( op2.ors_filter ) {
2213 filter_free_x( cp->op, op2.ors_filter );
2215 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2216 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2218 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2219 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2222 ldap_free_urldesc( ludp );
2224 if ( anlistp && anlistp != anlist ) {
2225 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2232 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2234 AciSetCookie *cp = (AciSetCookie *)cookie;
2235 BerVarray bvals = NULL;
2239 /* this routine needs to return the bervals instead of
2240 * plain strings, since syntax is not known. It should
2241 * also return the syntax or some "comparison cookie".
2243 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2244 if ( rc == LDAP_SUCCESS ) {
2245 if ( desc == slap_schema.si_ad_entryDN ) {
2246 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2247 cp->op->o_tmpmemctx );
2249 BER_BVZERO( &bvals[ 1 ] );
2253 backend_attribute( cp->op,
2254 cp->e, &ndn, desc, &bvals, ACL_NONE );
2257 if ( !BER_BVISNULL( &ndn ) ) {
2258 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2267 struct berval *subj,
2273 struct berval set = BER_BVNULL;
2275 AciSetCookie cookie;
2277 if ( setref == 0 ) {
2278 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2281 struct berval subjdn, ndn = BER_BVNULL;
2282 struct berval setat;
2285 AttributeDescription *desc = NULL;
2287 /* format of string is "entry/setAttrName" */
2288 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2292 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2293 setat = aci_bv_set_attr;
2297 * NOTE: dnNormalize honors the ber_len field
2298 * as the length of the dn to be normalized
2300 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2301 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2303 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2304 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2308 BER_BVZERO( &bvals[0] );
2309 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2311 bvals[0].bv_val = bvals[i-1].bv_val;
2312 BER_BVZERO( &bvals[i-1] );
2314 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2315 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2320 if ( !BER_BVISNULL( &set ) ) {
2323 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2324 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2325 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2331 #ifdef SLAPD_ACI_ENABLED
2333 aci_list_map_rights(
2334 struct berval *list )
2341 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2344 switch (*bv.bv_val) {
2346 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2349 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2350 * the right 's' to mean "set", but in the examples states
2351 * that the right 's' means "search". The latter definition
2354 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2357 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2360 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2363 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2364 * define any equivalent to the AUTH right, so I've just used
2367 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2379 struct berval *list,
2380 const struct berval *attr,
2381 struct berval *val )
2383 struct berval bv, left, right;
2386 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2387 if (aci_get_part(&bv, 0, '=', &left) < 0
2388 || aci_get_part(&bv, 1, '=', &right) < 0)
2390 if (ber_bvstrcasecmp(attr, &bv) == 0)
2392 } else if (val == NULL) {
2393 if (ber_bvstrcasecmp(attr, &left) == 0)
2396 if (ber_bvstrcasecmp(attr, &left) == 0) {
2397 /* this is experimental code that implements a
2398 * simple (prefix) match of the attribute value.
2399 * the ACI draft does not provide for aci's that
2400 * apply to specific values, but it would be
2401 * nice to have. If the <attr> part of an aci's
2402 * rights list is of the form <attr>=<value>,
2403 * that means the aci applies only to attrs with
2404 * the given value. Furthermore, if the attr is
2405 * of the form <attr>=<value>*, then <value> is
2406 * treated as a prefix, and the aci applies to
2407 * any value with that prefix.
2409 * Ideally, this would allow r.e. matches.
2411 if (aci_get_part(&right, 0, '*', &left) < 0
2412 || right.bv_len <= left.bv_len)
2414 if (ber_bvstrcasecmp(val, &right) == 0)
2416 } else if (val->bv_len >= left.bv_len) {
2417 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2426 static slap_access_t
2427 aci_list_get_attr_rights(
2428 struct berval *list,
2429 const struct berval *attr,
2430 struct berval *val )
2436 /* loop through each rights/attr pair, skip first part (action) */
2438 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2439 if (aci_list_has_attr(&bv, attr, val) == 0)
2441 if (aci_get_part(list, i, ';', &bv) < 0)
2443 mask |= aci_list_map_rights(&bv);
2449 aci_list_get_rights(
2450 struct berval *list,
2451 const struct berval *attr,
2453 slap_access_t *grant,
2454 slap_access_t *deny )
2456 struct berval perm, actn;
2457 slap_access_t *mask;
2460 if (attr == NULL || attr->bv_len == 0
2461 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2462 attr = &aci_bv_br_entry;
2468 /* loop through each permissions clause */
2469 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2470 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2472 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2474 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2481 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2482 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2489 struct berval *subj,
2490 struct berval *defgrpoc,
2491 struct berval *defgrpat,
2498 struct berval subjdn;
2499 struct berval grpoc;
2500 struct berval grpat;
2501 ObjectClass *grp_oc = NULL;
2502 AttributeDescription *grp_ad = NULL;
2506 /* format of string is "group/objectClassValue/groupAttrName" */
2507 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2511 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2515 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2519 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2520 if( rc != LDAP_SUCCESS ) {
2526 grp_oc = oc_bvfind( &grpoc );
2528 if (grp_oc != NULL && grp_ad != NULL ) {
2529 char buf[ACL_BUF_SIZE];
2530 struct berval bv, ndn;
2531 bv.bv_len = sizeof( buf ) - 1;
2532 bv.bv_val = (char *)&buf;
2533 if ( string_expand(&bv, &subjdn,
2534 e->e_ndn, nmatch, matches) )
2539 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2540 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2541 grp_oc, grp_ad ) == 0 );
2542 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2554 AttributeDescription *desc,
2558 regmatch_t *matches,
2559 slap_access_t *grant,
2560 slap_access_t *deny,
2561 slap_aci_scope_t asserted_scope
2564 struct berval bv, scope, perms, type, sdn;
2568 assert( !BER_BVISNULL( &desc->ad_cname ) );
2570 /* parse an aci of the form:
2571 oid # scope # action;rights;attr;rights;attr
2572 $ action;rights;attr;rights;attr # type # subject
2574 [NOTE: the following comment is very outdated,
2575 as the draft version it refers to (Ando, 2004-11-20)].
2577 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2578 a full description of the format for this attribute.
2579 Differences: "this" in the draft is "self" here, and
2580 "self" and "public" is in the position of type.
2582 <scope> = {entry|children|subtree}
2583 <type> = {public|users|access-id|subtree|onelevel|children|
2584 self|dnattr|group|role|set|set-ref}
2586 This routine now supports scope={ENTRY,CHILDREN}
2588 - ENTRY applies to "entry" and "subtree";
2589 - CHILDREN aplies to "children" and "subtree"
2592 /* check that the aci has all 5 components */
2593 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2597 /* check that the aci family is supported */
2598 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2602 /* check that the scope matches */
2603 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2607 /* note: scope can be either ENTRY or CHILDREN;
2608 * they respectively match "entry" and "children" in bv
2609 * both match "subtree" */
2610 switch ( asserted_scope ) {
2611 case SLAP_ACI_SCOPE_ENTRY:
2612 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2613 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2619 case SLAP_ACI_SCOPE_CHILDREN:
2620 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2621 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2631 /* get the list of permissions clauses, bail if empty */
2632 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2636 /* check if any permissions allow desired access */
2637 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2641 /* see if we have a DN match */
2642 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2646 /* see if we have a public (i.e. anonymous) access */
2647 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2651 /* otherwise require an identity */
2652 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2656 /* see if we have a users access */
2657 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2661 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2662 * just grab all the berval up to its end (ITS#3303).
2663 * NOTE: the problem could be solved by providing the DN with
2664 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
2665 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2667 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2671 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2672 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2674 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2677 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2678 if ( rc != LDAP_SUCCESS ) {
2682 if ( dn_match( &op->o_ndn, &ndn ) ) {
2685 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2689 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2692 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2693 if ( rc != LDAP_SUCCESS ) {
2697 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2700 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2704 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2705 struct berval ndn, pndn;
2707 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2708 if ( rc != LDAP_SUCCESS ) {
2712 dnParent( &ndn, &pndn );
2714 if ( dn_match( &op->o_ndn, &pndn ) ) {
2717 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2721 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2724 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2725 if ( rc != LDAP_SUCCESS ) {
2729 if ( !dn_match( &op->o_ndn, &ndn )
2730 && dnIsSuffix( &op->o_ndn, &ndn ) )
2734 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2738 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2739 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2743 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2745 AttributeDescription *ad = NULL;
2748 rc = slap_bv2ad( &sdn, &ad, &text );
2750 if( rc != LDAP_SUCCESS ) {
2756 for ( at = attrs_find( e->e_attrs, ad );
2758 at = attrs_find( at->a_next, ad ) )
2760 if ( value_find_ex( ad,
2761 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2762 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2764 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2773 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2774 if ( aci_group_member( &sdn, &aci_bv_group_class,
2775 &aci_bv_group_attr, op, e, nmatch, matches ) )
2780 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2781 if ( aci_group_member( &sdn, &aci_bv_role_class,
2782 &aci_bv_role_attr, op, e, nmatch, matches ) )
2787 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2788 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2792 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2793 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2803 * FIXME: there is a silly dependence that makes it difficult
2804 * to move ACIs in a run-time loadable module under the "dynacl"
2805 * umbrella, because sets share some helpers with ACIs.
2808 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2810 AttributeDescription *ad = NULL;
2811 const char *text = NULL;
2813 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2814 fprintf( stderr, "%s: line %d: "
2815 "inappropriate style \"%s\" in \"aci\" by clause\n",
2816 fname, lineno, sty );
2820 if ( right != NULL && *right != '\0' ) {
2821 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2823 "%s: line %d: aci \"%s\": %s\n",
2824 fname, lineno, right, text );
2829 ad = slap_schema.si_ad_aci;
2832 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2833 fprintf( stderr, "%s: line %d: "
2834 "aci \"%s\": inappropriate syntax: %s\n",
2835 fname, lineno, right,
2836 ad->ad_type->sat_syntax_oid );
2840 *privp = (void *)ad;
2846 dynacl_aci_unparse( void *priv, struct berval *bv )
2848 AttributeDescription *ad = ( AttributeDescription * )priv;
2853 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
2854 ptr = lutil_strcopy( bv->bv_val, " aci=" );
2855 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
2856 bv->bv_len = ptr - bv->bv_val;
2867 AttributeDescription *desc,
2870 regmatch_t *matches,
2871 slap_access_t *grantp,
2872 slap_access_t *denyp )
2874 AttributeDescription *ad = ( AttributeDescription * )priv;
2876 slap_access_t tgrant, tdeny, grant, deny;
2878 char accessmaskbuf[ACCESSMASK_MAXLEN];
2879 char accessmaskbuf1[ACCESSMASK_MAXLEN];
2880 #endif /* LDAP_DEBUG */
2882 /* start out with nothing granted, nothing denied */
2886 /* get the aci attribute */
2887 at = attr_find( e->e_attrs, ad );
2891 /* the aci is an multi-valued attribute. The
2892 * rights are determined by OR'ing the individual
2893 * rights given by the acis.
2895 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2896 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2897 nmatch, matches, &grant, &deny,
2898 SLAP_ACI_SCOPE_ENTRY ) != 0 )
2905 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2906 accessmask2str( tgrant, accessmaskbuf, 1 ),
2907 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2910 /* If the entry level aci didn't contain anything valid for the
2911 * current operation, climb up the tree and evaluate the
2912 * acis with scope set to subtree
2914 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2915 struct berval parent_ndn;
2918 /* to solve the chicken'n'egg problem of accessing
2919 * the OpenLDAPaci attribute, the direct access
2920 * to the entry's attribute is unchecked; however,
2921 * further accesses to OpenLDAPaci values in the
2922 * ancestors occur through backend_attribute(), i.e.
2923 * with the identity of the operation, requiring
2924 * further access checking. For uniformity, this
2925 * makes further requests occur as the rootdn, if
2926 * any, i.e. searching for the OpenLDAPaci attribute
2927 * is considered an internal search. If this is not
2928 * acceptable, then the same check needs be performed
2929 * when accessing the entry's attribute. */
2930 Operation op2 = *op;
2932 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
2933 op2.o_dn = op->o_bd->be_rootdn;
2934 op2.o_ndn = op->o_bd->be_rootndn;
2938 dnParent( &e->e_nname, &parent_ndn );
2939 while ( !BER_BVISEMPTY( &parent_ndn ) ){
2941 BerVarray bvals = NULL;
2944 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
2945 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
2954 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
2955 if ( aci_mask( op, e, desc, val,
2959 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2963 /* evaluation stops as soon as either a "deny" or a
2964 * "grant" directive matches.
2966 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
2970 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2971 accessmask2str( tgrant, accessmaskbuf, 1 ),
2972 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2976 case LDAP_NO_SUCH_ATTRIBUTE:
2977 /* just go on if the aci-Attribute is not present in
2980 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
2984 case LDAP_NO_SUCH_OBJECT:
2985 /* We have reached the base object */
2986 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
2998 dnParent( &parent_ndn, &parent_ndn );
3008 /* need to register this at some point */
3009 static slap_dynacl_t dynacl_aci = {
3019 #endif /* SLAP_DYNACL */
3021 #endif /* SLAPD_ACI_ENABLED */
3026 * dynamic ACL infrastructure
3028 static slap_dynacl_t *da_list = NULL;
3031 slap_dynacl_register( slap_dynacl_t *da )
3035 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3036 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3041 if ( tmp != NULL ) {
3045 if ( da->da_mask == NULL ) {
3049 da->da_private = NULL;
3050 da->da_next = da_list;
3056 static slap_dynacl_t *
3057 slap_dynacl_next( slap_dynacl_t *da )
3066 slap_dynacl_get( const char *name )
3070 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3071 if ( strcasecmp( da->da_name, name ) == 0 ) {
3078 #endif /* SLAP_DYNACL */
3085 slap_dynacl_t *known_dynacl[] = {
3086 #ifdef SLAPD_ACI_ENABLED
3088 #endif /* SLAPD_ACI_ENABLED */
3092 for ( i = 0; known_dynacl[ i ]; i++ ) {
3093 rc = slap_dynacl_register( known_dynacl[ i ] );
3098 #endif /* SLAP_DYNACL */
3109 regmatch_t *matches)
3117 bv->bv_val[0] = '\0';
3118 bv->bv_len--; /* leave space for lone $ */
3121 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3122 sp < pat->bv_val + pat->bv_len ; sp++ )
3124 /* did we previously see a $ */
3126 if ( flag == 1 && *sp == '$' ) {
3131 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3134 } else if ( *sp >= '0' && *sp <= '9' ) {
3142 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3143 if ( *sp >= '0' && *sp <= '9' ) {
3144 n = 10*n + ( *sp - '0' );
3148 if ( *sp != /*'{'*/ '}' ) {
3154 if ( n >= nmatch ) {
3160 i = matches[n].rm_so;
3161 l = matches[n].rm_eo;
3162 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3180 /* must have ended with a single $ */
3188 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3189 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3196 struct berval *pat, /* pattern to expand and match against */
3197 char *str, /* string to match against pattern */
3198 char *buf, /* buffer with $N expansion variables */
3199 int nmatch, /* size of the matches array */
3200 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3204 char newbuf[ACL_BUF_SIZE];
3208 bv.bv_len = sizeof( newbuf ) - 1;
3215 string_expand( &bv, pat, buf, nmatch, matches );
3216 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3218 char error[ACL_BUF_SIZE];
3219 regerror( rc, &re, error, sizeof( error ) );
3221 Debug( LDAP_DEBUG_TRACE,
3222 "compile( \"%s\", \"%s\") failed %s\n",
3223 pat->bv_val, str, error );
3227 rc = regexec( &re, str, 0, NULL, 0 );
3230 Debug( LDAP_DEBUG_TRACE,
3231 "=> regex_matches: string: %s\n", str, 0, 0 );
3232 Debug( LDAP_DEBUG_TRACE,
3233 "=> regex_matches: rc: %d %s\n",
3234 rc, !rc ? "matches" : "no matches", 0 );