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"),
58 aci_bv_anonymous = BER_BVC("anonymous"),
60 aci_bv_public = BER_BVC("public"),
61 aci_bv_users = BER_BVC("users"),
62 aci_bv_self = BER_BVC("self"),
63 aci_bv_dnattr = BER_BVC("dnattr"),
64 aci_bv_group = BER_BVC("group"),
65 aci_bv_role = BER_BVC("role"),
66 aci_bv_set = BER_BVC("set"),
67 aci_bv_set_ref = BER_BVC("set-ref"),
68 aci_bv_grant = BER_BVC("grant"),
69 aci_bv_deny = BER_BVC("deny"),
71 aci_bv_ip_eq = BER_BVC("IP="),
73 aci_bv_path_eq = BER_BVC("PATH="),
75 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
77 #endif /* LDAP_PF_LOCAL */
79 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
80 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
81 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
82 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
83 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
85 typedef enum slap_aci_scope_t {
86 SLAP_ACI_SCOPE_ENTRY = 0x1,
87 SLAP_ACI_SCOPE_CHILDREN = 0x2,
88 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
91 static AccessControl * slap_acl_get(
92 AccessControl *ac, int *count,
93 Operation *op, Entry *e,
94 AttributeDescription *desc,
96 int nmatch, regmatch_t *matches,
97 AccessControlState *state );
99 static slap_control_t slap_acl_mask(
100 AccessControl *ac, slap_mask_t *mask,
101 Operation *op, Entry *e,
102 AttributeDescription *desc,
107 AccessControlState *state );
109 #ifdef SLAPD_ACI_ENABLED
111 Operation *op, Entry *e,
112 AttributeDescription *desc,
117 slap_access_t *grant,
119 slap_aci_scope_t scope);
120 #endif /* SLAPD_ACI_ENABLED */
122 static int regex_matches(
123 struct berval *pat, char *str, char *buf,
124 int nmatch, regmatch_t *matches);
125 static int string_expand(
126 struct berval *newbuf, struct berval *pattern,
127 char *match, int nmatch, regmatch_t *matches);
129 typedef struct AciSetCookie {
134 SLAP_SET_GATHER aci_set_gather;
135 SLAP_SET_GATHER aci_set_gather2;
136 static int aci_match_set ( struct berval *subj, Operation *op,
137 Entry *e, int setref );
140 * access_allowed - check whether op->o_ndn is allowed the requested access
141 * to entry e, attribute attr, value val. if val is null, access to
142 * the whole attribute is assumed (all values).
144 * This routine loops through all access controls and calls
145 * slap_acl_mask() on each applicable access control.
146 * The loop exits when a definitive answer is reached or
147 * or no more controls remain.
154 * - can be legally called with op == NULL
155 * - can be legally called with op->o_bd == NULL
158 #ifdef SLAP_OVERLAY_ACCESS
160 slap_access_always_allowed(
163 AttributeDescription *desc,
165 slap_access_t access,
166 AccessControlState *state,
169 assert( maskp != NULL );
171 ACL_PRIV_SET( *maskp, ACL_ACCESS2PRIV( access ) );
180 AttributeDescription *desc,
182 slap_access_t access,
183 AccessControlState *state,
188 AccessControl *a = NULL;
191 char accessmaskbuf[ACCESSMASK_MAXLEN];
194 slap_control_t control;
195 slap_access_t access_level;
197 regmatch_t matches[MAXREMATCHES];
198 int st_same_attr = 0;
200 assert( op != NULL );
202 assert( desc != NULL );
203 assert( maskp != NULL );
205 access_level = ACL_LEVEL( access );
206 attr = desc->ad_cname.bv_val;
208 assert( attr != NULL );
211 if ( op->o_pb != NULL ) {
212 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
214 /* ACL plugin denied access */
218 #endif /* LDAP_SLAPI */
220 /* grant database root access */
221 if ( be_isroot( op ) ) {
222 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
223 mask = ACL_LVL_MANAGE;
228 * no-user-modification operational attributes are ignored
229 * by ACL_WRITE checking as any found here are not provided
232 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
233 && desc != slap_schema.si_ad_entry
234 && desc != slap_schema.si_ad_children )
236 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
237 " %s access granted\n",
242 /* use backend default access if no backend acls */
243 if ( op->o_bd->be_acl == NULL ) {
246 Debug( LDAP_DEBUG_ACL,
247 "=> slap_access_allowed: backend default %s "
248 "access %s to \"%s\"\n",
249 access2str( access ),
250 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
251 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
252 ret = op->o_bd->be_dfltaccess >= access_level;
254 mask = ACL_PRIV_LEVEL;
255 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
256 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
265 if ( st_same_attr ) {
266 assert( state->as_vd_acl != NULL );
268 a = state->as_vd_acl;
269 count = state->as_vd_acl_count;
270 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
271 mask = state->as_vd_acl_mask;
272 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
277 if ( state ) state->as_vi_acl = NULL;
279 ACL_PRIV_ASSIGN( mask, *maskp );
281 memset( matches, '\0', sizeof( matches ) );
284 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
285 MAXREMATCHES, matches, state ) ) != NULL )
289 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
290 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
291 (int)matches[i].rm_so, (int)matches[i].rm_eo );
292 if ( matches[i].rm_so <= matches[0].rm_eo ) {
294 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
295 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
298 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
302 if ( state->as_vi_acl == a &&
303 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
305 Debug( LDAP_DEBUG_ACL,
306 "=> slap_access_allowed: result from state (%s)\n",
308 ret = state->as_result;
311 Debug( LDAP_DEBUG_ACL,
312 "=> slap_access_allowed: no res from state (%s)\n",
318 control = slap_acl_mask( a, &mask, op,
319 e, desc, val, MAXREMATCHES, matches, count, state );
321 if ( control != ACL_BREAK ) {
325 memset( matches, '\0', sizeof( matches ) );
328 if ( ACL_IS_INVALID( mask ) ) {
329 Debug( LDAP_DEBUG_ACL,
330 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
332 ACL_PRIV_ASSIGN( mask, *maskp );
334 } else if ( control == ACL_BREAK ) {
335 Debug( LDAP_DEBUG_ACL,
336 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
341 ret = ACL_GRANT( mask, access );
343 Debug( LDAP_DEBUG_ACL,
344 "=> slap_access_allowed: %s access %s by %s\n",
345 access2str( access ), ret ? "granted" : "denied",
346 accessmask2str( mask, accessmaskbuf, 1 ) );
349 ACL_PRIV_ASSIGN( *maskp, mask );
357 AttributeDescription *desc,
359 slap_access_t access,
360 AccessControlState *state,
364 AccessControl *a = NULL;
368 char accessmaskbuf[ACCESSMASK_MAXLEN];
371 slap_control_t control;
372 slap_access_t access_level;
374 int st_same_attr = 0;
375 static AccessControlState state_init = ACL_STATE_INIT;
378 assert( desc != NULL );
380 access_level = ACL_LEVEL( access );
382 assert( access_level > ACL_NONE );
385 if ( maskp ) ACL_INVALIDATE( *maskp );
387 attr = desc->ad_cname.bv_val;
389 assert( attr != NULL );
391 if ( op && op->o_is_auth_check &&
392 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
398 if ( state->as_vd_ad == desc ) {
399 if ( state->as_recorded ) {
400 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
403 return state->as_result;
405 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
406 val != NULL && state->as_vd_acl == NULL )
408 return state->as_result;
416 state->as_vd_ad = desc;
419 Debug( LDAP_DEBUG_ACL,
420 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
421 access2str( access ), e->e_dn, attr );
428 if ( op->o_bd == NULL ) {
429 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
434 * FIXME: experimental; use first backend rules
435 * iff there is no global_acl (ITS#3100) */
436 if ( frontendDB->be_acl != NULL ) {
437 op->o_bd = frontendDB;
439 #endif /* LDAP_DEVEL */
441 assert( op->o_bd != NULL );
443 /* this is enforced in backend_add() */
444 if ( op->o_bd->bd_info->bi_access_allowed ) {
445 /* delegate to backend */
446 ret = op->o_bd->bd_info->bi_access_allowed( op, e, desc, val, access, state, &mask );
450 ret = slap_access_allowed( op, e, desc, val, access, state, &mask );
454 if ( ACL_IS_INVALID( mask ) ) {
455 Debug( LDAP_DEBUG_ACL,
456 "=> access_allowed: \"%s\" (%s) invalid!\n",
460 } else if ( control == ACL_BREAK ) {
461 Debug( LDAP_DEBUG_ACL,
462 "=> access_allowed: no more rules\n", 0, 0, 0 );
467 ret = ACL_GRANT( mask, access );
470 Debug( LDAP_DEBUG_ACL,
471 "=> access_allowed: %s access %s by %s\n",
472 access2str( access ), ret ? "granted" : "denied",
473 accessmask2str( mask, accessmaskbuf, 1 ) );
476 if ( state != NULL ) {
477 /* If not value-dependent, save ACL in case of more attrs */
478 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
479 state->as_vi_acl = a;
480 state->as_result = ret;
482 state->as_recorded |= ACL_STATE_RECORDED;
484 if ( be_null ) op->o_bd = NULL;
485 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
489 #else /* !SLAP_OVERLAY_ACCESS */
495 AttributeDescription *desc,
497 slap_access_t access,
498 AccessControlState *state,
503 AccessControl *a = NULL;
508 char accessmaskbuf[ACCESSMASK_MAXLEN];
511 slap_control_t control;
512 slap_access_t access_level;
514 regmatch_t matches[MAXREMATCHES];
515 int st_same_attr = 0;
516 static AccessControlState state_init = ACL_STATE_INIT;
519 assert( desc != NULL );
521 access_level = ACL_LEVEL( access );
523 assert( access_level > ACL_NONE );
524 if ( maskp ) ACL_INVALIDATE( *maskp );
526 attr = desc->ad_cname.bv_val;
528 assert( attr != NULL );
530 if ( op && op->o_is_auth_check &&
531 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
537 if ( state->as_vd_ad == desc ) {
538 if ( state->as_recorded ) {
539 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
542 return state->as_result;
544 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
545 val != NULL && state->as_vd_acl == NULL )
547 return state->as_result;
555 state->as_vd_ad=desc;
558 Debug( LDAP_DEBUG_ACL,
559 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
560 access2str( access ), e->e_dn, attr );
569 be = LDAP_STAILQ_FIRST(&backendDB);
573 * FIXME: experimental; use first backend rules
574 * iff there is no global_acl (ITS#3100) */
575 if ( frontendDB->be_acl == NULL )
581 assert( be != NULL );
584 if ( op->o_pb != NULL ) {
585 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
587 /* ACL plugin denied access */
591 #endif /* LDAP_SLAPI */
593 /* grant database root access */
594 if ( be_isroot( op ) ) {
595 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
597 mask = ACL_LVL_MANAGE;
604 * no-user-modification operational attributes are ignored
605 * by ACL_WRITE checking as any found here are not provided
608 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
609 && desc != slap_schema.si_ad_entry
610 && desc != slap_schema.si_ad_children )
612 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
613 " %s access granted\n",
618 /* use backend default access if no backend acls */
619 if ( be->be_acl == NULL ) {
620 Debug( LDAP_DEBUG_ACL,
621 "=> access_allowed: backend default %s "
622 "access %s to \"%s\"\n",
623 access2str( access ),
624 be->be_dfltaccess >= access_level ? "granted" : "denied",
625 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
626 ret = be->be_dfltaccess >= access_level;
631 mask = ACL_PRIV_LEVEL;
632 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
633 mask |= ACL_ACCESS2PRIV( i );
640 /* be is always non-NULL */
641 /* use global default access if no global acls */
642 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
643 Debug( LDAP_DEBUG_ACL,
644 "=> access_allowed: global default %s access %s to \"%s\"\n",
645 access2str( access ),
646 frontendDB->be_dfltaccess >= access_level ?
647 "granted" : "denied", op->o_dn.bv_val );
648 ret = frontendDB->be_dfltaccess >= access_level;
653 mask = ACL_PRIV_LEVEL;
654 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
655 mask |= ACL_ACCESS2PRIV( i );
666 if ( st_same_attr ) {
667 assert( state->as_vd_acl != NULL );
669 a = state->as_vd_acl;
670 count = state->as_vd_acl_count;
671 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
672 mask = state->as_vd_acl_mask;
673 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
678 if ( state ) state->as_vi_acl = NULL;
682 memset( matches, '\0', sizeof(matches) );
685 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
686 MAXREMATCHES, matches, state ) ) != NULL )
690 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
691 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
692 (int)matches[i].rm_so, (int)matches[i].rm_eo );
693 if ( matches[i].rm_so <= matches[0].rm_eo ) {
695 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
696 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
699 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
703 if ( state->as_vi_acl == a &&
704 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
706 Debug( LDAP_DEBUG_ACL,
707 "access_allowed: result from state (%s)\n",
709 ret = state->as_result;
712 Debug( LDAP_DEBUG_ACL,
713 "access_allowed: no res from state (%s)\n",
719 control = slap_acl_mask( a, &mask, op,
720 e, desc, val, MAXREMATCHES, matches, count, state );
722 if ( control != ACL_BREAK ) {
726 memset( matches, '\0', sizeof(matches) );
729 if ( ACL_IS_INVALID( mask ) ) {
730 Debug( LDAP_DEBUG_ACL,
731 "=> access_allowed: \"%s\" (%s) invalid!\n",
735 } else if ( control == ACL_BREAK ) {
736 Debug( LDAP_DEBUG_ACL,
737 "=> access_allowed: no more rules\n", 0, 0, 0 );
742 Debug( LDAP_DEBUG_ACL,
743 "=> access_allowed: %s access %s by %s\n",
744 access2str( access ),
745 ACL_GRANT(mask, access) ? "granted" : "denied",
746 accessmask2str( mask, accessmaskbuf, 1 ) );
748 ret = ACL_GRANT(mask, access);
751 if ( state != NULL ) {
752 /* If not value-dependent, save ACL in case of more attrs */
753 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
754 state->as_vi_acl = a;
755 state->as_result = ret;
757 state->as_recorded |= ACL_STATE_RECORDED;
759 if ( be_null ) op->o_bd = NULL;
760 if ( maskp ) *maskp = mask;
764 #endif /* SLAP_OVERLAY_ACCESS */
767 * slap_acl_get - return the acl applicable to entry e, attribute
768 * attr. the acl returned is suitable for use in subsequent calls to
769 * acl_access_allowed().
772 static AccessControl *
778 AttributeDescription *desc,
782 AccessControlState *state )
789 assert( count != NULL );
790 assert( desc != NULL );
792 attr = desc->ad_cname.bv_val;
794 assert( attr != NULL );
797 if( op->o_bd == NULL ) {
798 a = frontendDB->be_acl;
800 a = op->o_bd->be_acl;
811 dnlen = e->e_nname.bv_len;
813 for ( ; a != NULL; a = a->acl_next ) {
816 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
817 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
818 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
819 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
820 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
824 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
825 *count, a->acl_dn_pat.bv_val, 0 );
826 patlen = a->acl_dn_pat.bv_len;
827 if ( dnlen < patlen )
830 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
831 /* base dn -- entire object DN must match */
832 if ( dnlen != patlen )
835 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
836 int rdnlen = -1, sep = 0;
838 if ( dnlen <= patlen )
842 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
847 rdnlen = dn_rdnlen( NULL, &e->e_nname );
848 if ( rdnlen != dnlen - patlen - sep )
851 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
852 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
855 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
856 if ( dnlen <= patlen )
858 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
862 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
866 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
870 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
871 matches[0].rm_so = matches[0].rm_eo = -1;
875 /* Is this ACL only for a specific value? */
876 if ( a->acl_attrval.bv_len ) {
881 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
882 state->as_recorded |= ACL_STATE_RECORDED_VD;
883 state->as_vd_acl = a;
884 state->as_vd_acl_count = *count;
885 state->as_vd_access = a->acl_access;
886 state->as_vd_access_count = 1;
887 ACL_INVALIDATE( state->as_vd_acl_mask );
890 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
891 Debug( LDAP_DEBUG_ACL,
892 "acl_get: valpat %s\n",
893 a->acl_attrval.bv_val, 0, 0 );
894 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
902 Debug( LDAP_DEBUG_ACL,
904 a->acl_attrval.bv_val, 0, 0 );
906 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
907 if (value_match( &match, desc,
908 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
909 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
916 patlen = a->acl_attrval.bv_len;
917 vdnlen = val->bv_len;
919 if ( vdnlen < patlen )
922 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
923 if ( vdnlen > patlen )
926 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
929 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
932 rdnlen = dn_rdnlen( NULL, val );
933 if ( rdnlen != vdnlen - patlen - 1 )
936 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
937 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
940 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
941 if ( vdnlen <= patlen )
944 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
948 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
954 if ( a->acl_filter != NULL ) {
955 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
956 if ( rc != LDAP_COMPARE_TRUE ) {
961 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
966 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
978 struct berval *opndn )
981 * if access applies to the entry itself, and the
982 * user is bound as somebody in the same namespace as
983 * the entry, OR the given dn matches the dn pattern
986 * NOTE: styles "anonymous", "users" and "self"
987 * have been moved to enum slap_style_t, whose
988 * value is set in a_dn_style; however, the string
989 * is maintaned in a_dn_pat.
991 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
992 if ( !BER_BVISEMPTY( opndn ) ) {
996 } else if ( b->a_style == ACL_STYLE_USERS ) {
997 if ( BER_BVISEMPTY( opndn ) ) {
1001 } else if ( b->a_style == ACL_STYLE_SELF ) {
1002 struct berval ndn, selfndn;
1005 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1009 level = b->a_self_level;
1017 selfndn = e->e_nname;
1020 for ( ; level > 0; level-- ) {
1021 if ( BER_BVISEMPTY( &ndn ) ) {
1024 dnParent( &ndn, &ndn );
1027 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1032 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1033 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1035 regmatch_t tmp_matches[2],
1036 *tmp_matchesp = tmp_matches;
1040 switch ( a->acl_dn_style ) {
1041 case ACL_STYLE_REGEX:
1042 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1043 tmp_matchesp = matches;
1044 tmp_nmatch = nmatch;
1047 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1049 case ACL_STYLE_BASE:
1050 tmp_matches[0].rm_so = 0;
1051 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1056 case ACL_STYLE_SUBTREE:
1057 case ACL_STYLE_CHILDREN:
1058 tmp_matches[0].rm_so = 0;
1059 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1060 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1061 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1075 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1076 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1084 ber_len_t patlen, odnlen;
1087 if ( e->e_dn == NULL )
1090 if ( b->a_expand ) {
1092 char buf[ACL_BUF_SIZE];
1095 regmatch_t tmp_matches[2],
1096 *tmp_matchesp = tmp_matches;
1100 bv.bv_len = sizeof( buf ) - 1;
1103 switch ( a->acl_dn_style ) {
1104 case ACL_STYLE_REGEX:
1105 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1106 tmp_matchesp = matches;
1107 tmp_nmatch = nmatch;
1110 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1112 case ACL_STYLE_BASE:
1113 tmp_matches[0].rm_so = 0;
1114 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1119 case ACL_STYLE_SUBTREE:
1120 case ACL_STYLE_CHILDREN:
1121 tmp_matches[0].rm_so = 0;
1122 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1123 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1124 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1138 if ( string_expand( &bv, &b->a_pat,
1140 tmp_nmatch, tmp_matchesp ) )
1145 if ( dnNormalize(0, NULL, NULL, &bv,
1146 &pat, op->o_tmpmemctx )
1149 /* did not expand to a valid dn */
1157 patlen = pat.bv_len;
1158 odnlen = opndn->bv_len;
1159 if ( odnlen < patlen ) {
1160 goto dn_match_cleanup;
1164 if ( b->a_style == ACL_STYLE_BASE ) {
1165 /* base dn -- entire object DN must match */
1166 if ( odnlen != patlen ) {
1167 goto dn_match_cleanup;
1170 } else if ( b->a_style == ACL_STYLE_ONE ) {
1173 if ( odnlen <= patlen ) {
1174 goto dn_match_cleanup;
1177 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1178 goto dn_match_cleanup;
1181 rdnlen = dn_rdnlen( NULL, opndn );
1182 if ( rdnlen != odnlen - patlen - 1 ) {
1183 goto dn_match_cleanup;
1186 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1187 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1188 goto dn_match_cleanup;
1191 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1192 if ( odnlen <= patlen ) {
1193 goto dn_match_cleanup;
1196 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1197 goto dn_match_cleanup;
1200 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1204 if ( odnlen <= patlen ) {
1205 goto dn_match_cleanup;
1208 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1210 goto dn_match_cleanup;
1215 for ( ; level > 0; level-- ) {
1216 if ( BER_BVISEMPTY( &ndn ) ) {
1217 goto dn_match_cleanup;
1219 dnParent( &ndn, &ndn );
1220 if ( ndn.bv_len < patlen ) {
1221 goto dn_match_cleanup;
1225 if ( ndn.bv_len != patlen ) {
1226 goto dn_match_cleanup;
1230 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1233 if ( pat.bv_val != b->a_pat.bv_val ) {
1234 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1246 * Record value-dependent access control state
1248 #define ACL_RECORD_VALUE_STATE do { \
1249 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1250 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1251 state->as_vd_acl = a; \
1252 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1253 sizeof( state->as_vd_acl_matches )) ; \
1254 state->as_vd_acl_count = count; \
1255 state->as_vd_access = b; \
1256 state->as_vd_access_count = i; \
1268 regmatch_t *matches,
1270 AccessControlState *state,
1271 slap_dn_access *bdn,
1272 struct berval *opndn )
1278 const char *attr = bdn->a_at->ad_cname.bv_val;
1280 assert( attr != NULL );
1282 if ( BER_BVISEMPTY( opndn ) ) {
1286 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1289 /* see if asker is listed in dnattr */
1290 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1292 at = attrs_find( at->a_next, bdn->a_at ) )
1294 if ( value_find_ex( bdn->a_at,
1295 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1296 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1298 &bv, op->o_tmpmemctx ) == 0 )
1307 /* have a dnattr match. if this is a self clause then
1308 * the target must also match the op dn.
1310 if ( bdn->a_self ) {
1311 /* check if the target is an attribute. */
1312 if ( val == NULL ) return 1;
1314 /* target is attribute, check if the attribute value
1317 rc = value_match( &match, bdn->a_at,
1318 bdn->a_at->ad_type->sat_equality, 0,
1320 /* on match error or no match, fail the ACL clause */
1321 if ( rc != LDAP_SUCCESS || match != 0 )
1326 /* no dnattr match, check if this is a self clause */
1327 if ( ! bdn->a_self )
1330 ACL_RECORD_VALUE_STATE;
1332 /* this is a self clause, check if the target is an
1338 /* target is attribute, check if the attribute value
1341 rc = value_match( &match, bdn->a_at,
1342 bdn->a_at->ad_type->sat_equality, 0,
1345 /* on match error or no match, fail the ACL clause */
1346 if ( rc != LDAP_SUCCESS || match != 0 )
1355 * slap_acl_mask - modifies mask based upon the given acl and the
1356 * requested access to entry e, attribute attr, value val. if val
1357 * is null, access to the whole attribute is assumed (all values).
1359 * returns 0 access NOT allowed
1363 static slap_control_t
1369 AttributeDescription *desc,
1372 regmatch_t *matches,
1374 AccessControlState *state )
1379 char accessmaskbuf[ACCESSMASK_MAXLEN];
1380 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1381 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1382 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1386 assert( a != NULL );
1387 assert( mask != NULL );
1388 assert( desc != NULL );
1390 attr = desc->ad_cname.bv_val;
1392 assert( attr != NULL );
1394 Debug( LDAP_DEBUG_ACL,
1395 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1398 Debug( LDAP_DEBUG_ACL,
1399 "=> acl_mask: to %s by \"%s\", (%s) \n",
1400 val ? "value" : "all values",
1401 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1402 accessmask2str( *mask, accessmaskbuf, 1 ) );
1405 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1406 && state->as_vd_acl == a )
1408 b = state->as_vd_access;
1409 i = state->as_vd_access_count;
1416 for ( ; b != NULL; b = b->a_next, i++ ) {
1417 slap_mask_t oldmask, modmask;
1419 ACL_INVALIDATE( modmask );
1421 /* AND <who> clauses */
1422 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1423 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1424 b->a_dn_pat.bv_val, 0, 0);
1426 * if access applies to the entry itself, and the
1427 * user is bound as somebody in the same namespace as
1428 * the entry, OR the given dn matches the dn pattern
1431 * NOTE: styles "anonymous", "users" and "self"
1432 * have been moved to enum slap_style_t, whose
1433 * value is set in a_dn_style; however, the string
1434 * is maintaned in a_dn_pat.
1437 if ( acl_mask_dn( op, e, a, nmatch, matches,
1438 &b->a_dn, &op->o_ndn ) )
1444 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1447 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1448 b->a_realdn_pat.bv_val, 0, 0);
1450 * if access applies to the entry itself, and the
1451 * user is bound as somebody in the same namespace as
1452 * the entry, OR the given dn matches the dn pattern
1455 * NOTE: styles "anonymous", "users" and "self"
1456 * have been moved to enum slap_style_t, whose
1457 * value is set in a_dn_style; however, the string
1458 * is maintaned in a_dn_pat.
1461 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1463 ndn = op->o_conn->c_ndn;
1468 if ( acl_mask_dn( op, e, a, nmatch, matches,
1469 &b->a_realdn, &ndn ) )
1475 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1476 if ( ! op->o_conn->c_listener ) {
1479 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1480 b->a_sockurl_pat.bv_val, 0, 0 );
1482 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1483 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1484 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1485 e->e_ndn, nmatch, matches ) )
1490 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1492 char buf[ACL_BUF_SIZE];
1494 bv.bv_len = sizeof( buf ) - 1;
1496 if ( string_expand( &bv, &b->a_sockurl_pat,
1497 e->e_ndn, nmatch, matches ) )
1502 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1508 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1516 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1517 if ( !op->o_conn->c_peer_domain.bv_val ) {
1520 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1521 b->a_domain_pat.bv_val, 0, 0 );
1522 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1523 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1524 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1525 e->e_ndn, nmatch, matches ) )
1530 char buf[ACL_BUF_SIZE];
1532 struct berval cmp = op->o_conn->c_peer_domain;
1533 struct berval pat = b->a_domain_pat;
1535 if ( b->a_domain_expand ) {
1538 bv.bv_len = sizeof(buf) - 1;
1541 if ( string_expand(&bv, &b->a_domain_pat,
1542 e->e_ndn, nmatch, matches) )
1549 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1550 int offset = cmp.bv_len - pat.bv_len;
1555 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1559 /* trim the domain */
1560 cmp.bv_val = &cmp.bv_val[ offset ];
1561 cmp.bv_len -= offset;
1564 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1571 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1572 if ( !op->o_conn->c_peer_name.bv_val ) {
1575 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1576 b->a_peername_pat.bv_val, 0, 0 );
1577 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1578 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1579 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1580 e->e_ndn, nmatch, matches ) )
1586 /* try exact match */
1587 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1588 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1592 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1594 char buf[ACL_BUF_SIZE];
1596 bv.bv_len = sizeof( buf ) - 1;
1598 if ( string_expand( &bv, &b->a_peername_pat,
1599 e->e_ndn, nmatch, matches ) )
1604 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1608 /* extract IP and try exact match */
1609 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1611 char buf[] = "255.255.255.255";
1614 int port_number = -1;
1616 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1617 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1620 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1621 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1623 port = strrchr( ip.bv_val, ':' );
1627 ip.bv_len = port - ip.bv_val;
1629 port_number = strtol( port, &next, 10 );
1630 if ( next[0] != '\0' )
1634 /* the port check can be anticipated here */
1635 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1638 /* address longer than expected? */
1639 if ( ip.bv_len >= sizeof(buf) )
1642 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1643 buf[ ip.bv_len ] = '\0';
1645 addr = inet_addr( buf );
1647 /* unable to convert? */
1648 if ( addr == (unsigned long)(-1) )
1651 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1654 #ifdef LDAP_PF_LOCAL
1655 /* extract path and try exact match */
1656 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1659 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1660 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1663 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1664 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1666 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1669 #endif /* LDAP_PF_LOCAL */
1671 /* exact match (very unlikely...) */
1672 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1679 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1680 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1683 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1684 b->a_sockname_pat.bv_val, 0, 0 );
1685 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1686 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1687 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1688 e->e_ndn, nmatch, matches ) )
1693 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1695 char buf[ACL_BUF_SIZE];
1697 bv.bv_len = sizeof( buf ) - 1;
1699 if ( string_expand( &bv, &b->a_sockname_pat,
1700 e->e_ndn, nmatch, matches ) )
1705 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1710 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1717 if ( b->a_dn_at != NULL ) {
1718 if ( acl_mask_dnattr( op, e, val, a, b, i,
1719 matches, count, state,
1720 &b->a_dn, &op->o_ndn ) )
1726 if ( b->a_realdn_at != NULL ) {
1729 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1731 ndn = op->o_conn->c_ndn;
1736 if ( acl_mask_dnattr( op, e, val, a, b, i,
1737 matches, count, state,
1738 &b->a_realdn, &ndn ) )
1744 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1746 struct berval ndn = BER_BVNULL;
1749 if ( op->o_ndn.bv_len == 0 ) {
1753 /* b->a_group is an unexpanded entry name, expanded it should be an
1754 * entry with objectclass group* and we test to see if odn is one of
1755 * the values in the attribute group
1757 /* see if asker is listed in dnattr */
1758 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1759 char buf[ACL_BUF_SIZE];
1761 regmatch_t tmp_matches[2],
1762 *tmp_matchesp = tmp_matches;
1764 bv.bv_len = sizeof(buf) - 1;
1769 switch ( a->acl_dn_style ) {
1770 case ACL_STYLE_REGEX:
1771 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1772 tmp_matchesp = matches;
1773 tmp_nmatch = nmatch;
1777 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1778 case ACL_STYLE_BASE:
1779 tmp_matches[0].rm_so = 0;
1780 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1785 case ACL_STYLE_SUBTREE:
1786 case ACL_STYLE_CHILDREN:
1787 tmp_matches[0].rm_so = 0;
1788 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1789 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1790 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1804 if ( string_expand( &bv, &b->a_group_pat,
1806 tmp_nmatch, tmp_matchesp ) )
1811 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1812 op->o_tmpmemctx ) != LDAP_SUCCESS )
1814 /* did not expand to a valid dn */
1821 bv = b->a_group_pat;
1824 rc = backend_group( op, e, &bv, &op->o_ndn,
1825 b->a_group_oc, b->a_group_at );
1828 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1836 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1838 char buf[ACL_BUF_SIZE];
1840 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1842 regmatch_t tmp_matches[2],
1843 *tmp_matchesp = tmp_matches;
1846 bv.bv_len = sizeof( buf ) - 1;
1851 switch ( a->acl_dn_style ) {
1852 case ACL_STYLE_REGEX:
1853 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1854 tmp_matchesp = matches;
1855 tmp_nmatch = nmatch;
1859 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1860 case ACL_STYLE_BASE:
1861 tmp_matches[0].rm_so = 0;
1862 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1867 case ACL_STYLE_SUBTREE:
1868 case ACL_STYLE_CHILDREN:
1869 tmp_matches[0].rm_so = 0;
1870 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1871 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1872 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1886 if ( string_expand( &bv, &b->a_set_pat,
1888 tmp_nmatch, tmp_matchesp ) )
1897 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1902 if ( b->a_authz.sai_ssf ) {
1903 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1904 b->a_authz.sai_ssf, op->o_ssf, 0 );
1905 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1910 if ( b->a_authz.sai_transport_ssf ) {
1911 Debug( LDAP_DEBUG_ACL,
1912 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1913 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1914 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1919 if ( b->a_authz.sai_tls_ssf ) {
1920 Debug( LDAP_DEBUG_ACL,
1921 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1922 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1923 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1928 if ( b->a_authz.sai_sasl_ssf ) {
1929 Debug( LDAP_DEBUG_ACL,
1930 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1931 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1932 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1938 if ( b->a_dynacl ) {
1940 slap_access_t tgrant, tdeny;
1942 /* this case works different from the others above.
1943 * since aci's themselves give permissions, we need
1944 * to first check b->a_access_mask, the ACL's access level.
1946 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1947 /* no ACIs in the root DSE */
1951 /* first check if the right being requested
1952 * is allowed by the ACL clause.
1954 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1958 /* start out with nothing granted, nothing denied */
1962 for ( da = b->a_dynacl; da; da = da->da_next ) {
1963 slap_access_t grant, deny;
1965 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1971 /* remove anything that the ACL clause does not allow */
1972 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1973 tdeny &= ACL_PRIV_MASK;
1975 /* see if we have anything to contribute */
1976 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1980 /* this could be improved by changing slap_acl_mask so that it can deal with
1981 * by clauses that return grant/deny pairs. Right now, it does either
1982 * additive or subtractive rights, but not both at the same time. So,
1983 * we need to combine the grant/deny pair into a single rights mask in
1984 * a smart way: if either grant or deny is "empty", then we use the
1985 * opposite as is, otherwise we remove any denied rights from the grant
1986 * rights mask and construct an additive mask.
1988 if (ACL_IS_INVALID(tdeny)) {
1989 modmask = tgrant | ACL_PRIV_ADDITIVE;
1991 } else if (ACL_IS_INVALID(tgrant)) {
1992 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1995 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1999 #else /* !SLAP_DYNACL */
2001 #ifdef SLAPD_ACI_ENABLED
2002 if ( b->a_aci_at != NULL ) {
2004 slap_access_t grant, deny, tgrant, tdeny;
2005 struct berval parent_ndn;
2006 BerVarray bvals = NULL;
2009 /* this case works different from the others above.
2010 * since aci's themselves give permissions, we need
2011 * to first check b->a_access_mask, the ACL's access level.
2014 if ( BER_BVISEMPTY( &e->e_nname ) ) {
2015 /* no ACIs in the root DSE */
2019 /* first check if the right being requested
2020 * is allowed by the ACL clause.
2022 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2025 /* start out with nothing granted, nothing denied */
2029 /* get the aci attribute */
2030 at = attr_find( e->e_attrs, b->a_aci_at );
2033 /* FIXME: this breaks acl caching;
2034 * see also ACL_RECORD_VALUE_STATE below */
2035 ACL_RECORD_VALUE_STATE;
2037 /* the aci is an multi-valued attribute. The
2038 * rights are determined by OR'ing the individual
2039 * rights given by the acis.
2041 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2046 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2052 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2053 accessmask2str(tgrant, accessmaskbuf, 1),
2054 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2057 /* If the entry level aci didn't contain anything valid for the
2058 * current operation, climb up the tree and evaluate the
2059 * acis with scope set to subtree
2061 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2062 dnParent( &e->e_nname, &parent_ndn );
2063 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2064 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2065 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2073 for( i = 0; bvals[i].bv_val != NULL; i++){
2075 /* FIXME: this breaks acl caching;
2076 * see also ACL_RECORD_VALUE_STATE above */
2077 ACL_RECORD_VALUE_STATE;
2079 if (aci_mask(op, e, desc, val, &bvals[i],
2081 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2085 /* evaluation stops as soon as either a "deny" or a
2086 * "grant" directive matches.
2088 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2092 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2093 accessmask2str(tgrant, accessmaskbuf, 1),
2094 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2098 case LDAP_NO_SUCH_ATTRIBUTE:
2099 /* just go on if the aci-Attribute is not present in
2102 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2106 case LDAP_NO_SUCH_OBJECT:
2107 /* We have reached the base object */
2108 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2119 dnParent( &parent_ndn, &parent_ndn );
2124 /* remove anything that the ACL clause does not allow */
2125 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2126 tdeny &= ACL_PRIV_MASK;
2128 /* see if we have anything to contribute */
2129 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2133 /* this could be improved by changing slap_acl_mask so that it can deal with
2134 * by clauses that return grant/deny pairs. Right now, it does either
2135 * additive or subtractive rights, but not both at the same time. So,
2136 * we need to combine the grant/deny pair into a single rights mask in
2137 * a smart way: if either grant or deny is "empty", then we use the
2138 * opposite as is, otherwise we remove any denied rights from the grant
2139 * rights mask and construct an additive mask.
2141 if (ACL_IS_INVALID(tdeny)) {
2142 modmask = tgrant | ACL_PRIV_ADDITIVE;
2144 } else if (ACL_IS_INVALID(tgrant)) {
2145 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2148 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2152 #endif /* SLAPD_ACI_ENABLED */
2153 #endif /* !SLAP_DYNACL */
2155 modmask = b->a_access_mask;
2158 Debug( LDAP_DEBUG_ACL,
2159 "<= acl_mask: [%d] applying %s (%s)\n",
2160 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2161 b->a_type == ACL_CONTINUE
2163 : b->a_type == ACL_BREAK
2169 if( ACL_IS_ADDITIVE(modmask) ) {
2171 ACL_PRIV_SET( *mask, modmask );
2174 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2176 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2177 /* substract privs */
2178 ACL_PRIV_CLR( *mask, modmask );
2181 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2188 Debug( LDAP_DEBUG_ACL,
2189 "<= acl_mask: [%d] mask: %s\n",
2190 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2192 if( b->a_type == ACL_CONTINUE ) {
2195 } else if ( b->a_type == ACL_BREAK ) {
2203 /* implicit "by * none" clause */
2206 Debug( LDAP_DEBUG_ACL,
2207 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2208 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2213 * acl_check_modlist - check access control on the given entry to see if
2214 * it allows the given modifications by the user associated with op.
2215 * returns 1 if mods allowed ok
2216 * 0 mods not allowed
2223 Modifications *mlist
2227 AccessControlState state = ACL_STATE_INIT;
2230 int ret = 1; /* default is access allowed */
2234 be = LDAP_STAILQ_FIRST(&backendDB);
2238 assert( be != NULL );
2240 /* short circuit root database access */
2241 if ( be_isroot( op ) ) {
2242 Debug( LDAP_DEBUG_ACL,
2243 "<= acl_access_allowed: granted to database root\n",
2248 /* use backend default access if no backend acls */
2249 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2250 Debug( LDAP_DEBUG_ACL,
2251 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2252 access2str( ACL_WRITE ),
2253 op->o_bd->be_dfltaccess >= ACL_WRITE
2254 ? "granted" : "denied",
2256 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2260 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2262 * Internal mods are ignored by ACL_WRITE checking
2264 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2265 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2266 " modify access granted\n",
2267 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2272 * no-user-modification operational attributes are ignored
2273 * by ACL_WRITE checking as any found here are not provided
2276 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2277 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2278 " modify access granted\n",
2279 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2283 switch ( mlist->sml_op ) {
2284 case LDAP_MOD_REPLACE:
2286 * We must check both permission to delete the whole
2287 * attribute and permission to add the specific attributes.
2288 * This prevents abuse from selfwriters.
2290 if ( ! access_allowed( op, e,
2291 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2297 if ( mlist->sml_values == NULL ) break;
2299 /* fall thru to check value to add */
2302 assert( mlist->sml_values != NULL );
2304 for ( bv = mlist->sml_nvalues
2305 ? mlist->sml_nvalues : mlist->sml_values;
2306 bv->bv_val != NULL; bv++ )
2308 if ( ! access_allowed( op, e,
2309 mlist->sml_desc, bv, ACL_WADD, &state ) )
2317 case LDAP_MOD_DELETE:
2318 if ( mlist->sml_values == NULL ) {
2319 if ( ! access_allowed( op, e,
2320 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2327 for ( bv = mlist->sml_nvalues
2328 ? mlist->sml_nvalues : mlist->sml_values;
2329 bv->bv_val != NULL; bv++ )
2331 if ( ! access_allowed( op, e,
2332 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2340 case SLAP_MOD_SOFTADD:
2341 /* allow adding attribute via modrdn thru */
2353 if (be_null) op->o_bd = NULL;
2359 struct berval *list,
2372 while ( len >= 0 && --ix >= 0 ) {
2373 while ( --len >= 0 && *p++ != sep )
2376 while ( len >= 0 && *p == ' ' ) {
2389 while ( --len >= 0 && *p != sep ) {
2393 while ( bv->bv_len > 0 && *--p == ' ' ) {
2400 typedef struct aci_set_gather_t {
2406 aci_set_cb_gather( Operation *op, SlapReply *rs )
2408 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2410 if ( rs->sr_type == REP_SEARCH ) {
2411 BerValue bvals[ 2 ];
2412 BerVarray bvalsp = NULL;
2415 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2416 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2418 if ( desc == slap_schema.si_ad_entryDN ) {
2420 bvals[ 0 ] = rs->sr_entry->e_nname;
2421 BER_BVZERO( &bvals[ 1 ] );
2426 a = attr_find( rs->sr_entry->e_attrs, desc );
2430 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2433 bvalsp = a->a_nvals;
2439 p->bvals = slap_set_join( p->cookie, p->bvals,
2440 ( '|' | SLAP_SET_RREF ), bvalsp );
2444 assert( rs->sr_type == REP_RESULT );
2451 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2453 AciSetCookie *cp = (AciSetCookie *)cookie;
2455 LDAPURLDesc *ludp = NULL;
2456 Operation op2 = { 0 };
2457 SlapReply rs = {REP_RESULT};
2458 AttributeName anlist[ 2 ], *anlistp = NULL;
2460 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2461 aci_set_gather_t p = { 0 };
2462 const char *text = NULL;
2463 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2465 /* this routine needs to return the bervals instead of
2466 * plain strings, since syntax is not known. It should
2467 * also return the syntax or some "comparison cookie".
2469 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2470 return aci_set_gather2( cookie, name, desc );
2473 rc = ldap_url_parse( name->bv_val, &ludp );
2474 if ( rc != LDAP_URL_SUCCESS ) {
2475 rc = LDAP_PROTOCOL_ERROR;
2479 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2481 /* host part must be empty */
2482 /* extensions parts must be empty */
2483 rc = LDAP_PROTOCOL_ERROR;
2487 /* Grab the searchbase and see if an appropriate database can be found */
2488 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2489 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2490 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2491 BER_BVZERO( &op2.o_req_dn );
2492 if ( rc != LDAP_SUCCESS ) {
2496 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2497 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2498 rc = LDAP_NO_SUCH_OBJECT;
2502 /* Grab the filter */
2503 if ( ludp->lud_filter ) {
2504 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2505 cp->op->o_tmpmemctx );
2508 op2.ors_filterstr = defaultFilter_bv;
2511 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2512 if ( op2.ors_filter == NULL ) {
2513 rc = LDAP_PROTOCOL_ERROR;
2517 /* Grab the scope */
2518 op2.ors_scope = ludp->lud_scope;
2520 /* Grap the attributes */
2521 if ( ludp->lud_attrs ) {
2522 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2525 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2526 cp->op->o_tmpmemctx );
2528 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2529 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2530 anlistp[ nattrs ].an_desc = NULL;
2531 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2532 &anlistp[ nattrs ].an_desc, &text );
2533 if ( rc != LDAP_SUCCESS ) {
2542 anlistp[ nattrs ].an_name = desc->ad_cname;
2543 anlistp[ nattrs ].an_desc = desc;
2545 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2549 op2.o_hdr = cp->op->o_hdr;
2550 op2.o_tag = LDAP_REQ_SEARCH;
2551 op2.o_ndn = op2.o_bd->be_rootndn;
2552 op2.o_callback = &cb;
2553 op2.o_time = slap_get_time();
2554 op2.o_do_not_cache = 1;
2555 op2.o_is_auth_check = 0;
2556 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2557 op2.ors_slimit = SLAP_NO_LIMIT;
2558 op2.ors_tlimit = SLAP_NO_LIMIT;
2559 op2.ors_attrs = anlistp;
2560 op2.ors_attrsonly = 0;
2561 op2.o_private = cp->op->o_private;
2565 rc = op2.o_bd->be_search( &op2, &rs );
2571 if ( op2.ors_filter ) {
2572 filter_free_x( cp->op, op2.ors_filter );
2574 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2575 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2577 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2578 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2581 ldap_free_urldesc( ludp );
2583 if ( anlistp && anlistp != anlist ) {
2584 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2591 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2593 AciSetCookie *cp = (AciSetCookie *)cookie;
2594 BerVarray bvals = NULL;
2598 /* this routine needs to return the bervals instead of
2599 * plain strings, since syntax is not known. It should
2600 * also return the syntax or some "comparison cookie".
2602 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2603 if ( rc == LDAP_SUCCESS ) {
2604 if ( desc == slap_schema.si_ad_entryDN ) {
2605 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2606 cp->op->o_tmpmemctx );
2608 BER_BVZERO( &bvals[ 1 ] );
2612 backend_attribute( cp->op,
2613 cp->e, &ndn, desc, &bvals, ACL_NONE );
2616 if ( !BER_BVISNULL( &ndn ) ) {
2617 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2626 struct berval *subj,
2632 struct berval set = BER_BVNULL;
2634 AciSetCookie cookie;
2636 if ( setref == 0 ) {
2637 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2640 struct berval subjdn, ndn = BER_BVNULL;
2641 struct berval setat;
2644 AttributeDescription *desc = NULL;
2646 /* format of string is "entry/setAttrName" */
2647 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2651 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2652 setat = aci_bv_set_attr;
2656 * NOTE: dnNormalize honors the ber_len field
2657 * as the length of the dn to be normalized
2659 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2660 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2662 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2663 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2667 BER_BVZERO( &bvals[0] );
2668 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2670 bvals[0].bv_val = bvals[i-1].bv_val;
2671 BER_BVZERO( &bvals[i-1] );
2673 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2674 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2679 if ( !BER_BVISNULL( &set ) ) {
2682 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2683 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2684 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2690 #ifdef SLAPD_ACI_ENABLED
2692 aci_list_map_rights(
2693 struct berval *list )
2700 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2703 switch (*bv.bv_val) {
2705 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2708 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2709 * the right 's' to mean "set", but in the examples states
2710 * that the right 's' means "search". The latter definition
2713 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2716 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2719 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2722 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2723 * define any equivalent to the AUTH right, so I've just used
2726 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2738 struct berval *list,
2739 const struct berval *attr,
2740 struct berval *val )
2742 struct berval bv, left, right;
2745 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2746 if (aci_get_part(&bv, 0, '=', &left) < 0
2747 || aci_get_part(&bv, 1, '=', &right) < 0)
2749 if (ber_bvstrcasecmp(attr, &bv) == 0)
2751 } else if (val == NULL) {
2752 if (ber_bvstrcasecmp(attr, &left) == 0)
2755 if (ber_bvstrcasecmp(attr, &left) == 0) {
2756 /* this is experimental code that implements a
2757 * simple (prefix) match of the attribute value.
2758 * the ACI draft does not provide for aci's that
2759 * apply to specific values, but it would be
2760 * nice to have. If the <attr> part of an aci's
2761 * rights list is of the form <attr>=<value>,
2762 * that means the aci applies only to attrs with
2763 * the given value. Furthermore, if the attr is
2764 * of the form <attr>=<value>*, then <value> is
2765 * treated as a prefix, and the aci applies to
2766 * any value with that prefix.
2768 * Ideally, this would allow r.e. matches.
2770 if (aci_get_part(&right, 0, '*', &left) < 0
2771 || right.bv_len <= left.bv_len)
2773 if (ber_bvstrcasecmp(val, &right) == 0)
2775 } else if (val->bv_len >= left.bv_len) {
2776 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2785 static slap_access_t
2786 aci_list_get_attr_rights(
2787 struct berval *list,
2788 const struct berval *attr,
2789 struct berval *val )
2795 /* loop through each rights/attr pair, skip first part (action) */
2797 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2798 if (aci_list_has_attr(&bv, attr, val) == 0)
2800 if (aci_get_part(list, i, ';', &bv) < 0)
2802 mask |= aci_list_map_rights(&bv);
2808 aci_list_get_rights(
2809 struct berval *list,
2810 const struct berval *attr,
2812 slap_access_t *grant,
2813 slap_access_t *deny )
2815 struct berval perm, actn;
2816 slap_access_t *mask;
2819 if (attr == NULL || attr->bv_len == 0
2820 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2821 attr = &aci_bv_br_entry;
2827 /* loop through each permissions clause */
2828 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2829 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2831 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2833 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2840 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2841 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2848 struct berval *subj,
2849 struct berval *defgrpoc,
2850 struct berval *defgrpat,
2857 struct berval subjdn;
2858 struct berval grpoc;
2859 struct berval grpat;
2860 ObjectClass *grp_oc = NULL;
2861 AttributeDescription *grp_ad = NULL;
2865 /* format of string is "group/objectClassValue/groupAttrName" */
2866 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2870 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2874 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2878 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2879 if( rc != LDAP_SUCCESS ) {
2885 grp_oc = oc_bvfind( &grpoc );
2887 if (grp_oc != NULL && grp_ad != NULL ) {
2888 char buf[ACL_BUF_SIZE];
2889 struct berval bv, ndn;
2890 bv.bv_len = sizeof( buf ) - 1;
2891 bv.bv_val = (char *)&buf;
2892 if ( string_expand(&bv, &subjdn,
2893 e->e_ndn, nmatch, matches) )
2898 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2899 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2900 grp_oc, grp_ad ) == 0 );
2901 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2913 AttributeDescription *desc,
2917 regmatch_t *matches,
2918 slap_access_t *grant,
2919 slap_access_t *deny,
2920 slap_aci_scope_t asserted_scope
2923 struct berval bv, scope, perms, type, sdn;
2927 assert( !BER_BVISNULL( &desc->ad_cname ) );
2929 /* parse an aci of the form:
2930 oid # scope # action;rights;attr;rights;attr
2931 $ action;rights;attr;rights;attr # type # subject
2933 [NOTE: the following comment is very outdated,
2934 as the draft version it refers to (Ando, 2004-11-20)].
2936 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2937 a full description of the format for this attribute.
2938 Differences: "this" in the draft is "self" here, and
2939 "self" and "public" is in the position of type.
2941 <scope> = {entry|children|subtree}
2942 <type> = {public|users|access-id|subtree|onelevel|children|
2943 self|dnattr|group|role|set|set-ref}
2945 This routine now supports scope={ENTRY,CHILDREN}
2947 - ENTRY applies to "entry" and "subtree";
2948 - CHILDREN aplies to "children" and "subtree"
2951 /* check that the aci has all 5 components */
2952 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2956 /* check that the aci family is supported */
2957 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2961 /* check that the scope matches */
2962 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2966 /* note: scope can be either ENTRY or CHILDREN;
2967 * they respectively match "entry" and "children" in bv
2968 * both match "subtree" */
2969 switch ( asserted_scope ) {
2970 case SLAP_ACI_SCOPE_ENTRY:
2971 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2972 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2978 case SLAP_ACI_SCOPE_CHILDREN:
2979 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2980 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2990 /* get the list of permissions clauses, bail if empty */
2991 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2995 /* check if any permissions allow desired access */
2996 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
3000 /* see if we have a DN match */
3001 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
3005 /* see if we have a public (i.e. anonymous) access */
3006 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
3010 /* otherwise require an identity */
3011 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3015 /* see if we have a users access */
3016 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3020 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3021 * just grab all the berval up to its end (ITS#3303).
3022 * NOTE: the problem could be solved by providing the DN with
3023 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3024 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3026 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3030 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3031 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3033 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3036 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3037 if ( rc != LDAP_SUCCESS ) {
3041 if ( dn_match( &op->o_ndn, &ndn ) ) {
3044 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3048 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3051 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3052 if ( rc != LDAP_SUCCESS ) {
3056 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3059 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3063 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3064 struct berval ndn, pndn;
3066 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3067 if ( rc != LDAP_SUCCESS ) {
3071 dnParent( &ndn, &pndn );
3073 if ( dn_match( &op->o_ndn, &pndn ) ) {
3076 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3080 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3083 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3084 if ( rc != LDAP_SUCCESS ) {
3088 if ( !dn_match( &op->o_ndn, &ndn )
3089 && dnIsSuffix( &op->o_ndn, &ndn ) )
3093 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3097 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3098 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3102 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3104 AttributeDescription *ad = NULL;
3107 rc = slap_bv2ad( &sdn, &ad, &text );
3109 if( rc != LDAP_SUCCESS ) {
3115 for ( at = attrs_find( e->e_attrs, ad );
3117 at = attrs_find( at->a_next, ad ) )
3119 if ( value_find_ex( ad,
3120 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3121 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3123 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3132 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3133 if ( aci_group_member( &sdn, &aci_bv_group_class,
3134 &aci_bv_group_attr, op, e, nmatch, matches ) )
3139 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3140 if ( aci_group_member( &sdn, &aci_bv_role_class,
3141 &aci_bv_role_attr, op, e, nmatch, matches ) )
3146 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3147 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3151 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3152 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3162 * FIXME: there is a silly dependence that makes it difficult
3163 * to move ACIs in a run-time loadable module under the "dynacl"
3164 * umbrella, because sets share some helpers with ACIs.
3167 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3169 AttributeDescription *ad = NULL;
3170 const char *text = NULL;
3172 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3173 fprintf( stderr, "%s: line %d: "
3174 "inappropriate style \"%s\" in \"aci\" by clause\n",
3175 fname, lineno, style_strings[sty] );
3179 if ( right != NULL && *right != '\0' ) {
3180 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3182 "%s: line %d: aci \"%s\": %s\n",
3183 fname, lineno, right, text );
3188 ad = slap_schema.si_ad_aci;
3191 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3192 fprintf( stderr, "%s: line %d: "
3193 "aci \"%s\": inappropriate syntax: %s\n",
3194 fname, lineno, right,
3195 ad->ad_type->sat_syntax_oid );
3199 *privp = (void *)ad;
3205 dynacl_aci_unparse( void *priv, struct berval *bv )
3207 AttributeDescription *ad = ( AttributeDescription * )priv;
3210 assert( ad != NULL );
3212 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3213 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3214 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3215 bv->bv_len = ptr - bv->bv_val;
3226 AttributeDescription *desc,
3229 regmatch_t *matches,
3230 slap_access_t *grantp,
3231 slap_access_t *denyp )
3233 AttributeDescription *ad = ( AttributeDescription * )priv;
3235 slap_access_t tgrant, tdeny, grant, deny;
3237 char accessmaskbuf[ACCESSMASK_MAXLEN];
3238 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3239 #endif /* LDAP_DEBUG */
3241 /* start out with nothing granted, nothing denied */
3245 /* get the aci attribute */
3246 at = attr_find( e->e_attrs, ad );
3250 /* the aci is an multi-valued attribute. The
3251 * rights are determined by OR'ing the individual
3252 * rights given by the acis.
3254 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3255 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3256 nmatch, matches, &grant, &deny,
3257 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3264 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3265 accessmask2str( tgrant, accessmaskbuf, 1 ),
3266 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3269 /* If the entry level aci didn't contain anything valid for the
3270 * current operation, climb up the tree and evaluate the
3271 * acis with scope set to subtree
3273 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3274 struct berval parent_ndn;
3277 /* to solve the chicken'n'egg problem of accessing
3278 * the OpenLDAPaci attribute, the direct access
3279 * to the entry's attribute is unchecked; however,
3280 * further accesses to OpenLDAPaci values in the
3281 * ancestors occur through backend_attribute(), i.e.
3282 * with the identity of the operation, requiring
3283 * further access checking. For uniformity, this
3284 * makes further requests occur as the rootdn, if
3285 * any, i.e. searching for the OpenLDAPaci attribute
3286 * is considered an internal search. If this is not
3287 * acceptable, then the same check needs be performed
3288 * when accessing the entry's attribute. */
3289 Operation op2 = *op;
3291 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3292 op2.o_dn = op->o_bd->be_rootdn;
3293 op2.o_ndn = op->o_bd->be_rootndn;
3297 dnParent( &e->e_nname, &parent_ndn );
3298 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3300 BerVarray bvals = NULL;
3303 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3304 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3313 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3314 if ( aci_mask( op, e, desc, val,
3318 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3322 /* evaluation stops as soon as either a "deny" or a
3323 * "grant" directive matches.
3325 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3329 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3330 accessmask2str( tgrant, accessmaskbuf, 1 ),
3331 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3335 case LDAP_NO_SUCH_ATTRIBUTE:
3336 /* just go on if the aci-Attribute is not present in
3339 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3343 case LDAP_NO_SUCH_OBJECT:
3344 /* We have reached the base object */
3345 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3357 dnParent( &parent_ndn, &parent_ndn );
3367 /* need to register this at some point */
3368 static slap_dynacl_t dynacl_aci = {
3378 #endif /* SLAP_DYNACL */
3380 #endif /* SLAPD_ACI_ENABLED */
3385 * dynamic ACL infrastructure
3387 static slap_dynacl_t *da_list = NULL;
3390 slap_dynacl_register( slap_dynacl_t *da )
3394 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3395 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3400 if ( tmp != NULL ) {
3404 if ( da->da_mask == NULL ) {
3408 da->da_private = NULL;
3409 da->da_next = da_list;
3415 static slap_dynacl_t *
3416 slap_dynacl_next( slap_dynacl_t *da )
3425 slap_dynacl_get( const char *name )
3429 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3430 if ( strcasecmp( da->da_name, name ) == 0 ) {
3437 #endif /* SLAP_DYNACL */
3444 slap_dynacl_t *known_dynacl[] = {
3445 #ifdef SLAPD_ACI_ENABLED
3447 #endif /* SLAPD_ACI_ENABLED */
3451 for ( i = 0; known_dynacl[ i ]; i++ ) {
3452 rc = slap_dynacl_register( known_dynacl[ i ] );
3457 #endif /* SLAP_DYNACL */
3468 regmatch_t *matches)
3476 bv->bv_val[0] = '\0';
3477 bv->bv_len--; /* leave space for lone $ */
3480 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3481 sp < pat->bv_val + pat->bv_len ; sp++ )
3483 /* did we previously see a $ */
3485 if ( flag == 1 && *sp == '$' ) {
3490 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3493 } else if ( *sp >= '0' && *sp <= '9' ) {
3501 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3502 if ( *sp >= '0' && *sp <= '9' ) {
3503 n = 10*n + ( *sp - '0' );
3507 if ( *sp != /*'{'*/ '}' ) {
3513 if ( n >= nmatch ) {
3519 i = matches[n].rm_so;
3520 l = matches[n].rm_eo;
3521 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3539 /* must have ended with a single $ */
3547 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3548 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3555 struct berval *pat, /* pattern to expand and match against */
3556 char *str, /* string to match against pattern */
3557 char *buf, /* buffer with $N expansion variables */
3558 int nmatch, /* size of the matches array */
3559 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3563 char newbuf[ACL_BUF_SIZE];
3567 bv.bv_len = sizeof( newbuf ) - 1;
3574 string_expand( &bv, pat, buf, nmatch, matches );
3575 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3577 char error[ACL_BUF_SIZE];
3578 regerror( rc, &re, error, sizeof( error ) );
3580 Debug( LDAP_DEBUG_TRACE,
3581 "compile( \"%s\", \"%s\") failed %s\n",
3582 pat->bv_val, str, error );
3586 rc = regexec( &re, str, 0, NULL, 0 );
3589 Debug( LDAP_DEBUG_TRACE,
3590 "=> regex_matches: string: %s\n", str, 0, 0 );
3591 Debug( LDAP_DEBUG_TRACE,
3592 "=> regex_matches: rc: %d %s\n",
3593 rc, !rc ? "matches" : "no matches", 0 );