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);
78 static AccessControl * acl_get(
79 AccessControl *ac, int *count,
80 Operation *op, Entry *e,
81 AttributeDescription *desc,
83 int nmatches, regmatch_t *matches );
85 static slap_control_t acl_mask(
86 AccessControl *ac, slap_mask_t *mask,
87 Operation *op, Entry *e,
88 AttributeDescription *desc,
92 AccessControlState *state );
94 #ifdef SLAPD_ACI_ENABLED
96 Operation *op, Entry *e,
97 AttributeDescription *desc,
101 slap_access_t *grant,
103 struct berval *scope);
106 static int regex_matches(
107 struct berval *pat, char *str, char *buf, regmatch_t *matches);
108 static void string_expand(
109 struct berval *newbuf, struct berval *pattern,
110 char *match, regmatch_t *matches);
112 typedef struct AciSetCookie {
117 SLAP_SET_GATHER aci_set_gather;
118 static int aci_match_set ( struct berval *subj, Operation *op,
119 Entry *e, int setref );
122 * access_allowed - check whether op->o_ndn is allowed the requested access
123 * to entry e, attribute attr, value val. if val is null, access to
124 * the whole attribute is assumed (all values).
126 * This routine loops through all access controls and calls
127 * acl_mask() on each applicable access control.
128 * The loop exits when a definitive answer is reached or
129 * or no more controls remain.
140 AttributeDescription *desc,
142 slap_access_t access,
143 AccessControlState *state )
147 AccessControl *a = NULL;
152 char accessmaskbuf[ACCESSMASK_MAXLEN];
155 slap_control_t control;
157 regmatch_t matches[MAXREMATCHES];
158 int st_same_attr = 0;
159 int st_initialized = 0;
160 static AccessControlState state_init = ACL_STATE_INIT;
163 assert( desc != NULL );
164 assert( access > ACL_NONE );
166 attr = desc->ad_cname.bv_val;
168 assert( attr != NULL );
170 if( op && op->o_is_auth_check &&
171 ( access == ACL_SEARCH || access == ACL_READ ))
176 if( state && state->as_recorded && state->as_vd_ad==desc) {
177 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
180 return state->as_result;
182 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
183 val != NULL && state->as_vd_acl == NULL )
185 return state->as_result;
191 state->as_vd_ad=desc;
195 LDAP_LOG( ACL, ENTRY,
196 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
197 access2str( access ), e->e_dn, attr );
199 Debug( LDAP_DEBUG_ACL,
200 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
201 access2str( access ), e->e_dn, attr );
215 assert( be != NULL );
218 if ( op->o_pb != NULL ) {
219 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
221 /* ACL plugin denied access */
225 #endif /* LDAP_SLAPI */
227 /* grant database root access */
228 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
231 "access_allowed: conn %lu root access granted\n",
232 op->o_connid, 0, 0 );
234 Debug( LDAP_DEBUG_ACL,
235 "<= root access granted\n",
242 * no-user-modification operational attributes are ignored
243 * by ACL_WRITE checking as any found here are not provided
246 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
247 && desc != slap_schema.si_ad_entry
248 && desc != slap_schema.si_ad_children )
251 LDAP_LOG( ACL, DETAIL1,
252 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
253 "access granted\n", op->o_connid, attr , 0 );
255 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
256 " %s access granted\n",
262 /* use backend default access if no backend acls */
263 if( be != NULL && be->be_acl == NULL ) {
265 LDAP_LOG( ACL, DETAIL1,
266 "access_allowed: backend default %s access %s to \"%s\"\n",
267 access2str( access ),
268 be->be_dfltaccess >= access ? "granted" : "denied",
269 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
271 Debug( LDAP_DEBUG_ACL,
272 "=> access_allowed: backend default %s access %s to \"%s\"\n",
273 access2str( access ),
274 be->be_dfltaccess >= access ? "granted" : "denied",
275 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
277 ret = be->be_dfltaccess >= access;
281 /* be is always non-NULL */
282 /* use global default access if no global acls */
283 } else if ( be == NULL && global_acl == NULL ) {
285 LDAP_LOG( ACL, DETAIL1,
286 "access_allowed: global default %s access %s to \"%s\"\n",
287 access2str( access ),
288 global_default_access >= access ? "granted" : "denied",
291 Debug( LDAP_DEBUG_ACL,
292 "=> access_allowed: global default %s access %s to \"%s\"\n",
293 access2str( access ),
294 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
296 ret = global_default_access >= access;
305 assert( state->as_vd_acl != NULL );
307 a = state->as_vd_acl;
308 mask = state->as_vd_acl_mask;
309 count = state->as_vd_acl_count;
310 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
314 if ( state ) state->as_vi_acl = NULL;
318 memset(matches, '\0', sizeof(matches));
321 while((a = acl_get( a, &count, op, e, desc, val,
322 MAXREMATCHES, matches )) != NULL)
326 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
328 LDAP_LOG( ACL, DETAIL1,
329 "access_allowed: match[%d]: %d %d ",
330 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
332 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
333 (int)matches[i].rm_so, (int)matches[i].rm_eo );
335 if( matches[i].rm_so <= matches[0].rm_eo ) {
337 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
338 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
342 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
344 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
349 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
350 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
351 ret = state->as_result;
353 } else if (!st_initialized) {
354 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
356 state->as_vd_ad=desc;
362 control = acl_mask( a, &mask, op,
363 e, desc, val, matches, count, state );
365 if ( control != ACL_BREAK ) {
369 memset(matches, '\0', sizeof(matches));
372 if ( ACL_IS_INVALID( mask ) ) {
374 LDAP_LOG( ACL, DETAIL1,
375 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
376 op->o_connid, e->e_dn, attr );
378 Debug( LDAP_DEBUG_ACL,
379 "=> access_allowed: \"%s\" (%s) invalid!\n",
384 } else if ( control == ACL_BREAK ) {
386 LDAP_LOG( ACL, DETAIL1,
387 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
389 Debug( LDAP_DEBUG_ACL,
390 "=> access_allowed: no more rules\n", 0, 0, 0);
397 LDAP_LOG( ACL, ENTRY,
398 "access_allowed: %s access %s by %s\n",
399 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
400 accessmask2str( mask, accessmaskbuf ) );
402 Debug( LDAP_DEBUG_ACL,
403 "=> access_allowed: %s access %s by %s\n",
404 access2str( access ),
405 ACL_GRANT(mask, access) ? "granted" : "denied",
406 accessmask2str( mask, accessmaskbuf ) );
409 ret = ACL_GRANT(mask, access);
412 if( state != NULL ) {
413 /* If not value-dependent, save ACL in case of more attrs */
414 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
415 state->as_vi_acl = a;
416 state->as_result = ret;
418 state->as_recorded |= ACL_STATE_RECORDED;
420 if (be_null) op->o_bd = NULL;
425 * acl_get - return the acl applicable to entry e, attribute
426 * attr. the acl returned is suitable for use in subsequent calls to
427 * acl_access_allowed().
430 static AccessControl *
436 AttributeDescription *desc,
439 regmatch_t *matches )
445 assert( count != NULL );
446 assert( desc != NULL );
448 attr = desc->ad_cname.bv_val;
450 assert( attr != NULL );
453 if( op->o_bd == NULL ) {
456 a = op->o_bd->be_acl;
465 dnlen = e->e_nname.bv_len;
467 for ( ; a != NULL; a = a->acl_next ) {
470 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
471 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
473 LDAP_LOG( ACL, DETAIL1,
474 "acl_get: dnpat [%d] %s nsub: %d\n",
475 *count, a->acl_dn_pat.bv_val,
476 (int) a->acl_dn_re.re_nsub );
478 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
479 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
481 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
486 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
487 *count, a->acl_dn_pat.bv_val, 0 );
489 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
490 *count, a->acl_dn_pat.bv_val, 0 );
492 patlen = a->acl_dn_pat.bv_len;
493 if ( dnlen < patlen )
496 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
497 /* base dn -- entire object DN must match */
498 if ( dnlen != patlen )
501 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
504 if ( dnlen <= patlen )
507 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
510 rdnlen = dn_rdnlen( NULL, &e->e_nname );
511 if ( rdnlen != dnlen - patlen - 1 )
514 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
515 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
518 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
519 if ( dnlen <= patlen )
521 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
525 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
530 LDAP_LOG( ACL, DETAIL1,
531 "acl_get: [%d] matched\n", *count, 0, 0 );
533 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
538 if ( a->acl_filter != NULL ) {
539 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
540 if ( rc != LDAP_COMPARE_TRUE ) {
546 LDAP_LOG( ACL, DETAIL1,
547 "acl_get: [%d] check attr %s\n", *count, attr ,0 );
549 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
552 if ( a->acl_attrs == NULL ||
553 ad_inlist( desc, a->acl_attrs ) )
556 LDAP_LOG( ACL, DETAIL1,
557 "acl_get: [%d] acl %s attr: %s\n", *count, e->e_dn, attr );
559 Debug( LDAP_DEBUG_ACL,
560 "<= acl_get: [%d] acl %s attr: %s\n",
561 *count, e->e_dn, attr );
565 matches[0].rm_so = matches[0].rm_eo = -1;
569 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
571 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
577 * Record value-dependent access control state
579 #define ACL_RECORD_VALUE_STATE do { \
580 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
581 state->as_recorded |= ACL_STATE_RECORDED_VD; \
582 state->as_vd_acl = a; \
583 AC_MEMCPY( state->as_vd_acl_matches, matches, \
584 sizeof( state->as_vd_acl_matches )) ; \
585 state->as_vd_acl_count = count; \
586 state->as_vd_access = b; \
587 state->as_vd_access_count = i; \
592 * acl_mask - modifies mask based upon the given acl and the
593 * requested access to entry e, attribute attr, value val. if val
594 * is null, access to the whole attribute is assumed (all values).
596 * returns 0 access NOT allowed
600 static slap_control_t
606 AttributeDescription *desc,
610 AccessControlState *state )
612 int i, odnlen, patlen;
615 char accessmaskbuf[ACCESSMASK_MAXLEN];
616 char accessmaskbuf1[ACCESSMASK_MAXLEN];
621 assert( mask != NULL );
622 assert( desc != NULL );
624 attr = desc->ad_cname.bv_val;
626 assert( attr != NULL );
629 LDAP_LOG( ACL, ENTRY,
630 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
631 op->o_connid, e->e_dn, attr );
634 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
635 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
636 accessmask2str( *mask, accessmaskbuf ) );
638 Debug( LDAP_DEBUG_ACL,
639 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
642 Debug( LDAP_DEBUG_ACL,
643 "=> acl_mask: to %s by \"%s\", (%s) \n",
644 val ? "value" : "all values",
645 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
646 accessmask2str( *mask, accessmaskbuf ) );
649 /* Is this ACL only for a specific value? */
650 if ( a->acl_attrval.bv_len ) {
651 if ( state && !state->as_vd_acl ) {
652 state->as_vd_acl = a;
653 state->as_vd_access = a->acl_access;
654 state->as_vd_access_count = 1;
659 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
661 LDAP_LOG( ACL, DETAIL1,
662 "acl_get: valpat %s\n",
663 a->acl_attrval.bv_val, 0, 0 );
665 Debug( LDAP_DEBUG_ACL,
666 "acl_get: valpat %s\n",
667 a->acl_attrval.bv_val, 0, 0 );
669 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
675 LDAP_LOG( ACL, DETAIL1,
677 a->acl_attrval.bv_val, 0, 0 );
679 Debug( LDAP_DEBUG_ACL,
681 a->acl_attrval.bv_val, 0, 0 );
684 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
685 if (value_match( &match, desc,
686 desc->ad_type->sat_equality, 0,
687 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
692 int patlen, vdnlen, rc, got_match = 0;
693 struct berval vdn = { 0, NULL };
696 assert( a->acl_attrs[0].an_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName );
698 rc = dnNormalize( 0, NULL, NULL, val, &vdn,
700 if ( rc != LDAP_SUCCESS ) {
705 patlen = a->acl_attrval.bv_len;
708 if ( vdnlen < patlen )
709 goto attrval_cleanup;
711 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
712 if ( vdnlen > patlen )
713 goto attrval_cleanup;
715 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
718 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
719 goto attrval_cleanup;
721 rdnlen = dn_rdnlen( NULL, &vdn );
722 if ( rdnlen != vdnlen - patlen - 1 )
723 goto attrval_cleanup;
725 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
726 if ( vdnlen > patlen && !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
727 goto attrval_cleanup;
729 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
730 if ( vdnlen <= patlen )
731 goto attrval_cleanup;
733 if ( !DN_SEPARATOR( vdn.bv_val[vdnlen - patlen - 1] ) )
734 goto attrval_cleanup;
737 got_match = strcmp( a->acl_attrval.bv_val, vdn.bv_val + vdnlen - patlen );
750 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
751 && state->as_vd_acl == a )
753 b = state->as_vd_access;
754 i = state->as_vd_access_count;
761 for ( ; b != NULL; b = b->a_next, i++ ) {
762 slap_mask_t oldmask, modmask;
764 ACL_INVALIDATE( modmask );
766 /* AND <who> clauses */
767 if ( b->a_dn_pat.bv_len != 0 ) {
769 LDAP_LOG( ACL, DETAIL1,
770 "acl_mask: conn %lu check a_dn_pat: %s\n",
771 op->o_connid, b->a_dn_pat.bv_val ,0 );
773 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
774 b->a_dn_pat.bv_val, 0, 0);
777 * if access applies to the entry itself, and the
778 * user is bound as somebody in the same namespace as
779 * the entry, OR the given dn matches the dn pattern
781 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
782 if ( op->o_ndn.bv_len != 0 ) {
786 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
787 if ( op->o_ndn.bv_len == 0 ) {
791 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
792 if ( op->o_ndn.bv_len == 0 ) {
796 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
800 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
801 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
802 int ret = regex_matches( &b->a_dn_pat,
803 op->o_ndn.bv_val, e->e_ndn, matches );
814 if ( e->e_dn == NULL )
817 if ( b->a_dn_expand ) {
819 char buf[ACL_BUF_SIZE];
821 bv.bv_len = sizeof( buf ) - 1;
824 string_expand(&bv, &b->a_dn_pat,
826 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
827 /* did not expand to a valid dn */
835 odnlen = op->o_ndn.bv_len;
836 if ( odnlen < patlen ) {
837 goto dn_match_cleanup;
841 if ( b->a_dn_style == ACL_STYLE_BASE ) {
842 /* base dn -- entire object DN must match */
843 if ( odnlen != patlen ) {
844 goto dn_match_cleanup;
847 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
850 if ( odnlen <= patlen ) {
851 goto dn_match_cleanup;
854 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
855 goto dn_match_cleanup;
858 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
859 if ( rdnlen != odnlen - patlen - 1 ) {
860 goto dn_match_cleanup;
863 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
864 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
865 goto dn_match_cleanup;
868 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
869 if ( odnlen <= patlen ) {
870 goto dn_match_cleanup;
873 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
874 goto dn_match_cleanup;
878 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
881 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
891 if ( b->a_sockurl_pat.bv_len ) {
892 if ( ! op->o_conn->c_listener ) {
896 LDAP_LOG( ACL, DETAIL1,
897 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
898 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
900 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
901 b->a_sockurl_pat.bv_val, 0, 0 );
904 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
905 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
906 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
907 e->e_ndn, matches ) )
912 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
918 if ( b->a_domain_pat.bv_len ) {
919 if ( !op->o_conn->c_peer_domain.bv_val ) {
923 LDAP_LOG( ACL, DETAIL1,
924 "acl_mask: conn %lu check a_domain_pat: %s\n",
925 op->o_connid, b->a_domain_pat.bv_val , 0 );
927 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
928 b->a_domain_pat.bv_val, 0, 0 );
930 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
931 if ( b->a_domain_style == ACL_STYLE_REGEX) {
932 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
933 e->e_ndn, matches ) )
938 char buf[ACL_BUF_SIZE];
940 struct berval cmp = op->o_conn->c_peer_domain;
941 struct berval pat = b->a_domain_pat;
943 if ( b->a_domain_expand ) {
946 bv.bv_len = sizeof(buf) - 1;
949 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
953 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
954 int offset = cmp.bv_len - pat.bv_len;
959 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
963 /* trim the domain */
964 cmp.bv_val = &cmp.bv_val[ offset ];
965 cmp.bv_len -= offset;
968 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
975 if ( b->a_peername_pat.bv_len ) {
976 if ( !op->o_conn->c_peer_name.bv_val ) {
980 LDAP_LOG( ACL, DETAIL1,
981 "acl_mask: conn %lu check a_peername_path: %s\n",
982 op->o_connid, b->a_peername_pat.bv_val , 0 );
984 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
985 b->a_peername_pat.bv_val, 0, 0 );
987 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
988 if ( b->a_peername_style == ACL_STYLE_REGEX) {
989 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
990 e->e_ndn, matches ) )
996 /* try exact match */
997 if ( b->a_peername_style == ACL_STYLE_BASE ) {
998 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 )
1001 /* extract IP and try exact match */
1002 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1004 char buf[] = "255.255.255.255";
1007 int port_number = -1;
1009 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1010 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1013 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1014 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1016 port = strrchr( ip.bv_val, ':' );
1020 ip.bv_len = port - ip.bv_val;
1022 port_number = strtol( port, &next, 10 );
1023 if ( next[0] != '\0' )
1027 /* the port check can be anticipated here */
1028 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1031 /* address longer than expected? */
1032 if ( ip.bv_len >= sizeof(buf) )
1035 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1036 buf[ ip.bv_len ] = '\0';
1038 addr = inet_addr( buf );
1040 /* unable to convert? */
1041 if ( addr == (unsigned long)(-1) )
1044 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1047 #ifdef LDAP_PF_LOCAL
1048 /* extract path and try exact match */
1049 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1052 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1053 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1056 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1057 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1059 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1062 #endif /* LDAP_PF_LOCAL */
1064 /* exact match (very unlikely...) */
1065 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1072 if ( b->a_sockname_pat.bv_len ) {
1073 if ( !op->o_conn->c_sock_name.bv_val ) {
1077 LDAP_LOG( ACL, DETAIL1,
1078 "acl_mask: conn %lu check a_sockname_path: %s\n",
1079 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1081 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1082 b->a_sockname_pat.bv_val, 0, 0 );
1084 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1085 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1086 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1087 e->e_ndn, matches ) )
1092 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1098 if ( b->a_dn_at != NULL ) {
1103 const char *attr = b->a_dn_at->ad_cname.bv_val;
1105 assert( attr != NULL );
1107 if ( op->o_ndn.bv_len == 0 ) {
1112 LDAP_LOG( ACL, DETAIL1,
1113 "acl_mask: conn %lu check a_dn_pat: %s\n",
1114 op->o_connid, attr , 0 );
1116 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1121 /* see if asker is listed in dnattr */
1122 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1124 at = attrs_find( at->a_next, b->a_dn_at ) )
1126 if( value_find_ex( b->a_dn_at,
1127 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1128 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1130 &bv, op->o_tmpmemctx ) == 0 )
1139 /* have a dnattr match. if this is a self clause then
1140 * the target must also match the op dn.
1142 if ( b->a_dn_self ) {
1143 /* check if the target is an attribute. */
1144 if ( val == NULL ) continue;
1146 /* target is attribute, check if the attribute value
1149 rc = value_match( &match, b->a_dn_at,
1150 b->a_dn_at->ad_type->sat_equality, 0,
1152 /* on match error or no match, fail the ACL clause */
1153 if (rc != LDAP_SUCCESS || match != 0 )
1157 /* no dnattr match, check if this is a self clause */
1158 if ( ! b->a_dn_self )
1161 ACL_RECORD_VALUE_STATE;
1163 /* this is a self clause, check if the target is an
1169 /* target is attribute, check if the attribute value
1172 rc = value_match( &match, b->a_dn_at,
1173 b->a_dn_at->ad_type->sat_equality, 0,
1176 /* on match error or no match, fail the ACL clause */
1177 if (rc != LDAP_SUCCESS || match != 0 )
1182 if ( b->a_group_pat.bv_len ) {
1184 struct berval ndn = { 0, NULL };
1187 if ( op->o_ndn.bv_len == 0 ) {
1191 /* b->a_group is an unexpanded entry name, expanded it should be an
1192 * entry with objectclass group* and we test to see if odn is one of
1193 * the values in the attribute group
1195 /* see if asker is listed in dnattr */
1196 if ( b->a_group_style == ACL_STYLE_REGEX ) {
1197 char buf[ACL_BUF_SIZE];
1198 bv.bv_len = sizeof(buf) - 1;
1201 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1202 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1203 /* did not expand to a valid dn */
1210 bv = b->a_group_pat;
1213 rc = backend_group( op, e, &bv, &op->o_ndn,
1214 b->a_group_oc, b->a_group_at );
1216 if ( ndn.bv_val ) free( ndn.bv_val );
1223 if ( b->a_set_pat.bv_len != 0 ) {
1225 char buf[ACL_BUF_SIZE];
1226 if( b->a_set_style == ACL_STYLE_REGEX ){
1227 bv.bv_len = sizeof(buf) - 1;
1229 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1233 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1238 if ( b->a_authz.sai_ssf ) {
1240 LDAP_LOG( ACL, DETAIL1,
1241 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1242 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1244 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1245 b->a_authz.sai_ssf, op->o_ssf, 0 );
1247 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1252 if ( b->a_authz.sai_transport_ssf ) {
1254 LDAP_LOG( ACL, DETAIL1,
1255 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1257 op->o_connid, b->a_authz.sai_transport_ssf,
1258 op->o_transport_ssf );
1260 Debug( LDAP_DEBUG_ACL,
1261 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1262 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1264 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1269 if ( b->a_authz.sai_tls_ssf ) {
1271 LDAP_LOG( ACL, DETAIL1,
1272 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1274 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1276 Debug( LDAP_DEBUG_ACL,
1277 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1278 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1280 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1285 if ( b->a_authz.sai_sasl_ssf ) {
1287 LDAP_LOG( ACL, DETAIL1,
1288 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1290 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1292 Debug( LDAP_DEBUG_ACL,
1293 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1294 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1296 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1301 #ifdef SLAPD_ACI_ENABLED
1302 if ( b->a_aci_at != NULL ) {
1304 slap_access_t grant, deny, tgrant, tdeny;
1305 struct berval parent_ndn, old_parent_ndn;
1306 BerVarray bvals = NULL;
1309 /* this case works different from the others above.
1310 * since aci's themselves give permissions, we need
1311 * to first check b->a_access_mask, the ACL's access level.
1314 if ( e->e_nname.bv_len == 0 ) {
1315 /* no ACIs in the root DSE */
1319 /* first check if the right being requested
1320 * is allowed by the ACL clause.
1322 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1325 /* start out with nothing granted, nothing denied */
1329 /* get the aci attribute */
1330 at = attr_find( e->e_attrs, b->a_aci_at );
1332 ACL_RECORD_VALUE_STATE;
1333 /* the aci is an multi-valued attribute. The
1334 * rights are determined by OR'ing the individual
1335 * rights given by the acis.
1337 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1341 matches, &grant, &deny, &aci_bv_entry ) != 0)
1347 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1348 accessmask2str(tgrant,accessmaskbuf),
1349 accessmask2str(tdeny, accessmaskbuf1), 0);
1352 /* If the entry level aci didn't contain anything valid for the
1353 * current operation, climb up the tree and evaluate the
1354 * acis with scope set to subtree
1356 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1357 dnParent(&(e->e_nname), &parent_ndn);
1358 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1359 old_parent_ndn = parent_ndn;
1360 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1361 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1365 for( i = 0; bvals[i].bv_val != NULL; i++){
1366 ACL_RECORD_VALUE_STATE;
1367 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1368 &grant, &deny, &aci_bv_children) != 0) {
1371 /* evaluation stops as soon as either a "deny" or a
1372 * "grant" directive matches.
1374 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1378 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1379 accessmask2str(tgrant,accessmaskbuf),
1380 accessmask2str(tdeny, accessmaskbuf1), 0);
1385 case LDAP_NO_SUCH_ATTRIBUTE:
1386 /* just go on if the aci-Attribute is not present in
1389 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1392 case LDAP_NO_SUCH_OBJECT:
1393 /* We have reached the base object */
1394 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1404 dnParent(&old_parent_ndn, &parent_ndn);
1409 /* remove anything that the ACL clause does not allow */
1410 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1411 tdeny &= ACL_PRIV_MASK;
1413 /* see if we have anything to contribute */
1414 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1418 /* this could be improved by changing acl_mask so that it can deal with
1419 * by clauses that return grant/deny pairs. Right now, it does either
1420 * additive or subtractive rights, but not both at the same time. So,
1421 * we need to combine the grant/deny pair into a single rights mask in
1422 * a smart way: if either grant or deny is "empty", then we use the
1423 * opposite as is, otherwise we remove any denied rights from the grant
1424 * rights mask and construct an additive mask.
1426 if (ACL_IS_INVALID(tdeny)) {
1427 modmask = tgrant | ACL_PRIV_ADDITIVE;
1429 } else if (ACL_IS_INVALID(tgrant)) {
1430 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1433 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1439 modmask = b->a_access_mask;
1443 LDAP_LOG( ACL, RESULTS,
1444 "acl_mask: [%d] applying %s (%s)\n",
1445 i, accessmask2str( modmask, accessmaskbuf),
1446 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1447 ? "break" : "stop" );
1449 Debug( LDAP_DEBUG_ACL,
1450 "<= acl_mask: [%d] applying %s (%s)\n",
1451 i, accessmask2str( modmask, accessmaskbuf ),
1452 b->a_type == ACL_CONTINUE
1454 : b->a_type == ACL_BREAK
1461 if( ACL_IS_ADDITIVE(modmask) ) {
1463 ACL_PRIV_SET( *mask, modmask );
1466 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1468 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1469 /* substract privs */
1470 ACL_PRIV_CLR( *mask, modmask );
1473 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1481 LDAP_LOG( ACL, DETAIL1,
1482 "acl_mask: conn %lu [%d] mask: %s\n",
1483 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1485 Debug( LDAP_DEBUG_ACL,
1486 "<= acl_mask: [%d] mask: %s\n",
1487 i, accessmask2str(*mask, accessmaskbuf), 0 );
1490 if( b->a_type == ACL_CONTINUE ) {
1493 } else if ( b->a_type == ACL_BREAK ) {
1501 /* implicit "by * none" clause */
1505 LDAP_LOG( ACL, RESULTS,
1506 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1507 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1509 Debug( LDAP_DEBUG_ACL,
1510 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1511 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1517 * acl_check_modlist - check access control on the given entry to see if
1518 * it allows the given modifications by the user associated with op.
1519 * returns 1 if mods allowed ok
1520 * 0 mods not allowed
1527 Modifications *mlist
1531 AccessControlState state = ACL_STATE_INIT;
1534 int ret = 1; /* default is access allowed */
1542 assert( be != NULL );
1544 /* short circuit root database access */
1545 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1547 LDAP_LOG( ACL, DETAIL1,
1548 "acl_check_modlist: conn %lu access granted to root user\n",
1549 op->o_connid, 0, 0 );
1551 Debug( LDAP_DEBUG_ACL,
1552 "<= acl_access_allowed: granted to database root\n",
1558 /* use backend default access if no backend acls */
1559 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1561 LDAP_LOG( ACL, DETAIL1,
1562 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1563 access2str( ACL_WRITE ),
1564 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1567 Debug( LDAP_DEBUG_ACL,
1568 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1569 access2str( ACL_WRITE ),
1570 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1572 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1576 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1578 * no-user-modification operational attributes are ignored
1579 * by ACL_WRITE checking as any found here are not provided
1582 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1584 LDAP_LOG( ACL, DETAIL1,
1585 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1586 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1588 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1589 " modify access granted\n",
1590 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1595 switch ( mlist->sml_op ) {
1596 case LDAP_MOD_REPLACE:
1598 * We must check both permission to delete the whole
1599 * attribute and permission to add the specific attributes.
1600 * This prevents abuse from selfwriters.
1602 if ( ! access_allowed( op, e,
1603 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1609 if ( mlist->sml_bvalues == NULL ) break;
1611 /* fall thru to check value to add */
1614 assert( mlist->sml_bvalues != NULL );
1616 for ( bv = mlist->sml_nvalues
1617 ? mlist->sml_nvalues : mlist->sml_values;
1618 bv->bv_val != NULL; bv++ )
1620 if ( ! access_allowed( op, e,
1621 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1629 case LDAP_MOD_DELETE:
1630 if ( mlist->sml_bvalues == NULL ) {
1631 if ( ! access_allowed( op, e,
1632 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1639 for ( bv = mlist->sml_nvalues
1640 ? mlist->sml_nvalues : mlist->sml_values;
1641 bv->bv_val != NULL; bv++ )
1643 if ( ! access_allowed( op, e,
1644 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1652 case SLAP_MOD_SOFTADD:
1653 /* allow adding attribute via modrdn thru */
1665 if (be_null) op->o_bd = NULL;
1671 struct berval *list,
1685 while (len >= 0 && --ix >= 0) {
1686 while (--len >= 0 && *p++ != sep) ;
1688 while (len >= 0 && *p == ' ') {
1699 while (--len >= 0 && *p != sep) {
1703 while (bv->bv_len > 0 && *--p == ' ')
1709 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1711 AciSetCookie *cp = (AciSetCookie *)cookie;
1712 BerVarray bvals = NULL;
1715 /* this routine needs to return the bervals instead of
1716 * plain strings, since syntax is not known. It should
1717 * also return the syntax or some "comparison cookie".
1720 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1722 AttributeDescription *desc = NULL;
1723 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1724 backend_attribute(cp->op,
1725 cp->e, &ndn, desc, &bvals);
1727 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1734 struct berval *subj,
1740 struct berval set = { 0, NULL };
1742 AciSetCookie cookie;
1745 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1747 struct berval subjdn, ndn = { 0, NULL };
1748 struct berval setat;
1751 AttributeDescription *desc = NULL;
1753 /* format of string is "entry/setAttrName" */
1754 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1758 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1759 setat.bv_val = SLAPD_ACI_SET_ATTR;
1760 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1763 if ( setat.bv_val != NULL ) {
1765 * NOTE: dnNormalize honors the ber_len field
1766 * as the length of the dn to be normalized
1768 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1769 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1771 backend_attribute(op, e,
1772 &ndn, desc, &bvals);
1773 if ( bvals != NULL ) {
1774 if ( bvals[0].bv_val != NULL ) {
1777 bvals[0].bv_val = NULL;
1778 for (i=1;bvals[i].bv_val;i++);
1779 bvals[0].bv_val = bvals[i-1].bv_val;
1780 bvals[i-1].bv_val = NULL;
1782 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1790 if (set.bv_val != NULL) {
1793 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1794 &op->o_ndn, &e->e_nname, NULL) > 0);
1795 sl_free(set.bv_val, op->o_tmpmemctx);
1800 #ifdef SLAPD_ACI_ENABLED
1802 aci_list_map_rights(
1803 struct berval *list )
1810 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1813 switch (*bv.bv_val) {
1815 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1818 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1819 * the right 's' to mean "set", but in the examples states
1820 * that the right 's' means "search". The latter definition
1823 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1826 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1829 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1832 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1833 * define any equivalent to the AUTH right, so I've just used
1836 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1848 struct berval *list,
1849 const struct berval *attr,
1850 struct berval *val )
1852 struct berval bv, left, right;
1855 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1856 if (aci_get_part(&bv, 0, '=', &left) < 0
1857 || aci_get_part(&bv, 1, '=', &right) < 0)
1859 if (ber_bvstrcasecmp(attr, &bv) == 0)
1861 } else if (val == NULL) {
1862 if (ber_bvstrcasecmp(attr, &left) == 0)
1865 if (ber_bvstrcasecmp(attr, &left) == 0) {
1866 /* this is experimental code that implements a
1867 * simple (prefix) match of the attribute value.
1868 * the ACI draft does not provide for aci's that
1869 * apply to specific values, but it would be
1870 * nice to have. If the <attr> part of an aci's
1871 * rights list is of the form <attr>=<value>,
1872 * that means the aci applies only to attrs with
1873 * the given value. Furthermore, if the attr is
1874 * of the form <attr>=<value>*, then <value> is
1875 * treated as a prefix, and the aci applies to
1876 * any value with that prefix.
1878 * Ideally, this would allow r.e. matches.
1880 if (aci_get_part(&right, 0, '*', &left) < 0
1881 || right.bv_len <= left.bv_len)
1883 if (ber_bvstrcasecmp(val, &right) == 0)
1885 } else if (val->bv_len >= left.bv_len) {
1886 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1895 static slap_access_t
1896 aci_list_get_attr_rights(
1897 struct berval *list,
1898 const struct berval *attr,
1899 struct berval *val )
1905 /* loop through each rights/attr pair, skip first part (action) */
1907 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1908 if (aci_list_has_attr(&bv, attr, val) == 0)
1910 if (aci_get_part(list, i, ';', &bv) < 0)
1912 mask |= aci_list_map_rights(&bv);
1918 aci_list_get_rights(
1919 struct berval *list,
1920 const struct berval *attr,
1922 slap_access_t *grant,
1923 slap_access_t *deny )
1925 struct berval perm, actn;
1926 slap_access_t *mask;
1929 if (attr == NULL || attr->bv_len == 0
1930 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1931 attr = &aci_bv_br_entry;
1937 /* loop through each permissions clause */
1938 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1939 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1941 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1943 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1950 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1951 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1958 struct berval *subj,
1959 struct berval *defgrpoc,
1960 struct berval *defgrpat,
1966 struct berval subjdn;
1967 struct berval grpoc;
1968 struct berval grpat;
1969 ObjectClass *grp_oc = NULL;
1970 AttributeDescription *grp_ad = NULL;
1974 /* format of string is "group/objectClassValue/groupAttrName" */
1975 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1979 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1983 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1987 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1988 if( rc != LDAP_SUCCESS ) {
1994 grp_oc = oc_bvfind( &grpoc );
1996 if (grp_oc != NULL && grp_ad != NULL ) {
1997 char buf[ACL_BUF_SIZE];
1998 struct berval bv, ndn;
1999 bv.bv_len = sizeof( buf ) - 1;
2000 bv.bv_val = (char *)&buf;
2001 string_expand(&bv, &subjdn, e->e_ndn, matches);
2002 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2003 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2004 grp_oc, grp_ad) == 0);
2017 AttributeDescription *desc,
2020 regmatch_t *matches,
2021 slap_access_t *grant,
2022 slap_access_t *deny,
2023 struct berval *scope
2026 struct berval bv, perms, sdn;
2030 assert( desc->ad_cname.bv_val != NULL );
2032 /* parse an aci of the form:
2033 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2035 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2036 a full description of the format for this attribute.
2037 Differences: "this" in the draft is "self" here, and
2038 "self" and "public" is in the position of dnType.
2040 For now, this routine only supports scope=entry.
2042 /* check that the aci has all 5 components */
2043 if (aci_get_part(aci, 4, '#', NULL) < 0)
2046 /* check that the aci family is supported */
2047 if (aci_get_part(aci, 0, '#', &bv) < 0)
2050 /* check that the scope matches */
2051 if (aci_get_part(aci, 1, '#', &bv) < 0
2052 || ber_bvstrcasecmp( scope, &bv ) != 0)
2057 /* get the list of permissions clauses, bail if empty */
2058 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2061 /* check if any permissions allow desired access */
2062 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2065 /* see if we have a DN match */
2066 if (aci_get_part(aci, 3, '#', &bv) < 0)
2069 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2072 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2075 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2076 if (dn_match( &op->o_ndn, &ndn))
2082 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2085 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2086 if (dn_match(&op->o_ndn, &e->e_nname))
2089 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2091 AttributeDescription *ad = NULL;
2094 rc = slap_bv2ad( &sdn, &ad, &text );
2096 if( rc != LDAP_SUCCESS ) {
2104 for(at = attrs_find( e->e_attrs, ad );
2106 at = attrs_find( at->a_next, ad ) )
2108 if (value_find_ex( ad,
2109 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2110 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2112 &bv, op->o_tmpmemctx) == 0 )
2122 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2123 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2126 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2127 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2130 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2131 if (aci_match_set(&sdn, op, e, 0))
2134 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2135 if (aci_match_set(&sdn, op, e, 1))
2143 #endif /* SLAPD_ACI_ENABLED */
2150 regmatch_t *matches)
2158 bv->bv_val[0] = '\0';
2159 bv->bv_len--; /* leave space for lone $ */
2162 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2163 sp < pat->bv_val + pat->bv_len ; sp++ )
2165 /* did we previously see a $ */
2167 if ( flag == 1 && *sp == '$' ) {
2172 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2175 } else if ( *sp >= '0' && *sp <= '9' ) {
2183 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2184 if ( *sp >= '0' && *sp <= '9' ) {
2185 n = 10*n + ( *sp - '0' );
2189 if ( *sp != /*'{'*/ '}' ) {
2194 if ( n >= MAXREMATCHES ) {
2199 i = matches[n].rm_so;
2200 l = matches[n].rm_eo;
2201 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2219 /* must have ended with a single $ */
2228 LDAP_LOG( ACL, DETAIL1,
2229 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2230 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2232 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2233 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2239 struct berval *pat, /* pattern to expand and match against */
2240 char *str, /* string to match against pattern */
2241 char *buf, /* buffer with $N expansion variables */
2242 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2246 char newbuf[ACL_BUF_SIZE];
2250 bv.bv_len = sizeof(newbuf) - 1;
2253 if(str == NULL) str = "";
2255 string_expand(&bv, pat, buf, matches);
2256 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2257 char error[ACL_BUF_SIZE];
2258 regerror(rc, &re, error, sizeof(error));
2262 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2263 pat->bv_val, str, error );
2265 Debug( LDAP_DEBUG_TRACE,
2266 "compile( \"%s\", \"%s\") failed %s\n",
2267 pat->bv_val, str, error );
2272 rc = regexec(&re, str, 0, NULL, 0);
2276 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2277 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2278 rc, rc ? "matches" : "no matches", 0 );
2280 Debug( LDAP_DEBUG_TRACE,
2281 "=> regex_matches: string: %s\n", str, 0, 0 );
2282 Debug( LDAP_DEBUG_TRACE,
2283 "=> regex_matches: rc: %d %s\n",
2284 rc, !rc ? "matches" : "no matches", 0 );