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-2003 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>
39 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
45 aci_bv_entry = BER_BVC("entry"),
46 aci_bv_children = BER_BVC("children"),
47 aci_bv_br_entry = BER_BVC("[entry]"),
48 aci_bv_br_all = BER_BVC("[all]"),
49 aci_bv_access_id = BER_BVC("access-id"),
50 aci_bv_anonymous = BER_BVC("anonymous"),
51 aci_bv_public = BER_BVC("public"),
52 aci_bv_users = BER_BVC("users"),
53 aci_bv_self = BER_BVC("self"),
54 aci_bv_dnattr = BER_BVC("dnattr"),
55 aci_bv_group = BER_BVC("group"),
56 aci_bv_role = BER_BVC("role"),
57 aci_bv_set = BER_BVC("set"),
58 aci_bv_set_ref = BER_BVC("set-ref"),
59 aci_bv_grant = BER_BVC("grant"),
60 aci_bv_deny = BER_BVC("deny"),
62 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
63 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
64 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
65 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR);
68 static AccessControl * acl_get(
69 AccessControl *ac, int *count,
70 Operation *op, Entry *e,
71 AttributeDescription *desc,
73 int nmatches, regmatch_t *matches );
75 static slap_control_t acl_mask(
76 AccessControl *ac, slap_mask_t *mask,
77 Operation *op, Entry *e,
78 AttributeDescription *desc,
82 AccessControlState *state );
84 #ifdef SLAPD_ACI_ENABLED
86 Operation *op, Entry *e,
87 AttributeDescription *desc,
93 struct berval *scope);
96 static int regex_matches(
97 struct berval *pat, char *str, char *buf, regmatch_t *matches);
98 static void string_expand(
99 struct berval *newbuf, struct berval *pattern,
100 char *match, regmatch_t *matches);
102 typedef struct AciSetCookie {
107 SLAP_SET_GATHER aci_set_gather;
108 static int aci_match_set ( struct berval *subj, Operation *op,
109 Entry *e, int setref );
112 * access_allowed - check whether op->o_ndn is allowed the requested access
113 * to entry e, attribute attr, value val. if val is null, access to
114 * the whole attribute is assumed (all values).
116 * This routine loops through all access controls and calls
117 * acl_mask() on each applicable access control.
118 * The loop exits when a definitive answer is reached or
119 * or no more controls remain.
130 AttributeDescription *desc,
132 slap_access_t access,
133 AccessControlState *state )
137 AccessControl *a = NULL;
142 char accessmaskbuf[ACCESSMASK_MAXLEN];
145 slap_control_t control;
147 regmatch_t matches[MAXREMATCHES];
148 int st_same_attr = 0;
149 int st_initialized = 0;
150 static AccessControlState state_init = ACL_STATE_INIT;
153 assert( desc != NULL );
154 assert( access > ACL_NONE );
156 attr = desc->ad_cname.bv_val;
158 assert( attr != NULL );
160 if( op && op->o_is_auth_check &&
161 ( access == ACL_SEARCH || access == ACL_READ ))
166 if( state && state->as_recorded && state->as_vd_ad==desc) {
167 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
170 return state->as_result;
172 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
173 val != NULL && state->as_vd_acl == NULL )
175 return state->as_result;
181 state->as_vd_ad=desc;
185 LDAP_LOG( ACL, ENTRY,
186 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
187 access2str( access ), e->e_dn, attr );
189 Debug( LDAP_DEBUG_ACL,
190 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
191 access2str( access ), e->e_dn, attr );
201 !slapi_x_access_allowed( op, e, desc, val, access, state )) {
202 /* ACL plugin denied access */
205 #endif /* LDAP_SLAPI */
213 assert( be != NULL );
215 /* grant database root access */
216 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
219 "access_allowed: conn %lu root access granted\n",
220 op->o_connid, 0, 0 );
222 Debug( LDAP_DEBUG_ACL,
223 "<= root access granted\n",
230 * no-user-modification operational attributes are ignored
231 * by ACL_WRITE checking as any found here are not provided
234 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
235 && desc != slap_schema.si_ad_entry
236 && desc != slap_schema.si_ad_children )
239 LDAP_LOG( ACL, DETAIL1,
240 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
241 "access granted\n", op->o_connid, attr , 0 );
243 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
244 " %s access granted\n",
250 /* use backend default access if no backend acls */
251 if( be != NULL && be->be_acl == NULL ) {
253 LDAP_LOG( ACL, DETAIL1,
254 "access_allowed: backend default %s access %s to \"%s\"\n",
255 access2str( access ),
256 be->be_dfltaccess >= access ? "granted" : "denied",
257 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
259 Debug( LDAP_DEBUG_ACL,
260 "=> access_allowed: backend default %s access %s to \"%s\"\n",
261 access2str( access ),
262 be->be_dfltaccess >= access ? "granted" : "denied",
263 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
265 ret = be->be_dfltaccess >= access;
269 /* be is always non-NULL */
270 /* use global default access if no global acls */
271 } else if ( be == NULL && global_acl == NULL ) {
273 LDAP_LOG( ACL, DETAIL1,
274 "access_allowed: global default %s access %s to \"%s\"\n",
275 access2str( access ),
276 global_default_access >= access ? "granted" : "denied",
279 Debug( LDAP_DEBUG_ACL,
280 "=> access_allowed: global default %s access %s to \"%s\"\n",
281 access2str( access ),
282 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
284 ret = global_default_access >= access;
293 assert( state->as_vd_acl != NULL );
295 a = state->as_vd_acl;
296 mask = state->as_vd_acl_mask;
297 count = state->as_vd_acl_count;
298 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
302 if ( state ) state->as_vi_acl = NULL;
306 memset(matches, '\0', sizeof(matches));
309 while((a = acl_get( a, &count, op, e, desc, val,
310 MAXREMATCHES, matches )) != NULL)
314 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
316 LDAP_LOG( ACL, DETAIL1,
317 "access_allowed: match[%d]: %d %d ",
318 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
320 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
321 (int)matches[i].rm_so, (int)matches[i].rm_eo );
323 if( matches[i].rm_so <= matches[0].rm_eo ) {
325 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
326 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
330 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
332 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
337 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
338 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
339 ret = state->as_result;
341 } else if (!st_initialized) {
342 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
344 state->as_vd_ad=desc;
350 control = acl_mask( a, &mask, op,
351 e, desc, val, matches, count, state );
353 if ( control != ACL_BREAK ) {
357 memset(matches, '\0', sizeof(matches));
360 if ( ACL_IS_INVALID( mask ) ) {
362 LDAP_LOG( ACL, DETAIL1,
363 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
364 op->o_connid, e->e_dn, attr );
366 Debug( LDAP_DEBUG_ACL,
367 "=> access_allowed: \"%s\" (%s) invalid!\n",
372 } else if ( control == ACL_BREAK ) {
374 LDAP_LOG( ACL, DETAIL1,
375 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
377 Debug( LDAP_DEBUG_ACL,
378 "=> access_allowed: no more rules\n", 0, 0, 0);
385 LDAP_LOG( ACL, ENTRY,
386 "access_allowed: %s access %s by %s\n",
387 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
388 accessmask2str( mask, accessmaskbuf ) );
390 Debug( LDAP_DEBUG_ACL,
391 "=> access_allowed: %s access %s by %s\n",
392 access2str( access ),
393 ACL_GRANT(mask, access) ? "granted" : "denied",
394 accessmask2str( mask, accessmaskbuf ) );
397 ret = ACL_GRANT(mask, access);
400 if( state != NULL ) {
401 /* If not value-dependent, save ACL in case of more attrs */
402 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
403 state->as_vi_acl = a;
404 state->as_result = ret;
406 state->as_recorded |= ACL_STATE_RECORDED;
408 if (be_null) op->o_bd = NULL;
413 * acl_get - return the acl applicable to entry e, attribute
414 * attr. the acl returned is suitable for use in subsequent calls to
415 * acl_access_allowed().
418 static AccessControl *
424 AttributeDescription *desc,
427 regmatch_t *matches )
433 assert( count != NULL );
434 assert( desc != NULL );
436 attr = desc->ad_cname.bv_val;
438 assert( attr != NULL );
441 if( op->o_bd == NULL ) {
444 a = op->o_bd->be_acl;
453 dnlen = e->e_nname.bv_len;
455 for ( ; a != NULL; a = a->acl_next ) {
458 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
459 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
461 LDAP_LOG( ACL, DETAIL1,
462 "acl_get: dnpat [%d] %s nsub: %d\n",
463 *count, a->acl_dn_pat.bv_val,
464 (int) a->acl_dn_re.re_nsub );
466 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
467 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
469 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
474 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
475 *count, a->acl_dn_pat.bv_val, 0 );
477 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
478 *count, a->acl_dn_pat.bv_val, 0 );
480 patlen = a->acl_dn_pat.bv_len;
481 if ( dnlen < patlen )
484 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
485 /* base dn -- entire object DN must match */
486 if ( dnlen != patlen )
489 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
492 if ( dnlen <= patlen )
495 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
498 rdnlen = dn_rdnlen( NULL, &e->e_nname );
499 if ( rdnlen != dnlen - patlen - 1 )
502 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
503 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
506 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
507 if ( dnlen <= patlen )
509 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
513 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
518 LDAP_LOG( ACL, DETAIL1,
519 "acl_get: [%d] matched\n", *count, 0, 0 );
521 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
526 if ( a->acl_filter != NULL ) {
527 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
528 if ( rc != LDAP_COMPARE_TRUE ) {
534 LDAP_LOG( ACL, DETAIL1,
535 "acl_get: [%d] check attr %s\n", *count, attr ,0 );
537 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
540 if ( a->acl_attrs == NULL ||
541 ad_inlist( desc, a->acl_attrs ) )
544 LDAP_LOG( ACL, DETAIL1,
545 "acl_get: [%d] acl %s attr: %s\n", *count, e->e_dn, attr );
547 Debug( LDAP_DEBUG_ACL,
548 "<= acl_get: [%d] acl %s attr: %s\n",
549 *count, e->e_dn, attr );
553 matches[0].rm_so = matches[0].rm_eo = -1;
557 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
559 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
565 * Record value-dependent access control state
567 #define ACL_RECORD_VALUE_STATE do { \
568 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
569 state->as_recorded |= ACL_STATE_RECORDED_VD; \
570 state->as_vd_acl = a; \
571 AC_MEMCPY( state->as_vd_acl_matches, matches, \
572 sizeof( state->as_vd_acl_matches )) ; \
573 state->as_vd_acl_count = count; \
574 state->as_vd_access = b; \
575 state->as_vd_access_count = i; \
580 * acl_mask - modifies mask based upon the given acl and the
581 * requested access to entry e, attribute attr, value val. if val
582 * is null, access to the whole attribute is assumed (all values).
584 * returns 0 access NOT allowed
588 static slap_control_t
594 AttributeDescription *desc,
598 AccessControlState *state )
600 int i, odnlen, patlen;
603 char accessmaskbuf[ACCESSMASK_MAXLEN];
604 char accessmaskbuf1[ACCESSMASK_MAXLEN];
609 assert( mask != NULL );
610 assert( desc != NULL );
612 attr = desc->ad_cname.bv_val;
614 assert( attr != NULL );
617 LDAP_LOG( ACL, ENTRY,
618 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
619 op->o_connid, e->e_dn, attr );
622 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
623 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
624 accessmask2str( *mask, accessmaskbuf ) );
626 Debug( LDAP_DEBUG_ACL,
627 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
630 Debug( LDAP_DEBUG_ACL,
631 "=> acl_mask: to %s by \"%s\", (%s) \n",
632 val ? "value" : "all values",
633 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
634 accessmask2str( *mask, accessmaskbuf ) );
637 /* Is this ACL only for a specific value? */
638 if ( a->acl_attrval.bv_len ) {
639 if ( state && !state->as_vd_acl ) {
640 state->as_vd_acl = a;
641 state->as_vd_access = a->acl_access;
642 state->as_vd_access_count = 1;
647 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
649 LDAP_LOG( ACL, DETAIL1,
650 "acl_get: valpat %s\n",
651 a->acl_attrval.bv_val, 0, 0 );
653 Debug( LDAP_DEBUG_ACL,
654 "acl_get: valpat %s\n",
655 a->acl_attrval.bv_val, 0, 0 );
657 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
663 LDAP_LOG( ACL, DETAIL1,
665 a->acl_attrval.bv_val, 0, 0 );
667 Debug( LDAP_DEBUG_ACL,
669 a->acl_attrval.bv_val, 0, 0 );
672 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
673 if (value_match( &match, desc,
674 desc->ad_type->sat_equality, 0,
675 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
680 int patlen, vdnlen, rc, got_match = 0;
681 struct berval vdn = { 0, NULL };
684 assert( a->acl_attrs[0].an_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName );
686 rc = dnNormalize( 0, NULL, NULL, val, &vdn,
688 if ( rc != LDAP_SUCCESS ) {
693 patlen = a->acl_attrval.bv_len;
696 if ( vdnlen < patlen )
697 goto attrval_cleanup;
699 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
700 if ( vdnlen > patlen )
701 goto attrval_cleanup;
703 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
706 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
707 goto attrval_cleanup;
709 rdnlen = dn_rdnlen( NULL, &vdn );
710 if ( rdnlen != vdnlen - patlen - 1 )
711 goto attrval_cleanup;
713 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
714 if ( vdnlen > patlen && !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
715 goto attrval_cleanup;
717 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
718 if ( vdnlen <= patlen )
719 goto attrval_cleanup;
721 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
722 goto attrval_cleanup;
725 got_match = strcmp( a->acl_attrval.bv_val, vdn.bv_val + vdnlen - patlen );
738 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
739 && state->as_vd_acl == a )
741 b = state->as_vd_access;
742 i = state->as_vd_access_count;
749 for ( ; b != NULL; b = b->a_next, i++ ) {
750 slap_mask_t oldmask, modmask;
752 ACL_INVALIDATE( modmask );
754 /* AND <who> clauses */
755 if ( b->a_dn_pat.bv_len != 0 ) {
757 LDAP_LOG( ACL, DETAIL1,
758 "acl_mask: conn %lu check a_dn_pat: %s\n",
759 op->o_connid, b->a_dn_pat.bv_val ,0 );
761 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
762 b->a_dn_pat.bv_val, 0, 0);
765 * if access applies to the entry itself, and the
766 * user is bound as somebody in the same namespace as
767 * the entry, OR the given dn matches the dn pattern
769 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
770 if ( op->o_ndn.bv_len != 0 ) {
774 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
775 if ( op->o_ndn.bv_len == 0 ) {
779 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
780 if ( op->o_ndn.bv_len == 0 ) {
784 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
788 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
789 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
790 int ret = regex_matches( &b->a_dn_pat,
791 op->o_ndn.bv_val, e->e_ndn, matches );
802 if ( e->e_dn == NULL )
805 if ( b->a_dn_expand ) {
807 char buf[ACL_BUF_SIZE];
809 bv.bv_len = sizeof( buf ) - 1;
812 string_expand(&bv, &b->a_dn_pat,
814 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
815 /* did not expand to a valid dn */
823 odnlen = op->o_ndn.bv_len;
824 if ( odnlen < patlen ) {
825 goto dn_match_cleanup;
829 if ( b->a_dn_style == ACL_STYLE_BASE ) {
830 /* base dn -- entire object DN must match */
831 if ( odnlen != patlen ) {
832 goto dn_match_cleanup;
835 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
838 if ( odnlen <= patlen ) {
839 goto dn_match_cleanup;
842 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
843 goto dn_match_cleanup;
846 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
847 if ( rdnlen != odnlen - patlen - 1 ) {
848 goto dn_match_cleanup;
851 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
852 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
853 goto dn_match_cleanup;
856 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
857 if ( odnlen <= patlen ) {
858 goto dn_match_cleanup;
861 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
862 goto dn_match_cleanup;
866 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
869 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
879 if ( b->a_sockurl_pat.bv_len ) {
880 if ( ! op->o_conn->c_listener ) {
884 LDAP_LOG( ACL, DETAIL1,
885 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
886 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
888 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
889 b->a_sockurl_pat.bv_val, 0, 0 );
892 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
893 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
894 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
895 e->e_ndn, matches ) )
900 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
906 if ( b->a_domain_pat.bv_len ) {
907 if ( !op->o_conn->c_peer_domain.bv_val ) {
911 LDAP_LOG( ACL, DETAIL1,
912 "acl_mask: conn %lu check a_domain_pat: %s\n",
913 op->o_connid, b->a_domain_pat.bv_val , 0 );
915 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
916 b->a_domain_pat.bv_val, 0, 0 );
918 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
919 if ( b->a_domain_style == ACL_STYLE_REGEX) {
920 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
921 e->e_ndn, matches ) )
926 char buf[ACL_BUF_SIZE];
928 struct berval cmp = op->o_conn->c_peer_domain;
929 struct berval pat = b->a_domain_pat;
931 if ( b->a_domain_expand ) {
934 bv.bv_len = sizeof(buf) - 1;
937 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
941 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
942 int offset = cmp.bv_len - pat.bv_len;
947 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
951 /* trim the domain */
952 cmp.bv_val = &cmp.bv_val[ offset ];
953 cmp.bv_len -= offset;
956 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
963 if ( b->a_peername_pat.bv_len ) {
964 if ( !op->o_conn->c_peer_name.bv_val ) {
968 LDAP_LOG( ACL, DETAIL1,
969 "acl_mask: conn %lu check a_peername_path: %s\n",
970 op->o_connid, b->a_peername_pat.bv_val , 0 );
972 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
973 b->a_peername_pat.bv_val, 0, 0 );
975 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
976 if ( b->a_peername_style == ACL_STYLE_REGEX) {
977 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
978 e->e_ndn, matches ) )
983 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 )
989 if ( b->a_sockname_pat.bv_len ) {
990 if ( !op->o_conn->c_sock_name.bv_val ) {
994 LDAP_LOG( ACL, DETAIL1,
995 "acl_mask: conn %lu check a_sockname_path: %s\n",
996 op->o_connid, b->a_sockname_pat.bv_val , 0 );
998 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
999 b->a_sockname_pat.bv_val, 0, 0 );
1001 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1002 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1003 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1004 e->e_ndn, matches ) )
1009 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1015 if ( b->a_dn_at != NULL ) {
1020 const char *attr = b->a_dn_at->ad_cname.bv_val;
1022 assert( attr != NULL );
1024 if ( op->o_ndn.bv_len == 0 ) {
1029 LDAP_LOG( ACL, DETAIL1,
1030 "acl_mask: conn %lu check a_dn_pat: %s\n",
1031 op->o_connid, attr , 0 );
1033 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1038 /* see if asker is listed in dnattr */
1039 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1041 at = attrs_find( at->a_next, b->a_dn_at ) )
1043 if( value_find_ex( b->a_dn_at,
1044 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1045 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1047 &bv, op->o_tmpmemctx ) == 0 )
1056 /* have a dnattr match. if this is a self clause then
1057 * the target must also match the op dn.
1059 if ( b->a_dn_self ) {
1060 /* check if the target is an attribute. */
1061 if ( val == NULL ) continue;
1063 /* target is attribute, check if the attribute value
1066 rc = value_match( &match, b->a_dn_at,
1067 b->a_dn_at->ad_type->sat_equality, 0,
1069 /* on match error or no match, fail the ACL clause */
1070 if (rc != LDAP_SUCCESS || match != 0 )
1074 /* no dnattr match, check if this is a self clause */
1075 if ( ! b->a_dn_self )
1078 ACL_RECORD_VALUE_STATE;
1080 /* this is a self clause, check if the target is an
1086 /* target is attribute, check if the attribute value
1089 rc = value_match( &match, b->a_dn_at,
1090 b->a_dn_at->ad_type->sat_equality, 0,
1093 /* on match error or no match, fail the ACL clause */
1094 if (rc != LDAP_SUCCESS || match != 0 )
1099 if ( b->a_group_pat.bv_len ) {
1101 struct berval ndn = { 0, NULL };
1104 if ( op->o_ndn.bv_len == 0 ) {
1108 /* b->a_group is an unexpanded entry name, expanded it should be an
1109 * entry with objectclass group* and we test to see if odn is one of
1110 * the values in the attribute group
1112 /* see if asker is listed in dnattr */
1113 if ( b->a_group_style == ACL_STYLE_REGEX ) {
1114 char buf[ACL_BUF_SIZE];
1115 bv.bv_len = sizeof(buf) - 1;
1118 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1119 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1120 /* did not expand to a valid dn */
1127 bv = b->a_group_pat;
1130 rc = backend_group( op, e, &bv, &op->o_ndn,
1131 b->a_group_oc, b->a_group_at );
1133 if ( ndn.bv_val ) free( ndn.bv_val );
1140 if ( b->a_set_pat.bv_len != 0 ) {
1142 char buf[ACL_BUF_SIZE];
1143 if( b->a_set_style == ACL_STYLE_REGEX ){
1144 bv.bv_len = sizeof(buf) - 1;
1146 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1150 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1155 if ( b->a_authz.sai_ssf ) {
1157 LDAP_LOG( ACL, DETAIL1,
1158 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1159 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1161 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1162 b->a_authz.sai_ssf, op->o_ssf, 0 );
1164 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1169 if ( b->a_authz.sai_transport_ssf ) {
1171 LDAP_LOG( ACL, DETAIL1,
1172 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1174 op->o_connid, b->a_authz.sai_transport_ssf,
1175 op->o_transport_ssf );
1177 Debug( LDAP_DEBUG_ACL,
1178 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1179 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1181 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1186 if ( b->a_authz.sai_tls_ssf ) {
1188 LDAP_LOG( ACL, DETAIL1,
1189 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1191 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1193 Debug( LDAP_DEBUG_ACL,
1194 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1195 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1197 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1202 if ( b->a_authz.sai_sasl_ssf ) {
1204 LDAP_LOG( ACL, DETAIL1,
1205 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1207 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1209 Debug( LDAP_DEBUG_ACL,
1210 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1211 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1213 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1218 #ifdef SLAPD_ACI_ENABLED
1219 if ( b->a_aci_at != NULL ) {
1221 slap_access_t grant, deny, tgrant, tdeny;
1222 struct berval parent_ndn, old_parent_ndn;
1223 BerVarray bvals = NULL;
1226 /* this case works different from the others above.
1227 * since aci's themselves give permissions, we need
1228 * to first check b->a_access_mask, the ACL's access level.
1231 if ( e->e_nname.bv_len == 0 ) {
1232 /* no ACIs in the root DSE */
1236 /* first check if the right being requested
1237 * is allowed by the ACL clause.
1239 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1242 /* start out with nothing granted, nothing denied */
1246 /* get the aci attribute */
1247 at = attr_find( e->e_attrs, b->a_aci_at );
1249 ACL_RECORD_VALUE_STATE;
1250 /* the aci is an multi-valued attribute. The
1251 * rights are determined by OR'ing the individual
1252 * rights given by the acis.
1254 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1258 matches, &grant, &deny, &aci_bv_entry ) != 0)
1264 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1265 accessmask2str(tgrant,accessmaskbuf),
1266 accessmask2str(tdeny, accessmaskbuf1), 0);
1269 /* If the entry level aci didn't contain anything valid for the
1270 * current operation, climb up the tree and evaluate the
1271 * acis with scope set to subtree
1273 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1274 dnParent(&(e->e_nname), &parent_ndn);
1275 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1276 old_parent_ndn = parent_ndn;
1277 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1278 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1282 for( i = 0; bvals[i].bv_val != NULL; i++){
1283 ACL_RECORD_VALUE_STATE;
1284 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1285 &grant, &deny, &aci_bv_children) != 0) {
1288 /* evaluation stops as soon as either a "deny" or a
1289 * "grant" directive matches.
1291 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1295 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1296 accessmask2str(tgrant,accessmaskbuf),
1297 accessmask2str(tdeny, accessmaskbuf1), 0);
1302 case LDAP_NO_SUCH_ATTRIBUTE:
1303 /* just go on if the aci-Attribute is not present in
1306 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1309 case LDAP_NO_SUCH_OBJECT:
1310 /* We have reached the base object */
1311 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1321 dnParent(&old_parent_ndn, &parent_ndn);
1326 /* remove anything that the ACL clause does not allow */
1327 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1328 tdeny &= ACL_PRIV_MASK;
1330 /* see if we have anything to contribute */
1331 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1335 /* this could be improved by changing acl_mask so that it can deal with
1336 * by clauses that return grant/deny pairs. Right now, it does either
1337 * additive or subtractive rights, but not both at the same time. So,
1338 * we need to combine the grant/deny pair into a single rights mask in
1339 * a smart way: if either grant or deny is "empty", then we use the
1340 * opposite as is, otherwise we remove any denied rights from the grant
1341 * rights mask and construct an additive mask.
1343 if (ACL_IS_INVALID(tdeny)) {
1344 modmask = tgrant | ACL_PRIV_ADDITIVE;
1346 } else if (ACL_IS_INVALID(tgrant)) {
1347 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1350 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1356 modmask = b->a_access_mask;
1360 LDAP_LOG( ACL, RESULTS,
1361 "acl_mask: [%d] applying %s (%s)\n",
1362 i, accessmask2str( modmask, accessmaskbuf),
1363 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1364 ? "break" : "stop" );
1366 Debug( LDAP_DEBUG_ACL,
1367 "<= acl_mask: [%d] applying %s (%s)\n",
1368 i, accessmask2str( modmask, accessmaskbuf ),
1369 b->a_type == ACL_CONTINUE
1371 : b->a_type == ACL_BREAK
1378 if( ACL_IS_ADDITIVE(modmask) ) {
1380 ACL_PRIV_SET( *mask, modmask );
1383 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1385 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1386 /* substract privs */
1387 ACL_PRIV_CLR( *mask, modmask );
1390 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1398 LDAP_LOG( ACL, DETAIL1,
1399 "acl_mask: conn %lu [%d] mask: %s\n",
1400 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1402 Debug( LDAP_DEBUG_ACL,
1403 "<= acl_mask: [%d] mask: %s\n",
1404 i, accessmask2str(*mask, accessmaskbuf), 0 );
1407 if( b->a_type == ACL_CONTINUE ) {
1410 } else if ( b->a_type == ACL_BREAK ) {
1418 /* implicit "by * none" clause */
1422 LDAP_LOG( ACL, RESULTS,
1423 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1424 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1426 Debug( LDAP_DEBUG_ACL,
1427 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1428 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1434 * acl_check_modlist - check access control on the given entry to see if
1435 * it allows the given modifications by the user associated with op.
1436 * returns 1 if mods allowed ok
1437 * 0 mods not allowed
1444 Modifications *mlist
1448 AccessControlState state = ACL_STATE_INIT;
1450 assert( op->o_bd != NULL );
1452 /* short circuit root database access */
1453 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1455 LDAP_LOG( ACL, DETAIL1,
1456 "acl_check_modlist: conn %lu access granted to root user\n",
1457 op->o_connid, 0, 0 );
1459 Debug( LDAP_DEBUG_ACL,
1460 "<= acl_access_allowed: granted to database root\n",
1466 /* use backend default access if no backend acls */
1467 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1469 LDAP_LOG( ACL, DETAIL1,
1470 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1471 access2str( ACL_WRITE ),
1472 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1475 Debug( LDAP_DEBUG_ACL,
1476 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1477 access2str( ACL_WRITE ),
1478 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1480 return op->o_bd->be_dfltaccess >= ACL_WRITE;
1483 /* op->o_bd is always non-NULL */
1484 /* use global default access if no global acls */
1485 } else if ( op->o_bd == NULL && global_acl == NULL ) {
1487 LDAP_LOG( ACL, DETAIL1,
1488 "acl_check_modlist: global default %s access %s to \"%s\"\n",
1489 access2str( ACL_WRITE ),
1490 global_default_access >= ACL_WRITE ? "granted" : "denied",
1493 Debug( LDAP_DEBUG_ACL,
1494 "=> access_allowed: global default %s access %s to \"%s\"\n",
1495 access2str( ACL_WRITE ),
1496 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1498 return global_default_access >= ACL_WRITE;
1502 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1504 * no-user-modification operational attributes are ignored
1505 * by ACL_WRITE checking as any found here are not provided
1508 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1510 LDAP_LOG( ACL, DETAIL1,
1511 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1512 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1514 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1515 " modify access granted\n",
1516 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1521 switch ( mlist->sml_op ) {
1522 case LDAP_MOD_REPLACE:
1524 * We must check both permission to delete the whole
1525 * attribute and permission to add the specific attributes.
1526 * This prevents abuse from selfwriters.
1528 if ( ! access_allowed( op, e,
1529 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1534 if ( mlist->sml_bvalues == NULL ) break;
1536 /* fall thru to check value to add */
1539 assert( mlist->sml_bvalues != NULL );
1541 for ( bv = mlist->sml_nvalues
1542 ? mlist->sml_nvalues : mlist->sml_values;
1543 bv->bv_val != NULL; bv++ )
1545 if ( ! access_allowed( op, e,
1546 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1553 case LDAP_MOD_DELETE:
1554 if ( mlist->sml_bvalues == NULL ) {
1555 if ( ! access_allowed( op, e,
1556 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1562 for ( bv = mlist->sml_nvalues
1563 ? mlist->sml_nvalues : mlist->sml_values;
1564 bv->bv_val != NULL; bv++ )
1566 if ( ! access_allowed( op, e,
1567 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1574 case SLAP_MOD_SOFTADD:
1575 /* allow adding attribute via modrdn thru */
1589 struct berval *list,
1603 while (len >= 0 && --ix >= 0) {
1604 while (--len >= 0 && *p++ != sep) ;
1606 while (len >= 0 && *p == ' ') {
1617 while (--len >= 0 && *p != sep) {
1621 while (bv->bv_len > 0 && *--p == ' ')
1627 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1629 AciSetCookie *cp = (AciSetCookie *)cookie;
1630 BerVarray bvals = NULL;
1633 /* this routine needs to return the bervals instead of
1634 * plain strings, since syntax is not known. It should
1635 * also return the syntax or some "comparison cookie".
1638 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1640 AttributeDescription *desc = NULL;
1641 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1642 backend_attribute(cp->op,
1643 cp->e, &ndn, desc, &bvals);
1645 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1652 struct berval *subj,
1658 struct berval set = { 0, NULL };
1660 AciSetCookie cookie;
1663 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1665 struct berval subjdn, ndn = { 0, NULL };
1666 struct berval setat;
1669 AttributeDescription *desc = NULL;
1671 /* format of string is "entry/setAttrName" */
1672 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1676 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1677 setat.bv_val = SLAPD_ACI_SET_ATTR;
1678 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1681 if ( setat.bv_val != NULL ) {
1683 * NOTE: dnNormalize honors the ber_len field
1684 * as the length of the dn to be normalized
1686 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1687 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1689 backend_attribute(op, e,
1690 &ndn, desc, &bvals);
1691 if ( bvals != NULL ) {
1692 if ( bvals[0].bv_val != NULL ) {
1695 bvals[0].bv_val = NULL;
1696 for (i=1;bvals[i].bv_val;i++);
1697 bvals[0].bv_val = bvals[i-1].bv_val;
1698 bvals[i-1].bv_val = NULL;
1700 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1708 if (set.bv_val != NULL) {
1711 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1712 &op->o_ndn, &e->e_nname, NULL) > 0);
1713 sl_free(set.bv_val, op->o_tmpmemctx);
1718 #ifdef SLAPD_ACI_ENABLED
1720 aci_list_map_rights(
1721 struct berval *list )
1728 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1731 switch (*bv.bv_val) {
1733 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1736 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1737 * the right 's' to mean "set", but in the examples states
1738 * that the right 's' means "search". The latter definition
1741 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1744 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1747 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1750 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1751 * define any equivalent to the AUTH right, so I've just used
1754 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1766 struct berval *list,
1767 const struct berval *attr,
1768 struct berval *val )
1770 struct berval bv, left, right;
1773 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1774 if (aci_get_part(&bv, 0, '=', &left) < 0
1775 || aci_get_part(&bv, 1, '=', &right) < 0)
1777 if (ber_bvstrcasecmp(attr, &bv) == 0)
1779 } else if (val == NULL) {
1780 if (ber_bvstrcasecmp(attr, &left) == 0)
1783 if (ber_bvstrcasecmp(attr, &left) == 0) {
1784 /* this is experimental code that implements a
1785 * simple (prefix) match of the attribute value.
1786 * the ACI draft does not provide for aci's that
1787 * apply to specific values, but it would be
1788 * nice to have. If the <attr> part of an aci's
1789 * rights list is of the form <attr>=<value>,
1790 * that means the aci applies only to attrs with
1791 * the given value. Furthermore, if the attr is
1792 * of the form <attr>=<value>*, then <value> is
1793 * treated as a prefix, and the aci applies to
1794 * any value with that prefix.
1796 * Ideally, this would allow r.e. matches.
1798 if (aci_get_part(&right, 0, '*', &left) < 0
1799 || right.bv_len <= left.bv_len)
1801 if (ber_bvstrcasecmp(val, &right) == 0)
1803 } else if (val->bv_len >= left.bv_len) {
1804 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1813 static slap_access_t
1814 aci_list_get_attr_rights(
1815 struct berval *list,
1816 const struct berval *attr,
1817 struct berval *val )
1823 /* loop through each rights/attr pair, skip first part (action) */
1825 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1826 if (aci_list_has_attr(&bv, attr, val) == 0)
1828 if (aci_get_part(list, i, ';', &bv) < 0)
1830 mask |= aci_list_map_rights(&bv);
1836 aci_list_get_rights(
1837 struct berval *list,
1838 const struct berval *attr,
1840 slap_access_t *grant,
1841 slap_access_t *deny )
1843 struct berval perm, actn;
1844 slap_access_t *mask;
1847 if (attr == NULL || attr->bv_len == 0
1848 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1849 attr = &aci_bv_br_entry;
1855 /* loop through each permissions clause */
1856 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1857 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1859 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1861 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1868 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1869 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1876 struct berval *subj,
1877 struct berval *defgrpoc,
1878 struct berval *defgrpat,
1884 struct berval subjdn;
1885 struct berval grpoc;
1886 struct berval grpat;
1887 ObjectClass *grp_oc = NULL;
1888 AttributeDescription *grp_ad = NULL;
1892 /* format of string is "group/objectClassValue/groupAttrName" */
1893 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1897 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1901 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1905 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1906 if( rc != LDAP_SUCCESS ) {
1912 grp_oc = oc_bvfind( &grpoc );
1914 if (grp_oc != NULL && grp_ad != NULL ) {
1915 char buf[ACL_BUF_SIZE];
1916 struct berval bv, ndn;
1917 bv.bv_len = sizeof( buf ) - 1;
1918 bv.bv_val = (char *)&buf;
1919 string_expand(&bv, &subjdn, e->e_ndn, matches);
1920 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1921 rc = (backend_group(op, e, &ndn, &op->o_ndn,
1922 grp_oc, grp_ad) == 0);
1935 AttributeDescription *desc,
1938 regmatch_t *matches,
1939 slap_access_t *grant,
1940 slap_access_t *deny,
1941 struct berval *scope
1944 struct berval bv, perms, sdn;
1948 assert( desc->ad_cname.bv_val != NULL );
1950 /* parse an aci of the form:
1951 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1953 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1954 a full description of the format for this attribute.
1955 Differences: "this" in the draft is "self" here, and
1956 "self" and "public" is in the position of dnType.
1958 For now, this routine only supports scope=entry.
1960 /* check that the aci has all 5 components */
1961 if (aci_get_part(aci, 4, '#', NULL) < 0)
1964 /* check that the aci family is supported */
1965 if (aci_get_part(aci, 0, '#', &bv) < 0)
1968 /* check that the scope matches */
1969 if (aci_get_part(aci, 1, '#', &bv) < 0
1970 || ber_bvstrcasecmp( scope, &bv ) != 0)
1975 /* get the list of permissions clauses, bail if empty */
1976 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1979 /* check if any permissions allow desired access */
1980 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1983 /* see if we have a DN match */
1984 if (aci_get_part(aci, 3, '#', &bv) < 0)
1987 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1990 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1993 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1994 if (dn_match( &op->o_ndn, &ndn))
2000 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2003 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2004 if (dn_match(&op->o_ndn, &e->e_nname))
2007 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2009 AttributeDescription *ad = NULL;
2012 rc = slap_bv2ad( &sdn, &ad, &text );
2014 if( rc != LDAP_SUCCESS ) {
2022 for(at = attrs_find( e->e_attrs, ad );
2024 at = attrs_find( at->a_next, ad ) )
2026 if (value_find_ex( ad,
2027 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2028 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2030 &bv, op->o_tmpmemctx) == 0 )
2040 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2041 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2044 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2045 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2048 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2049 if (aci_match_set(&sdn, op, e, 0))
2052 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2053 if (aci_match_set(&sdn, op, e, 1))
2061 #endif /* SLAPD_ACI_ENABLED */
2068 regmatch_t *matches)
2076 bv->bv_val[0] = '\0';
2077 bv->bv_len--; /* leave space for lone $ */
2080 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2081 sp < pat->bv_val + pat->bv_len ; sp++ )
2083 /* did we previously see a $ */
2085 if ( flag == 1 && *sp == '$' ) {
2090 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2093 } else if ( *sp >= '0' && *sp <= '9' ) {
2101 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2102 if ( *sp >= '0' && *sp <= '9' ) {
2103 n = 10*n + ( *sp - '0' );
2107 if ( *sp != /*'{'*/ '}' ) {
2112 if ( n >= MAXREMATCHES ) {
2117 i = matches[n].rm_so;
2118 l = matches[n].rm_eo;
2119 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2137 /* must have ended with a single $ */
2146 LDAP_LOG( ACL, DETAIL1,
2147 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2148 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2150 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2151 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2157 struct berval *pat, /* pattern to expand and match against */
2158 char *str, /* string to match against pattern */
2159 char *buf, /* buffer with $N expansion variables */
2160 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2164 char newbuf[ACL_BUF_SIZE];
2168 bv.bv_len = sizeof(newbuf) - 1;
2171 if(str == NULL) str = "";
2173 string_expand(&bv, pat, buf, matches);
2174 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2175 char error[ACL_BUF_SIZE];
2176 regerror(rc, &re, error, sizeof(error));
2180 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2181 pat->bv_val, str, error );
2183 Debug( LDAP_DEBUG_TRACE,
2184 "compile( \"%s\", \"%s\") failed %s\n",
2185 pat->bv_val, str, error );
2190 rc = regexec(&re, str, 0, NULL, 0);
2194 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2195 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2196 rc, rc ? "matches" : "no matches", 0 );
2198 Debug( LDAP_DEBUG_TRACE,
2199 "=> regex_matches: string: %s\n", str, 0, 0 );
2200 Debug( LDAP_DEBUG_TRACE,
2201 "=> regex_matches: rc: %d %s\n",
2202 rc, !rc ? "matches" : "no matches", 0 );