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>
40 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
46 aci_bv_entry = BER_BVC("entry"),
47 aci_bv_children = BER_BVC("children"),
48 aci_bv_onelevel = BER_BVC("onelevel"),
49 aci_bv_subtree = BER_BVC("subtree"),
50 aci_bv_br_entry = BER_BVC("[entry]"),
51 aci_bv_br_all = BER_BVC("[all]"),
52 aci_bv_access_id = BER_BVC("access-id"),
54 aci_bv_anonymous = BER_BVC("anonymous"),
56 aci_bv_public = BER_BVC("public"),
57 aci_bv_users = BER_BVC("users"),
58 aci_bv_self = BER_BVC("self"),
59 aci_bv_dnattr = BER_BVC("dnattr"),
60 aci_bv_group = BER_BVC("group"),
61 aci_bv_role = BER_BVC("role"),
62 aci_bv_set = BER_BVC("set"),
63 aci_bv_set_ref = BER_BVC("set-ref"),
64 aci_bv_grant = BER_BVC("grant"),
65 aci_bv_deny = BER_BVC("deny"),
67 aci_bv_ip_eq = BER_BVC("IP="),
69 aci_bv_path_eq = BER_BVC("PATH="),
71 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
73 #endif /* LDAP_PF_LOCAL */
75 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
76 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
77 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
78 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
79 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
81 typedef enum slap_aci_scope_t {
82 SLAP_ACI_SCOPE_ENTRY = 0x1,
83 SLAP_ACI_SCOPE_CHILDREN = 0x2,
84 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
87 static AccessControl * slap_acl_get(
88 AccessControl *ac, int *count,
89 Operation *op, Entry *e,
90 AttributeDescription *desc,
92 int nmatch, regmatch_t *matches,
93 AccessControlState *state );
95 static slap_control_t slap_acl_mask(
96 AccessControl *ac, slap_mask_t *mask,
97 Operation *op, Entry *e,
98 AttributeDescription *desc,
103 AccessControlState *state );
105 #ifdef SLAPD_ACI_ENABLED
107 Operation *op, Entry *e,
108 AttributeDescription *desc,
113 slap_access_t *grant,
115 slap_aci_scope_t scope);
116 #endif /* SLAPD_ACI_ENABLED */
118 static int regex_matches(
119 struct berval *pat, char *str, char *buf,
120 int nmatch, regmatch_t *matches);
121 static int string_expand(
122 struct berval *newbuf, struct berval *pattern,
123 char *match, int nmatch, regmatch_t *matches);
125 typedef struct AciSetCookie {
130 SLAP_SET_GATHER aci_set_gather;
131 SLAP_SET_GATHER aci_set_gather2;
132 static int aci_match_set ( struct berval *subj, Operation *op,
133 Entry *e, int setref );
136 * access_allowed - check whether op->o_ndn is allowed the requested access
137 * to entry e, attribute attr, value val. if val is null, access to
138 * the whole attribute is assumed (all values).
140 * This routine loops through all access controls and calls
141 * slap_acl_mask() on each applicable access control.
142 * The loop exits when a definitive answer is reached or
143 * or no more controls remain.
150 * - can be legally called with op == NULL
151 * - can be legally called with op->o_bd == NULL
154 #ifdef SLAP_OVERLAY_ACCESS
156 slap_access_always_allowed(
159 AttributeDescription *desc,
161 slap_access_t access,
162 AccessControlState *state,
165 assert( maskp != NULL );
167 ACL_PRIV_SET( *maskp, ACL_ACCESS2PRIV( access ) );
176 AttributeDescription *desc,
178 slap_access_t access,
179 AccessControlState *state,
184 AccessControl *a = NULL;
187 char accessmaskbuf[ACCESSMASK_MAXLEN];
190 slap_control_t control;
191 slap_access_t access_level;
193 regmatch_t matches[MAXREMATCHES];
194 int st_same_attr = 0;
196 assert( op != NULL );
198 assert( desc != NULL );
199 assert( maskp != NULL );
201 access_level = ACL_LEVEL( access );
202 attr = desc->ad_cname.bv_val;
204 assert( attr != NULL );
206 /* grant database root access */
207 if ( be_isroot( op ) ) {
208 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
209 mask = ACL_LVL_MANAGE;
214 * no-user-modification operational attributes are ignored
215 * by ACL_WRITE checking as any found here are not provided
218 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
219 && desc != slap_schema.si_ad_entry
220 && desc != slap_schema.si_ad_children )
222 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
223 " %s access granted\n",
228 /* use backend default access if no backend acls */
229 if ( op->o_bd->be_acl == NULL ) {
232 Debug( LDAP_DEBUG_ACL,
233 "=> slap_access_allowed: backend default %s "
234 "access %s to \"%s\"\n",
235 access2str( access ),
236 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
237 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
238 ret = op->o_bd->be_dfltaccess >= access_level;
240 mask = ACL_PRIV_LEVEL;
241 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
242 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
251 if ( st_same_attr ) {
252 assert( state->as_vd_acl != NULL );
254 a = state->as_vd_acl;
255 count = state->as_vd_acl_count;
256 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
257 mask = state->as_vd_acl_mask;
258 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
263 if ( state ) state->as_vi_acl = NULL;
265 ACL_PRIV_ASSIGN( mask, *maskp );
267 memset( matches, '\0', sizeof( matches ) );
270 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
271 MAXREMATCHES, matches, state ) ) != NULL )
275 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
276 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
277 (int)matches[i].rm_so, (int)matches[i].rm_eo );
278 if ( matches[i].rm_so <= matches[0].rm_eo ) {
280 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
281 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
284 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
288 if ( state->as_vi_acl == a &&
289 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
291 Debug( LDAP_DEBUG_ACL,
292 "=> slap_access_allowed: result from state (%s)\n",
294 ret = state->as_result;
297 Debug( LDAP_DEBUG_ACL,
298 "=> slap_access_allowed: no res from state (%s)\n",
304 control = slap_acl_mask( a, &mask, op,
305 e, desc, val, MAXREMATCHES, matches, count, state );
307 if ( control != ACL_BREAK ) {
311 memset( matches, '\0', sizeof( matches ) );
314 if ( ACL_IS_INVALID( mask ) ) {
315 Debug( LDAP_DEBUG_ACL,
316 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
318 ACL_PRIV_ASSIGN( mask, *maskp );
320 } else if ( control == ACL_BREAK ) {
321 Debug( LDAP_DEBUG_ACL,
322 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
327 ret = ACL_GRANT( mask, access );
329 Debug( LDAP_DEBUG_ACL,
330 "=> slap_access_allowed: %s access %s by %s\n",
331 access2str( access ), ret ? "granted" : "denied",
332 accessmask2str( mask, accessmaskbuf, 1 ) );
335 ACL_PRIV_ASSIGN( *maskp, mask );
343 AttributeDescription *desc,
345 slap_access_t access,
346 AccessControlState *state,
350 AccessControl *a = NULL;
354 char accessmaskbuf[ACCESSMASK_MAXLEN];
357 slap_control_t control;
358 slap_access_t access_level;
360 int st_same_attr = 0;
361 static AccessControlState state_init = ACL_STATE_INIT;
365 assert( desc != NULL );
367 access_level = ACL_LEVEL( access );
369 assert( access_level > ACL_NONE );
372 if ( maskp ) ACL_INVALIDATE( *maskp );
374 attr = desc->ad_cname.bv_val;
376 assert( attr != NULL );
378 if ( op && op->o_is_auth_check &&
379 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
385 if ( state->as_vd_ad == desc ) {
386 if ( state->as_recorded ) {
387 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
390 return state->as_result;
392 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
393 val != NULL && state->as_vd_acl == NULL )
395 return state->as_result;
403 state->as_vd_ad = desc;
406 Debug( LDAP_DEBUG_ACL,
407 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
408 access2str( access ), e->e_dn, attr );
415 if ( op->o_bd == NULL ) {
416 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
421 * FIXME: experimental; use first backend rules
422 * iff there is no global_acl (ITS#3100) */
423 if ( frontendDB->be_acl != NULL ) {
424 op->o_bd = frontendDB;
426 #endif /* LDAP_DEVEL */
428 assert( op->o_bd != NULL );
430 /* this is enforced in backend_add() */
431 if ( op->o_bd->bd_info->bi_access_allowed ) {
432 /* delegate to backend */
433 ret = op->o_bd->bd_info->bi_access_allowed( op, e, desc, val, access, state, &mask );
436 /* use default (but pass through frontend for global ACL overlays) */
439 op->o_bd = frontendDB;
440 ret = frontendDB->bd_info->bi_access_allowed( op, e, desc, val, access, state, &mask );
445 if ( ACL_IS_INVALID( mask ) ) {
446 Debug( LDAP_DEBUG_ACL,
447 "=> access_allowed: \"%s\" (%s) invalid!\n",
451 } else if ( control == ACL_BREAK ) {
452 Debug( LDAP_DEBUG_ACL,
453 "=> access_allowed: no more rules\n", 0, 0, 0 );
458 ret = ACL_GRANT( mask, access );
461 Debug( LDAP_DEBUG_ACL,
462 "=> access_allowed: %s access %s by %s\n",
463 access2str( access ), ret ? "granted" : "denied",
464 accessmask2str( mask, accessmaskbuf, 1 ) );
467 if ( state != NULL ) {
468 /* If not value-dependent, save ACL in case of more attrs */
469 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
470 state->as_vi_acl = a;
471 state->as_result = ret;
473 state->as_recorded |= ACL_STATE_RECORDED;
475 if ( be_null ) op->o_bd = NULL;
476 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
480 #else /* !SLAP_OVERLAY_ACCESS */
486 AttributeDescription *desc,
488 slap_access_t access,
489 AccessControlState *state,
494 AccessControl *a = NULL;
499 char accessmaskbuf[ACCESSMASK_MAXLEN];
502 slap_control_t control;
503 slap_access_t access_level;
505 regmatch_t matches[MAXREMATCHES];
506 int st_same_attr = 0;
507 static AccessControlState state_init = ACL_STATE_INIT;
510 assert( desc != NULL );
512 access_level = ACL_LEVEL( access );
514 assert( access_level > ACL_NONE );
515 if ( maskp ) ACL_INVALIDATE( *maskp );
517 attr = desc->ad_cname.bv_val;
519 assert( attr != NULL );
521 if ( op && op->o_is_auth_check &&
522 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
528 if ( state->as_vd_ad == desc ) {
529 if ( state->as_recorded ) {
530 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
533 return state->as_result;
535 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
536 val != NULL && state->as_vd_acl == NULL )
538 return state->as_result;
546 state->as_vd_ad=desc;
549 Debug( LDAP_DEBUG_ACL,
550 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
551 access2str( access ), e->e_dn, attr );
560 be = LDAP_STAILQ_FIRST(&backendDB);
564 * FIXME: experimental; use first backend rules
565 * iff there is no global_acl (ITS#3100) */
566 if ( frontendDB->be_acl == NULL )
572 assert( be != NULL );
574 /* grant database root access */
575 if ( be_isroot( op ) ) {
576 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
578 mask = ACL_LVL_MANAGE;
585 * no-user-modification operational attributes are ignored
586 * by ACL_WRITE checking as any found here are not provided
589 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
590 && desc != slap_schema.si_ad_entry
591 && desc != slap_schema.si_ad_children )
593 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
594 " %s access granted\n",
599 /* use backend default access if no backend acls */
600 if ( be->be_acl == NULL ) {
601 Debug( LDAP_DEBUG_ACL,
602 "=> access_allowed: backend default %s "
603 "access %s to \"%s\"\n",
604 access2str( access ),
605 be->be_dfltaccess >= access_level ? "granted" : "denied",
606 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
607 ret = be->be_dfltaccess >= access_level;
612 mask = ACL_PRIV_LEVEL;
613 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
614 mask |= ACL_ACCESS2PRIV( i );
621 /* be is always non-NULL */
622 /* use global default access if no global acls */
623 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
624 Debug( LDAP_DEBUG_ACL,
625 "=> access_allowed: global default %s access %s to \"%s\"\n",
626 access2str( access ),
627 frontendDB->be_dfltaccess >= access_level ?
628 "granted" : "denied", op->o_dn.bv_val );
629 ret = frontendDB->be_dfltaccess >= access_level;
634 mask = ACL_PRIV_LEVEL;
635 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
636 mask |= ACL_ACCESS2PRIV( i );
647 if ( st_same_attr ) {
648 assert( state->as_vd_acl != NULL );
650 a = state->as_vd_acl;
651 count = state->as_vd_acl_count;
652 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
653 mask = state->as_vd_acl_mask;
654 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
659 if ( state ) state->as_vi_acl = NULL;
663 memset( matches, '\0', sizeof(matches) );
666 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
667 MAXREMATCHES, matches, state ) ) != NULL )
671 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
672 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
673 (int)matches[i].rm_so, (int)matches[i].rm_eo );
674 if ( matches[i].rm_so <= matches[0].rm_eo ) {
676 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
677 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
680 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
684 if ( state->as_vi_acl == a &&
685 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
687 Debug( LDAP_DEBUG_ACL,
688 "access_allowed: result from state (%s)\n",
690 ret = state->as_result;
693 Debug( LDAP_DEBUG_ACL,
694 "access_allowed: no res from state (%s)\n",
700 control = slap_acl_mask( a, &mask, op,
701 e, desc, val, MAXREMATCHES, matches, count, state );
703 if ( control != ACL_BREAK ) {
707 memset( matches, '\0', sizeof(matches) );
710 if ( ACL_IS_INVALID( mask ) ) {
711 Debug( LDAP_DEBUG_ACL,
712 "=> access_allowed: \"%s\" (%s) invalid!\n",
716 } else if ( control == ACL_BREAK ) {
717 Debug( LDAP_DEBUG_ACL,
718 "=> access_allowed: no more rules\n", 0, 0, 0 );
723 Debug( LDAP_DEBUG_ACL,
724 "=> access_allowed: %s access %s by %s\n",
725 access2str( access ),
726 ACL_GRANT(mask, access) ? "granted" : "denied",
727 accessmask2str( mask, accessmaskbuf, 1 ) );
729 ret = ACL_GRANT(mask, access);
732 if ( state != NULL ) {
733 /* If not value-dependent, save ACL in case of more attrs */
734 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
735 state->as_vi_acl = a;
736 state->as_result = ret;
738 state->as_recorded |= ACL_STATE_RECORDED;
740 if ( be_null ) op->o_bd = NULL;
741 if ( maskp ) *maskp = mask;
745 #endif /* SLAP_OVERLAY_ACCESS */
748 * slap_acl_get - return the acl applicable to entry e, attribute
749 * attr. the acl returned is suitable for use in subsequent calls to
750 * acl_access_allowed().
753 static AccessControl *
759 AttributeDescription *desc,
763 AccessControlState *state )
770 assert( count != NULL );
771 assert( desc != NULL );
773 attr = desc->ad_cname.bv_val;
775 assert( attr != NULL );
778 if( op->o_bd == NULL ) {
779 a = frontendDB->be_acl;
781 a = op->o_bd->be_acl;
792 dnlen = e->e_nname.bv_len;
794 for ( ; a != NULL; a = a->acl_next ) {
797 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
798 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
799 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
800 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
801 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
805 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
806 *count, a->acl_dn_pat.bv_val, 0 );
807 patlen = a->acl_dn_pat.bv_len;
808 if ( dnlen < patlen )
811 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
812 /* base dn -- entire object DN must match */
813 if ( dnlen != patlen )
816 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
817 int rdnlen = -1, sep = 0;
819 if ( dnlen <= patlen )
823 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
828 rdnlen = dn_rdnlen( NULL, &e->e_nname );
829 if ( rdnlen != dnlen - patlen - sep )
832 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
833 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
836 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
837 if ( dnlen <= patlen )
839 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
843 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
847 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
851 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
852 matches[0].rm_so = matches[0].rm_eo = -1;
856 /* Is this ACL only for a specific value? */
857 if ( a->acl_attrval.bv_len ) {
862 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
863 state->as_recorded |= ACL_STATE_RECORDED_VD;
864 state->as_vd_acl = a;
865 state->as_vd_acl_count = *count;
866 state->as_vd_access = a->acl_access;
867 state->as_vd_access_count = 1;
868 ACL_INVALIDATE( state->as_vd_acl_mask );
871 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
872 Debug( LDAP_DEBUG_ACL,
873 "acl_get: valpat %s\n",
874 a->acl_attrval.bv_val, 0, 0 );
875 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
883 Debug( LDAP_DEBUG_ACL,
885 a->acl_attrval.bv_val, 0, 0 );
887 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
888 if (value_match( &match, desc,
889 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
890 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
897 patlen = a->acl_attrval.bv_len;
898 vdnlen = val->bv_len;
900 if ( vdnlen < patlen )
903 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
904 if ( vdnlen > patlen )
907 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
910 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
913 rdnlen = dn_rdnlen( NULL, val );
914 if ( rdnlen != vdnlen - patlen - 1 )
917 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
918 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
921 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
922 if ( vdnlen <= patlen )
925 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
929 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
935 if ( a->acl_filter != NULL ) {
936 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
937 if ( rc != LDAP_COMPARE_TRUE ) {
942 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
947 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
959 struct berval *opndn )
962 * if access applies to the entry itself, and the
963 * user is bound as somebody in the same namespace as
964 * the entry, OR the given dn matches the dn pattern
967 * NOTE: styles "anonymous", "users" and "self"
968 * have been moved to enum slap_style_t, whose
969 * value is set in a_dn_style; however, the string
970 * is maintaned in a_dn_pat.
972 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
973 if ( !BER_BVISEMPTY( opndn ) ) {
977 } else if ( b->a_style == ACL_STYLE_USERS ) {
978 if ( BER_BVISEMPTY( opndn ) ) {
982 } else if ( b->a_style == ACL_STYLE_SELF ) {
983 struct berval ndn, selfndn;
986 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
990 level = b->a_self_level;
998 selfndn = e->e_nname;
1001 for ( ; level > 0; level-- ) {
1002 if ( BER_BVISEMPTY( &ndn ) ) {
1005 dnParent( &ndn, &ndn );
1008 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1013 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1014 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1016 regmatch_t tmp_matches[2],
1017 *tmp_matchesp = tmp_matches;
1021 switch ( a->acl_dn_style ) {
1022 case ACL_STYLE_REGEX:
1023 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1024 tmp_matchesp = matches;
1025 tmp_nmatch = nmatch;
1028 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1030 case ACL_STYLE_BASE:
1031 tmp_matches[0].rm_so = 0;
1032 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1037 case ACL_STYLE_SUBTREE:
1038 case ACL_STYLE_CHILDREN:
1039 tmp_matches[0].rm_so = 0;
1040 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1041 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1042 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1056 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1057 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1065 ber_len_t patlen, odnlen;
1068 if ( e->e_dn == NULL )
1071 if ( b->a_expand ) {
1073 char buf[ACL_BUF_SIZE];
1076 regmatch_t tmp_matches[2],
1077 *tmp_matchesp = tmp_matches;
1081 bv.bv_len = sizeof( buf ) - 1;
1084 switch ( a->acl_dn_style ) {
1085 case ACL_STYLE_REGEX:
1086 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1087 tmp_matchesp = matches;
1088 tmp_nmatch = nmatch;
1091 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1093 case ACL_STYLE_BASE:
1094 tmp_matches[0].rm_so = 0;
1095 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1100 case ACL_STYLE_SUBTREE:
1101 case ACL_STYLE_CHILDREN:
1102 tmp_matches[0].rm_so = 0;
1103 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1104 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1105 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1119 if ( string_expand( &bv, &b->a_pat,
1121 tmp_nmatch, tmp_matchesp ) )
1126 if ( dnNormalize(0, NULL, NULL, &bv,
1127 &pat, op->o_tmpmemctx )
1130 /* did not expand to a valid dn */
1138 patlen = pat.bv_len;
1139 odnlen = opndn->bv_len;
1140 if ( odnlen < patlen ) {
1141 goto dn_match_cleanup;
1145 if ( b->a_style == ACL_STYLE_BASE ) {
1146 /* base dn -- entire object DN must match */
1147 if ( odnlen != patlen ) {
1148 goto dn_match_cleanup;
1151 } else if ( b->a_style == ACL_STYLE_ONE ) {
1154 if ( odnlen <= patlen ) {
1155 goto dn_match_cleanup;
1158 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1159 goto dn_match_cleanup;
1162 rdnlen = dn_rdnlen( NULL, opndn );
1163 if ( rdnlen != odnlen - patlen - 1 ) {
1164 goto dn_match_cleanup;
1167 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1168 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1169 goto dn_match_cleanup;
1172 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1173 if ( odnlen <= patlen ) {
1174 goto dn_match_cleanup;
1177 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1178 goto dn_match_cleanup;
1181 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1185 if ( odnlen <= patlen ) {
1186 goto dn_match_cleanup;
1189 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1191 goto dn_match_cleanup;
1196 for ( ; level > 0; level-- ) {
1197 if ( BER_BVISEMPTY( &ndn ) ) {
1198 goto dn_match_cleanup;
1200 dnParent( &ndn, &ndn );
1201 if ( ndn.bv_len < patlen ) {
1202 goto dn_match_cleanup;
1206 if ( ndn.bv_len != patlen ) {
1207 goto dn_match_cleanup;
1211 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1214 if ( pat.bv_val != b->a_pat.bv_val ) {
1215 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1227 * Record value-dependent access control state
1229 #define ACL_RECORD_VALUE_STATE do { \
1230 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1231 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1232 state->as_vd_acl = a; \
1233 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1234 sizeof( state->as_vd_acl_matches )) ; \
1235 state->as_vd_acl_count = count; \
1236 state->as_vd_access = b; \
1237 state->as_vd_access_count = i; \
1249 regmatch_t *matches,
1251 AccessControlState *state,
1252 slap_dn_access *bdn,
1253 struct berval *opndn )
1259 const char *attr = bdn->a_at->ad_cname.bv_val;
1261 assert( attr != NULL );
1263 if ( BER_BVISEMPTY( opndn ) ) {
1267 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1270 /* see if asker is listed in dnattr */
1271 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1273 at = attrs_find( at->a_next, bdn->a_at ) )
1275 if ( value_find_ex( bdn->a_at,
1276 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1277 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1279 &bv, op->o_tmpmemctx ) == 0 )
1288 /* have a dnattr match. if this is a self clause then
1289 * the target must also match the op dn.
1291 if ( bdn->a_self ) {
1292 /* check if the target is an attribute. */
1293 if ( val == NULL ) return 1;
1295 /* target is attribute, check if the attribute value
1298 rc = value_match( &match, bdn->a_at,
1299 bdn->a_at->ad_type->sat_equality, 0,
1301 /* on match error or no match, fail the ACL clause */
1302 if ( rc != LDAP_SUCCESS || match != 0 )
1307 /* no dnattr match, check if this is a self clause */
1308 if ( ! bdn->a_self )
1311 ACL_RECORD_VALUE_STATE;
1313 /* this is a self clause, check if the target is an
1319 /* target is attribute, check if the attribute value
1322 rc = value_match( &match, bdn->a_at,
1323 bdn->a_at->ad_type->sat_equality, 0,
1326 /* on match error or no match, fail the ACL clause */
1327 if ( rc != LDAP_SUCCESS || match != 0 )
1336 * slap_acl_mask - modifies mask based upon the given acl and the
1337 * requested access to entry e, attribute attr, value val. if val
1338 * is null, access to the whole attribute is assumed (all values).
1340 * returns 0 access NOT allowed
1344 static slap_control_t
1350 AttributeDescription *desc,
1353 regmatch_t *matches,
1355 AccessControlState *state )
1360 char accessmaskbuf[ACCESSMASK_MAXLEN];
1361 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1362 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1363 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1367 assert( a != NULL );
1368 assert( mask != NULL );
1369 assert( desc != NULL );
1371 attr = desc->ad_cname.bv_val;
1373 assert( attr != NULL );
1375 Debug( LDAP_DEBUG_ACL,
1376 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1379 Debug( LDAP_DEBUG_ACL,
1380 "=> acl_mask: to %s by \"%s\", (%s) \n",
1381 val ? "value" : "all values",
1382 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1383 accessmask2str( *mask, accessmaskbuf, 1 ) );
1386 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1387 && state->as_vd_acl == a )
1389 b = state->as_vd_access;
1390 i = state->as_vd_access_count;
1397 for ( ; b != NULL; b = b->a_next, i++ ) {
1398 slap_mask_t oldmask, modmask;
1400 ACL_INVALIDATE( modmask );
1402 /* AND <who> clauses */
1403 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1404 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1405 b->a_dn_pat.bv_val, 0, 0);
1407 * if access applies to the entry itself, and the
1408 * user is bound as somebody in the same namespace as
1409 * the entry, OR the given dn matches the dn pattern
1412 * NOTE: styles "anonymous", "users" and "self"
1413 * have been moved to enum slap_style_t, whose
1414 * value is set in a_dn_style; however, the string
1415 * is maintaned in a_dn_pat.
1418 if ( acl_mask_dn( op, e, a, nmatch, matches,
1419 &b->a_dn, &op->o_ndn ) )
1425 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1428 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1429 b->a_realdn_pat.bv_val, 0, 0);
1431 * if access applies to the entry itself, and the
1432 * user is bound as somebody in the same namespace as
1433 * the entry, OR the given dn matches the dn pattern
1436 * NOTE: styles "anonymous", "users" and "self"
1437 * have been moved to enum slap_style_t, whose
1438 * value is set in a_dn_style; however, the string
1439 * is maintaned in a_dn_pat.
1442 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1444 ndn = op->o_conn->c_ndn;
1449 if ( acl_mask_dn( op, e, a, nmatch, matches,
1450 &b->a_realdn, &ndn ) )
1456 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1457 if ( ! op->o_conn->c_listener ) {
1460 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1461 b->a_sockurl_pat.bv_val, 0, 0 );
1463 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1464 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1465 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1466 e->e_ndn, nmatch, matches ) )
1471 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1473 char buf[ACL_BUF_SIZE];
1475 bv.bv_len = sizeof( buf ) - 1;
1477 if ( string_expand( &bv, &b->a_sockurl_pat,
1478 e->e_ndn, nmatch, matches ) )
1483 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1489 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1497 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1498 if ( !op->o_conn->c_peer_domain.bv_val ) {
1501 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1502 b->a_domain_pat.bv_val, 0, 0 );
1503 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1504 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1505 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1506 e->e_ndn, nmatch, matches ) )
1511 char buf[ACL_BUF_SIZE];
1513 struct berval cmp = op->o_conn->c_peer_domain;
1514 struct berval pat = b->a_domain_pat;
1516 if ( b->a_domain_expand ) {
1519 bv.bv_len = sizeof(buf) - 1;
1522 if ( string_expand(&bv, &b->a_domain_pat,
1523 e->e_ndn, nmatch, matches) )
1530 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1531 int offset = cmp.bv_len - pat.bv_len;
1536 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1540 /* trim the domain */
1541 cmp.bv_val = &cmp.bv_val[ offset ];
1542 cmp.bv_len -= offset;
1545 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1552 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1553 if ( !op->o_conn->c_peer_name.bv_val ) {
1556 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1557 b->a_peername_pat.bv_val, 0, 0 );
1558 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1559 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1560 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1561 e->e_ndn, nmatch, matches ) )
1567 /* try exact match */
1568 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1569 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1573 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1575 char buf[ACL_BUF_SIZE];
1577 bv.bv_len = sizeof( buf ) - 1;
1579 if ( string_expand( &bv, &b->a_peername_pat,
1580 e->e_ndn, nmatch, matches ) )
1585 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1589 /* extract IP and try exact match */
1590 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1592 char buf[] = "255.255.255.255";
1595 int port_number = -1;
1597 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1598 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1601 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1602 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1604 port = strrchr( ip.bv_val, ':' );
1608 ip.bv_len = port - ip.bv_val;
1610 port_number = strtol( port, &next, 10 );
1611 if ( next[0] != '\0' )
1615 /* the port check can be anticipated here */
1616 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1619 /* address longer than expected? */
1620 if ( ip.bv_len >= sizeof(buf) )
1623 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1624 buf[ ip.bv_len ] = '\0';
1626 addr = inet_addr( buf );
1628 /* unable to convert? */
1629 if ( addr == (unsigned long)(-1) )
1632 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1635 #ifdef LDAP_PF_LOCAL
1636 /* extract path and try exact match */
1637 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1640 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1641 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1644 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1645 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1647 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1650 #endif /* LDAP_PF_LOCAL */
1652 /* exact match (very unlikely...) */
1653 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1660 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1661 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1664 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1665 b->a_sockname_pat.bv_val, 0, 0 );
1666 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1667 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1668 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1669 e->e_ndn, nmatch, matches ) )
1674 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1676 char buf[ACL_BUF_SIZE];
1678 bv.bv_len = sizeof( buf ) - 1;
1680 if ( string_expand( &bv, &b->a_sockname_pat,
1681 e->e_ndn, nmatch, matches ) )
1686 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1691 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1698 if ( b->a_dn_at != NULL ) {
1699 if ( acl_mask_dnattr( op, e, val, a, b, i,
1700 matches, count, state,
1701 &b->a_dn, &op->o_ndn ) )
1707 if ( b->a_realdn_at != NULL ) {
1710 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1712 ndn = op->o_conn->c_ndn;
1717 if ( acl_mask_dnattr( op, e, val, a, b, i,
1718 matches, count, state,
1719 &b->a_realdn, &ndn ) )
1725 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1727 struct berval ndn = BER_BVNULL;
1730 if ( op->o_ndn.bv_len == 0 ) {
1734 /* b->a_group is an unexpanded entry name, expanded it should be an
1735 * entry with objectclass group* and we test to see if odn is one of
1736 * the values in the attribute group
1738 /* see if asker is listed in dnattr */
1739 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1740 char buf[ACL_BUF_SIZE];
1742 regmatch_t tmp_matches[2],
1743 *tmp_matchesp = tmp_matches;
1745 bv.bv_len = sizeof(buf) - 1;
1750 switch ( a->acl_dn_style ) {
1751 case ACL_STYLE_REGEX:
1752 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1753 tmp_matchesp = matches;
1754 tmp_nmatch = nmatch;
1758 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1759 case ACL_STYLE_BASE:
1760 tmp_matches[0].rm_so = 0;
1761 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1766 case ACL_STYLE_SUBTREE:
1767 case ACL_STYLE_CHILDREN:
1768 tmp_matches[0].rm_so = 0;
1769 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1770 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1771 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1785 if ( string_expand( &bv, &b->a_group_pat,
1787 tmp_nmatch, tmp_matchesp ) )
1792 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1793 op->o_tmpmemctx ) != LDAP_SUCCESS )
1795 /* did not expand to a valid dn */
1802 bv = b->a_group_pat;
1805 rc = backend_group( op, e, &bv, &op->o_ndn,
1806 b->a_group_oc, b->a_group_at );
1809 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1817 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1819 char buf[ACL_BUF_SIZE];
1821 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1823 regmatch_t tmp_matches[2],
1824 *tmp_matchesp = tmp_matches;
1827 bv.bv_len = sizeof( buf ) - 1;
1832 switch ( a->acl_dn_style ) {
1833 case ACL_STYLE_REGEX:
1834 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1835 tmp_matchesp = matches;
1836 tmp_nmatch = nmatch;
1840 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1841 case ACL_STYLE_BASE:
1842 tmp_matches[0].rm_so = 0;
1843 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1848 case ACL_STYLE_SUBTREE:
1849 case ACL_STYLE_CHILDREN:
1850 tmp_matches[0].rm_so = 0;
1851 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1852 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1853 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1867 if ( string_expand( &bv, &b->a_set_pat,
1869 tmp_nmatch, tmp_matchesp ) )
1878 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1883 if ( b->a_authz.sai_ssf ) {
1884 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1885 b->a_authz.sai_ssf, op->o_ssf, 0 );
1886 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1891 if ( b->a_authz.sai_transport_ssf ) {
1892 Debug( LDAP_DEBUG_ACL,
1893 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1894 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1895 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1900 if ( b->a_authz.sai_tls_ssf ) {
1901 Debug( LDAP_DEBUG_ACL,
1902 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1903 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1904 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1909 if ( b->a_authz.sai_sasl_ssf ) {
1910 Debug( LDAP_DEBUG_ACL,
1911 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1912 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1913 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1919 if ( b->a_dynacl ) {
1921 slap_access_t tgrant, tdeny;
1923 /* this case works different from the others above.
1924 * since aci's themselves give permissions, we need
1925 * to first check b->a_access_mask, the ACL's access level.
1927 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1928 /* no ACIs in the root DSE */
1932 /* first check if the right being requested
1933 * is allowed by the ACL clause.
1935 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1939 /* start out with nothing granted, nothing denied */
1943 for ( da = b->a_dynacl; da; da = da->da_next ) {
1944 slap_access_t grant, deny;
1946 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1952 /* remove anything that the ACL clause does not allow */
1953 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1954 tdeny &= ACL_PRIV_MASK;
1956 /* see if we have anything to contribute */
1957 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1961 /* this could be improved by changing slap_acl_mask so that it can deal with
1962 * by clauses that return grant/deny pairs. Right now, it does either
1963 * additive or subtractive rights, but not both at the same time. So,
1964 * we need to combine the grant/deny pair into a single rights mask in
1965 * a smart way: if either grant or deny is "empty", then we use the
1966 * opposite as is, otherwise we remove any denied rights from the grant
1967 * rights mask and construct an additive mask.
1969 if (ACL_IS_INVALID(tdeny)) {
1970 modmask = tgrant | ACL_PRIV_ADDITIVE;
1972 } else if (ACL_IS_INVALID(tgrant)) {
1973 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1976 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1980 #else /* !SLAP_DYNACL */
1982 #ifdef SLAPD_ACI_ENABLED
1983 if ( b->a_aci_at != NULL ) {
1985 slap_access_t grant, deny, tgrant, tdeny;
1986 struct berval parent_ndn;
1987 BerVarray bvals = NULL;
1990 /* this case works different from the others above.
1991 * since aci's themselves give permissions, we need
1992 * to first check b->a_access_mask, the ACL's access level.
1995 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1996 /* no ACIs in the root DSE */
2000 /* first check if the right being requested
2001 * is allowed by the ACL clause.
2003 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2006 /* start out with nothing granted, nothing denied */
2010 /* get the aci attribute */
2011 at = attr_find( e->e_attrs, b->a_aci_at );
2014 /* FIXME: this breaks acl caching;
2015 * see also ACL_RECORD_VALUE_STATE below */
2016 ACL_RECORD_VALUE_STATE;
2018 /* the aci is an multi-valued attribute. The
2019 * rights are determined by OR'ing the individual
2020 * rights given by the acis.
2022 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2027 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2033 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2034 accessmask2str(tgrant, accessmaskbuf, 1),
2035 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2038 /* If the entry level aci didn't contain anything valid for the
2039 * current operation, climb up the tree and evaluate the
2040 * acis with scope set to subtree
2042 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2043 dnParent( &e->e_nname, &parent_ndn );
2044 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2045 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2046 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2054 for( i = 0; bvals[i].bv_val != NULL; i++){
2056 /* FIXME: this breaks acl caching;
2057 * see also ACL_RECORD_VALUE_STATE above */
2058 ACL_RECORD_VALUE_STATE;
2060 if (aci_mask(op, e, desc, val, &bvals[i],
2062 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2066 /* evaluation stops as soon as either a "deny" or a
2067 * "grant" directive matches.
2069 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2073 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2074 accessmask2str(tgrant, accessmaskbuf, 1),
2075 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2079 case LDAP_NO_SUCH_ATTRIBUTE:
2080 /* just go on if the aci-Attribute is not present in
2083 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2087 case LDAP_NO_SUCH_OBJECT:
2088 /* We have reached the base object */
2089 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2100 dnParent( &parent_ndn, &parent_ndn );
2105 /* remove anything that the ACL clause does not allow */
2106 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2107 tdeny &= ACL_PRIV_MASK;
2109 /* see if we have anything to contribute */
2110 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2114 /* this could be improved by changing slap_acl_mask so that it can deal with
2115 * by clauses that return grant/deny pairs. Right now, it does either
2116 * additive or subtractive rights, but not both at the same time. So,
2117 * we need to combine the grant/deny pair into a single rights mask in
2118 * a smart way: if either grant or deny is "empty", then we use the
2119 * opposite as is, otherwise we remove any denied rights from the grant
2120 * rights mask and construct an additive mask.
2122 if (ACL_IS_INVALID(tdeny)) {
2123 modmask = tgrant | ACL_PRIV_ADDITIVE;
2125 } else if (ACL_IS_INVALID(tgrant)) {
2126 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2129 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2133 #endif /* SLAPD_ACI_ENABLED */
2134 #endif /* !SLAP_DYNACL */
2136 modmask = b->a_access_mask;
2139 Debug( LDAP_DEBUG_ACL,
2140 "<= acl_mask: [%d] applying %s (%s)\n",
2141 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2142 b->a_type == ACL_CONTINUE
2144 : b->a_type == ACL_BREAK
2150 if( ACL_IS_ADDITIVE(modmask) ) {
2152 ACL_PRIV_SET( *mask, modmask );
2155 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2157 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2158 /* substract privs */
2159 ACL_PRIV_CLR( *mask, modmask );
2162 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2169 Debug( LDAP_DEBUG_ACL,
2170 "<= acl_mask: [%d] mask: %s\n",
2171 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2173 if( b->a_type == ACL_CONTINUE ) {
2176 } else if ( b->a_type == ACL_BREAK ) {
2184 /* implicit "by * none" clause */
2187 Debug( LDAP_DEBUG_ACL,
2188 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2189 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2194 * acl_check_modlist - check access control on the given entry to see if
2195 * it allows the given modifications by the user associated with op.
2196 * returns 1 if mods allowed ok
2197 * 0 mods not allowed
2204 Modifications *mlist
2208 AccessControlState state = ACL_STATE_INIT;
2211 int ret = 1; /* default is access allowed */
2215 be = LDAP_STAILQ_FIRST(&backendDB);
2219 assert( be != NULL );
2221 /* short circuit root database access */
2222 if ( be_isroot( op ) ) {
2223 Debug( LDAP_DEBUG_ACL,
2224 "<= acl_access_allowed: granted to database root\n",
2229 /* use backend default access if no backend acls */
2230 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2231 Debug( LDAP_DEBUG_ACL,
2232 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2233 access2str( ACL_WRITE ),
2234 op->o_bd->be_dfltaccess >= ACL_WRITE
2235 ? "granted" : "denied",
2237 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2241 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2243 * Internal mods are ignored by ACL_WRITE checking
2245 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2246 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2247 " modify access granted\n",
2248 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2253 * no-user-modification operational attributes are ignored
2254 * by ACL_WRITE checking as any found here are not provided
2257 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2258 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2259 " modify access granted\n",
2260 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2264 switch ( mlist->sml_op ) {
2265 case LDAP_MOD_REPLACE:
2267 * We must check both permission to delete the whole
2268 * attribute and permission to add the specific attributes.
2269 * This prevents abuse from selfwriters.
2271 if ( ! access_allowed( op, e,
2272 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2278 if ( mlist->sml_values == NULL ) break;
2280 /* fall thru to check value to add */
2283 assert( mlist->sml_values != NULL );
2285 for ( bv = mlist->sml_nvalues
2286 ? mlist->sml_nvalues : mlist->sml_values;
2287 bv->bv_val != NULL; bv++ )
2289 if ( ! access_allowed( op, e,
2290 mlist->sml_desc, bv, ACL_WADD, &state ) )
2298 case LDAP_MOD_DELETE:
2299 if ( mlist->sml_values == NULL ) {
2300 if ( ! access_allowed( op, e,
2301 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2308 for ( bv = mlist->sml_nvalues
2309 ? mlist->sml_nvalues : mlist->sml_values;
2310 bv->bv_val != NULL; bv++ )
2312 if ( ! access_allowed( op, e,
2313 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2321 case SLAP_MOD_SOFTADD:
2322 /* allow adding attribute via modrdn thru */
2334 if (be_null) op->o_bd = NULL;
2340 struct berval *list,
2353 while ( len >= 0 && --ix >= 0 ) {
2354 while ( --len >= 0 && *p++ != sep )
2357 while ( len >= 0 && *p == ' ' ) {
2370 while ( --len >= 0 && *p != sep ) {
2374 while ( bv->bv_len > 0 && *--p == ' ' ) {
2381 typedef struct aci_set_gather_t {
2387 aci_set_cb_gather( Operation *op, SlapReply *rs )
2389 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2391 if ( rs->sr_type == REP_SEARCH ) {
2392 BerValue bvals[ 2 ];
2393 BerVarray bvalsp = NULL;
2396 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2397 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2399 if ( desc == slap_schema.si_ad_entryDN ) {
2401 bvals[ 0 ] = rs->sr_entry->e_nname;
2402 BER_BVZERO( &bvals[ 1 ] );
2407 a = attr_find( rs->sr_entry->e_attrs, desc );
2411 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2414 bvalsp = a->a_nvals;
2420 p->bvals = slap_set_join( p->cookie, p->bvals,
2421 ( '|' | SLAP_SET_RREF ), bvalsp );
2425 assert( rs->sr_type == REP_RESULT );
2432 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2434 AciSetCookie *cp = (AciSetCookie *)cookie;
2436 LDAPURLDesc *ludp = NULL;
2437 Operation op2 = { 0 };
2438 SlapReply rs = {REP_RESULT};
2439 AttributeName anlist[ 2 ], *anlistp = NULL;
2441 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2442 aci_set_gather_t p = { 0 };
2443 const char *text = NULL;
2444 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2446 /* this routine needs to return the bervals instead of
2447 * plain strings, since syntax is not known. It should
2448 * also return the syntax or some "comparison cookie".
2450 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2451 return aci_set_gather2( cookie, name, desc );
2454 rc = ldap_url_parse( name->bv_val, &ludp );
2455 if ( rc != LDAP_URL_SUCCESS ) {
2456 rc = LDAP_PROTOCOL_ERROR;
2460 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2462 /* host part must be empty */
2463 /* extensions parts must be empty */
2464 rc = LDAP_PROTOCOL_ERROR;
2468 /* Grab the searchbase and see if an appropriate database can be found */
2469 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2470 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2471 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2472 BER_BVZERO( &op2.o_req_dn );
2473 if ( rc != LDAP_SUCCESS ) {
2477 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2478 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2479 rc = LDAP_NO_SUCH_OBJECT;
2483 /* Grab the filter */
2484 if ( ludp->lud_filter ) {
2485 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2486 cp->op->o_tmpmemctx );
2489 op2.ors_filterstr = defaultFilter_bv;
2492 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2493 if ( op2.ors_filter == NULL ) {
2494 rc = LDAP_PROTOCOL_ERROR;
2498 /* Grab the scope */
2499 op2.ors_scope = ludp->lud_scope;
2501 /* Grap the attributes */
2502 if ( ludp->lud_attrs ) {
2503 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2506 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2507 cp->op->o_tmpmemctx );
2509 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2510 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2511 anlistp[ nattrs ].an_desc = NULL;
2512 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2513 &anlistp[ nattrs ].an_desc, &text );
2514 if ( rc != LDAP_SUCCESS ) {
2523 anlistp[ nattrs ].an_name = desc->ad_cname;
2524 anlistp[ nattrs ].an_desc = desc;
2526 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2530 op2.o_hdr = cp->op->o_hdr;
2531 op2.o_tag = LDAP_REQ_SEARCH;
2532 op2.o_ndn = op2.o_bd->be_rootndn;
2533 op2.o_callback = &cb;
2534 op2.o_time = slap_get_time();
2535 op2.o_do_not_cache = 1;
2536 op2.o_is_auth_check = 0;
2537 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2538 op2.ors_slimit = SLAP_NO_LIMIT;
2539 op2.ors_tlimit = SLAP_NO_LIMIT;
2540 op2.ors_attrs = anlistp;
2541 op2.ors_attrsonly = 0;
2542 op2.o_private = cp->op->o_private;
2546 rc = op2.o_bd->be_search( &op2, &rs );
2552 if ( op2.ors_filter ) {
2553 filter_free_x( cp->op, op2.ors_filter );
2555 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2556 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2558 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2559 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2562 ldap_free_urldesc( ludp );
2564 if ( anlistp && anlistp != anlist ) {
2565 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2572 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2574 AciSetCookie *cp = (AciSetCookie *)cookie;
2575 BerVarray bvals = NULL;
2579 /* this routine needs to return the bervals instead of
2580 * plain strings, since syntax is not known. It should
2581 * also return the syntax or some "comparison cookie".
2583 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2584 if ( rc == LDAP_SUCCESS ) {
2585 if ( desc == slap_schema.si_ad_entryDN ) {
2586 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2587 cp->op->o_tmpmemctx );
2589 BER_BVZERO( &bvals[ 1 ] );
2593 backend_attribute( cp->op,
2594 cp->e, &ndn, desc, &bvals, ACL_NONE );
2597 if ( !BER_BVISNULL( &ndn ) ) {
2598 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2607 struct berval *subj,
2613 struct berval set = BER_BVNULL;
2615 AciSetCookie cookie;
2617 if ( setref == 0 ) {
2618 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2621 struct berval subjdn, ndn = BER_BVNULL;
2622 struct berval setat;
2625 AttributeDescription *desc = NULL;
2627 /* format of string is "entry/setAttrName" */
2628 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2632 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2633 setat = aci_bv_set_attr;
2637 * NOTE: dnNormalize honors the ber_len field
2638 * as the length of the dn to be normalized
2640 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2641 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2643 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2644 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2648 BER_BVZERO( &bvals[0] );
2649 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2651 bvals[0].bv_val = bvals[i-1].bv_val;
2652 BER_BVZERO( &bvals[i-1] );
2654 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2655 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2660 if ( !BER_BVISNULL( &set ) ) {
2663 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2664 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2665 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2671 #ifdef SLAPD_ACI_ENABLED
2673 aci_list_map_rights(
2674 struct berval *list )
2681 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2684 switch (*bv.bv_val) {
2686 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2689 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2690 * the right 's' to mean "set", but in the examples states
2691 * that the right 's' means "search". The latter definition
2694 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2697 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2700 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2703 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2704 * define any equivalent to the AUTH right, so I've just used
2707 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2719 struct berval *list,
2720 const struct berval *attr,
2721 struct berval *val )
2723 struct berval bv, left, right;
2726 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2727 if (aci_get_part(&bv, 0, '=', &left) < 0
2728 || aci_get_part(&bv, 1, '=', &right) < 0)
2730 if (ber_bvstrcasecmp(attr, &bv) == 0)
2732 } else if (val == NULL) {
2733 if (ber_bvstrcasecmp(attr, &left) == 0)
2736 if (ber_bvstrcasecmp(attr, &left) == 0) {
2737 /* this is experimental code that implements a
2738 * simple (prefix) match of the attribute value.
2739 * the ACI draft does not provide for aci's that
2740 * apply to specific values, but it would be
2741 * nice to have. If the <attr> part of an aci's
2742 * rights list is of the form <attr>=<value>,
2743 * that means the aci applies only to attrs with
2744 * the given value. Furthermore, if the attr is
2745 * of the form <attr>=<value>*, then <value> is
2746 * treated as a prefix, and the aci applies to
2747 * any value with that prefix.
2749 * Ideally, this would allow r.e. matches.
2751 if (aci_get_part(&right, 0, '*', &left) < 0
2752 || right.bv_len <= left.bv_len)
2754 if (ber_bvstrcasecmp(val, &right) == 0)
2756 } else if (val->bv_len >= left.bv_len) {
2757 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2766 static slap_access_t
2767 aci_list_get_attr_rights(
2768 struct berval *list,
2769 const struct berval *attr,
2770 struct berval *val )
2776 /* loop through each rights/attr pair, skip first part (action) */
2778 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2779 if (aci_list_has_attr(&bv, attr, val) == 0)
2781 if (aci_get_part(list, i, ';', &bv) < 0)
2783 mask |= aci_list_map_rights(&bv);
2789 aci_list_get_rights(
2790 struct berval *list,
2791 const struct berval *attr,
2793 slap_access_t *grant,
2794 slap_access_t *deny )
2796 struct berval perm, actn;
2797 slap_access_t *mask;
2800 if (attr == NULL || attr->bv_len == 0
2801 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2802 attr = &aci_bv_br_entry;
2808 /* loop through each permissions clause */
2809 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2810 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2812 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2814 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2821 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2822 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2829 struct berval *subj,
2830 struct berval *defgrpoc,
2831 struct berval *defgrpat,
2838 struct berval subjdn;
2839 struct berval grpoc;
2840 struct berval grpat;
2841 ObjectClass *grp_oc = NULL;
2842 AttributeDescription *grp_ad = NULL;
2846 /* format of string is "group/objectClassValue/groupAttrName" */
2847 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2851 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2855 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2859 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2860 if( rc != LDAP_SUCCESS ) {
2866 grp_oc = oc_bvfind( &grpoc );
2868 if (grp_oc != NULL && grp_ad != NULL ) {
2869 char buf[ACL_BUF_SIZE];
2870 struct berval bv, ndn;
2871 bv.bv_len = sizeof( buf ) - 1;
2872 bv.bv_val = (char *)&buf;
2873 if ( string_expand(&bv, &subjdn,
2874 e->e_ndn, nmatch, matches) )
2879 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2880 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2881 grp_oc, grp_ad ) == 0 );
2882 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2894 AttributeDescription *desc,
2898 regmatch_t *matches,
2899 slap_access_t *grant,
2900 slap_access_t *deny,
2901 slap_aci_scope_t asserted_scope
2904 struct berval bv, scope, perms, type, sdn;
2908 assert( !BER_BVISNULL( &desc->ad_cname ) );
2910 /* parse an aci of the form:
2911 oid # scope # action;rights;attr;rights;attr
2912 $ action;rights;attr;rights;attr # type # subject
2914 [NOTE: the following comment is very outdated,
2915 as the draft version it refers to (Ando, 2004-11-20)].
2917 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2918 a full description of the format for this attribute.
2919 Differences: "this" in the draft is "self" here, and
2920 "self" and "public" is in the position of type.
2922 <scope> = {entry|children|subtree}
2923 <type> = {public|users|access-id|subtree|onelevel|children|
2924 self|dnattr|group|role|set|set-ref}
2926 This routine now supports scope={ENTRY,CHILDREN}
2928 - ENTRY applies to "entry" and "subtree";
2929 - CHILDREN aplies to "children" and "subtree"
2932 /* check that the aci has all 5 components */
2933 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2937 /* check that the aci family is supported */
2938 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2942 /* check that the scope matches */
2943 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2947 /* note: scope can be either ENTRY or CHILDREN;
2948 * they respectively match "entry" and "children" in bv
2949 * both match "subtree" */
2950 switch ( asserted_scope ) {
2951 case SLAP_ACI_SCOPE_ENTRY:
2952 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2953 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2959 case SLAP_ACI_SCOPE_CHILDREN:
2960 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2961 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2971 /* get the list of permissions clauses, bail if empty */
2972 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2976 /* check if any permissions allow desired access */
2977 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2981 /* see if we have a DN match */
2982 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2986 /* see if we have a public (i.e. anonymous) access */
2987 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2991 /* otherwise require an identity */
2992 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2996 /* see if we have a users access */
2997 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3001 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3002 * just grab all the berval up to its end (ITS#3303).
3003 * NOTE: the problem could be solved by providing the DN with
3004 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3005 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3007 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3011 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3012 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3014 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3017 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3018 if ( rc != LDAP_SUCCESS ) {
3022 if ( dn_match( &op->o_ndn, &ndn ) ) {
3025 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3029 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3032 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3033 if ( rc != LDAP_SUCCESS ) {
3037 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3040 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3044 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3045 struct berval ndn, pndn;
3047 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3048 if ( rc != LDAP_SUCCESS ) {
3052 dnParent( &ndn, &pndn );
3054 if ( dn_match( &op->o_ndn, &pndn ) ) {
3057 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3061 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3064 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3065 if ( rc != LDAP_SUCCESS ) {
3069 if ( !dn_match( &op->o_ndn, &ndn )
3070 && dnIsSuffix( &op->o_ndn, &ndn ) )
3074 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3078 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3079 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3083 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3085 AttributeDescription *ad = NULL;
3088 rc = slap_bv2ad( &sdn, &ad, &text );
3090 if( rc != LDAP_SUCCESS ) {
3096 for ( at = attrs_find( e->e_attrs, ad );
3098 at = attrs_find( at->a_next, ad ) )
3100 if ( value_find_ex( ad,
3101 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3102 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3104 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3113 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3114 if ( aci_group_member( &sdn, &aci_bv_group_class,
3115 &aci_bv_group_attr, op, e, nmatch, matches ) )
3120 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3121 if ( aci_group_member( &sdn, &aci_bv_role_class,
3122 &aci_bv_role_attr, op, e, nmatch, matches ) )
3127 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3128 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3132 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3133 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3143 * FIXME: there is a silly dependence that makes it difficult
3144 * to move ACIs in a run-time loadable module under the "dynacl"
3145 * umbrella, because sets share some helpers with ACIs.
3148 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3150 AttributeDescription *ad = NULL;
3151 const char *text = NULL;
3153 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3154 fprintf( stderr, "%s: line %d: "
3155 "inappropriate style \"%s\" in \"aci\" by clause\n",
3156 fname, lineno, style_strings[sty] );
3160 if ( right != NULL && *right != '\0' ) {
3161 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3163 "%s: line %d: aci \"%s\": %s\n",
3164 fname, lineno, right, text );
3169 ad = slap_schema.si_ad_aci;
3172 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3173 fprintf( stderr, "%s: line %d: "
3174 "aci \"%s\": inappropriate syntax: %s\n",
3175 fname, lineno, right,
3176 ad->ad_type->sat_syntax_oid );
3180 *privp = (void *)ad;
3186 dynacl_aci_unparse( void *priv, struct berval *bv )
3188 AttributeDescription *ad = ( AttributeDescription * )priv;
3191 assert( ad != NULL );
3193 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3194 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3195 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3196 bv->bv_len = ptr - bv->bv_val;
3207 AttributeDescription *desc,
3210 regmatch_t *matches,
3211 slap_access_t *grantp,
3212 slap_access_t *denyp )
3214 AttributeDescription *ad = ( AttributeDescription * )priv;
3216 slap_access_t tgrant, tdeny, grant, deny;
3218 char accessmaskbuf[ACCESSMASK_MAXLEN];
3219 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3220 #endif /* LDAP_DEBUG */
3222 /* start out with nothing granted, nothing denied */
3226 /* get the aci attribute */
3227 at = attr_find( e->e_attrs, ad );
3231 /* the aci is an multi-valued attribute. The
3232 * rights are determined by OR'ing the individual
3233 * rights given by the acis.
3235 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3236 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3237 nmatch, matches, &grant, &deny,
3238 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3245 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3246 accessmask2str( tgrant, accessmaskbuf, 1 ),
3247 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3250 /* If the entry level aci didn't contain anything valid for the
3251 * current operation, climb up the tree and evaluate the
3252 * acis with scope set to subtree
3254 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3255 struct berval parent_ndn;
3258 /* to solve the chicken'n'egg problem of accessing
3259 * the OpenLDAPaci attribute, the direct access
3260 * to the entry's attribute is unchecked; however,
3261 * further accesses to OpenLDAPaci values in the
3262 * ancestors occur through backend_attribute(), i.e.
3263 * with the identity of the operation, requiring
3264 * further access checking. For uniformity, this
3265 * makes further requests occur as the rootdn, if
3266 * any, i.e. searching for the OpenLDAPaci attribute
3267 * is considered an internal search. If this is not
3268 * acceptable, then the same check needs be performed
3269 * when accessing the entry's attribute. */
3270 Operation op2 = *op;
3272 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3273 op2.o_dn = op->o_bd->be_rootdn;
3274 op2.o_ndn = op->o_bd->be_rootndn;
3278 dnParent( &e->e_nname, &parent_ndn );
3279 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3281 BerVarray bvals = NULL;
3284 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3285 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3294 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3295 if ( aci_mask( op, e, desc, val,
3299 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3303 /* evaluation stops as soon as either a "deny" or a
3304 * "grant" directive matches.
3306 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3310 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3311 accessmask2str( tgrant, accessmaskbuf, 1 ),
3312 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3316 case LDAP_NO_SUCH_ATTRIBUTE:
3317 /* just go on if the aci-Attribute is not present in
3320 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3324 case LDAP_NO_SUCH_OBJECT:
3325 /* We have reached the base object */
3326 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3338 dnParent( &parent_ndn, &parent_ndn );
3348 /* need to register this at some point */
3349 static slap_dynacl_t dynacl_aci = {
3359 #endif /* SLAP_DYNACL */
3361 #endif /* SLAPD_ACI_ENABLED */
3366 * dynamic ACL infrastructure
3368 static slap_dynacl_t *da_list = NULL;
3371 slap_dynacl_register( slap_dynacl_t *da )
3375 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3376 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3381 if ( tmp != NULL ) {
3385 if ( da->da_mask == NULL ) {
3389 da->da_private = NULL;
3390 da->da_next = da_list;
3396 static slap_dynacl_t *
3397 slap_dynacl_next( slap_dynacl_t *da )
3406 slap_dynacl_get( const char *name )
3410 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3411 if ( strcasecmp( da->da_name, name ) == 0 ) {
3418 #endif /* SLAP_DYNACL */
3425 slap_dynacl_t *known_dynacl[] = {
3426 #ifdef SLAPD_ACI_ENABLED
3428 #endif /* SLAPD_ACI_ENABLED */
3432 for ( i = 0; known_dynacl[ i ]; i++ ) {
3433 rc = slap_dynacl_register( known_dynacl[ i ] );
3438 #endif /* SLAP_DYNACL */
3449 regmatch_t *matches)
3457 bv->bv_val[0] = '\0';
3458 bv->bv_len--; /* leave space for lone $ */
3461 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3462 sp < pat->bv_val + pat->bv_len ; sp++ )
3464 /* did we previously see a $ */
3466 if ( flag == 1 && *sp == '$' ) {
3471 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3474 } else if ( *sp >= '0' && *sp <= '9' ) {
3482 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3483 if ( *sp >= '0' && *sp <= '9' ) {
3484 n = 10*n + ( *sp - '0' );
3488 if ( *sp != /*'{'*/ '}' ) {
3494 if ( n >= nmatch ) {
3500 i = matches[n].rm_so;
3501 l = matches[n].rm_eo;
3502 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3520 /* must have ended with a single $ */
3528 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3529 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3536 struct berval *pat, /* pattern to expand and match against */
3537 char *str, /* string to match against pattern */
3538 char *buf, /* buffer with $N expansion variables */
3539 int nmatch, /* size of the matches array */
3540 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3544 char newbuf[ACL_BUF_SIZE];
3548 bv.bv_len = sizeof( newbuf ) - 1;
3555 string_expand( &bv, pat, buf, nmatch, matches );
3556 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3558 char error[ACL_BUF_SIZE];
3559 regerror( rc, &re, error, sizeof( error ) );
3561 Debug( LDAP_DEBUG_TRACE,
3562 "compile( \"%s\", \"%s\") failed %s\n",
3563 pat->bv_val, str, error );
3567 rc = regexec( &re, str, 0, NULL, 0 );
3570 Debug( LDAP_DEBUG_TRACE,
3571 "=> regex_matches: string: %s\n", str, 0, 0 );
3572 Debug( LDAP_DEBUG_TRACE,
3573 "=> regex_matches: rc: %d %s\n",
3574 rc, !rc ? "matches" : "no matches", 0 );