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,
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;
376 BI_access_allowed *bi_access_allowed = NULL;
379 assert( desc != NULL );
381 access_level = ACL_LEVEL( access );
383 assert( access_level > ACL_NONE );
386 if ( maskp ) ACL_INVALIDATE( *maskp );
388 attr = desc->ad_cname.bv_val;
390 assert( attr != NULL );
392 if ( op && op->o_is_auth_check &&
393 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
399 if ( state->as_vd_ad == desc ) {
400 if ( state->as_recorded ) {
401 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
404 return state->as_result;
406 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
407 val != NULL && state->as_vd_acl == NULL )
409 return state->as_result;
417 state->as_vd_ad = desc;
420 Debug( LDAP_DEBUG_ACL,
421 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
422 access2str( access ), e->e_dn, attr );
429 if ( op->o_bd == NULL ) {
430 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
435 * FIXME: experimental; use first backend rules
436 * iff there is no global_acl (ITS#3100) */
437 if ( frontendDB->be_acl != NULL ) {
438 op->o_bd = frontendDB;
440 #endif /* LDAP_DEVEL */
442 assert( op->o_bd != NULL );
444 /* this is enforced in backend_add() */
445 if ( op->o_bd->bd_info->bi_access_allowed ) {
446 /* delegate to backend */
447 ret = op->o_bd->bd_info->bi_access_allowed( op, e, desc, val, access, state, &mask );
451 ret = slap_access_allowed( op, e, desc, val, access, state, &mask );
455 if ( ACL_IS_INVALID( mask ) ) {
456 Debug( LDAP_DEBUG_ACL,
457 "=> access_allowed: \"%s\" (%s) invalid!\n",
461 } else if ( control == ACL_BREAK ) {
462 Debug( LDAP_DEBUG_ACL,
463 "=> access_allowed: no more rules\n", 0, 0, 0 );
468 ret = ACL_GRANT( mask, access );
471 Debug( LDAP_DEBUG_ACL,
472 "=> access_allowed: %s access %s by %s\n",
473 access2str( access ), ret ? "granted" : "denied",
474 accessmask2str( mask, accessmaskbuf, 1 ) );
477 if ( state != NULL ) {
478 /* If not value-dependent, save ACL in case of more attrs */
479 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
480 state->as_vi_acl = a;
481 state->as_result = ret;
483 state->as_recorded |= ACL_STATE_RECORDED;
485 if ( be_null ) op->o_bd = NULL;
486 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
490 #else /* !SLAP_OVERLAY_ACCESS */
496 AttributeDescription *desc,
498 slap_access_t access,
499 AccessControlState *state,
504 AccessControl *a = NULL;
509 char accessmaskbuf[ACCESSMASK_MAXLEN];
512 slap_control_t control;
513 slap_access_t access_level;
515 regmatch_t matches[MAXREMATCHES];
516 int st_same_attr = 0;
517 static AccessControlState state_init = ACL_STATE_INIT;
520 assert( desc != NULL );
522 access_level = ACL_LEVEL( access );
524 assert( access_level > ACL_NONE );
525 if ( maskp ) ACL_INVALIDATE( *maskp );
527 attr = desc->ad_cname.bv_val;
529 assert( attr != NULL );
531 if ( op && op->o_is_auth_check &&
532 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
538 if ( state->as_vd_ad == desc ) {
539 if ( state->as_recorded ) {
540 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
543 return state->as_result;
545 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
546 val != NULL && state->as_vd_acl == NULL )
548 return state->as_result;
556 state->as_vd_ad=desc;
559 Debug( LDAP_DEBUG_ACL,
560 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
561 access2str( access ), e->e_dn, attr );
570 be = LDAP_STAILQ_FIRST(&backendDB);
574 * FIXME: experimental; use first backend rules
575 * iff there is no global_acl (ITS#3100) */
576 if ( frontendDB->be_acl == NULL )
582 assert( be != NULL );
585 if ( op->o_pb != NULL ) {
586 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
588 /* ACL plugin denied access */
592 #endif /* LDAP_SLAPI */
594 /* grant database root access */
595 if ( be_isroot( op ) ) {
596 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
598 mask = ACL_LVL_MANAGE;
605 * no-user-modification operational attributes are ignored
606 * by ACL_WRITE checking as any found here are not provided
609 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
610 && desc != slap_schema.si_ad_entry
611 && desc != slap_schema.si_ad_children )
613 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
614 " %s access granted\n",
619 /* use backend default access if no backend acls */
620 if ( be->be_acl == NULL ) {
621 Debug( LDAP_DEBUG_ACL,
622 "=> access_allowed: backend default %s "
623 "access %s to \"%s\"\n",
624 access2str( access ),
625 be->be_dfltaccess >= access_level ? "granted" : "denied",
626 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
627 ret = be->be_dfltaccess >= access_level;
632 mask = ACL_PRIV_LEVEL;
633 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
634 mask |= ACL_ACCESS2PRIV( i );
641 /* be is always non-NULL */
642 /* use global default access if no global acls */
643 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
644 Debug( LDAP_DEBUG_ACL,
645 "=> access_allowed: global default %s access %s to \"%s\"\n",
646 access2str( access ),
647 frontendDB->be_dfltaccess >= access_level ?
648 "granted" : "denied", op->o_dn.bv_val );
649 ret = frontendDB->be_dfltaccess >= access_level;
654 mask = ACL_PRIV_LEVEL;
655 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
656 mask |= ACL_ACCESS2PRIV( i );
667 if ( st_same_attr ) {
668 assert( state->as_vd_acl != NULL );
670 a = state->as_vd_acl;
671 count = state->as_vd_acl_count;
672 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
673 mask = state->as_vd_acl_mask;
674 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
679 if ( state ) state->as_vi_acl = NULL;
683 memset( matches, '\0', sizeof(matches) );
686 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
687 MAXREMATCHES, matches, state ) ) != NULL )
691 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
692 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
693 (int)matches[i].rm_so, (int)matches[i].rm_eo );
694 if ( matches[i].rm_so <= matches[0].rm_eo ) {
696 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
697 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
700 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
704 if ( state->as_vi_acl == a &&
705 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
707 Debug( LDAP_DEBUG_ACL,
708 "access_allowed: result from state (%s)\n",
710 ret = state->as_result;
713 Debug( LDAP_DEBUG_ACL,
714 "access_allowed: no res from state (%s)\n",
720 control = slap_acl_mask( a, &mask, op,
721 e, desc, val, MAXREMATCHES, matches, count, state );
723 if ( control != ACL_BREAK ) {
727 memset( matches, '\0', sizeof(matches) );
730 if ( ACL_IS_INVALID( mask ) ) {
731 Debug( LDAP_DEBUG_ACL,
732 "=> access_allowed: \"%s\" (%s) invalid!\n",
736 } else if ( control == ACL_BREAK ) {
737 Debug( LDAP_DEBUG_ACL,
738 "=> access_allowed: no more rules\n", 0, 0, 0 );
743 Debug( LDAP_DEBUG_ACL,
744 "=> access_allowed: %s access %s by %s\n",
745 access2str( access ),
746 ACL_GRANT(mask, access) ? "granted" : "denied",
747 accessmask2str( mask, accessmaskbuf, 1 ) );
749 ret = ACL_GRANT(mask, access);
752 if ( state != NULL ) {
753 /* If not value-dependent, save ACL in case of more attrs */
754 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
755 state->as_vi_acl = a;
756 state->as_result = ret;
758 state->as_recorded |= ACL_STATE_RECORDED;
760 if ( be_null ) op->o_bd = NULL;
761 if ( maskp ) *maskp = mask;
765 #endif /* SLAP_OVERLAY_ACCESS */
768 * slap_acl_get - return the acl applicable to entry e, attribute
769 * attr. the acl returned is suitable for use in subsequent calls to
770 * acl_access_allowed().
773 static AccessControl *
779 AttributeDescription *desc,
783 AccessControlState *state )
790 assert( count != NULL );
791 assert( desc != NULL );
793 attr = desc->ad_cname.bv_val;
795 assert( attr != NULL );
798 if( op->o_bd == NULL ) {
799 a = frontendDB->be_acl;
801 a = op->o_bd->be_acl;
812 dnlen = e->e_nname.bv_len;
814 for ( ; a != NULL; a = a->acl_next ) {
817 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
818 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
819 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
820 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
821 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
825 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
826 *count, a->acl_dn_pat.bv_val, 0 );
827 patlen = a->acl_dn_pat.bv_len;
828 if ( dnlen < patlen )
831 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
832 /* base dn -- entire object DN must match */
833 if ( dnlen != patlen )
836 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
837 int rdnlen = -1, sep = 0;
839 if ( dnlen <= patlen )
843 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
848 rdnlen = dn_rdnlen( NULL, &e->e_nname );
849 if ( rdnlen != dnlen - patlen - sep )
852 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
853 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
856 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
857 if ( dnlen <= patlen )
859 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
863 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
867 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
871 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
872 matches[0].rm_so = matches[0].rm_eo = -1;
876 /* Is this ACL only for a specific value? */
877 if ( a->acl_attrval.bv_len ) {
882 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
883 state->as_recorded |= ACL_STATE_RECORDED_VD;
884 state->as_vd_acl = a;
885 state->as_vd_acl_count = *count;
886 state->as_vd_access = a->acl_access;
887 state->as_vd_access_count = 1;
888 ACL_INVALIDATE( state->as_vd_acl_mask );
891 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
892 Debug( LDAP_DEBUG_ACL,
893 "acl_get: valpat %s\n",
894 a->acl_attrval.bv_val, 0, 0 );
895 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
903 Debug( LDAP_DEBUG_ACL,
905 a->acl_attrval.bv_val, 0, 0 );
907 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
908 if (value_match( &match, desc,
909 desc->ad_type->sat_equality, 0,
910 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
917 patlen = a->acl_attrval.bv_len;
918 vdnlen = val->bv_len;
920 if ( vdnlen < patlen )
923 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
924 if ( vdnlen > patlen )
927 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
930 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
933 rdnlen = dn_rdnlen( NULL, val );
934 if ( rdnlen != vdnlen - patlen - 1 )
937 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
938 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
941 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
942 if ( vdnlen <= patlen )
945 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
949 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
955 if ( a->acl_filter != NULL ) {
956 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
957 if ( rc != LDAP_COMPARE_TRUE ) {
962 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
967 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
979 struct berval *opndn )
982 * if access applies to the entry itself, and the
983 * user is bound as somebody in the same namespace as
984 * the entry, OR the given dn matches the dn pattern
987 * NOTE: styles "anonymous", "users" and "self"
988 * have been moved to enum slap_style_t, whose
989 * value is set in a_dn_style; however, the string
990 * is maintaned in a_dn_pat.
992 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
993 if ( !BER_BVISEMPTY( opndn ) ) {
997 } else if ( b->a_style == ACL_STYLE_USERS ) {
998 if ( BER_BVISEMPTY( opndn ) ) {
1002 } else if ( b->a_style == ACL_STYLE_SELF ) {
1003 struct berval ndn, selfndn;
1006 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1010 level = b->a_self_level;
1018 selfndn = e->e_nname;
1021 for ( ; level > 0; level-- ) {
1022 if ( BER_BVISEMPTY( &ndn ) ) {
1025 dnParent( &ndn, &ndn );
1028 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1033 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1034 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1036 regmatch_t tmp_matches[2],
1037 *tmp_matchesp = tmp_matches;
1041 switch ( a->acl_dn_style ) {
1042 case ACL_STYLE_REGEX:
1043 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1044 tmp_matchesp = matches;
1045 tmp_nmatch = nmatch;
1048 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1050 case ACL_STYLE_BASE:
1051 tmp_matches[0].rm_so = 0;
1052 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1057 case ACL_STYLE_SUBTREE:
1058 case ACL_STYLE_CHILDREN:
1059 tmp_matches[0].rm_so = 0;
1060 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1061 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1062 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1076 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1077 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1085 ber_len_t patlen, odnlen;
1088 if ( e->e_dn == NULL )
1091 if ( b->a_expand ) {
1093 char buf[ACL_BUF_SIZE];
1096 regmatch_t tmp_matches[2],
1097 *tmp_matchesp = tmp_matches;
1101 bv.bv_len = sizeof( buf ) - 1;
1104 switch ( a->acl_dn_style ) {
1105 case ACL_STYLE_REGEX:
1106 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1107 tmp_matchesp = matches;
1108 tmp_nmatch = nmatch;
1111 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1113 case ACL_STYLE_BASE:
1114 tmp_matches[0].rm_so = 0;
1115 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1120 case ACL_STYLE_SUBTREE:
1121 case ACL_STYLE_CHILDREN:
1122 tmp_matches[0].rm_so = 0;
1123 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1124 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1125 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1139 if ( string_expand( &bv, &b->a_pat,
1141 tmp_nmatch, tmp_matchesp ) )
1146 if ( dnNormalize(0, NULL, NULL, &bv,
1147 &pat, op->o_tmpmemctx )
1150 /* did not expand to a valid dn */
1158 patlen = pat.bv_len;
1159 odnlen = opndn->bv_len;
1160 if ( odnlen < patlen ) {
1161 goto dn_match_cleanup;
1165 if ( b->a_style == ACL_STYLE_BASE ) {
1166 /* base dn -- entire object DN must match */
1167 if ( odnlen != patlen ) {
1168 goto dn_match_cleanup;
1171 } else if ( b->a_style == ACL_STYLE_ONE ) {
1174 if ( odnlen <= patlen ) {
1175 goto dn_match_cleanup;
1178 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1179 goto dn_match_cleanup;
1182 rdnlen = dn_rdnlen( NULL, opndn );
1183 if ( rdnlen != odnlen - patlen - 1 ) {
1184 goto dn_match_cleanup;
1187 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1188 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1189 goto dn_match_cleanup;
1192 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1193 if ( odnlen <= patlen ) {
1194 goto dn_match_cleanup;
1197 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1198 goto dn_match_cleanup;
1201 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1205 if ( odnlen <= patlen ) {
1206 goto dn_match_cleanup;
1209 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1211 goto dn_match_cleanup;
1216 for ( ; level > 0; level-- ) {
1217 if ( BER_BVISEMPTY( &ndn ) ) {
1218 goto dn_match_cleanup;
1220 dnParent( &ndn, &ndn );
1221 if ( ndn.bv_len < patlen ) {
1222 goto dn_match_cleanup;
1226 if ( ndn.bv_len != patlen ) {
1227 goto dn_match_cleanup;
1231 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1234 if ( pat.bv_val != b->a_pat.bv_val ) {
1235 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1247 * Record value-dependent access control state
1249 #define ACL_RECORD_VALUE_STATE do { \
1250 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1251 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1252 state->as_vd_acl = a; \
1253 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1254 sizeof( state->as_vd_acl_matches )) ; \
1255 state->as_vd_acl_count = count; \
1256 state->as_vd_access = b; \
1257 state->as_vd_access_count = i; \
1269 regmatch_t *matches,
1271 AccessControlState *state,
1272 slap_dn_access *bdn,
1273 struct berval *opndn )
1279 const char *attr = bdn->a_at->ad_cname.bv_val;
1281 assert( attr != NULL );
1283 if ( BER_BVISEMPTY( opndn ) ) {
1287 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1290 /* see if asker is listed in dnattr */
1291 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1293 at = attrs_find( at->a_next, bdn->a_at ) )
1295 if ( value_find_ex( bdn->a_at,
1296 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1297 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1299 &bv, op->o_tmpmemctx ) == 0 )
1308 /* have a dnattr match. if this is a self clause then
1309 * the target must also match the op dn.
1311 if ( bdn->a_self ) {
1312 /* check if the target is an attribute. */
1313 if ( val == NULL ) return 1;
1315 /* target is attribute, check if the attribute value
1318 rc = value_match( &match, bdn->a_at,
1319 bdn->a_at->ad_type->sat_equality, 0,
1321 /* on match error or no match, fail the ACL clause */
1322 if ( rc != LDAP_SUCCESS || match != 0 )
1327 /* no dnattr match, check if this is a self clause */
1328 if ( ! bdn->a_self )
1331 ACL_RECORD_VALUE_STATE;
1333 /* this is a self clause, check if the target is an
1339 /* target is attribute, check if the attribute value
1342 rc = value_match( &match, bdn->a_at,
1343 bdn->a_at->ad_type->sat_equality, 0,
1346 /* on match error or no match, fail the ACL clause */
1347 if ( rc != LDAP_SUCCESS || match != 0 )
1356 * slap_acl_mask - modifies mask based upon the given acl and the
1357 * requested access to entry e, attribute attr, value val. if val
1358 * is null, access to the whole attribute is assumed (all values).
1360 * returns 0 access NOT allowed
1364 static slap_control_t
1370 AttributeDescription *desc,
1373 regmatch_t *matches,
1375 AccessControlState *state )
1380 char accessmaskbuf[ACCESSMASK_MAXLEN];
1381 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1382 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1383 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1387 assert( a != NULL );
1388 assert( mask != NULL );
1389 assert( desc != NULL );
1391 attr = desc->ad_cname.bv_val;
1393 assert( attr != NULL );
1395 Debug( LDAP_DEBUG_ACL,
1396 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1399 Debug( LDAP_DEBUG_ACL,
1400 "=> acl_mask: to %s by \"%s\", (%s) \n",
1401 val ? "value" : "all values",
1402 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1403 accessmask2str( *mask, accessmaskbuf, 1 ) );
1406 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1407 && state->as_vd_acl == a )
1409 b = state->as_vd_access;
1410 i = state->as_vd_access_count;
1417 for ( ; b != NULL; b = b->a_next, i++ ) {
1418 slap_mask_t oldmask, modmask;
1420 ACL_INVALIDATE( modmask );
1422 /* AND <who> clauses */
1423 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1424 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1425 b->a_dn_pat.bv_val, 0, 0);
1427 * if access applies to the entry itself, and the
1428 * user is bound as somebody in the same namespace as
1429 * the entry, OR the given dn matches the dn pattern
1432 * NOTE: styles "anonymous", "users" and "self"
1433 * have been moved to enum slap_style_t, whose
1434 * value is set in a_dn_style; however, the string
1435 * is maintaned in a_dn_pat.
1438 if ( acl_mask_dn( op, e, a, nmatch, matches,
1439 &b->a_dn, &op->o_ndn ) )
1445 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1448 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1449 b->a_realdn_pat.bv_val, 0, 0);
1451 * if access applies to the entry itself, and the
1452 * user is bound as somebody in the same namespace as
1453 * the entry, OR the given dn matches the dn pattern
1456 * NOTE: styles "anonymous", "users" and "self"
1457 * have been moved to enum slap_style_t, whose
1458 * value is set in a_dn_style; however, the string
1459 * is maintaned in a_dn_pat.
1462 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1464 ndn = op->o_conn->c_ndn;
1469 if ( acl_mask_dn( op, e, a, nmatch, matches,
1470 &b->a_realdn, &ndn ) )
1476 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1477 if ( ! op->o_conn->c_listener ) {
1480 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1481 b->a_sockurl_pat.bv_val, 0, 0 );
1483 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1484 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1485 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1486 e->e_ndn, nmatch, matches ) )
1491 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1493 char buf[ACL_BUF_SIZE];
1495 bv.bv_len = sizeof( buf ) - 1;
1497 if ( string_expand( &bv, &b->a_sockurl_pat,
1498 e->e_ndn, nmatch, matches ) )
1503 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1509 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1517 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1518 if ( !op->o_conn->c_peer_domain.bv_val ) {
1521 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1522 b->a_domain_pat.bv_val, 0, 0 );
1523 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1524 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1525 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1526 e->e_ndn, nmatch, matches ) )
1531 char buf[ACL_BUF_SIZE];
1533 struct berval cmp = op->o_conn->c_peer_domain;
1534 struct berval pat = b->a_domain_pat;
1536 if ( b->a_domain_expand ) {
1539 bv.bv_len = sizeof(buf) - 1;
1542 if ( string_expand(&bv, &b->a_domain_pat,
1543 e->e_ndn, nmatch, matches) )
1550 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1551 int offset = cmp.bv_len - pat.bv_len;
1556 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1560 /* trim the domain */
1561 cmp.bv_val = &cmp.bv_val[ offset ];
1562 cmp.bv_len -= offset;
1565 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1572 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1573 if ( !op->o_conn->c_peer_name.bv_val ) {
1576 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1577 b->a_peername_pat.bv_val, 0, 0 );
1578 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1579 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1580 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1581 e->e_ndn, nmatch, matches ) )
1587 /* try exact match */
1588 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1589 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1593 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1595 char buf[ACL_BUF_SIZE];
1597 bv.bv_len = sizeof( buf ) - 1;
1599 if ( string_expand( &bv, &b->a_peername_pat,
1600 e->e_ndn, nmatch, matches ) )
1605 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1609 /* extract IP and try exact match */
1610 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1612 char buf[] = "255.255.255.255";
1615 int port_number = -1;
1617 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1618 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1621 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1622 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1624 port = strrchr( ip.bv_val, ':' );
1628 ip.bv_len = port - ip.bv_val;
1630 port_number = strtol( port, &next, 10 );
1631 if ( next[0] != '\0' )
1635 /* the port check can be anticipated here */
1636 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1639 /* address longer than expected? */
1640 if ( ip.bv_len >= sizeof(buf) )
1643 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1644 buf[ ip.bv_len ] = '\0';
1646 addr = inet_addr( buf );
1648 /* unable to convert? */
1649 if ( addr == (unsigned long)(-1) )
1652 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1655 #ifdef LDAP_PF_LOCAL
1656 /* extract path and try exact match */
1657 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1660 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1661 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1664 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1665 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1667 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1670 #endif /* LDAP_PF_LOCAL */
1672 /* exact match (very unlikely...) */
1673 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1680 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1681 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1684 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1685 b->a_sockname_pat.bv_val, 0, 0 );
1686 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1687 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1688 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1689 e->e_ndn, nmatch, matches ) )
1694 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1696 char buf[ACL_BUF_SIZE];
1698 bv.bv_len = sizeof( buf ) - 1;
1700 if ( string_expand( &bv, &b->a_sockname_pat,
1701 e->e_ndn, nmatch, matches ) )
1706 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1711 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1718 if ( b->a_dn_at != NULL ) {
1719 if ( acl_mask_dnattr( op, e, val, a, b, i,
1720 matches, count, state,
1721 &b->a_dn, &op->o_ndn ) )
1727 if ( b->a_realdn_at != NULL ) {
1730 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1732 ndn = op->o_conn->c_ndn;
1737 if ( acl_mask_dnattr( op, e, val, a, b, i,
1738 matches, count, state,
1739 &b->a_realdn, &ndn ) )
1745 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1747 struct berval ndn = BER_BVNULL;
1750 if ( op->o_ndn.bv_len == 0 ) {
1754 /* b->a_group is an unexpanded entry name, expanded it should be an
1755 * entry with objectclass group* and we test to see if odn is one of
1756 * the values in the attribute group
1758 /* see if asker is listed in dnattr */
1759 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1760 char buf[ACL_BUF_SIZE];
1762 regmatch_t tmp_matches[2],
1763 *tmp_matchesp = tmp_matches;
1765 bv.bv_len = sizeof(buf) - 1;
1770 switch ( a->acl_dn_style ) {
1771 case ACL_STYLE_REGEX:
1772 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1773 tmp_matchesp = matches;
1774 tmp_nmatch = nmatch;
1778 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1779 case ACL_STYLE_BASE:
1780 tmp_matches[0].rm_so = 0;
1781 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1786 case ACL_STYLE_SUBTREE:
1787 case ACL_STYLE_CHILDREN:
1788 tmp_matches[0].rm_so = 0;
1789 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1790 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1791 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1805 if ( string_expand( &bv, &b->a_group_pat,
1807 tmp_nmatch, tmp_matchesp ) )
1812 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1813 op->o_tmpmemctx ) != LDAP_SUCCESS )
1815 /* did not expand to a valid dn */
1822 bv = b->a_group_pat;
1825 rc = backend_group( op, e, &bv, &op->o_ndn,
1826 b->a_group_oc, b->a_group_at );
1829 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1837 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1839 char buf[ACL_BUF_SIZE];
1841 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1843 regmatch_t tmp_matches[2],
1844 *tmp_matchesp = tmp_matches;
1847 bv.bv_len = sizeof( buf ) - 1;
1852 switch ( a->acl_dn_style ) {
1853 case ACL_STYLE_REGEX:
1854 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1855 tmp_matchesp = matches;
1856 tmp_nmatch = nmatch;
1860 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1861 case ACL_STYLE_BASE:
1862 tmp_matches[0].rm_so = 0;
1863 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1868 case ACL_STYLE_SUBTREE:
1869 case ACL_STYLE_CHILDREN:
1870 tmp_matches[0].rm_so = 0;
1871 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1872 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1873 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1887 if ( string_expand( &bv, &b->a_set_pat,
1889 tmp_nmatch, tmp_matchesp ) )
1898 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1903 if ( b->a_authz.sai_ssf ) {
1904 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1905 b->a_authz.sai_ssf, op->o_ssf, 0 );
1906 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1911 if ( b->a_authz.sai_transport_ssf ) {
1912 Debug( LDAP_DEBUG_ACL,
1913 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1914 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1915 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1920 if ( b->a_authz.sai_tls_ssf ) {
1921 Debug( LDAP_DEBUG_ACL,
1922 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1923 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1924 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1929 if ( b->a_authz.sai_sasl_ssf ) {
1930 Debug( LDAP_DEBUG_ACL,
1931 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1932 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1933 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1939 if ( b->a_dynacl ) {
1941 slap_access_t tgrant, tdeny;
1943 /* this case works different from the others above.
1944 * since aci's themselves give permissions, we need
1945 * to first check b->a_access_mask, the ACL's access level.
1947 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1948 /* no ACIs in the root DSE */
1952 /* first check if the right being requested
1953 * is allowed by the ACL clause.
1955 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1959 /* start out with nothing granted, nothing denied */
1963 for ( da = b->a_dynacl; da; da = da->da_next ) {
1964 slap_access_t grant, deny;
1966 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1972 /* remove anything that the ACL clause does not allow */
1973 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1974 tdeny &= ACL_PRIV_MASK;
1976 /* see if we have anything to contribute */
1977 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1981 /* this could be improved by changing slap_acl_mask so that it can deal with
1982 * by clauses that return grant/deny pairs. Right now, it does either
1983 * additive or subtractive rights, but not both at the same time. So,
1984 * we need to combine the grant/deny pair into a single rights mask in
1985 * a smart way: if either grant or deny is "empty", then we use the
1986 * opposite as is, otherwise we remove any denied rights from the grant
1987 * rights mask and construct an additive mask.
1989 if (ACL_IS_INVALID(tdeny)) {
1990 modmask = tgrant | ACL_PRIV_ADDITIVE;
1992 } else if (ACL_IS_INVALID(tgrant)) {
1993 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1996 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2000 #else /* !SLAP_DYNACL */
2002 #ifdef SLAPD_ACI_ENABLED
2003 if ( b->a_aci_at != NULL ) {
2005 slap_access_t grant, deny, tgrant, tdeny;
2006 struct berval parent_ndn;
2007 BerVarray bvals = NULL;
2010 /* this case works different from the others above.
2011 * since aci's themselves give permissions, we need
2012 * to first check b->a_access_mask, the ACL's access level.
2015 if ( BER_BVISEMPTY( &e->e_nname ) ) {
2016 /* no ACIs in the root DSE */
2020 /* first check if the right being requested
2021 * is allowed by the ACL clause.
2023 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2026 /* start out with nothing granted, nothing denied */
2030 /* get the aci attribute */
2031 at = attr_find( e->e_attrs, b->a_aci_at );
2034 /* FIXME: this breaks acl caching;
2035 * see also ACL_RECORD_VALUE_STATE below */
2036 ACL_RECORD_VALUE_STATE;
2038 /* the aci is an multi-valued attribute. The
2039 * rights are determined by OR'ing the individual
2040 * rights given by the acis.
2042 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2047 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2053 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2054 accessmask2str(tgrant, accessmaskbuf, 1),
2055 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2058 /* If the entry level aci didn't contain anything valid for the
2059 * current operation, climb up the tree and evaluate the
2060 * acis with scope set to subtree
2062 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2063 dnParent( &e->e_nname, &parent_ndn );
2064 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2065 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2066 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2074 for( i = 0; bvals[i].bv_val != NULL; i++){
2076 /* FIXME: this breaks acl caching;
2077 * see also ACL_RECORD_VALUE_STATE above */
2078 ACL_RECORD_VALUE_STATE;
2080 if (aci_mask(op, e, desc, val, &bvals[i],
2082 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2086 /* evaluation stops as soon as either a "deny" or a
2087 * "grant" directive matches.
2089 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2093 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2094 accessmask2str(tgrant, accessmaskbuf, 1),
2095 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2099 case LDAP_NO_SUCH_ATTRIBUTE:
2100 /* just go on if the aci-Attribute is not present in
2103 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2107 case LDAP_NO_SUCH_OBJECT:
2108 /* We have reached the base object */
2109 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2120 dnParent( &parent_ndn, &parent_ndn );
2125 /* remove anything that the ACL clause does not allow */
2126 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2127 tdeny &= ACL_PRIV_MASK;
2129 /* see if we have anything to contribute */
2130 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2134 /* this could be improved by changing slap_acl_mask so that it can deal with
2135 * by clauses that return grant/deny pairs. Right now, it does either
2136 * additive or subtractive rights, but not both at the same time. So,
2137 * we need to combine the grant/deny pair into a single rights mask in
2138 * a smart way: if either grant or deny is "empty", then we use the
2139 * opposite as is, otherwise we remove any denied rights from the grant
2140 * rights mask and construct an additive mask.
2142 if (ACL_IS_INVALID(tdeny)) {
2143 modmask = tgrant | ACL_PRIV_ADDITIVE;
2145 } else if (ACL_IS_INVALID(tgrant)) {
2146 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2149 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2153 #endif /* SLAPD_ACI_ENABLED */
2154 #endif /* !SLAP_DYNACL */
2156 modmask = b->a_access_mask;
2159 Debug( LDAP_DEBUG_ACL,
2160 "<= acl_mask: [%d] applying %s (%s)\n",
2161 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2162 b->a_type == ACL_CONTINUE
2164 : b->a_type == ACL_BREAK
2170 if( ACL_IS_ADDITIVE(modmask) ) {
2172 ACL_PRIV_SET( *mask, modmask );
2175 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2177 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2178 /* substract privs */
2179 ACL_PRIV_CLR( *mask, modmask );
2182 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2189 Debug( LDAP_DEBUG_ACL,
2190 "<= acl_mask: [%d] mask: %s\n",
2191 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2193 if( b->a_type == ACL_CONTINUE ) {
2196 } else if ( b->a_type == ACL_BREAK ) {
2204 /* implicit "by * none" clause */
2207 Debug( LDAP_DEBUG_ACL,
2208 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2209 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2214 * acl_check_modlist - check access control on the given entry to see if
2215 * it allows the given modifications by the user associated with op.
2216 * returns 1 if mods allowed ok
2217 * 0 mods not allowed
2224 Modifications *mlist
2228 AccessControlState state = ACL_STATE_INIT;
2231 int ret = 1; /* default is access allowed */
2235 be = LDAP_STAILQ_FIRST(&backendDB);
2239 assert( be != NULL );
2241 /* short circuit root database access */
2242 if ( be_isroot( op ) ) {
2243 Debug( LDAP_DEBUG_ACL,
2244 "<= acl_access_allowed: granted to database root\n",
2249 /* use backend default access if no backend acls */
2250 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2251 Debug( LDAP_DEBUG_ACL,
2252 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2253 access2str( ACL_WRITE ),
2254 op->o_bd->be_dfltaccess >= ACL_WRITE
2255 ? "granted" : "denied",
2257 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2261 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2263 * Internal mods are ignored by ACL_WRITE checking
2265 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2266 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2267 " modify access granted\n",
2268 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2273 * no-user-modification operational attributes are ignored
2274 * by ACL_WRITE checking as any found here are not provided
2277 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2278 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2279 " modify access granted\n",
2280 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2284 switch ( mlist->sml_op ) {
2285 case LDAP_MOD_REPLACE:
2287 * We must check both permission to delete the whole
2288 * attribute and permission to add the specific attributes.
2289 * This prevents abuse from selfwriters.
2291 if ( ! access_allowed( op, e,
2292 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2298 if ( mlist->sml_values == NULL ) break;
2300 /* fall thru to check value to add */
2303 assert( mlist->sml_values != NULL );
2305 for ( bv = mlist->sml_nvalues
2306 ? mlist->sml_nvalues : mlist->sml_values;
2307 bv->bv_val != NULL; bv++ )
2309 if ( ! access_allowed( op, e,
2310 mlist->sml_desc, bv, ACL_WADD, &state ) )
2318 case LDAP_MOD_DELETE:
2319 if ( mlist->sml_values == NULL ) {
2320 if ( ! access_allowed( op, e,
2321 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2328 for ( bv = mlist->sml_nvalues
2329 ? mlist->sml_nvalues : mlist->sml_values;
2330 bv->bv_val != NULL; bv++ )
2332 if ( ! access_allowed( op, e,
2333 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2341 case SLAP_MOD_SOFTADD:
2342 /* allow adding attribute via modrdn thru */
2354 if (be_null) op->o_bd = NULL;
2360 struct berval *list,
2373 while ( len >= 0 && --ix >= 0 ) {
2374 while ( --len >= 0 && *p++ != sep )
2377 while ( len >= 0 && *p == ' ' ) {
2390 while ( --len >= 0 && *p != sep ) {
2394 while ( bv->bv_len > 0 && *--p == ' ' ) {
2401 typedef struct aci_set_gather_t {
2407 aci_set_cb_gather( Operation *op, SlapReply *rs )
2409 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2411 if ( rs->sr_type == REP_SEARCH ) {
2412 BerValue bvals[ 2 ];
2413 BerVarray bvalsp = NULL;
2416 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2417 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2419 if ( desc == slap_schema.si_ad_entryDN ) {
2421 bvals[ 0 ] = rs->sr_entry->e_nname;
2422 BER_BVZERO( &bvals[ 1 ] );
2427 a = attr_find( rs->sr_entry->e_attrs, desc );
2431 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2434 bvalsp = a->a_nvals;
2440 p->bvals = slap_set_join( p->cookie, p->bvals,
2441 ( '|' | SLAP_SET_RREF ), bvalsp );
2445 assert( rs->sr_type == REP_RESULT );
2452 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2454 AciSetCookie *cp = (AciSetCookie *)cookie;
2456 LDAPURLDesc *ludp = NULL;
2457 Operation op2 = { 0 };
2458 SlapReply rs = {REP_RESULT};
2459 AttributeName anlist[ 2 ], *anlistp = NULL;
2461 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2462 aci_set_gather_t p = { 0 };
2463 const char *text = NULL;
2464 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2466 /* this routine needs to return the bervals instead of
2467 * plain strings, since syntax is not known. It should
2468 * also return the syntax or some "comparison cookie".
2470 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2471 return aci_set_gather2( cookie, name, desc );
2474 rc = ldap_url_parse( name->bv_val, &ludp );
2475 if ( rc != LDAP_URL_SUCCESS ) {
2476 rc = LDAP_PROTOCOL_ERROR;
2480 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2482 /* host part must be empty */
2483 /* extensions parts must be empty */
2484 rc = LDAP_PROTOCOL_ERROR;
2488 /* Grab the searchbase and see if an appropriate database can be found */
2489 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2490 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2491 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2492 BER_BVZERO( &op2.o_req_dn );
2493 if ( rc != LDAP_SUCCESS ) {
2497 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2498 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2499 rc = LDAP_NO_SUCH_OBJECT;
2503 /* Grab the filter */
2504 if ( ludp->lud_filter ) {
2505 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2506 cp->op->o_tmpmemctx );
2509 op2.ors_filterstr = defaultFilter_bv;
2512 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2513 if ( op2.ors_filter == NULL ) {
2514 rc = LDAP_PROTOCOL_ERROR;
2518 /* Grab the scope */
2519 op2.ors_scope = ludp->lud_scope;
2521 /* Grap the attributes */
2522 if ( ludp->lud_attrs ) {
2523 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2526 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2527 cp->op->o_tmpmemctx );
2529 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2530 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2531 anlistp[ nattrs ].an_desc = NULL;
2532 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2533 &anlistp[ nattrs ].an_desc, &text );
2534 if ( rc != LDAP_SUCCESS ) {
2543 anlistp[ nattrs ].an_name = desc->ad_cname;
2544 anlistp[ nattrs ].an_desc = desc;
2546 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2550 op2.o_hdr = cp->op->o_hdr;
2551 op2.o_tag = LDAP_REQ_SEARCH;
2552 op2.o_ndn = op2.o_bd->be_rootndn;
2553 op2.o_callback = &cb;
2554 op2.o_time = slap_get_time();
2555 op2.o_do_not_cache = 1;
2556 op2.o_is_auth_check = 0;
2557 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2558 op2.ors_slimit = SLAP_NO_LIMIT;
2559 op2.ors_tlimit = SLAP_NO_LIMIT;
2560 op2.ors_attrs = anlistp;
2561 op2.ors_attrsonly = 0;
2562 op2.o_private = cp->op->o_private;
2566 rc = op2.o_bd->be_search( &op2, &rs );
2572 if ( op2.ors_filter ) {
2573 filter_free_x( cp->op, op2.ors_filter );
2575 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2576 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2578 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2579 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2582 ldap_free_urldesc( ludp );
2584 if ( anlistp && anlistp != anlist ) {
2585 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2592 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2594 AciSetCookie *cp = (AciSetCookie *)cookie;
2595 BerVarray bvals = NULL;
2599 /* this routine needs to return the bervals instead of
2600 * plain strings, since syntax is not known. It should
2601 * also return the syntax or some "comparison cookie".
2603 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2604 if ( rc == LDAP_SUCCESS ) {
2605 if ( desc == slap_schema.si_ad_entryDN ) {
2606 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2607 cp->op->o_tmpmemctx );
2609 BER_BVZERO( &bvals[ 1 ] );
2613 backend_attribute( cp->op,
2614 cp->e, &ndn, desc, &bvals, ACL_NONE );
2617 if ( !BER_BVISNULL( &ndn ) ) {
2618 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2627 struct berval *subj,
2633 struct berval set = BER_BVNULL;
2635 AciSetCookie cookie;
2637 if ( setref == 0 ) {
2638 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2641 struct berval subjdn, ndn = BER_BVNULL;
2642 struct berval setat;
2645 AttributeDescription *desc = NULL;
2647 /* format of string is "entry/setAttrName" */
2648 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2652 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2653 setat = aci_bv_set_attr;
2657 * NOTE: dnNormalize honors the ber_len field
2658 * as the length of the dn to be normalized
2660 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2661 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2663 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2664 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2668 BER_BVZERO( &bvals[0] );
2669 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2671 bvals[0].bv_val = bvals[i-1].bv_val;
2672 BER_BVZERO( &bvals[i-1] );
2674 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2675 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2680 if ( !BER_BVISNULL( &set ) ) {
2683 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2684 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2685 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2691 #ifdef SLAPD_ACI_ENABLED
2693 aci_list_map_rights(
2694 struct berval *list )
2701 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2704 switch (*bv.bv_val) {
2706 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2709 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2710 * the right 's' to mean "set", but in the examples states
2711 * that the right 's' means "search". The latter definition
2714 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2717 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2720 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2723 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2724 * define any equivalent to the AUTH right, so I've just used
2727 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2739 struct berval *list,
2740 const struct berval *attr,
2741 struct berval *val )
2743 struct berval bv, left, right;
2746 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2747 if (aci_get_part(&bv, 0, '=', &left) < 0
2748 || aci_get_part(&bv, 1, '=', &right) < 0)
2750 if (ber_bvstrcasecmp(attr, &bv) == 0)
2752 } else if (val == NULL) {
2753 if (ber_bvstrcasecmp(attr, &left) == 0)
2756 if (ber_bvstrcasecmp(attr, &left) == 0) {
2757 /* this is experimental code that implements a
2758 * simple (prefix) match of the attribute value.
2759 * the ACI draft does not provide for aci's that
2760 * apply to specific values, but it would be
2761 * nice to have. If the <attr> part of an aci's
2762 * rights list is of the form <attr>=<value>,
2763 * that means the aci applies only to attrs with
2764 * the given value. Furthermore, if the attr is
2765 * of the form <attr>=<value>*, then <value> is
2766 * treated as a prefix, and the aci applies to
2767 * any value with that prefix.
2769 * Ideally, this would allow r.e. matches.
2771 if (aci_get_part(&right, 0, '*', &left) < 0
2772 || right.bv_len <= left.bv_len)
2774 if (ber_bvstrcasecmp(val, &right) == 0)
2776 } else if (val->bv_len >= left.bv_len) {
2777 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2786 static slap_access_t
2787 aci_list_get_attr_rights(
2788 struct berval *list,
2789 const struct berval *attr,
2790 struct berval *val )
2796 /* loop through each rights/attr pair, skip first part (action) */
2798 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2799 if (aci_list_has_attr(&bv, attr, val) == 0)
2801 if (aci_get_part(list, i, ';', &bv) < 0)
2803 mask |= aci_list_map_rights(&bv);
2809 aci_list_get_rights(
2810 struct berval *list,
2811 const struct berval *attr,
2813 slap_access_t *grant,
2814 slap_access_t *deny )
2816 struct berval perm, actn;
2817 slap_access_t *mask;
2820 if (attr == NULL || attr->bv_len == 0
2821 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2822 attr = &aci_bv_br_entry;
2828 /* loop through each permissions clause */
2829 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2830 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2832 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2834 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2841 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2842 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2849 struct berval *subj,
2850 struct berval *defgrpoc,
2851 struct berval *defgrpat,
2858 struct berval subjdn;
2859 struct berval grpoc;
2860 struct berval grpat;
2861 ObjectClass *grp_oc = NULL;
2862 AttributeDescription *grp_ad = NULL;
2866 /* format of string is "group/objectClassValue/groupAttrName" */
2867 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2871 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2875 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2879 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2880 if( rc != LDAP_SUCCESS ) {
2886 grp_oc = oc_bvfind( &grpoc );
2888 if (grp_oc != NULL && grp_ad != NULL ) {
2889 char buf[ACL_BUF_SIZE];
2890 struct berval bv, ndn;
2891 bv.bv_len = sizeof( buf ) - 1;
2892 bv.bv_val = (char *)&buf;
2893 if ( string_expand(&bv, &subjdn,
2894 e->e_ndn, nmatch, matches) )
2899 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2900 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2901 grp_oc, grp_ad ) == 0 );
2902 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2914 AttributeDescription *desc,
2918 regmatch_t *matches,
2919 slap_access_t *grant,
2920 slap_access_t *deny,
2921 slap_aci_scope_t asserted_scope
2924 struct berval bv, scope, perms, type, sdn;
2928 assert( !BER_BVISNULL( &desc->ad_cname ) );
2930 /* parse an aci of the form:
2931 oid # scope # action;rights;attr;rights;attr
2932 $ action;rights;attr;rights;attr # type # subject
2934 [NOTE: the following comment is very outdated,
2935 as the draft version it refers to (Ando, 2004-11-20)].
2937 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2938 a full description of the format for this attribute.
2939 Differences: "this" in the draft is "self" here, and
2940 "self" and "public" is in the position of type.
2942 <scope> = {entry|children|subtree}
2943 <type> = {public|users|access-id|subtree|onelevel|children|
2944 self|dnattr|group|role|set|set-ref}
2946 This routine now supports scope={ENTRY,CHILDREN}
2948 - ENTRY applies to "entry" and "subtree";
2949 - CHILDREN aplies to "children" and "subtree"
2952 /* check that the aci has all 5 components */
2953 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2957 /* check that the aci family is supported */
2958 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2962 /* check that the scope matches */
2963 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2967 /* note: scope can be either ENTRY or CHILDREN;
2968 * they respectively match "entry" and "children" in bv
2969 * both match "subtree" */
2970 switch ( asserted_scope ) {
2971 case SLAP_ACI_SCOPE_ENTRY:
2972 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2973 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2979 case SLAP_ACI_SCOPE_CHILDREN:
2980 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2981 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2991 /* get the list of permissions clauses, bail if empty */
2992 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2996 /* check if any permissions allow desired access */
2997 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
3001 /* see if we have a DN match */
3002 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
3006 /* see if we have a public (i.e. anonymous) access */
3007 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
3011 /* otherwise require an identity */
3012 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3016 /* see if we have a users access */
3017 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3021 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3022 * just grab all the berval up to its end (ITS#3303).
3023 * NOTE: the problem could be solved by providing the DN with
3024 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3025 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3027 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3031 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3032 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3034 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3037 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3038 if ( rc != LDAP_SUCCESS ) {
3042 if ( dn_match( &op->o_ndn, &ndn ) ) {
3045 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3049 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3052 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3053 if ( rc != LDAP_SUCCESS ) {
3057 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3060 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3064 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3065 struct berval ndn, pndn;
3067 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3068 if ( rc != LDAP_SUCCESS ) {
3072 dnParent( &ndn, &pndn );
3074 if ( dn_match( &op->o_ndn, &pndn ) ) {
3077 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3081 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3084 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3085 if ( rc != LDAP_SUCCESS ) {
3089 if ( !dn_match( &op->o_ndn, &ndn )
3090 && dnIsSuffix( &op->o_ndn, &ndn ) )
3094 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3098 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3099 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3103 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3105 AttributeDescription *ad = NULL;
3108 rc = slap_bv2ad( &sdn, &ad, &text );
3110 if( rc != LDAP_SUCCESS ) {
3116 for ( at = attrs_find( e->e_attrs, ad );
3118 at = attrs_find( at->a_next, ad ) )
3120 if ( value_find_ex( ad,
3121 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3122 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3124 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3133 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3134 if ( aci_group_member( &sdn, &aci_bv_group_class,
3135 &aci_bv_group_attr, op, e, nmatch, matches ) )
3140 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3141 if ( aci_group_member( &sdn, &aci_bv_role_class,
3142 &aci_bv_role_attr, op, e, nmatch, matches ) )
3147 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3148 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3152 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3153 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3163 * FIXME: there is a silly dependence that makes it difficult
3164 * to move ACIs in a run-time loadable module under the "dynacl"
3165 * umbrella, because sets share some helpers with ACIs.
3168 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3170 AttributeDescription *ad = NULL;
3171 const char *text = NULL;
3173 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3174 fprintf( stderr, "%s: line %d: "
3175 "inappropriate style \"%s\" in \"aci\" by clause\n",
3176 fname, lineno, sty );
3180 if ( right != NULL && *right != '\0' ) {
3181 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3183 "%s: line %d: aci \"%s\": %s\n",
3184 fname, lineno, right, text );
3189 ad = slap_schema.si_ad_aci;
3192 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3193 fprintf( stderr, "%s: line %d: "
3194 "aci \"%s\": inappropriate syntax: %s\n",
3195 fname, lineno, right,
3196 ad->ad_type->sat_syntax_oid );
3200 *privp = (void *)ad;
3206 dynacl_aci_unparse( void *priv, struct berval *bv )
3208 AttributeDescription *ad = ( AttributeDescription * )priv;
3213 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3214 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3215 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3216 bv->bv_len = ptr - bv->bv_val;
3227 AttributeDescription *desc,
3230 regmatch_t *matches,
3231 slap_access_t *grantp,
3232 slap_access_t *denyp )
3234 AttributeDescription *ad = ( AttributeDescription * )priv;
3236 slap_access_t tgrant, tdeny, grant, deny;
3238 char accessmaskbuf[ACCESSMASK_MAXLEN];
3239 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3240 #endif /* LDAP_DEBUG */
3242 /* start out with nothing granted, nothing denied */
3246 /* get the aci attribute */
3247 at = attr_find( e->e_attrs, ad );
3251 /* the aci is an multi-valued attribute. The
3252 * rights are determined by OR'ing the individual
3253 * rights given by the acis.
3255 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3256 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3257 nmatch, matches, &grant, &deny,
3258 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3265 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3266 accessmask2str( tgrant, accessmaskbuf, 1 ),
3267 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3270 /* If the entry level aci didn't contain anything valid for the
3271 * current operation, climb up the tree and evaluate the
3272 * acis with scope set to subtree
3274 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3275 struct berval parent_ndn;
3278 /* to solve the chicken'n'egg problem of accessing
3279 * the OpenLDAPaci attribute, the direct access
3280 * to the entry's attribute is unchecked; however,
3281 * further accesses to OpenLDAPaci values in the
3282 * ancestors occur through backend_attribute(), i.e.
3283 * with the identity of the operation, requiring
3284 * further access checking. For uniformity, this
3285 * makes further requests occur as the rootdn, if
3286 * any, i.e. searching for the OpenLDAPaci attribute
3287 * is considered an internal search. If this is not
3288 * acceptable, then the same check needs be performed
3289 * when accessing the entry's attribute. */
3290 Operation op2 = *op;
3292 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3293 op2.o_dn = op->o_bd->be_rootdn;
3294 op2.o_ndn = op->o_bd->be_rootndn;
3298 dnParent( &e->e_nname, &parent_ndn );
3299 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3301 BerVarray bvals = NULL;
3304 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3305 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3314 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3315 if ( aci_mask( op, e, desc, val,
3319 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3323 /* evaluation stops as soon as either a "deny" or a
3324 * "grant" directive matches.
3326 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3330 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3331 accessmask2str( tgrant, accessmaskbuf, 1 ),
3332 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3336 case LDAP_NO_SUCH_ATTRIBUTE:
3337 /* just go on if the aci-Attribute is not present in
3340 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3344 case LDAP_NO_SUCH_OBJECT:
3345 /* We have reached the base object */
3346 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3358 dnParent( &parent_ndn, &parent_ndn );
3368 /* need to register this at some point */
3369 static slap_dynacl_t dynacl_aci = {
3379 #endif /* SLAP_DYNACL */
3381 #endif /* SLAPD_ACI_ENABLED */
3386 * dynamic ACL infrastructure
3388 static slap_dynacl_t *da_list = NULL;
3391 slap_dynacl_register( slap_dynacl_t *da )
3395 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3396 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3401 if ( tmp != NULL ) {
3405 if ( da->da_mask == NULL ) {
3409 da->da_private = NULL;
3410 da->da_next = da_list;
3416 static slap_dynacl_t *
3417 slap_dynacl_next( slap_dynacl_t *da )
3426 slap_dynacl_get( const char *name )
3430 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3431 if ( strcasecmp( da->da_name, name ) == 0 ) {
3438 #endif /* SLAP_DYNACL */
3445 slap_dynacl_t *known_dynacl[] = {
3446 #ifdef SLAPD_ACI_ENABLED
3448 #endif /* SLAPD_ACI_ENABLED */
3452 for ( i = 0; known_dynacl[ i ]; i++ ) {
3453 rc = slap_dynacl_register( known_dynacl[ i ] );
3458 #endif /* SLAP_DYNACL */
3469 regmatch_t *matches)
3477 bv->bv_val[0] = '\0';
3478 bv->bv_len--; /* leave space for lone $ */
3481 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3482 sp < pat->bv_val + pat->bv_len ; sp++ )
3484 /* did we previously see a $ */
3486 if ( flag == 1 && *sp == '$' ) {
3491 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3494 } else if ( *sp >= '0' && *sp <= '9' ) {
3502 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3503 if ( *sp >= '0' && *sp <= '9' ) {
3504 n = 10*n + ( *sp - '0' );
3508 if ( *sp != /*'{'*/ '}' ) {
3514 if ( n >= nmatch ) {
3520 i = matches[n].rm_so;
3521 l = matches[n].rm_eo;
3522 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3540 /* must have ended with a single $ */
3548 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3549 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3556 struct berval *pat, /* pattern to expand and match against */
3557 char *str, /* string to match against pattern */
3558 char *buf, /* buffer with $N expansion variables */
3559 int nmatch, /* size of the matches array */
3560 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3564 char newbuf[ACL_BUF_SIZE];
3568 bv.bv_len = sizeof( newbuf ) - 1;
3575 string_expand( &bv, pat, buf, nmatch, matches );
3576 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3578 char error[ACL_BUF_SIZE];
3579 regerror( rc, &re, error, sizeof( error ) );
3581 Debug( LDAP_DEBUG_TRACE,
3582 "compile( \"%s\", \"%s\") failed %s\n",
3583 pat->bv_val, str, error );
3587 rc = regexec( &re, str, 0, NULL, 0 );
3590 Debug( LDAP_DEBUG_TRACE,
3591 "=> regex_matches: string: %s\n", str, 0, 0 );
3592 Debug( LDAP_DEBUG_TRACE,
3593 "=> regex_matches: rc: %d %s\n",
3594 rc, !rc ? "matches" : "no matches", 0 );