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,
353 * NOTE: control gets here if FIXME
354 * if an appropriate backend cannot be selected for the operation,
355 * we assume that the frontend should handle this
356 * FIXME: should select_backend() take care of this,
357 * and return frontendDB instead of NULL? maybe for some value
362 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
363 if ( op->o_bd == NULL ) {
364 op->o_bd = frontendDB;
366 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
376 AttributeDescription *desc,
378 slap_access_t access,
379 AccessControlState *state,
383 AccessControl *a = NULL;
387 char accessmaskbuf[ACCESSMASK_MAXLEN];
390 slap_control_t control;
391 slap_access_t access_level;
393 int st_same_attr = 0;
394 static AccessControlState state_init = ACL_STATE_INIT;
397 assert( desc != NULL );
399 access_level = ACL_LEVEL( access );
401 assert( access_level > ACL_NONE );
404 if ( maskp ) ACL_INVALIDATE( *maskp );
406 attr = desc->ad_cname.bv_val;
408 assert( attr != NULL );
410 if ( op && op->o_is_auth_check &&
411 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
417 if ( state->as_vd_ad == desc ) {
418 if ( state->as_recorded ) {
419 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
422 return state->as_result;
424 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
425 val != NULL && state->as_vd_acl == NULL )
427 return state->as_result;
435 state->as_vd_ad = desc;
438 Debug( LDAP_DEBUG_ACL,
439 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
440 access2str( access ), e->e_dn, attr );
447 if ( op->o_bd == NULL ) {
448 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
453 * FIXME: experimental; use first backend rules
454 * iff there is no global_acl (ITS#3100) */
455 if ( frontendDB->be_acl != NULL ) {
456 op->o_bd = frontendDB;
458 #endif /* LDAP_DEVEL */
460 assert( op->o_bd != NULL );
462 /* this is enforced in backend_add() */
463 if ( op->o_bd->bd_info->bi_access_allowed ) {
464 /* delegate to backend */
465 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
466 desc, val, access, state, &mask );
469 BackendDB *be_orig = op->o_bd;
471 /* use default (but pass through frontend
472 * for global ACL overlays) */
473 op->o_bd = frontendDB;
474 ret = frontendDB->bd_info->bi_access_allowed( op, e,
475 desc, val, access, state, &mask );
480 if ( ACL_IS_INVALID( mask ) ) {
481 Debug( LDAP_DEBUG_ACL,
482 "=> access_allowed: \"%s\" (%s) invalid!\n",
486 } else if ( control == ACL_BREAK ) {
487 Debug( LDAP_DEBUG_ACL,
488 "=> access_allowed: no more rules\n", 0, 0, 0 );
493 ret = ACL_GRANT( mask, access );
496 Debug( LDAP_DEBUG_ACL,
497 "=> access_allowed: %s access %s by %s\n",
498 access2str( access ), ret ? "granted" : "denied",
499 accessmask2str( mask, accessmaskbuf, 1 ) );
502 if ( state != NULL ) {
503 /* If not value-dependent, save ACL in case of more attrs */
504 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
505 state->as_vi_acl = a;
506 state->as_result = ret;
508 state->as_recorded |= ACL_STATE_RECORDED;
510 if ( be_null ) op->o_bd = NULL;
511 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
515 #else /* !SLAP_OVERLAY_ACCESS */
521 AttributeDescription *desc,
523 slap_access_t access,
524 AccessControlState *state,
529 AccessControl *a = NULL;
534 char accessmaskbuf[ACCESSMASK_MAXLEN];
537 slap_control_t control;
538 slap_access_t access_level;
540 regmatch_t matches[MAXREMATCHES];
541 int st_same_attr = 0;
542 static AccessControlState state_init = ACL_STATE_INIT;
545 assert( desc != NULL );
547 access_level = ACL_LEVEL( access );
549 assert( access_level > ACL_NONE );
550 if ( maskp ) ACL_INVALIDATE( *maskp );
552 attr = desc->ad_cname.bv_val;
554 assert( attr != NULL );
556 if ( op && op->o_is_auth_check &&
557 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
563 if ( state->as_vd_ad == desc ) {
564 if ( state->as_recorded ) {
565 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
568 return state->as_result;
570 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
571 val != NULL && state->as_vd_acl == NULL )
573 return state->as_result;
581 state->as_vd_ad=desc;
584 Debug( LDAP_DEBUG_ACL,
585 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
586 access2str( access ), e->e_dn, attr );
595 be = LDAP_STAILQ_FIRST(&backendDB);
599 * FIXME: experimental; use first backend rules
600 * iff there is no global_acl (ITS#3100) */
601 if ( frontendDB->be_acl == NULL )
607 assert( be != NULL );
609 /* grant database root access */
610 if ( be_isroot( op ) ) {
611 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
613 mask = ACL_LVL_MANAGE;
620 * no-user-modification operational attributes are ignored
621 * by ACL_WRITE checking as any found here are not provided
624 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
625 && desc != slap_schema.si_ad_entry
626 && desc != slap_schema.si_ad_children )
628 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
629 " %s access granted\n",
634 /* use backend default access if no backend acls */
635 if ( be->be_acl == NULL ) {
636 Debug( LDAP_DEBUG_ACL,
637 "=> access_allowed: backend default %s "
638 "access %s to \"%s\"\n",
639 access2str( access ),
640 be->be_dfltaccess >= access_level ? "granted" : "denied",
641 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
642 ret = be->be_dfltaccess >= access_level;
647 mask = ACL_PRIV_LEVEL;
648 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
649 mask |= ACL_ACCESS2PRIV( i );
656 /* be is always non-NULL */
657 /* use global default access if no global acls */
658 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
659 Debug( LDAP_DEBUG_ACL,
660 "=> access_allowed: global default %s access %s to \"%s\"\n",
661 access2str( access ),
662 frontendDB->be_dfltaccess >= access_level ?
663 "granted" : "denied", op->o_dn.bv_val );
664 ret = frontendDB->be_dfltaccess >= access_level;
669 mask = ACL_PRIV_LEVEL;
670 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
671 mask |= ACL_ACCESS2PRIV( i );
682 if ( st_same_attr ) {
683 assert( state->as_vd_acl != NULL );
685 a = state->as_vd_acl;
686 count = state->as_vd_acl_count;
687 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
688 mask = state->as_vd_acl_mask;
689 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
694 if ( state ) state->as_vi_acl = NULL;
698 memset( matches, '\0', sizeof(matches) );
701 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
702 MAXREMATCHES, matches, state ) ) != NULL )
706 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
707 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
708 (int)matches[i].rm_so, (int)matches[i].rm_eo );
709 if ( matches[i].rm_so <= matches[0].rm_eo ) {
711 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
712 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
715 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
719 if ( state->as_vi_acl == a &&
720 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
722 Debug( LDAP_DEBUG_ACL,
723 "access_allowed: result from state (%s)\n",
725 ret = state->as_result;
728 Debug( LDAP_DEBUG_ACL,
729 "access_allowed: no res from state (%s)\n",
735 control = slap_acl_mask( a, &mask, op,
736 e, desc, val, MAXREMATCHES, matches, count, state );
738 if ( control != ACL_BREAK ) {
742 memset( matches, '\0', sizeof(matches) );
745 if ( ACL_IS_INVALID( mask ) ) {
746 Debug( LDAP_DEBUG_ACL,
747 "=> access_allowed: \"%s\" (%s) invalid!\n",
751 } else if ( control == ACL_BREAK ) {
752 Debug( LDAP_DEBUG_ACL,
753 "=> access_allowed: no more rules\n", 0, 0, 0 );
758 Debug( LDAP_DEBUG_ACL,
759 "=> access_allowed: %s access %s by %s\n",
760 access2str( access ),
761 ACL_GRANT(mask, access) ? "granted" : "denied",
762 accessmask2str( mask, accessmaskbuf, 1 ) );
764 ret = ACL_GRANT(mask, access);
767 if ( state != NULL ) {
768 /* If not value-dependent, save ACL in case of more attrs */
769 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
770 state->as_vi_acl = a;
771 state->as_result = ret;
773 state->as_recorded |= ACL_STATE_RECORDED;
775 if ( be_null ) op->o_bd = NULL;
776 if ( maskp ) *maskp = mask;
780 #endif /* SLAP_OVERLAY_ACCESS */
783 * slap_acl_get - return the acl applicable to entry e, attribute
784 * attr. the acl returned is suitable for use in subsequent calls to
785 * acl_access_allowed().
788 static AccessControl *
794 AttributeDescription *desc,
798 AccessControlState *state )
805 assert( count != NULL );
806 assert( desc != NULL );
808 attr = desc->ad_cname.bv_val;
810 assert( attr != NULL );
813 if( op->o_bd == NULL ) {
814 a = frontendDB->be_acl;
816 a = op->o_bd->be_acl;
827 dnlen = e->e_nname.bv_len;
829 for ( ; a != NULL; a = a->acl_next ) {
832 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
833 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
834 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
835 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
836 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
840 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
841 *count, a->acl_dn_pat.bv_val, 0 );
842 patlen = a->acl_dn_pat.bv_len;
843 if ( dnlen < patlen )
846 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
847 /* base dn -- entire object DN must match */
848 if ( dnlen != patlen )
851 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
852 int rdnlen = -1, sep = 0;
854 if ( dnlen <= patlen )
858 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
863 rdnlen = dn_rdnlen( NULL, &e->e_nname );
864 if ( rdnlen != dnlen - patlen - sep )
867 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
868 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
871 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
872 if ( dnlen <= patlen )
874 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
878 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
882 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
886 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
887 matches[0].rm_so = matches[0].rm_eo = -1;
891 /* Is this ACL only for a specific value? */
892 if ( a->acl_attrval.bv_len ) {
897 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
898 state->as_recorded |= ACL_STATE_RECORDED_VD;
899 state->as_vd_acl = a;
900 state->as_vd_acl_count = *count;
901 state->as_vd_access = a->acl_access;
902 state->as_vd_access_count = 1;
903 ACL_INVALIDATE( state->as_vd_acl_mask );
906 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
907 Debug( LDAP_DEBUG_ACL,
908 "acl_get: valpat %s\n",
909 a->acl_attrval.bv_val, 0, 0 );
910 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
918 Debug( LDAP_DEBUG_ACL,
920 a->acl_attrval.bv_val, 0, 0 );
922 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
923 if (value_match( &match, desc,
924 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
925 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
932 patlen = a->acl_attrval.bv_len;
933 vdnlen = val->bv_len;
935 if ( vdnlen < patlen )
938 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
939 if ( vdnlen > patlen )
942 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
945 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
948 rdnlen = dn_rdnlen( NULL, val );
949 if ( rdnlen != vdnlen - patlen - 1 )
952 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
953 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
956 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
957 if ( vdnlen <= patlen )
960 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
964 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
970 if ( a->acl_filter != NULL ) {
971 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
972 if ( rc != LDAP_COMPARE_TRUE ) {
977 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
982 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
994 struct berval *opndn )
997 * if access applies to the entry itself, and the
998 * user is bound as somebody in the same namespace as
999 * the entry, OR the given dn matches the dn pattern
1002 * NOTE: styles "anonymous", "users" and "self"
1003 * have been moved to enum slap_style_t, whose
1004 * value is set in a_dn_style; however, the string
1005 * is maintaned in a_dn_pat.
1007 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
1008 if ( !BER_BVISEMPTY( opndn ) ) {
1012 } else if ( b->a_style == ACL_STYLE_USERS ) {
1013 if ( BER_BVISEMPTY( opndn ) ) {
1017 } else if ( b->a_style == ACL_STYLE_SELF ) {
1018 struct berval ndn, selfndn;
1021 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1025 level = b->a_self_level;
1033 selfndn = e->e_nname;
1036 for ( ; level > 0; level-- ) {
1037 if ( BER_BVISEMPTY( &ndn ) ) {
1040 dnParent( &ndn, &ndn );
1043 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1048 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1049 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1051 regmatch_t tmp_matches[2],
1052 *tmp_matchesp = tmp_matches;
1056 switch ( a->acl_dn_style ) {
1057 case ACL_STYLE_REGEX:
1058 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1059 tmp_matchesp = matches;
1060 tmp_nmatch = nmatch;
1063 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1065 case ACL_STYLE_BASE:
1066 tmp_matches[0].rm_so = 0;
1067 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1072 case ACL_STYLE_SUBTREE:
1073 case ACL_STYLE_CHILDREN:
1074 tmp_matches[0].rm_so = 0;
1075 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1076 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1077 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1091 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1092 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1100 ber_len_t patlen, odnlen;
1103 if ( e->e_dn == NULL )
1106 if ( b->a_expand ) {
1108 char buf[ACL_BUF_SIZE];
1111 regmatch_t tmp_matches[2],
1112 *tmp_matchesp = tmp_matches;
1116 bv.bv_len = sizeof( buf ) - 1;
1119 switch ( a->acl_dn_style ) {
1120 case ACL_STYLE_REGEX:
1121 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1122 tmp_matchesp = matches;
1123 tmp_nmatch = nmatch;
1126 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1128 case ACL_STYLE_BASE:
1129 tmp_matches[0].rm_so = 0;
1130 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1135 case ACL_STYLE_SUBTREE:
1136 case ACL_STYLE_CHILDREN:
1137 tmp_matches[0].rm_so = 0;
1138 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1139 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1140 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1154 if ( string_expand( &bv, &b->a_pat,
1156 tmp_nmatch, tmp_matchesp ) )
1161 if ( dnNormalize(0, NULL, NULL, &bv,
1162 &pat, op->o_tmpmemctx )
1165 /* did not expand to a valid dn */
1173 patlen = pat.bv_len;
1174 odnlen = opndn->bv_len;
1175 if ( odnlen < patlen ) {
1176 goto dn_match_cleanup;
1180 if ( b->a_style == ACL_STYLE_BASE ) {
1181 /* base dn -- entire object DN must match */
1182 if ( odnlen != patlen ) {
1183 goto dn_match_cleanup;
1186 } else if ( b->a_style == ACL_STYLE_ONE ) {
1189 if ( odnlen <= patlen ) {
1190 goto dn_match_cleanup;
1193 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1194 goto dn_match_cleanup;
1197 rdnlen = dn_rdnlen( NULL, opndn );
1198 if ( rdnlen != odnlen - patlen - 1 ) {
1199 goto dn_match_cleanup;
1202 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1203 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1204 goto dn_match_cleanup;
1207 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1208 if ( odnlen <= patlen ) {
1209 goto dn_match_cleanup;
1212 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1213 goto dn_match_cleanup;
1216 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1220 if ( odnlen <= patlen ) {
1221 goto dn_match_cleanup;
1224 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1226 goto dn_match_cleanup;
1231 for ( ; level > 0; level-- ) {
1232 if ( BER_BVISEMPTY( &ndn ) ) {
1233 goto dn_match_cleanup;
1235 dnParent( &ndn, &ndn );
1236 if ( ndn.bv_len < patlen ) {
1237 goto dn_match_cleanup;
1241 if ( ndn.bv_len != patlen ) {
1242 goto dn_match_cleanup;
1246 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1249 if ( pat.bv_val != b->a_pat.bv_val ) {
1250 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1262 * Record value-dependent access control state
1264 #define ACL_RECORD_VALUE_STATE do { \
1265 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1266 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1267 state->as_vd_acl = a; \
1268 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1269 sizeof( state->as_vd_acl_matches )) ; \
1270 state->as_vd_acl_count = count; \
1271 state->as_vd_access = b; \
1272 state->as_vd_access_count = i; \
1284 regmatch_t *matches,
1286 AccessControlState *state,
1287 slap_dn_access *bdn,
1288 struct berval *opndn )
1294 const char *attr = bdn->a_at->ad_cname.bv_val;
1296 assert( attr != NULL );
1298 if ( BER_BVISEMPTY( opndn ) ) {
1302 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1305 /* see if asker is listed in dnattr */
1306 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1308 at = attrs_find( at->a_next, bdn->a_at ) )
1310 if ( value_find_ex( bdn->a_at,
1311 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1312 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1314 &bv, op->o_tmpmemctx ) == 0 )
1323 /* have a dnattr match. if this is a self clause then
1324 * the target must also match the op dn.
1326 if ( bdn->a_self ) {
1327 /* check if the target is an attribute. */
1328 if ( val == NULL ) return 1;
1330 /* target is attribute, check if the attribute value
1333 rc = value_match( &match, bdn->a_at,
1334 bdn->a_at->ad_type->sat_equality, 0,
1336 /* on match error or no match, fail the ACL clause */
1337 if ( rc != LDAP_SUCCESS || match != 0 )
1342 /* no dnattr match, check if this is a self clause */
1343 if ( ! bdn->a_self )
1346 ACL_RECORD_VALUE_STATE;
1348 /* this is a self clause, check if the target is an
1354 /* target is attribute, check if the attribute value
1357 rc = value_match( &match, bdn->a_at,
1358 bdn->a_at->ad_type->sat_equality, 0,
1361 /* on match error or no match, fail the ACL clause */
1362 if ( rc != LDAP_SUCCESS || match != 0 )
1371 * slap_acl_mask - modifies mask based upon the given acl and the
1372 * requested access to entry e, attribute attr, value val. if val
1373 * is null, access to the whole attribute is assumed (all values).
1375 * returns 0 access NOT allowed
1379 static slap_control_t
1385 AttributeDescription *desc,
1388 regmatch_t *matches,
1390 AccessControlState *state )
1395 char accessmaskbuf[ACCESSMASK_MAXLEN];
1396 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1397 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1398 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1402 assert( a != NULL );
1403 assert( mask != NULL );
1404 assert( desc != NULL );
1406 attr = desc->ad_cname.bv_val;
1408 assert( attr != NULL );
1410 Debug( LDAP_DEBUG_ACL,
1411 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1414 Debug( LDAP_DEBUG_ACL,
1415 "=> acl_mask: to %s by \"%s\", (%s) \n",
1416 val ? "value" : "all values",
1417 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1418 accessmask2str( *mask, accessmaskbuf, 1 ) );
1421 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1422 && state->as_vd_acl == a )
1424 b = state->as_vd_access;
1425 i = state->as_vd_access_count;
1432 for ( ; b != NULL; b = b->a_next, i++ ) {
1433 slap_mask_t oldmask, modmask;
1435 ACL_INVALIDATE( modmask );
1437 /* AND <who> clauses */
1438 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1439 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1440 b->a_dn_pat.bv_val, 0, 0);
1442 * if access applies to the entry itself, and the
1443 * user is bound as somebody in the same namespace as
1444 * the entry, OR the given dn matches the dn pattern
1447 * NOTE: styles "anonymous", "users" and "self"
1448 * have been moved to enum slap_style_t, whose
1449 * value is set in a_dn_style; however, the string
1450 * is maintaned in a_dn_pat.
1453 if ( acl_mask_dn( op, e, a, nmatch, matches,
1454 &b->a_dn, &op->o_ndn ) )
1460 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1463 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1464 b->a_realdn_pat.bv_val, 0, 0);
1466 * if access applies to the entry itself, and the
1467 * user is bound as somebody in the same namespace as
1468 * the entry, OR the given dn matches the dn pattern
1471 * NOTE: styles "anonymous", "users" and "self"
1472 * have been moved to enum slap_style_t, whose
1473 * value is set in a_dn_style; however, the string
1474 * is maintaned in a_dn_pat.
1477 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1479 ndn = op->o_conn->c_ndn;
1484 if ( acl_mask_dn( op, e, a, nmatch, matches,
1485 &b->a_realdn, &ndn ) )
1491 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1492 if ( ! op->o_conn->c_listener ) {
1495 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1496 b->a_sockurl_pat.bv_val, 0, 0 );
1498 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1499 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1500 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1501 e->e_ndn, nmatch, matches ) )
1506 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1508 char buf[ACL_BUF_SIZE];
1510 bv.bv_len = sizeof( buf ) - 1;
1512 if ( string_expand( &bv, &b->a_sockurl_pat,
1513 e->e_ndn, nmatch, matches ) )
1518 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1524 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1532 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1533 if ( !op->o_conn->c_peer_domain.bv_val ) {
1536 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1537 b->a_domain_pat.bv_val, 0, 0 );
1538 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1539 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1540 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1541 e->e_ndn, nmatch, matches ) )
1546 char buf[ACL_BUF_SIZE];
1548 struct berval cmp = op->o_conn->c_peer_domain;
1549 struct berval pat = b->a_domain_pat;
1551 if ( b->a_domain_expand ) {
1554 bv.bv_len = sizeof(buf) - 1;
1557 if ( string_expand(&bv, &b->a_domain_pat,
1558 e->e_ndn, nmatch, matches) )
1565 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1566 int offset = cmp.bv_len - pat.bv_len;
1571 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1575 /* trim the domain */
1576 cmp.bv_val = &cmp.bv_val[ offset ];
1577 cmp.bv_len -= offset;
1580 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1587 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1588 if ( !op->o_conn->c_peer_name.bv_val ) {
1591 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1592 b->a_peername_pat.bv_val, 0, 0 );
1593 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1594 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1595 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1596 e->e_ndn, nmatch, matches ) )
1602 /* try exact match */
1603 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1604 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1608 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1610 char buf[ACL_BUF_SIZE];
1612 bv.bv_len = sizeof( buf ) - 1;
1614 if ( string_expand( &bv, &b->a_peername_pat,
1615 e->e_ndn, nmatch, matches ) )
1620 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1624 /* extract IP and try exact match */
1625 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1627 char buf[] = "255.255.255.255";
1630 int port_number = -1;
1632 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1633 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1636 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1637 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1639 port = strrchr( ip.bv_val, ':' );
1643 ip.bv_len = port - ip.bv_val;
1645 port_number = strtol( port, &next, 10 );
1646 if ( next[0] != '\0' )
1650 /* the port check can be anticipated here */
1651 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1654 /* address longer than expected? */
1655 if ( ip.bv_len >= sizeof(buf) )
1658 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1659 buf[ ip.bv_len ] = '\0';
1661 addr = inet_addr( buf );
1663 /* unable to convert? */
1664 if ( addr == (unsigned long)(-1) )
1667 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1670 #ifdef LDAP_PF_LOCAL
1671 /* extract path and try exact match */
1672 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1675 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1676 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1679 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1680 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1682 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1685 #endif /* LDAP_PF_LOCAL */
1687 /* exact match (very unlikely...) */
1688 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1695 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1696 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1699 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1700 b->a_sockname_pat.bv_val, 0, 0 );
1701 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1702 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1703 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1704 e->e_ndn, nmatch, matches ) )
1709 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1711 char buf[ACL_BUF_SIZE];
1713 bv.bv_len = sizeof( buf ) - 1;
1715 if ( string_expand( &bv, &b->a_sockname_pat,
1716 e->e_ndn, nmatch, matches ) )
1721 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1726 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1733 if ( b->a_dn_at != NULL ) {
1734 if ( acl_mask_dnattr( op, e, val, a, b, i,
1735 matches, count, state,
1736 &b->a_dn, &op->o_ndn ) )
1742 if ( b->a_realdn_at != NULL ) {
1745 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1747 ndn = op->o_conn->c_ndn;
1752 if ( acl_mask_dnattr( op, e, val, a, b, i,
1753 matches, count, state,
1754 &b->a_realdn, &ndn ) )
1760 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1762 struct berval ndn = BER_BVNULL;
1765 if ( op->o_ndn.bv_len == 0 ) {
1769 /* b->a_group is an unexpanded entry name, expanded it should be an
1770 * entry with objectclass group* and we test to see if odn is one of
1771 * the values in the attribute group
1773 /* see if asker is listed in dnattr */
1774 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1775 char buf[ACL_BUF_SIZE];
1777 regmatch_t tmp_matches[2],
1778 *tmp_matchesp = tmp_matches;
1780 bv.bv_len = sizeof(buf) - 1;
1785 switch ( a->acl_dn_style ) {
1786 case ACL_STYLE_REGEX:
1787 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1788 tmp_matchesp = matches;
1789 tmp_nmatch = nmatch;
1793 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1794 case ACL_STYLE_BASE:
1795 tmp_matches[0].rm_so = 0;
1796 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1801 case ACL_STYLE_SUBTREE:
1802 case ACL_STYLE_CHILDREN:
1803 tmp_matches[0].rm_so = 0;
1804 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1805 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1806 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1820 if ( string_expand( &bv, &b->a_group_pat,
1822 tmp_nmatch, tmp_matchesp ) )
1827 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1828 op->o_tmpmemctx ) != LDAP_SUCCESS )
1830 /* did not expand to a valid dn */
1837 bv = b->a_group_pat;
1840 rc = backend_group( op, e, &bv, &op->o_ndn,
1841 b->a_group_oc, b->a_group_at );
1844 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1852 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1854 char buf[ACL_BUF_SIZE];
1856 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1858 regmatch_t tmp_matches[2],
1859 *tmp_matchesp = tmp_matches;
1862 bv.bv_len = sizeof( buf ) - 1;
1867 switch ( a->acl_dn_style ) {
1868 case ACL_STYLE_REGEX:
1869 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1870 tmp_matchesp = matches;
1871 tmp_nmatch = nmatch;
1875 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1876 case ACL_STYLE_BASE:
1877 tmp_matches[0].rm_so = 0;
1878 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1883 case ACL_STYLE_SUBTREE:
1884 case ACL_STYLE_CHILDREN:
1885 tmp_matches[0].rm_so = 0;
1886 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1887 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1888 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1902 if ( string_expand( &bv, &b->a_set_pat,
1904 tmp_nmatch, tmp_matchesp ) )
1913 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1918 if ( b->a_authz.sai_ssf ) {
1919 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1920 b->a_authz.sai_ssf, op->o_ssf, 0 );
1921 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1926 if ( b->a_authz.sai_transport_ssf ) {
1927 Debug( LDAP_DEBUG_ACL,
1928 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1929 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1930 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1935 if ( b->a_authz.sai_tls_ssf ) {
1936 Debug( LDAP_DEBUG_ACL,
1937 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1938 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1939 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1944 if ( b->a_authz.sai_sasl_ssf ) {
1945 Debug( LDAP_DEBUG_ACL,
1946 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1947 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1948 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1954 if ( b->a_dynacl ) {
1956 slap_access_t tgrant, tdeny;
1958 /* this case works different from the others above.
1959 * since aci's themselves give permissions, we need
1960 * to first check b->a_access_mask, the ACL's access level.
1962 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1963 /* no ACIs in the root DSE */
1967 /* first check if the right being requested
1968 * is allowed by the ACL clause.
1970 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1974 /* start out with nothing granted, nothing denied */
1978 for ( da = b->a_dynacl; da; da = da->da_next ) {
1979 slap_access_t grant, deny;
1981 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1987 /* remove anything that the ACL clause does not allow */
1988 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1989 tdeny &= ACL_PRIV_MASK;
1991 /* see if we have anything to contribute */
1992 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1996 /* this could be improved by changing slap_acl_mask so that it can deal with
1997 * by clauses that return grant/deny pairs. Right now, it does either
1998 * additive or subtractive rights, but not both at the same time. So,
1999 * we need to combine the grant/deny pair into a single rights mask in
2000 * a smart way: if either grant or deny is "empty", then we use the
2001 * opposite as is, otherwise we remove any denied rights from the grant
2002 * rights mask and construct an additive mask.
2004 if (ACL_IS_INVALID(tdeny)) {
2005 modmask = tgrant | ACL_PRIV_ADDITIVE;
2007 } else if (ACL_IS_INVALID(tgrant)) {
2008 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2011 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2015 #else /* !SLAP_DYNACL */
2017 #ifdef SLAPD_ACI_ENABLED
2018 if ( b->a_aci_at != NULL ) {
2020 slap_access_t grant, deny, tgrant, tdeny;
2021 struct berval parent_ndn;
2022 BerVarray bvals = NULL;
2025 /* this case works different from the others above.
2026 * since aci's themselves give permissions, we need
2027 * to first check b->a_access_mask, the ACL's access level.
2030 if ( BER_BVISEMPTY( &e->e_nname ) ) {
2031 /* no ACIs in the root DSE */
2035 /* first check if the right being requested
2036 * is allowed by the ACL clause.
2038 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2041 /* start out with nothing granted, nothing denied */
2045 /* get the aci attribute */
2046 at = attr_find( e->e_attrs, b->a_aci_at );
2049 /* FIXME: this breaks acl caching;
2050 * see also ACL_RECORD_VALUE_STATE below */
2051 ACL_RECORD_VALUE_STATE;
2053 /* the aci is an multi-valued attribute. The
2054 * rights are determined by OR'ing the individual
2055 * rights given by the acis.
2057 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2062 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2068 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2069 accessmask2str(tgrant, accessmaskbuf, 1),
2070 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2073 /* If the entry level aci didn't contain anything valid for the
2074 * current operation, climb up the tree and evaluate the
2075 * acis with scope set to subtree
2077 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2078 dnParent( &e->e_nname, &parent_ndn );
2079 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2080 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2081 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2089 for( i = 0; bvals[i].bv_val != NULL; i++){
2091 /* FIXME: this breaks acl caching;
2092 * see also ACL_RECORD_VALUE_STATE above */
2093 ACL_RECORD_VALUE_STATE;
2095 if (aci_mask(op, e, desc, val, &bvals[i],
2097 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2101 /* evaluation stops as soon as either a "deny" or a
2102 * "grant" directive matches.
2104 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2108 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2109 accessmask2str(tgrant, accessmaskbuf, 1),
2110 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2114 case LDAP_NO_SUCH_ATTRIBUTE:
2115 /* just go on if the aci-Attribute is not present in
2118 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2122 case LDAP_NO_SUCH_OBJECT:
2123 /* We have reached the base object */
2124 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2135 dnParent( &parent_ndn, &parent_ndn );
2140 /* remove anything that the ACL clause does not allow */
2141 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2142 tdeny &= ACL_PRIV_MASK;
2144 /* see if we have anything to contribute */
2145 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2149 /* this could be improved by changing slap_acl_mask so that it can deal with
2150 * by clauses that return grant/deny pairs. Right now, it does either
2151 * additive or subtractive rights, but not both at the same time. So,
2152 * we need to combine the grant/deny pair into a single rights mask in
2153 * a smart way: if either grant or deny is "empty", then we use the
2154 * opposite as is, otherwise we remove any denied rights from the grant
2155 * rights mask and construct an additive mask.
2157 if (ACL_IS_INVALID(tdeny)) {
2158 modmask = tgrant | ACL_PRIV_ADDITIVE;
2160 } else if (ACL_IS_INVALID(tgrant)) {
2161 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2164 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2168 #endif /* SLAPD_ACI_ENABLED */
2169 #endif /* !SLAP_DYNACL */
2171 modmask = b->a_access_mask;
2174 Debug( LDAP_DEBUG_ACL,
2175 "<= acl_mask: [%d] applying %s (%s)\n",
2176 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2177 b->a_type == ACL_CONTINUE
2179 : b->a_type == ACL_BREAK
2185 if( ACL_IS_ADDITIVE(modmask) ) {
2187 ACL_PRIV_SET( *mask, modmask );
2190 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2192 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2193 /* substract privs */
2194 ACL_PRIV_CLR( *mask, modmask );
2197 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2204 Debug( LDAP_DEBUG_ACL,
2205 "<= acl_mask: [%d] mask: %s\n",
2206 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2208 if( b->a_type == ACL_CONTINUE ) {
2211 } else if ( b->a_type == ACL_BREAK ) {
2219 /* implicit "by * none" clause */
2222 Debug( LDAP_DEBUG_ACL,
2223 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2224 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2229 * acl_check_modlist - check access control on the given entry to see if
2230 * it allows the given modifications by the user associated with op.
2231 * returns 1 if mods allowed ok
2232 * 0 mods not allowed
2239 Modifications *mlist
2243 AccessControlState state = ACL_STATE_INIT;
2246 int ret = 1; /* default is access allowed */
2250 be = LDAP_STAILQ_FIRST(&backendDB);
2254 assert( be != NULL );
2256 /* short circuit root database access */
2257 if ( be_isroot( op ) ) {
2258 Debug( LDAP_DEBUG_ACL,
2259 "<= acl_access_allowed: granted to database root\n",
2264 /* use backend default access if no backend acls */
2265 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2266 Debug( LDAP_DEBUG_ACL,
2267 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2268 access2str( ACL_WRITE ),
2269 op->o_bd->be_dfltaccess >= ACL_WRITE
2270 ? "granted" : "denied",
2272 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2276 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2278 * Internal mods are ignored by ACL_WRITE checking
2280 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2281 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2282 " modify access granted\n",
2283 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2288 * no-user-modification operational attributes are ignored
2289 * by ACL_WRITE checking as any found here are not provided
2292 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2293 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2294 " modify access granted\n",
2295 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2299 switch ( mlist->sml_op ) {
2300 case LDAP_MOD_REPLACE:
2302 * We must check both permission to delete the whole
2303 * attribute and permission to add the specific attributes.
2304 * This prevents abuse from selfwriters.
2306 if ( ! access_allowed( op, e,
2307 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2313 if ( mlist->sml_values == NULL ) break;
2315 /* fall thru to check value to add */
2318 assert( mlist->sml_values != NULL );
2320 for ( bv = mlist->sml_nvalues
2321 ? mlist->sml_nvalues : mlist->sml_values;
2322 bv->bv_val != NULL; bv++ )
2324 if ( ! access_allowed( op, e,
2325 mlist->sml_desc, bv, ACL_WADD, &state ) )
2333 case LDAP_MOD_DELETE:
2334 if ( mlist->sml_values == NULL ) {
2335 if ( ! access_allowed( op, e,
2336 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2343 for ( bv = mlist->sml_nvalues
2344 ? mlist->sml_nvalues : mlist->sml_values;
2345 bv->bv_val != NULL; bv++ )
2347 if ( ! access_allowed( op, e,
2348 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2356 case SLAP_MOD_SOFTADD:
2357 /* allow adding attribute via modrdn thru */
2369 if (be_null) op->o_bd = NULL;
2375 struct berval *list,
2388 while ( len >= 0 && --ix >= 0 ) {
2389 while ( --len >= 0 && *p++ != sep )
2392 while ( len >= 0 && *p == ' ' ) {
2405 while ( --len >= 0 && *p != sep ) {
2409 while ( bv->bv_len > 0 && *--p == ' ' ) {
2416 typedef struct aci_set_gather_t {
2422 aci_set_cb_gather( Operation *op, SlapReply *rs )
2424 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2426 if ( rs->sr_type == REP_SEARCH ) {
2427 BerValue bvals[ 2 ];
2428 BerVarray bvalsp = NULL;
2431 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2432 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2434 if ( desc == slap_schema.si_ad_entryDN ) {
2436 bvals[ 0 ] = rs->sr_entry->e_nname;
2437 BER_BVZERO( &bvals[ 1 ] );
2442 a = attr_find( rs->sr_entry->e_attrs, desc );
2446 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2449 bvalsp = a->a_nvals;
2455 p->bvals = slap_set_join( p->cookie, p->bvals,
2456 ( '|' | SLAP_SET_RREF ), bvalsp );
2460 assert( rs->sr_type == REP_RESULT );
2467 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2469 AciSetCookie *cp = (AciSetCookie *)cookie;
2471 LDAPURLDesc *ludp = NULL;
2472 Operation op2 = { 0 };
2473 SlapReply rs = {REP_RESULT};
2474 AttributeName anlist[ 2 ], *anlistp = NULL;
2476 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2477 aci_set_gather_t p = { 0 };
2478 const char *text = NULL;
2479 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2481 /* this routine needs to return the bervals instead of
2482 * plain strings, since syntax is not known. It should
2483 * also return the syntax or some "comparison cookie".
2485 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2486 return aci_set_gather2( cookie, name, desc );
2489 rc = ldap_url_parse( name->bv_val, &ludp );
2490 if ( rc != LDAP_URL_SUCCESS ) {
2491 rc = LDAP_PROTOCOL_ERROR;
2495 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2497 /* host part must be empty */
2498 /* extensions parts must be empty */
2499 rc = LDAP_PROTOCOL_ERROR;
2503 /* Grab the searchbase and see if an appropriate database can be found */
2504 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2505 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2506 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2507 BER_BVZERO( &op2.o_req_dn );
2508 if ( rc != LDAP_SUCCESS ) {
2512 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2513 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2514 rc = LDAP_NO_SUCH_OBJECT;
2518 /* Grab the filter */
2519 if ( ludp->lud_filter ) {
2520 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2521 cp->op->o_tmpmemctx );
2524 op2.ors_filterstr = defaultFilter_bv;
2527 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2528 if ( op2.ors_filter == NULL ) {
2529 rc = LDAP_PROTOCOL_ERROR;
2533 /* Grab the scope */
2534 op2.ors_scope = ludp->lud_scope;
2536 /* Grap the attributes */
2537 if ( ludp->lud_attrs ) {
2538 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2541 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2542 cp->op->o_tmpmemctx );
2544 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2545 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2546 anlistp[ nattrs ].an_desc = NULL;
2547 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2548 &anlistp[ nattrs ].an_desc, &text );
2549 if ( rc != LDAP_SUCCESS ) {
2558 anlistp[ nattrs ].an_name = desc->ad_cname;
2559 anlistp[ nattrs ].an_desc = desc;
2561 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2565 op2.o_hdr = cp->op->o_hdr;
2566 op2.o_tag = LDAP_REQ_SEARCH;
2567 op2.o_ndn = op2.o_bd->be_rootndn;
2568 op2.o_callback = &cb;
2569 op2.o_time = slap_get_time();
2570 op2.o_do_not_cache = 1;
2571 op2.o_is_auth_check = 0;
2572 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2573 op2.ors_slimit = SLAP_NO_LIMIT;
2574 op2.ors_tlimit = SLAP_NO_LIMIT;
2575 op2.ors_attrs = anlistp;
2576 op2.ors_attrsonly = 0;
2577 op2.o_private = cp->op->o_private;
2581 rc = op2.o_bd->be_search( &op2, &rs );
2587 if ( op2.ors_filter ) {
2588 filter_free_x( cp->op, op2.ors_filter );
2590 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2591 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2593 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2594 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2597 ldap_free_urldesc( ludp );
2599 if ( anlistp && anlistp != anlist ) {
2600 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2607 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2609 AciSetCookie *cp = (AciSetCookie *)cookie;
2610 BerVarray bvals = NULL;
2614 /* this routine needs to return the bervals instead of
2615 * plain strings, since syntax is not known. It should
2616 * also return the syntax or some "comparison cookie".
2618 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2619 if ( rc == LDAP_SUCCESS ) {
2620 if ( desc == slap_schema.si_ad_entryDN ) {
2621 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2622 cp->op->o_tmpmemctx );
2624 BER_BVZERO( &bvals[ 1 ] );
2628 backend_attribute( cp->op,
2629 cp->e, &ndn, desc, &bvals, ACL_NONE );
2632 if ( !BER_BVISNULL( &ndn ) ) {
2633 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2642 struct berval *subj,
2648 struct berval set = BER_BVNULL;
2650 AciSetCookie cookie;
2652 if ( setref == 0 ) {
2653 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2656 struct berval subjdn, ndn = BER_BVNULL;
2657 struct berval setat;
2660 AttributeDescription *desc = NULL;
2662 /* format of string is "entry/setAttrName" */
2663 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2667 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2668 setat = aci_bv_set_attr;
2672 * NOTE: dnNormalize honors the ber_len field
2673 * as the length of the dn to be normalized
2675 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2676 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2678 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2679 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2683 BER_BVZERO( &bvals[0] );
2684 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2686 bvals[0].bv_val = bvals[i-1].bv_val;
2687 BER_BVZERO( &bvals[i-1] );
2689 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2690 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2695 if ( !BER_BVISNULL( &set ) ) {
2698 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2699 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2700 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2706 #ifdef SLAPD_ACI_ENABLED
2708 aci_list_map_rights(
2709 struct berval *list )
2716 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2719 switch (*bv.bv_val) {
2721 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2724 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2725 * the right 's' to mean "set", but in the examples states
2726 * that the right 's' means "search". The latter definition
2729 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2732 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2735 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2738 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2739 * define any equivalent to the AUTH right, so I've just used
2742 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2754 struct berval *list,
2755 const struct berval *attr,
2756 struct berval *val )
2758 struct berval bv, left, right;
2761 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2762 if (aci_get_part(&bv, 0, '=', &left) < 0
2763 || aci_get_part(&bv, 1, '=', &right) < 0)
2765 if (ber_bvstrcasecmp(attr, &bv) == 0)
2767 } else if (val == NULL) {
2768 if (ber_bvstrcasecmp(attr, &left) == 0)
2771 if (ber_bvstrcasecmp(attr, &left) == 0) {
2772 /* this is experimental code that implements a
2773 * simple (prefix) match of the attribute value.
2774 * the ACI draft does not provide for aci's that
2775 * apply to specific values, but it would be
2776 * nice to have. If the <attr> part of an aci's
2777 * rights list is of the form <attr>=<value>,
2778 * that means the aci applies only to attrs with
2779 * the given value. Furthermore, if the attr is
2780 * of the form <attr>=<value>*, then <value> is
2781 * treated as a prefix, and the aci applies to
2782 * any value with that prefix.
2784 * Ideally, this would allow r.e. matches.
2786 if (aci_get_part(&right, 0, '*', &left) < 0
2787 || right.bv_len <= left.bv_len)
2789 if (ber_bvstrcasecmp(val, &right) == 0)
2791 } else if (val->bv_len >= left.bv_len) {
2792 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2801 static slap_access_t
2802 aci_list_get_attr_rights(
2803 struct berval *list,
2804 const struct berval *attr,
2805 struct berval *val )
2811 /* loop through each rights/attr pair, skip first part (action) */
2813 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2814 if (aci_list_has_attr(&bv, attr, val) == 0)
2816 if (aci_get_part(list, i, ';', &bv) < 0)
2818 mask |= aci_list_map_rights(&bv);
2824 aci_list_get_rights(
2825 struct berval *list,
2826 const struct berval *attr,
2828 slap_access_t *grant,
2829 slap_access_t *deny )
2831 struct berval perm, actn;
2832 slap_access_t *mask;
2835 if (attr == NULL || attr->bv_len == 0
2836 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2837 attr = &aci_bv_br_entry;
2843 /* loop through each permissions clause */
2844 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2845 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2847 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2849 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2856 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2857 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2864 struct berval *subj,
2865 struct berval *defgrpoc,
2866 struct berval *defgrpat,
2873 struct berval subjdn;
2874 struct berval grpoc;
2875 struct berval grpat;
2876 ObjectClass *grp_oc = NULL;
2877 AttributeDescription *grp_ad = NULL;
2881 /* format of string is "group/objectClassValue/groupAttrName" */
2882 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2886 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2890 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2894 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2895 if( rc != LDAP_SUCCESS ) {
2901 grp_oc = oc_bvfind( &grpoc );
2903 if (grp_oc != NULL && grp_ad != NULL ) {
2904 char buf[ACL_BUF_SIZE];
2905 struct berval bv, ndn;
2906 bv.bv_len = sizeof( buf ) - 1;
2907 bv.bv_val = (char *)&buf;
2908 if ( string_expand(&bv, &subjdn,
2909 e->e_ndn, nmatch, matches) )
2914 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2915 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2916 grp_oc, grp_ad ) == 0 );
2917 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2929 AttributeDescription *desc,
2933 regmatch_t *matches,
2934 slap_access_t *grant,
2935 slap_access_t *deny,
2936 slap_aci_scope_t asserted_scope
2939 struct berval bv, scope, perms, type, sdn;
2943 assert( !BER_BVISNULL( &desc->ad_cname ) );
2945 /* parse an aci of the form:
2946 oid # scope # action;rights;attr;rights;attr
2947 $ action;rights;attr;rights;attr # type # subject
2949 [NOTE: the following comment is very outdated,
2950 as the draft version it refers to (Ando, 2004-11-20)].
2952 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2953 a full description of the format for this attribute.
2954 Differences: "this" in the draft is "self" here, and
2955 "self" and "public" is in the position of type.
2957 <scope> = {entry|children|subtree}
2958 <type> = {public|users|access-id|subtree|onelevel|children|
2959 self|dnattr|group|role|set|set-ref}
2961 This routine now supports scope={ENTRY,CHILDREN}
2963 - ENTRY applies to "entry" and "subtree";
2964 - CHILDREN aplies to "children" and "subtree"
2967 /* check that the aci has all 5 components */
2968 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2972 /* check that the aci family is supported */
2973 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2977 /* check that the scope matches */
2978 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2982 /* note: scope can be either ENTRY or CHILDREN;
2983 * they respectively match "entry" and "children" in bv
2984 * both match "subtree" */
2985 switch ( asserted_scope ) {
2986 case SLAP_ACI_SCOPE_ENTRY:
2987 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2988 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2994 case SLAP_ACI_SCOPE_CHILDREN:
2995 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2996 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
3006 /* get the list of permissions clauses, bail if empty */
3007 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
3011 /* check if any permissions allow desired access */
3012 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
3016 /* see if we have a DN match */
3017 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
3021 /* see if we have a public (i.e. anonymous) access */
3022 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
3026 /* otherwise require an identity */
3027 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3031 /* see if we have a users access */
3032 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3036 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3037 * just grab all the berval up to its end (ITS#3303).
3038 * NOTE: the problem could be solved by providing the DN with
3039 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3040 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3042 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3046 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3047 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3049 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3052 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3053 if ( rc != LDAP_SUCCESS ) {
3057 if ( dn_match( &op->o_ndn, &ndn ) ) {
3060 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3064 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3067 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3068 if ( rc != LDAP_SUCCESS ) {
3072 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3075 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3079 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3080 struct berval ndn, pndn;
3082 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3083 if ( rc != LDAP_SUCCESS ) {
3087 dnParent( &ndn, &pndn );
3089 if ( dn_match( &op->o_ndn, &pndn ) ) {
3092 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3096 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3099 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3100 if ( rc != LDAP_SUCCESS ) {
3104 if ( !dn_match( &op->o_ndn, &ndn )
3105 && dnIsSuffix( &op->o_ndn, &ndn ) )
3109 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3113 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3114 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3118 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3120 AttributeDescription *ad = NULL;
3123 rc = slap_bv2ad( &sdn, &ad, &text );
3125 if( rc != LDAP_SUCCESS ) {
3131 for ( at = attrs_find( e->e_attrs, ad );
3133 at = attrs_find( at->a_next, ad ) )
3135 if ( value_find_ex( ad,
3136 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3137 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3139 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3148 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3149 if ( aci_group_member( &sdn, &aci_bv_group_class,
3150 &aci_bv_group_attr, op, e, nmatch, matches ) )
3155 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3156 if ( aci_group_member( &sdn, &aci_bv_role_class,
3157 &aci_bv_role_attr, op, e, nmatch, matches ) )
3162 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3163 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3167 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3168 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3178 * FIXME: there is a silly dependence that makes it difficult
3179 * to move ACIs in a run-time loadable module under the "dynacl"
3180 * umbrella, because sets share some helpers with ACIs.
3183 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3185 AttributeDescription *ad = NULL;
3186 const char *text = NULL;
3188 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3189 fprintf( stderr, "%s: line %d: "
3190 "inappropriate style \"%s\" in \"aci\" by clause\n",
3191 fname, lineno, style_strings[sty] );
3195 if ( right != NULL && *right != '\0' ) {
3196 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3198 "%s: line %d: aci \"%s\": %s\n",
3199 fname, lineno, right, text );
3204 ad = slap_schema.si_ad_aci;
3207 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3208 fprintf( stderr, "%s: line %d: "
3209 "aci \"%s\": inappropriate syntax: %s\n",
3210 fname, lineno, right,
3211 ad->ad_type->sat_syntax_oid );
3215 *privp = (void *)ad;
3221 dynacl_aci_unparse( void *priv, struct berval *bv )
3223 AttributeDescription *ad = ( AttributeDescription * )priv;
3226 assert( ad != NULL );
3228 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3229 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3230 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3231 bv->bv_len = ptr - bv->bv_val;
3242 AttributeDescription *desc,
3245 regmatch_t *matches,
3246 slap_access_t *grantp,
3247 slap_access_t *denyp )
3249 AttributeDescription *ad = ( AttributeDescription * )priv;
3251 slap_access_t tgrant, tdeny, grant, deny;
3253 char accessmaskbuf[ACCESSMASK_MAXLEN];
3254 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3255 #endif /* LDAP_DEBUG */
3257 /* start out with nothing granted, nothing denied */
3261 /* get the aci attribute */
3262 at = attr_find( e->e_attrs, ad );
3266 /* the aci is an multi-valued attribute. The
3267 * rights are determined by OR'ing the individual
3268 * rights given by the acis.
3270 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3271 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3272 nmatch, matches, &grant, &deny,
3273 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3280 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3281 accessmask2str( tgrant, accessmaskbuf, 1 ),
3282 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3285 /* If the entry level aci didn't contain anything valid for the
3286 * current operation, climb up the tree and evaluate the
3287 * acis with scope set to subtree
3289 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3290 struct berval parent_ndn;
3293 /* to solve the chicken'n'egg problem of accessing
3294 * the OpenLDAPaci attribute, the direct access
3295 * to the entry's attribute is unchecked; however,
3296 * further accesses to OpenLDAPaci values in the
3297 * ancestors occur through backend_attribute(), i.e.
3298 * with the identity of the operation, requiring
3299 * further access checking. For uniformity, this
3300 * makes further requests occur as the rootdn, if
3301 * any, i.e. searching for the OpenLDAPaci attribute
3302 * is considered an internal search. If this is not
3303 * acceptable, then the same check needs be performed
3304 * when accessing the entry's attribute. */
3305 Operation op2 = *op;
3307 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3308 op2.o_dn = op->o_bd->be_rootdn;
3309 op2.o_ndn = op->o_bd->be_rootndn;
3313 dnParent( &e->e_nname, &parent_ndn );
3314 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3316 BerVarray bvals = NULL;
3319 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3320 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3329 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3330 if ( aci_mask( op, e, desc, val,
3334 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3338 /* evaluation stops as soon as either a "deny" or a
3339 * "grant" directive matches.
3341 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3345 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3346 accessmask2str( tgrant, accessmaskbuf, 1 ),
3347 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3351 case LDAP_NO_SUCH_ATTRIBUTE:
3352 /* just go on if the aci-Attribute is not present in
3355 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3359 case LDAP_NO_SUCH_OBJECT:
3360 /* We have reached the base object */
3361 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3373 dnParent( &parent_ndn, &parent_ndn );
3383 /* need to register this at some point */
3384 static slap_dynacl_t dynacl_aci = {
3394 #endif /* SLAP_DYNACL */
3396 #endif /* SLAPD_ACI_ENABLED */
3401 * dynamic ACL infrastructure
3403 static slap_dynacl_t *da_list = NULL;
3406 slap_dynacl_register( slap_dynacl_t *da )
3410 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3411 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3416 if ( tmp != NULL ) {
3420 if ( da->da_mask == NULL ) {
3424 da->da_private = NULL;
3425 da->da_next = da_list;
3431 static slap_dynacl_t *
3432 slap_dynacl_next( slap_dynacl_t *da )
3441 slap_dynacl_get( const char *name )
3445 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3446 if ( strcasecmp( da->da_name, name ) == 0 ) {
3453 #endif /* SLAP_DYNACL */
3460 slap_dynacl_t *known_dynacl[] = {
3461 #ifdef SLAPD_ACI_ENABLED
3463 #endif /* SLAPD_ACI_ENABLED */
3467 for ( i = 0; known_dynacl[ i ]; i++ ) {
3468 rc = slap_dynacl_register( known_dynacl[ i ] );
3473 #endif /* SLAP_DYNACL */
3484 regmatch_t *matches)
3492 bv->bv_val[0] = '\0';
3493 bv->bv_len--; /* leave space for lone $ */
3496 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3497 sp < pat->bv_val + pat->bv_len ; sp++ )
3499 /* did we previously see a $ */
3501 if ( flag == 1 && *sp == '$' ) {
3506 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3509 } else if ( *sp >= '0' && *sp <= '9' ) {
3517 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3518 if ( *sp >= '0' && *sp <= '9' ) {
3519 n = 10*n + ( *sp - '0' );
3523 if ( *sp != /*'{'*/ '}' ) {
3529 if ( n >= nmatch ) {
3535 i = matches[n].rm_so;
3536 l = matches[n].rm_eo;
3537 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3555 /* must have ended with a single $ */
3563 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3564 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3571 struct berval *pat, /* pattern to expand and match against */
3572 char *str, /* string to match against pattern */
3573 char *buf, /* buffer with $N expansion variables */
3574 int nmatch, /* size of the matches array */
3575 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3579 char newbuf[ACL_BUF_SIZE];
3583 bv.bv_len = sizeof( newbuf ) - 1;
3590 string_expand( &bv, pat, buf, nmatch, matches );
3591 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3593 char error[ACL_BUF_SIZE];
3594 regerror( rc, &re, error, sizeof( error ) );
3596 Debug( LDAP_DEBUG_TRACE,
3597 "compile( \"%s\", \"%s\") failed %s\n",
3598 pat->bv_val, str, error );
3602 rc = regexec( &re, str, 0, NULL, 0 );
3605 Debug( LDAP_DEBUG_TRACE,
3606 "=> regex_matches: string: %s\n", str, 0, 0 );
3607 Debug( LDAP_DEBUG_TRACE,
3608 "=> regex_matches: rc: %d %s\n",
3609 rc, !rc ? "matches" : "no matches", 0 );