1 /* acl.c - routines to parse and check acl's */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
41 #include "slapi/slapi.h"
42 #endif /* LDAPI_SLAPI */
44 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
50 aci_bv_entry = BER_BVC("entry"),
51 aci_bv_children = BER_BVC("children"),
52 aci_bv_onelevel = BER_BVC("onelevel"),
53 aci_bv_subtree = BER_BVC("subtree"),
54 aci_bv_br_entry = BER_BVC("[entry]"),
55 aci_bv_br_all = BER_BVC("[all]"),
56 aci_bv_access_id = BER_BVC("access-id"),
58 aci_bv_anonymous = BER_BVC("anonymous"),
60 aci_bv_public = BER_BVC("public"),
61 aci_bv_users = BER_BVC("users"),
62 aci_bv_self = BER_BVC("self"),
63 aci_bv_dnattr = BER_BVC("dnattr"),
64 aci_bv_group = BER_BVC("group"),
65 aci_bv_role = BER_BVC("role"),
66 aci_bv_set = BER_BVC("set"),
67 aci_bv_set_ref = BER_BVC("set-ref"),
68 aci_bv_grant = BER_BVC("grant"),
69 aci_bv_deny = BER_BVC("deny"),
71 aci_bv_ip_eq = BER_BVC("IP="),
73 aci_bv_path_eq = BER_BVC("PATH="),
75 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
77 #endif /* LDAP_PF_LOCAL */
79 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
80 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
81 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
82 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
83 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
85 typedef enum slap_aci_scope_t {
86 SLAP_ACI_SCOPE_ENTRY = 0x1,
87 SLAP_ACI_SCOPE_CHILDREN = 0x2,
88 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
91 static AccessControl * acl_get(
92 AccessControl *ac, int *count,
93 Operation *op, Entry *e,
94 AttributeDescription *desc,
96 int nmatch, regmatch_t *matches,
97 AccessControlState *state );
99 static slap_control_t acl_mask(
100 AccessControl *ac, slap_mask_t *mask,
101 Operation *op, Entry *e,
102 AttributeDescription *desc,
107 AccessControlState *state );
109 #ifdef SLAPD_ACI_ENABLED
111 Operation *op, Entry *e,
112 AttributeDescription *desc,
117 slap_access_t *grant,
119 slap_aci_scope_t scope);
120 #endif /* SLAPD_ACI_ENABLED */
122 static int regex_matches(
123 struct berval *pat, char *str, char *buf,
124 int nmatch, regmatch_t *matches);
125 static int string_expand(
126 struct berval *newbuf, struct berval *pattern,
127 char *match, int nmatch, regmatch_t *matches);
129 typedef struct AciSetCookie {
134 SLAP_SET_GATHER aci_set_gather;
135 SLAP_SET_GATHER aci_set_gather2;
136 static int aci_match_set ( struct berval *subj, Operation *op,
137 Entry *e, int setref );
140 * access_allowed - check whether op->o_ndn is allowed the requested access
141 * to entry e, attribute attr, value val. if val is null, access to
142 * the whole attribute is assumed (all values).
144 * This routine loops through all access controls and calls
145 * acl_mask() on each applicable access control.
146 * The loop exits when a definitive answer is reached or
147 * or no more controls remain.
154 * - can be legally called with op == NULL
155 * - can be legally called with op->o_bd == NULL
158 #ifdef SLAP_OVERLAY_ACCESS
160 slap_access_always_allowed(
163 AttributeDescription *desc,
165 slap_access_t access,
166 AccessControlState *state,
171 ACL_PRIV_SET( *maskp, ACL_ACCESS2PRIV( access ) );
180 AttributeDescription *desc,
182 slap_access_t access,
183 AccessControlState *state,
188 AccessControl *a = NULL;
191 char accessmaskbuf[ACCESSMASK_MAXLEN];
194 slap_control_t control;
195 slap_access_t access_level;
197 regmatch_t matches[MAXREMATCHES];
198 int st_same_attr = 0;
200 assert( op != NULL );
202 assert( desc != NULL );
203 assert( maskp != NULL );
205 access_level = ACL_LEVEL( access );
206 attr = desc->ad_cname.bv_val;
208 assert( attr != NULL );
211 if ( op->o_pb != NULL ) {
212 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
214 /* ACL plugin denied access */
218 #endif /* LDAP_SLAPI */
220 /* grant database root access */
221 if ( be_isroot( op ) ) {
222 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
223 mask = ACL_LVL_MANAGE;
228 * no-user-modification operational attributes are ignored
229 * by ACL_WRITE checking as any found here are not provided
232 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
233 && desc != slap_schema.si_ad_entry
234 && desc != slap_schema.si_ad_children )
236 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
237 " %s access granted\n",
242 /* use backend default access if no backend acls */
243 if ( op->o_bd->be_acl == NULL ) {
246 Debug( LDAP_DEBUG_ACL,
247 "=> slap_access_allowed: backend default %s "
248 "access %s to \"%s\"\n",
249 access2str( access ),
250 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
251 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
252 ret = op->o_bd->be_dfltaccess >= access_level;
254 mask = ACL_PRIV_LEVEL;
255 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
256 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
265 if ( st_same_attr ) {
266 assert( state->as_vd_acl != NULL );
268 a = state->as_vd_acl;
269 count = state->as_vd_acl_count;
270 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
271 mask = state->as_vd_acl_mask;
272 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
277 if ( state ) state->as_vi_acl = NULL;
279 ACL_PRIV_ASSIGN( mask, *maskp );
281 memset( matches, '\0', sizeof( matches ) );
284 while ( ( a = acl_get( a, &count, op, e, desc, val,
285 MAXREMATCHES, matches, state ) ) != NULL )
289 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
290 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
291 (int)matches[i].rm_so, (int)matches[i].rm_eo );
292 if ( matches[i].rm_so <= matches[0].rm_eo ) {
294 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
295 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
298 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
302 if ( state->as_vi_acl == a &&
303 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
305 Debug( LDAP_DEBUG_ACL,
306 "slap_access_allowed: result from state (%s)\n",
308 ret = state->as_result;
311 Debug( LDAP_DEBUG_ACL,
312 "slap_access_allowed: no res from state (%s)\n",
318 control = acl_mask( a, &mask, op,
319 e, desc, val, MAXREMATCHES, matches, count, state );
321 if ( control != ACL_BREAK ) {
325 memset( matches, '\0', sizeof( matches ) );
328 if ( ACL_IS_INVALID( mask ) ) {
329 Debug( LDAP_DEBUG_ACL,
330 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
332 ACL_PRIV_ASSIGN( mask, *maskp );
334 } else if ( control == ACL_BREAK ) {
335 Debug( LDAP_DEBUG_ACL,
336 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
341 ret = ACL_GRANT( mask, access );
343 Debug( LDAP_DEBUG_ACL,
344 "=> slap_access_allowed: %s access %s by %s\n",
345 access2str( access ), ret ? "granted" : "denied",
346 accessmask2str( mask, accessmaskbuf, 1 ) );
349 ACL_PRIV_ASSIGN( *maskp, mask );
357 AttributeDescription *desc,
359 slap_access_t access,
360 AccessControlState *state,
364 AccessControl *a = NULL;
368 char accessmaskbuf[ACCESSMASK_MAXLEN];
371 slap_control_t control;
372 slap_access_t access_level;
374 int st_same_attr = 0;
375 static AccessControlState state_init = ACL_STATE_INIT;
376 BI_access_allowed *bi_access_allowed = NULL;
379 assert( desc != NULL );
381 access_level = ACL_LEVEL( access );
383 assert( access_level > ACL_NONE );
386 if ( maskp ) ACL_INVALIDATE( *maskp );
388 attr = desc->ad_cname.bv_val;
390 assert( attr != NULL );
392 if ( op && op->o_is_auth_check &&
393 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
399 if ( state->as_vd_ad == desc ) {
400 if ( state->as_recorded ) {
401 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
404 return state->as_result;
406 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
407 val != NULL && state->as_vd_acl == NULL )
409 return state->as_result;
417 state->as_vd_ad = desc;
420 Debug( LDAP_DEBUG_ACL,
421 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
422 access2str( access ), e->e_dn, attr );
429 if ( op->o_bd == NULL ) {
430 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
435 * FIXME: experimental; use first backend rules
436 * iff there is no global_acl (ITS#3100) */
437 if ( frontendDB->be_acl != NULL ) {
438 op->o_bd = frontendDB;
440 #endif /* LDAP_DEVEL */
442 assert( op->o_bd != NULL );
444 /* delegate to backend */
445 if ( op->o_bd->bd_info->bi_access_allowed != NULL ) {
446 bi_access_allowed = op->o_bd->bd_info->bi_access_allowed;
448 bi_access_allowed = slap_access_allowed;
450 ret = bi_access_allowed( op, e, desc, val, access, state, &mask );
452 if ( ACL_IS_INVALID( mask ) ) {
453 Debug( LDAP_DEBUG_ACL,
454 "=> access_allowed: \"%s\" (%s) invalid!\n",
458 } else if ( control == ACL_BREAK ) {
459 Debug( LDAP_DEBUG_ACL,
460 "=> access_allowed: no more rules\n", 0, 0, 0 );
465 ret = ACL_GRANT( mask, access );
468 Debug( LDAP_DEBUG_ACL,
469 "=> access_allowed: %s access %s by %s\n",
470 access2str( access ), ret ? "granted" : "denied",
471 accessmask2str( mask, accessmaskbuf, 1 ) );
474 if ( state != NULL ) {
475 /* If not value-dependent, save ACL in case of more attrs */
476 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
477 state->as_vi_acl = a;
478 state->as_result = ret;
480 state->as_recorded |= ACL_STATE_RECORDED;
482 if ( be_null ) op->o_bd = NULL;
483 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
487 #else /* !SLAP_OVERLAY_ACCESS */
493 AttributeDescription *desc,
495 slap_access_t access,
496 AccessControlState *state,
501 AccessControl *a = NULL;
506 char accessmaskbuf[ACCESSMASK_MAXLEN];
509 slap_control_t control;
510 slap_access_t access_level;
512 regmatch_t matches[MAXREMATCHES];
513 int st_same_attr = 0;
514 static AccessControlState state_init = ACL_STATE_INIT;
517 assert( desc != NULL );
519 access_level = ACL_LEVEL( access );
521 assert( access_level > ACL_NONE );
522 if ( maskp ) ACL_INVALIDATE( *maskp );
524 attr = desc->ad_cname.bv_val;
526 assert( attr != NULL );
528 if ( op && op->o_is_auth_check &&
529 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
535 if ( state->as_vd_ad == desc ) {
536 if ( state->as_recorded ) {
537 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
540 return state->as_result;
542 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
543 val != NULL && state->as_vd_acl == NULL )
545 return state->as_result;
553 state->as_vd_ad=desc;
556 Debug( LDAP_DEBUG_ACL,
557 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
558 access2str( access ), e->e_dn, attr );
567 be = LDAP_STAILQ_FIRST(&backendDB);
571 * FIXME: experimental; use first backend rules
572 * iff there is no global_acl (ITS#3100) */
573 if ( frontendDB->be_acl == NULL )
579 assert( be != NULL );
582 if ( op->o_pb != NULL ) {
583 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
585 /* ACL plugin denied access */
589 #endif /* LDAP_SLAPI */
591 /* grant database root access */
592 if ( be_isroot( op ) ) {
593 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
595 mask = ACL_LVL_MANAGE;
602 * no-user-modification operational attributes are ignored
603 * by ACL_WRITE checking as any found here are not provided
606 if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
607 && desc != slap_schema.si_ad_entry
608 && desc != slap_schema.si_ad_children )
610 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
611 " %s access granted\n",
616 /* use backend default access if no backend acls */
617 if ( be->be_acl == NULL ) {
618 Debug( LDAP_DEBUG_ACL,
619 "=> access_allowed: backend default %s "
620 "access %s to \"%s\"\n",
621 access2str( access ),
622 be->be_dfltaccess >= access_level ? "granted" : "denied",
623 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
624 ret = be->be_dfltaccess >= access_level;
629 mask = ACL_PRIV_LEVEL;
630 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
631 mask |= ACL_ACCESS2PRIV( i );
638 /* be is always non-NULL */
639 /* use global default access if no global acls */
640 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
641 Debug( LDAP_DEBUG_ACL,
642 "=> access_allowed: global default %s access %s to \"%s\"\n",
643 access2str( access ),
644 frontendDB->be_dfltaccess >= access_level ?
645 "granted" : "denied", op->o_dn.bv_val );
646 ret = frontendDB->be_dfltaccess >= access_level;
651 mask = ACL_PRIV_LEVEL;
652 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
653 mask |= ACL_ACCESS2PRIV( i );
664 if ( st_same_attr ) {
665 assert( state->as_vd_acl != NULL );
667 a = state->as_vd_acl;
668 count = state->as_vd_acl_count;
669 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
670 mask = state->as_vd_acl_mask;
671 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
676 if ( state ) state->as_vi_acl = NULL;
680 memset( matches, '\0', sizeof(matches) );
683 while ( ( a = acl_get( a, &count, op, e, desc, val,
684 MAXREMATCHES, matches, state ) ) != NULL )
688 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
689 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
690 (int)matches[i].rm_so, (int)matches[i].rm_eo );
691 if ( matches[i].rm_so <= matches[0].rm_eo ) {
693 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
694 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
697 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
701 if ( state->as_vi_acl == a &&
702 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
704 Debug( LDAP_DEBUG_ACL,
705 "access_allowed: result from state (%s)\n",
707 ret = state->as_result;
710 Debug( LDAP_DEBUG_ACL,
711 "access_allowed: no res from state (%s)\n",
717 control = acl_mask( a, &mask, op,
718 e, desc, val, MAXREMATCHES, matches, count, state );
720 if ( control != ACL_BREAK ) {
724 memset( matches, '\0', sizeof(matches) );
727 if ( ACL_IS_INVALID( mask ) ) {
728 Debug( LDAP_DEBUG_ACL,
729 "=> access_allowed: \"%s\" (%s) invalid!\n",
733 } else if ( control == ACL_BREAK ) {
734 Debug( LDAP_DEBUG_ACL,
735 "=> access_allowed: no more rules\n", 0, 0, 0 );
740 Debug( LDAP_DEBUG_ACL,
741 "=> access_allowed: %s access %s by %s\n",
742 access2str( access ),
743 ACL_GRANT(mask, access) ? "granted" : "denied",
744 accessmask2str( mask, accessmaskbuf, 1 ) );
746 ret = ACL_GRANT(mask, access);
749 if ( state != NULL ) {
750 /* If not value-dependent, save ACL in case of more attrs */
751 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
752 state->as_vi_acl = a;
753 state->as_result = ret;
755 state->as_recorded |= ACL_STATE_RECORDED;
757 if ( be_null ) op->o_bd = NULL;
758 if ( maskp ) *maskp = mask;
762 #endif /* SLAP_OVERLAY_ACCESS */
765 * acl_get - return the acl applicable to entry e, attribute
766 * attr. the acl returned is suitable for use in subsequent calls to
767 * acl_access_allowed().
770 static AccessControl *
776 AttributeDescription *desc,
780 AccessControlState *state )
787 assert( count != NULL );
788 assert( desc != NULL );
790 attr = desc->ad_cname.bv_val;
792 assert( attr != NULL );
795 if( op->o_bd == NULL ) {
796 a = frontendDB->be_acl;
798 a = op->o_bd->be_acl;
809 dnlen = e->e_nname.bv_len;
811 for ( ; a != NULL; a = a->acl_next ) {
814 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
815 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
816 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
817 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
818 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
822 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
823 *count, a->acl_dn_pat.bv_val, 0 );
824 patlen = a->acl_dn_pat.bv_len;
825 if ( dnlen < patlen )
828 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
829 /* base dn -- entire object DN must match */
830 if ( dnlen != patlen )
833 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
834 int rdnlen = -1, sep = 0;
836 if ( dnlen <= patlen )
840 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
845 rdnlen = dn_rdnlen( NULL, &e->e_nname );
846 if ( rdnlen != dnlen - patlen - sep )
849 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
850 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
853 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
854 if ( dnlen <= patlen )
856 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
860 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
864 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
868 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
869 matches[0].rm_so = matches[0].rm_eo = -1;
873 /* Is this ACL only for a specific value? */
874 if ( a->acl_attrval.bv_len ) {
879 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
880 state->as_recorded |= ACL_STATE_RECORDED_VD;
881 state->as_vd_acl = prev;
882 state->as_vd_acl_count = *count;
883 state->as_vd_access = a->acl_access;
884 state->as_vd_access_count = 1;
885 ACL_INVALIDATE( state->as_vd_acl_mask );
888 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
889 Debug( LDAP_DEBUG_ACL,
890 "acl_get: valpat %s\n",
891 a->acl_attrval.bv_val, 0, 0 );
892 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
900 Debug( LDAP_DEBUG_ACL,
902 a->acl_attrval.bv_val, 0, 0 );
904 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
905 if (value_match( &match, desc,
906 desc->ad_type->sat_equality, 0,
907 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
914 patlen = a->acl_attrval.bv_len;
915 vdnlen = val->bv_len;
917 if ( vdnlen < patlen )
920 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
921 if ( vdnlen > patlen )
924 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
927 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
930 rdnlen = dn_rdnlen( NULL, val );
931 if ( rdnlen != vdnlen - patlen - 1 )
934 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
935 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
938 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
939 if ( vdnlen <= patlen )
942 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
946 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
952 if ( a->acl_filter != NULL ) {
953 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
954 if ( rc != LDAP_COMPARE_TRUE ) {
959 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
964 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
976 struct berval *opndn )
979 * if access applies to the entry itself, and the
980 * user is bound as somebody in the same namespace as
981 * the entry, OR the given dn matches the dn pattern
984 * NOTE: styles "anonymous", "users" and "self"
985 * have been moved to enum slap_style_t, whose
986 * value is set in a_dn_style; however, the string
987 * is maintaned in a_dn_pat.
989 if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
990 if ( !BER_BVISEMPTY( opndn ) ) {
994 } else if ( b->a_style == ACL_STYLE_USERS ) {
995 if ( BER_BVISEMPTY( opndn ) ) {
999 } else if ( b->a_style == ACL_STYLE_SELF ) {
1000 struct berval ndn, selfndn;
1003 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1007 level = b->a_self_level;
1015 selfndn = e->e_nname;
1018 for ( ; level > 0; level-- ) {
1019 if ( BER_BVISEMPTY( &ndn ) ) {
1022 dnParent( &ndn, &ndn );
1025 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1030 } else if ( b->a_style == ACL_STYLE_REGEX ) {
1031 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1033 regmatch_t tmp_matches[2],
1034 *tmp_matchesp = tmp_matches;
1038 switch ( a->acl_dn_style ) {
1039 case ACL_STYLE_REGEX:
1040 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1041 tmp_matchesp = matches;
1042 tmp_nmatch = nmatch;
1045 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1047 case ACL_STYLE_BASE:
1048 tmp_matches[0].rm_so = 0;
1049 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1054 case ACL_STYLE_SUBTREE:
1055 case ACL_STYLE_CHILDREN:
1056 tmp_matches[0].rm_so = 0;
1057 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1058 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1059 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1073 if ( !regex_matches( &b->a_pat, opndn->bv_val,
1074 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1082 ber_len_t patlen, odnlen;
1085 if ( e->e_dn == NULL )
1088 if ( b->a_expand ) {
1090 char buf[ACL_BUF_SIZE];
1093 regmatch_t tmp_matches[2],
1094 *tmp_matchesp = tmp_matches;
1098 bv.bv_len = sizeof( buf ) - 1;
1101 switch ( a->acl_dn_style ) {
1102 case ACL_STYLE_REGEX:
1103 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1104 tmp_matchesp = matches;
1105 tmp_nmatch = nmatch;
1108 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1110 case ACL_STYLE_BASE:
1111 tmp_matches[0].rm_so = 0;
1112 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1117 case ACL_STYLE_SUBTREE:
1118 case ACL_STYLE_CHILDREN:
1119 tmp_matches[0].rm_so = 0;
1120 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1121 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1122 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1136 if ( string_expand( &bv, &b->a_pat,
1138 tmp_nmatch, tmp_matchesp ) )
1143 if ( dnNormalize(0, NULL, NULL, &bv,
1144 &pat, op->o_tmpmemctx )
1147 /* did not expand to a valid dn */
1155 patlen = pat.bv_len;
1156 odnlen = opndn->bv_len;
1157 if ( odnlen < patlen ) {
1158 goto dn_match_cleanup;
1162 if ( b->a_style == ACL_STYLE_BASE ) {
1163 /* base dn -- entire object DN must match */
1164 if ( odnlen != patlen ) {
1165 goto dn_match_cleanup;
1168 } else if ( b->a_style == ACL_STYLE_ONE ) {
1171 if ( odnlen <= patlen ) {
1172 goto dn_match_cleanup;
1175 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1176 goto dn_match_cleanup;
1179 rdnlen = dn_rdnlen( NULL, opndn );
1180 if ( rdnlen != odnlen - patlen - 1 ) {
1181 goto dn_match_cleanup;
1184 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1185 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1186 goto dn_match_cleanup;
1189 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1190 if ( odnlen <= patlen ) {
1191 goto dn_match_cleanup;
1194 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1195 goto dn_match_cleanup;
1198 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1202 if ( odnlen <= patlen ) {
1203 goto dn_match_cleanup;
1206 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1208 goto dn_match_cleanup;
1213 for ( ; level > 0; level-- ) {
1214 if ( BER_BVISEMPTY( &ndn ) ) {
1215 goto dn_match_cleanup;
1217 dnParent( &ndn, &ndn );
1218 if ( ndn.bv_len < patlen ) {
1219 goto dn_match_cleanup;
1223 if ( ndn.bv_len != patlen ) {
1224 goto dn_match_cleanup;
1228 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1231 if ( pat.bv_val != b->a_pat.bv_val ) {
1232 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1244 * Record value-dependent access control state
1246 #define ACL_RECORD_VALUE_STATE do { \
1247 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1248 state->as_recorded |= ACL_STATE_RECORDED_VD; \
1249 state->as_vd_acl = a; \
1250 AC_MEMCPY( state->as_vd_acl_matches, matches, \
1251 sizeof( state->as_vd_acl_matches )) ; \
1252 state->as_vd_acl_count = count; \
1253 state->as_vd_access = b; \
1254 state->as_vd_access_count = i; \
1266 regmatch_t *matches,
1268 AccessControlState *state,
1269 slap_dn_access *bdn,
1270 struct berval *opndn )
1276 const char *attr = bdn->a_at->ad_cname.bv_val;
1278 assert( attr != NULL );
1280 if ( BER_BVISEMPTY( opndn ) ) {
1284 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1287 /* see if asker is listed in dnattr */
1288 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1290 at = attrs_find( at->a_next, bdn->a_at ) )
1292 if ( value_find_ex( bdn->a_at,
1293 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1294 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1296 &bv, op->o_tmpmemctx ) == 0 )
1305 /* have a dnattr match. if this is a self clause then
1306 * the target must also match the op dn.
1308 if ( bdn->a_self ) {
1309 /* check if the target is an attribute. */
1310 if ( val == NULL ) return 1;
1312 /* target is attribute, check if the attribute value
1315 rc = value_match( &match, bdn->a_at,
1316 bdn->a_at->ad_type->sat_equality, 0,
1318 /* on match error or no match, fail the ACL clause */
1319 if ( rc != LDAP_SUCCESS || match != 0 )
1324 /* no dnattr match, check if this is a self clause */
1325 if ( ! bdn->a_self )
1328 ACL_RECORD_VALUE_STATE;
1330 /* this is a self clause, check if the target is an
1336 /* target is attribute, check if the attribute value
1339 rc = value_match( &match, bdn->a_at,
1340 bdn->a_at->ad_type->sat_equality, 0,
1343 /* on match error or no match, fail the ACL clause */
1344 if ( rc != LDAP_SUCCESS || match != 0 )
1353 * acl_mask - modifies mask based upon the given acl and the
1354 * requested access to entry e, attribute attr, value val. if val
1355 * is null, access to the whole attribute is assumed (all values).
1357 * returns 0 access NOT allowed
1361 static slap_control_t
1367 AttributeDescription *desc,
1370 regmatch_t *matches,
1372 AccessControlState *state )
1377 char accessmaskbuf[ACCESSMASK_MAXLEN];
1378 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1379 char accessmaskbuf1[ACCESSMASK_MAXLEN];
1380 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1384 assert( a != NULL );
1385 assert( mask != NULL );
1386 assert( desc != NULL );
1388 attr = desc->ad_cname.bv_val;
1390 assert( attr != NULL );
1392 Debug( LDAP_DEBUG_ACL,
1393 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1396 Debug( LDAP_DEBUG_ACL,
1397 "=> acl_mask: to %s by \"%s\", (%s) \n",
1398 val ? "value" : "all values",
1399 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1400 accessmask2str( *mask, accessmaskbuf, 1 ) );
1403 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1404 && state->as_vd_acl == a )
1406 b = state->as_vd_access;
1407 i = state->as_vd_access_count;
1414 for ( ; b != NULL; b = b->a_next, i++ ) {
1415 slap_mask_t oldmask, modmask;
1417 ACL_INVALIDATE( modmask );
1419 /* AND <who> clauses */
1420 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1421 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1422 b->a_dn_pat.bv_val, 0, 0);
1424 * if access applies to the entry itself, and the
1425 * user is bound as somebody in the same namespace as
1426 * the entry, OR the given dn matches the dn pattern
1429 * NOTE: styles "anonymous", "users" and "self"
1430 * have been moved to enum slap_style_t, whose
1431 * value is set in a_dn_style; however, the string
1432 * is maintaned in a_dn_pat.
1435 if ( acl_mask_dn( op, e, a, nmatch, matches,
1436 &b->a_dn, &op->o_ndn ) )
1442 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1445 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1446 b->a_realdn_pat.bv_val, 0, 0);
1448 * if access applies to the entry itself, and the
1449 * user is bound as somebody in the same namespace as
1450 * the entry, OR the given dn matches the dn pattern
1453 * NOTE: styles "anonymous", "users" and "self"
1454 * have been moved to enum slap_style_t, whose
1455 * value is set in a_dn_style; however, the string
1456 * is maintaned in a_dn_pat.
1459 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1461 ndn = op->o_conn->c_ndn;
1466 if ( acl_mask_dn( op, e, a, nmatch, matches,
1467 &b->a_realdn, &ndn ) )
1473 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1474 if ( ! op->o_conn->c_listener ) {
1477 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1478 b->a_sockurl_pat.bv_val, 0, 0 );
1480 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1481 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1482 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1483 e->e_ndn, nmatch, matches ) )
1488 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1490 char buf[ACL_BUF_SIZE];
1492 bv.bv_len = sizeof( buf ) - 1;
1494 if ( string_expand( &bv, &b->a_sockurl_pat,
1495 e->e_ndn, nmatch, matches ) )
1500 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1506 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1514 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1515 if ( !op->o_conn->c_peer_domain.bv_val ) {
1518 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1519 b->a_domain_pat.bv_val, 0, 0 );
1520 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1521 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1522 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1523 e->e_ndn, nmatch, matches ) )
1528 char buf[ACL_BUF_SIZE];
1530 struct berval cmp = op->o_conn->c_peer_domain;
1531 struct berval pat = b->a_domain_pat;
1533 if ( b->a_domain_expand ) {
1536 bv.bv_len = sizeof(buf) - 1;
1539 if ( string_expand(&bv, &b->a_domain_pat,
1540 e->e_ndn, nmatch, matches) )
1547 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1548 int offset = cmp.bv_len - pat.bv_len;
1553 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1557 /* trim the domain */
1558 cmp.bv_val = &cmp.bv_val[ offset ];
1559 cmp.bv_len -= offset;
1562 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1569 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1570 if ( !op->o_conn->c_peer_name.bv_val ) {
1573 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1574 b->a_peername_pat.bv_val, 0, 0 );
1575 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1576 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1577 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1578 e->e_ndn, nmatch, matches ) )
1584 /* try exact match */
1585 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1586 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1590 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1592 char buf[ACL_BUF_SIZE];
1594 bv.bv_len = sizeof( buf ) - 1;
1596 if ( string_expand( &bv, &b->a_peername_pat,
1597 e->e_ndn, nmatch, matches ) )
1602 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1606 /* extract IP and try exact match */
1607 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1609 char buf[] = "255.255.255.255";
1612 int port_number = -1;
1614 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1615 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1618 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1619 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1621 port = strrchr( ip.bv_val, ':' );
1625 ip.bv_len = port - ip.bv_val;
1627 port_number = strtol( port, &next, 10 );
1628 if ( next[0] != '\0' )
1632 /* the port check can be anticipated here */
1633 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1636 /* address longer than expected? */
1637 if ( ip.bv_len >= sizeof(buf) )
1640 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1641 buf[ ip.bv_len ] = '\0';
1643 addr = inet_addr( buf );
1645 /* unable to convert? */
1646 if ( addr == (unsigned long)(-1) )
1649 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1652 #ifdef LDAP_PF_LOCAL
1653 /* extract path and try exact match */
1654 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1657 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1658 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1661 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1662 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1664 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1667 #endif /* LDAP_PF_LOCAL */
1669 /* exact match (very unlikely...) */
1670 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1677 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1678 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1681 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1682 b->a_sockname_pat.bv_val, 0, 0 );
1683 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1684 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1685 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1686 e->e_ndn, nmatch, matches ) )
1691 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1693 char buf[ACL_BUF_SIZE];
1695 bv.bv_len = sizeof( buf ) - 1;
1697 if ( string_expand( &bv, &b->a_sockname_pat,
1698 e->e_ndn, nmatch, matches ) )
1703 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1708 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1715 if ( b->a_dn_at != NULL ) {
1716 if ( acl_mask_dnattr( op, e, val, a, b, i,
1717 matches, count, state,
1718 &b->a_dn, &op->o_ndn ) )
1724 if ( b->a_realdn_at != NULL ) {
1727 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1729 ndn = op->o_conn->c_ndn;
1734 if ( acl_mask_dnattr( op, e, val, a, b, i,
1735 matches, count, state,
1736 &b->a_realdn, &ndn ) )
1742 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1744 struct berval ndn = BER_BVNULL;
1747 if ( op->o_ndn.bv_len == 0 ) {
1751 /* b->a_group is an unexpanded entry name, expanded it should be an
1752 * entry with objectclass group* and we test to see if odn is one of
1753 * the values in the attribute group
1755 /* see if asker is listed in dnattr */
1756 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1757 char buf[ACL_BUF_SIZE];
1759 regmatch_t tmp_matches[2],
1760 *tmp_matchesp = tmp_matches;
1762 bv.bv_len = sizeof(buf) - 1;
1767 switch ( a->acl_dn_style ) {
1768 case ACL_STYLE_REGEX:
1769 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1770 tmp_matchesp = matches;
1771 tmp_nmatch = nmatch;
1775 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1776 case ACL_STYLE_BASE:
1777 tmp_matches[0].rm_so = 0;
1778 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1783 case ACL_STYLE_SUBTREE:
1784 case ACL_STYLE_CHILDREN:
1785 tmp_matches[0].rm_so = 0;
1786 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1787 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1788 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1802 if ( string_expand( &bv, &b->a_group_pat,
1804 tmp_nmatch, tmp_matchesp ) )
1809 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1810 op->o_tmpmemctx ) != LDAP_SUCCESS )
1812 /* did not expand to a valid dn */
1819 bv = b->a_group_pat;
1822 rc = backend_group( op, e, &bv, &op->o_ndn,
1823 b->a_group_oc, b->a_group_at );
1826 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1834 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1836 char buf[ACL_BUF_SIZE];
1838 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1840 regmatch_t tmp_matches[2],
1841 *tmp_matchesp = tmp_matches;
1844 bv.bv_len = sizeof( buf ) - 1;
1849 switch ( a->acl_dn_style ) {
1850 case ACL_STYLE_REGEX:
1851 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1852 tmp_matchesp = matches;
1853 tmp_nmatch = nmatch;
1857 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1858 case ACL_STYLE_BASE:
1859 tmp_matches[0].rm_so = 0;
1860 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1865 case ACL_STYLE_SUBTREE:
1866 case ACL_STYLE_CHILDREN:
1867 tmp_matches[0].rm_so = 0;
1868 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1869 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1870 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1884 if ( string_expand( &bv, &b->a_set_pat,
1886 tmp_nmatch, tmp_matchesp ) )
1895 if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1900 if ( b->a_authz.sai_ssf ) {
1901 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1902 b->a_authz.sai_ssf, op->o_ssf, 0 );
1903 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1908 if ( b->a_authz.sai_transport_ssf ) {
1909 Debug( LDAP_DEBUG_ACL,
1910 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1911 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1912 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1917 if ( b->a_authz.sai_tls_ssf ) {
1918 Debug( LDAP_DEBUG_ACL,
1919 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1920 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1921 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1926 if ( b->a_authz.sai_sasl_ssf ) {
1927 Debug( LDAP_DEBUG_ACL,
1928 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1929 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1930 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1936 if ( b->a_dynacl ) {
1938 slap_access_t tgrant, tdeny;
1940 /* this case works different from the others above.
1941 * since aci's themselves give permissions, we need
1942 * to first check b->a_access_mask, the ACL's access level.
1944 if ( BER_BVISEMPTY( &e->e_nname ) ) {
1945 /* no ACIs in the root DSE */
1949 /* first check if the right being requested
1950 * is allowed by the ACL clause.
1952 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1956 /* start out with nothing granted, nothing denied */
1960 for ( da = b->a_dynacl; da; da = da->da_next ) {
1961 slap_access_t grant, deny;
1963 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1969 /* remove anything that the ACL clause does not allow */
1970 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1971 tdeny &= ACL_PRIV_MASK;
1973 /* see if we have anything to contribute */
1974 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1978 /* this could be improved by changing acl_mask so that it can deal with
1979 * by clauses that return grant/deny pairs. Right now, it does either
1980 * additive or subtractive rights, but not both at the same time. So,
1981 * we need to combine the grant/deny pair into a single rights mask in
1982 * a smart way: if either grant or deny is "empty", then we use the
1983 * opposite as is, otherwise we remove any denied rights from the grant
1984 * rights mask and construct an additive mask.
1986 if (ACL_IS_INVALID(tdeny)) {
1987 modmask = tgrant | ACL_PRIV_ADDITIVE;
1989 } else if (ACL_IS_INVALID(tgrant)) {
1990 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1993 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1997 #else /* !SLAP_DYNACL */
1999 #ifdef SLAPD_ACI_ENABLED
2000 if ( b->a_aci_at != NULL ) {
2002 slap_access_t grant, deny, tgrant, tdeny;
2003 struct berval parent_ndn;
2004 BerVarray bvals = NULL;
2007 /* this case works different from the others above.
2008 * since aci's themselves give permissions, we need
2009 * to first check b->a_access_mask, the ACL's access level.
2012 if ( BER_BVISEMPTY( &e->e_nname ) ) {
2013 /* no ACIs in the root DSE */
2017 /* first check if the right being requested
2018 * is allowed by the ACL clause.
2020 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2023 /* start out with nothing granted, nothing denied */
2027 /* get the aci attribute */
2028 at = attr_find( e->e_attrs, b->a_aci_at );
2031 /* FIXME: this breaks acl caching;
2032 * see also ACL_RECORD_VALUE_STATE below */
2033 ACL_RECORD_VALUE_STATE;
2035 /* the aci is an multi-valued attribute. The
2036 * rights are determined by OR'ing the individual
2037 * rights given by the acis.
2039 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2044 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2050 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2051 accessmask2str(tgrant, accessmaskbuf, 1),
2052 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2055 /* If the entry level aci didn't contain anything valid for the
2056 * current operation, climb up the tree and evaluate the
2057 * acis with scope set to subtree
2059 if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2060 dnParent( &e->e_nname, &parent_ndn );
2061 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2062 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2063 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2071 for( i = 0; bvals[i].bv_val != NULL; i++){
2073 /* FIXME: this breaks acl caching;
2074 * see also ACL_RECORD_VALUE_STATE above */
2075 ACL_RECORD_VALUE_STATE;
2077 if (aci_mask(op, e, desc, val, &bvals[i],
2079 &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2083 /* evaluation stops as soon as either a "deny" or a
2084 * "grant" directive matches.
2086 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2090 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2091 accessmask2str(tgrant, accessmaskbuf, 1),
2092 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2096 case LDAP_NO_SUCH_ATTRIBUTE:
2097 /* just go on if the aci-Attribute is not present in
2100 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2104 case LDAP_NO_SUCH_OBJECT:
2105 /* We have reached the base object */
2106 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2117 dnParent( &parent_ndn, &parent_ndn );
2122 /* remove anything that the ACL clause does not allow */
2123 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2124 tdeny &= ACL_PRIV_MASK;
2126 /* see if we have anything to contribute */
2127 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
2131 /* this could be improved by changing acl_mask so that it can deal with
2132 * by clauses that return grant/deny pairs. Right now, it does either
2133 * additive or subtractive rights, but not both at the same time. So,
2134 * we need to combine the grant/deny pair into a single rights mask in
2135 * a smart way: if either grant or deny is "empty", then we use the
2136 * opposite as is, otherwise we remove any denied rights from the grant
2137 * rights mask and construct an additive mask.
2139 if (ACL_IS_INVALID(tdeny)) {
2140 modmask = tgrant | ACL_PRIV_ADDITIVE;
2142 } else if (ACL_IS_INVALID(tgrant)) {
2143 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2146 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2150 #endif /* SLAPD_ACI_ENABLED */
2151 #endif /* !SLAP_DYNACL */
2153 modmask = b->a_access_mask;
2156 Debug( LDAP_DEBUG_ACL,
2157 "<= acl_mask: [%d] applying %s (%s)\n",
2158 i, accessmask2str( modmask, accessmaskbuf, 1 ),
2159 b->a_type == ACL_CONTINUE
2161 : b->a_type == ACL_BREAK
2167 if( ACL_IS_ADDITIVE(modmask) ) {
2169 ACL_PRIV_SET( *mask, modmask );
2172 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2174 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2175 /* substract privs */
2176 ACL_PRIV_CLR( *mask, modmask );
2179 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2186 Debug( LDAP_DEBUG_ACL,
2187 "<= acl_mask: [%d] mask: %s\n",
2188 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2190 if( b->a_type == ACL_CONTINUE ) {
2193 } else if ( b->a_type == ACL_BREAK ) {
2201 /* implicit "by * none" clause */
2204 Debug( LDAP_DEBUG_ACL,
2205 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2206 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2211 * acl_check_modlist - check access control on the given entry to see if
2212 * it allows the given modifications by the user associated with op.
2213 * returns 1 if mods allowed ok
2214 * 0 mods not allowed
2221 Modifications *mlist
2225 AccessControlState state = ACL_STATE_INIT;
2228 int ret = 1; /* default is access allowed */
2232 be = LDAP_STAILQ_FIRST(&backendDB);
2236 assert( be != NULL );
2238 /* short circuit root database access */
2239 if ( be_isroot( op ) ) {
2240 Debug( LDAP_DEBUG_ACL,
2241 "<= acl_access_allowed: granted to database root\n",
2246 /* use backend default access if no backend acls */
2247 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2248 Debug( LDAP_DEBUG_ACL,
2249 "=> access_allowed: backend default %s access %s to \"%s\"\n",
2250 access2str( ACL_WRITE ),
2251 op->o_bd->be_dfltaccess >= ACL_WRITE
2252 ? "granted" : "denied",
2254 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2258 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2260 * no-user-modification operational attributes are ignored
2261 * by ACL_WRITE checking as any found here are not provided
2264 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2265 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2266 " modify access granted\n",
2267 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2271 switch ( mlist->sml_op ) {
2272 case LDAP_MOD_REPLACE:
2274 * We must check both permission to delete the whole
2275 * attribute and permission to add the specific attributes.
2276 * This prevents abuse from selfwriters.
2278 if ( ! access_allowed( op, e,
2279 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2285 if ( mlist->sml_values == NULL ) break;
2287 /* fall thru to check value to add */
2290 assert( mlist->sml_values != NULL );
2292 for ( bv = mlist->sml_nvalues
2293 ? mlist->sml_nvalues : mlist->sml_values;
2294 bv->bv_val != NULL; bv++ )
2296 if ( ! access_allowed( op, e,
2297 mlist->sml_desc, bv, ACL_WADD, &state ) )
2305 case LDAP_MOD_DELETE:
2306 if ( mlist->sml_values == NULL ) {
2307 if ( ! access_allowed( op, e,
2308 mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2315 for ( bv = mlist->sml_nvalues
2316 ? mlist->sml_nvalues : mlist->sml_values;
2317 bv->bv_val != NULL; bv++ )
2319 if ( ! access_allowed( op, e,
2320 mlist->sml_desc, bv, ACL_WDEL, &state ) )
2328 case SLAP_MOD_SOFTADD:
2329 /* allow adding attribute via modrdn thru */
2341 if (be_null) op->o_bd = NULL;
2347 struct berval *list,
2360 while ( len >= 0 && --ix >= 0 ) {
2361 while ( --len >= 0 && *p++ != sep )
2364 while ( len >= 0 && *p == ' ' ) {
2377 while ( --len >= 0 && *p != sep ) {
2381 while ( bv->bv_len > 0 && *--p == ' ' ) {
2388 typedef struct aci_set_gather_t {
2394 aci_set_cb_gather( Operation *op, SlapReply *rs )
2396 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
2398 if ( rs->sr_type == REP_SEARCH ) {
2399 BerValue bvals[ 2 ];
2400 BerVarray bvalsp = NULL;
2403 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2404 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2406 if ( desc == slap_schema.si_ad_entryDN ) {
2408 bvals[ 0 ] = rs->sr_entry->e_nname;
2409 BER_BVZERO( &bvals[ 1 ] );
2414 a = attr_find( rs->sr_entry->e_attrs, desc );
2418 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2421 bvalsp = a->a_nvals;
2427 p->bvals = slap_set_join( p->cookie, p->bvals,
2428 ( '|' | SLAP_SET_RREF ), bvalsp );
2432 assert( rs->sr_type == REP_RESULT );
2439 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2441 AciSetCookie *cp = (AciSetCookie *)cookie;
2443 LDAPURLDesc *ludp = NULL;
2444 Operation op2 = { 0 };
2445 SlapReply rs = {REP_RESULT};
2446 AttributeName anlist[ 2 ], *anlistp = NULL;
2448 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
2449 aci_set_gather_t p = { 0 };
2450 const char *text = NULL;
2451 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2453 /* this routine needs to return the bervals instead of
2454 * plain strings, since syntax is not known. It should
2455 * also return the syntax or some "comparison cookie".
2457 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2458 return aci_set_gather2( cookie, name, desc );
2461 rc = ldap_url_parse( name->bv_val, &ludp );
2462 if ( rc != LDAP_URL_SUCCESS ) {
2463 rc = LDAP_PROTOCOL_ERROR;
2467 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2469 /* host part must be empty */
2470 /* extensions parts must be empty */
2471 rc = LDAP_PROTOCOL_ERROR;
2475 /* Grab the searchbase and see if an appropriate database can be found */
2476 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2477 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2478 &op2.o_req_ndn, cp->op->o_tmpmemctx );
2479 BER_BVZERO( &op2.o_req_dn );
2480 if ( rc != LDAP_SUCCESS ) {
2484 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2485 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2486 rc = LDAP_NO_SUCH_OBJECT;
2490 /* Grab the filter */
2491 if ( ludp->lud_filter ) {
2492 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2493 cp->op->o_tmpmemctx );
2496 op2.ors_filterstr = defaultFilter_bv;
2499 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2500 if ( op2.ors_filter == NULL ) {
2501 rc = LDAP_PROTOCOL_ERROR;
2505 /* Grab the scope */
2506 op2.ors_scope = ludp->lud_scope;
2508 /* Grap the attributes */
2509 if ( ludp->lud_attrs ) {
2510 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2513 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2514 cp->op->o_tmpmemctx );
2516 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2517 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2518 anlistp[ nattrs ].an_desc = NULL;
2519 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2520 &anlistp[ nattrs ].an_desc, &text );
2521 if ( rc != LDAP_SUCCESS ) {
2530 anlistp[ nattrs ].an_name = desc->ad_cname;
2531 anlistp[ nattrs ].an_desc = desc;
2533 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2537 op2.o_hdr = cp->op->o_hdr;
2538 op2.o_tag = LDAP_REQ_SEARCH;
2539 op2.o_ndn = op2.o_bd->be_rootndn;
2540 op2.o_callback = &cb;
2541 op2.o_time = slap_get_time();
2542 op2.o_do_not_cache = 1;
2543 op2.o_is_auth_check = 0;
2544 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2545 op2.ors_slimit = SLAP_NO_LIMIT;
2546 op2.ors_tlimit = SLAP_NO_LIMIT;
2547 op2.ors_attrs = anlistp;
2548 op2.ors_attrsonly = 0;
2549 op2.o_private = cp->op->o_private;
2553 rc = op2.o_bd->be_search( &op2, &rs );
2559 if ( op2.ors_filter ) {
2560 filter_free_x( cp->op, op2.ors_filter );
2562 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2563 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2565 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2566 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2569 ldap_free_urldesc( ludp );
2571 if ( anlistp && anlistp != anlist ) {
2572 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2579 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2581 AciSetCookie *cp = (AciSetCookie *)cookie;
2582 BerVarray bvals = NULL;
2586 /* this routine needs to return the bervals instead of
2587 * plain strings, since syntax is not known. It should
2588 * also return the syntax or some "comparison cookie".
2590 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2591 if ( rc == LDAP_SUCCESS ) {
2592 if ( desc == slap_schema.si_ad_entryDN ) {
2593 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2594 cp->op->o_tmpmemctx );
2596 BER_BVZERO( &bvals[ 1 ] );
2600 backend_attribute( cp->op,
2601 cp->e, &ndn, desc, &bvals, ACL_NONE );
2604 if ( !BER_BVISNULL( &ndn ) ) {
2605 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2614 struct berval *subj,
2620 struct berval set = BER_BVNULL;
2622 AciSetCookie cookie;
2624 if ( setref == 0 ) {
2625 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2628 struct berval subjdn, ndn = BER_BVNULL;
2629 struct berval setat;
2632 AttributeDescription *desc = NULL;
2634 /* format of string is "entry/setAttrName" */
2635 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2639 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2640 setat = aci_bv_set_attr;
2644 * NOTE: dnNormalize honors the ber_len field
2645 * as the length of the dn to be normalized
2647 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2648 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2650 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2651 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2655 BER_BVZERO( &bvals[0] );
2656 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2658 bvals[0].bv_val = bvals[i-1].bv_val;
2659 BER_BVZERO( &bvals[i-1] );
2661 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2662 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2667 if ( !BER_BVISNULL( &set ) ) {
2670 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2671 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2672 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2678 #ifdef SLAPD_ACI_ENABLED
2680 aci_list_map_rights(
2681 struct berval *list )
2688 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2691 switch (*bv.bv_val) {
2693 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2696 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2697 * the right 's' to mean "set", but in the examples states
2698 * that the right 's' means "search". The latter definition
2701 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2704 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2707 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2710 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2711 * define any equivalent to the AUTH right, so I've just used
2714 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2726 struct berval *list,
2727 const struct berval *attr,
2728 struct berval *val )
2730 struct berval bv, left, right;
2733 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2734 if (aci_get_part(&bv, 0, '=', &left) < 0
2735 || aci_get_part(&bv, 1, '=', &right) < 0)
2737 if (ber_bvstrcasecmp(attr, &bv) == 0)
2739 } else if (val == NULL) {
2740 if (ber_bvstrcasecmp(attr, &left) == 0)
2743 if (ber_bvstrcasecmp(attr, &left) == 0) {
2744 /* this is experimental code that implements a
2745 * simple (prefix) match of the attribute value.
2746 * the ACI draft does not provide for aci's that
2747 * apply to specific values, but it would be
2748 * nice to have. If the <attr> part of an aci's
2749 * rights list is of the form <attr>=<value>,
2750 * that means the aci applies only to attrs with
2751 * the given value. Furthermore, if the attr is
2752 * of the form <attr>=<value>*, then <value> is
2753 * treated as a prefix, and the aci applies to
2754 * any value with that prefix.
2756 * Ideally, this would allow r.e. matches.
2758 if (aci_get_part(&right, 0, '*', &left) < 0
2759 || right.bv_len <= left.bv_len)
2761 if (ber_bvstrcasecmp(val, &right) == 0)
2763 } else if (val->bv_len >= left.bv_len) {
2764 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2773 static slap_access_t
2774 aci_list_get_attr_rights(
2775 struct berval *list,
2776 const struct berval *attr,
2777 struct berval *val )
2783 /* loop through each rights/attr pair, skip first part (action) */
2785 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2786 if (aci_list_has_attr(&bv, attr, val) == 0)
2788 if (aci_get_part(list, i, ';', &bv) < 0)
2790 mask |= aci_list_map_rights(&bv);
2796 aci_list_get_rights(
2797 struct berval *list,
2798 const struct berval *attr,
2800 slap_access_t *grant,
2801 slap_access_t *deny )
2803 struct berval perm, actn;
2804 slap_access_t *mask;
2807 if (attr == NULL || attr->bv_len == 0
2808 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2809 attr = &aci_bv_br_entry;
2815 /* loop through each permissions clause */
2816 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2817 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2819 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2821 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2828 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2829 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2836 struct berval *subj,
2837 struct berval *defgrpoc,
2838 struct berval *defgrpat,
2845 struct berval subjdn;
2846 struct berval grpoc;
2847 struct berval grpat;
2848 ObjectClass *grp_oc = NULL;
2849 AttributeDescription *grp_ad = NULL;
2853 /* format of string is "group/objectClassValue/groupAttrName" */
2854 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2858 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2862 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2866 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2867 if( rc != LDAP_SUCCESS ) {
2873 grp_oc = oc_bvfind( &grpoc );
2875 if (grp_oc != NULL && grp_ad != NULL ) {
2876 char buf[ACL_BUF_SIZE];
2877 struct berval bv, ndn;
2878 bv.bv_len = sizeof( buf ) - 1;
2879 bv.bv_val = (char *)&buf;
2880 if ( string_expand(&bv, &subjdn,
2881 e->e_ndn, nmatch, matches) )
2886 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2887 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2888 grp_oc, grp_ad ) == 0 );
2889 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2901 AttributeDescription *desc,
2905 regmatch_t *matches,
2906 slap_access_t *grant,
2907 slap_access_t *deny,
2908 slap_aci_scope_t asserted_scope
2911 struct berval bv, scope, perms, type, sdn;
2915 assert( !BER_BVISNULL( &desc->ad_cname ) );
2917 /* parse an aci of the form:
2918 oid # scope # action;rights;attr;rights;attr
2919 $ action;rights;attr;rights;attr # type # subject
2921 [NOTE: the following comment is very outdated,
2922 as the draft version it refers to (Ando, 2004-11-20)].
2924 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2925 a full description of the format for this attribute.
2926 Differences: "this" in the draft is "self" here, and
2927 "self" and "public" is in the position of type.
2929 <scope> = {entry|children|subtree}
2930 <type> = {public|users|access-id|subtree|onelevel|children|
2931 self|dnattr|group|role|set|set-ref}
2933 This routine now supports scope={ENTRY,CHILDREN}
2935 - ENTRY applies to "entry" and "subtree";
2936 - CHILDREN aplies to "children" and "subtree"
2939 /* check that the aci has all 5 components */
2940 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2944 /* check that the aci family is supported */
2945 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2949 /* check that the scope matches */
2950 if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2954 /* note: scope can be either ENTRY or CHILDREN;
2955 * they respectively match "entry" and "children" in bv
2956 * both match "subtree" */
2957 switch ( asserted_scope ) {
2958 case SLAP_ACI_SCOPE_ENTRY:
2959 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2960 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2966 case SLAP_ACI_SCOPE_CHILDREN:
2967 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2968 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2978 /* get the list of permissions clauses, bail if empty */
2979 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2983 /* check if any permissions allow desired access */
2984 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2988 /* see if we have a DN match */
2989 if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2993 /* see if we have a public (i.e. anonymous) access */
2994 if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2998 /* otherwise require an identity */
2999 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3003 /* see if we have a users access */
3004 if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3008 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3009 * just grab all the berval up to its end (ITS#3303).
3010 * NOTE: the problem could be solved by providing the DN with
3011 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
3012 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3014 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3018 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3019 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3021 if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3024 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3025 if ( rc != LDAP_SUCCESS ) {
3029 if ( dn_match( &op->o_ndn, &ndn ) ) {
3032 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3036 } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3039 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3040 if ( rc != LDAP_SUCCESS ) {
3044 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3047 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3051 } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3052 struct berval ndn, pndn;
3054 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3055 if ( rc != LDAP_SUCCESS ) {
3059 dnParent( &ndn, &pndn );
3061 if ( dn_match( &op->o_ndn, &pndn ) ) {
3064 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3068 } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3071 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3072 if ( rc != LDAP_SUCCESS ) {
3076 if ( !dn_match( &op->o_ndn, &ndn )
3077 && dnIsSuffix( &op->o_ndn, &ndn ) )
3081 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3085 } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3086 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3090 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3092 AttributeDescription *ad = NULL;
3095 rc = slap_bv2ad( &sdn, &ad, &text );
3097 if( rc != LDAP_SUCCESS ) {
3103 for ( at = attrs_find( e->e_attrs, ad );
3105 at = attrs_find( at->a_next, ad ) )
3107 if ( value_find_ex( ad,
3108 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3109 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3111 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3120 } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3121 if ( aci_group_member( &sdn, &aci_bv_group_class,
3122 &aci_bv_group_attr, op, e, nmatch, matches ) )
3127 } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3128 if ( aci_group_member( &sdn, &aci_bv_role_class,
3129 &aci_bv_role_attr, op, e, nmatch, matches ) )
3134 } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3135 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3139 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3140 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3150 * FIXME: there is a silly dependence that makes it difficult
3151 * to move ACIs in a run-time loadable module under the "dynacl"
3152 * umbrella, because sets share some helpers with ACIs.
3155 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3157 AttributeDescription *ad = NULL;
3158 const char *text = NULL;
3160 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3161 fprintf( stderr, "%s: line %d: "
3162 "inappropriate style \"%s\" in \"aci\" by clause\n",
3163 fname, lineno, sty );
3167 if ( right != NULL && *right != '\0' ) {
3168 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3170 "%s: line %d: aci \"%s\": %s\n",
3171 fname, lineno, right, text );
3176 ad = slap_schema.si_ad_aci;
3179 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3180 fprintf( stderr, "%s: line %d: "
3181 "aci \"%s\": inappropriate syntax: %s\n",
3182 fname, lineno, right,
3183 ad->ad_type->sat_syntax_oid );
3187 *privp = (void *)ad;
3193 dynacl_aci_unparse( void *priv, struct berval *bv )
3195 AttributeDescription *ad = ( AttributeDescription * )priv;
3200 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3201 ptr = lutil_strcopy( bv->bv_val, " aci=" );
3202 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3203 bv->bv_len = ptr - bv->bv_val;
3214 AttributeDescription *desc,
3217 regmatch_t *matches,
3218 slap_access_t *grantp,
3219 slap_access_t *denyp )
3221 AttributeDescription *ad = ( AttributeDescription * )priv;
3223 slap_access_t tgrant, tdeny, grant, deny;
3225 char accessmaskbuf[ACCESSMASK_MAXLEN];
3226 char accessmaskbuf1[ACCESSMASK_MAXLEN];
3227 #endif /* LDAP_DEBUG */
3229 /* start out with nothing granted, nothing denied */
3233 /* get the aci attribute */
3234 at = attr_find( e->e_attrs, ad );
3238 /* the aci is an multi-valued attribute. The
3239 * rights are determined by OR'ing the individual
3240 * rights given by the acis.
3242 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3243 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3244 nmatch, matches, &grant, &deny,
3245 SLAP_ACI_SCOPE_ENTRY ) != 0 )
3252 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3253 accessmask2str( tgrant, accessmaskbuf, 1 ),
3254 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3257 /* If the entry level aci didn't contain anything valid for the
3258 * current operation, climb up the tree and evaluate the
3259 * acis with scope set to subtree
3261 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3262 struct berval parent_ndn;
3265 /* to solve the chicken'n'egg problem of accessing
3266 * the OpenLDAPaci attribute, the direct access
3267 * to the entry's attribute is unchecked; however,
3268 * further accesses to OpenLDAPaci values in the
3269 * ancestors occur through backend_attribute(), i.e.
3270 * with the identity of the operation, requiring
3271 * further access checking. For uniformity, this
3272 * makes further requests occur as the rootdn, if
3273 * any, i.e. searching for the OpenLDAPaci attribute
3274 * is considered an internal search. If this is not
3275 * acceptable, then the same check needs be performed
3276 * when accessing the entry's attribute. */
3277 Operation op2 = *op;
3279 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3280 op2.o_dn = op->o_bd->be_rootdn;
3281 op2.o_ndn = op->o_bd->be_rootndn;
3285 dnParent( &e->e_nname, &parent_ndn );
3286 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3288 BerVarray bvals = NULL;
3291 Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3292 ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3301 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3302 if ( aci_mask( op, e, desc, val,
3306 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3310 /* evaluation stops as soon as either a "deny" or a
3311 * "grant" directive matches.
3313 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3317 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3318 accessmask2str( tgrant, accessmaskbuf, 1 ),
3319 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3323 case LDAP_NO_SUCH_ATTRIBUTE:
3324 /* just go on if the aci-Attribute is not present in
3327 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3331 case LDAP_NO_SUCH_OBJECT:
3332 /* We have reached the base object */
3333 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3345 dnParent( &parent_ndn, &parent_ndn );
3355 /* need to register this at some point */
3356 static slap_dynacl_t dynacl_aci = {
3366 #endif /* SLAP_DYNACL */
3368 #endif /* SLAPD_ACI_ENABLED */
3373 * dynamic ACL infrastructure
3375 static slap_dynacl_t *da_list = NULL;
3378 slap_dynacl_register( slap_dynacl_t *da )
3382 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3383 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3388 if ( tmp != NULL ) {
3392 if ( da->da_mask == NULL ) {
3396 da->da_private = NULL;
3397 da->da_next = da_list;
3403 static slap_dynacl_t *
3404 slap_dynacl_next( slap_dynacl_t *da )
3413 slap_dynacl_get( const char *name )
3417 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3418 if ( strcasecmp( da->da_name, name ) == 0 ) {
3425 #endif /* SLAP_DYNACL */
3432 slap_dynacl_t *known_dynacl[] = {
3433 #ifdef SLAPD_ACI_ENABLED
3435 #endif /* SLAPD_ACI_ENABLED */
3439 for ( i = 0; known_dynacl[ i ]; i++ ) {
3440 rc = slap_dynacl_register( known_dynacl[ i ] );
3445 #endif /* SLAP_DYNACL */
3456 regmatch_t *matches)
3464 bv->bv_val[0] = '\0';
3465 bv->bv_len--; /* leave space for lone $ */
3468 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3469 sp < pat->bv_val + pat->bv_len ; sp++ )
3471 /* did we previously see a $ */
3473 if ( flag == 1 && *sp == '$' ) {
3478 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3481 } else if ( *sp >= '0' && *sp <= '9' ) {
3489 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3490 if ( *sp >= '0' && *sp <= '9' ) {
3491 n = 10*n + ( *sp - '0' );
3495 if ( *sp != /*'{'*/ '}' ) {
3501 if ( n >= nmatch ) {
3507 i = matches[n].rm_so;
3508 l = matches[n].rm_eo;
3509 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3527 /* must have ended with a single $ */
3535 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3536 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3543 struct berval *pat, /* pattern to expand and match against */
3544 char *str, /* string to match against pattern */
3545 char *buf, /* buffer with $N expansion variables */
3546 int nmatch, /* size of the matches array */
3547 regmatch_t *matches /* offsets in buffer for $N expansion variables */
3551 char newbuf[ACL_BUF_SIZE];
3555 bv.bv_len = sizeof( newbuf ) - 1;
3562 string_expand( &bv, pat, buf, nmatch, matches );
3563 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3565 char error[ACL_BUF_SIZE];
3566 regerror( rc, &re, error, sizeof( error ) );
3568 Debug( LDAP_DEBUG_TRACE,
3569 "compile( \"%s\", \"%s\") failed %s\n",
3570 pat->bv_val, str, error );
3574 rc = regexec( &re, str, 0, NULL, 0 );
3577 Debug( LDAP_DEBUG_TRACE,
3578 "=> regex_matches: string: %s\n", str, 0, 0 );
3579 Debug( LDAP_DEBUG_TRACE,
3580 "=> regex_matches: rc: %d %s\n",
3581 rc, !rc ? "matches" : "no matches", 0 );