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-2004 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 #include "slapi/slapi.h"
41 #endif /* LDAPI_SLAPI */
43 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
49 aci_bv_entry = BER_BVC("entry"),
50 aci_bv_children = BER_BVC("children"),
51 aci_bv_br_entry = BER_BVC("[entry]"),
52 aci_bv_br_all = BER_BVC("[all]"),
53 aci_bv_access_id = BER_BVC("access-id"),
54 aci_bv_anonymous = BER_BVC("anonymous"),
55 aci_bv_public = BER_BVC("public"),
56 aci_bv_users = BER_BVC("users"),
57 aci_bv_self = BER_BVC("self"),
58 aci_bv_dnattr = BER_BVC("dnattr"),
59 aci_bv_group = BER_BVC("group"),
60 aci_bv_role = BER_BVC("role"),
61 aci_bv_set = BER_BVC("set"),
62 aci_bv_set_ref = BER_BVC("set-ref"),
63 aci_bv_grant = BER_BVC("grant"),
64 aci_bv_deny = BER_BVC("deny"),
66 aci_bv_ip_eq = BER_BVC("IP="),
68 aci_bv_path_eq = BER_BVC("PATH="),
69 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
70 #endif /* LDAP_PF_LOCAL */
72 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
73 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
74 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
75 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
76 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
79 static AccessControl * acl_get(
80 AccessControl *ac, int *count,
81 Operation *op, Entry *e,
82 AttributeDescription *desc,
84 int nmatch, regmatch_t *matches,
85 AccessControlState *state );
87 static slap_control_t acl_mask(
88 AccessControl *ac, slap_mask_t *mask,
89 Operation *op, Entry *e,
90 AttributeDescription *desc,
95 AccessControlState *state );
97 #ifdef SLAPD_ACI_ENABLED
99 Operation *op, Entry *e,
100 AttributeDescription *desc,
105 slap_access_t *grant,
107 struct berval *scope);
110 static int regex_matches(
111 struct berval *pat, char *str, char *buf,
112 int nmatch, regmatch_t *matches);
113 static int string_expand(
114 struct berval *newbuf, struct berval *pattern,
115 char *match, int nmatch, regmatch_t *matches);
117 typedef struct AciSetCookie {
122 SLAP_SET_GATHER aci_set_gather;
123 SLAP_SET_GATHER aci_set_gather2;
124 static int aci_match_set ( struct berval *subj, Operation *op,
125 Entry *e, int setref );
128 * access_allowed - check whether op->o_ndn is allowed the requested access
129 * to entry e, attribute attr, value val. if val is null, access to
130 * the whole attribute is assumed (all values).
132 * This routine loops through all access controls and calls
133 * acl_mask() on each applicable access control.
134 * The loop exits when a definitive answer is reached or
135 * or no more controls remain.
142 * - can be legally called with op == NULL
143 * - can be legally called with op->o_bd == NULL
150 AttributeDescription *desc,
152 slap_access_t access,
153 AccessControlState *state,
158 AccessControl *a = NULL;
163 char accessmaskbuf[ACCESSMASK_MAXLEN];
166 slap_control_t control;
168 regmatch_t matches[MAXREMATCHES];
169 int st_same_attr = 0;
170 static AccessControlState state_init = ACL_STATE_INIT;
173 assert( desc != NULL );
174 assert( access > ACL_NONE );
175 if ( maskp ) ACL_INVALIDATE( *maskp );
177 attr = desc->ad_cname.bv_val;
179 assert( attr != NULL );
181 if( op && op->o_is_auth_check &&
182 ( access == ACL_SEARCH || access == ACL_READ ))
188 if ( state->as_vd_ad==desc) {
189 if ( state->as_recorded ) {
190 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
193 return state->as_result;
194 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
195 val != NULL && state->as_vd_acl == NULL )
197 return state->as_result;
205 state->as_vd_ad=desc;
208 Debug( LDAP_DEBUG_ACL,
209 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
210 access2str( access ), e->e_dn, attr );
223 * FIXME: experimental; use first backend rules
224 * iff there is no global_acl (ITS#3100) */
225 if ( frontendDB->be_acl == NULL )
231 assert( be != NULL );
234 if ( op->o_pb != NULL ) {
235 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
237 /* ACL plugin denied access */
241 #endif /* LDAP_SLAPI */
243 /* grant database root access */
244 if ( be != NULL && be_isroot( op ) ) {
245 Debug( LDAP_DEBUG_ACL,
246 "<= root access granted\n",
249 mask = ACL_LVL_WRITE;
256 * no-user-modification operational attributes are ignored
257 * by ACL_WRITE checking as any found here are not provided
260 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
261 && desc != slap_schema.si_ad_entry
262 && desc != slap_schema.si_ad_children )
264 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
265 " %s access granted\n",
270 /* use backend default access if no backend acls */
271 if( be != NULL && be->be_acl == NULL ) {
272 Debug( LDAP_DEBUG_ACL,
273 "=> access_allowed: backend default %s access %s to \"%s\"\n",
274 access2str( access ),
275 be->be_dfltaccess >= access ? "granted" : "denied",
276 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
277 ret = be->be_dfltaccess >= access;
282 mask = ACL_PRIV_LEVEL;
283 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
284 mask |= ACL_ACCESS2PRIV( i );
291 /* be is always non-NULL */
292 /* use global default access if no global acls */
293 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
294 Debug( LDAP_DEBUG_ACL,
295 "=> access_allowed: global default %s access %s to \"%s\"\n",
296 access2str( access ),
297 frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
298 ret = frontendDB->be_dfltaccess >= access;
303 mask = ACL_PRIV_LEVEL;
304 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
305 mask |= ACL_ACCESS2PRIV( i );
317 assert( state->as_vd_acl != NULL );
319 a = state->as_vd_acl;
320 count = state->as_vd_acl_count;
321 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
322 mask = state->as_vd_acl_mask;
323 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
328 if ( state ) state->as_vi_acl = NULL;
332 memset(matches, '\0', sizeof(matches));
335 while((a = acl_get( a, &count, op, e, desc, val,
336 MAXREMATCHES, matches, state )) != NULL)
340 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
341 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
342 (int)matches[i].rm_so, (int)matches[i].rm_eo );
343 if( matches[i].rm_so <= matches[0].rm_eo ) {
345 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
346 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
349 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
353 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
354 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
355 ret = state->as_result;
358 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
363 control = acl_mask( a, &mask, op,
364 e, desc, val, MAXREMATCHES, matches, count, state );
366 if ( control != ACL_BREAK ) {
370 memset(matches, '\0', sizeof(matches));
373 if ( ACL_IS_INVALID( mask ) ) {
374 Debug( LDAP_DEBUG_ACL,
375 "=> access_allowed: \"%s\" (%s) invalid!\n",
379 } else if ( control == ACL_BREAK ) {
380 Debug( LDAP_DEBUG_ACL,
381 "=> access_allowed: no more rules\n", 0, 0, 0);
386 Debug( LDAP_DEBUG_ACL,
387 "=> access_allowed: %s access %s by %s\n",
388 access2str( access ),
389 ACL_GRANT(mask, access) ? "granted" : "denied",
390 accessmask2str( mask, accessmaskbuf ) );
392 ret = ACL_GRANT(mask, access);
395 if( state != NULL ) {
396 /* If not value-dependent, save ACL in case of more attrs */
397 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
398 state->as_vi_acl = a;
399 state->as_result = ret;
401 state->as_recorded |= ACL_STATE_RECORDED;
403 if (be_null) op->o_bd = NULL;
404 if ( maskp ) *maskp = mask;
410 * acl_get - return the acl applicable to entry e, attribute
411 * attr. the acl returned is suitable for use in subsequent calls to
412 * acl_access_allowed().
415 static AccessControl *
421 AttributeDescription *desc,
425 AccessControlState *state )
432 assert( count != NULL );
433 assert( desc != NULL );
435 attr = desc->ad_cname.bv_val;
437 assert( attr != NULL );
440 if( op->o_bd == NULL ) {
441 a = frontendDB->be_acl;
443 a = op->o_bd->be_acl;
454 dnlen = e->e_nname.bv_len;
456 for ( ; a != NULL; a = a->acl_next ) {
459 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
460 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
461 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
462 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
463 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
467 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
468 *count, a->acl_dn_pat.bv_val, 0 );
469 patlen = a->acl_dn_pat.bv_len;
470 if ( dnlen < patlen )
473 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
474 /* base dn -- entire object DN must match */
475 if ( dnlen != patlen )
478 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
479 int rdnlen = -1, sep = 0;
481 if ( dnlen <= patlen )
485 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
490 rdnlen = dn_rdnlen( NULL, &e->e_nname );
491 if ( rdnlen != dnlen - patlen - sep )
494 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
495 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
498 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
499 if ( dnlen <= patlen )
501 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
505 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
509 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
513 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
514 matches[0].rm_so = matches[0].rm_eo = -1;
518 /* Is this ACL only for a specific value? */
519 if ( a->acl_attrval.bv_len ) {
524 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
525 state->as_recorded |= ACL_STATE_RECORDED_VD;
526 state->as_vd_acl = prev;
527 state->as_vd_acl_count = *count;
528 state->as_vd_access = a->acl_access;
529 state->as_vd_access_count = 1;
530 ACL_INVALIDATE( state->as_vd_acl_mask );
533 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
534 Debug( LDAP_DEBUG_ACL,
535 "acl_get: valpat %s\n",
536 a->acl_attrval.bv_val, 0, 0 );
537 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
542 Debug( LDAP_DEBUG_ACL,
544 a->acl_attrval.bv_val, 0, 0 );
546 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
547 if (value_match( &match, desc,
548 desc->ad_type->sat_equality, 0,
549 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
556 patlen = a->acl_attrval.bv_len;
557 vdnlen = val->bv_len;
559 if ( vdnlen < patlen )
562 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
563 if ( vdnlen > patlen )
566 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
569 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
572 rdnlen = dn_rdnlen( NULL, val );
573 if ( rdnlen != vdnlen - patlen - 1 )
576 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
577 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
580 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
581 if ( vdnlen <= patlen )
584 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
588 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
594 if ( a->acl_filter != NULL ) {
595 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
596 if ( rc != LDAP_COMPARE_TRUE ) {
601 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
606 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
611 * Record value-dependent access control state
613 #define ACL_RECORD_VALUE_STATE do { \
614 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
615 state->as_recorded |= ACL_STATE_RECORDED_VD; \
616 state->as_vd_acl = a; \
617 AC_MEMCPY( state->as_vd_acl_matches, matches, \
618 sizeof( state->as_vd_acl_matches )) ; \
619 state->as_vd_acl_count = count; \
620 state->as_vd_access = b; \
621 state->as_vd_access_count = i; \
626 * acl_mask - modifies mask based upon the given acl and the
627 * requested access to entry e, attribute attr, value val. if val
628 * is null, access to the whole attribute is assumed (all values).
630 * returns 0 access NOT allowed
634 static slap_control_t
640 AttributeDescription *desc,
645 AccessControlState *state )
647 int i, odnlen, patlen;
650 char accessmaskbuf[ACCESSMASK_MAXLEN];
651 char accessmaskbuf1[ACCESSMASK_MAXLEN];
656 assert( mask != NULL );
657 assert( desc != NULL );
659 attr = desc->ad_cname.bv_val;
661 assert( attr != NULL );
663 Debug( LDAP_DEBUG_ACL,
664 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
667 Debug( LDAP_DEBUG_ACL,
668 "=> acl_mask: to %s by \"%s\", (%s) \n",
669 val ? "value" : "all values",
670 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
671 accessmask2str( *mask, accessmaskbuf ) );
674 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
675 && state->as_vd_acl == a )
677 b = state->as_vd_access;
678 i = state->as_vd_access_count;
685 for ( ; b != NULL; b = b->a_next, i++ ) {
686 slap_mask_t oldmask, modmask;
688 ACL_INVALIDATE( modmask );
690 /* AND <who> clauses */
691 if ( b->a_dn_pat.bv_len != 0 ) {
692 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
693 b->a_dn_pat.bv_val, 0, 0);
695 * if access applies to the entry itself, and the
696 * user is bound as somebody in the same namespace as
697 * the entry, OR the given dn matches the dn pattern
699 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
700 if ( op->o_ndn.bv_len != 0 ) {
704 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
705 if ( op->o_ndn.bv_len == 0 ) {
709 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
710 if ( op->o_ndn.bv_len == 0 ) {
714 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
718 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
719 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
721 regmatch_t tmp_matches[2],
722 *tmp_matchesp = tmp_matches;
726 switch ( a->acl_dn_style ) {
727 case ACL_STYLE_REGEX:
728 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
729 tmp_matchesp = matches;
733 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
736 tmp_matches[0].rm_so = 0;
737 tmp_matches[0].rm_eo = e->e_nname.bv_len;
742 case ACL_STYLE_SUBTREE:
743 case ACL_STYLE_CHILDREN:
744 tmp_matches[0].rm_so = 0;
745 tmp_matches[0].rm_eo = e->e_nname.bv_len;
746 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
747 tmp_matches[1].rm_eo = e->e_nname.bv_len;
761 if ( !regex_matches( &b->a_dn_pat,
762 op->o_ndn.bv_val, e->e_ndn,
763 tmp_nmatch, tmp_matchesp ) )
773 if ( e->e_dn == NULL )
776 if ( b->a_dn_expand ) {
778 char buf[ACL_BUF_SIZE];
781 regmatch_t tmp_matches[2],
782 *tmp_matchesp = tmp_matches;
786 bv.bv_len = sizeof( buf ) - 1;
789 switch ( a->acl_dn_style ) {
790 case ACL_STYLE_REGEX:
791 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
792 tmp_matchesp = matches;
796 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
799 tmp_matches[0].rm_so = 0;
800 tmp_matches[0].rm_eo = e->e_nname.bv_len;
805 case ACL_STYLE_SUBTREE:
806 case ACL_STYLE_CHILDREN:
807 tmp_matches[0].rm_so = 0;
808 tmp_matches[0].rm_eo = e->e_nname.bv_len;
809 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
810 tmp_matches[1].rm_eo = e->e_nname.bv_len;
824 if ( string_expand( &bv, &b->a_dn_pat,
826 tmp_nmatch, tmp_matchesp ) )
831 if ( dnNormalize(0, NULL, NULL, &bv,
832 &pat, op->o_tmpmemctx )
835 /* did not expand to a valid dn */
844 odnlen = op->o_ndn.bv_len;
845 if ( odnlen < patlen ) {
846 goto dn_match_cleanup;
850 if ( b->a_dn_style == ACL_STYLE_BASE ) {
851 /* base dn -- entire object DN must match */
852 if ( odnlen != patlen ) {
853 goto dn_match_cleanup;
856 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
859 if ( odnlen <= patlen ) {
860 goto dn_match_cleanup;
863 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
864 goto dn_match_cleanup;
867 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
868 if ( rdnlen != odnlen - patlen - 1 ) {
869 goto dn_match_cleanup;
872 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
873 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
874 goto dn_match_cleanup;
877 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
878 if ( odnlen <= patlen ) {
879 goto dn_match_cleanup;
882 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
883 goto dn_match_cleanup;
887 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
890 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
891 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
900 if ( b->a_sockurl_pat.bv_len ) {
901 if ( ! op->o_conn->c_listener ) {
904 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
905 b->a_sockurl_pat.bv_val, 0, 0 );
907 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
908 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
909 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
910 e->e_ndn, nmatch, matches ) )
915 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
917 char buf[ACL_BUF_SIZE];
919 bv.bv_len = sizeof( buf ) - 1;
921 if ( string_expand( &bv, &b->a_sockurl_pat,
922 e->e_ndn, nmatch, matches ) )
927 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
933 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
941 if ( b->a_domain_pat.bv_len ) {
942 if ( !op->o_conn->c_peer_domain.bv_val ) {
945 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
946 b->a_domain_pat.bv_val, 0, 0 );
947 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
948 if ( b->a_domain_style == ACL_STYLE_REGEX) {
949 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
950 e->e_ndn, nmatch, matches ) )
955 char buf[ACL_BUF_SIZE];
957 struct berval cmp = op->o_conn->c_peer_domain;
958 struct berval pat = b->a_domain_pat;
960 if ( b->a_domain_expand ) {
963 bv.bv_len = sizeof(buf) - 1;
966 if ( string_expand(&bv, &b->a_domain_pat,
967 e->e_ndn, nmatch, matches) )
974 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
975 int offset = cmp.bv_len - pat.bv_len;
980 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
984 /* trim the domain */
985 cmp.bv_val = &cmp.bv_val[ offset ];
986 cmp.bv_len -= offset;
989 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
996 if ( b->a_peername_pat.bv_len ) {
997 if ( !op->o_conn->c_peer_name.bv_val ) {
1000 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1001 b->a_peername_pat.bv_val, 0, 0 );
1002 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1003 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1004 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1005 e->e_ndn, nmatch, matches ) )
1011 /* try exact match */
1012 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1013 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1017 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1019 char buf[ACL_BUF_SIZE];
1021 bv.bv_len = sizeof( buf ) - 1;
1023 if ( string_expand( &bv, &b->a_peername_pat,
1024 e->e_ndn, nmatch, matches ) )
1029 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1033 /* extract IP and try exact match */
1034 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1036 char buf[] = "255.255.255.255";
1039 int port_number = -1;
1041 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1042 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1045 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1046 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1048 port = strrchr( ip.bv_val, ':' );
1052 ip.bv_len = port - ip.bv_val;
1054 port_number = strtol( port, &next, 10 );
1055 if ( next[0] != '\0' )
1059 /* the port check can be anticipated here */
1060 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1063 /* address longer than expected? */
1064 if ( ip.bv_len >= sizeof(buf) )
1067 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1068 buf[ ip.bv_len ] = '\0';
1070 addr = inet_addr( buf );
1072 /* unable to convert? */
1073 if ( addr == (unsigned long)(-1) )
1076 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1079 #ifdef LDAP_PF_LOCAL
1080 /* extract path and try exact match */
1081 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1084 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1085 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1088 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1089 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1091 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1094 #endif /* LDAP_PF_LOCAL */
1096 /* exact match (very unlikely...) */
1097 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1104 if ( b->a_sockname_pat.bv_len ) {
1105 if ( !op->o_conn->c_sock_name.bv_val ) {
1108 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1109 b->a_sockname_pat.bv_val, 0, 0 );
1110 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1111 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1112 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1113 e->e_ndn, nmatch, matches ) )
1118 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1120 char buf[ACL_BUF_SIZE];
1122 bv.bv_len = sizeof( buf ) - 1;
1124 if ( string_expand( &bv, &b->a_sockname_pat,
1125 e->e_ndn, nmatch, matches ) )
1130 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1135 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1142 if ( b->a_dn_at != NULL ) {
1147 const char *attr = b->a_dn_at->ad_cname.bv_val;
1149 assert( attr != NULL );
1151 if ( op->o_ndn.bv_len == 0 ) {
1155 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1159 /* see if asker is listed in dnattr */
1160 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1162 at = attrs_find( at->a_next, b->a_dn_at ) )
1164 if( value_find_ex( b->a_dn_at,
1165 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1166 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1168 &bv, op->o_tmpmemctx ) == 0 )
1177 /* have a dnattr match. if this is a self clause then
1178 * the target must also match the op dn.
1180 if ( b->a_dn_self ) {
1181 /* check if the target is an attribute. */
1182 if ( val == NULL ) continue;
1184 /* target is attribute, check if the attribute value
1187 rc = value_match( &match, b->a_dn_at,
1188 b->a_dn_at->ad_type->sat_equality, 0,
1190 /* on match error or no match, fail the ACL clause */
1191 if (rc != LDAP_SUCCESS || match != 0 )
1195 /* no dnattr match, check if this is a self clause */
1196 if ( ! b->a_dn_self )
1199 ACL_RECORD_VALUE_STATE;
1201 /* this is a self clause, check if the target is an
1207 /* target is attribute, check if the attribute value
1210 rc = value_match( &match, b->a_dn_at,
1211 b->a_dn_at->ad_type->sat_equality, 0,
1214 /* on match error or no match, fail the ACL clause */
1215 if (rc != LDAP_SUCCESS || match != 0 )
1220 if ( b->a_group_pat.bv_len ) {
1222 struct berval ndn = BER_BVNULL;
1225 if ( op->o_ndn.bv_len == 0 ) {
1229 /* b->a_group is an unexpanded entry name, expanded it should be an
1230 * entry with objectclass group* and we test to see if odn is one of
1231 * the values in the attribute group
1233 /* see if asker is listed in dnattr */
1234 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1235 char buf[ACL_BUF_SIZE];
1237 regmatch_t tmp_matches[2],
1238 *tmp_matchesp = tmp_matches;
1240 bv.bv_len = sizeof(buf) - 1;
1245 switch ( a->acl_dn_style ) {
1246 case ACL_STYLE_REGEX:
1247 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1248 tmp_matchesp = matches;
1249 tmp_nmatch = nmatch;
1253 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1254 case ACL_STYLE_BASE:
1255 tmp_matches[0].rm_so = 0;
1256 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1261 case ACL_STYLE_SUBTREE:
1262 case ACL_STYLE_CHILDREN:
1263 tmp_matches[0].rm_so = 0;
1264 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1265 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1266 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1280 if ( string_expand( &bv, &b->a_group_pat,
1282 tmp_nmatch, tmp_matchesp ) )
1287 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1288 op->o_tmpmemctx ) != LDAP_SUCCESS )
1290 /* did not expand to a valid dn */
1297 bv = b->a_group_pat;
1300 rc = backend_group( op, e, &bv, &op->o_ndn,
1301 b->a_group_oc, b->a_group_at );
1304 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1312 if ( b->a_set_pat.bv_len != 0 ) {
1314 char buf[ACL_BUF_SIZE];
1316 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1318 regmatch_t tmp_matches[2],
1319 *tmp_matchesp = tmp_matches;
1322 bv.bv_len = sizeof(buf) - 1;
1327 switch ( a->acl_dn_style ) {
1328 case ACL_STYLE_REGEX:
1329 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1330 tmp_matchesp = matches;
1331 tmp_nmatch = nmatch;
1335 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1336 case ACL_STYLE_BASE:
1337 tmp_matches[0].rm_so = 0;
1338 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1343 case ACL_STYLE_SUBTREE:
1344 case ACL_STYLE_CHILDREN:
1345 tmp_matches[0].rm_so = 0;
1346 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1347 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1348 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1362 if ( string_expand( &bv, &b->a_set_pat,
1364 tmp_nmatch, tmp_matchesp ) )
1372 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1377 if ( b->a_authz.sai_ssf ) {
1378 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1379 b->a_authz.sai_ssf, op->o_ssf, 0 );
1380 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1385 if ( b->a_authz.sai_transport_ssf ) {
1386 Debug( LDAP_DEBUG_ACL,
1387 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1388 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1389 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1394 if ( b->a_authz.sai_tls_ssf ) {
1395 Debug( LDAP_DEBUG_ACL,
1396 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1397 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1398 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1403 if ( b->a_authz.sai_sasl_ssf ) {
1404 Debug( LDAP_DEBUG_ACL,
1405 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1406 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1407 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1412 #ifdef SLAPD_ACI_ENABLED
1413 if ( b->a_aci_at != NULL ) {
1415 slap_access_t grant, deny, tgrant, tdeny;
1416 struct berval parent_ndn, old_parent_ndn;
1417 BerVarray bvals = NULL;
1420 /* this case works different from the others above.
1421 * since aci's themselves give permissions, we need
1422 * to first check b->a_access_mask, the ACL's access level.
1425 if ( e->e_nname.bv_len == 0 ) {
1426 /* no ACIs in the root DSE */
1430 /* first check if the right being requested
1431 * is allowed by the ACL clause.
1433 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1436 /* start out with nothing granted, nothing denied */
1440 /* get the aci attribute */
1441 at = attr_find( e->e_attrs, b->a_aci_at );
1444 /* FIXME: this breaks acl caching;
1445 * see also ACL_RECORD_VALUE_STATE below */
1446 ACL_RECORD_VALUE_STATE;
1448 /* the aci is an multi-valued attribute. The
1449 * rights are determined by OR'ing the individual
1450 * rights given by the acis.
1452 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1457 &grant, &deny, &aci_bv_entry ) != 0)
1463 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1464 accessmask2str(tgrant,accessmaskbuf),
1465 accessmask2str(tdeny, accessmaskbuf1), 0);
1468 /* If the entry level aci didn't contain anything valid for the
1469 * current operation, climb up the tree and evaluate the
1470 * acis with scope set to subtree
1472 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1473 dnParent(&(e->e_nname), &parent_ndn);
1474 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1475 old_parent_ndn = parent_ndn;
1476 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1477 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1485 for( i = 0; bvals[i].bv_val != NULL; i++){
1487 /* FIXME: this breaks acl caching;
1488 * see also ACL_RECORD_VALUE_STATE above */
1489 ACL_RECORD_VALUE_STATE;
1491 if (aci_mask(op, e, desc, val, &bvals[i],
1493 &grant, &deny, &aci_bv_children) != 0)
1497 /* evaluation stops as soon as either a "deny" or a
1498 * "grant" directive matches.
1500 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1504 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1505 accessmask2str(tgrant,accessmaskbuf),
1506 accessmask2str(tdeny, accessmaskbuf1), 0);
1510 case LDAP_NO_SUCH_ATTRIBUTE:
1511 /* just go on if the aci-Attribute is not present in
1514 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1518 case LDAP_NO_SUCH_OBJECT:
1519 /* We have reached the base object */
1520 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1531 dnParent(&old_parent_ndn, &parent_ndn);
1536 /* remove anything that the ACL clause does not allow */
1537 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1538 tdeny &= ACL_PRIV_MASK;
1540 /* see if we have anything to contribute */
1541 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1545 /* this could be improved by changing acl_mask so that it can deal with
1546 * by clauses that return grant/deny pairs. Right now, it does either
1547 * additive or subtractive rights, but not both at the same time. So,
1548 * we need to combine the grant/deny pair into a single rights mask in
1549 * a smart way: if either grant or deny is "empty", then we use the
1550 * opposite as is, otherwise we remove any denied rights from the grant
1551 * rights mask and construct an additive mask.
1553 if (ACL_IS_INVALID(tdeny)) {
1554 modmask = tgrant | ACL_PRIV_ADDITIVE;
1556 } else if (ACL_IS_INVALID(tgrant)) {
1557 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1560 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1566 modmask = b->a_access_mask;
1569 Debug( LDAP_DEBUG_ACL,
1570 "<= acl_mask: [%d] applying %s (%s)\n",
1571 i, accessmask2str( modmask, accessmaskbuf ),
1572 b->a_type == ACL_CONTINUE
1574 : b->a_type == ACL_BREAK
1580 if( ACL_IS_ADDITIVE(modmask) ) {
1582 ACL_PRIV_SET( *mask, modmask );
1585 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1587 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1588 /* substract privs */
1589 ACL_PRIV_CLR( *mask, modmask );
1592 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1599 Debug( LDAP_DEBUG_ACL,
1600 "<= acl_mask: [%d] mask: %s\n",
1601 i, accessmask2str(*mask, accessmaskbuf), 0 );
1603 if( b->a_type == ACL_CONTINUE ) {
1606 } else if ( b->a_type == ACL_BREAK ) {
1614 /* implicit "by * none" clause */
1617 Debug( LDAP_DEBUG_ACL,
1618 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1619 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1624 * acl_check_modlist - check access control on the given entry to see if
1625 * it allows the given modifications by the user associated with op.
1626 * returns 1 if mods allowed ok
1627 * 0 mods not allowed
1634 Modifications *mlist
1638 AccessControlState state = ACL_STATE_INIT;
1641 int ret = 1; /* default is access allowed */
1649 assert( be != NULL );
1651 /* short circuit root database access */
1652 if ( be_isroot( op ) ) {
1653 Debug( LDAP_DEBUG_ACL,
1654 "<= acl_access_allowed: granted to database root\n",
1659 /* use backend default access if no backend acls */
1660 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1661 Debug( LDAP_DEBUG_ACL,
1662 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1663 access2str( ACL_WRITE ),
1664 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1665 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1669 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1671 * no-user-modification operational attributes are ignored
1672 * by ACL_WRITE checking as any found here are not provided
1675 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1676 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1677 " modify access granted\n",
1678 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1682 switch ( mlist->sml_op ) {
1683 case LDAP_MOD_REPLACE:
1685 * We must check both permission to delete the whole
1686 * attribute and permission to add the specific attributes.
1687 * This prevents abuse from selfwriters.
1689 if ( ! access_allowed( op, e,
1690 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1696 if ( mlist->sml_values == NULL ) break;
1698 /* fall thru to check value to add */
1701 assert( mlist->sml_values != NULL );
1703 for ( bv = mlist->sml_nvalues
1704 ? mlist->sml_nvalues : mlist->sml_values;
1705 bv->bv_val != NULL; bv++ )
1707 if ( ! access_allowed( op, e,
1708 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1716 case LDAP_MOD_DELETE:
1717 if ( mlist->sml_values == NULL ) {
1718 if ( ! access_allowed( op, e,
1719 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1726 for ( bv = mlist->sml_nvalues
1727 ? mlist->sml_nvalues : mlist->sml_values;
1728 bv->bv_val != NULL; bv++ )
1730 if ( ! access_allowed( op, e,
1731 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1739 case SLAP_MOD_SOFTADD:
1740 /* allow adding attribute via modrdn thru */
1752 if (be_null) op->o_bd = NULL;
1758 struct berval *list,
1771 while (len >= 0 && --ix >= 0) {
1772 while (--len >= 0 && *p++ != sep) ;
1774 while (len >= 0 && *p == ' ') {
1785 while (--len >= 0 && *p != sep) {
1789 while (bv->bv_len > 0 && *--p == ' ')
1794 typedef struct aci_set_gather_t {
1800 aci_set_cb_gather( Operation *op, SlapReply *rs )
1802 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
1804 if ( rs->sr_type == REP_SEARCH ) {
1805 BerVarray bvals = NULL;
1808 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1809 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
1811 if ( desc == slap_schema.si_ad_entryDN ) {
1812 bvals = slap_sl_malloc( sizeof( BerValue ) * 2, op->o_tmpmemctx );
1813 ber_dupbv_x( &bvals[ 0 ], &rs->sr_entry->e_nname, op->o_tmpmemctx );
1814 BER_BVZERO( &bvals[ 1 ] );
1819 a = attr_find( rs->sr_entry->e_attrs, desc );
1823 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1826 bvals = slap_sl_malloc( sizeof( BerValue ) * ( i + 1 ), op->o_tmpmemctx );
1827 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
1828 ber_dupbv_x( &bvals[ i ], &a->a_nvals[ i ], op->o_tmpmemctx );
1830 BER_BVZERO( &bvals[ i ] );
1836 p->bvals = slap_set_join( p->cookie, p->bvals, '|', bvals );
1840 assert( rs->sr_type == REP_RESULT );
1847 aci_set_gather( SetCookie *cookie, struct berval *name, struct berval *attr )
1849 AciSetCookie *cp = (AciSetCookie *)cookie;
1851 LDAPURLDesc *ludp = NULL;
1852 Operation op2 = { 0 };
1853 SlapReply rs = {REP_RESULT};
1854 AttributeName anlist[ 2 ], *anlistp = NULL;
1856 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
1857 aci_set_gather_t p = { 0 };
1858 const char *text = NULL;
1859 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
1861 /* this routine needs to return the bervals instead of
1862 * plain strings, since syntax is not known. It should
1863 * also return the syntax or some "comparison cookie".
1865 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
1866 return aci_set_gather2( cookie, name, attr );
1869 rc = ldap_url_parse( name->bv_val, &ludp );
1870 if ( rc != LDAP_URL_SUCCESS ) {
1871 rc = LDAP_PROTOCOL_ERROR;
1875 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
1877 /* host part must be empty */
1878 /* extensions parts must be empty */
1879 rc = LDAP_PROTOCOL_ERROR;
1883 /* Grab the searchbase and see if an appropriate database can be found */
1884 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
1885 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
1886 &op2.o_req_ndn, cp->op->o_tmpmemctx );
1887 BER_BVZERO( &op2.o_req_dn );
1888 if ( rc != LDAP_SUCCESS ) {
1892 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
1893 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
1894 rc = LDAP_NO_SUCH_OBJECT;
1898 /* Grab the filter */
1899 if ( ludp->lud_filter ) {
1900 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
1901 cp->op->o_tmpmemctx );
1904 op2.ors_filterstr = defaultFilter_bv;
1907 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
1908 if ( op2.ors_filter == NULL ) {
1909 rc = LDAP_PROTOCOL_ERROR;
1913 /* Grab the scope */
1914 op2.ors_scope = ludp->lud_scope;
1916 /* Grap the attributes */
1917 if ( ludp->lud_attrs ) {
1918 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
1921 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
1922 cp->op->o_tmpmemctx );
1924 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
1925 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
1926 anlistp[ nattrs ].an_desc = NULL;
1927 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
1928 &anlistp[ nattrs ].an_desc, &text );
1929 if ( rc != LDAP_SUCCESS ) {
1938 anlistp[ nattrs ].an_name = *attr;
1939 anlistp[ nattrs ].an_desc = NULL;
1940 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
1941 &anlistp[ nattrs ].an_desc, &text );
1942 if ( rc != LDAP_SUCCESS ) {
1946 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
1950 op2.o_tag = LDAP_REQ_SEARCH;
1951 op2.o_protocol = LDAP_VERSION3;
1952 op2.o_ndn = op2.o_bd->be_rootndn;
1953 op2.o_callback = &cb;
1954 op2.o_time = slap_get_time();
1955 op2.o_do_not_cache = 1;
1956 op2.o_is_auth_check = 0;
1957 op2.o_threadctx = cp->op->o_threadctx;
1958 op2.o_tmpmemctx = cp->op->o_tmpmemctx;
1959 op2.o_tmpmfuncs = cp->op->o_tmpmfuncs;
1961 op2.o_pb = cp->op->o_pb;
1963 op2.o_conn = cp->op->o_conn;
1964 op2.o_connid = cp->op->o_connid;
1965 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
1966 op2.ors_slimit = SLAP_NO_LIMIT;
1967 op2.ors_tlimit = SLAP_NO_LIMIT;
1968 op2.ors_attrs = anlistp;
1969 op2.ors_attrsonly = 0;
1970 op2.o_sync_slog_size = -1;
1974 rc = op2.o_bd->be_search( &op2, &rs );
1980 if ( op2.ors_filter ) {
1981 filter_free_x( cp->op, op2.ors_filter );
1983 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
1984 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
1986 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
1987 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
1990 ldap_free_urldesc( ludp );
1992 if ( anlistp && anlistp != anlist ) {
1993 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2000 aci_set_gather2( SetCookie *cookie, struct berval *name, struct berval *attr )
2002 AciSetCookie *cp = (AciSetCookie *)cookie;
2003 BerVarray bvals = NULL;
2007 /* this routine needs to return the bervals instead of
2008 * plain strings, since syntax is not known. It should
2009 * also return the syntax or some "comparison cookie".
2011 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2012 if ( rc == LDAP_SUCCESS ) {
2014 AttributeDescription *desc = NULL;
2016 if ( slap_bv2ad( attr, &desc, &text ) == LDAP_SUCCESS ) {
2017 if ( desc == slap_schema.si_ad_entryDN ) {
2018 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2019 cp->op->o_tmpmemctx );
2021 BER_BVZERO( &bvals[ 1 ] );
2025 backend_attribute( cp->op,
2026 cp->e, &ndn, desc, &bvals, ACL_NONE );
2030 if ( !BER_BVISNULL( &ndn ) ) {
2031 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2040 struct berval *subj,
2046 struct berval set = BER_BVNULL;
2048 AciSetCookie cookie;
2051 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2053 struct berval subjdn, ndn = BER_BVNULL;
2054 struct berval setat;
2057 AttributeDescription *desc = NULL;
2059 /* format of string is "entry/setAttrName" */
2060 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2064 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2065 setat = aci_bv_set_attr;
2069 * NOTE: dnNormalize honors the ber_len field
2070 * as the length of the dn to be normalized
2072 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2073 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2075 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2076 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2080 BER_BVZERO( &bvals[0] );
2081 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2083 bvals[0].bv_val = bvals[i-1].bv_val;
2084 BER_BVZERO( &bvals[i-1] );
2086 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2087 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2092 if ( !BER_BVISNULL( &set ) ) {
2095 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2096 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2097 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2103 #ifdef SLAPD_ACI_ENABLED
2105 aci_list_map_rights(
2106 struct berval *list )
2113 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2116 switch (*bv.bv_val) {
2118 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2121 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2122 * the right 's' to mean "set", but in the examples states
2123 * that the right 's' means "search". The latter definition
2126 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2129 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2132 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2135 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2136 * define any equivalent to the AUTH right, so I've just used
2139 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2151 struct berval *list,
2152 const struct berval *attr,
2153 struct berval *val )
2155 struct berval bv, left, right;
2158 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2159 if (aci_get_part(&bv, 0, '=', &left) < 0
2160 || aci_get_part(&bv, 1, '=', &right) < 0)
2162 if (ber_bvstrcasecmp(attr, &bv) == 0)
2164 } else if (val == NULL) {
2165 if (ber_bvstrcasecmp(attr, &left) == 0)
2168 if (ber_bvstrcasecmp(attr, &left) == 0) {
2169 /* this is experimental code that implements a
2170 * simple (prefix) match of the attribute value.
2171 * the ACI draft does not provide for aci's that
2172 * apply to specific values, but it would be
2173 * nice to have. If the <attr> part of an aci's
2174 * rights list is of the form <attr>=<value>,
2175 * that means the aci applies only to attrs with
2176 * the given value. Furthermore, if the attr is
2177 * of the form <attr>=<value>*, then <value> is
2178 * treated as a prefix, and the aci applies to
2179 * any value with that prefix.
2181 * Ideally, this would allow r.e. matches.
2183 if (aci_get_part(&right, 0, '*', &left) < 0
2184 || right.bv_len <= left.bv_len)
2186 if (ber_bvstrcasecmp(val, &right) == 0)
2188 } else if (val->bv_len >= left.bv_len) {
2189 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2198 static slap_access_t
2199 aci_list_get_attr_rights(
2200 struct berval *list,
2201 const struct berval *attr,
2202 struct berval *val )
2208 /* loop through each rights/attr pair, skip first part (action) */
2210 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2211 if (aci_list_has_attr(&bv, attr, val) == 0)
2213 if (aci_get_part(list, i, ';', &bv) < 0)
2215 mask |= aci_list_map_rights(&bv);
2221 aci_list_get_rights(
2222 struct berval *list,
2223 const struct berval *attr,
2225 slap_access_t *grant,
2226 slap_access_t *deny )
2228 struct berval perm, actn;
2229 slap_access_t *mask;
2232 if (attr == NULL || attr->bv_len == 0
2233 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2234 attr = &aci_bv_br_entry;
2240 /* loop through each permissions clause */
2241 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2242 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2244 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2246 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2253 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2254 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2261 struct berval *subj,
2262 struct berval *defgrpoc,
2263 struct berval *defgrpat,
2270 struct berval subjdn;
2271 struct berval grpoc;
2272 struct berval grpat;
2273 ObjectClass *grp_oc = NULL;
2274 AttributeDescription *grp_ad = NULL;
2278 /* format of string is "group/objectClassValue/groupAttrName" */
2279 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2283 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2287 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2291 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2292 if( rc != LDAP_SUCCESS ) {
2298 grp_oc = oc_bvfind( &grpoc );
2300 if (grp_oc != NULL && grp_ad != NULL ) {
2301 char buf[ACL_BUF_SIZE];
2302 struct berval bv, ndn;
2303 bv.bv_len = sizeof( buf ) - 1;
2304 bv.bv_val = (char *)&buf;
2305 if ( string_expand(&bv, &subjdn,
2306 e->e_ndn, nmatch, matches) )
2311 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2312 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2313 grp_oc, grp_ad ) == 0 );
2314 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2326 AttributeDescription *desc,
2330 regmatch_t *matches,
2331 slap_access_t *grant,
2332 slap_access_t *deny,
2333 struct berval *scope
2336 struct berval bv, perms, sdn;
2340 assert( desc->ad_cname.bv_val != NULL );
2342 /* parse an aci of the form:
2343 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2345 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2346 a full description of the format for this attribute.
2347 Differences: "this" in the draft is "self" here, and
2348 "self" and "public" is in the position of dnType.
2350 For now, this routine only supports scope=entry.
2352 /* check that the aci has all 5 components */
2353 if (aci_get_part(aci, 4, '#', NULL) < 0)
2356 /* check that the aci family is supported */
2357 if (aci_get_part(aci, 0, '#', &bv) < 0)
2360 /* check that the scope matches */
2361 if (aci_get_part(aci, 1, '#', &bv) < 0
2362 || ber_bvstrcasecmp( scope, &bv ) != 0)
2367 /* get the list of permissions clauses, bail if empty */
2368 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2371 /* check if any permissions allow desired access */
2372 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2375 /* see if we have a DN match */
2376 if (aci_get_part(aci, 3, '#', &bv) < 0)
2379 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2382 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2385 if ( dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2386 if ( dn_match( &op->o_ndn, &ndn ) ) {
2389 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2393 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2396 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2397 if (dn_match(&op->o_ndn, &e->e_nname))
2400 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2402 AttributeDescription *ad = NULL;
2405 rc = slap_bv2ad( &sdn, &ad, &text );
2407 if( rc != LDAP_SUCCESS ) {
2415 for(at = attrs_find( e->e_attrs, ad );
2417 at = attrs_find( at->a_next, ad ) )
2419 if (value_find_ex( ad,
2420 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2421 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2423 &bv, op->o_tmpmemctx) == 0 )
2433 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2434 if (aci_group_member(&sdn, &aci_bv_group_class,
2435 &aci_bv_group_attr, op, e, nmatch, matches))
2438 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2439 if (aci_group_member(&sdn, &aci_bv_role_class,
2440 &aci_bv_role_attr, op, e, nmatch, matches))
2443 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2444 if (aci_match_set(&sdn, op, e, 0))
2447 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2448 if (aci_match_set(&sdn, op, e, 1))
2456 #endif /* SLAPD_ACI_ENABLED */
2464 regmatch_t *matches)
2472 bv->bv_val[0] = '\0';
2473 bv->bv_len--; /* leave space for lone $ */
2476 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2477 sp < pat->bv_val + pat->bv_len ; sp++ )
2479 /* did we previously see a $ */
2481 if ( flag == 1 && *sp == '$' ) {
2486 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2489 } else if ( *sp >= '0' && *sp <= '9' ) {
2497 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2498 if ( *sp >= '0' && *sp <= '9' ) {
2499 n = 10*n + ( *sp - '0' );
2503 if ( *sp != /*'{'*/ '}' ) {
2509 if ( n >= nmatch ) {
2515 i = matches[n].rm_so;
2516 l = matches[n].rm_eo;
2517 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2535 /* must have ended with a single $ */
2543 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2544 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2551 struct berval *pat, /* pattern to expand and match against */
2552 char *str, /* string to match against pattern */
2553 char *buf, /* buffer with $N expansion variables */
2554 int nmatch, /* size of the matches array */
2555 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2559 char newbuf[ACL_BUF_SIZE];
2563 bv.bv_len = sizeof( newbuf ) - 1;
2570 string_expand( &bv, pat, buf, nmatch, matches );
2571 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2573 char error[ACL_BUF_SIZE];
2574 regerror( rc, &re, error, sizeof( error ) );
2576 Debug( LDAP_DEBUG_TRACE,
2577 "compile( \"%s\", \"%s\") failed %s\n",
2578 pat->bv_val, str, error );
2582 rc = regexec( &re, str, 0, NULL, 0 );
2585 Debug( LDAP_DEBUG_TRACE,
2586 "=> regex_matches: string: %s\n", str, 0, 0 );
2587 Debug( LDAP_DEBUG_TRACE,
2588 "=> regex_matches: rc: %d %s\n",
2589 rc, !rc ? "matches" : "no matches", 0 );