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,
354 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
355 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
365 AttributeDescription *desc,
367 slap_access_t access,
368 AccessControlState *state,
372 AccessControl *a = NULL;
376 char accessmaskbuf[ACCESSMASK_MAXLEN];
379 slap_control_t control;
380 slap_access_t access_level;
382 int st_same_attr = 0;
383 static AccessControlState state_init = ACL_STATE_INIT;
386 assert( desc != NULL );
388 access_level = ACL_LEVEL( access );
390 assert( access_level > ACL_NONE );
393 if ( maskp ) ACL_INVALIDATE( *maskp );
395 attr = desc->ad_cname.bv_val;
397 assert( attr != NULL );
399 if ( op && op->o_is_auth_check &&
400 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
406 if ( state->as_vd_ad == desc ) {
407 if ( state->as_recorded ) {
408 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
411 return state->as_result;
413 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
414 val != NULL && state->as_vd_acl == NULL )
416 return state->as_result;
424 state->as_vd_ad = desc;
427 Debug( LDAP_DEBUG_ACL,
428 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
429 access2str( access ), e->e_dn, attr );
436 if ( op->o_bd == NULL ) {
437 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
442 * FIXME: experimental; use first backend rules
443 * iff there is no global_acl (ITS#3100) */
444 if ( frontendDB->be_acl != NULL ) {
445 op->o_bd = frontendDB;
447 #endif /* LDAP_DEVEL */
449 assert( op->o_bd != NULL );
451 /* this is enforced in backend_add() */
452 if ( op->o_bd->bd_info->bi_access_allowed ) {
453 /* delegate to backend */
454 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
455 desc, val, access, state, &mask );
458 BackendDB *be_orig = op->o_bd;
460 /* use default (but pass through frontend
461 * for global ACL overlays) */
462 op->o_bd = frontendDB;
463 ret = frontendDB->bd_info->bi_access_allowed( op, e,
464 desc, val, access, state, &mask );
469 if ( ACL_IS_INVALID( mask ) ) {
470 Debug( LDAP_DEBUG_ACL,
471 "=> access_allowed: \"%s\" (%s) invalid!\n",
475 } else if ( control == ACL_BREAK ) {
476 Debug( LDAP_DEBUG_ACL,
477 "=> access_allowed: no more rules\n", 0, 0, 0 );
482 ret = ACL_GRANT( mask, access );
485 Debug( LDAP_DEBUG_ACL,
486 "=> access_allowed: %s access %s by %s\n",
487 access2str( access ), ret ? "granted" : "denied",
488 accessmask2str( mask, accessmaskbuf, 1 ) );
491 if ( state != NULL ) {
492 /* If not value-dependent, save ACL in case of more attrs */
493 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
494 state->as_vi_acl = a;
495 state->as_result = ret;
497 state->as_recorded |= ACL_STATE_RECORDED;
499 if ( be_null ) op->o_bd = NULL;
500 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
504 #else /* !SLAP_OVERLAY_ACCESS */
510 AttributeDescription *desc,
512 slap_access_t access,
513 AccessControlState *state,
518 AccessControl *a = NULL;
523 char accessmaskbuf[ACCESSMASK_MAXLEN];
526 slap_control_t control;
527 slap_access_t access_level;
529 regmatch_t matches[MAXREMATCHES];
530 int st_same_attr = 0;
531 static AccessControlState state_init = ACL_STATE_INIT;
534 assert( desc != NULL );
536 access_level = ACL_LEVEL( access );
538 assert( access_level > ACL_NONE );
539 if ( maskp ) ACL_INVALIDATE( *maskp );
541 attr = desc->ad_cname.bv_val;
543 assert( attr != NULL );
545 if ( op && op->o_is_auth_check &&
546 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
552 if ( state->as_vd_ad == desc ) {
553 if ( state->as_recorded ) {
554 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
557 return state->as_result;
559 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
560 val != NULL && state->as_vd_acl == NULL )
562 return state->as_result;
570 state->as_vd_ad=desc;
573 Debug( LDAP_DEBUG_ACL,
574 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
575 access2str( access ), e->e_dn, attr );
584 be = LDAP_STAILQ_FIRST(&backendDB);
588 * FIXME: experimental; use first backend rules
589 * iff there is no global_acl (ITS#3100) */
590 if ( frontendDB->be_acl == NULL )
596 assert( be != NULL );
598 /* grant database root access */
599 if ( be_isroot( op ) ) {
600 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
602 mask = ACL_LVL_MANAGE;
609 * no-user-modification operational attributes are ignored
610 * by ACL_WRITE checking as any found here are not provided
613 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
614 && desc != slap_schema.si_ad_entry
615 && desc != slap_schema.si_ad_children )
617 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
618 " %s access granted\n",
623 /* use backend default access if no backend acls */
624 if ( be->be_acl == NULL ) {
625 Debug( LDAP_DEBUG_ACL,
626 "=> access_allowed: backend default %s "
627 "access %s to \"%s\"\n",
628 access2str( access ),
629 be->be_dfltaccess >= access_level ? "granted" : "denied",
630 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
631 ret = be->be_dfltaccess >= access_level;
636 mask = ACL_PRIV_LEVEL;
637 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
638 mask |= ACL_ACCESS2PRIV( i );
645 /* be is always non-NULL */
646 /* use global default access if no global acls */
647 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
648 Debug( LDAP_DEBUG_ACL,
649 "=> access_allowed: global default %s access %s to \"%s\"\n",
650 access2str( access ),
651 frontendDB->be_dfltaccess >= access_level ?
652 "granted" : "denied", op->o_dn.bv_val );
653 ret = frontendDB->be_dfltaccess >= access_level;
658 mask = ACL_PRIV_LEVEL;
659 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
660 mask |= ACL_ACCESS2PRIV( i );
671 if ( st_same_attr ) {
672 assert( state->as_vd_acl != NULL );
674 a = state->as_vd_acl;
675 count = state->as_vd_acl_count;
676 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
677 mask = state->as_vd_acl_mask;
678 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
683 if ( state ) state->as_vi_acl = NULL;
687 memset( matches, '\0', sizeof(matches) );
690 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
691 MAXREMATCHES, matches, state ) ) != NULL )
695 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
696 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
697 (int)matches[i].rm_so, (int)matches[i].rm_eo );
698 if ( matches[i].rm_so <= matches[0].rm_eo ) {
700 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
701 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
704 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
708 if ( state->as_vi_acl == a &&
709 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
711 Debug( LDAP_DEBUG_ACL,
712 "access_allowed: result from state (%s)\n",
714 ret = state->as_result;
717 Debug( LDAP_DEBUG_ACL,
718 "access_allowed: no res from state (%s)\n",
724 control = slap_acl_mask( a, &mask, op,
725 e, desc, val, MAXREMATCHES, matches, count, state );
727 if ( control != ACL_BREAK ) {
731 memset( matches, '\0', sizeof(matches) );
734 if ( ACL_IS_INVALID( mask ) ) {
735 Debug( LDAP_DEBUG_ACL,
736 "=> access_allowed: \"%s\" (%s) invalid!\n",
740 } else if ( control == ACL_BREAK ) {
741 Debug( LDAP_DEBUG_ACL,
742 "=> access_allowed: no more rules\n", 0, 0, 0 );
747 Debug( LDAP_DEBUG_ACL,
748 "=> access_allowed: %s access %s by %s\n",
749 access2str( access ),
750 ACL_GRANT(mask, access) ? "granted" : "denied",
751 accessmask2str( mask, accessmaskbuf, 1 ) );
753 ret = ACL_GRANT(mask, access);
756 if ( state != NULL ) {
757 /* If not value-dependent, save ACL in case of more attrs */
758 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
759 state->as_vi_acl = a;
760 state->as_result = ret;
762 state->as_recorded |= ACL_STATE_RECORDED;
764 if ( be_null ) op->o_bd = NULL;
765 if ( maskp ) *maskp = mask;
769 #endif /* SLAP_OVERLAY_ACCESS */
772 * slap_acl_get - return the acl applicable to entry e, attribute
773 * attr. the acl returned is suitable for use in subsequent calls to
774 * acl_access_allowed().
777 static AccessControl *
783 AttributeDescription *desc,
787 AccessControlState *state )
794 assert( count != NULL );
795 assert( desc != NULL );
797 attr = desc->ad_cname.bv_val;
799 assert( attr != NULL );
802 if( op->o_bd == NULL ) {
803 a = frontendDB->be_acl;
805 a = op->o_bd->be_acl;
816 dnlen = e->e_nname.bv_len;
818 for ( ; a != NULL; a = a->acl_next ) {
821 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
822 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
823 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
824 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
825 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
829 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
830 *count, a->acl_dn_pat.bv_val, 0 );
831 patlen = a->acl_dn_pat.bv_len;
832 if ( dnlen < patlen )
835 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
836 /* base dn -- entire object DN must match */
837 if ( dnlen != patlen )
840 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
841 int rdnlen = -1, sep = 0;
843 if ( dnlen <= patlen )
847 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
852 rdnlen = dn_rdnlen( NULL, &e->e_nname );
853 if ( rdnlen != dnlen - patlen - sep )
856 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
857 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
860 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
861 if ( dnlen <= patlen )
863 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
867 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
871 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
875 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
876 matches[0].rm_so = matches[0].rm_eo = -1;
880 /* Is this ACL only for a specific value? */
881 if ( a->acl_attrval.bv_len ) {
886 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
887 state->as_recorded |= ACL_STATE_RECORDED_VD;
888 state->as_vd_acl = a;
889 state->as_vd_acl_count = *count;
890 state->as_vd_access = a->acl_access;
891 state->as_vd_access_count = 1;
892 ACL_INVALIDATE( state->as_vd_acl_mask );
895 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
896 Debug( LDAP_DEBUG_ACL,
897 "acl_get: valpat %s\n",
898 a->acl_attrval.bv_val, 0, 0 );
899 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
907 Debug( LDAP_DEBUG_ACL,
909 a->acl_attrval.bv_val, 0, 0 );
911 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
912 if (value_match( &match, desc,
913 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
914 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
921 patlen = a->acl_attrval.bv_len;
922 vdnlen = val->bv_len;
924 if ( vdnlen < patlen )
927 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
928 if ( vdnlen > patlen )
931 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
934 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
937 rdnlen = dn_rdnlen( NULL, val );
938 if ( rdnlen != vdnlen - patlen - 1 )
941 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
942 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
945 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
946 if ( vdnlen <= patlen )
949 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
953 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
959 if ( a->acl_filter != NULL ) {
960 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
961 if ( rc != LDAP_COMPARE_TRUE ) {
966 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
971 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
983 struct berval *opndn )
986 * if access applies to the entry itself, and the
987 * user is bound as somebody in the same namespace as
988 * the entry, OR the given dn matches the dn pattern
991 * NOTE: styles "anonymous", "users" and "self"
992 * have been moved to enum slap_style_t, whose
993 * value is set in a_dn_style; however, the string
994 * is maintaned in a_dn_pat.
996 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
997 if ( !BER_BVISEMPTY( opndn ) ) {
1001 } else if ( b->a_style == ACL_STYLE_USERS ) {
1002 if ( BER_BVISEMPTY( opndn ) ) {
1006 } else if ( b->a_style == ACL_STYLE_SELF ) {
1007 struct berval ndn, selfndn;
1010 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1014 level = b->a_self_level;
1022 selfndn = e->e_nname;
1025 for ( ; level > 0; level-- ) {
1026 if ( BER_BVISEMPTY( &ndn ) ) {
1029 dnParent( &ndn, &ndn );
1032 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1037 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1038 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1040 regmatch_t tmp_matches[2],
1041 *tmp_matchesp = tmp_matches;
1045 switch ( a->acl_dn_style ) {
1046 case ACL_STYLE_REGEX:
1047 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1048 tmp_matchesp = matches;
1049 tmp_nmatch = nmatch;
1052 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1054 case ACL_STYLE_BASE:
1055 tmp_matches[0].rm_so = 0;
1056 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1061 case ACL_STYLE_SUBTREE:
1062 case ACL_STYLE_CHILDREN:
1063 tmp_matches[0].rm_so = 0;
1064 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1065 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1066 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1080 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1081 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1089 ber_len_t patlen, odnlen;
1092 if ( e->e_dn == NULL )
1095 if ( b->a_expand ) {
1097 char buf[ACL_BUF_SIZE];
1100 regmatch_t tmp_matches[2],
1101 *tmp_matchesp = tmp_matches;
1105 bv.bv_len = sizeof( buf ) - 1;
1108 switch ( a->acl_dn_style ) {
1109 case ACL_STYLE_REGEX:
1110 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1111 tmp_matchesp = matches;
1112 tmp_nmatch = nmatch;
1115 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1117 case ACL_STYLE_BASE:
1118 tmp_matches[0].rm_so = 0;
1119 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1124 case ACL_STYLE_SUBTREE:
1125 case ACL_STYLE_CHILDREN:
1126 tmp_matches[0].rm_so = 0;
1127 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1128 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1129 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1143 if ( string_expand( &bv, &b->a_pat,
1145 tmp_nmatch, tmp_matchesp ) )
1150 if ( dnNormalize(0, NULL, NULL, &bv,
1151 &pat, op->o_tmpmemctx )
1154 /* did not expand to a valid dn */
1162 patlen = pat.bv_len;
1163 odnlen = opndn->bv_len;
1164 if ( odnlen < patlen ) {
1165 goto dn_match_cleanup;
1169 if ( b->a_style == ACL_STYLE_BASE ) {
1170 /* base dn -- entire object DN must match */
1171 if ( odnlen != patlen ) {
1172 goto dn_match_cleanup;
1175 } else if ( b->a_style == ACL_STYLE_ONE ) {
1178 if ( odnlen <= patlen ) {
1179 goto dn_match_cleanup;
1182 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1183 goto dn_match_cleanup;
1186 rdnlen = dn_rdnlen( NULL, opndn );
1187 if ( rdnlen != odnlen - patlen - 1 ) {
1188 goto dn_match_cleanup;
1191 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1192 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1193 goto dn_match_cleanup;
1196 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1197 if ( odnlen <= patlen ) {
1198 goto dn_match_cleanup;
1201 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1202 goto dn_match_cleanup;
1205 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1209 if ( odnlen <= patlen ) {
1210 goto dn_match_cleanup;
1213 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1215 goto dn_match_cleanup;
1220 for ( ; level > 0; level-- ) {
1221 if ( BER_BVISEMPTY( &ndn ) ) {
1222 goto dn_match_cleanup;
1224 dnParent( &ndn, &ndn );
1225 if ( ndn.bv_len < patlen ) {
1226 goto dn_match_cleanup;
1230 if ( ndn.bv_len != patlen ) {
1231 goto dn_match_cleanup;
1235 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1238 if ( pat.bv_val != b->a_pat.bv_val ) {
1239 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1251 * Record value-dependent access control state
1253 #define ACL_RECORD_VALUE_STATE do { \
1254 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1255 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1256 state->as_vd_acl = a; \
1257 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1258 sizeof( state->as_vd_acl_matches )) ; \
1259 state->as_vd_acl_count = count; \
1260 state->as_vd_access = b; \
1261 state->as_vd_access_count = i; \
1273 regmatch_t *matches,
1275 AccessControlState *state,
1276 slap_dn_access *bdn,
1277 struct berval *opndn )
1283 const char *attr = bdn->a_at->ad_cname.bv_val;
1285 assert( attr != NULL );
1287 if ( BER_BVISEMPTY( opndn ) ) {
1291 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1294 /* see if asker is listed in dnattr */
1295 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1297 at = attrs_find( at->a_next, bdn->a_at ) )
1299 if ( value_find_ex( bdn->a_at,
1300 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1301 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1303 &bv, op->o_tmpmemctx ) == 0 )
1312 /* have a dnattr match. if this is a self clause then
1313 * the target must also match the op dn.
1315 if ( bdn->a_self ) {
1316 /* check if the target is an attribute. */
1317 if ( val == NULL ) return 1;
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,
1325 /* on match error or no match, fail the ACL clause */
1326 if ( rc != LDAP_SUCCESS || match != 0 )
1331 /* no dnattr match, check if this is a self clause */
1332 if ( ! bdn->a_self )
1335 ACL_RECORD_VALUE_STATE;
1337 /* this is a self clause, check if the target is an
1343 /* target is attribute, check if the attribute value
1346 rc = value_match( &match, bdn->a_at,
1347 bdn->a_at->ad_type->sat_equality, 0,
1350 /* on match error or no match, fail the ACL clause */
1351 if ( rc != LDAP_SUCCESS || match != 0 )
1360 * slap_acl_mask - modifies mask based upon the given acl and the
1361 * requested access to entry e, attribute attr, value val. if val
1362 * is null, access to the whole attribute is assumed (all values).
1364 * returns 0 access NOT allowed
1368 static slap_control_t
1374 AttributeDescription *desc,
1377 regmatch_t *matches,
1379 AccessControlState *state )
1384 char accessmaskbuf[ACCESSMASK_MAXLEN];
1385 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1386 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1387 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1391 assert( a != NULL );
1392 assert( mask != NULL );
1393 assert( desc != NULL );
1395 attr = desc->ad_cname.bv_val;
1397 assert( attr != NULL );
1399 Debug( LDAP_DEBUG_ACL,
1400 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1403 Debug( LDAP_DEBUG_ACL,
1404 "=> acl_mask: to %s by \"%s\", (%s) \n",
1405 val ? "value" : "all values",
1406 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1407 accessmask2str( *mask, accessmaskbuf, 1 ) );
1410 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1411 && state->as_vd_acl == a )
1413 b = state->as_vd_access;
1414 i = state->as_vd_access_count;
1421 for ( ; b != NULL; b = b->a_next, i++ ) {
1422 slap_mask_t oldmask, modmask;
1424 ACL_INVALIDATE( modmask );
1426 /* AND <who> clauses */
1427 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1428 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1429 b->a_dn_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 ( acl_mask_dn( op, e, a, nmatch, matches,
1443 &b->a_dn, &op->o_ndn ) )
1449 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1452 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1453 b->a_realdn_pat.bv_val, 0, 0);
1455 * if access applies to the entry itself, and the
1456 * user is bound as somebody in the same namespace as
1457 * the entry, OR the given dn matches the dn pattern
1460 * NOTE: styles "anonymous", "users" and "self"
1461 * have been moved to enum slap_style_t, whose
1462 * value is set in a_dn_style; however, the string
1463 * is maintaned in a_dn_pat.
1466 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1468 ndn = op->o_conn->c_ndn;
1473 if ( acl_mask_dn( op, e, a, nmatch, matches,
1474 &b->a_realdn, &ndn ) )
1480 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1481 if ( ! op->o_conn->c_listener ) {
1484 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1485 b->a_sockurl_pat.bv_val, 0, 0 );
1487 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1488 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1489 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1490 e->e_ndn, nmatch, matches ) )
1495 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1497 char buf[ACL_BUF_SIZE];
1499 bv.bv_len = sizeof( buf ) - 1;
1501 if ( string_expand( &bv, &b->a_sockurl_pat,
1502 e->e_ndn, nmatch, matches ) )
1507 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1513 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1521 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1522 if ( !op->o_conn->c_peer_domain.bv_val ) {
1525 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1526 b->a_domain_pat.bv_val, 0, 0 );
1527 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1528 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1529 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1530 e->e_ndn, nmatch, matches ) )
1535 char buf[ACL_BUF_SIZE];
1537 struct berval cmp = op->o_conn->c_peer_domain;
1538 struct berval pat = b->a_domain_pat;
1540 if ( b->a_domain_expand ) {
1543 bv.bv_len = sizeof(buf) - 1;
1546 if ( string_expand(&bv, &b->a_domain_pat,
1547 e->e_ndn, nmatch, matches) )
1554 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1555 int offset = cmp.bv_len - pat.bv_len;
1560 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1564 /* trim the domain */
1565 cmp.bv_val = &cmp.bv_val[ offset ];
1566 cmp.bv_len -= offset;
1569 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1576 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1577 if ( !op->o_conn->c_peer_name.bv_val ) {
1580 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1581 b->a_peername_pat.bv_val, 0, 0 );
1582 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1583 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1584 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1585 e->e_ndn, nmatch, matches ) )
1591 /* try exact match */
1592 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1593 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1597 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1599 char buf[ACL_BUF_SIZE];
1601 bv.bv_len = sizeof( buf ) - 1;
1603 if ( string_expand( &bv, &b->a_peername_pat,
1604 e->e_ndn, nmatch, matches ) )
1609 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1613 /* extract IP and try exact match */
1614 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1616 char buf[] = "255.255.255.255";
1619 int port_number = -1;
1621 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1622 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1625 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1626 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1628 port = strrchr( ip.bv_val, ':' );
1632 ip.bv_len = port - ip.bv_val;
1634 port_number = strtol( port, &next, 10 );
1635 if ( next[0] != '\0' )
1639 /* the port check can be anticipated here */
1640 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1643 /* address longer than expected? */
1644 if ( ip.bv_len >= sizeof(buf) )
1647 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1648 buf[ ip.bv_len ] = '\0';
1650 addr = inet_addr( buf );
1652 /* unable to convert? */
1653 if ( addr == (unsigned long)(-1) )
1656 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1659 #ifdef LDAP_PF_LOCAL
1660 /* extract path and try exact match */
1661 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1664 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1665 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1668 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1669 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1671 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1674 #endif /* LDAP_PF_LOCAL */
1676 /* exact match (very unlikely...) */
1677 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1684 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1685 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1688 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1689 b->a_sockname_pat.bv_val, 0, 0 );
1690 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1691 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1692 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1693 e->e_ndn, nmatch, matches ) )
1698 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1700 char buf[ACL_BUF_SIZE];
1702 bv.bv_len = sizeof( buf ) - 1;
1704 if ( string_expand( &bv, &b->a_sockname_pat,
1705 e->e_ndn, nmatch, matches ) )
1710 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1715 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1722 if ( b->a_dn_at != NULL ) {
1723 if ( acl_mask_dnattr( op, e, val, a, b, i,
1724 matches, count, state,
1725 &b->a_dn, &op->o_ndn ) )
1731 if ( b->a_realdn_at != NULL ) {
1734 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1736 ndn = op->o_conn->c_ndn;
1741 if ( acl_mask_dnattr( op, e, val, a, b, i,
1742 matches, count, state,
1743 &b->a_realdn, &ndn ) )
1749 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1751 struct berval ndn = BER_BVNULL;
1754 if ( op->o_ndn.bv_len == 0 ) {
1758 /* b->a_group is an unexpanded entry name, expanded it should be an
1759 * entry with objectclass group* and we test to see if odn is one of
1760 * the values in the attribute group
1762 /* see if asker is listed in dnattr */
1763 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1764 char buf[ACL_BUF_SIZE];
1766 regmatch_t tmp_matches[2],
1767 *tmp_matchesp = tmp_matches;
1769 bv.bv_len = sizeof(buf) - 1;
1774 switch ( a->acl_dn_style ) {
1775 case ACL_STYLE_REGEX:
1776 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1777 tmp_matchesp = matches;
1778 tmp_nmatch = nmatch;
1782 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1783 case ACL_STYLE_BASE:
1784 tmp_matches[0].rm_so = 0;
1785 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1790 case ACL_STYLE_SUBTREE:
1791 case ACL_STYLE_CHILDREN:
1792 tmp_matches[0].rm_so = 0;
1793 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1794 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1795 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1809 if ( string_expand( &bv, &b->a_group_pat,
1811 tmp_nmatch, tmp_matchesp ) )
1816 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1817 op->o_tmpmemctx ) != LDAP_SUCCESS )
1819 /* did not expand to a valid dn */
1826 bv = b->a_group_pat;
1829 rc = backend_group( op, e, &bv, &op->o_ndn,
1830 b->a_group_oc, b->a_group_at );
1833 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1841 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1843 char buf[ACL_BUF_SIZE];
1845 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1847 regmatch_t tmp_matches[2],
1848 *tmp_matchesp = tmp_matches;
1851 bv.bv_len = sizeof( buf ) - 1;
1856 switch ( a->acl_dn_style ) {
1857 case ACL_STYLE_REGEX:
1858 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1859 tmp_matchesp = matches;
1860 tmp_nmatch = nmatch;
1864 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1865 case ACL_STYLE_BASE:
1866 tmp_matches[0].rm_so = 0;
1867 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1872 case ACL_STYLE_SUBTREE:
1873 case ACL_STYLE_CHILDREN:
1874 tmp_matches[0].rm_so = 0;
1875 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1876 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1877 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1891 if ( string_expand( &bv, &b->a_set_pat,
1893 tmp_nmatch, tmp_matchesp ) )
1902 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1907 if ( b->a_authz.sai_ssf ) {
1908 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1909 b->a_authz.sai_ssf, op->o_ssf, 0 );
1910 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1915 if ( b->a_authz.sai_transport_ssf ) {
1916 Debug( LDAP_DEBUG_ACL,
1917 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1918 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1919 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1924 if ( b->a_authz.sai_tls_ssf ) {
1925 Debug( LDAP_DEBUG_ACL,
1926 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1927 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1928 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1933 if ( b->a_authz.sai_sasl_ssf ) {
1934 Debug( LDAP_DEBUG_ACL,
1935 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1936 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1937 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1943 if ( b->a_dynacl ) {
1945 slap_access_t tgrant, tdeny;
1947 /* this case works different from the others above.
1948 * since aci's themselves give permissions, we need
1949 * to first check b->a_access_mask, the ACL's access level.
1951 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1952 /* no ACIs in the root DSE */
1956 /* first check if the right being requested
1957 * is allowed by the ACL clause.
1959 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1963 /* start out with nothing granted, nothing denied */
1967 for ( da = b->a_dynacl; da; da = da->da_next ) {
1968 slap_access_t grant, deny;
1970 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1976 /* remove anything that the ACL clause does not allow */
1977 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1978 tdeny &= ACL_PRIV_MASK;
1980 /* see if we have anything to contribute */
1981 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1985 /* this could be improved by changing slap_acl_mask so that it can deal with
1986 * by clauses that return grant/deny pairs. Right now, it does either
1987 * additive or subtractive rights, but not both at the same time. So,
1988 * we need to combine the grant/deny pair into a single rights mask in
1989 * a smart way: if either grant or deny is "empty", then we use the
1990 * opposite as is, otherwise we remove any denied rights from the grant
1991 * rights mask and construct an additive mask.
1993 if (ACL_IS_INVALID(tdeny)) {
1994 modmask = tgrant | ACL_PRIV_ADDITIVE;
1996 } else if (ACL_IS_INVALID(tgrant)) {
1997 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2000 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2004 #else /* !SLAP_DYNACL */
2006 #ifdef SLAPD_ACI_ENABLED
2007 if ( b->a_aci_at != NULL ) {
2009 slap_access_t grant, deny, tgrant, tdeny;
2010 struct berval parent_ndn;
2011 BerVarray bvals = NULL;
2014 /* this case works different from the others above.
2015 * since aci's themselves give permissions, we need
2016 * to first check b->a_access_mask, the ACL's access level.
2019 if ( BER_BVISEMPTY( &e->e_nname ) ) {
2020 /* no ACIs in the root DSE */
2024 /* first check if the right being requested
2025 * is allowed by the ACL clause.
2027 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2030 /* start out with nothing granted, nothing denied */
2034 /* get the aci attribute */
2035 at = attr_find( e->e_attrs, b->a_aci_at );
2038 /* FIXME: this breaks acl caching;
2039 * see also ACL_RECORD_VALUE_STATE below */
2040 ACL_RECORD_VALUE_STATE;
2042 /* the aci is an multi-valued attribute. The
2043 * rights are determined by OR'ing the individual
2044 * rights given by the acis.
2046 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2051 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2057 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2058 accessmask2str(tgrant, accessmaskbuf, 1),
2059 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2062 /* If the entry level aci didn't contain anything valid for the
2063 * current operation, climb up the tree and evaluate the
2064 * acis with scope set to subtree
2066 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2067 dnParent( &e->e_nname, &parent_ndn );
2068 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2069 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2070 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2078 for( i = 0; bvals[i].bv_val != NULL; i++){
2080 /* FIXME: this breaks acl caching;
2081 * see also ACL_RECORD_VALUE_STATE above */
2082 ACL_RECORD_VALUE_STATE;
2084 if (aci_mask(op, e, desc, val, &bvals[i],
2086 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2090 /* evaluation stops as soon as either a "deny" or a
2091 * "grant" directive matches.
2093 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2097 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2098 accessmask2str(tgrant, accessmaskbuf, 1),
2099 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2103 case LDAP_NO_SUCH_ATTRIBUTE:
2104 /* just go on if the aci-Attribute is not present in
2107 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2111 case LDAP_NO_SUCH_OBJECT:
2112 /* We have reached the base object */
2113 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2124 dnParent( &parent_ndn, &parent_ndn );
2129 /* remove anything that the ACL clause does not allow */
2130 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2131 tdeny &= ACL_PRIV_MASK;
2133 /* see if we have anything to contribute */
2134 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2138 /* this could be improved by changing slap_acl_mask so that it can deal with
2139 * by clauses that return grant/deny pairs. Right now, it does either
2140 * additive or subtractive rights, but not both at the same time. So,
2141 * we need to combine the grant/deny pair into a single rights mask in
2142 * a smart way: if either grant or deny is "empty", then we use the
2143 * opposite as is, otherwise we remove any denied rights from the grant
2144 * rights mask and construct an additive mask.
2146 if (ACL_IS_INVALID(tdeny)) {
2147 modmask = tgrant | ACL_PRIV_ADDITIVE;
2149 } else if (ACL_IS_INVALID(tgrant)) {
2150 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2153 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2157 #endif /* SLAPD_ACI_ENABLED */
2158 #endif /* !SLAP_DYNACL */
2160 modmask = b->a_access_mask;
2163 Debug( LDAP_DEBUG_ACL,
2164 "<= acl_mask: [%d] applying %s (%s)\n",
2165 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2166 b->a_type == ACL_CONTINUE
2168 : b->a_type == ACL_BREAK
2174 if( ACL_IS_ADDITIVE(modmask) ) {
2176 ACL_PRIV_SET( *mask, modmask );
2179 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2181 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2182 /* substract privs */
2183 ACL_PRIV_CLR( *mask, modmask );
2186 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2193 Debug( LDAP_DEBUG_ACL,
2194 "<= acl_mask: [%d] mask: %s\n",
2195 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2197 if( b->a_type == ACL_CONTINUE ) {
2200 } else if ( b->a_type == ACL_BREAK ) {
2208 /* implicit "by * none" clause */
2211 Debug( LDAP_DEBUG_ACL,
2212 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2213 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2218 * acl_check_modlist - check access control on the given entry to see if
2219 * it allows the given modifications by the user associated with op.
2220 * returns 1 if mods allowed ok
2221 * 0 mods not allowed
2228 Modifications *mlist
2232 AccessControlState state = ACL_STATE_INIT;
2235 int ret = 1; /* default is access allowed */
2239 be = LDAP_STAILQ_FIRST(&backendDB);
2243 assert( be != NULL );
2245 /* short circuit root database access */
2246 if ( be_isroot( op ) ) {
2247 Debug( LDAP_DEBUG_ACL,
2248 "<= acl_access_allowed: granted to database root\n",
2253 /* use backend default access if no backend acls */
2254 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2255 Debug( LDAP_DEBUG_ACL,
2256 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2257 access2str( ACL_WRITE ),
2258 op->o_bd->be_dfltaccess >= ACL_WRITE
2259 ? "granted" : "denied",
2261 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2265 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2267 * Internal mods are ignored by ACL_WRITE checking
2269 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2270 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2271 " modify access granted\n",
2272 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2277 * no-user-modification operational attributes are ignored
2278 * by ACL_WRITE checking as any found here are not provided
2281 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2282 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2283 " modify access granted\n",
2284 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2288 switch ( mlist->sml_op ) {
2289 case LDAP_MOD_REPLACE:
2291 * We must check both permission to delete the whole
2292 * attribute and permission to add the specific attributes.
2293 * This prevents abuse from selfwriters.
2295 if ( ! access_allowed( op, e,
2296 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2302 if ( mlist->sml_values == NULL ) break;
2304 /* fall thru to check value to add */
2307 assert( mlist->sml_values != NULL );
2309 for ( bv = mlist->sml_nvalues
2310 ? mlist->sml_nvalues : mlist->sml_values;
2311 bv->bv_val != NULL; bv++ )
2313 if ( ! access_allowed( op, e,
2314 mlist->sml_desc, bv, ACL_WADD, &state ) )
2322 case LDAP_MOD_DELETE:
2323 if ( mlist->sml_values == NULL ) {
2324 if ( ! access_allowed( op, e,
2325 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2332 for ( bv = mlist->sml_nvalues
2333 ? mlist->sml_nvalues : mlist->sml_values;
2334 bv->bv_val != NULL; bv++ )
2336 if ( ! access_allowed( op, e,
2337 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2345 case SLAP_MOD_SOFTADD:
2346 /* allow adding attribute via modrdn thru */
2358 if (be_null) op->o_bd = NULL;
2364 struct berval *list,
2377 while ( len >= 0 && --ix >= 0 ) {
2378 while ( --len >= 0 && *p++ != sep )
2381 while ( len >= 0 && *p == ' ' ) {
2394 while ( --len >= 0 && *p != sep ) {
2398 while ( bv->bv_len > 0 && *--p == ' ' ) {
2405 typedef struct aci_set_gather_t {
2411 aci_set_cb_gather( Operation *op, SlapReply *rs )
2413 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2415 if ( rs->sr_type == REP_SEARCH ) {
2416 BerValue bvals[ 2 ];
2417 BerVarray bvalsp = NULL;
2420 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2421 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2423 if ( desc == slap_schema.si_ad_entryDN ) {
2425 bvals[ 0 ] = rs->sr_entry->e_nname;
2426 BER_BVZERO( &bvals[ 1 ] );
2431 a = attr_find( rs->sr_entry->e_attrs, desc );
2435 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2438 bvalsp = a->a_nvals;
2444 p->bvals = slap_set_join( p->cookie, p->bvals,
2445 ( '|' | SLAP_SET_RREF ), bvalsp );
2449 assert( rs->sr_type == REP_RESULT );
2456 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2458 AciSetCookie *cp = (AciSetCookie *)cookie;
2460 LDAPURLDesc *ludp = NULL;
2461 Operation op2 = { 0 };
2462 SlapReply rs = {REP_RESULT};
2463 AttributeName anlist[ 2 ], *anlistp = NULL;
2465 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2466 aci_set_gather_t p = { 0 };
2467 const char *text = NULL;
2468 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2470 /* this routine needs to return the bervals instead of
2471 * plain strings, since syntax is not known. It should
2472 * also return the syntax or some "comparison cookie".
2474 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2475 return aci_set_gather2( cookie, name, desc );
2478 rc = ldap_url_parse( name->bv_val, &ludp );
2479 if ( rc != LDAP_URL_SUCCESS ) {
2480 rc = LDAP_PROTOCOL_ERROR;
2484 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2486 /* host part must be empty */
2487 /* extensions parts must be empty */
2488 rc = LDAP_PROTOCOL_ERROR;
2492 /* Grab the searchbase and see if an appropriate database can be found */
2493 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2494 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2495 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2496 BER_BVZERO( &op2.o_req_dn );
2497 if ( rc != LDAP_SUCCESS ) {
2501 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2502 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2503 rc = LDAP_NO_SUCH_OBJECT;
2507 /* Grab the filter */
2508 if ( ludp->lud_filter ) {
2509 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2510 cp->op->o_tmpmemctx );
2513 op2.ors_filterstr = defaultFilter_bv;
2516 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2517 if ( op2.ors_filter == NULL ) {
2518 rc = LDAP_PROTOCOL_ERROR;
2522 /* Grab the scope */
2523 op2.ors_scope = ludp->lud_scope;
2525 /* Grap the attributes */
2526 if ( ludp->lud_attrs ) {
2527 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2530 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2531 cp->op->o_tmpmemctx );
2533 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2534 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2535 anlistp[ nattrs ].an_desc = NULL;
2536 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2537 &anlistp[ nattrs ].an_desc, &text );
2538 if ( rc != LDAP_SUCCESS ) {
2547 anlistp[ nattrs ].an_name = desc->ad_cname;
2548 anlistp[ nattrs ].an_desc = desc;
2550 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2554 op2.o_hdr = cp->op->o_hdr;
2555 op2.o_tag = LDAP_REQ_SEARCH;
2556 op2.o_ndn = op2.o_bd->be_rootndn;
2557 op2.o_callback = &cb;
2558 op2.o_time = slap_get_time();
2559 op2.o_do_not_cache = 1;
2560 op2.o_is_auth_check = 0;
2561 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2562 op2.ors_slimit = SLAP_NO_LIMIT;
2563 op2.ors_tlimit = SLAP_NO_LIMIT;
2564 op2.ors_attrs = anlistp;
2565 op2.ors_attrsonly = 0;
2566 op2.o_private = cp->op->o_private;
2570 rc = op2.o_bd->be_search( &op2, &rs );
2576 if ( op2.ors_filter ) {
2577 filter_free_x( cp->op, op2.ors_filter );
2579 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2580 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2582 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2583 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2586 ldap_free_urldesc( ludp );
2588 if ( anlistp && anlistp != anlist ) {
2589 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2596 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2598 AciSetCookie *cp = (AciSetCookie *)cookie;
2599 BerVarray bvals = NULL;
2603 /* this routine needs to return the bervals instead of
2604 * plain strings, since syntax is not known. It should
2605 * also return the syntax or some "comparison cookie".
2607 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2608 if ( rc == LDAP_SUCCESS ) {
2609 if ( desc == slap_schema.si_ad_entryDN ) {
2610 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2611 cp->op->o_tmpmemctx );
2613 BER_BVZERO( &bvals[ 1 ] );
2617 backend_attribute( cp->op,
2618 cp->e, &ndn, desc, &bvals, ACL_NONE );
2621 if ( !BER_BVISNULL( &ndn ) ) {
2622 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2631 struct berval *subj,
2637 struct berval set = BER_BVNULL;
2639 AciSetCookie cookie;
2641 if ( setref == 0 ) {
2642 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2645 struct berval subjdn, ndn = BER_BVNULL;
2646 struct berval setat;
2649 AttributeDescription *desc = NULL;
2651 /* format of string is "entry/setAttrName" */
2652 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2656 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2657 setat = aci_bv_set_attr;
2661 * NOTE: dnNormalize honors the ber_len field
2662 * as the length of the dn to be normalized
2664 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2665 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2667 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2668 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2672 BER_BVZERO( &bvals[0] );
2673 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2675 bvals[0].bv_val = bvals[i-1].bv_val;
2676 BER_BVZERO( &bvals[i-1] );
2678 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2679 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2684 if ( !BER_BVISNULL( &set ) ) {
2687 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2688 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2689 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2695 #ifdef SLAPD_ACI_ENABLED
2697 aci_list_map_rights(
2698 struct berval *list )
2705 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2708 switch (*bv.bv_val) {
2710 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2713 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2714 * the right 's' to mean "set", but in the examples states
2715 * that the right 's' means "search". The latter definition
2718 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2721 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2724 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2727 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2728 * define any equivalent to the AUTH right, so I've just used
2731 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2743 struct berval *list,
2744 const struct berval *attr,
2745 struct berval *val )
2747 struct berval bv, left, right;
2750 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2751 if (aci_get_part(&bv, 0, '=', &left) < 0
2752 || aci_get_part(&bv, 1, '=', &right) < 0)
2754 if (ber_bvstrcasecmp(attr, &bv) == 0)
2756 } else if (val == NULL) {
2757 if (ber_bvstrcasecmp(attr, &left) == 0)
2760 if (ber_bvstrcasecmp(attr, &left) == 0) {
2761 /* this is experimental code that implements a
2762 * simple (prefix) match of the attribute value.
2763 * the ACI draft does not provide for aci's that
2764 * apply to specific values, but it would be
2765 * nice to have. If the <attr> part of an aci's
2766 * rights list is of the form <attr>=<value>,
2767 * that means the aci applies only to attrs with
2768 * the given value. Furthermore, if the attr is
2769 * of the form <attr>=<value>*, then <value> is
2770 * treated as a prefix, and the aci applies to
2771 * any value with that prefix.
2773 * Ideally, this would allow r.e. matches.
2775 if (aci_get_part(&right, 0, '*', &left) < 0
2776 || right.bv_len <= left.bv_len)
2778 if (ber_bvstrcasecmp(val, &right) == 0)
2780 } else if (val->bv_len >= left.bv_len) {
2781 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2790 static slap_access_t
2791 aci_list_get_attr_rights(
2792 struct berval *list,
2793 const struct berval *attr,
2794 struct berval *val )
2800 /* loop through each rights/attr pair, skip first part (action) */
2802 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2803 if (aci_list_has_attr(&bv, attr, val) == 0)
2805 if (aci_get_part(list, i, ';', &bv) < 0)
2807 mask |= aci_list_map_rights(&bv);
2813 aci_list_get_rights(
2814 struct berval *list,
2815 const struct berval *attr,
2817 slap_access_t *grant,
2818 slap_access_t *deny )
2820 struct berval perm, actn;
2821 slap_access_t *mask;
2824 if (attr == NULL || attr->bv_len == 0
2825 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2826 attr = &aci_bv_br_entry;
2832 /* loop through each permissions clause */
2833 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2834 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2836 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2838 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2845 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2846 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2853 struct berval *subj,
2854 struct berval *defgrpoc,
2855 struct berval *defgrpat,
2862 struct berval subjdn;
2863 struct berval grpoc;
2864 struct berval grpat;
2865 ObjectClass *grp_oc = NULL;
2866 AttributeDescription *grp_ad = NULL;
2870 /* format of string is "group/objectClassValue/groupAttrName" */
2871 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2875 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2879 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2883 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2884 if( rc != LDAP_SUCCESS ) {
2890 grp_oc = oc_bvfind( &grpoc );
2892 if (grp_oc != NULL && grp_ad != NULL ) {
2893 char buf[ACL_BUF_SIZE];
2894 struct berval bv, ndn;
2895 bv.bv_len = sizeof( buf ) - 1;
2896 bv.bv_val = (char *)&buf;
2897 if ( string_expand(&bv, &subjdn,
2898 e->e_ndn, nmatch, matches) )
2903 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2904 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2905 grp_oc, grp_ad ) == 0 );
2906 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2918 AttributeDescription *desc,
2922 regmatch_t *matches,
2923 slap_access_t *grant,
2924 slap_access_t *deny,
2925 slap_aci_scope_t asserted_scope
2928 struct berval bv, scope, perms, type, sdn;
2932 assert( !BER_BVISNULL( &desc->ad_cname ) );
2934 /* parse an aci of the form:
2935 oid # scope # action;rights;attr;rights;attr
2936 $ action;rights;attr;rights;attr # type # subject
2938 [NOTE: the following comment is very outdated,
2939 as the draft version it refers to (Ando, 2004-11-20)].
2941 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2942 a full description of the format for this attribute.
2943 Differences: "this" in the draft is "self" here, and
2944 "self" and "public" is in the position of type.
2946 <scope> = {entry|children|subtree}
2947 <type> = {public|users|access-id|subtree|onelevel|children|
2948 self|dnattr|group|role|set|set-ref}
2950 This routine now supports scope={ENTRY,CHILDREN}
2952 - ENTRY applies to "entry" and "subtree";
2953 - CHILDREN aplies to "children" and "subtree"
2956 /* check that the aci has all 5 components */
2957 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2961 /* check that the aci family is supported */
2962 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2966 /* check that the scope matches */
2967 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2971 /* note: scope can be either ENTRY or CHILDREN;
2972 * they respectively match "entry" and "children" in bv
2973 * both match "subtree" */
2974 switch ( asserted_scope ) {
2975 case SLAP_ACI_SCOPE_ENTRY:
2976 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2977 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2983 case SLAP_ACI_SCOPE_CHILDREN:
2984 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2985 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2995 /* get the list of permissions clauses, bail if empty */
2996 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
3000 /* check if any permissions allow desired access */
3001 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
3005 /* see if we have a DN match */
3006 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
3010 /* see if we have a public (i.e. anonymous) access */
3011 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
3015 /* otherwise require an identity */
3016 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3020 /* see if we have a users access */
3021 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3025 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3026 * just grab all the berval up to its end (ITS#3303).
3027 * NOTE: the problem could be solved by providing the DN with
3028 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3029 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3031 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3035 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3036 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3038 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3041 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3042 if ( rc != LDAP_SUCCESS ) {
3046 if ( dn_match( &op->o_ndn, &ndn ) ) {
3049 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3053 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3056 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3057 if ( rc != LDAP_SUCCESS ) {
3061 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3064 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3068 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3069 struct berval ndn, pndn;
3071 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3072 if ( rc != LDAP_SUCCESS ) {
3076 dnParent( &ndn, &pndn );
3078 if ( dn_match( &op->o_ndn, &pndn ) ) {
3081 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3085 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3088 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3089 if ( rc != LDAP_SUCCESS ) {
3093 if ( !dn_match( &op->o_ndn, &ndn )
3094 && dnIsSuffix( &op->o_ndn, &ndn ) )
3098 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3102 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3103 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3107 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3109 AttributeDescription *ad = NULL;
3112 rc = slap_bv2ad( &sdn, &ad, &text );
3114 if( rc != LDAP_SUCCESS ) {
3120 for ( at = attrs_find( e->e_attrs, ad );
3122 at = attrs_find( at->a_next, ad ) )
3124 if ( value_find_ex( ad,
3125 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3126 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3128 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3137 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3138 if ( aci_group_member( &sdn, &aci_bv_group_class,
3139 &aci_bv_group_attr, op, e, nmatch, matches ) )
3144 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3145 if ( aci_group_member( &sdn, &aci_bv_role_class,
3146 &aci_bv_role_attr, op, e, nmatch, matches ) )
3151 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3152 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3156 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3157 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3167 * FIXME: there is a silly dependence that makes it difficult
3168 * to move ACIs in a run-time loadable module under the "dynacl"
3169 * umbrella, because sets share some helpers with ACIs.
3172 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3174 AttributeDescription *ad = NULL;
3175 const char *text = NULL;
3177 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3178 fprintf( stderr, "%s: line %d: "
3179 "inappropriate style \"%s\" in \"aci\" by clause\n",
3180 fname, lineno, style_strings[sty] );
3184 if ( right != NULL && *right != '\0' ) {
3185 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3187 "%s: line %d: aci \"%s\": %s\n",
3188 fname, lineno, right, text );
3193 ad = slap_schema.si_ad_aci;
3196 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3197 fprintf( stderr, "%s: line %d: "
3198 "aci \"%s\": inappropriate syntax: %s\n",
3199 fname, lineno, right,
3200 ad->ad_type->sat_syntax_oid );
3204 *privp = (void *)ad;
3210 dynacl_aci_unparse( void *priv, struct berval *bv )
3212 AttributeDescription *ad = ( AttributeDescription * )priv;
3215 assert( ad != NULL );
3217 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3218 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3219 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3220 bv->bv_len = ptr - bv->bv_val;
3231 AttributeDescription *desc,
3234 regmatch_t *matches,
3235 slap_access_t *grantp,
3236 slap_access_t *denyp )
3238 AttributeDescription *ad = ( AttributeDescription * )priv;
3240 slap_access_t tgrant, tdeny, grant, deny;
3242 char accessmaskbuf[ACCESSMASK_MAXLEN];
3243 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3244 #endif /* LDAP_DEBUG */
3246 /* start out with nothing granted, nothing denied */
3250 /* get the aci attribute */
3251 at = attr_find( e->e_attrs, ad );
3255 /* the aci is an multi-valued attribute. The
3256 * rights are determined by OR'ing the individual
3257 * rights given by the acis.
3259 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3260 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3261 nmatch, matches, &grant, &deny,
3262 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3269 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3270 accessmask2str( tgrant, accessmaskbuf, 1 ),
3271 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3274 /* If the entry level aci didn't contain anything valid for the
3275 * current operation, climb up the tree and evaluate the
3276 * acis with scope set to subtree
3278 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3279 struct berval parent_ndn;
3282 /* to solve the chicken'n'egg problem of accessing
3283 * the OpenLDAPaci attribute, the direct access
3284 * to the entry's attribute is unchecked; however,
3285 * further accesses to OpenLDAPaci values in the
3286 * ancestors occur through backend_attribute(), i.e.
3287 * with the identity of the operation, requiring
3288 * further access checking. For uniformity, this
3289 * makes further requests occur as the rootdn, if
3290 * any, i.e. searching for the OpenLDAPaci attribute
3291 * is considered an internal search. If this is not
3292 * acceptable, then the same check needs be performed
3293 * when accessing the entry's attribute. */
3294 Operation op2 = *op;
3296 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3297 op2.o_dn = op->o_bd->be_rootdn;
3298 op2.o_ndn = op->o_bd->be_rootndn;
3302 dnParent( &e->e_nname, &parent_ndn );
3303 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3305 BerVarray bvals = NULL;
3308 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3309 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3318 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3319 if ( aci_mask( op, e, desc, val,
3323 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3327 /* evaluation stops as soon as either a "deny" or a
3328 * "grant" directive matches.
3330 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3334 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3335 accessmask2str( tgrant, accessmaskbuf, 1 ),
3336 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3340 case LDAP_NO_SUCH_ATTRIBUTE:
3341 /* just go on if the aci-Attribute is not present in
3344 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3348 case LDAP_NO_SUCH_OBJECT:
3349 /* We have reached the base object */
3350 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3362 dnParent( &parent_ndn, &parent_ndn );
3372 /* need to register this at some point */
3373 static slap_dynacl_t dynacl_aci = {
3383 #endif /* SLAP_DYNACL */
3385 #endif /* SLAPD_ACI_ENABLED */
3390 * dynamic ACL infrastructure
3392 static slap_dynacl_t *da_list = NULL;
3395 slap_dynacl_register( slap_dynacl_t *da )
3399 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3400 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3405 if ( tmp != NULL ) {
3409 if ( da->da_mask == NULL ) {
3413 da->da_private = NULL;
3414 da->da_next = da_list;
3420 static slap_dynacl_t *
3421 slap_dynacl_next( slap_dynacl_t *da )
3430 slap_dynacl_get( const char *name )
3434 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3435 if ( strcasecmp( da->da_name, name ) == 0 ) {
3442 #endif /* SLAP_DYNACL */
3449 slap_dynacl_t *known_dynacl[] = {
3450 #ifdef SLAPD_ACI_ENABLED
3452 #endif /* SLAPD_ACI_ENABLED */
3456 for ( i = 0; known_dynacl[ i ]; i++ ) {
3457 rc = slap_dynacl_register( known_dynacl[ i ] );
3462 #endif /* SLAP_DYNACL */
3473 regmatch_t *matches)
3481 bv->bv_val[0] = '\0';
3482 bv->bv_len--; /* leave space for lone $ */
3485 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3486 sp < pat->bv_val + pat->bv_len ; sp++ )
3488 /* did we previously see a $ */
3490 if ( flag == 1 && *sp == '$' ) {
3495 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3498 } else if ( *sp >= '0' && *sp <= '9' ) {
3506 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3507 if ( *sp >= '0' && *sp <= '9' ) {
3508 n = 10*n + ( *sp - '0' );
3512 if ( *sp != /*'{'*/ '}' ) {
3518 if ( n >= nmatch ) {
3524 i = matches[n].rm_so;
3525 l = matches[n].rm_eo;
3526 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3544 /* must have ended with a single $ */
3552 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3553 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3560 struct berval *pat, /* pattern to expand and match against */
3561 char *str, /* string to match against pattern */
3562 char *buf, /* buffer with $N expansion variables */
3563 int nmatch, /* size of the matches array */
3564 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3568 char newbuf[ACL_BUF_SIZE];
3572 bv.bv_len = sizeof( newbuf ) - 1;
3579 string_expand( &bv, pat, buf, nmatch, matches );
3580 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3582 char error[ACL_BUF_SIZE];
3583 regerror( rc, &re, error, sizeof( error ) );
3585 Debug( LDAP_DEBUG_TRACE,
3586 "compile( \"%s\", \"%s\") failed %s\n",
3587 pat->bv_val, str, error );
3591 rc = regexec( &re, str, 0, NULL, 0 );
3594 Debug( LDAP_DEBUG_TRACE,
3595 "=> regex_matches: string: %s\n", str, 0, 0 );
3596 Debug( LDAP_DEBUG_TRACE,
3597 "=> regex_matches: rc: %d %s\n",
3598 rc, !rc ? "matches" : "no matches", 0 );