1 /* acl.c - routines to parse and check acl's */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
40 #include "slapi/slapi.h"
41 #endif /* LDAPI_SLAPI */
43 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
49 aci_bv_entry = BER_BVC("entry"),
50 aci_bv_children = BER_BVC("children"),
51 aci_bv_br_entry = BER_BVC("[entry]"),
52 aci_bv_br_all = BER_BVC("[all]"),
53 aci_bv_access_id = BER_BVC("access-id"),
54 aci_bv_anonymous = BER_BVC("anonymous"),
55 aci_bv_public = BER_BVC("public"),
56 aci_bv_users = BER_BVC("users"),
57 aci_bv_self = BER_BVC("self"),
58 aci_bv_dnattr = BER_BVC("dnattr"),
59 aci_bv_group = BER_BVC("group"),
60 aci_bv_role = BER_BVC("role"),
61 aci_bv_set = BER_BVC("set"),
62 aci_bv_set_ref = BER_BVC("set-ref"),
63 aci_bv_grant = BER_BVC("grant"),
64 aci_bv_deny = BER_BVC("deny"),
66 aci_bv_ip_eq = BER_BVC("IP="),
68 aci_bv_path_eq = BER_BVC("PATH="),
69 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
70 #endif /* LDAP_PF_LOCAL */
72 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
73 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
74 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
75 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
76 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
79 static AccessControl * acl_get(
80 AccessControl *ac, int *count,
81 Operation *op, Entry *e,
82 AttributeDescription *desc,
84 int nmatches, regmatch_t *matches,
85 AccessControlState *state );
87 static slap_control_t acl_mask(
88 AccessControl *ac, slap_mask_t *mask,
89 Operation *op, Entry *e,
90 AttributeDescription *desc,
94 AccessControlState *state );
96 #ifdef SLAPD_ACI_ENABLED
98 Operation *op, Entry *e,
99 AttributeDescription *desc,
103 slap_access_t *grant,
105 struct berval *scope);
108 static int regex_matches(
109 struct berval *pat, char *str, char *buf, regmatch_t *matches);
110 static void string_expand(
111 struct berval *newbuf, struct berval *pattern,
112 char *match, regmatch_t *matches);
114 typedef struct AciSetCookie {
119 SLAP_SET_GATHER aci_set_gather;
120 static int aci_match_set ( struct berval *subj, Operation *op,
121 Entry *e, int setref );
124 * access_allowed - check whether op->o_ndn is allowed the requested access
125 * to entry e, attribute attr, value val. if val is null, access to
126 * the whole attribute is assumed (all values).
128 * This routine loops through all access controls and calls
129 * acl_mask() on each applicable access control.
130 * The loop exits when a definitive answer is reached or
131 * or no more controls remain.
142 AttributeDescription *desc,
144 slap_access_t access,
145 AccessControlState *state )
149 AccessControl *a = NULL;
154 char accessmaskbuf[ACCESSMASK_MAXLEN];
157 slap_control_t control;
159 regmatch_t matches[MAXREMATCHES];
160 int st_same_attr = 0;
161 static AccessControlState state_init = ACL_STATE_INIT;
164 assert( desc != NULL );
165 assert( access > ACL_NONE );
167 attr = desc->ad_cname.bv_val;
169 assert( attr != NULL );
171 if( op && op->o_is_auth_check &&
172 ( access == ACL_SEARCH || access == ACL_READ ))
178 if ( state->as_vd_ad==desc) {
179 if ( state->as_recorded ) {
180 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
183 return state->as_result;
184 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
185 val != NULL && state->as_vd_acl == NULL )
187 return state->as_result;
195 state->as_vd_ad=desc;
199 LDAP_LOG( ACL, ENTRY,
200 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
201 access2str( access ), e->e_dn, attr );
203 Debug( LDAP_DEBUG_ACL,
204 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
205 access2str( access ), e->e_dn, attr );
219 assert( be != NULL );
222 if ( op->o_pb != NULL ) {
223 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
225 /* ACL plugin denied access */
229 #endif /* LDAP_SLAPI */
231 /* grant database root access */
232 if ( be != NULL && be_isroot( op ) ) {
235 "access_allowed: conn %lu root access granted\n",
236 op->o_connid, 0, 0 );
238 Debug( LDAP_DEBUG_ACL,
239 "<= root access granted\n",
246 * no-user-modification operational attributes are ignored
247 * by ACL_WRITE checking as any found here are not provided
250 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
251 && desc != slap_schema.si_ad_entry
252 && desc != slap_schema.si_ad_children )
255 LDAP_LOG( ACL, DETAIL1,
256 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
257 "access granted\n", op->o_connid, attr , 0 );
259 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
260 " %s access granted\n",
266 /* use backend default access if no backend acls */
267 if( be != NULL && be->be_acl == NULL ) {
269 LDAP_LOG( ACL, DETAIL1,
270 "access_allowed: backend default %s access %s to \"%s\"\n",
271 access2str( access ),
272 be->be_dfltaccess >= access ? "granted" : "denied",
273 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
275 Debug( LDAP_DEBUG_ACL,
276 "=> access_allowed: backend default %s access %s to \"%s\"\n",
277 access2str( access ),
278 be->be_dfltaccess >= access ? "granted" : "denied",
279 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
281 ret = be->be_dfltaccess >= access;
285 /* be is always non-NULL */
286 /* use global default access if no global acls */
287 } else if ( be == NULL && global_acl == NULL ) {
289 LDAP_LOG( ACL, DETAIL1,
290 "access_allowed: global default %s access %s to \"%s\"\n",
291 access2str( access ),
292 global_default_access >= access ? "granted" : "denied",
295 Debug( LDAP_DEBUG_ACL,
296 "=> access_allowed: global default %s access %s to \"%s\"\n",
297 access2str( access ),
298 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
300 ret = global_default_access >= access;
309 assert( state->as_vd_acl != NULL );
311 a = state->as_vd_acl;
312 count = state->as_vd_acl_count;
313 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
314 mask = state->as_vd_acl_mask;
315 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
320 if ( state ) state->as_vi_acl = NULL;
324 memset(matches, '\0', sizeof(matches));
327 while((a = acl_get( a, &count, op, e, desc, val,
328 MAXREMATCHES, matches, state )) != NULL)
332 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
334 LDAP_LOG( ACL, DETAIL1,
335 "access_allowed: match[%d]: %d %d ",
336 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
338 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
339 (int)matches[i].rm_so, (int)matches[i].rm_eo );
341 if( matches[i].rm_so <= matches[0].rm_eo ) {
343 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
344 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
348 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
350 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
355 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
356 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
357 ret = state->as_result;
360 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
365 control = acl_mask( a, &mask, op,
366 e, desc, val, matches, count, state );
368 if ( control != ACL_BREAK ) {
372 memset(matches, '\0', sizeof(matches));
375 if ( ACL_IS_INVALID( mask ) ) {
377 LDAP_LOG( ACL, DETAIL1,
378 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
379 op->o_connid, e->e_dn, attr );
381 Debug( LDAP_DEBUG_ACL,
382 "=> access_allowed: \"%s\" (%s) invalid!\n",
387 } else if ( control == ACL_BREAK ) {
389 LDAP_LOG( ACL, DETAIL1,
390 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
392 Debug( LDAP_DEBUG_ACL,
393 "=> access_allowed: no more rules\n", 0, 0, 0);
400 LDAP_LOG( ACL, ENTRY,
401 "access_allowed: %s access %s by %s\n",
402 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
403 accessmask2str( mask, accessmaskbuf ) );
405 Debug( LDAP_DEBUG_ACL,
406 "=> access_allowed: %s access %s by %s\n",
407 access2str( access ),
408 ACL_GRANT(mask, access) ? "granted" : "denied",
409 accessmask2str( mask, accessmaskbuf ) );
412 ret = ACL_GRANT(mask, access);
415 if( state != NULL ) {
416 /* If not value-dependent, save ACL in case of more attrs */
417 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
418 state->as_vi_acl = a;
419 state->as_result = ret;
421 state->as_recorded |= ACL_STATE_RECORDED;
423 if (be_null) op->o_bd = NULL;
429 * acl_get - return the acl applicable to entry e, attribute
430 * attr. the acl returned is suitable for use in subsequent calls to
431 * acl_access_allowed().
434 static AccessControl *
440 AttributeDescription *desc,
444 AccessControlState *state )
451 assert( count != NULL );
452 assert( desc != NULL );
454 attr = desc->ad_cname.bv_val;
456 assert( attr != NULL );
459 if( op->o_bd == NULL ) {
462 a = op->o_bd->be_acl;
473 dnlen = e->e_nname.bv_len;
475 for ( ; a != NULL; a = a->acl_next ) {
478 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
479 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
481 LDAP_LOG( ACL, DETAIL1,
482 "acl_get: dnpat [%d] %s nsub: %d\n",
483 *count, a->acl_dn_pat.bv_val,
484 (int) a->acl_dn_re.re_nsub );
486 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
487 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
489 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
494 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
495 *count, a->acl_dn_pat.bv_val, 0 );
497 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
498 *count, a->acl_dn_pat.bv_val, 0 );
500 patlen = a->acl_dn_pat.bv_len;
501 if ( dnlen < patlen )
504 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
505 /* base dn -- entire object DN must match */
506 if ( dnlen != patlen )
509 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
510 int rdnlen = -1, sep = 0;
512 if ( dnlen <= patlen )
516 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
521 rdnlen = dn_rdnlen( NULL, &e->e_nname );
522 if ( rdnlen != dnlen - patlen - sep )
525 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
526 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
529 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
530 if ( dnlen <= patlen )
532 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
536 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
541 LDAP_LOG( ACL, DETAIL1,
542 "acl_get: [%d] matched\n", *count, 0, 0 );
544 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
549 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
550 matches[0].rm_so = matches[0].rm_eo = -1;
554 /* Is this ACL only for a specific value? */
555 if ( a->acl_attrval.bv_len ) {
560 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
561 state->as_recorded |= ACL_STATE_RECORDED_VD;
562 state->as_vd_acl = prev;
563 state->as_vd_acl_count = *count;
564 state->as_vd_access = a->acl_access;
565 state->as_vd_access_count = 1;
566 ACL_INVALIDATE( state->as_vd_acl_mask );
569 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
571 LDAP_LOG( ACL, DETAIL1,
572 "acl_get: valpat %s\n",
573 a->acl_attrval.bv_val, 0, 0 );
575 Debug( LDAP_DEBUG_ACL,
576 "acl_get: valpat %s\n",
577 a->acl_attrval.bv_val, 0, 0 );
579 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
585 LDAP_LOG( ACL, DETAIL1,
587 a->acl_attrval.bv_val, 0, 0 );
589 Debug( LDAP_DEBUG_ACL,
591 a->acl_attrval.bv_val, 0, 0 );
594 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
595 if (value_match( &match, desc,
596 desc->ad_type->sat_equality, 0,
597 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
604 patlen = a->acl_attrval.bv_len;
605 vdnlen = val->bv_len;
607 if ( vdnlen < patlen )
610 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
611 if ( vdnlen > patlen )
614 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
617 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
620 rdnlen = dn_rdnlen( NULL, val );
621 if ( rdnlen != vdnlen - patlen - 1 )
624 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
625 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
628 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
629 if ( vdnlen <= patlen )
632 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
636 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
642 if ( a->acl_filter != NULL ) {
643 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
644 if ( rc != LDAP_COMPARE_TRUE ) {
650 LDAP_LOG( ACL, DETAIL1,
651 "acl_get: [%d] attr %s\n", *count, attr ,0 );
653 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
660 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
662 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
668 * Record value-dependent access control state
670 #define ACL_RECORD_VALUE_STATE do { \
671 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
672 state->as_recorded |= ACL_STATE_RECORDED_VD; \
673 state->as_vd_acl = a; \
674 AC_MEMCPY( state->as_vd_acl_matches, matches, \
675 sizeof( state->as_vd_acl_matches )) ; \
676 state->as_vd_acl_count = count; \
677 state->as_vd_access = b; \
678 state->as_vd_access_count = i; \
683 * acl_mask - modifies mask based upon the given acl and the
684 * requested access to entry e, attribute attr, value val. if val
685 * is null, access to the whole attribute is assumed (all values).
687 * returns 0 access NOT allowed
691 static slap_control_t
697 AttributeDescription *desc,
701 AccessControlState *state )
703 int i, odnlen, patlen;
706 char accessmaskbuf[ACCESSMASK_MAXLEN];
707 char accessmaskbuf1[ACCESSMASK_MAXLEN];
712 assert( mask != NULL );
713 assert( desc != NULL );
715 attr = desc->ad_cname.bv_val;
717 assert( attr != NULL );
720 LDAP_LOG( ACL, ENTRY,
721 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
722 op->o_connid, e->e_dn, attr );
725 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
726 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
727 accessmask2str( *mask, accessmaskbuf ) );
729 Debug( LDAP_DEBUG_ACL,
730 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
733 Debug( LDAP_DEBUG_ACL,
734 "=> acl_mask: to %s by \"%s\", (%s) \n",
735 val ? "value" : "all values",
736 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
737 accessmask2str( *mask, accessmaskbuf ) );
741 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
742 && state->as_vd_acl == a )
744 b = state->as_vd_access;
745 i = state->as_vd_access_count;
752 for ( ; b != NULL; b = b->a_next, i++ ) {
753 slap_mask_t oldmask, modmask;
755 ACL_INVALIDATE( modmask );
757 /* AND <who> clauses */
758 if ( b->a_dn_pat.bv_len != 0 ) {
760 LDAP_LOG( ACL, DETAIL1,
761 "acl_mask: conn %lu check a_dn_pat: %s\n",
762 op->o_connid, b->a_dn_pat.bv_val ,0 );
764 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
765 b->a_dn_pat.bv_val, 0, 0);
768 * if access applies to the entry itself, and the
769 * user is bound as somebody in the same namespace as
770 * the entry, OR the given dn matches the dn pattern
772 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
773 if ( op->o_ndn.bv_len != 0 ) {
777 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
778 if ( op->o_ndn.bv_len == 0 ) {
782 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
783 if ( op->o_ndn.bv_len == 0 ) {
787 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
791 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
792 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
793 int ret = regex_matches( &b->a_dn_pat,
794 op->o_ndn.bv_val, e->e_ndn, matches );
805 if ( e->e_dn == NULL )
808 if ( b->a_dn_expand ) {
810 char buf[ACL_BUF_SIZE];
812 bv.bv_len = sizeof( buf ) - 1;
815 string_expand(&bv, &b->a_dn_pat,
817 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
818 /* did not expand to a valid dn */
826 odnlen = op->o_ndn.bv_len;
827 if ( odnlen < patlen ) {
828 goto dn_match_cleanup;
832 if ( b->a_dn_style == ACL_STYLE_BASE ) {
833 /* base dn -- entire object DN must match */
834 if ( odnlen != patlen ) {
835 goto dn_match_cleanup;
838 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
841 if ( odnlen <= patlen ) {
842 goto dn_match_cleanup;
845 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
846 goto dn_match_cleanup;
849 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
850 if ( rdnlen != odnlen - patlen - 1 ) {
851 goto dn_match_cleanup;
854 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
855 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
856 goto dn_match_cleanup;
859 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
860 if ( odnlen <= patlen ) {
861 goto dn_match_cleanup;
864 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
865 goto dn_match_cleanup;
869 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
872 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
882 if ( b->a_sockurl_pat.bv_len ) {
883 if ( ! op->o_conn->c_listener ) {
887 LDAP_LOG( ACL, DETAIL1,
888 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
889 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
891 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
892 b->a_sockurl_pat.bv_val, 0, 0 );
895 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
896 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
897 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
898 e->e_ndn, matches ) )
903 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
905 char buf[ACL_BUF_SIZE];
907 bv.bv_len = sizeof( buf ) - 1;
909 string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, matches );
911 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 ) {
916 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
922 if ( b->a_domain_pat.bv_len ) {
923 if ( !op->o_conn->c_peer_domain.bv_val ) {
927 LDAP_LOG( ACL, DETAIL1,
928 "acl_mask: conn %lu check a_domain_pat: %s\n",
929 op->o_connid, b->a_domain_pat.bv_val , 0 );
931 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
932 b->a_domain_pat.bv_val, 0, 0 );
934 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
935 if ( b->a_domain_style == ACL_STYLE_REGEX) {
936 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
937 e->e_ndn, matches ) )
942 char buf[ACL_BUF_SIZE];
944 struct berval cmp = op->o_conn->c_peer_domain;
945 struct berval pat = b->a_domain_pat;
947 if ( b->a_domain_expand ) {
950 bv.bv_len = sizeof(buf) - 1;
953 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
957 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
958 int offset = cmp.bv_len - pat.bv_len;
963 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
967 /* trim the domain */
968 cmp.bv_val = &cmp.bv_val[ offset ];
969 cmp.bv_len -= offset;
972 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
979 if ( b->a_peername_pat.bv_len ) {
980 if ( !op->o_conn->c_peer_name.bv_val ) {
984 LDAP_LOG( ACL, DETAIL1,
985 "acl_mask: conn %lu check a_peername_path: %s\n",
986 op->o_connid, b->a_peername_pat.bv_val , 0 );
988 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
989 b->a_peername_pat.bv_val, 0, 0 );
991 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
992 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
993 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
994 e->e_ndn, matches ) )
1000 /* try exact match */
1001 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1002 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1006 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1008 char buf[ACL_BUF_SIZE];
1010 bv.bv_len = sizeof( buf ) - 1;
1012 string_expand( &bv, &b->a_peername_pat, e->e_ndn, matches );
1014 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1018 /* extract IP and try exact match */
1019 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1021 char buf[] = "255.255.255.255";
1024 int port_number = -1;
1026 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1027 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1030 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1031 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1033 port = strrchr( ip.bv_val, ':' );
1037 ip.bv_len = port - ip.bv_val;
1039 port_number = strtol( port, &next, 10 );
1040 if ( next[0] != '\0' )
1044 /* the port check can be anticipated here */
1045 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1048 /* address longer than expected? */
1049 if ( ip.bv_len >= sizeof(buf) )
1052 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1053 buf[ ip.bv_len ] = '\0';
1055 addr = inet_addr( buf );
1057 /* unable to convert? */
1058 if ( addr == (unsigned long)(-1) )
1061 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1064 #ifdef LDAP_PF_LOCAL
1065 /* extract path and try exact match */
1066 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1069 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1070 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1073 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1074 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1076 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1079 #endif /* LDAP_PF_LOCAL */
1081 /* exact match (very unlikely...) */
1082 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1089 if ( b->a_sockname_pat.bv_len ) {
1090 if ( !op->o_conn->c_sock_name.bv_val ) {
1094 LDAP_LOG( ACL, DETAIL1,
1095 "acl_mask: conn %lu check a_sockname_path: %s\n",
1096 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1098 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1099 b->a_sockname_pat.bv_val, 0, 0 );
1101 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1102 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1103 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1104 e->e_ndn, matches ) )
1109 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1111 char buf[ACL_BUF_SIZE];
1113 bv.bv_len = sizeof( buf ) - 1;
1115 string_expand( &bv, &b->a_sockname_pat, e->e_ndn, matches );
1117 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1122 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1128 if ( b->a_dn_at != NULL ) {
1133 const char *attr = b->a_dn_at->ad_cname.bv_val;
1135 assert( attr != NULL );
1137 if ( op->o_ndn.bv_len == 0 ) {
1142 LDAP_LOG( ACL, DETAIL1,
1143 "acl_mask: conn %lu check a_dn_pat: %s\n",
1144 op->o_connid, attr , 0 );
1146 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1151 /* see if asker is listed in dnattr */
1152 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1154 at = attrs_find( at->a_next, b->a_dn_at ) )
1156 if( value_find_ex( b->a_dn_at,
1157 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1158 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1160 &bv, op->o_tmpmemctx ) == 0 )
1169 /* have a dnattr match. if this is a self clause then
1170 * the target must also match the op dn.
1172 if ( b->a_dn_self ) {
1173 /* check if the target is an attribute. */
1174 if ( val == NULL ) continue;
1176 /* target is attribute, check if the attribute value
1179 rc = value_match( &match, b->a_dn_at,
1180 b->a_dn_at->ad_type->sat_equality, 0,
1182 /* on match error or no match, fail the ACL clause */
1183 if (rc != LDAP_SUCCESS || match != 0 )
1187 /* no dnattr match, check if this is a self clause */
1188 if ( ! b->a_dn_self )
1191 ACL_RECORD_VALUE_STATE;
1193 /* this is a self clause, check if the target is an
1199 /* target is attribute, check if the attribute value
1202 rc = value_match( &match, b->a_dn_at,
1203 b->a_dn_at->ad_type->sat_equality, 0,
1206 /* on match error or no match, fail the ACL clause */
1207 if (rc != LDAP_SUCCESS || match != 0 )
1212 if ( b->a_group_pat.bv_len ) {
1214 struct berval ndn = BER_BVNULL;
1217 if ( op->o_ndn.bv_len == 0 ) {
1221 /* b->a_group is an unexpanded entry name, expanded it should be an
1222 * entry with objectclass group* and we test to see if odn is one of
1223 * the values in the attribute group
1225 /* see if asker is listed in dnattr */
1226 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1227 char buf[ACL_BUF_SIZE];
1228 bv.bv_len = sizeof(buf) - 1;
1231 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1232 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1233 /* did not expand to a valid dn */
1240 bv = b->a_group_pat;
1243 rc = backend_group( op, e, &bv, &op->o_ndn,
1244 b->a_group_oc, b->a_group_at );
1246 if ( ndn.bv_val ) free( ndn.bv_val );
1253 if ( b->a_set_pat.bv_len != 0 ) {
1255 char buf[ACL_BUF_SIZE];
1256 if( b->a_set_style == ACL_STYLE_REGEX ){
1257 bv.bv_len = sizeof(buf) - 1;
1259 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1263 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1268 if ( b->a_authz.sai_ssf ) {
1270 LDAP_LOG( ACL, DETAIL1,
1271 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1272 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1274 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1275 b->a_authz.sai_ssf, op->o_ssf, 0 );
1277 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1282 if ( b->a_authz.sai_transport_ssf ) {
1284 LDAP_LOG( ACL, DETAIL1,
1285 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1287 op->o_connid, b->a_authz.sai_transport_ssf,
1288 op->o_transport_ssf );
1290 Debug( LDAP_DEBUG_ACL,
1291 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1292 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1294 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1299 if ( b->a_authz.sai_tls_ssf ) {
1301 LDAP_LOG( ACL, DETAIL1,
1302 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1304 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1306 Debug( LDAP_DEBUG_ACL,
1307 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1308 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1310 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1315 if ( b->a_authz.sai_sasl_ssf ) {
1317 LDAP_LOG( ACL, DETAIL1,
1318 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1320 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1322 Debug( LDAP_DEBUG_ACL,
1323 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1324 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1326 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1331 #ifdef SLAPD_ACI_ENABLED
1332 if ( b->a_aci_at != NULL ) {
1334 slap_access_t grant, deny, tgrant, tdeny;
1335 struct berval parent_ndn, old_parent_ndn;
1336 BerVarray bvals = NULL;
1339 /* this case works different from the others above.
1340 * since aci's themselves give permissions, we need
1341 * to first check b->a_access_mask, the ACL's access level.
1344 if ( e->e_nname.bv_len == 0 ) {
1345 /* no ACIs in the root DSE */
1349 /* first check if the right being requested
1350 * is allowed by the ACL clause.
1352 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1355 /* start out with nothing granted, nothing denied */
1359 /* get the aci attribute */
1360 at = attr_find( e->e_attrs, b->a_aci_at );
1362 ACL_RECORD_VALUE_STATE;
1363 /* the aci is an multi-valued attribute. The
1364 * rights are determined by OR'ing the individual
1365 * rights given by the acis.
1367 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1371 matches, &grant, &deny, &aci_bv_entry ) != 0)
1377 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1378 accessmask2str(tgrant,accessmaskbuf),
1379 accessmask2str(tdeny, accessmaskbuf1), 0);
1382 /* If the entry level aci didn't contain anything valid for the
1383 * current operation, climb up the tree and evaluate the
1384 * acis with scope set to subtree
1386 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1387 dnParent(&(e->e_nname), &parent_ndn);
1388 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1389 old_parent_ndn = parent_ndn;
1390 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1391 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1395 for( i = 0; bvals[i].bv_val != NULL; i++){
1396 ACL_RECORD_VALUE_STATE;
1397 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1398 &grant, &deny, &aci_bv_children) != 0) {
1401 /* evaluation stops as soon as either a "deny" or a
1402 * "grant" directive matches.
1404 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1408 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1409 accessmask2str(tgrant,accessmaskbuf),
1410 accessmask2str(tdeny, accessmaskbuf1), 0);
1415 case LDAP_NO_SUCH_ATTRIBUTE:
1416 /* just go on if the aci-Attribute is not present in
1419 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1422 case LDAP_NO_SUCH_OBJECT:
1423 /* We have reached the base object */
1424 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1434 dnParent(&old_parent_ndn, &parent_ndn);
1439 /* remove anything that the ACL clause does not allow */
1440 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1441 tdeny &= ACL_PRIV_MASK;
1443 /* see if we have anything to contribute */
1444 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1448 /* this could be improved by changing acl_mask so that it can deal with
1449 * by clauses that return grant/deny pairs. Right now, it does either
1450 * additive or subtractive rights, but not both at the same time. So,
1451 * we need to combine the grant/deny pair into a single rights mask in
1452 * a smart way: if either grant or deny is "empty", then we use the
1453 * opposite as is, otherwise we remove any denied rights from the grant
1454 * rights mask and construct an additive mask.
1456 if (ACL_IS_INVALID(tdeny)) {
1457 modmask = tgrant | ACL_PRIV_ADDITIVE;
1459 } else if (ACL_IS_INVALID(tgrant)) {
1460 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1463 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1469 modmask = b->a_access_mask;
1473 LDAP_LOG( ACL, RESULTS,
1474 "acl_mask: [%d] applying %s (%s)\n",
1475 i, accessmask2str( modmask, accessmaskbuf),
1476 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1477 ? "break" : "stop" );
1479 Debug( LDAP_DEBUG_ACL,
1480 "<= acl_mask: [%d] applying %s (%s)\n",
1481 i, accessmask2str( modmask, accessmaskbuf ),
1482 b->a_type == ACL_CONTINUE
1484 : b->a_type == ACL_BREAK
1491 if( ACL_IS_ADDITIVE(modmask) ) {
1493 ACL_PRIV_SET( *mask, modmask );
1496 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1498 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1499 /* substract privs */
1500 ACL_PRIV_CLR( *mask, modmask );
1503 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1511 LDAP_LOG( ACL, DETAIL1,
1512 "acl_mask: conn %lu [%d] mask: %s\n",
1513 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1515 Debug( LDAP_DEBUG_ACL,
1516 "<= acl_mask: [%d] mask: %s\n",
1517 i, accessmask2str(*mask, accessmaskbuf), 0 );
1520 if( b->a_type == ACL_CONTINUE ) {
1523 } else if ( b->a_type == ACL_BREAK ) {
1531 /* implicit "by * none" clause */
1535 LDAP_LOG( ACL, RESULTS,
1536 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1537 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1539 Debug( LDAP_DEBUG_ACL,
1540 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1541 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1547 * acl_check_modlist - check access control on the given entry to see if
1548 * it allows the given modifications by the user associated with op.
1549 * returns 1 if mods allowed ok
1550 * 0 mods not allowed
1557 Modifications *mlist
1561 AccessControlState state = ACL_STATE_INIT;
1564 int ret = 1; /* default is access allowed */
1572 assert( be != NULL );
1574 /* short circuit root database access */
1575 if ( be_isroot( op ) ) {
1577 LDAP_LOG( ACL, DETAIL1,
1578 "acl_check_modlist: conn %lu access granted to root user\n",
1579 op->o_connid, 0, 0 );
1581 Debug( LDAP_DEBUG_ACL,
1582 "<= acl_access_allowed: granted to database root\n",
1588 /* use backend default access if no backend acls */
1589 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1591 LDAP_LOG( ACL, DETAIL1,
1592 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1593 access2str( ACL_WRITE ),
1594 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1597 Debug( LDAP_DEBUG_ACL,
1598 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1599 access2str( ACL_WRITE ),
1600 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1602 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1606 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1608 * no-user-modification operational attributes are ignored
1609 * by ACL_WRITE checking as any found here are not provided
1612 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1614 LDAP_LOG( ACL, DETAIL1,
1615 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1616 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1618 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1619 " modify access granted\n",
1620 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1625 switch ( mlist->sml_op ) {
1626 case LDAP_MOD_REPLACE:
1628 * We must check both permission to delete the whole
1629 * attribute and permission to add the specific attributes.
1630 * This prevents abuse from selfwriters.
1632 if ( ! access_allowed( op, e,
1633 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1639 if ( mlist->sml_values == NULL ) break;
1641 /* fall thru to check value to add */
1644 assert( mlist->sml_values != NULL );
1646 for ( bv = mlist->sml_nvalues
1647 ? mlist->sml_nvalues : mlist->sml_values;
1648 bv->bv_val != NULL; bv++ )
1650 if ( ! access_allowed( op, e,
1651 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1659 case LDAP_MOD_DELETE:
1660 if ( mlist->sml_values == NULL ) {
1661 if ( ! access_allowed( op, e,
1662 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1669 for ( bv = mlist->sml_nvalues
1670 ? mlist->sml_nvalues : mlist->sml_values;
1671 bv->bv_val != NULL; bv++ )
1673 if ( ! access_allowed( op, e,
1674 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1682 case SLAP_MOD_SOFTADD:
1683 /* allow adding attribute via modrdn thru */
1695 if (be_null) op->o_bd = NULL;
1701 struct berval *list,
1714 while (len >= 0 && --ix >= 0) {
1715 while (--len >= 0 && *p++ != sep) ;
1717 while (len >= 0 && *p == ' ') {
1728 while (--len >= 0 && *p != sep) {
1732 while (bv->bv_len > 0 && *--p == ' ')
1738 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1740 AciSetCookie *cp = (AciSetCookie *)cookie;
1741 BerVarray bvals = NULL;
1744 /* this routine needs to return the bervals instead of
1745 * plain strings, since syntax is not known. It should
1746 * also return the syntax or some "comparison cookie".
1749 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1751 AttributeDescription *desc = NULL;
1752 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1753 backend_attribute(cp->op,
1754 cp->e, &ndn, desc, &bvals);
1756 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1763 struct berval *subj,
1769 struct berval set = BER_BVNULL;
1771 AciSetCookie cookie;
1772 Operation op2 = *op;
1777 ber_dupbv_x( &set, subj, op2.o_tmpmemctx );
1780 struct berval subjdn, ndn = BER_BVNULL;
1781 struct berval setat;
1784 AttributeDescription *desc = NULL;
1786 /* format of string is "entry/setAttrName" */
1787 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1791 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1792 setat = aci_bv_set_attr;
1796 * NOTE: dnNormalize honors the ber_len field
1797 * as the length of the dn to be normalized
1799 if ( slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS ) {
1800 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op2.o_tmpmemctx) == LDAP_SUCCESS )
1802 backend_attribute(&op2, e, &ndn, desc, &bvals);
1803 if ( bvals != NULL && bvals[0].bv_val != NULL ) {
1806 bvals[0].bv_val = NULL;
1807 for (i=1;bvals[i].bv_val;i++);
1808 bvals[0].bv_val = bvals[i-1].bv_val;
1809 bvals[i-1].bv_val = NULL;
1811 ber_bvarray_free_x(bvals, op2.o_tmpmemctx);
1812 sl_free(ndn.bv_val, op2.o_tmpmemctx);
1817 if (set.bv_val != NULL) {
1820 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1821 &op2.o_ndn, &e->e_nname, NULL) > 0);
1822 sl_free(set.bv_val, op2.o_tmpmemctx);
1828 #ifdef SLAPD_ACI_ENABLED
1830 aci_list_map_rights(
1831 struct berval *list )
1838 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1841 switch (*bv.bv_val) {
1843 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1846 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1847 * the right 's' to mean "set", but in the examples states
1848 * that the right 's' means "search". The latter definition
1851 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1854 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1857 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1860 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1861 * define any equivalent to the AUTH right, so I've just used
1864 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1876 struct berval *list,
1877 const struct berval *attr,
1878 struct berval *val )
1880 struct berval bv, left, right;
1883 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1884 if (aci_get_part(&bv, 0, '=', &left) < 0
1885 || aci_get_part(&bv, 1, '=', &right) < 0)
1887 if (ber_bvstrcasecmp(attr, &bv) == 0)
1889 } else if (val == NULL) {
1890 if (ber_bvstrcasecmp(attr, &left) == 0)
1893 if (ber_bvstrcasecmp(attr, &left) == 0) {
1894 /* this is experimental code that implements a
1895 * simple (prefix) match of the attribute value.
1896 * the ACI draft does not provide for aci's that
1897 * apply to specific values, but it would be
1898 * nice to have. If the <attr> part of an aci's
1899 * rights list is of the form <attr>=<value>,
1900 * that means the aci applies only to attrs with
1901 * the given value. Furthermore, if the attr is
1902 * of the form <attr>=<value>*, then <value> is
1903 * treated as a prefix, and the aci applies to
1904 * any value with that prefix.
1906 * Ideally, this would allow r.e. matches.
1908 if (aci_get_part(&right, 0, '*', &left) < 0
1909 || right.bv_len <= left.bv_len)
1911 if (ber_bvstrcasecmp(val, &right) == 0)
1913 } else if (val->bv_len >= left.bv_len) {
1914 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1923 static slap_access_t
1924 aci_list_get_attr_rights(
1925 struct berval *list,
1926 const struct berval *attr,
1927 struct berval *val )
1933 /* loop through each rights/attr pair, skip first part (action) */
1935 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1936 if (aci_list_has_attr(&bv, attr, val) == 0)
1938 if (aci_get_part(list, i, ';', &bv) < 0)
1940 mask |= aci_list_map_rights(&bv);
1946 aci_list_get_rights(
1947 struct berval *list,
1948 const struct berval *attr,
1950 slap_access_t *grant,
1951 slap_access_t *deny )
1953 struct berval perm, actn;
1954 slap_access_t *mask;
1957 if (attr == NULL || attr->bv_len == 0
1958 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1959 attr = &aci_bv_br_entry;
1965 /* loop through each permissions clause */
1966 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1967 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1969 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1971 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1978 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1979 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1986 struct berval *subj,
1987 struct berval *defgrpoc,
1988 struct berval *defgrpat,
1994 struct berval subjdn;
1995 struct berval grpoc;
1996 struct berval grpat;
1997 ObjectClass *grp_oc = NULL;
1998 AttributeDescription *grp_ad = NULL;
2002 /* format of string is "group/objectClassValue/groupAttrName" */
2003 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2007 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2011 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2015 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2016 if( rc != LDAP_SUCCESS ) {
2022 grp_oc = oc_bvfind( &grpoc );
2024 if (grp_oc != NULL && grp_ad != NULL ) {
2025 char buf[ACL_BUF_SIZE];
2026 struct berval bv, ndn;
2027 bv.bv_len = sizeof( buf ) - 1;
2028 bv.bv_val = (char *)&buf;
2029 string_expand(&bv, &subjdn, e->e_ndn, matches);
2030 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2031 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2032 grp_oc, grp_ad) == 0);
2045 AttributeDescription *desc,
2048 regmatch_t *matches,
2049 slap_access_t *grant,
2050 slap_access_t *deny,
2051 struct berval *scope
2054 struct berval bv, perms, sdn;
2058 assert( desc->ad_cname.bv_val != NULL );
2060 /* parse an aci of the form:
2061 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2063 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2064 a full description of the format for this attribute.
2065 Differences: "this" in the draft is "self" here, and
2066 "self" and "public" is in the position of dnType.
2068 For now, this routine only supports scope=entry.
2070 /* check that the aci has all 5 components */
2071 if (aci_get_part(aci, 4, '#', NULL) < 0)
2074 /* check that the aci family is supported */
2075 if (aci_get_part(aci, 0, '#', &bv) < 0)
2078 /* check that the scope matches */
2079 if (aci_get_part(aci, 1, '#', &bv) < 0
2080 || ber_bvstrcasecmp( scope, &bv ) != 0)
2085 /* get the list of permissions clauses, bail if empty */
2086 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2089 /* check if any permissions allow desired access */
2090 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2093 /* see if we have a DN match */
2094 if (aci_get_part(aci, 3, '#', &bv) < 0)
2097 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2100 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2103 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2104 if (dn_match( &op->o_ndn, &ndn))
2110 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2113 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2114 if (dn_match(&op->o_ndn, &e->e_nname))
2117 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2119 AttributeDescription *ad = NULL;
2122 rc = slap_bv2ad( &sdn, &ad, &text );
2124 if( rc != LDAP_SUCCESS ) {
2132 for(at = attrs_find( e->e_attrs, ad );
2134 at = attrs_find( at->a_next, ad ) )
2136 if (value_find_ex( ad,
2137 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2138 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2140 &bv, op->o_tmpmemctx) == 0 )
2150 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2151 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2154 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2155 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2158 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2159 if (aci_match_set(&sdn, op, e, 0))
2162 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2163 if (aci_match_set(&sdn, op, e, 1))
2171 #endif /* SLAPD_ACI_ENABLED */
2178 regmatch_t *matches)
2186 bv->bv_val[0] = '\0';
2187 bv->bv_len--; /* leave space for lone $ */
2190 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2191 sp < pat->bv_val + pat->bv_len ; sp++ )
2193 /* did we previously see a $ */
2195 if ( flag == 1 && *sp == '$' ) {
2200 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2203 } else if ( *sp >= '0' && *sp <= '9' ) {
2211 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2212 if ( *sp >= '0' && *sp <= '9' ) {
2213 n = 10*n + ( *sp - '0' );
2217 if ( *sp != /*'{'*/ '}' ) {
2222 if ( n >= MAXREMATCHES ) {
2227 i = matches[n].rm_so;
2228 l = matches[n].rm_eo;
2229 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2247 /* must have ended with a single $ */
2256 LDAP_LOG( ACL, DETAIL1,
2257 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2258 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2260 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2261 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2267 struct berval *pat, /* pattern to expand and match against */
2268 char *str, /* string to match against pattern */
2269 char *buf, /* buffer with $N expansion variables */
2270 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2274 char newbuf[ACL_BUF_SIZE];
2278 bv.bv_len = sizeof(newbuf) - 1;
2281 if(str == NULL) str = "";
2283 string_expand(&bv, pat, buf, matches);
2284 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2285 char error[ACL_BUF_SIZE];
2286 regerror(rc, &re, error, sizeof(error));
2290 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2291 pat->bv_val, str, error );
2293 Debug( LDAP_DEBUG_TRACE,
2294 "compile( \"%s\", \"%s\") failed %s\n",
2295 pat->bv_val, str, error );
2300 rc = regexec(&re, str, 0, NULL, 0);
2304 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2305 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2306 rc, rc ? "matches" : "no matches", 0 );
2308 Debug( LDAP_DEBUG_TRACE,
2309 "=> regex_matches: string: %s\n", str, 0, 0 );
2310 Debug( LDAP_DEBUG_TRACE,
2311 "=> regex_matches: rc: %d %s\n",
2312 rc, !rc ? "matches" : "no matches", 0 );