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.
138 * - can be legally called with op == NULL
139 * - can be legally called with op->o_bd == NULL
146 AttributeDescription *desc,
148 slap_access_t access,
149 AccessControlState *state,
154 AccessControl *a = NULL;
159 char accessmaskbuf[ACCESSMASK_MAXLEN];
162 slap_control_t control;
164 regmatch_t matches[MAXREMATCHES];
165 int st_same_attr = 0;
166 static AccessControlState state_init = ACL_STATE_INIT;
169 assert( desc != NULL );
170 assert( access > ACL_NONE );
171 if ( maskp ) ACL_INVALIDATE( *maskp );
173 attr = desc->ad_cname.bv_val;
175 assert( attr != NULL );
177 if( op && op->o_is_auth_check &&
178 ( access == ACL_SEARCH || access == ACL_READ ))
184 if ( state->as_vd_ad==desc) {
185 if ( state->as_recorded ) {
186 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
189 return state->as_result;
190 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
191 val != NULL && state->as_vd_acl == NULL )
193 return state->as_result;
201 state->as_vd_ad=desc;
205 LDAP_LOG( ACL, ENTRY,
206 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
207 access2str( access ), e->e_dn, attr );
209 Debug( LDAP_DEBUG_ACL,
210 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
211 access2str( access ), e->e_dn, attr );
225 * FIXME: experimental; use first backend rules
226 * iff there is no global_acl (ITS#3100) */
227 if ( frontendDB->be_acl == NULL )
233 assert( be != NULL );
236 if ( op->o_pb != NULL ) {
237 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
239 /* ACL plugin denied access */
243 #endif /* LDAP_SLAPI */
245 /* grant database root access */
246 if ( be != NULL && be_isroot( op ) ) {
249 "access_allowed: conn %lu root access granted\n",
250 op->o_connid, 0, 0 );
252 Debug( LDAP_DEBUG_ACL,
253 "<= root access granted\n",
257 mask = ACL_LVL_WRITE;
264 * no-user-modification operational attributes are ignored
265 * by ACL_WRITE checking as any found here are not provided
268 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
269 && desc != slap_schema.si_ad_entry
270 && desc != slap_schema.si_ad_children )
273 LDAP_LOG( ACL, DETAIL1,
274 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
275 "access granted\n", op->o_connid, attr , 0 );
277 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
278 " %s access granted\n",
284 /* use backend default access if no backend acls */
285 if( be != NULL && be->be_acl == NULL ) {
287 LDAP_LOG( ACL, DETAIL1,
288 "access_allowed: backend default %s access %s to \"%s\"\n",
289 access2str( access ),
290 be->be_dfltaccess >= access ? "granted" : "denied",
291 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
293 Debug( LDAP_DEBUG_ACL,
294 "=> access_allowed: backend default %s access %s to \"%s\"\n",
295 access2str( access ),
296 be->be_dfltaccess >= access ? "granted" : "denied",
297 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
299 ret = be->be_dfltaccess >= access;
304 mask = ACL_PRIV_LEVEL;
305 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
306 mask |= ACL_ACCESS2PRIV( i );
313 /* be is always non-NULL */
314 /* use global default access if no global acls */
315 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
317 LDAP_LOG( ACL, DETAIL1,
318 "access_allowed: global default %s access %s to \"%s\"\n",
319 access2str( access ),
320 frontendDB->be_dfltaccess >= access ? "granted" : "denied",
323 Debug( LDAP_DEBUG_ACL,
324 "=> access_allowed: global default %s access %s to \"%s\"\n",
325 access2str( access ),
326 frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
328 ret = frontendDB->be_dfltaccess >= access;
333 mask = ACL_PRIV_LEVEL;
334 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
335 mask |= ACL_ACCESS2PRIV( i );
347 assert( state->as_vd_acl != NULL );
349 a = state->as_vd_acl;
350 count = state->as_vd_acl_count;
351 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
352 mask = state->as_vd_acl_mask;
353 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
358 if ( state ) state->as_vi_acl = NULL;
362 memset(matches, '\0', sizeof(matches));
365 while((a = acl_get( a, &count, op, e, desc, val,
366 MAXREMATCHES, matches, state )) != NULL)
370 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
372 LDAP_LOG( ACL, DETAIL1,
373 "access_allowed: match[%d]: %d %d ",
374 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
376 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
377 (int)matches[i].rm_so, (int)matches[i].rm_eo );
379 if( matches[i].rm_so <= matches[0].rm_eo ) {
381 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
382 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
386 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
388 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
393 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
394 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
395 ret = state->as_result;
398 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
403 control = acl_mask( a, &mask, op,
404 e, desc, val, matches, count, state );
406 if ( control != ACL_BREAK ) {
410 memset(matches, '\0', sizeof(matches));
413 if ( ACL_IS_INVALID( mask ) ) {
415 LDAP_LOG( ACL, DETAIL1,
416 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
417 op->o_connid, e->e_dn, attr );
419 Debug( LDAP_DEBUG_ACL,
420 "=> access_allowed: \"%s\" (%s) invalid!\n",
425 } else if ( control == ACL_BREAK ) {
427 LDAP_LOG( ACL, DETAIL1,
428 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
430 Debug( LDAP_DEBUG_ACL,
431 "=> access_allowed: no more rules\n", 0, 0, 0);
438 LDAP_LOG( ACL, ENTRY,
439 "access_allowed: %s access %s by %s\n",
440 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
441 accessmask2str( mask, accessmaskbuf ) );
443 Debug( LDAP_DEBUG_ACL,
444 "=> access_allowed: %s access %s by %s\n",
445 access2str( access ),
446 ACL_GRANT(mask, access) ? "granted" : "denied",
447 accessmask2str( mask, accessmaskbuf ) );
450 ret = ACL_GRANT(mask, access);
453 if( state != NULL ) {
454 /* If not value-dependent, save ACL in case of more attrs */
455 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
456 state->as_vi_acl = a;
457 state->as_result = ret;
459 state->as_recorded |= ACL_STATE_RECORDED;
461 if (be_null) op->o_bd = NULL;
462 if ( maskp ) *maskp = mask;
468 * acl_get - return the acl applicable to entry e, attribute
469 * attr. the acl returned is suitable for use in subsequent calls to
470 * acl_access_allowed().
473 static AccessControl *
479 AttributeDescription *desc,
483 AccessControlState *state )
490 assert( count != NULL );
491 assert( desc != NULL );
493 attr = desc->ad_cname.bv_val;
495 assert( attr != NULL );
498 if( op->o_bd == NULL ) {
499 a = frontendDB->be_acl;
501 a = op->o_bd->be_acl;
512 dnlen = e->e_nname.bv_len;
514 for ( ; a != NULL; a = a->acl_next ) {
517 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
518 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
520 LDAP_LOG( ACL, DETAIL1,
521 "acl_get: dnpat [%d] %s nsub: %d\n",
522 *count, a->acl_dn_pat.bv_val,
523 (int) a->acl_dn_re.re_nsub );
525 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
526 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
528 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
533 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
534 *count, a->acl_dn_pat.bv_val, 0 );
536 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
537 *count, a->acl_dn_pat.bv_val, 0 );
539 patlen = a->acl_dn_pat.bv_len;
540 if ( dnlen < patlen )
543 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
544 /* base dn -- entire object DN must match */
545 if ( dnlen != patlen )
548 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
549 int rdnlen = -1, sep = 0;
551 if ( dnlen <= patlen )
555 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
560 rdnlen = dn_rdnlen( NULL, &e->e_nname );
561 if ( rdnlen != dnlen - patlen - sep )
564 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
565 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
568 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
569 if ( dnlen <= patlen )
571 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
575 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
580 LDAP_LOG( ACL, DETAIL1,
581 "acl_get: [%d] matched\n", *count, 0, 0 );
583 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
588 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
589 matches[0].rm_so = matches[0].rm_eo = -1;
593 /* Is this ACL only for a specific value? */
594 if ( a->acl_attrval.bv_len ) {
599 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
600 state->as_recorded |= ACL_STATE_RECORDED_VD;
601 state->as_vd_acl = prev;
602 state->as_vd_acl_count = *count;
603 state->as_vd_access = a->acl_access;
604 state->as_vd_access_count = 1;
605 ACL_INVALIDATE( state->as_vd_acl_mask );
608 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
610 LDAP_LOG( ACL, DETAIL1,
611 "acl_get: valpat %s\n",
612 a->acl_attrval.bv_val, 0, 0 );
614 Debug( LDAP_DEBUG_ACL,
615 "acl_get: valpat %s\n",
616 a->acl_attrval.bv_val, 0, 0 );
618 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
624 LDAP_LOG( ACL, DETAIL1,
626 a->acl_attrval.bv_val, 0, 0 );
628 Debug( LDAP_DEBUG_ACL,
630 a->acl_attrval.bv_val, 0, 0 );
633 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
634 if (value_match( &match, desc,
635 desc->ad_type->sat_equality, 0,
636 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
643 patlen = a->acl_attrval.bv_len;
644 vdnlen = val->bv_len;
646 if ( vdnlen < patlen )
649 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
650 if ( vdnlen > patlen )
653 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
656 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
659 rdnlen = dn_rdnlen( NULL, val );
660 if ( rdnlen != vdnlen - patlen - 1 )
663 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
664 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
667 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
668 if ( vdnlen <= patlen )
671 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
675 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
681 if ( a->acl_filter != NULL ) {
682 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
683 if ( rc != LDAP_COMPARE_TRUE ) {
689 LDAP_LOG( ACL, DETAIL1,
690 "acl_get: [%d] attr %s\n", *count, attr ,0 );
692 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
699 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
701 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
707 * Record value-dependent access control state
709 #define ACL_RECORD_VALUE_STATE do { \
710 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
711 state->as_recorded |= ACL_STATE_RECORDED_VD; \
712 state->as_vd_acl = a; \
713 AC_MEMCPY( state->as_vd_acl_matches, matches, \
714 sizeof( state->as_vd_acl_matches )) ; \
715 state->as_vd_acl_count = count; \
716 state->as_vd_access = b; \
717 state->as_vd_access_count = i; \
722 * acl_mask - modifies mask based upon the given acl and the
723 * requested access to entry e, attribute attr, value val. if val
724 * is null, access to the whole attribute is assumed (all values).
726 * returns 0 access NOT allowed
730 static slap_control_t
736 AttributeDescription *desc,
740 AccessControlState *state )
742 int i, odnlen, patlen;
745 char accessmaskbuf[ACCESSMASK_MAXLEN];
746 char accessmaskbuf1[ACCESSMASK_MAXLEN];
751 assert( mask != NULL );
752 assert( desc != NULL );
754 attr = desc->ad_cname.bv_val;
756 assert( attr != NULL );
759 LDAP_LOG( ACL, ENTRY,
760 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
761 op->o_connid, e->e_dn, attr );
764 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
765 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
766 accessmask2str( *mask, accessmaskbuf ) );
768 Debug( LDAP_DEBUG_ACL,
769 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
772 Debug( LDAP_DEBUG_ACL,
773 "=> acl_mask: to %s by \"%s\", (%s) \n",
774 val ? "value" : "all values",
775 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
776 accessmask2str( *mask, accessmaskbuf ) );
780 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
781 && state->as_vd_acl == a )
783 b = state->as_vd_access;
784 i = state->as_vd_access_count;
791 for ( ; b != NULL; b = b->a_next, i++ ) {
792 slap_mask_t oldmask, modmask;
794 ACL_INVALIDATE( modmask );
796 /* AND <who> clauses */
797 if ( b->a_dn_pat.bv_len != 0 ) {
799 LDAP_LOG( ACL, DETAIL1,
800 "acl_mask: conn %lu check a_dn_pat: %s\n",
801 op->o_connid, b->a_dn_pat.bv_val ,0 );
803 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
804 b->a_dn_pat.bv_val, 0, 0);
807 * if access applies to the entry itself, and the
808 * user is bound as somebody in the same namespace as
809 * the entry, OR the given dn matches the dn pattern
811 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
812 if ( op->o_ndn.bv_len != 0 ) {
816 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
817 if ( op->o_ndn.bv_len == 0 ) {
821 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
822 if ( op->o_ndn.bv_len == 0 ) {
826 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
830 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
831 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
832 int ret = regex_matches( &b->a_dn_pat,
833 op->o_ndn.bv_val, e->e_ndn, matches );
844 if ( e->e_dn == NULL )
847 if ( b->a_dn_expand ) {
849 char buf[ACL_BUF_SIZE];
851 bv.bv_len = sizeof( buf ) - 1;
854 string_expand(&bv, &b->a_dn_pat,
856 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
857 /* did not expand to a valid dn */
865 odnlen = op->o_ndn.bv_len;
866 if ( odnlen < patlen ) {
867 goto dn_match_cleanup;
871 if ( b->a_dn_style == ACL_STYLE_BASE ) {
872 /* base dn -- entire object DN must match */
873 if ( odnlen != patlen ) {
874 goto dn_match_cleanup;
877 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
880 if ( odnlen <= patlen ) {
881 goto dn_match_cleanup;
884 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
885 goto dn_match_cleanup;
888 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
889 if ( rdnlen != odnlen - patlen - 1 ) {
890 goto dn_match_cleanup;
893 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
894 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
895 goto dn_match_cleanup;
898 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
899 if ( odnlen <= patlen ) {
900 goto dn_match_cleanup;
903 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
904 goto dn_match_cleanup;
908 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
911 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
921 if ( b->a_sockurl_pat.bv_len ) {
922 if ( ! op->o_conn->c_listener ) {
926 LDAP_LOG( ACL, DETAIL1,
927 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
928 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
930 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
931 b->a_sockurl_pat.bv_val, 0, 0 );
934 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
935 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
936 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
937 e->e_ndn, matches ) )
942 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
944 char buf[ACL_BUF_SIZE];
946 bv.bv_len = sizeof( buf ) - 1;
948 string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, matches );
950 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 ) {
955 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
961 if ( b->a_domain_pat.bv_len ) {
962 if ( !op->o_conn->c_peer_domain.bv_val ) {
966 LDAP_LOG( ACL, DETAIL1,
967 "acl_mask: conn %lu check a_domain_pat: %s\n",
968 op->o_connid, b->a_domain_pat.bv_val , 0 );
970 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
971 b->a_domain_pat.bv_val, 0, 0 );
973 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
974 if ( b->a_domain_style == ACL_STYLE_REGEX) {
975 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
976 e->e_ndn, matches ) )
981 char buf[ACL_BUF_SIZE];
983 struct berval cmp = op->o_conn->c_peer_domain;
984 struct berval pat = b->a_domain_pat;
986 if ( b->a_domain_expand ) {
989 bv.bv_len = sizeof(buf) - 1;
992 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
996 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
997 int offset = cmp.bv_len - pat.bv_len;
1002 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1006 /* trim the domain */
1007 cmp.bv_val = &cmp.bv_val[ offset ];
1008 cmp.bv_len -= offset;
1011 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1018 if ( b->a_peername_pat.bv_len ) {
1019 if ( !op->o_conn->c_peer_name.bv_val ) {
1023 LDAP_LOG( ACL, DETAIL1,
1024 "acl_mask: conn %lu check a_peername_path: %s\n",
1025 op->o_connid, b->a_peername_pat.bv_val , 0 );
1027 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1028 b->a_peername_pat.bv_val, 0, 0 );
1030 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1031 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1032 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1033 e->e_ndn, matches ) )
1039 /* try exact match */
1040 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1041 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1045 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1047 char buf[ACL_BUF_SIZE];
1049 bv.bv_len = sizeof( buf ) - 1;
1051 string_expand( &bv, &b->a_peername_pat, e->e_ndn, matches );
1053 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1057 /* extract IP and try exact match */
1058 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1060 char buf[] = "255.255.255.255";
1063 int port_number = -1;
1065 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1066 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1069 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1070 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1072 port = strrchr( ip.bv_val, ':' );
1076 ip.bv_len = port - ip.bv_val;
1078 port_number = strtol( port, &next, 10 );
1079 if ( next[0] != '\0' )
1083 /* the port check can be anticipated here */
1084 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1087 /* address longer than expected? */
1088 if ( ip.bv_len >= sizeof(buf) )
1091 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1092 buf[ ip.bv_len ] = '\0';
1094 addr = inet_addr( buf );
1096 /* unable to convert? */
1097 if ( addr == (unsigned long)(-1) )
1100 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1103 #ifdef LDAP_PF_LOCAL
1104 /* extract path and try exact match */
1105 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1108 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1109 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1112 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1113 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1115 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1118 #endif /* LDAP_PF_LOCAL */
1120 /* exact match (very unlikely...) */
1121 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1128 if ( b->a_sockname_pat.bv_len ) {
1129 if ( !op->o_conn->c_sock_name.bv_val ) {
1133 LDAP_LOG( ACL, DETAIL1,
1134 "acl_mask: conn %lu check a_sockname_path: %s\n",
1135 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1137 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1138 b->a_sockname_pat.bv_val, 0, 0 );
1140 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1141 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1142 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1143 e->e_ndn, matches ) )
1148 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1150 char buf[ACL_BUF_SIZE];
1152 bv.bv_len = sizeof( buf ) - 1;
1154 string_expand( &bv, &b->a_sockname_pat, e->e_ndn, matches );
1156 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1161 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1167 if ( b->a_dn_at != NULL ) {
1172 const char *attr = b->a_dn_at->ad_cname.bv_val;
1174 assert( attr != NULL );
1176 if ( op->o_ndn.bv_len == 0 ) {
1181 LDAP_LOG( ACL, DETAIL1,
1182 "acl_mask: conn %lu check a_dn_pat: %s\n",
1183 op->o_connid, attr , 0 );
1185 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1190 /* see if asker is listed in dnattr */
1191 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1193 at = attrs_find( at->a_next, b->a_dn_at ) )
1195 if( value_find_ex( b->a_dn_at,
1196 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1197 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1199 &bv, op->o_tmpmemctx ) == 0 )
1208 /* have a dnattr match. if this is a self clause then
1209 * the target must also match the op dn.
1211 if ( b->a_dn_self ) {
1212 /* check if the target is an attribute. */
1213 if ( val == NULL ) continue;
1215 /* target is attribute, check if the attribute value
1218 rc = value_match( &match, b->a_dn_at,
1219 b->a_dn_at->ad_type->sat_equality, 0,
1221 /* on match error or no match, fail the ACL clause */
1222 if (rc != LDAP_SUCCESS || match != 0 )
1226 /* no dnattr match, check if this is a self clause */
1227 if ( ! b->a_dn_self )
1230 ACL_RECORD_VALUE_STATE;
1232 /* this is a self clause, check if the target is an
1238 /* target is attribute, check if the attribute value
1241 rc = value_match( &match, b->a_dn_at,
1242 b->a_dn_at->ad_type->sat_equality, 0,
1245 /* on match error or no match, fail the ACL clause */
1246 if (rc != LDAP_SUCCESS || match != 0 )
1251 if ( b->a_group_pat.bv_len ) {
1253 struct berval ndn = BER_BVNULL;
1256 if ( op->o_ndn.bv_len == 0 ) {
1260 /* b->a_group is an unexpanded entry name, expanded it should be an
1261 * entry with objectclass group* and we test to see if odn is one of
1262 * the values in the attribute group
1264 /* see if asker is listed in dnattr */
1265 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1266 char buf[ACL_BUF_SIZE];
1267 bv.bv_len = sizeof(buf) - 1;
1270 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1271 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1272 /* did not expand to a valid dn */
1279 bv = b->a_group_pat;
1282 rc = backend_group( op, e, &bv, &op->o_ndn,
1283 b->a_group_oc, b->a_group_at );
1285 if ( ndn.bv_val ) free( ndn.bv_val );
1292 if ( b->a_set_pat.bv_len != 0 ) {
1294 char buf[ACL_BUF_SIZE];
1295 if( b->a_set_style == ACL_STYLE_REGEX ){
1296 bv.bv_len = sizeof(buf) - 1;
1298 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1302 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1307 if ( b->a_authz.sai_ssf ) {
1309 LDAP_LOG( ACL, DETAIL1,
1310 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1311 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1313 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1314 b->a_authz.sai_ssf, op->o_ssf, 0 );
1316 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1321 if ( b->a_authz.sai_transport_ssf ) {
1323 LDAP_LOG( ACL, DETAIL1,
1324 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1326 op->o_connid, b->a_authz.sai_transport_ssf,
1327 op->o_transport_ssf );
1329 Debug( LDAP_DEBUG_ACL,
1330 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1331 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1333 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1338 if ( b->a_authz.sai_tls_ssf ) {
1340 LDAP_LOG( ACL, DETAIL1,
1341 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1343 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1345 Debug( LDAP_DEBUG_ACL,
1346 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1347 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1349 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1354 if ( b->a_authz.sai_sasl_ssf ) {
1356 LDAP_LOG( ACL, DETAIL1,
1357 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1359 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1361 Debug( LDAP_DEBUG_ACL,
1362 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1363 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1365 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1370 #ifdef SLAPD_ACI_ENABLED
1371 if ( b->a_aci_at != NULL ) {
1373 slap_access_t grant, deny, tgrant, tdeny;
1374 struct berval parent_ndn, old_parent_ndn;
1375 BerVarray bvals = NULL;
1378 /* this case works different from the others above.
1379 * since aci's themselves give permissions, we need
1380 * to first check b->a_access_mask, the ACL's access level.
1383 if ( e->e_nname.bv_len == 0 ) {
1384 /* no ACIs in the root DSE */
1388 /* first check if the right being requested
1389 * is allowed by the ACL clause.
1391 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1394 /* start out with nothing granted, nothing denied */
1398 /* get the aci attribute */
1399 at = attr_find( e->e_attrs, b->a_aci_at );
1402 /* FIXME: this breaks acl caching;
1403 * see also ACL_RECORD_VALUE_STATE below */
1404 ACL_RECORD_VALUE_STATE;
1406 /* the aci is an multi-valued attribute. The
1407 * rights are determined by OR'ing the individual
1408 * rights given by the acis.
1410 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1414 matches, &grant, &deny, &aci_bv_entry ) != 0)
1420 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1421 accessmask2str(tgrant,accessmaskbuf),
1422 accessmask2str(tdeny, accessmaskbuf1), 0);
1425 /* If the entry level aci didn't contain anything valid for the
1426 * current operation, climb up the tree and evaluate the
1427 * acis with scope set to subtree
1429 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1430 dnParent(&(e->e_nname), &parent_ndn);
1431 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1432 old_parent_ndn = parent_ndn;
1433 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1434 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1442 for( i = 0; bvals[i].bv_val != NULL; i++){
1444 /* FIXME: this breaks acl caching;
1445 * see also ACL_RECORD_VALUE_STATE above */
1446 ACL_RECORD_VALUE_STATE;
1448 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1449 &grant, &deny, &aci_bv_children) != 0) {
1452 /* evaluation stops as soon as either a "deny" or a
1453 * "grant" directive matches.
1455 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1459 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1460 accessmask2str(tgrant,accessmaskbuf),
1461 accessmask2str(tdeny, accessmaskbuf1), 0);
1465 case LDAP_NO_SUCH_ATTRIBUTE:
1466 /* just go on if the aci-Attribute is not present in
1469 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1473 case LDAP_NO_SUCH_OBJECT:
1474 /* We have reached the base object */
1475 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1486 dnParent(&old_parent_ndn, &parent_ndn);
1491 /* remove anything that the ACL clause does not allow */
1492 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1493 tdeny &= ACL_PRIV_MASK;
1495 /* see if we have anything to contribute */
1496 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1500 /* this could be improved by changing acl_mask so that it can deal with
1501 * by clauses that return grant/deny pairs. Right now, it does either
1502 * additive or subtractive rights, but not both at the same time. So,
1503 * we need to combine the grant/deny pair into a single rights mask in
1504 * a smart way: if either grant or deny is "empty", then we use the
1505 * opposite as is, otherwise we remove any denied rights from the grant
1506 * rights mask and construct an additive mask.
1508 if (ACL_IS_INVALID(tdeny)) {
1509 modmask = tgrant | ACL_PRIV_ADDITIVE;
1511 } else if (ACL_IS_INVALID(tgrant)) {
1512 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1515 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1521 modmask = b->a_access_mask;
1525 LDAP_LOG( ACL, RESULTS,
1526 "acl_mask: [%d] applying %s (%s)\n",
1527 i, accessmask2str( modmask, accessmaskbuf),
1528 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1529 ? "break" : "stop" );
1531 Debug( LDAP_DEBUG_ACL,
1532 "<= acl_mask: [%d] applying %s (%s)\n",
1533 i, accessmask2str( modmask, accessmaskbuf ),
1534 b->a_type == ACL_CONTINUE
1536 : b->a_type == ACL_BREAK
1543 if( ACL_IS_ADDITIVE(modmask) ) {
1545 ACL_PRIV_SET( *mask, modmask );
1548 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1550 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1551 /* substract privs */
1552 ACL_PRIV_CLR( *mask, modmask );
1555 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1563 LDAP_LOG( ACL, DETAIL1,
1564 "acl_mask: conn %lu [%d] mask: %s\n",
1565 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1567 Debug( LDAP_DEBUG_ACL,
1568 "<= acl_mask: [%d] mask: %s\n",
1569 i, accessmask2str(*mask, accessmaskbuf), 0 );
1572 if( b->a_type == ACL_CONTINUE ) {
1575 } else if ( b->a_type == ACL_BREAK ) {
1583 /* implicit "by * none" clause */
1587 LDAP_LOG( ACL, RESULTS,
1588 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1589 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1591 Debug( LDAP_DEBUG_ACL,
1592 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1593 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1599 * acl_check_modlist - check access control on the given entry to see if
1600 * it allows the given modifications by the user associated with op.
1601 * returns 1 if mods allowed ok
1602 * 0 mods not allowed
1609 Modifications *mlist
1613 AccessControlState state = ACL_STATE_INIT;
1616 int ret = 1; /* default is access allowed */
1624 assert( be != NULL );
1626 /* short circuit root database access */
1627 if ( be_isroot( op ) ) {
1629 LDAP_LOG( ACL, DETAIL1,
1630 "acl_check_modlist: conn %lu access granted to root user\n",
1631 op->o_connid, 0, 0 );
1633 Debug( LDAP_DEBUG_ACL,
1634 "<= acl_access_allowed: granted to database root\n",
1640 /* use backend default access if no backend acls */
1641 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1643 LDAP_LOG( ACL, DETAIL1,
1644 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1645 access2str( ACL_WRITE ),
1646 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1649 Debug( LDAP_DEBUG_ACL,
1650 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1651 access2str( ACL_WRITE ),
1652 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1654 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1658 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1660 * no-user-modification operational attributes are ignored
1661 * by ACL_WRITE checking as any found here are not provided
1664 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1666 LDAP_LOG( ACL, DETAIL1,
1667 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1668 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1670 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1671 " modify access granted\n",
1672 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1677 switch ( mlist->sml_op ) {
1678 case LDAP_MOD_REPLACE:
1680 * We must check both permission to delete the whole
1681 * attribute and permission to add the specific attributes.
1682 * This prevents abuse from selfwriters.
1684 if ( ! access_allowed( op, e,
1685 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1691 if ( mlist->sml_values == NULL ) break;
1693 /* fall thru to check value to add */
1696 assert( mlist->sml_values != NULL );
1698 for ( bv = mlist->sml_nvalues
1699 ? mlist->sml_nvalues : mlist->sml_values;
1700 bv->bv_val != NULL; bv++ )
1702 if ( ! access_allowed( op, e,
1703 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1711 case LDAP_MOD_DELETE:
1712 if ( mlist->sml_values == NULL ) {
1713 if ( ! access_allowed( op, e,
1714 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1721 for ( bv = mlist->sml_nvalues
1722 ? mlist->sml_nvalues : mlist->sml_values;
1723 bv->bv_val != NULL; bv++ )
1725 if ( ! access_allowed( op, e,
1726 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1734 case SLAP_MOD_SOFTADD:
1735 /* allow adding attribute via modrdn thru */
1747 if (be_null) op->o_bd = NULL;
1753 struct berval *list,
1766 while (len >= 0 && --ix >= 0) {
1767 while (--len >= 0 && *p++ != sep) ;
1769 while (len >= 0 && *p == ' ') {
1780 while (--len >= 0 && *p != sep) {
1784 while (bv->bv_len > 0 && *--p == ' ')
1790 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1792 AciSetCookie *cp = (AciSetCookie *)cookie;
1793 BerVarray bvals = NULL;
1796 /* this routine needs to return the bervals instead of
1797 * plain strings, since syntax is not known. It should
1798 * also return the syntax or some "comparison cookie".
1801 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1803 AttributeDescription *desc = NULL;
1804 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1805 backend_attribute(cp->op,
1806 cp->e, &ndn, desc, &bvals, ACL_NONE);
1808 slap_sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1815 struct berval *subj,
1821 struct berval set = BER_BVNULL;
1823 AciSetCookie cookie;
1826 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1828 struct berval subjdn, ndn = BER_BVNULL;
1829 struct berval setat;
1832 AttributeDescription *desc = NULL;
1834 /* format of string is "entry/setAttrName" */
1835 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
1839 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
1840 setat = aci_bv_set_attr;
1844 * NOTE: dnNormalize honors the ber_len field
1845 * as the length of the dn to be normalized
1847 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
1848 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
1850 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
1851 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
1855 BER_BVZERO( &bvals[0] );
1856 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
1858 bvals[0].bv_val = bvals[i-1].bv_val;
1859 BER_BVZERO( &bvals[i-1] );
1861 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
1862 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1867 if ( !BER_BVISNULL( &set ) ) {
1870 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
1871 &op->o_ndn, &e->e_nname, NULL ) > 0 );
1872 slap_sl_free( set.bv_val, op->o_tmpmemctx );
1878 #ifdef SLAPD_ACI_ENABLED
1880 aci_list_map_rights(
1881 struct berval *list )
1888 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1891 switch (*bv.bv_val) {
1893 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1896 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1897 * the right 's' to mean "set", but in the examples states
1898 * that the right 's' means "search". The latter definition
1901 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1904 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1907 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1910 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1911 * define any equivalent to the AUTH right, so I've just used
1914 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1926 struct berval *list,
1927 const struct berval *attr,
1928 struct berval *val )
1930 struct berval bv, left, right;
1933 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1934 if (aci_get_part(&bv, 0, '=', &left) < 0
1935 || aci_get_part(&bv, 1, '=', &right) < 0)
1937 if (ber_bvstrcasecmp(attr, &bv) == 0)
1939 } else if (val == NULL) {
1940 if (ber_bvstrcasecmp(attr, &left) == 0)
1943 if (ber_bvstrcasecmp(attr, &left) == 0) {
1944 /* this is experimental code that implements a
1945 * simple (prefix) match of the attribute value.
1946 * the ACI draft does not provide for aci's that
1947 * apply to specific values, but it would be
1948 * nice to have. If the <attr> part of an aci's
1949 * rights list is of the form <attr>=<value>,
1950 * that means the aci applies only to attrs with
1951 * the given value. Furthermore, if the attr is
1952 * of the form <attr>=<value>*, then <value> is
1953 * treated as a prefix, and the aci applies to
1954 * any value with that prefix.
1956 * Ideally, this would allow r.e. matches.
1958 if (aci_get_part(&right, 0, '*', &left) < 0
1959 || right.bv_len <= left.bv_len)
1961 if (ber_bvstrcasecmp(val, &right) == 0)
1963 } else if (val->bv_len >= left.bv_len) {
1964 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1973 static slap_access_t
1974 aci_list_get_attr_rights(
1975 struct berval *list,
1976 const struct berval *attr,
1977 struct berval *val )
1983 /* loop through each rights/attr pair, skip first part (action) */
1985 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1986 if (aci_list_has_attr(&bv, attr, val) == 0)
1988 if (aci_get_part(list, i, ';', &bv) < 0)
1990 mask |= aci_list_map_rights(&bv);
1996 aci_list_get_rights(
1997 struct berval *list,
1998 const struct berval *attr,
2000 slap_access_t *grant,
2001 slap_access_t *deny )
2003 struct berval perm, actn;
2004 slap_access_t *mask;
2007 if (attr == NULL || attr->bv_len == 0
2008 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2009 attr = &aci_bv_br_entry;
2015 /* loop through each permissions clause */
2016 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2017 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2019 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2021 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2028 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2029 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2036 struct berval *subj,
2037 struct berval *defgrpoc,
2038 struct berval *defgrpat,
2044 struct berval subjdn;
2045 struct berval grpoc;
2046 struct berval grpat;
2047 ObjectClass *grp_oc = NULL;
2048 AttributeDescription *grp_ad = NULL;
2052 /* format of string is "group/objectClassValue/groupAttrName" */
2053 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2057 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2061 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2065 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2066 if( rc != LDAP_SUCCESS ) {
2072 grp_oc = oc_bvfind( &grpoc );
2074 if (grp_oc != NULL && grp_ad != NULL ) {
2075 char buf[ACL_BUF_SIZE];
2076 struct berval bv, ndn;
2077 bv.bv_len = sizeof( buf ) - 1;
2078 bv.bv_val = (char *)&buf;
2079 string_expand(&bv, &subjdn, e->e_ndn, matches);
2080 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2081 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2082 grp_oc, grp_ad) == 0);
2095 AttributeDescription *desc,
2098 regmatch_t *matches,
2099 slap_access_t *grant,
2100 slap_access_t *deny,
2101 struct berval *scope
2104 struct berval bv, perms, sdn;
2108 assert( desc->ad_cname.bv_val != NULL );
2110 /* parse an aci of the form:
2111 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2113 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2114 a full description of the format for this attribute.
2115 Differences: "this" in the draft is "self" here, and
2116 "self" and "public" is in the position of dnType.
2118 For now, this routine only supports scope=entry.
2120 /* check that the aci has all 5 components */
2121 if (aci_get_part(aci, 4, '#', NULL) < 0)
2124 /* check that the aci family is supported */
2125 if (aci_get_part(aci, 0, '#', &bv) < 0)
2128 /* check that the scope matches */
2129 if (aci_get_part(aci, 1, '#', &bv) < 0
2130 || ber_bvstrcasecmp( scope, &bv ) != 0)
2135 /* get the list of permissions clauses, bail if empty */
2136 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2139 /* check if any permissions allow desired access */
2140 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2143 /* see if we have a DN match */
2144 if (aci_get_part(aci, 3, '#', &bv) < 0)
2147 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2150 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2153 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2154 if (dn_match( &op->o_ndn, &ndn))
2160 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2163 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2164 if (dn_match(&op->o_ndn, &e->e_nname))
2167 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2169 AttributeDescription *ad = NULL;
2172 rc = slap_bv2ad( &sdn, &ad, &text );
2174 if( rc != LDAP_SUCCESS ) {
2182 for(at = attrs_find( e->e_attrs, ad );
2184 at = attrs_find( at->a_next, ad ) )
2186 if (value_find_ex( ad,
2187 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2188 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2190 &bv, op->o_tmpmemctx) == 0 )
2200 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2201 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2204 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2205 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2208 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2209 if (aci_match_set(&sdn, op, e, 0))
2212 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2213 if (aci_match_set(&sdn, op, e, 1))
2221 #endif /* SLAPD_ACI_ENABLED */
2228 regmatch_t *matches)
2236 bv->bv_val[0] = '\0';
2237 bv->bv_len--; /* leave space for lone $ */
2240 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2241 sp < pat->bv_val + pat->bv_len ; sp++ )
2243 /* did we previously see a $ */
2245 if ( flag == 1 && *sp == '$' ) {
2250 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2253 } else if ( *sp >= '0' && *sp <= '9' ) {
2261 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2262 if ( *sp >= '0' && *sp <= '9' ) {
2263 n = 10*n + ( *sp - '0' );
2267 if ( *sp != /*'{'*/ '}' ) {
2272 if ( n >= MAXREMATCHES ) {
2277 i = matches[n].rm_so;
2278 l = matches[n].rm_eo;
2279 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2297 /* must have ended with a single $ */
2306 LDAP_LOG( ACL, DETAIL1,
2307 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2308 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2310 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2311 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2317 struct berval *pat, /* pattern to expand and match against */
2318 char *str, /* string to match against pattern */
2319 char *buf, /* buffer with $N expansion variables */
2320 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2324 char newbuf[ACL_BUF_SIZE];
2328 bv.bv_len = sizeof(newbuf) - 1;
2331 if(str == NULL) str = "";
2333 string_expand(&bv, pat, buf, matches);
2334 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2335 char error[ACL_BUF_SIZE];
2336 regerror(rc, &re, error, sizeof(error));
2340 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2341 pat->bv_val, str, error );
2343 Debug( LDAP_DEBUG_TRACE,
2344 "compile( \"%s\", \"%s\") failed %s\n",
2345 pat->bv_val, str, error );
2350 rc = regexec(&re, str, 0, NULL, 0);
2354 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2355 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2356 rc, rc ? "matches" : "no matches", 0 );
2358 Debug( LDAP_DEBUG_TRACE,
2359 "=> regex_matches: string: %s\n", str, 0, 0 );
2360 Debug( LDAP_DEBUG_TRACE,
2361 "=> regex_matches: rc: %d %s\n",
2362 rc, !rc ? "matches" : "no matches", 0 );