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;
364 assert( desc != NULL );
366 access_level = ACL_LEVEL( access );
368 assert( access_level > ACL_NONE );
371 if ( maskp ) ACL_INVALIDATE( *maskp );
373 attr = desc->ad_cname.bv_val;
375 assert( attr != NULL );
377 if ( op && op->o_is_auth_check &&
378 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
384 if ( state->as_vd_ad == desc ) {
385 if ( state->as_recorded ) {
386 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
389 return state->as_result;
391 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
392 val != NULL && state->as_vd_acl == NULL )
394 return state->as_result;
402 state->as_vd_ad = desc;
405 Debug( LDAP_DEBUG_ACL,
406 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
407 access2str( access ), e->e_dn, attr );
414 if ( op->o_bd == NULL ) {
415 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
420 * FIXME: experimental; use first backend rules
421 * iff there is no global_acl (ITS#3100) */
422 if ( frontendDB->be_acl != NULL ) {
423 op->o_bd = frontendDB;
425 #endif /* LDAP_DEVEL */
427 assert( op->o_bd != NULL );
429 /* this is enforced in backend_add() */
430 if ( op->o_bd->bd_info->bi_access_allowed ) {
431 /* delegate to backend */
432 ret = op->o_bd->bd_info->bi_access_allowed( op, e, desc, val, access, state, &mask );
436 ret = slap_access_allowed( op, e, desc, val, access, state, &mask );
440 if ( ACL_IS_INVALID( mask ) ) {
441 Debug( LDAP_DEBUG_ACL,
442 "=> access_allowed: \"%s\" (%s) invalid!\n",
446 } else if ( control == ACL_BREAK ) {
447 Debug( LDAP_DEBUG_ACL,
448 "=> access_allowed: no more rules\n", 0, 0, 0 );
453 ret = ACL_GRANT( mask, access );
456 Debug( LDAP_DEBUG_ACL,
457 "=> access_allowed: %s access %s by %s\n",
458 access2str( access ), ret ? "granted" : "denied",
459 accessmask2str( mask, accessmaskbuf, 1 ) );
462 if ( state != NULL ) {
463 /* If not value-dependent, save ACL in case of more attrs */
464 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
465 state->as_vi_acl = a;
466 state->as_result = ret;
468 state->as_recorded |= ACL_STATE_RECORDED;
470 if ( be_null ) op->o_bd = NULL;
471 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
475 #else /* !SLAP_OVERLAY_ACCESS */
481 AttributeDescription *desc,
483 slap_access_t access,
484 AccessControlState *state,
489 AccessControl *a = NULL;
494 char accessmaskbuf[ACCESSMASK_MAXLEN];
497 slap_control_t control;
498 slap_access_t access_level;
500 regmatch_t matches[MAXREMATCHES];
501 int st_same_attr = 0;
502 static AccessControlState state_init = ACL_STATE_INIT;
505 assert( desc != NULL );
507 access_level = ACL_LEVEL( access );
509 assert( access_level > ACL_NONE );
510 if ( maskp ) ACL_INVALIDATE( *maskp );
512 attr = desc->ad_cname.bv_val;
514 assert( attr != NULL );
516 if ( op && op->o_is_auth_check &&
517 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
523 if ( state->as_vd_ad == desc ) {
524 if ( state->as_recorded ) {
525 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
528 return state->as_result;
530 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
531 val != NULL && state->as_vd_acl == NULL )
533 return state->as_result;
541 state->as_vd_ad=desc;
544 Debug( LDAP_DEBUG_ACL,
545 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
546 access2str( access ), e->e_dn, attr );
555 be = LDAP_STAILQ_FIRST(&backendDB);
559 * FIXME: experimental; use first backend rules
560 * iff there is no global_acl (ITS#3100) */
561 if ( frontendDB->be_acl == NULL )
567 assert( be != NULL );
569 /* grant database root access */
570 if ( be_isroot( op ) ) {
571 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
573 mask = ACL_LVL_MANAGE;
580 * no-user-modification operational attributes are ignored
581 * by ACL_WRITE checking as any found here are not provided
584 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
585 && desc != slap_schema.si_ad_entry
586 && desc != slap_schema.si_ad_children )
588 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
589 " %s access granted\n",
594 /* use backend default access if no backend acls */
595 if ( be->be_acl == NULL ) {
596 Debug( LDAP_DEBUG_ACL,
597 "=> access_allowed: backend default %s "
598 "access %s to \"%s\"\n",
599 access2str( access ),
600 be->be_dfltaccess >= access_level ? "granted" : "denied",
601 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
602 ret = be->be_dfltaccess >= access_level;
607 mask = ACL_PRIV_LEVEL;
608 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
609 mask |= ACL_ACCESS2PRIV( i );
616 /* be is always non-NULL */
617 /* use global default access if no global acls */
618 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
619 Debug( LDAP_DEBUG_ACL,
620 "=> access_allowed: global default %s access %s to \"%s\"\n",
621 access2str( access ),
622 frontendDB->be_dfltaccess >= access_level ?
623 "granted" : "denied", op->o_dn.bv_val );
624 ret = frontendDB->be_dfltaccess >= access_level;
629 mask = ACL_PRIV_LEVEL;
630 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
631 mask |= ACL_ACCESS2PRIV( i );
642 if ( st_same_attr ) {
643 assert( state->as_vd_acl != NULL );
645 a = state->as_vd_acl;
646 count = state->as_vd_acl_count;
647 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
648 mask = state->as_vd_acl_mask;
649 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
654 if ( state ) state->as_vi_acl = NULL;
658 memset( matches, '\0', sizeof(matches) );
661 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
662 MAXREMATCHES, matches, state ) ) != NULL )
666 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
667 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
668 (int)matches[i].rm_so, (int)matches[i].rm_eo );
669 if ( matches[i].rm_so <= matches[0].rm_eo ) {
671 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
672 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
675 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
679 if ( state->as_vi_acl == a &&
680 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
682 Debug( LDAP_DEBUG_ACL,
683 "access_allowed: result from state (%s)\n",
685 ret = state->as_result;
688 Debug( LDAP_DEBUG_ACL,
689 "access_allowed: no res from state (%s)\n",
695 control = slap_acl_mask( a, &mask, op,
696 e, desc, val, MAXREMATCHES, matches, count, state );
698 if ( control != ACL_BREAK ) {
702 memset( matches, '\0', sizeof(matches) );
705 if ( ACL_IS_INVALID( mask ) ) {
706 Debug( LDAP_DEBUG_ACL,
707 "=> access_allowed: \"%s\" (%s) invalid!\n",
711 } else if ( control == ACL_BREAK ) {
712 Debug( LDAP_DEBUG_ACL,
713 "=> access_allowed: no more rules\n", 0, 0, 0 );
718 Debug( LDAP_DEBUG_ACL,
719 "=> access_allowed: %s access %s by %s\n",
720 access2str( access ),
721 ACL_GRANT(mask, access) ? "granted" : "denied",
722 accessmask2str( mask, accessmaskbuf, 1 ) );
724 ret = ACL_GRANT(mask, access);
727 if ( state != NULL ) {
728 /* If not value-dependent, save ACL in case of more attrs */
729 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
730 state->as_vi_acl = a;
731 state->as_result = ret;
733 state->as_recorded |= ACL_STATE_RECORDED;
735 if ( be_null ) op->o_bd = NULL;
736 if ( maskp ) *maskp = mask;
740 #endif /* SLAP_OVERLAY_ACCESS */
743 * slap_acl_get - return the acl applicable to entry e, attribute
744 * attr. the acl returned is suitable for use in subsequent calls to
745 * acl_access_allowed().
748 static AccessControl *
754 AttributeDescription *desc,
758 AccessControlState *state )
765 assert( count != NULL );
766 assert( desc != NULL );
768 attr = desc->ad_cname.bv_val;
770 assert( attr != NULL );
773 if( op->o_bd == NULL ) {
774 a = frontendDB->be_acl;
776 a = op->o_bd->be_acl;
787 dnlen = e->e_nname.bv_len;
789 for ( ; a != NULL; a = a->acl_next ) {
792 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
793 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
794 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
795 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
796 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
800 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
801 *count, a->acl_dn_pat.bv_val, 0 );
802 patlen = a->acl_dn_pat.bv_len;
803 if ( dnlen < patlen )
806 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
807 /* base dn -- entire object DN must match */
808 if ( dnlen != patlen )
811 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
812 int rdnlen = -1, sep = 0;
814 if ( dnlen <= patlen )
818 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
823 rdnlen = dn_rdnlen( NULL, &e->e_nname );
824 if ( rdnlen != dnlen - patlen - sep )
827 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
828 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
831 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
832 if ( dnlen <= patlen )
834 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
838 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
842 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
846 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
847 matches[0].rm_so = matches[0].rm_eo = -1;
851 /* Is this ACL only for a specific value? */
852 if ( a->acl_attrval.bv_len ) {
857 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
858 state->as_recorded |= ACL_STATE_RECORDED_VD;
859 state->as_vd_acl = a;
860 state->as_vd_acl_count = *count;
861 state->as_vd_access = a->acl_access;
862 state->as_vd_access_count = 1;
863 ACL_INVALIDATE( state->as_vd_acl_mask );
866 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
867 Debug( LDAP_DEBUG_ACL,
868 "acl_get: valpat %s\n",
869 a->acl_attrval.bv_val, 0, 0 );
870 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
878 Debug( LDAP_DEBUG_ACL,
880 a->acl_attrval.bv_val, 0, 0 );
882 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
883 if (value_match( &match, desc,
884 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
885 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
892 patlen = a->acl_attrval.bv_len;
893 vdnlen = val->bv_len;
895 if ( vdnlen < patlen )
898 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
899 if ( vdnlen > patlen )
902 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
905 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
908 rdnlen = dn_rdnlen( NULL, val );
909 if ( rdnlen != vdnlen - patlen - 1 )
912 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
913 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
916 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
917 if ( vdnlen <= patlen )
920 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
924 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
930 if ( a->acl_filter != NULL ) {
931 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
932 if ( rc != LDAP_COMPARE_TRUE ) {
937 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
942 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
954 struct berval *opndn )
957 * if access applies to the entry itself, and the
958 * user is bound as somebody in the same namespace as
959 * the entry, OR the given dn matches the dn pattern
962 * NOTE: styles "anonymous", "users" and "self"
963 * have been moved to enum slap_style_t, whose
964 * value is set in a_dn_style; however, the string
965 * is maintaned in a_dn_pat.
967 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
968 if ( !BER_BVISEMPTY( opndn ) ) {
972 } else if ( b->a_style == ACL_STYLE_USERS ) {
973 if ( BER_BVISEMPTY( opndn ) ) {
977 } else if ( b->a_style == ACL_STYLE_SELF ) {
978 struct berval ndn, selfndn;
981 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
985 level = b->a_self_level;
993 selfndn = e->e_nname;
996 for ( ; level > 0; level-- ) {
997 if ( BER_BVISEMPTY( &ndn ) ) {
1000 dnParent( &ndn, &ndn );
1003 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1008 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1009 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1011 regmatch_t tmp_matches[2],
1012 *tmp_matchesp = tmp_matches;
1016 switch ( a->acl_dn_style ) {
1017 case ACL_STYLE_REGEX:
1018 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1019 tmp_matchesp = matches;
1020 tmp_nmatch = nmatch;
1023 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1025 case ACL_STYLE_BASE:
1026 tmp_matches[0].rm_so = 0;
1027 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1032 case ACL_STYLE_SUBTREE:
1033 case ACL_STYLE_CHILDREN:
1034 tmp_matches[0].rm_so = 0;
1035 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1036 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1037 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1051 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1052 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1060 ber_len_t patlen, odnlen;
1063 if ( e->e_dn == NULL )
1066 if ( b->a_expand ) {
1068 char buf[ACL_BUF_SIZE];
1071 regmatch_t tmp_matches[2],
1072 *tmp_matchesp = tmp_matches;
1076 bv.bv_len = sizeof( buf ) - 1;
1079 switch ( a->acl_dn_style ) {
1080 case ACL_STYLE_REGEX:
1081 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1082 tmp_matchesp = matches;
1083 tmp_nmatch = nmatch;
1086 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1088 case ACL_STYLE_BASE:
1089 tmp_matches[0].rm_so = 0;
1090 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1095 case ACL_STYLE_SUBTREE:
1096 case ACL_STYLE_CHILDREN:
1097 tmp_matches[0].rm_so = 0;
1098 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1099 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1100 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1114 if ( string_expand( &bv, &b->a_pat,
1116 tmp_nmatch, tmp_matchesp ) )
1121 if ( dnNormalize(0, NULL, NULL, &bv,
1122 &pat, op->o_tmpmemctx )
1125 /* did not expand to a valid dn */
1133 patlen = pat.bv_len;
1134 odnlen = opndn->bv_len;
1135 if ( odnlen < patlen ) {
1136 goto dn_match_cleanup;
1140 if ( b->a_style == ACL_STYLE_BASE ) {
1141 /* base dn -- entire object DN must match */
1142 if ( odnlen != patlen ) {
1143 goto dn_match_cleanup;
1146 } else if ( b->a_style == ACL_STYLE_ONE ) {
1149 if ( odnlen <= patlen ) {
1150 goto dn_match_cleanup;
1153 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1154 goto dn_match_cleanup;
1157 rdnlen = dn_rdnlen( NULL, opndn );
1158 if ( rdnlen != odnlen - patlen - 1 ) {
1159 goto dn_match_cleanup;
1162 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1163 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1164 goto dn_match_cleanup;
1167 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1168 if ( odnlen <= patlen ) {
1169 goto dn_match_cleanup;
1172 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1173 goto dn_match_cleanup;
1176 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1180 if ( odnlen <= patlen ) {
1181 goto dn_match_cleanup;
1184 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1186 goto dn_match_cleanup;
1191 for ( ; level > 0; level-- ) {
1192 if ( BER_BVISEMPTY( &ndn ) ) {
1193 goto dn_match_cleanup;
1195 dnParent( &ndn, &ndn );
1196 if ( ndn.bv_len < patlen ) {
1197 goto dn_match_cleanup;
1201 if ( ndn.bv_len != patlen ) {
1202 goto dn_match_cleanup;
1206 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1209 if ( pat.bv_val != b->a_pat.bv_val ) {
1210 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1222 * Record value-dependent access control state
1224 #define ACL_RECORD_VALUE_STATE do { \
1225 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1226 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1227 state->as_vd_acl = a; \
1228 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1229 sizeof( state->as_vd_acl_matches )) ; \
1230 state->as_vd_acl_count = count; \
1231 state->as_vd_access = b; \
1232 state->as_vd_access_count = i; \
1244 regmatch_t *matches,
1246 AccessControlState *state,
1247 slap_dn_access *bdn,
1248 struct berval *opndn )
1254 const char *attr = bdn->a_at->ad_cname.bv_val;
1256 assert( attr != NULL );
1258 if ( BER_BVISEMPTY( opndn ) ) {
1262 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1265 /* see if asker is listed in dnattr */
1266 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1268 at = attrs_find( at->a_next, bdn->a_at ) )
1270 if ( value_find_ex( bdn->a_at,
1271 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1272 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1274 &bv, op->o_tmpmemctx ) == 0 )
1283 /* have a dnattr match. if this is a self clause then
1284 * the target must also match the op dn.
1286 if ( bdn->a_self ) {
1287 /* check if the target is an attribute. */
1288 if ( val == NULL ) return 1;
1290 /* target is attribute, check if the attribute value
1293 rc = value_match( &match, bdn->a_at,
1294 bdn->a_at->ad_type->sat_equality, 0,
1296 /* on match error or no match, fail the ACL clause */
1297 if ( rc != LDAP_SUCCESS || match != 0 )
1302 /* no dnattr match, check if this is a self clause */
1303 if ( ! bdn->a_self )
1306 ACL_RECORD_VALUE_STATE;
1308 /* this is a self clause, check if the target is an
1314 /* target is attribute, check if the attribute value
1317 rc = value_match( &match, bdn->a_at,
1318 bdn->a_at->ad_type->sat_equality, 0,
1321 /* on match error or no match, fail the ACL clause */
1322 if ( rc != LDAP_SUCCESS || match != 0 )
1331 * slap_acl_mask - modifies mask based upon the given acl and the
1332 * requested access to entry e, attribute attr, value val. if val
1333 * is null, access to the whole attribute is assumed (all values).
1335 * returns 0 access NOT allowed
1339 static slap_control_t
1345 AttributeDescription *desc,
1348 regmatch_t *matches,
1350 AccessControlState *state )
1355 char accessmaskbuf[ACCESSMASK_MAXLEN];
1356 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1357 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1358 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1362 assert( a != NULL );
1363 assert( mask != NULL );
1364 assert( desc != NULL );
1366 attr = desc->ad_cname.bv_val;
1368 assert( attr != NULL );
1370 Debug( LDAP_DEBUG_ACL,
1371 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1374 Debug( LDAP_DEBUG_ACL,
1375 "=> acl_mask: to %s by \"%s\", (%s) \n",
1376 val ? "value" : "all values",
1377 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1378 accessmask2str( *mask, accessmaskbuf, 1 ) );
1381 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1382 && state->as_vd_acl == a )
1384 b = state->as_vd_access;
1385 i = state->as_vd_access_count;
1392 for ( ; b != NULL; b = b->a_next, i++ ) {
1393 slap_mask_t oldmask, modmask;
1395 ACL_INVALIDATE( modmask );
1397 /* AND <who> clauses */
1398 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1399 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1400 b->a_dn_pat.bv_val, 0, 0);
1402 * if access applies to the entry itself, and the
1403 * user is bound as somebody in the same namespace as
1404 * the entry, OR the given dn matches the dn pattern
1407 * NOTE: styles "anonymous", "users" and "self"
1408 * have been moved to enum slap_style_t, whose
1409 * value is set in a_dn_style; however, the string
1410 * is maintaned in a_dn_pat.
1413 if ( acl_mask_dn( op, e, a, nmatch, matches,
1414 &b->a_dn, &op->o_ndn ) )
1420 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1423 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1424 b->a_realdn_pat.bv_val, 0, 0);
1426 * if access applies to the entry itself, and the
1427 * user is bound as somebody in the same namespace as
1428 * the entry, OR the given dn matches the dn pattern
1431 * NOTE: styles "anonymous", "users" and "self"
1432 * have been moved to enum slap_style_t, whose
1433 * value is set in a_dn_style; however, the string
1434 * is maintaned in a_dn_pat.
1437 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1439 ndn = op->o_conn->c_ndn;
1444 if ( acl_mask_dn( op, e, a, nmatch, matches,
1445 &b->a_realdn, &ndn ) )
1451 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1452 if ( ! op->o_conn->c_listener ) {
1455 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1456 b->a_sockurl_pat.bv_val, 0, 0 );
1458 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1459 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1460 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1461 e->e_ndn, nmatch, matches ) )
1466 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1468 char buf[ACL_BUF_SIZE];
1470 bv.bv_len = sizeof( buf ) - 1;
1472 if ( string_expand( &bv, &b->a_sockurl_pat,
1473 e->e_ndn, nmatch, matches ) )
1478 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1484 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1492 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1493 if ( !op->o_conn->c_peer_domain.bv_val ) {
1496 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1497 b->a_domain_pat.bv_val, 0, 0 );
1498 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1499 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1500 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1501 e->e_ndn, nmatch, matches ) )
1506 char buf[ACL_BUF_SIZE];
1508 struct berval cmp = op->o_conn->c_peer_domain;
1509 struct berval pat = b->a_domain_pat;
1511 if ( b->a_domain_expand ) {
1514 bv.bv_len = sizeof(buf) - 1;
1517 if ( string_expand(&bv, &b->a_domain_pat,
1518 e->e_ndn, nmatch, matches) )
1525 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1526 int offset = cmp.bv_len - pat.bv_len;
1531 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1535 /* trim the domain */
1536 cmp.bv_val = &cmp.bv_val[ offset ];
1537 cmp.bv_len -= offset;
1540 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1547 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1548 if ( !op->o_conn->c_peer_name.bv_val ) {
1551 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1552 b->a_peername_pat.bv_val, 0, 0 );
1553 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1554 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1555 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1556 e->e_ndn, nmatch, matches ) )
1562 /* try exact match */
1563 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1564 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1568 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1570 char buf[ACL_BUF_SIZE];
1572 bv.bv_len = sizeof( buf ) - 1;
1574 if ( string_expand( &bv, &b->a_peername_pat,
1575 e->e_ndn, nmatch, matches ) )
1580 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1584 /* extract IP and try exact match */
1585 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1587 char buf[] = "255.255.255.255";
1590 int port_number = -1;
1592 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1593 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1596 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1597 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1599 port = strrchr( ip.bv_val, ':' );
1603 ip.bv_len = port - ip.bv_val;
1605 port_number = strtol( port, &next, 10 );
1606 if ( next[0] != '\0' )
1610 /* the port check can be anticipated here */
1611 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1614 /* address longer than expected? */
1615 if ( ip.bv_len >= sizeof(buf) )
1618 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1619 buf[ ip.bv_len ] = '\0';
1621 addr = inet_addr( buf );
1623 /* unable to convert? */
1624 if ( addr == (unsigned long)(-1) )
1627 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1630 #ifdef LDAP_PF_LOCAL
1631 /* extract path and try exact match */
1632 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1635 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1636 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1639 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1640 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1642 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1645 #endif /* LDAP_PF_LOCAL */
1647 /* exact match (very unlikely...) */
1648 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1655 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1656 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1659 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1660 b->a_sockname_pat.bv_val, 0, 0 );
1661 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1662 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1663 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1664 e->e_ndn, nmatch, matches ) )
1669 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1671 char buf[ACL_BUF_SIZE];
1673 bv.bv_len = sizeof( buf ) - 1;
1675 if ( string_expand( &bv, &b->a_sockname_pat,
1676 e->e_ndn, nmatch, matches ) )
1681 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1686 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1693 if ( b->a_dn_at != NULL ) {
1694 if ( acl_mask_dnattr( op, e, val, a, b, i,
1695 matches, count, state,
1696 &b->a_dn, &op->o_ndn ) )
1702 if ( b->a_realdn_at != NULL ) {
1705 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1707 ndn = op->o_conn->c_ndn;
1712 if ( acl_mask_dnattr( op, e, val, a, b, i,
1713 matches, count, state,
1714 &b->a_realdn, &ndn ) )
1720 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1722 struct berval ndn = BER_BVNULL;
1725 if ( op->o_ndn.bv_len == 0 ) {
1729 /* b->a_group is an unexpanded entry name, expanded it should be an
1730 * entry with objectclass group* and we test to see if odn is one of
1731 * the values in the attribute group
1733 /* see if asker is listed in dnattr */
1734 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1735 char buf[ACL_BUF_SIZE];
1737 regmatch_t tmp_matches[2],
1738 *tmp_matchesp = tmp_matches;
1740 bv.bv_len = sizeof(buf) - 1;
1745 switch ( a->acl_dn_style ) {
1746 case ACL_STYLE_REGEX:
1747 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1748 tmp_matchesp = matches;
1749 tmp_nmatch = nmatch;
1753 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1754 case ACL_STYLE_BASE:
1755 tmp_matches[0].rm_so = 0;
1756 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1761 case ACL_STYLE_SUBTREE:
1762 case ACL_STYLE_CHILDREN:
1763 tmp_matches[0].rm_so = 0;
1764 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1765 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1766 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1780 if ( string_expand( &bv, &b->a_group_pat,
1782 tmp_nmatch, tmp_matchesp ) )
1787 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1788 op->o_tmpmemctx ) != LDAP_SUCCESS )
1790 /* did not expand to a valid dn */
1797 bv = b->a_group_pat;
1800 rc = backend_group( op, e, &bv, &op->o_ndn,
1801 b->a_group_oc, b->a_group_at );
1804 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1812 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1814 char buf[ACL_BUF_SIZE];
1816 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1818 regmatch_t tmp_matches[2],
1819 *tmp_matchesp = tmp_matches;
1822 bv.bv_len = sizeof( buf ) - 1;
1827 switch ( a->acl_dn_style ) {
1828 case ACL_STYLE_REGEX:
1829 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1830 tmp_matchesp = matches;
1831 tmp_nmatch = nmatch;
1835 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1836 case ACL_STYLE_BASE:
1837 tmp_matches[0].rm_so = 0;
1838 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1843 case ACL_STYLE_SUBTREE:
1844 case ACL_STYLE_CHILDREN:
1845 tmp_matches[0].rm_so = 0;
1846 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1847 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1848 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1862 if ( string_expand( &bv, &b->a_set_pat,
1864 tmp_nmatch, tmp_matchesp ) )
1873 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1878 if ( b->a_authz.sai_ssf ) {
1879 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1880 b->a_authz.sai_ssf, op->o_ssf, 0 );
1881 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1886 if ( b->a_authz.sai_transport_ssf ) {
1887 Debug( LDAP_DEBUG_ACL,
1888 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1889 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1890 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1895 if ( b->a_authz.sai_tls_ssf ) {
1896 Debug( LDAP_DEBUG_ACL,
1897 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1898 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1899 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1904 if ( b->a_authz.sai_sasl_ssf ) {
1905 Debug( LDAP_DEBUG_ACL,
1906 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1907 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1908 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1914 if ( b->a_dynacl ) {
1916 slap_access_t tgrant, tdeny;
1918 /* this case works different from the others above.
1919 * since aci's themselves give permissions, we need
1920 * to first check b->a_access_mask, the ACL's access level.
1922 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1923 /* no ACIs in the root DSE */
1927 /* first check if the right being requested
1928 * is allowed by the ACL clause.
1930 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1934 /* start out with nothing granted, nothing denied */
1938 for ( da = b->a_dynacl; da; da = da->da_next ) {
1939 slap_access_t grant, deny;
1941 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1947 /* remove anything that the ACL clause does not allow */
1948 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1949 tdeny &= ACL_PRIV_MASK;
1951 /* see if we have anything to contribute */
1952 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1956 /* this could be improved by changing slap_acl_mask so that it can deal with
1957 * by clauses that return grant/deny pairs. Right now, it does either
1958 * additive or subtractive rights, but not both at the same time. So,
1959 * we need to combine the grant/deny pair into a single rights mask in
1960 * a smart way: if either grant or deny is "empty", then we use the
1961 * opposite as is, otherwise we remove any denied rights from the grant
1962 * rights mask and construct an additive mask.
1964 if (ACL_IS_INVALID(tdeny)) {
1965 modmask = tgrant | ACL_PRIV_ADDITIVE;
1967 } else if (ACL_IS_INVALID(tgrant)) {
1968 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1971 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1975 #else /* !SLAP_DYNACL */
1977 #ifdef SLAPD_ACI_ENABLED
1978 if ( b->a_aci_at != NULL ) {
1980 slap_access_t grant, deny, tgrant, tdeny;
1981 struct berval parent_ndn;
1982 BerVarray bvals = NULL;
1985 /* this case works different from the others above.
1986 * since aci's themselves give permissions, we need
1987 * to first check b->a_access_mask, the ACL's access level.
1990 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1991 /* no ACIs in the root DSE */
1995 /* first check if the right being requested
1996 * is allowed by the ACL clause.
1998 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2001 /* start out with nothing granted, nothing denied */
2005 /* get the aci attribute */
2006 at = attr_find( e->e_attrs, b->a_aci_at );
2009 /* FIXME: this breaks acl caching;
2010 * see also ACL_RECORD_VALUE_STATE below */
2011 ACL_RECORD_VALUE_STATE;
2013 /* the aci is an multi-valued attribute. The
2014 * rights are determined by OR'ing the individual
2015 * rights given by the acis.
2017 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2022 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2028 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2029 accessmask2str(tgrant, accessmaskbuf, 1),
2030 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2033 /* If the entry level aci didn't contain anything valid for the
2034 * current operation, climb up the tree and evaluate the
2035 * acis with scope set to subtree
2037 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2038 dnParent( &e->e_nname, &parent_ndn );
2039 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2040 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2041 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2049 for( i = 0; bvals[i].bv_val != NULL; i++){
2051 /* FIXME: this breaks acl caching;
2052 * see also ACL_RECORD_VALUE_STATE above */
2053 ACL_RECORD_VALUE_STATE;
2055 if (aci_mask(op, e, desc, val, &bvals[i],
2057 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2061 /* evaluation stops as soon as either a "deny" or a
2062 * "grant" directive matches.
2064 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2068 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2069 accessmask2str(tgrant, accessmaskbuf, 1),
2070 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2074 case LDAP_NO_SUCH_ATTRIBUTE:
2075 /* just go on if the aci-Attribute is not present in
2078 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2082 case LDAP_NO_SUCH_OBJECT:
2083 /* We have reached the base object */
2084 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2095 dnParent( &parent_ndn, &parent_ndn );
2100 /* remove anything that the ACL clause does not allow */
2101 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2102 tdeny &= ACL_PRIV_MASK;
2104 /* see if we have anything to contribute */
2105 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2109 /* this could be improved by changing slap_acl_mask so that it can deal with
2110 * by clauses that return grant/deny pairs. Right now, it does either
2111 * additive or subtractive rights, but not both at the same time. So,
2112 * we need to combine the grant/deny pair into a single rights mask in
2113 * a smart way: if either grant or deny is "empty", then we use the
2114 * opposite as is, otherwise we remove any denied rights from the grant
2115 * rights mask and construct an additive mask.
2117 if (ACL_IS_INVALID(tdeny)) {
2118 modmask = tgrant | ACL_PRIV_ADDITIVE;
2120 } else if (ACL_IS_INVALID(tgrant)) {
2121 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2124 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2128 #endif /* SLAPD_ACI_ENABLED */
2129 #endif /* !SLAP_DYNACL */
2131 modmask = b->a_access_mask;
2134 Debug( LDAP_DEBUG_ACL,
2135 "<= acl_mask: [%d] applying %s (%s)\n",
2136 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2137 b->a_type == ACL_CONTINUE
2139 : b->a_type == ACL_BREAK
2145 if( ACL_IS_ADDITIVE(modmask) ) {
2147 ACL_PRIV_SET( *mask, modmask );
2150 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2152 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2153 /* substract privs */
2154 ACL_PRIV_CLR( *mask, modmask );
2157 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2164 Debug( LDAP_DEBUG_ACL,
2165 "<= acl_mask: [%d] mask: %s\n",
2166 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2168 if( b->a_type == ACL_CONTINUE ) {
2171 } else if ( b->a_type == ACL_BREAK ) {
2179 /* implicit "by * none" clause */
2182 Debug( LDAP_DEBUG_ACL,
2183 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2184 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2189 * acl_check_modlist - check access control on the given entry to see if
2190 * it allows the given modifications by the user associated with op.
2191 * returns 1 if mods allowed ok
2192 * 0 mods not allowed
2199 Modifications *mlist
2203 AccessControlState state = ACL_STATE_INIT;
2206 int ret = 1; /* default is access allowed */
2210 be = LDAP_STAILQ_FIRST(&backendDB);
2214 assert( be != NULL );
2216 /* short circuit root database access */
2217 if ( be_isroot( op ) ) {
2218 Debug( LDAP_DEBUG_ACL,
2219 "<= acl_access_allowed: granted to database root\n",
2224 /* use backend default access if no backend acls */
2225 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2226 Debug( LDAP_DEBUG_ACL,
2227 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2228 access2str( ACL_WRITE ),
2229 op->o_bd->be_dfltaccess >= ACL_WRITE
2230 ? "granted" : "denied",
2232 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2236 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2238 * Internal mods are ignored by ACL_WRITE checking
2240 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2241 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2242 " modify access granted\n",
2243 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2248 * no-user-modification operational attributes are ignored
2249 * by ACL_WRITE checking as any found here are not provided
2252 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2253 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2254 " modify access granted\n",
2255 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2259 switch ( mlist->sml_op ) {
2260 case LDAP_MOD_REPLACE:
2262 * We must check both permission to delete the whole
2263 * attribute and permission to add the specific attributes.
2264 * This prevents abuse from selfwriters.
2266 if ( ! access_allowed( op, e,
2267 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2273 if ( mlist->sml_values == NULL ) break;
2275 /* fall thru to check value to add */
2278 assert( mlist->sml_values != NULL );
2280 for ( bv = mlist->sml_nvalues
2281 ? mlist->sml_nvalues : mlist->sml_values;
2282 bv->bv_val != NULL; bv++ )
2284 if ( ! access_allowed( op, e,
2285 mlist->sml_desc, bv, ACL_WADD, &state ) )
2293 case LDAP_MOD_DELETE:
2294 if ( mlist->sml_values == NULL ) {
2295 if ( ! access_allowed( op, e,
2296 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2303 for ( bv = mlist->sml_nvalues
2304 ? mlist->sml_nvalues : mlist->sml_values;
2305 bv->bv_val != NULL; bv++ )
2307 if ( ! access_allowed( op, e,
2308 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2316 case SLAP_MOD_SOFTADD:
2317 /* allow adding attribute via modrdn thru */
2329 if (be_null) op->o_bd = NULL;
2335 struct berval *list,
2348 while ( len >= 0 && --ix >= 0 ) {
2349 while ( --len >= 0 && *p++ != sep )
2352 while ( len >= 0 && *p == ' ' ) {
2365 while ( --len >= 0 && *p != sep ) {
2369 while ( bv->bv_len > 0 && *--p == ' ' ) {
2376 typedef struct aci_set_gather_t {
2382 aci_set_cb_gather( Operation *op, SlapReply *rs )
2384 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2386 if ( rs->sr_type == REP_SEARCH ) {
2387 BerValue bvals[ 2 ];
2388 BerVarray bvalsp = NULL;
2391 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2392 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2394 if ( desc == slap_schema.si_ad_entryDN ) {
2396 bvals[ 0 ] = rs->sr_entry->e_nname;
2397 BER_BVZERO( &bvals[ 1 ] );
2402 a = attr_find( rs->sr_entry->e_attrs, desc );
2406 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2409 bvalsp = a->a_nvals;
2415 p->bvals = slap_set_join( p->cookie, p->bvals,
2416 ( '|' | SLAP_SET_RREF ), bvalsp );
2420 assert( rs->sr_type == REP_RESULT );
2427 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2429 AciSetCookie *cp = (AciSetCookie *)cookie;
2431 LDAPURLDesc *ludp = NULL;
2432 Operation op2 = { 0 };
2433 SlapReply rs = {REP_RESULT};
2434 AttributeName anlist[ 2 ], *anlistp = NULL;
2436 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2437 aci_set_gather_t p = { 0 };
2438 const char *text = NULL;
2439 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2441 /* this routine needs to return the bervals instead of
2442 * plain strings, since syntax is not known. It should
2443 * also return the syntax or some "comparison cookie".
2445 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2446 return aci_set_gather2( cookie, name, desc );
2449 rc = ldap_url_parse( name->bv_val, &ludp );
2450 if ( rc != LDAP_URL_SUCCESS ) {
2451 rc = LDAP_PROTOCOL_ERROR;
2455 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2457 /* host part must be empty */
2458 /* extensions parts must be empty */
2459 rc = LDAP_PROTOCOL_ERROR;
2463 /* Grab the searchbase and see if an appropriate database can be found */
2464 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2465 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2466 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2467 BER_BVZERO( &op2.o_req_dn );
2468 if ( rc != LDAP_SUCCESS ) {
2472 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2473 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2474 rc = LDAP_NO_SUCH_OBJECT;
2478 /* Grab the filter */
2479 if ( ludp->lud_filter ) {
2480 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2481 cp->op->o_tmpmemctx );
2484 op2.ors_filterstr = defaultFilter_bv;
2487 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2488 if ( op2.ors_filter == NULL ) {
2489 rc = LDAP_PROTOCOL_ERROR;
2493 /* Grab the scope */
2494 op2.ors_scope = ludp->lud_scope;
2496 /* Grap the attributes */
2497 if ( ludp->lud_attrs ) {
2498 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2501 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2502 cp->op->o_tmpmemctx );
2504 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2505 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2506 anlistp[ nattrs ].an_desc = NULL;
2507 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2508 &anlistp[ nattrs ].an_desc, &text );
2509 if ( rc != LDAP_SUCCESS ) {
2518 anlistp[ nattrs ].an_name = desc->ad_cname;
2519 anlistp[ nattrs ].an_desc = desc;
2521 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2525 op2.o_hdr = cp->op->o_hdr;
2526 op2.o_tag = LDAP_REQ_SEARCH;
2527 op2.o_ndn = op2.o_bd->be_rootndn;
2528 op2.o_callback = &cb;
2529 op2.o_time = slap_get_time();
2530 op2.o_do_not_cache = 1;
2531 op2.o_is_auth_check = 0;
2532 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2533 op2.ors_slimit = SLAP_NO_LIMIT;
2534 op2.ors_tlimit = SLAP_NO_LIMIT;
2535 op2.ors_attrs = anlistp;
2536 op2.ors_attrsonly = 0;
2537 op2.o_private = cp->op->o_private;
2541 rc = op2.o_bd->be_search( &op2, &rs );
2547 if ( op2.ors_filter ) {
2548 filter_free_x( cp->op, op2.ors_filter );
2550 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2551 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2553 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2554 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2557 ldap_free_urldesc( ludp );
2559 if ( anlistp && anlistp != anlist ) {
2560 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2567 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2569 AciSetCookie *cp = (AciSetCookie *)cookie;
2570 BerVarray bvals = NULL;
2574 /* this routine needs to return the bervals instead of
2575 * plain strings, since syntax is not known. It should
2576 * also return the syntax or some "comparison cookie".
2578 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2579 if ( rc == LDAP_SUCCESS ) {
2580 if ( desc == slap_schema.si_ad_entryDN ) {
2581 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2582 cp->op->o_tmpmemctx );
2584 BER_BVZERO( &bvals[ 1 ] );
2588 backend_attribute( cp->op,
2589 cp->e, &ndn, desc, &bvals, ACL_NONE );
2592 if ( !BER_BVISNULL( &ndn ) ) {
2593 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2602 struct berval *subj,
2608 struct berval set = BER_BVNULL;
2610 AciSetCookie cookie;
2612 if ( setref == 0 ) {
2613 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2616 struct berval subjdn, ndn = BER_BVNULL;
2617 struct berval setat;
2620 AttributeDescription *desc = NULL;
2622 /* format of string is "entry/setAttrName" */
2623 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2627 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2628 setat = aci_bv_set_attr;
2632 * NOTE: dnNormalize honors the ber_len field
2633 * as the length of the dn to be normalized
2635 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2636 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2638 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2639 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2643 BER_BVZERO( &bvals[0] );
2644 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2646 bvals[0].bv_val = bvals[i-1].bv_val;
2647 BER_BVZERO( &bvals[i-1] );
2649 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2650 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2655 if ( !BER_BVISNULL( &set ) ) {
2658 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2659 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2660 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2666 #ifdef SLAPD_ACI_ENABLED
2668 aci_list_map_rights(
2669 struct berval *list )
2676 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2679 switch (*bv.bv_val) {
2681 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2684 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2685 * the right 's' to mean "set", but in the examples states
2686 * that the right 's' means "search". The latter definition
2689 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2692 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2695 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2698 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2699 * define any equivalent to the AUTH right, so I've just used
2702 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2714 struct berval *list,
2715 const struct berval *attr,
2716 struct berval *val )
2718 struct berval bv, left, right;
2721 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2722 if (aci_get_part(&bv, 0, '=', &left) < 0
2723 || aci_get_part(&bv, 1, '=', &right) < 0)
2725 if (ber_bvstrcasecmp(attr, &bv) == 0)
2727 } else if (val == NULL) {
2728 if (ber_bvstrcasecmp(attr, &left) == 0)
2731 if (ber_bvstrcasecmp(attr, &left) == 0) {
2732 /* this is experimental code that implements a
2733 * simple (prefix) match of the attribute value.
2734 * the ACI draft does not provide for aci's that
2735 * apply to specific values, but it would be
2736 * nice to have. If the <attr> part of an aci's
2737 * rights list is of the form <attr>=<value>,
2738 * that means the aci applies only to attrs with
2739 * the given value. Furthermore, if the attr is
2740 * of the form <attr>=<value>*, then <value> is
2741 * treated as a prefix, and the aci applies to
2742 * any value with that prefix.
2744 * Ideally, this would allow r.e. matches.
2746 if (aci_get_part(&right, 0, '*', &left) < 0
2747 || right.bv_len <= left.bv_len)
2749 if (ber_bvstrcasecmp(val, &right) == 0)
2751 } else if (val->bv_len >= left.bv_len) {
2752 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2761 static slap_access_t
2762 aci_list_get_attr_rights(
2763 struct berval *list,
2764 const struct berval *attr,
2765 struct berval *val )
2771 /* loop through each rights/attr pair, skip first part (action) */
2773 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2774 if (aci_list_has_attr(&bv, attr, val) == 0)
2776 if (aci_get_part(list, i, ';', &bv) < 0)
2778 mask |= aci_list_map_rights(&bv);
2784 aci_list_get_rights(
2785 struct berval *list,
2786 const struct berval *attr,
2788 slap_access_t *grant,
2789 slap_access_t *deny )
2791 struct berval perm, actn;
2792 slap_access_t *mask;
2795 if (attr == NULL || attr->bv_len == 0
2796 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2797 attr = &aci_bv_br_entry;
2803 /* loop through each permissions clause */
2804 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2805 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2807 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2809 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2816 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2817 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2824 struct berval *subj,
2825 struct berval *defgrpoc,
2826 struct berval *defgrpat,
2833 struct berval subjdn;
2834 struct berval grpoc;
2835 struct berval grpat;
2836 ObjectClass *grp_oc = NULL;
2837 AttributeDescription *grp_ad = NULL;
2841 /* format of string is "group/objectClassValue/groupAttrName" */
2842 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2846 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2850 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2854 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2855 if( rc != LDAP_SUCCESS ) {
2861 grp_oc = oc_bvfind( &grpoc );
2863 if (grp_oc != NULL && grp_ad != NULL ) {
2864 char buf[ACL_BUF_SIZE];
2865 struct berval bv, ndn;
2866 bv.bv_len = sizeof( buf ) - 1;
2867 bv.bv_val = (char *)&buf;
2868 if ( string_expand(&bv, &subjdn,
2869 e->e_ndn, nmatch, matches) )
2874 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2875 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2876 grp_oc, grp_ad ) == 0 );
2877 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2889 AttributeDescription *desc,
2893 regmatch_t *matches,
2894 slap_access_t *grant,
2895 slap_access_t *deny,
2896 slap_aci_scope_t asserted_scope
2899 struct berval bv, scope, perms, type, sdn;
2903 assert( !BER_BVISNULL( &desc->ad_cname ) );
2905 /* parse an aci of the form:
2906 oid # scope # action;rights;attr;rights;attr
2907 $ action;rights;attr;rights;attr # type # subject
2909 [NOTE: the following comment is very outdated,
2910 as the draft version it refers to (Ando, 2004-11-20)].
2912 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2913 a full description of the format for this attribute.
2914 Differences: "this" in the draft is "self" here, and
2915 "self" and "public" is in the position of type.
2917 <scope> = {entry|children|subtree}
2918 <type> = {public|users|access-id|subtree|onelevel|children|
2919 self|dnattr|group|role|set|set-ref}
2921 This routine now supports scope={ENTRY,CHILDREN}
2923 - ENTRY applies to "entry" and "subtree";
2924 - CHILDREN aplies to "children" and "subtree"
2927 /* check that the aci has all 5 components */
2928 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2932 /* check that the aci family is supported */
2933 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2937 /* check that the scope matches */
2938 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2942 /* note: scope can be either ENTRY or CHILDREN;
2943 * they respectively match "entry" and "children" in bv
2944 * both match "subtree" */
2945 switch ( asserted_scope ) {
2946 case SLAP_ACI_SCOPE_ENTRY:
2947 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2948 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2954 case SLAP_ACI_SCOPE_CHILDREN:
2955 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2956 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2966 /* get the list of permissions clauses, bail if empty */
2967 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2971 /* check if any permissions allow desired access */
2972 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2976 /* see if we have a DN match */
2977 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2981 /* see if we have a public (i.e. anonymous) access */
2982 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2986 /* otherwise require an identity */
2987 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2991 /* see if we have a users access */
2992 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2996 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2997 * just grab all the berval up to its end (ITS#3303).
2998 * NOTE: the problem could be solved by providing the DN with
2999 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3000 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3002 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3006 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3007 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3009 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3012 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3013 if ( rc != LDAP_SUCCESS ) {
3017 if ( dn_match( &op->o_ndn, &ndn ) ) {
3020 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3024 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3027 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3028 if ( rc != LDAP_SUCCESS ) {
3032 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3035 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3039 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3040 struct berval ndn, pndn;
3042 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3043 if ( rc != LDAP_SUCCESS ) {
3047 dnParent( &ndn, &pndn );
3049 if ( dn_match( &op->o_ndn, &pndn ) ) {
3052 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3056 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3059 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3060 if ( rc != LDAP_SUCCESS ) {
3064 if ( !dn_match( &op->o_ndn, &ndn )
3065 && dnIsSuffix( &op->o_ndn, &ndn ) )
3069 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3073 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3074 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3078 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3080 AttributeDescription *ad = NULL;
3083 rc = slap_bv2ad( &sdn, &ad, &text );
3085 if( rc != LDAP_SUCCESS ) {
3091 for ( at = attrs_find( e->e_attrs, ad );
3093 at = attrs_find( at->a_next, ad ) )
3095 if ( value_find_ex( ad,
3096 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3097 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3099 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3108 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3109 if ( aci_group_member( &sdn, &aci_bv_group_class,
3110 &aci_bv_group_attr, op, e, nmatch, matches ) )
3115 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3116 if ( aci_group_member( &sdn, &aci_bv_role_class,
3117 &aci_bv_role_attr, op, e, nmatch, matches ) )
3122 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3123 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3127 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3128 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3138 * FIXME: there is a silly dependence that makes it difficult
3139 * to move ACIs in a run-time loadable module under the "dynacl"
3140 * umbrella, because sets share some helpers with ACIs.
3143 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3145 AttributeDescription *ad = NULL;
3146 const char *text = NULL;
3148 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3149 fprintf( stderr, "%s: line %d: "
3150 "inappropriate style \"%s\" in \"aci\" by clause\n",
3151 fname, lineno, style_strings[sty] );
3155 if ( right != NULL && *right != '\0' ) {
3156 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3158 "%s: line %d: aci \"%s\": %s\n",
3159 fname, lineno, right, text );
3164 ad = slap_schema.si_ad_aci;
3167 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3168 fprintf( stderr, "%s: line %d: "
3169 "aci \"%s\": inappropriate syntax: %s\n",
3170 fname, lineno, right,
3171 ad->ad_type->sat_syntax_oid );
3175 *privp = (void *)ad;
3181 dynacl_aci_unparse( void *priv, struct berval *bv )
3183 AttributeDescription *ad = ( AttributeDescription * )priv;
3186 assert( ad != NULL );
3188 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3189 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3190 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3191 bv->bv_len = ptr - bv->bv_val;
3202 AttributeDescription *desc,
3205 regmatch_t *matches,
3206 slap_access_t *grantp,
3207 slap_access_t *denyp )
3209 AttributeDescription *ad = ( AttributeDescription * )priv;
3211 slap_access_t tgrant, tdeny, grant, deny;
3213 char accessmaskbuf[ACCESSMASK_MAXLEN];
3214 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3215 #endif /* LDAP_DEBUG */
3217 /* start out with nothing granted, nothing denied */
3221 /* get the aci attribute */
3222 at = attr_find( e->e_attrs, ad );
3226 /* the aci is an multi-valued attribute. The
3227 * rights are determined by OR'ing the individual
3228 * rights given by the acis.
3230 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3231 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3232 nmatch, matches, &grant, &deny,
3233 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3240 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3241 accessmask2str( tgrant, accessmaskbuf, 1 ),
3242 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3245 /* If the entry level aci didn't contain anything valid for the
3246 * current operation, climb up the tree and evaluate the
3247 * acis with scope set to subtree
3249 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3250 struct berval parent_ndn;
3253 /* to solve the chicken'n'egg problem of accessing
3254 * the OpenLDAPaci attribute, the direct access
3255 * to the entry's attribute is unchecked; however,
3256 * further accesses to OpenLDAPaci values in the
3257 * ancestors occur through backend_attribute(), i.e.
3258 * with the identity of the operation, requiring
3259 * further access checking. For uniformity, this
3260 * makes further requests occur as the rootdn, if
3261 * any, i.e. searching for the OpenLDAPaci attribute
3262 * is considered an internal search. If this is not
3263 * acceptable, then the same check needs be performed
3264 * when accessing the entry's attribute. */
3265 Operation op2 = *op;
3267 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3268 op2.o_dn = op->o_bd->be_rootdn;
3269 op2.o_ndn = op->o_bd->be_rootndn;
3273 dnParent( &e->e_nname, &parent_ndn );
3274 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3276 BerVarray bvals = NULL;
3279 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3280 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3289 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3290 if ( aci_mask( op, e, desc, val,
3294 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3298 /* evaluation stops as soon as either a "deny" or a
3299 * "grant" directive matches.
3301 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3305 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3306 accessmask2str( tgrant, accessmaskbuf, 1 ),
3307 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3311 case LDAP_NO_SUCH_ATTRIBUTE:
3312 /* just go on if the aci-Attribute is not present in
3315 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3319 case LDAP_NO_SUCH_OBJECT:
3320 /* We have reached the base object */
3321 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3333 dnParent( &parent_ndn, &parent_ndn );
3343 /* need to register this at some point */
3344 static slap_dynacl_t dynacl_aci = {
3354 #endif /* SLAP_DYNACL */
3356 #endif /* SLAPD_ACI_ENABLED */
3361 * dynamic ACL infrastructure
3363 static slap_dynacl_t *da_list = NULL;
3366 slap_dynacl_register( slap_dynacl_t *da )
3370 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3371 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3376 if ( tmp != NULL ) {
3380 if ( da->da_mask == NULL ) {
3384 da->da_private = NULL;
3385 da->da_next = da_list;
3391 static slap_dynacl_t *
3392 slap_dynacl_next( slap_dynacl_t *da )
3401 slap_dynacl_get( const char *name )
3405 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3406 if ( strcasecmp( da->da_name, name ) == 0 ) {
3413 #endif /* SLAP_DYNACL */
3420 slap_dynacl_t *known_dynacl[] = {
3421 #ifdef SLAPD_ACI_ENABLED
3423 #endif /* SLAPD_ACI_ENABLED */
3427 for ( i = 0; known_dynacl[ i ]; i++ ) {
3428 rc = slap_dynacl_register( known_dynacl[ i ] );
3433 #endif /* SLAP_DYNACL */
3444 regmatch_t *matches)
3452 bv->bv_val[0] = '\0';
3453 bv->bv_len--; /* leave space for lone $ */
3456 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3457 sp < pat->bv_val + pat->bv_len ; sp++ )
3459 /* did we previously see a $ */
3461 if ( flag == 1 && *sp == '$' ) {
3466 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3469 } else if ( *sp >= '0' && *sp <= '9' ) {
3477 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3478 if ( *sp >= '0' && *sp <= '9' ) {
3479 n = 10*n + ( *sp - '0' );
3483 if ( *sp != /*'{'*/ '}' ) {
3489 if ( n >= nmatch ) {
3495 i = matches[n].rm_so;
3496 l = matches[n].rm_eo;
3497 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3515 /* must have ended with a single $ */
3523 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3524 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3531 struct berval *pat, /* pattern to expand and match against */
3532 char *str, /* string to match against pattern */
3533 char *buf, /* buffer with $N expansion variables */
3534 int nmatch, /* size of the matches array */
3535 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3539 char newbuf[ACL_BUF_SIZE];
3543 bv.bv_len = sizeof( newbuf ) - 1;
3550 string_expand( &bv, pat, buf, nmatch, matches );
3551 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3553 char error[ACL_BUF_SIZE];
3554 regerror( rc, &re, error, sizeof( error ) );
3556 Debug( LDAP_DEBUG_TRACE,
3557 "compile( \"%s\", \"%s\") failed %s\n",
3558 pat->bv_val, str, error );
3562 rc = regexec( &re, str, 0, NULL, 0 );
3565 Debug( LDAP_DEBUG_TRACE,
3566 "=> regex_matches: string: %s\n", str, 0, 0 );
3567 Debug( LDAP_DEBUG_TRACE,
3568 "=> regex_matches: rc: %d %s\n",
3569 rc, !rc ? "matches" : "no matches", 0 );