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 * 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 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 * 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 = 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 = 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 assert( op->o_bd->bd_info->bi_access_allowed );
447 /* delegate to backend */
448 ret = op->o_bd->bd_info->bi_access_allowed( op, e, desc, val, access, state, &mask );
450 if ( ACL_IS_INVALID( mask ) ) {
451 Debug( LDAP_DEBUG_ACL,
452 "=> access_allowed: \"%s\" (%s) invalid!\n",
456 } else if ( control == ACL_BREAK ) {
457 Debug( LDAP_DEBUG_ACL,
458 "=> access_allowed: no more rules\n", 0, 0, 0 );
463 ret = ACL_GRANT( mask, access );
466 Debug( LDAP_DEBUG_ACL,
467 "=> access_allowed: %s access %s by %s\n",
468 access2str( access ), ret ? "granted" : "denied",
469 accessmask2str( mask, accessmaskbuf, 1 ) );
472 if ( state != NULL ) {
473 /* If not value-dependent, save ACL in case of more attrs */
474 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
475 state->as_vi_acl = a;
476 state->as_result = ret;
478 state->as_recorded |= ACL_STATE_RECORDED;
480 if ( be_null ) op->o_bd = NULL;
481 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
485 #else /* !SLAP_OVERLAY_ACCESS */
491 AttributeDescription *desc,
493 slap_access_t access,
494 AccessControlState *state,
499 AccessControl *a = NULL;
504 char accessmaskbuf[ACCESSMASK_MAXLEN];
507 slap_control_t control;
508 slap_access_t access_level;
510 regmatch_t matches[MAXREMATCHES];
511 int st_same_attr = 0;
512 static AccessControlState state_init = ACL_STATE_INIT;
515 assert( desc != NULL );
517 access_level = ACL_LEVEL( access );
519 assert( access_level > ACL_NONE );
520 if ( maskp ) ACL_INVALIDATE( *maskp );
522 attr = desc->ad_cname.bv_val;
524 assert( attr != NULL );
526 if ( op && op->o_is_auth_check &&
527 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
533 if ( state->as_vd_ad == desc ) {
534 if ( state->as_recorded ) {
535 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
538 return state->as_result;
540 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
541 val != NULL && state->as_vd_acl == NULL )
543 return state->as_result;
551 state->as_vd_ad=desc;
554 Debug( LDAP_DEBUG_ACL,
555 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
556 access2str( access ), e->e_dn, attr );
565 be = LDAP_STAILQ_FIRST(&backendDB);
569 * FIXME: experimental; use first backend rules
570 * iff there is no global_acl (ITS#3100) */
571 if ( frontendDB->be_acl == NULL )
577 assert( be != NULL );
580 if ( op->o_pb != NULL ) {
581 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
583 /* ACL plugin denied access */
587 #endif /* LDAP_SLAPI */
589 /* grant database root access */
590 if ( be_isroot( op ) ) {
591 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
593 mask = ACL_LVL_MANAGE;
600 * no-user-modification operational attributes are ignored
601 * by ACL_WRITE checking as any found here are not provided
604 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
605 && desc != slap_schema.si_ad_entry
606 && desc != slap_schema.si_ad_children )
608 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
609 " %s access granted\n",
614 /* use backend default access if no backend acls */
615 if ( be->be_acl == NULL ) {
616 Debug( LDAP_DEBUG_ACL,
617 "=> access_allowed: backend default %s "
618 "access %s to \"%s\"\n",
619 access2str( access ),
620 be->be_dfltaccess >= access_level ? "granted" : "denied",
621 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
622 ret = be->be_dfltaccess >= access_level;
627 mask = ACL_PRIV_LEVEL;
628 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
629 mask |= ACL_ACCESS2PRIV( i );
636 /* be is always non-NULL */
637 /* use global default access if no global acls */
638 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
639 Debug( LDAP_DEBUG_ACL,
640 "=> access_allowed: global default %s access %s to \"%s\"\n",
641 access2str( access ),
642 frontendDB->be_dfltaccess >= access_level ?
643 "granted" : "denied", op->o_dn.bv_val );
644 ret = frontendDB->be_dfltaccess >= access_level;
649 mask = ACL_PRIV_LEVEL;
650 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
651 mask |= ACL_ACCESS2PRIV( i );
662 if ( st_same_attr ) {
663 assert( state->as_vd_acl != NULL );
665 a = state->as_vd_acl;
666 count = state->as_vd_acl_count;
667 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
668 mask = state->as_vd_acl_mask;
669 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
674 if ( state ) state->as_vi_acl = NULL;
678 memset( matches, '\0', sizeof(matches) );
681 while ( ( a = acl_get( a, &count, op, e, desc, val,
682 MAXREMATCHES, matches, state ) ) != NULL )
686 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
687 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
688 (int)matches[i].rm_so, (int)matches[i].rm_eo );
689 if ( matches[i].rm_so <= matches[0].rm_eo ) {
691 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
692 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
695 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
699 if ( state->as_vi_acl == a &&
700 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
702 Debug( LDAP_DEBUG_ACL,
703 "access_allowed: result from state (%s)\n",
705 ret = state->as_result;
708 Debug( LDAP_DEBUG_ACL,
709 "access_allowed: no res from state (%s)\n",
715 control = acl_mask( a, &mask, op,
716 e, desc, val, MAXREMATCHES, matches, count, state );
718 if ( control != ACL_BREAK ) {
722 memset( matches, '\0', sizeof(matches) );
725 if ( ACL_IS_INVALID( mask ) ) {
726 Debug( LDAP_DEBUG_ACL,
727 "=> access_allowed: \"%s\" (%s) invalid!\n",
731 } else if ( control == ACL_BREAK ) {
732 Debug( LDAP_DEBUG_ACL,
733 "=> access_allowed: no more rules\n", 0, 0, 0 );
738 Debug( LDAP_DEBUG_ACL,
739 "=> access_allowed: %s access %s by %s\n",
740 access2str( access ),
741 ACL_GRANT(mask, access) ? "granted" : "denied",
742 accessmask2str( mask, accessmaskbuf, 1 ) );
744 ret = ACL_GRANT(mask, access);
747 if ( state != NULL ) {
748 /* If not value-dependent, save ACL in case of more attrs */
749 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
750 state->as_vi_acl = a;
751 state->as_result = ret;
753 state->as_recorded |= ACL_STATE_RECORDED;
755 if ( be_null ) op->o_bd = NULL;
756 if ( maskp ) *maskp = mask;
760 #endif /* SLAP_OVERLAY_ACCESS */
763 * acl_get - return the acl applicable to entry e, attribute
764 * attr. the acl returned is suitable for use in subsequent calls to
765 * acl_access_allowed().
768 static AccessControl *
774 AttributeDescription *desc,
778 AccessControlState *state )
785 assert( count != NULL );
786 assert( desc != NULL );
788 attr = desc->ad_cname.bv_val;
790 assert( attr != NULL );
793 if( op->o_bd == NULL ) {
794 a = frontendDB->be_acl;
796 a = op->o_bd->be_acl;
807 dnlen = e->e_nname.bv_len;
809 for ( ; a != NULL; a = a->acl_next ) {
812 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
813 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
814 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
815 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
816 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
820 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
821 *count, a->acl_dn_pat.bv_val, 0 );
822 patlen = a->acl_dn_pat.bv_len;
823 if ( dnlen < patlen )
826 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
827 /* base dn -- entire object DN must match */
828 if ( dnlen != patlen )
831 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
832 int rdnlen = -1, sep = 0;
834 if ( dnlen <= patlen )
838 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
843 rdnlen = dn_rdnlen( NULL, &e->e_nname );
844 if ( rdnlen != dnlen - patlen - sep )
847 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
848 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
851 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
852 if ( dnlen <= patlen )
854 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
858 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
862 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
866 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
867 matches[0].rm_so = matches[0].rm_eo = -1;
871 /* Is this ACL only for a specific value? */
872 if ( a->acl_attrval.bv_len ) {
877 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
878 state->as_recorded |= ACL_STATE_RECORDED_VD;
879 state->as_vd_acl = prev;
880 state->as_vd_acl_count = *count;
881 state->as_vd_access = a->acl_access;
882 state->as_vd_access_count = 1;
883 ACL_INVALIDATE( state->as_vd_acl_mask );
886 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
887 Debug( LDAP_DEBUG_ACL,
888 "acl_get: valpat %s\n",
889 a->acl_attrval.bv_val, 0, 0 );
890 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
898 Debug( LDAP_DEBUG_ACL,
900 a->acl_attrval.bv_val, 0, 0 );
902 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
903 if (value_match( &match, desc,
904 desc->ad_type->sat_equality, 0,
905 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
912 patlen = a->acl_attrval.bv_len;
913 vdnlen = val->bv_len;
915 if ( vdnlen < patlen )
918 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
919 if ( vdnlen > patlen )
922 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
925 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
928 rdnlen = dn_rdnlen( NULL, val );
929 if ( rdnlen != vdnlen - patlen - 1 )
932 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
933 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
936 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
937 if ( vdnlen <= patlen )
940 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
944 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
950 if ( a->acl_filter != NULL ) {
951 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
952 if ( rc != LDAP_COMPARE_TRUE ) {
957 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
962 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
974 struct berval *opndn )
977 * if access applies to the entry itself, and the
978 * user is bound as somebody in the same namespace as
979 * the entry, OR the given dn matches the dn pattern
982 * NOTE: styles "anonymous", "users" and "self"
983 * have been moved to enum slap_style_t, whose
984 * value is set in a_dn_style; however, the string
985 * is maintaned in a_dn_pat.
987 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
988 if ( !BER_BVISEMPTY( opndn ) ) {
992 } else if ( b->a_style == ACL_STYLE_USERS ) {
993 if ( BER_BVISEMPTY( opndn ) ) {
997 } else if ( b->a_style == ACL_STYLE_SELF ) {
998 struct berval ndn, selfndn;
1001 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1005 level = b->a_self_level;
1013 selfndn = e->e_nname;
1016 for ( ; level > 0; level-- ) {
1017 if ( BER_BVISEMPTY( &ndn ) ) {
1020 dnParent( &ndn, &ndn );
1023 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1028 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1029 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1031 regmatch_t tmp_matches[2],
1032 *tmp_matchesp = tmp_matches;
1036 switch ( a->acl_dn_style ) {
1037 case ACL_STYLE_REGEX:
1038 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1039 tmp_matchesp = matches;
1040 tmp_nmatch = nmatch;
1043 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1045 case ACL_STYLE_BASE:
1046 tmp_matches[0].rm_so = 0;
1047 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1052 case ACL_STYLE_SUBTREE:
1053 case ACL_STYLE_CHILDREN:
1054 tmp_matches[0].rm_so = 0;
1055 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1056 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1057 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1071 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1072 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1080 ber_len_t patlen, odnlen;
1083 if ( e->e_dn == NULL )
1086 if ( b->a_expand ) {
1088 char buf[ACL_BUF_SIZE];
1091 regmatch_t tmp_matches[2],
1092 *tmp_matchesp = tmp_matches;
1096 bv.bv_len = sizeof( buf ) - 1;
1099 switch ( a->acl_dn_style ) {
1100 case ACL_STYLE_REGEX:
1101 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1102 tmp_matchesp = matches;
1103 tmp_nmatch = nmatch;
1106 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1108 case ACL_STYLE_BASE:
1109 tmp_matches[0].rm_so = 0;
1110 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1115 case ACL_STYLE_SUBTREE:
1116 case ACL_STYLE_CHILDREN:
1117 tmp_matches[0].rm_so = 0;
1118 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1119 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1120 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1134 if ( string_expand( &bv, &b->a_pat,
1136 tmp_nmatch, tmp_matchesp ) )
1141 if ( dnNormalize(0, NULL, NULL, &bv,
1142 &pat, op->o_tmpmemctx )
1145 /* did not expand to a valid dn */
1153 patlen = pat.bv_len;
1154 odnlen = opndn->bv_len;
1155 if ( odnlen < patlen ) {
1156 goto dn_match_cleanup;
1160 if ( b->a_style == ACL_STYLE_BASE ) {
1161 /* base dn -- entire object DN must match */
1162 if ( odnlen != patlen ) {
1163 goto dn_match_cleanup;
1166 } else if ( b->a_style == ACL_STYLE_ONE ) {
1169 if ( odnlen <= patlen ) {
1170 goto dn_match_cleanup;
1173 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1174 goto dn_match_cleanup;
1177 rdnlen = dn_rdnlen( NULL, opndn );
1178 if ( rdnlen != odnlen - patlen - 1 ) {
1179 goto dn_match_cleanup;
1182 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1183 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1184 goto dn_match_cleanup;
1187 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1188 if ( odnlen <= patlen ) {
1189 goto dn_match_cleanup;
1192 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1193 goto dn_match_cleanup;
1196 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1200 if ( odnlen <= patlen ) {
1201 goto dn_match_cleanup;
1204 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1206 goto dn_match_cleanup;
1211 for ( ; level > 0; level-- ) {
1212 if ( BER_BVISEMPTY( &ndn ) ) {
1213 goto dn_match_cleanup;
1215 dnParent( &ndn, &ndn );
1216 if ( ndn.bv_len < patlen ) {
1217 goto dn_match_cleanup;
1221 if ( ndn.bv_len != patlen ) {
1222 goto dn_match_cleanup;
1226 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1229 if ( pat.bv_val != b->a_pat.bv_val ) {
1230 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1242 * Record value-dependent access control state
1244 #define ACL_RECORD_VALUE_STATE do { \
1245 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1246 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1247 state->as_vd_acl = a; \
1248 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1249 sizeof( state->as_vd_acl_matches )) ; \
1250 state->as_vd_acl_count = count; \
1251 state->as_vd_access = b; \
1252 state->as_vd_access_count = i; \
1264 regmatch_t *matches,
1266 AccessControlState *state,
1267 slap_dn_access *bdn,
1268 struct berval *opndn )
1274 const char *attr = bdn->a_at->ad_cname.bv_val;
1276 assert( attr != NULL );
1278 if ( BER_BVISEMPTY( opndn ) ) {
1282 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1285 /* see if asker is listed in dnattr */
1286 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1288 at = attrs_find( at->a_next, bdn->a_at ) )
1290 if ( value_find_ex( bdn->a_at,
1291 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1292 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1294 &bv, op->o_tmpmemctx ) == 0 )
1303 /* have a dnattr match. if this is a self clause then
1304 * the target must also match the op dn.
1306 if ( bdn->a_self ) {
1307 /* check if the target is an attribute. */
1308 if ( val == NULL ) return 1;
1310 /* target is attribute, check if the attribute value
1313 rc = value_match( &match, bdn->a_at,
1314 bdn->a_at->ad_type->sat_equality, 0,
1316 /* on match error or no match, fail the ACL clause */
1317 if ( rc != LDAP_SUCCESS || match != 0 )
1322 /* no dnattr match, check if this is a self clause */
1323 if ( ! bdn->a_self )
1326 ACL_RECORD_VALUE_STATE;
1328 /* this is a self clause, check if the target is an
1334 /* target is attribute, check if the attribute value
1337 rc = value_match( &match, bdn->a_at,
1338 bdn->a_at->ad_type->sat_equality, 0,
1341 /* on match error or no match, fail the ACL clause */
1342 if ( rc != LDAP_SUCCESS || match != 0 )
1351 * acl_mask - modifies mask based upon the given acl and the
1352 * requested access to entry e, attribute attr, value val. if val
1353 * is null, access to the whole attribute is assumed (all values).
1355 * returns 0 access NOT allowed
1359 static slap_control_t
1365 AttributeDescription *desc,
1368 regmatch_t *matches,
1370 AccessControlState *state )
1375 char accessmaskbuf[ACCESSMASK_MAXLEN];
1376 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1377 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1378 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1382 assert( a != NULL );
1383 assert( mask != NULL );
1384 assert( desc != NULL );
1386 attr = desc->ad_cname.bv_val;
1388 assert( attr != NULL );
1390 Debug( LDAP_DEBUG_ACL,
1391 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1394 Debug( LDAP_DEBUG_ACL,
1395 "=> acl_mask: to %s by \"%s\", (%s) \n",
1396 val ? "value" : "all values",
1397 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1398 accessmask2str( *mask, accessmaskbuf, 1 ) );
1401 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1402 && state->as_vd_acl == a )
1404 b = state->as_vd_access;
1405 i = state->as_vd_access_count;
1412 for ( ; b != NULL; b = b->a_next, i++ ) {
1413 slap_mask_t oldmask, modmask;
1415 ACL_INVALIDATE( modmask );
1417 /* AND <who> clauses */
1418 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1419 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1420 b->a_dn_pat.bv_val, 0, 0);
1422 * if access applies to the entry itself, and the
1423 * user is bound as somebody in the same namespace as
1424 * the entry, OR the given dn matches the dn pattern
1427 * NOTE: styles "anonymous", "users" and "self"
1428 * have been moved to enum slap_style_t, whose
1429 * value is set in a_dn_style; however, the string
1430 * is maintaned in a_dn_pat.
1433 if ( acl_mask_dn( op, e, a, nmatch, matches,
1434 &b->a_dn, &op->o_ndn ) )
1440 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1443 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1444 b->a_realdn_pat.bv_val, 0, 0);
1446 * if access applies to the entry itself, and the
1447 * user is bound as somebody in the same namespace as
1448 * the entry, OR the given dn matches the dn pattern
1451 * NOTE: styles "anonymous", "users" and "self"
1452 * have been moved to enum slap_style_t, whose
1453 * value is set in a_dn_style; however, the string
1454 * is maintaned in a_dn_pat.
1457 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1459 ndn = op->o_conn->c_ndn;
1464 if ( acl_mask_dn( op, e, a, nmatch, matches,
1465 &b->a_realdn, &ndn ) )
1471 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1472 if ( ! op->o_conn->c_listener ) {
1475 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1476 b->a_sockurl_pat.bv_val, 0, 0 );
1478 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1479 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1480 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1481 e->e_ndn, nmatch, matches ) )
1486 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1488 char buf[ACL_BUF_SIZE];
1490 bv.bv_len = sizeof( buf ) - 1;
1492 if ( string_expand( &bv, &b->a_sockurl_pat,
1493 e->e_ndn, nmatch, matches ) )
1498 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1504 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1512 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1513 if ( !op->o_conn->c_peer_domain.bv_val ) {
1516 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1517 b->a_domain_pat.bv_val, 0, 0 );
1518 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1519 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1520 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1521 e->e_ndn, nmatch, matches ) )
1526 char buf[ACL_BUF_SIZE];
1528 struct berval cmp = op->o_conn->c_peer_domain;
1529 struct berval pat = b->a_domain_pat;
1531 if ( b->a_domain_expand ) {
1534 bv.bv_len = sizeof(buf) - 1;
1537 if ( string_expand(&bv, &b->a_domain_pat,
1538 e->e_ndn, nmatch, matches) )
1545 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1546 int offset = cmp.bv_len - pat.bv_len;
1551 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1555 /* trim the domain */
1556 cmp.bv_val = &cmp.bv_val[ offset ];
1557 cmp.bv_len -= offset;
1560 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1567 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1568 if ( !op->o_conn->c_peer_name.bv_val ) {
1571 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1572 b->a_peername_pat.bv_val, 0, 0 );
1573 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1574 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1575 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1576 e->e_ndn, nmatch, matches ) )
1582 /* try exact match */
1583 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1584 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1588 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1590 char buf[ACL_BUF_SIZE];
1592 bv.bv_len = sizeof( buf ) - 1;
1594 if ( string_expand( &bv, &b->a_peername_pat,
1595 e->e_ndn, nmatch, matches ) )
1600 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1604 /* extract IP and try exact match */
1605 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1607 char buf[] = "255.255.255.255";
1610 int port_number = -1;
1612 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1613 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1616 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1617 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1619 port = strrchr( ip.bv_val, ':' );
1623 ip.bv_len = port - ip.bv_val;
1625 port_number = strtol( port, &next, 10 );
1626 if ( next[0] != '\0' )
1630 /* the port check can be anticipated here */
1631 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1634 /* address longer than expected? */
1635 if ( ip.bv_len >= sizeof(buf) )
1638 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1639 buf[ ip.bv_len ] = '\0';
1641 addr = inet_addr( buf );
1643 /* unable to convert? */
1644 if ( addr == (unsigned long)(-1) )
1647 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1650 #ifdef LDAP_PF_LOCAL
1651 /* extract path and try exact match */
1652 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1655 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1656 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1659 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1660 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1662 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1665 #endif /* LDAP_PF_LOCAL */
1667 /* exact match (very unlikely...) */
1668 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1675 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1676 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1679 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1680 b->a_sockname_pat.bv_val, 0, 0 );
1681 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1682 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1683 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1684 e->e_ndn, nmatch, matches ) )
1689 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1691 char buf[ACL_BUF_SIZE];
1693 bv.bv_len = sizeof( buf ) - 1;
1695 if ( string_expand( &bv, &b->a_sockname_pat,
1696 e->e_ndn, nmatch, matches ) )
1701 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1706 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1713 if ( b->a_dn_at != NULL ) {
1714 if ( acl_mask_dnattr( op, e, val, a, b, i,
1715 matches, count, state,
1716 &b->a_dn, &op->o_ndn ) )
1722 if ( b->a_realdn_at != NULL ) {
1725 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1727 ndn = op->o_conn->c_ndn;
1732 if ( acl_mask_dnattr( op, e, val, a, b, i,
1733 matches, count, state,
1734 &b->a_realdn, &ndn ) )
1740 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1742 struct berval ndn = BER_BVNULL;
1745 if ( op->o_ndn.bv_len == 0 ) {
1749 /* b->a_group is an unexpanded entry name, expanded it should be an
1750 * entry with objectclass group* and we test to see if odn is one of
1751 * the values in the attribute group
1753 /* see if asker is listed in dnattr */
1754 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1755 char buf[ACL_BUF_SIZE];
1757 regmatch_t tmp_matches[2],
1758 *tmp_matchesp = tmp_matches;
1760 bv.bv_len = sizeof(buf) - 1;
1765 switch ( a->acl_dn_style ) {
1766 case ACL_STYLE_REGEX:
1767 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1768 tmp_matchesp = matches;
1769 tmp_nmatch = nmatch;
1773 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1774 case ACL_STYLE_BASE:
1775 tmp_matches[0].rm_so = 0;
1776 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1781 case ACL_STYLE_SUBTREE:
1782 case ACL_STYLE_CHILDREN:
1783 tmp_matches[0].rm_so = 0;
1784 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1785 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1786 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1800 if ( string_expand( &bv, &b->a_group_pat,
1802 tmp_nmatch, tmp_matchesp ) )
1807 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1808 op->o_tmpmemctx ) != LDAP_SUCCESS )
1810 /* did not expand to a valid dn */
1817 bv = b->a_group_pat;
1820 rc = backend_group( op, e, &bv, &op->o_ndn,
1821 b->a_group_oc, b->a_group_at );
1824 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1832 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1834 char buf[ACL_BUF_SIZE];
1836 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1838 regmatch_t tmp_matches[2],
1839 *tmp_matchesp = tmp_matches;
1842 bv.bv_len = sizeof( buf ) - 1;
1847 switch ( a->acl_dn_style ) {
1848 case ACL_STYLE_REGEX:
1849 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1850 tmp_matchesp = matches;
1851 tmp_nmatch = nmatch;
1855 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1856 case ACL_STYLE_BASE:
1857 tmp_matches[0].rm_so = 0;
1858 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1863 case ACL_STYLE_SUBTREE:
1864 case ACL_STYLE_CHILDREN:
1865 tmp_matches[0].rm_so = 0;
1866 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1867 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1868 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1882 if ( string_expand( &bv, &b->a_set_pat,
1884 tmp_nmatch, tmp_matchesp ) )
1893 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1898 if ( b->a_authz.sai_ssf ) {
1899 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1900 b->a_authz.sai_ssf, op->o_ssf, 0 );
1901 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1906 if ( b->a_authz.sai_transport_ssf ) {
1907 Debug( LDAP_DEBUG_ACL,
1908 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1909 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1910 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1915 if ( b->a_authz.sai_tls_ssf ) {
1916 Debug( LDAP_DEBUG_ACL,
1917 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1918 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1919 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1924 if ( b->a_authz.sai_sasl_ssf ) {
1925 Debug( LDAP_DEBUG_ACL,
1926 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1927 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1928 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1934 if ( b->a_dynacl ) {
1936 slap_access_t tgrant, tdeny;
1938 /* this case works different from the others above.
1939 * since aci's themselves give permissions, we need
1940 * to first check b->a_access_mask, the ACL's access level.
1942 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1943 /* no ACIs in the root DSE */
1947 /* first check if the right being requested
1948 * is allowed by the ACL clause.
1950 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1954 /* start out with nothing granted, nothing denied */
1958 for ( da = b->a_dynacl; da; da = da->da_next ) {
1959 slap_access_t grant, deny;
1961 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1967 /* remove anything that the ACL clause does not allow */
1968 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1969 tdeny &= ACL_PRIV_MASK;
1971 /* see if we have anything to contribute */
1972 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1976 /* this could be improved by changing acl_mask so that it can deal with
1977 * by clauses that return grant/deny pairs. Right now, it does either
1978 * additive or subtractive rights, but not both at the same time. So,
1979 * we need to combine the grant/deny pair into a single rights mask in
1980 * a smart way: if either grant or deny is "empty", then we use the
1981 * opposite as is, otherwise we remove any denied rights from the grant
1982 * rights mask and construct an additive mask.
1984 if (ACL_IS_INVALID(tdeny)) {
1985 modmask = tgrant | ACL_PRIV_ADDITIVE;
1987 } else if (ACL_IS_INVALID(tgrant)) {
1988 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1991 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1995 #else /* !SLAP_DYNACL */
1997 #ifdef SLAPD_ACI_ENABLED
1998 if ( b->a_aci_at != NULL ) {
2000 slap_access_t grant, deny, tgrant, tdeny;
2001 struct berval parent_ndn;
2002 BerVarray bvals = NULL;
2005 /* this case works different from the others above.
2006 * since aci's themselves give permissions, we need
2007 * to first check b->a_access_mask, the ACL's access level.
2010 if ( BER_BVISEMPTY( &e->e_nname ) ) {
2011 /* no ACIs in the root DSE */
2015 /* first check if the right being requested
2016 * is allowed by the ACL clause.
2018 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2021 /* start out with nothing granted, nothing denied */
2025 /* get the aci attribute */
2026 at = attr_find( e->e_attrs, b->a_aci_at );
2029 /* FIXME: this breaks acl caching;
2030 * see also ACL_RECORD_VALUE_STATE below */
2031 ACL_RECORD_VALUE_STATE;
2033 /* the aci is an multi-valued attribute. The
2034 * rights are determined by OR'ing the individual
2035 * rights given by the acis.
2037 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2042 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2048 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2049 accessmask2str(tgrant, accessmaskbuf, 1),
2050 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2053 /* If the entry level aci didn't contain anything valid for the
2054 * current operation, climb up the tree and evaluate the
2055 * acis with scope set to subtree
2057 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2058 dnParent( &e->e_nname, &parent_ndn );
2059 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2060 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2061 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2069 for( i = 0; bvals[i].bv_val != NULL; i++){
2071 /* FIXME: this breaks acl caching;
2072 * see also ACL_RECORD_VALUE_STATE above */
2073 ACL_RECORD_VALUE_STATE;
2075 if (aci_mask(op, e, desc, val, &bvals[i],
2077 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2081 /* evaluation stops as soon as either a "deny" or a
2082 * "grant" directive matches.
2084 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2088 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2089 accessmask2str(tgrant, accessmaskbuf, 1),
2090 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2094 case LDAP_NO_SUCH_ATTRIBUTE:
2095 /* just go on if the aci-Attribute is not present in
2098 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2102 case LDAP_NO_SUCH_OBJECT:
2103 /* We have reached the base object */
2104 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2115 dnParent( &parent_ndn, &parent_ndn );
2120 /* remove anything that the ACL clause does not allow */
2121 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2122 tdeny &= ACL_PRIV_MASK;
2124 /* see if we have anything to contribute */
2125 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2129 /* this could be improved by changing acl_mask so that it can deal with
2130 * by clauses that return grant/deny pairs. Right now, it does either
2131 * additive or subtractive rights, but not both at the same time. So,
2132 * we need to combine the grant/deny pair into a single rights mask in
2133 * a smart way: if either grant or deny is "empty", then we use the
2134 * opposite as is, otherwise we remove any denied rights from the grant
2135 * rights mask and construct an additive mask.
2137 if (ACL_IS_INVALID(tdeny)) {
2138 modmask = tgrant | ACL_PRIV_ADDITIVE;
2140 } else if (ACL_IS_INVALID(tgrant)) {
2141 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2144 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2148 #endif /* SLAPD_ACI_ENABLED */
2149 #endif /* !SLAP_DYNACL */
2151 modmask = b->a_access_mask;
2154 Debug( LDAP_DEBUG_ACL,
2155 "<= acl_mask: [%d] applying %s (%s)\n",
2156 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2157 b->a_type == ACL_CONTINUE
2159 : b->a_type == ACL_BREAK
2165 if( ACL_IS_ADDITIVE(modmask) ) {
2167 ACL_PRIV_SET( *mask, modmask );
2170 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2172 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2173 /* substract privs */
2174 ACL_PRIV_CLR( *mask, modmask );
2177 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2184 Debug( LDAP_DEBUG_ACL,
2185 "<= acl_mask: [%d] mask: %s\n",
2186 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2188 if( b->a_type == ACL_CONTINUE ) {
2191 } else if ( b->a_type == ACL_BREAK ) {
2199 /* implicit "by * none" clause */
2202 Debug( LDAP_DEBUG_ACL,
2203 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2204 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2209 * acl_check_modlist - check access control on the given entry to see if
2210 * it allows the given modifications by the user associated with op.
2211 * returns 1 if mods allowed ok
2212 * 0 mods not allowed
2219 Modifications *mlist
2223 AccessControlState state = ACL_STATE_INIT;
2226 int ret = 1; /* default is access allowed */
2230 be = LDAP_STAILQ_FIRST(&backendDB);
2234 assert( be != NULL );
2236 /* short circuit root database access */
2237 if ( be_isroot( op ) ) {
2238 Debug( LDAP_DEBUG_ACL,
2239 "<= acl_access_allowed: granted to database root\n",
2244 /* use backend default access if no backend acls */
2245 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2246 Debug( LDAP_DEBUG_ACL,
2247 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2248 access2str( ACL_WRITE ),
2249 op->o_bd->be_dfltaccess >= ACL_WRITE
2250 ? "granted" : "denied",
2252 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2256 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2258 * no-user-modification operational attributes are ignored
2259 * by ACL_WRITE checking as any found here are not provided
2262 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2263 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2264 " modify access granted\n",
2265 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2269 switch ( mlist->sml_op ) {
2270 case LDAP_MOD_REPLACE:
2272 * We must check both permission to delete the whole
2273 * attribute and permission to add the specific attributes.
2274 * This prevents abuse from selfwriters.
2276 if ( ! access_allowed( op, e,
2277 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2283 if ( mlist->sml_values == NULL ) break;
2285 /* fall thru to check value to add */
2288 assert( mlist->sml_values != NULL );
2290 for ( bv = mlist->sml_nvalues
2291 ? mlist->sml_nvalues : mlist->sml_values;
2292 bv->bv_val != NULL; bv++ )
2294 if ( ! access_allowed( op, e,
2295 mlist->sml_desc, bv, ACL_WADD, &state ) )
2303 case LDAP_MOD_DELETE:
2304 if ( mlist->sml_values == NULL ) {
2305 if ( ! access_allowed( op, e,
2306 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2313 for ( bv = mlist->sml_nvalues
2314 ? mlist->sml_nvalues : mlist->sml_values;
2315 bv->bv_val != NULL; bv++ )
2317 if ( ! access_allowed( op, e,
2318 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2326 case SLAP_MOD_SOFTADD:
2327 /* allow adding attribute via modrdn thru */
2339 if (be_null) op->o_bd = NULL;
2345 struct berval *list,
2358 while ( len >= 0 && --ix >= 0 ) {
2359 while ( --len >= 0 && *p++ != sep )
2362 while ( len >= 0 && *p == ' ' ) {
2375 while ( --len >= 0 && *p != sep ) {
2379 while ( bv->bv_len > 0 && *--p == ' ' ) {
2386 typedef struct aci_set_gather_t {
2392 aci_set_cb_gather( Operation *op, SlapReply *rs )
2394 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2396 if ( rs->sr_type == REP_SEARCH ) {
2397 BerValue bvals[ 2 ];
2398 BerVarray bvalsp = NULL;
2401 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2402 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2404 if ( desc == slap_schema.si_ad_entryDN ) {
2406 bvals[ 0 ] = rs->sr_entry->e_nname;
2407 BER_BVZERO( &bvals[ 1 ] );
2412 a = attr_find( rs->sr_entry->e_attrs, desc );
2416 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2419 bvalsp = a->a_nvals;
2425 p->bvals = slap_set_join( p->cookie, p->bvals,
2426 ( '|' | SLAP_SET_RREF ), bvalsp );
2430 assert( rs->sr_type == REP_RESULT );
2437 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2439 AciSetCookie *cp = (AciSetCookie *)cookie;
2441 LDAPURLDesc *ludp = NULL;
2442 Operation op2 = { 0 };
2443 SlapReply rs = {REP_RESULT};
2444 AttributeName anlist[ 2 ], *anlistp = NULL;
2446 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2447 aci_set_gather_t p = { 0 };
2448 const char *text = NULL;
2449 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2451 /* this routine needs to return the bervals instead of
2452 * plain strings, since syntax is not known. It should
2453 * also return the syntax or some "comparison cookie".
2455 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2456 return aci_set_gather2( cookie, name, desc );
2459 rc = ldap_url_parse( name->bv_val, &ludp );
2460 if ( rc != LDAP_URL_SUCCESS ) {
2461 rc = LDAP_PROTOCOL_ERROR;
2465 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2467 /* host part must be empty */
2468 /* extensions parts must be empty */
2469 rc = LDAP_PROTOCOL_ERROR;
2473 /* Grab the searchbase and see if an appropriate database can be found */
2474 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2475 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2476 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2477 BER_BVZERO( &op2.o_req_dn );
2478 if ( rc != LDAP_SUCCESS ) {
2482 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2483 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2484 rc = LDAP_NO_SUCH_OBJECT;
2488 /* Grab the filter */
2489 if ( ludp->lud_filter ) {
2490 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2491 cp->op->o_tmpmemctx );
2494 op2.ors_filterstr = defaultFilter_bv;
2497 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2498 if ( op2.ors_filter == NULL ) {
2499 rc = LDAP_PROTOCOL_ERROR;
2503 /* Grab the scope */
2504 op2.ors_scope = ludp->lud_scope;
2506 /* Grap the attributes */
2507 if ( ludp->lud_attrs ) {
2508 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2511 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2512 cp->op->o_tmpmemctx );
2514 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2515 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2516 anlistp[ nattrs ].an_desc = NULL;
2517 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2518 &anlistp[ nattrs ].an_desc, &text );
2519 if ( rc != LDAP_SUCCESS ) {
2528 anlistp[ nattrs ].an_name = desc->ad_cname;
2529 anlistp[ nattrs ].an_desc = desc;
2531 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2535 op2.o_hdr = cp->op->o_hdr;
2536 op2.o_tag = LDAP_REQ_SEARCH;
2537 op2.o_ndn = op2.o_bd->be_rootndn;
2538 op2.o_callback = &cb;
2539 op2.o_time = slap_get_time();
2540 op2.o_do_not_cache = 1;
2541 op2.o_is_auth_check = 0;
2542 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2543 op2.ors_slimit = SLAP_NO_LIMIT;
2544 op2.ors_tlimit = SLAP_NO_LIMIT;
2545 op2.ors_attrs = anlistp;
2546 op2.ors_attrsonly = 0;
2547 op2.o_private = cp->op->o_private;
2551 rc = op2.o_bd->be_search( &op2, &rs );
2557 if ( op2.ors_filter ) {
2558 filter_free_x( cp->op, op2.ors_filter );
2560 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2561 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2563 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2564 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2567 ldap_free_urldesc( ludp );
2569 if ( anlistp && anlistp != anlist ) {
2570 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2577 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2579 AciSetCookie *cp = (AciSetCookie *)cookie;
2580 BerVarray bvals = NULL;
2584 /* this routine needs to return the bervals instead of
2585 * plain strings, since syntax is not known. It should
2586 * also return the syntax or some "comparison cookie".
2588 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2589 if ( rc == LDAP_SUCCESS ) {
2590 if ( desc == slap_schema.si_ad_entryDN ) {
2591 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2592 cp->op->o_tmpmemctx );
2594 BER_BVZERO( &bvals[ 1 ] );
2598 backend_attribute( cp->op,
2599 cp->e, &ndn, desc, &bvals, ACL_NONE );
2602 if ( !BER_BVISNULL( &ndn ) ) {
2603 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2612 struct berval *subj,
2618 struct berval set = BER_BVNULL;
2620 AciSetCookie cookie;
2622 if ( setref == 0 ) {
2623 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2626 struct berval subjdn, ndn = BER_BVNULL;
2627 struct berval setat;
2630 AttributeDescription *desc = NULL;
2632 /* format of string is "entry/setAttrName" */
2633 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2637 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2638 setat = aci_bv_set_attr;
2642 * NOTE: dnNormalize honors the ber_len field
2643 * as the length of the dn to be normalized
2645 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2646 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2648 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2649 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2653 BER_BVZERO( &bvals[0] );
2654 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2656 bvals[0].bv_val = bvals[i-1].bv_val;
2657 BER_BVZERO( &bvals[i-1] );
2659 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2660 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2665 if ( !BER_BVISNULL( &set ) ) {
2668 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2669 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2670 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2676 #ifdef SLAPD_ACI_ENABLED
2678 aci_list_map_rights(
2679 struct berval *list )
2686 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2689 switch (*bv.bv_val) {
2691 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2694 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2695 * the right 's' to mean "set", but in the examples states
2696 * that the right 's' means "search". The latter definition
2699 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2702 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2705 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2708 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2709 * define any equivalent to the AUTH right, so I've just used
2712 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2724 struct berval *list,
2725 const struct berval *attr,
2726 struct berval *val )
2728 struct berval bv, left, right;
2731 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2732 if (aci_get_part(&bv, 0, '=', &left) < 0
2733 || aci_get_part(&bv, 1, '=', &right) < 0)
2735 if (ber_bvstrcasecmp(attr, &bv) == 0)
2737 } else if (val == NULL) {
2738 if (ber_bvstrcasecmp(attr, &left) == 0)
2741 if (ber_bvstrcasecmp(attr, &left) == 0) {
2742 /* this is experimental code that implements a
2743 * simple (prefix) match of the attribute value.
2744 * the ACI draft does not provide for aci's that
2745 * apply to specific values, but it would be
2746 * nice to have. If the <attr> part of an aci's
2747 * rights list is of the form <attr>=<value>,
2748 * that means the aci applies only to attrs with
2749 * the given value. Furthermore, if the attr is
2750 * of the form <attr>=<value>*, then <value> is
2751 * treated as a prefix, and the aci applies to
2752 * any value with that prefix.
2754 * Ideally, this would allow r.e. matches.
2756 if (aci_get_part(&right, 0, '*', &left) < 0
2757 || right.bv_len <= left.bv_len)
2759 if (ber_bvstrcasecmp(val, &right) == 0)
2761 } else if (val->bv_len >= left.bv_len) {
2762 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2771 static slap_access_t
2772 aci_list_get_attr_rights(
2773 struct berval *list,
2774 const struct berval *attr,
2775 struct berval *val )
2781 /* loop through each rights/attr pair, skip first part (action) */
2783 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2784 if (aci_list_has_attr(&bv, attr, val) == 0)
2786 if (aci_get_part(list, i, ';', &bv) < 0)
2788 mask |= aci_list_map_rights(&bv);
2794 aci_list_get_rights(
2795 struct berval *list,
2796 const struct berval *attr,
2798 slap_access_t *grant,
2799 slap_access_t *deny )
2801 struct berval perm, actn;
2802 slap_access_t *mask;
2805 if (attr == NULL || attr->bv_len == 0
2806 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2807 attr = &aci_bv_br_entry;
2813 /* loop through each permissions clause */
2814 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2815 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2817 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2819 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2826 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2827 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2834 struct berval *subj,
2835 struct berval *defgrpoc,
2836 struct berval *defgrpat,
2843 struct berval subjdn;
2844 struct berval grpoc;
2845 struct berval grpat;
2846 ObjectClass *grp_oc = NULL;
2847 AttributeDescription *grp_ad = NULL;
2851 /* format of string is "group/objectClassValue/groupAttrName" */
2852 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2856 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2860 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2864 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2865 if( rc != LDAP_SUCCESS ) {
2871 grp_oc = oc_bvfind( &grpoc );
2873 if (grp_oc != NULL && grp_ad != NULL ) {
2874 char buf[ACL_BUF_SIZE];
2875 struct berval bv, ndn;
2876 bv.bv_len = sizeof( buf ) - 1;
2877 bv.bv_val = (char *)&buf;
2878 if ( string_expand(&bv, &subjdn,
2879 e->e_ndn, nmatch, matches) )
2884 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2885 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2886 grp_oc, grp_ad ) == 0 );
2887 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2899 AttributeDescription *desc,
2903 regmatch_t *matches,
2904 slap_access_t *grant,
2905 slap_access_t *deny,
2906 slap_aci_scope_t asserted_scope
2909 struct berval bv, scope, perms, type, sdn;
2913 assert( !BER_BVISNULL( &desc->ad_cname ) );
2915 /* parse an aci of the form:
2916 oid # scope # action;rights;attr;rights;attr
2917 $ action;rights;attr;rights;attr # type # subject
2919 [NOTE: the following comment is very outdated,
2920 as the draft version it refers to (Ando, 2004-11-20)].
2922 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2923 a full description of the format for this attribute.
2924 Differences: "this" in the draft is "self" here, and
2925 "self" and "public" is in the position of type.
2927 <scope> = {entry|children|subtree}
2928 <type> = {public|users|access-id|subtree|onelevel|children|
2929 self|dnattr|group|role|set|set-ref}
2931 This routine now supports scope={ENTRY,CHILDREN}
2933 - ENTRY applies to "entry" and "subtree";
2934 - CHILDREN aplies to "children" and "subtree"
2937 /* check that the aci has all 5 components */
2938 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2942 /* check that the aci family is supported */
2943 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2947 /* check that the scope matches */
2948 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2952 /* note: scope can be either ENTRY or CHILDREN;
2953 * they respectively match "entry" and "children" in bv
2954 * both match "subtree" */
2955 switch ( asserted_scope ) {
2956 case SLAP_ACI_SCOPE_ENTRY:
2957 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2958 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2964 case SLAP_ACI_SCOPE_CHILDREN:
2965 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2966 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2976 /* get the list of permissions clauses, bail if empty */
2977 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2981 /* check if any permissions allow desired access */
2982 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2986 /* see if we have a DN match */
2987 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2991 /* see if we have a public (i.e. anonymous) access */
2992 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2996 /* otherwise require an identity */
2997 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3001 /* see if we have a users access */
3002 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3006 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3007 * just grab all the berval up to its end (ITS#3303).
3008 * NOTE: the problem could be solved by providing the DN with
3009 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3010 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3012 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3016 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3017 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3019 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3022 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3023 if ( rc != LDAP_SUCCESS ) {
3027 if ( dn_match( &op->o_ndn, &ndn ) ) {
3030 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3034 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3037 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3038 if ( rc != LDAP_SUCCESS ) {
3042 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3045 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3049 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3050 struct berval ndn, pndn;
3052 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3053 if ( rc != LDAP_SUCCESS ) {
3057 dnParent( &ndn, &pndn );
3059 if ( dn_match( &op->o_ndn, &pndn ) ) {
3062 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3066 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3069 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3070 if ( rc != LDAP_SUCCESS ) {
3074 if ( !dn_match( &op->o_ndn, &ndn )
3075 && dnIsSuffix( &op->o_ndn, &ndn ) )
3079 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3083 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3084 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3088 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3090 AttributeDescription *ad = NULL;
3093 rc = slap_bv2ad( &sdn, &ad, &text );
3095 if( rc != LDAP_SUCCESS ) {
3101 for ( at = attrs_find( e->e_attrs, ad );
3103 at = attrs_find( at->a_next, ad ) )
3105 if ( value_find_ex( ad,
3106 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3107 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3109 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3118 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3119 if ( aci_group_member( &sdn, &aci_bv_group_class,
3120 &aci_bv_group_attr, op, e, nmatch, matches ) )
3125 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3126 if ( aci_group_member( &sdn, &aci_bv_role_class,
3127 &aci_bv_role_attr, op, e, nmatch, matches ) )
3132 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3133 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3137 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3138 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3148 * FIXME: there is a silly dependence that makes it difficult
3149 * to move ACIs in a run-time loadable module under the "dynacl"
3150 * umbrella, because sets share some helpers with ACIs.
3153 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3155 AttributeDescription *ad = NULL;
3156 const char *text = NULL;
3158 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3159 fprintf( stderr, "%s: line %d: "
3160 "inappropriate style \"%s\" in \"aci\" by clause\n",
3161 fname, lineno, sty );
3165 if ( right != NULL && *right != '\0' ) {
3166 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3168 "%s: line %d: aci \"%s\": %s\n",
3169 fname, lineno, right, text );
3174 ad = slap_schema.si_ad_aci;
3177 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3178 fprintf( stderr, "%s: line %d: "
3179 "aci \"%s\": inappropriate syntax: %s\n",
3180 fname, lineno, right,
3181 ad->ad_type->sat_syntax_oid );
3185 *privp = (void *)ad;
3191 dynacl_aci_unparse( void *priv, struct berval *bv )
3193 AttributeDescription *ad = ( AttributeDescription * )priv;
3198 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3199 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3200 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3201 bv->bv_len = ptr - bv->bv_val;
3212 AttributeDescription *desc,
3215 regmatch_t *matches,
3216 slap_access_t *grantp,
3217 slap_access_t *denyp )
3219 AttributeDescription *ad = ( AttributeDescription * )priv;
3221 slap_access_t tgrant, tdeny, grant, deny;
3223 char accessmaskbuf[ACCESSMASK_MAXLEN];
3224 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3225 #endif /* LDAP_DEBUG */
3227 /* start out with nothing granted, nothing denied */
3231 /* get the aci attribute */
3232 at = attr_find( e->e_attrs, ad );
3236 /* the aci is an multi-valued attribute. The
3237 * rights are determined by OR'ing the individual
3238 * rights given by the acis.
3240 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3241 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3242 nmatch, matches, &grant, &deny,
3243 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3250 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3251 accessmask2str( tgrant, accessmaskbuf, 1 ),
3252 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3255 /* If the entry level aci didn't contain anything valid for the
3256 * current operation, climb up the tree and evaluate the
3257 * acis with scope set to subtree
3259 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3260 struct berval parent_ndn;
3263 /* to solve the chicken'n'egg problem of accessing
3264 * the OpenLDAPaci attribute, the direct access
3265 * to the entry's attribute is unchecked; however,
3266 * further accesses to OpenLDAPaci values in the
3267 * ancestors occur through backend_attribute(), i.e.
3268 * with the identity of the operation, requiring
3269 * further access checking. For uniformity, this
3270 * makes further requests occur as the rootdn, if
3271 * any, i.e. searching for the OpenLDAPaci attribute
3272 * is considered an internal search. If this is not
3273 * acceptable, then the same check needs be performed
3274 * when accessing the entry's attribute. */
3275 Operation op2 = *op;
3277 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3278 op2.o_dn = op->o_bd->be_rootdn;
3279 op2.o_ndn = op->o_bd->be_rootndn;
3283 dnParent( &e->e_nname, &parent_ndn );
3284 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3286 BerVarray bvals = NULL;
3289 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3290 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3299 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3300 if ( aci_mask( op, e, desc, val,
3304 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3308 /* evaluation stops as soon as either a "deny" or a
3309 * "grant" directive matches.
3311 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3315 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3316 accessmask2str( tgrant, accessmaskbuf, 1 ),
3317 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3321 case LDAP_NO_SUCH_ATTRIBUTE:
3322 /* just go on if the aci-Attribute is not present in
3325 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3329 case LDAP_NO_SUCH_OBJECT:
3330 /* We have reached the base object */
3331 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3343 dnParent( &parent_ndn, &parent_ndn );
3353 /* need to register this at some point */
3354 static slap_dynacl_t dynacl_aci = {
3364 #endif /* SLAP_DYNACL */
3366 #endif /* SLAPD_ACI_ENABLED */
3371 * dynamic ACL infrastructure
3373 static slap_dynacl_t *da_list = NULL;
3376 slap_dynacl_register( slap_dynacl_t *da )
3380 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3381 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3386 if ( tmp != NULL ) {
3390 if ( da->da_mask == NULL ) {
3394 da->da_private = NULL;
3395 da->da_next = da_list;
3401 static slap_dynacl_t *
3402 slap_dynacl_next( slap_dynacl_t *da )
3411 slap_dynacl_get( const char *name )
3415 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3416 if ( strcasecmp( da->da_name, name ) == 0 ) {
3423 #endif /* SLAP_DYNACL */
3430 slap_dynacl_t *known_dynacl[] = {
3431 #ifdef SLAPD_ACI_ENABLED
3433 #endif /* SLAPD_ACI_ENABLED */
3437 for ( i = 0; known_dynacl[ i ]; i++ ) {
3438 rc = slap_dynacl_register( known_dynacl[ i ] );
3443 #endif /* SLAP_DYNACL */
3454 regmatch_t *matches)
3462 bv->bv_val[0] = '\0';
3463 bv->bv_len--; /* leave space for lone $ */
3466 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3467 sp < pat->bv_val + pat->bv_len ; sp++ )
3469 /* did we previously see a $ */
3471 if ( flag == 1 && *sp == '$' ) {
3476 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3479 } else if ( *sp >= '0' && *sp <= '9' ) {
3487 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3488 if ( *sp >= '0' && *sp <= '9' ) {
3489 n = 10*n + ( *sp - '0' );
3493 if ( *sp != /*'{'*/ '}' ) {
3499 if ( n >= nmatch ) {
3505 i = matches[n].rm_so;
3506 l = matches[n].rm_eo;
3507 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3525 /* must have ended with a single $ */
3533 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3534 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3541 struct berval *pat, /* pattern to expand and match against */
3542 char *str, /* string to match against pattern */
3543 char *buf, /* buffer with $N expansion variables */
3544 int nmatch, /* size of the matches array */
3545 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3549 char newbuf[ACL_BUF_SIZE];
3553 bv.bv_len = sizeof( newbuf ) - 1;
3560 string_expand( &bv, pat, buf, nmatch, matches );
3561 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3563 char error[ACL_BUF_SIZE];
3564 regerror( rc, &re, error, sizeof( error ) );
3566 Debug( LDAP_DEBUG_TRACE,
3567 "compile( \"%s\", \"%s\") failed %s\n",
3568 pat->bv_val, str, error );
3572 rc = regexec( &re, str, 0, NULL, 0 );
3575 Debug( LDAP_DEBUG_TRACE,
3576 "=> regex_matches: string: %s\n", str, 0, 0 );
3577 Debug( LDAP_DEBUG_TRACE,
3578 "=> regex_matches: rc: %d %s\n",
3579 rc, !rc ? "matches" : "no matches", 0 );