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>
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_group_class = BER_BVC(SLAPD_GROUP_CLASS),
67 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
68 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
69 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR);
72 static AccessControl * acl_get(
73 AccessControl *ac, int *count,
74 Operation *op, Entry *e,
75 AttributeDescription *desc,
77 int nmatches, regmatch_t *matches );
79 static slap_control_t acl_mask(
80 AccessControl *ac, slap_mask_t *mask,
81 Operation *op, Entry *e,
82 AttributeDescription *desc,
86 AccessControlState *state );
88 #ifdef SLAPD_ACI_ENABLED
90 Operation *op, Entry *e,
91 AttributeDescription *desc,
97 struct berval *scope);
100 static int regex_matches(
101 struct berval *pat, char *str, char *buf, regmatch_t *matches);
102 static void string_expand(
103 struct berval *newbuf, struct berval *pattern,
104 char *match, regmatch_t *matches);
106 typedef struct AciSetCookie {
111 SLAP_SET_GATHER aci_set_gather;
112 static int aci_match_set ( struct berval *subj, Operation *op,
113 Entry *e, int setref );
116 * access_allowed - check whether op->o_ndn is allowed the requested access
117 * to entry e, attribute attr, value val. if val is null, access to
118 * the whole attribute is assumed (all values).
120 * This routine loops through all access controls and calls
121 * acl_mask() on each applicable access control.
122 * The loop exits when a definitive answer is reached or
123 * or no more controls remain.
134 AttributeDescription *desc,
136 slap_access_t access,
137 AccessControlState *state )
141 AccessControl *a = NULL;
146 char accessmaskbuf[ACCESSMASK_MAXLEN];
149 slap_control_t control;
151 regmatch_t matches[MAXREMATCHES];
152 int st_same_attr = 0;
153 int st_initialized = 0;
154 static AccessControlState state_init = ACL_STATE_INIT;
157 assert( desc != NULL );
158 assert( access > ACL_NONE );
160 attr = desc->ad_cname.bv_val;
162 assert( attr != NULL );
164 if( op && op->o_is_auth_check &&
165 ( access == ACL_SEARCH || access == ACL_READ ))
170 if( state && state->as_recorded && state->as_vd_ad==desc) {
171 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
174 return state->as_result;
176 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
177 val != NULL && state->as_vd_acl == NULL )
179 return state->as_result;
185 state->as_vd_ad=desc;
189 LDAP_LOG( ACL, ENTRY,
190 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
191 access2str( access ), e->e_dn, attr );
193 Debug( LDAP_DEBUG_ACL,
194 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
195 access2str( access ), e->e_dn, attr );
205 !slapi_int_access_allowed( op, e, desc, val, access, state )) {
206 /* ACL plugin denied access */
209 #endif /* LDAP_SLAPI */
217 assert( be != NULL );
219 /* grant database root access */
220 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
223 "access_allowed: conn %lu root access granted\n",
224 op->o_connid, 0, 0 );
226 Debug( LDAP_DEBUG_ACL,
227 "<= root access granted\n",
234 * no-user-modification operational attributes are ignored
235 * by ACL_WRITE checking as any found here are not provided
238 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
239 && desc != slap_schema.si_ad_entry
240 && desc != slap_schema.si_ad_children )
243 LDAP_LOG( ACL, DETAIL1,
244 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
245 "access granted\n", op->o_connid, attr , 0 );
247 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
248 " %s access granted\n",
254 /* use backend default access if no backend acls */
255 if( be != NULL && be->be_acl == NULL ) {
257 LDAP_LOG( ACL, DETAIL1,
258 "access_allowed: backend default %s access %s to \"%s\"\n",
259 access2str( access ),
260 be->be_dfltaccess >= access ? "granted" : "denied",
261 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
263 Debug( LDAP_DEBUG_ACL,
264 "=> access_allowed: backend default %s access %s to \"%s\"\n",
265 access2str( access ),
266 be->be_dfltaccess >= access ? "granted" : "denied",
267 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
269 ret = be->be_dfltaccess >= access;
273 /* be is always non-NULL */
274 /* use global default access if no global acls */
275 } else if ( be == NULL && global_acl == NULL ) {
277 LDAP_LOG( ACL, DETAIL1,
278 "access_allowed: global default %s access %s to \"%s\"\n",
279 access2str( access ),
280 global_default_access >= access ? "granted" : "denied",
283 Debug( LDAP_DEBUG_ACL,
284 "=> access_allowed: global default %s access %s to \"%s\"\n",
285 access2str( access ),
286 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
288 ret = global_default_access >= access;
297 assert( state->as_vd_acl != NULL );
299 a = state->as_vd_acl;
300 mask = state->as_vd_acl_mask;
301 count = state->as_vd_acl_count;
302 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
306 if ( state ) state->as_vi_acl = NULL;
310 memset(matches, '\0', sizeof(matches));
313 while((a = acl_get( a, &count, op, e, desc, val,
314 MAXREMATCHES, matches )) != NULL)
318 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
320 LDAP_LOG( ACL, DETAIL1,
321 "access_allowed: match[%d]: %d %d ",
322 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
324 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
325 (int)matches[i].rm_so, (int)matches[i].rm_eo );
327 if( matches[i].rm_so <= matches[0].rm_eo ) {
329 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
330 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
334 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
336 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
341 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
342 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
343 ret = state->as_result;
345 } else if (!st_initialized) {
346 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
348 state->as_vd_ad=desc;
354 control = acl_mask( a, &mask, op,
355 e, desc, val, matches, count, state );
357 if ( control != ACL_BREAK ) {
361 memset(matches, '\0', sizeof(matches));
364 if ( ACL_IS_INVALID( mask ) ) {
366 LDAP_LOG( ACL, DETAIL1,
367 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
368 op->o_connid, e->e_dn, attr );
370 Debug( LDAP_DEBUG_ACL,
371 "=> access_allowed: \"%s\" (%s) invalid!\n",
376 } else if ( control == ACL_BREAK ) {
378 LDAP_LOG( ACL, DETAIL1,
379 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
381 Debug( LDAP_DEBUG_ACL,
382 "=> access_allowed: no more rules\n", 0, 0, 0);
389 LDAP_LOG( ACL, ENTRY,
390 "access_allowed: %s access %s by %s\n",
391 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
392 accessmask2str( mask, accessmaskbuf ) );
394 Debug( LDAP_DEBUG_ACL,
395 "=> access_allowed: %s access %s by %s\n",
396 access2str( access ),
397 ACL_GRANT(mask, access) ? "granted" : "denied",
398 accessmask2str( mask, accessmaskbuf ) );
401 ret = ACL_GRANT(mask, access);
404 if( state != NULL ) {
405 /* If not value-dependent, save ACL in case of more attrs */
406 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
407 state->as_vi_acl = a;
408 state->as_result = ret;
410 state->as_recorded |= ACL_STATE_RECORDED;
412 if (be_null) op->o_bd = NULL;
417 * acl_get - return the acl applicable to entry e, attribute
418 * attr. the acl returned is suitable for use in subsequent calls to
419 * acl_access_allowed().
422 static AccessControl *
428 AttributeDescription *desc,
431 regmatch_t *matches )
437 assert( count != NULL );
438 assert( desc != NULL );
440 attr = desc->ad_cname.bv_val;
442 assert( attr != NULL );
445 if( op->o_bd == NULL ) {
448 a = op->o_bd->be_acl;
457 dnlen = e->e_nname.bv_len;
459 for ( ; a != NULL; a = a->acl_next ) {
462 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
463 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
465 LDAP_LOG( ACL, DETAIL1,
466 "acl_get: dnpat [%d] %s nsub: %d\n",
467 *count, a->acl_dn_pat.bv_val,
468 (int) a->acl_dn_re.re_nsub );
470 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
471 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
473 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
478 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
479 *count, a->acl_dn_pat.bv_val, 0 );
481 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
482 *count, a->acl_dn_pat.bv_val, 0 );
484 patlen = a->acl_dn_pat.bv_len;
485 if ( dnlen < patlen )
488 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
489 /* base dn -- entire object DN must match */
490 if ( dnlen != patlen )
493 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
496 if ( dnlen <= patlen )
499 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
502 rdnlen = dn_rdnlen( NULL, &e->e_nname );
503 if ( rdnlen != dnlen - patlen - 1 )
506 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
507 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
510 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
511 if ( dnlen <= patlen )
513 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
517 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
522 LDAP_LOG( ACL, DETAIL1,
523 "acl_get: [%d] matched\n", *count, 0, 0 );
525 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
530 if ( a->acl_filter != NULL ) {
531 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
532 if ( rc != LDAP_COMPARE_TRUE ) {
538 LDAP_LOG( ACL, DETAIL1,
539 "acl_get: [%d] check attr %s\n", *count, attr ,0 );
541 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
544 if ( a->acl_attrs == NULL ||
545 ad_inlist( desc, a->acl_attrs ) )
548 LDAP_LOG( ACL, DETAIL1,
549 "acl_get: [%d] acl %s attr: %s\n", *count, e->e_dn, attr );
551 Debug( LDAP_DEBUG_ACL,
552 "<= acl_get: [%d] acl %s attr: %s\n",
553 *count, e->e_dn, attr );
557 matches[0].rm_so = matches[0].rm_eo = -1;
561 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
569 * Record value-dependent access control state
571 #define ACL_RECORD_VALUE_STATE do { \
572 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
573 state->as_recorded |= ACL_STATE_RECORDED_VD; \
574 state->as_vd_acl = a; \
575 AC_MEMCPY( state->as_vd_acl_matches, matches, \
576 sizeof( state->as_vd_acl_matches )) ; \
577 state->as_vd_acl_count = count; \
578 state->as_vd_access = b; \
579 state->as_vd_access_count = i; \
584 * acl_mask - modifies mask based upon the given acl and the
585 * requested access to entry e, attribute attr, value val. if val
586 * is null, access to the whole attribute is assumed (all values).
588 * returns 0 access NOT allowed
592 static slap_control_t
598 AttributeDescription *desc,
602 AccessControlState *state )
604 int i, odnlen, patlen;
607 char accessmaskbuf[ACCESSMASK_MAXLEN];
608 char accessmaskbuf1[ACCESSMASK_MAXLEN];
613 assert( mask != NULL );
614 assert( desc != NULL );
616 attr = desc->ad_cname.bv_val;
618 assert( attr != NULL );
621 LDAP_LOG( ACL, ENTRY,
622 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
623 op->o_connid, e->e_dn, attr );
626 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
627 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
628 accessmask2str( *mask, accessmaskbuf ) );
630 Debug( LDAP_DEBUG_ACL,
631 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
634 Debug( LDAP_DEBUG_ACL,
635 "=> acl_mask: to %s by \"%s\", (%s) \n",
636 val ? "value" : "all values",
637 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
638 accessmask2str( *mask, accessmaskbuf ) );
641 /* Is this ACL only for a specific value? */
642 if ( a->acl_attrval.bv_len ) {
643 if ( state && !state->as_vd_acl ) {
644 state->as_vd_acl = a;
645 state->as_vd_access = a->acl_access;
646 state->as_vd_access_count = 1;
651 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
653 LDAP_LOG( ACL, DETAIL1,
654 "acl_get: valpat %s\n",
655 a->acl_attrval.bv_val, 0, 0 );
657 Debug( LDAP_DEBUG_ACL,
658 "acl_get: valpat %s\n",
659 a->acl_attrval.bv_val, 0, 0 );
661 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
667 LDAP_LOG( ACL, DETAIL1,
669 a->acl_attrval.bv_val, 0, 0 );
671 Debug( LDAP_DEBUG_ACL,
673 a->acl_attrval.bv_val, 0, 0 );
676 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
677 if (value_match( &match, desc,
678 desc->ad_type->sat_equality, 0,
679 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
684 int patlen, vdnlen, rc, got_match = 0;
685 struct berval vdn = { 0, NULL };
688 assert( a->acl_attrs[0].an_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName );
690 rc = dnNormalize( 0, NULL, NULL, val, &vdn,
692 if ( rc != LDAP_SUCCESS ) {
697 patlen = a->acl_attrval.bv_len;
700 if ( vdnlen < patlen )
701 goto attrval_cleanup;
703 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
704 if ( vdnlen > patlen )
705 goto attrval_cleanup;
707 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
710 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
711 goto attrval_cleanup;
713 rdnlen = dn_rdnlen( NULL, &vdn );
714 if ( rdnlen != vdnlen - patlen - 1 )
715 goto attrval_cleanup;
717 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
718 if ( vdnlen > patlen && !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
719 goto attrval_cleanup;
721 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
722 if ( vdnlen <= patlen )
723 goto attrval_cleanup;
725 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
726 goto attrval_cleanup;
729 got_match = strcmp( a->acl_attrval.bv_val, vdn.bv_val + vdnlen - patlen );
742 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
743 && state->as_vd_acl == a )
745 b = state->as_vd_access;
746 i = state->as_vd_access_count;
753 for ( ; b != NULL; b = b->a_next, i++ ) {
754 slap_mask_t oldmask, modmask;
756 ACL_INVALIDATE( modmask );
758 /* AND <who> clauses */
759 if ( b->a_dn_pat.bv_len != 0 ) {
761 LDAP_LOG( ACL, DETAIL1,
762 "acl_mask: conn %lu check a_dn_pat: %s\n",
763 op->o_connid, b->a_dn_pat.bv_val ,0 );
765 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
766 b->a_dn_pat.bv_val, 0, 0);
769 * if access applies to the entry itself, and the
770 * user is bound as somebody in the same namespace as
771 * the entry, OR the given dn matches the dn pattern
773 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
774 if ( op->o_ndn.bv_len != 0 ) {
778 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
779 if ( op->o_ndn.bv_len == 0 ) {
783 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
784 if ( op->o_ndn.bv_len == 0 ) {
788 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
792 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
793 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
794 int ret = regex_matches( &b->a_dn_pat,
795 op->o_ndn.bv_val, e->e_ndn, matches );
806 if ( e->e_dn == NULL )
809 if ( b->a_dn_expand ) {
811 char buf[ACL_BUF_SIZE];
813 bv.bv_len = sizeof( buf ) - 1;
816 string_expand(&bv, &b->a_dn_pat,
818 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
819 /* did not expand to a valid dn */
827 odnlen = op->o_ndn.bv_len;
828 if ( odnlen < patlen ) {
829 goto dn_match_cleanup;
833 if ( b->a_dn_style == ACL_STYLE_BASE ) {
834 /* base dn -- entire object DN must match */
835 if ( odnlen != patlen ) {
836 goto dn_match_cleanup;
839 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
842 if ( odnlen <= patlen ) {
843 goto dn_match_cleanup;
846 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
847 goto dn_match_cleanup;
850 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
851 if ( rdnlen != odnlen - patlen - 1 ) {
852 goto dn_match_cleanup;
855 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
856 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
857 goto dn_match_cleanup;
860 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
861 if ( odnlen <= patlen ) {
862 goto dn_match_cleanup;
865 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
866 goto dn_match_cleanup;
870 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
873 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
883 if ( b->a_sockurl_pat.bv_len ) {
884 if ( ! op->o_conn->c_listener ) {
888 LDAP_LOG( ACL, DETAIL1,
889 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
890 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
892 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
893 b->a_sockurl_pat.bv_val, 0, 0 );
896 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
897 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
898 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
899 e->e_ndn, matches ) )
904 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
910 if ( b->a_domain_pat.bv_len ) {
911 if ( !op->o_conn->c_peer_domain.bv_val ) {
915 LDAP_LOG( ACL, DETAIL1,
916 "acl_mask: conn %lu check a_domain_pat: %s\n",
917 op->o_connid, b->a_domain_pat.bv_val , 0 );
919 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
920 b->a_domain_pat.bv_val, 0, 0 );
922 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
923 if ( b->a_domain_style == ACL_STYLE_REGEX) {
924 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
925 e->e_ndn, matches ) )
930 char buf[ACL_BUF_SIZE];
932 struct berval cmp = op->o_conn->c_peer_domain;
933 struct berval pat = b->a_domain_pat;
935 if ( b->a_domain_expand ) {
938 bv.bv_len = sizeof(buf) - 1;
941 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
945 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
946 int offset = cmp.bv_len - pat.bv_len;
951 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
955 /* trim the domain */
956 cmp.bv_val = &cmp.bv_val[ offset ];
957 cmp.bv_len -= offset;
960 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
967 if ( b->a_peername_pat.bv_len ) {
968 if ( !op->o_conn->c_peer_name.bv_val ) {
972 LDAP_LOG( ACL, DETAIL1,
973 "acl_mask: conn %lu check a_peername_path: %s\n",
974 op->o_connid, b->a_peername_pat.bv_val , 0 );
976 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
977 b->a_peername_pat.bv_val, 0, 0 );
979 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
980 if ( b->a_peername_style == ACL_STYLE_REGEX) {
981 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
982 e->e_ndn, matches ) )
987 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 )
993 if ( b->a_sockname_pat.bv_len ) {
994 if ( !op->o_conn->c_sock_name.bv_val ) {
998 LDAP_LOG( ACL, DETAIL1,
999 "acl_mask: conn %lu check a_sockname_path: %s\n",
1000 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1002 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1003 b->a_sockname_pat.bv_val, 0, 0 );
1005 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1006 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1007 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1008 e->e_ndn, matches ) )
1013 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1019 if ( b->a_dn_at != NULL ) {
1024 const char *attr = b->a_dn_at->ad_cname.bv_val;
1026 assert( attr != NULL );
1028 if ( op->o_ndn.bv_len == 0 ) {
1033 LDAP_LOG( ACL, DETAIL1,
1034 "acl_mask: conn %lu check a_dn_pat: %s\n",
1035 op->o_connid, attr , 0 );
1037 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1042 /* see if asker is listed in dnattr */
1043 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1045 at = attrs_find( at->a_next, b->a_dn_at ) )
1047 if( value_find_ex( b->a_dn_at,
1048 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1049 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1051 &bv, op->o_tmpmemctx ) == 0 )
1060 /* have a dnattr match. if this is a self clause then
1061 * the target must also match the op dn.
1063 if ( b->a_dn_self ) {
1064 /* check if the target is an attribute. */
1065 if ( val == NULL ) continue;
1067 /* target is attribute, check if the attribute value
1070 rc = value_match( &match, b->a_dn_at,
1071 b->a_dn_at->ad_type->sat_equality, 0,
1073 /* on match error or no match, fail the ACL clause */
1074 if (rc != LDAP_SUCCESS || match != 0 )
1078 /* no dnattr match, check if this is a self clause */
1079 if ( ! b->a_dn_self )
1082 ACL_RECORD_VALUE_STATE;
1084 /* this is a self clause, check if the target is an
1090 /* target is attribute, check if the attribute value
1093 rc = value_match( &match, b->a_dn_at,
1094 b->a_dn_at->ad_type->sat_equality, 0,
1097 /* on match error or no match, fail the ACL clause */
1098 if (rc != LDAP_SUCCESS || match != 0 )
1103 if ( b->a_group_pat.bv_len ) {
1105 struct berval ndn = { 0, NULL };
1108 if ( op->o_ndn.bv_len == 0 ) {
1112 /* b->a_group is an unexpanded entry name, expanded it should be an
1113 * entry with objectclass group* and we test to see if odn is one of
1114 * the values in the attribute group
1116 /* see if asker is listed in dnattr */
1117 if ( b->a_group_style == ACL_STYLE_REGEX ) {
1118 char buf[ACL_BUF_SIZE];
1119 bv.bv_len = sizeof(buf) - 1;
1122 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1123 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1124 /* did not expand to a valid dn */
1131 bv = b->a_group_pat;
1134 rc = backend_group( op, e, &bv, &op->o_ndn,
1135 b->a_group_oc, b->a_group_at );
1137 if ( ndn.bv_val ) free( ndn.bv_val );
1144 if ( b->a_set_pat.bv_len != 0 ) {
1146 char buf[ACL_BUF_SIZE];
1147 if( b->a_set_style == ACL_STYLE_REGEX ){
1148 bv.bv_len = sizeof(buf) - 1;
1150 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1154 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1159 if ( b->a_authz.sai_ssf ) {
1161 LDAP_LOG( ACL, DETAIL1,
1162 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1163 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1165 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1166 b->a_authz.sai_ssf, op->o_ssf, 0 );
1168 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1173 if ( b->a_authz.sai_transport_ssf ) {
1175 LDAP_LOG( ACL, DETAIL1,
1176 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1178 op->o_connid, b->a_authz.sai_transport_ssf,
1179 op->o_transport_ssf );
1181 Debug( LDAP_DEBUG_ACL,
1182 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1183 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1185 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1190 if ( b->a_authz.sai_tls_ssf ) {
1192 LDAP_LOG( ACL, DETAIL1,
1193 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1195 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1197 Debug( LDAP_DEBUG_ACL,
1198 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1199 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1201 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1206 if ( b->a_authz.sai_sasl_ssf ) {
1208 LDAP_LOG( ACL, DETAIL1,
1209 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1211 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1213 Debug( LDAP_DEBUG_ACL,
1214 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1215 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1217 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1222 #ifdef SLAPD_ACI_ENABLED
1223 if ( b->a_aci_at != NULL ) {
1225 slap_access_t grant, deny, tgrant, tdeny;
1226 struct berval parent_ndn, old_parent_ndn;
1227 BerVarray bvals = NULL;
1230 /* this case works different from the others above.
1231 * since aci's themselves give permissions, we need
1232 * to first check b->a_access_mask, the ACL's access level.
1235 if ( e->e_nname.bv_len == 0 ) {
1236 /* no ACIs in the root DSE */
1240 /* first check if the right being requested
1241 * is allowed by the ACL clause.
1243 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1246 /* start out with nothing granted, nothing denied */
1250 /* get the aci attribute */
1251 at = attr_find( e->e_attrs, b->a_aci_at );
1253 ACL_RECORD_VALUE_STATE;
1254 /* the aci is an multi-valued attribute. The
1255 * rights are determined by OR'ing the individual
1256 * rights given by the acis.
1258 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1262 matches, &grant, &deny, &aci_bv_entry ) != 0)
1268 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1269 accessmask2str(tgrant,accessmaskbuf),
1270 accessmask2str(tdeny, accessmaskbuf1), 0);
1273 /* If the entry level aci didn't contain anything valid for the
1274 * current operation, climb up the tree and evaluate the
1275 * acis with scope set to subtree
1277 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1278 dnParent(&(e->e_nname), &parent_ndn);
1279 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1280 old_parent_ndn = parent_ndn;
1281 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1282 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1286 for( i = 0; bvals[i].bv_val != NULL; i++){
1287 ACL_RECORD_VALUE_STATE;
1288 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1289 &grant, &deny, &aci_bv_children) != 0) {
1292 /* evaluation stops as soon as either a "deny" or a
1293 * "grant" directive matches.
1295 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1299 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1300 accessmask2str(tgrant,accessmaskbuf),
1301 accessmask2str(tdeny, accessmaskbuf1), 0);
1306 case LDAP_NO_SUCH_ATTRIBUTE:
1307 /* just go on if the aci-Attribute is not present in
1310 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1313 case LDAP_NO_SUCH_OBJECT:
1314 /* We have reached the base object */
1315 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1325 dnParent(&old_parent_ndn, &parent_ndn);
1330 /* remove anything that the ACL clause does not allow */
1331 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1332 tdeny &= ACL_PRIV_MASK;
1334 /* see if we have anything to contribute */
1335 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1339 /* this could be improved by changing acl_mask so that it can deal with
1340 * by clauses that return grant/deny pairs. Right now, it does either
1341 * additive or subtractive rights, but not both at the same time. So,
1342 * we need to combine the grant/deny pair into a single rights mask in
1343 * a smart way: if either grant or deny is "empty", then we use the
1344 * opposite as is, otherwise we remove any denied rights from the grant
1345 * rights mask and construct an additive mask.
1347 if (ACL_IS_INVALID(tdeny)) {
1348 modmask = tgrant | ACL_PRIV_ADDITIVE;
1350 } else if (ACL_IS_INVALID(tgrant)) {
1351 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1354 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1360 modmask = b->a_access_mask;
1364 LDAP_LOG( ACL, RESULTS,
1365 "acl_mask: [%d] applying %s (%s)\n",
1366 i, accessmask2str( modmask, accessmaskbuf),
1367 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1368 ? "break" : "stop" );
1370 Debug( LDAP_DEBUG_ACL,
1371 "<= acl_mask: [%d] applying %s (%s)\n",
1372 i, accessmask2str( modmask, accessmaskbuf ),
1373 b->a_type == ACL_CONTINUE
1375 : b->a_type == ACL_BREAK
1382 if( ACL_IS_ADDITIVE(modmask) ) {
1384 ACL_PRIV_SET( *mask, modmask );
1387 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1389 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1390 /* substract privs */
1391 ACL_PRIV_CLR( *mask, modmask );
1394 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1402 LDAP_LOG( ACL, DETAIL1,
1403 "acl_mask: conn %lu [%d] mask: %s\n",
1404 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1406 Debug( LDAP_DEBUG_ACL,
1407 "<= acl_mask: [%d] mask: %s\n",
1408 i, accessmask2str(*mask, accessmaskbuf), 0 );
1411 if( b->a_type == ACL_CONTINUE ) {
1414 } else if ( b->a_type == ACL_BREAK ) {
1422 /* implicit "by * none" clause */
1426 LDAP_LOG( ACL, RESULTS,
1427 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1428 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1430 Debug( LDAP_DEBUG_ACL,
1431 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1432 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1438 * acl_check_modlist - check access control on the given entry to see if
1439 * it allows the given modifications by the user associated with op.
1440 * returns 1 if mods allowed ok
1441 * 0 mods not allowed
1448 Modifications *mlist
1452 AccessControlState state = ACL_STATE_INIT;
1454 assert( op->o_bd != NULL );
1456 /* short circuit root database access */
1457 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1459 LDAP_LOG( ACL, DETAIL1,
1460 "acl_check_modlist: conn %lu access granted to root user\n",
1461 op->o_connid, 0, 0 );
1463 Debug( LDAP_DEBUG_ACL,
1464 "<= acl_access_allowed: granted to database root\n",
1470 /* use backend default access if no backend acls */
1471 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1473 LDAP_LOG( ACL, DETAIL1,
1474 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1475 access2str( ACL_WRITE ),
1476 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1479 Debug( LDAP_DEBUG_ACL,
1480 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1481 access2str( ACL_WRITE ),
1482 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1484 return op->o_bd->be_dfltaccess >= ACL_WRITE;
1487 /* op->o_bd is always non-NULL */
1488 /* use global default access if no global acls */
1489 } else if ( op->o_bd == NULL && global_acl == NULL ) {
1491 LDAP_LOG( ACL, DETAIL1,
1492 "acl_check_modlist: global default %s access %s to \"%s\"\n",
1493 access2str( ACL_WRITE ),
1494 global_default_access >= ACL_WRITE ? "granted" : "denied",
1497 Debug( LDAP_DEBUG_ACL,
1498 "=> access_allowed: global default %s access %s to \"%s\"\n",
1499 access2str( ACL_WRITE ),
1500 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1502 return global_default_access >= ACL_WRITE;
1506 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1508 * no-user-modification operational attributes are ignored
1509 * by ACL_WRITE checking as any found here are not provided
1512 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1514 LDAP_LOG( ACL, DETAIL1,
1515 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1516 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1518 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1519 " modify access granted\n",
1520 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1525 switch ( mlist->sml_op ) {
1526 case LDAP_MOD_REPLACE:
1528 * We must check both permission to delete the whole
1529 * attribute and permission to add the specific attributes.
1530 * This prevents abuse from selfwriters.
1532 if ( ! access_allowed( op, e,
1533 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1538 if ( mlist->sml_bvalues == NULL ) break;
1540 /* fall thru to check value to add */
1543 assert( mlist->sml_bvalues != NULL );
1545 for ( bv = mlist->sml_nvalues
1546 ? mlist->sml_nvalues : mlist->sml_values;
1547 bv->bv_val != NULL; bv++ )
1549 if ( ! access_allowed( op, e,
1550 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1557 case LDAP_MOD_DELETE:
1558 if ( mlist->sml_bvalues == NULL ) {
1559 if ( ! access_allowed( op, e,
1560 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1566 for ( bv = mlist->sml_nvalues
1567 ? mlist->sml_nvalues : mlist->sml_values;
1568 bv->bv_val != NULL; bv++ )
1570 if ( ! access_allowed( op, e,
1571 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1578 case SLAP_MOD_SOFTADD:
1579 /* allow adding attribute via modrdn thru */
1593 struct berval *list,
1607 while (len >= 0 && --ix >= 0) {
1608 while (--len >= 0 && *p++ != sep) ;
1610 while (len >= 0 && *p == ' ') {
1621 while (--len >= 0 && *p != sep) {
1625 while (bv->bv_len > 0 && *--p == ' ')
1631 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1633 AciSetCookie *cp = (AciSetCookie *)cookie;
1634 BerVarray bvals = NULL;
1637 /* this routine needs to return the bervals instead of
1638 * plain strings, since syntax is not known. It should
1639 * also return the syntax or some "comparison cookie".
1642 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1644 AttributeDescription *desc = NULL;
1645 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1646 backend_attribute(cp->op,
1647 cp->e, &ndn, desc, &bvals);
1649 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1656 struct berval *subj,
1662 struct berval set = { 0, NULL };
1664 AciSetCookie cookie;
1667 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1669 struct berval subjdn, ndn = { 0, NULL };
1670 struct berval setat;
1673 AttributeDescription *desc = NULL;
1675 /* format of string is "entry/setAttrName" */
1676 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1680 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1681 setat.bv_val = SLAPD_ACI_SET_ATTR;
1682 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1685 if ( setat.bv_val != NULL ) {
1687 * NOTE: dnNormalize honors the ber_len field
1688 * as the length of the dn to be normalized
1690 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1691 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1693 backend_attribute(op, e,
1694 &ndn, desc, &bvals);
1695 if ( bvals != NULL ) {
1696 if ( bvals[0].bv_val != NULL ) {
1699 bvals[0].bv_val = NULL;
1700 for (i=1;bvals[i].bv_val;i++);
1701 bvals[0].bv_val = bvals[i-1].bv_val;
1702 bvals[i-1].bv_val = NULL;
1704 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1712 if (set.bv_val != NULL) {
1715 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1716 &op->o_ndn, &e->e_nname, NULL) > 0);
1717 sl_free(set.bv_val, op->o_tmpmemctx);
1722 #ifdef SLAPD_ACI_ENABLED
1724 aci_list_map_rights(
1725 struct berval *list )
1732 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1735 switch (*bv.bv_val) {
1737 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1740 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1741 * the right 's' to mean "set", but in the examples states
1742 * that the right 's' means "search". The latter definition
1745 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1748 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1751 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1754 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1755 * define any equivalent to the AUTH right, so I've just used
1758 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1770 struct berval *list,
1771 const struct berval *attr,
1772 struct berval *val )
1774 struct berval bv, left, right;
1777 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1778 if (aci_get_part(&bv, 0, '=', &left) < 0
1779 || aci_get_part(&bv, 1, '=', &right) < 0)
1781 if (ber_bvstrcasecmp(attr, &bv) == 0)
1783 } else if (val == NULL) {
1784 if (ber_bvstrcasecmp(attr, &left) == 0)
1787 if (ber_bvstrcasecmp(attr, &left) == 0) {
1788 /* this is experimental code that implements a
1789 * simple (prefix) match of the attribute value.
1790 * the ACI draft does not provide for aci's that
1791 * apply to specific values, but it would be
1792 * nice to have. If the <attr> part of an aci's
1793 * rights list is of the form <attr>=<value>,
1794 * that means the aci applies only to attrs with
1795 * the given value. Furthermore, if the attr is
1796 * of the form <attr>=<value>*, then <value> is
1797 * treated as a prefix, and the aci applies to
1798 * any value with that prefix.
1800 * Ideally, this would allow r.e. matches.
1802 if (aci_get_part(&right, 0, '*', &left) < 0
1803 || right.bv_len <= left.bv_len)
1805 if (ber_bvstrcasecmp(val, &right) == 0)
1807 } else if (val->bv_len >= left.bv_len) {
1808 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1817 static slap_access_t
1818 aci_list_get_attr_rights(
1819 struct berval *list,
1820 const struct berval *attr,
1821 struct berval *val )
1827 /* loop through each rights/attr pair, skip first part (action) */
1829 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1830 if (aci_list_has_attr(&bv, attr, val) == 0)
1832 if (aci_get_part(list, i, ';', &bv) < 0)
1834 mask |= aci_list_map_rights(&bv);
1840 aci_list_get_rights(
1841 struct berval *list,
1842 const struct berval *attr,
1844 slap_access_t *grant,
1845 slap_access_t *deny )
1847 struct berval perm, actn;
1848 slap_access_t *mask;
1851 if (attr == NULL || attr->bv_len == 0
1852 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1853 attr = &aci_bv_br_entry;
1859 /* loop through each permissions clause */
1860 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1861 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1863 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1865 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1872 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1873 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1880 struct berval *subj,
1881 struct berval *defgrpoc,
1882 struct berval *defgrpat,
1888 struct berval subjdn;
1889 struct berval grpoc;
1890 struct berval grpat;
1891 ObjectClass *grp_oc = NULL;
1892 AttributeDescription *grp_ad = NULL;
1896 /* format of string is "group/objectClassValue/groupAttrName" */
1897 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1901 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1905 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1909 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1910 if( rc != LDAP_SUCCESS ) {
1916 grp_oc = oc_bvfind( &grpoc );
1918 if (grp_oc != NULL && grp_ad != NULL ) {
1919 char buf[ACL_BUF_SIZE];
1920 struct berval bv, ndn;
1921 bv.bv_len = sizeof( buf ) - 1;
1922 bv.bv_val = (char *)&buf;
1923 string_expand(&bv, &subjdn, e->e_ndn, matches);
1924 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1925 rc = (backend_group(op, e, &ndn, &op->o_ndn,
1926 grp_oc, grp_ad) == 0);
1939 AttributeDescription *desc,
1942 regmatch_t *matches,
1943 slap_access_t *grant,
1944 slap_access_t *deny,
1945 struct berval *scope
1948 struct berval bv, perms, sdn;
1952 assert( desc->ad_cname.bv_val != NULL );
1954 /* parse an aci of the form:
1955 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1957 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1958 a full description of the format for this attribute.
1959 Differences: "this" in the draft is "self" here, and
1960 "self" and "public" is in the position of dnType.
1962 For now, this routine only supports scope=entry.
1964 /* check that the aci has all 5 components */
1965 if (aci_get_part(aci, 4, '#', NULL) < 0)
1968 /* check that the aci family is supported */
1969 if (aci_get_part(aci, 0, '#', &bv) < 0)
1972 /* check that the scope matches */
1973 if (aci_get_part(aci, 1, '#', &bv) < 0
1974 || ber_bvstrcasecmp( scope, &bv ) != 0)
1979 /* get the list of permissions clauses, bail if empty */
1980 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1983 /* check if any permissions allow desired access */
1984 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1987 /* see if we have a DN match */
1988 if (aci_get_part(aci, 3, '#', &bv) < 0)
1991 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1994 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1997 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1998 if (dn_match( &op->o_ndn, &ndn))
2004 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2007 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2008 if (dn_match(&op->o_ndn, &e->e_nname))
2011 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2013 AttributeDescription *ad = NULL;
2016 rc = slap_bv2ad( &sdn, &ad, &text );
2018 if( rc != LDAP_SUCCESS ) {
2026 for(at = attrs_find( e->e_attrs, ad );
2028 at = attrs_find( at->a_next, ad ) )
2030 if (value_find_ex( ad,
2031 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2032 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2034 &bv, op->o_tmpmemctx) == 0 )
2044 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2045 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2048 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2049 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2052 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2053 if (aci_match_set(&sdn, op, e, 0))
2056 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2057 if (aci_match_set(&sdn, op, e, 1))
2065 #endif /* SLAPD_ACI_ENABLED */
2072 regmatch_t *matches)
2080 bv->bv_val[0] = '\0';
2081 bv->bv_len--; /* leave space for lone $ */
2084 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2085 sp < pat->bv_val + pat->bv_len ; sp++ )
2087 /* did we previously see a $ */
2089 if ( flag == 1 && *sp == '$' ) {
2094 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2097 } else if ( *sp >= '0' && *sp <= '9' ) {
2105 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2106 if ( *sp >= '0' && *sp <= '9' ) {
2107 n = 10*n + ( *sp - '0' );
2111 if ( *sp != /*'{'*/ '}' ) {
2116 if ( n >= MAXREMATCHES ) {
2121 i = matches[n].rm_so;
2122 l = matches[n].rm_eo;
2123 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2141 /* must have ended with a single $ */
2150 LDAP_LOG( ACL, DETAIL1,
2151 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2152 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2154 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2155 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2161 struct berval *pat, /* pattern to expand and match against */
2162 char *str, /* string to match against pattern */
2163 char *buf, /* buffer with $N expansion variables */
2164 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2168 char newbuf[ACL_BUF_SIZE];
2172 bv.bv_len = sizeof(newbuf) - 1;
2175 if(str == NULL) str = "";
2177 string_expand(&bv, pat, buf, matches);
2178 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2179 char error[ACL_BUF_SIZE];
2180 regerror(rc, &re, error, sizeof(error));
2184 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2185 pat->bv_val, str, error );
2187 Debug( LDAP_DEBUG_TRACE,
2188 "compile( \"%s\", \"%s\") failed %s\n",
2189 pat->bv_val, str, error );
2194 rc = regexec(&re, str, 0, NULL, 0);
2198 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2199 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2200 rc, rc ? "matches" : "no matches", 0 );
2202 Debug( LDAP_DEBUG_TRACE,
2203 "=> regex_matches: string: %s\n", str, 0, 0 );
2204 Debug( LDAP_DEBUG_TRACE,
2205 "=> regex_matches: rc: %d %s\n",
2206 rc, !rc ? "matches" : "no matches", 0 );