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_creator = BER_BVC("creator"),
59 aci_bv_dnattr = BER_BVC("dnattr"),
60 aci_bv_group = BER_BVC("group"),
61 aci_bv_role = BER_BVC("role"),
62 aci_bv_set = BER_BVC("set"),
63 aci_bv_set_ref = BER_BVC("set-ref"),
64 aci_bv_grant = BER_BVC("grant"),
65 aci_bv_deny = BER_BVC("deny"),
67 aci_bv_ip_eq = BER_BVC("IP="),
69 aci_bv_path_eq = BER_BVC("PATH="),
70 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
71 #endif /* LDAP_PF_LOCAL */
73 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
74 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
75 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
76 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR),
77 aci_bv_set_attr = BER_BVC(SLAPD_ACI_SET_ATTR);
80 static AccessControl * acl_get(
81 AccessControl *ac, int *count,
82 Operation *op, Entry *e,
83 AttributeDescription *desc,
85 int nmatch, regmatch_t *matches,
86 AccessControlState *state );
88 static slap_control_t acl_mask(
89 AccessControl *ac, slap_mask_t *mask,
90 Operation *op, Entry *e,
91 AttributeDescription *desc,
96 AccessControlState *state );
98 #ifdef SLAPD_ACI_ENABLED
100 Operation *op, Entry *e,
101 AttributeDescription *desc,
106 slap_access_t *grant,
108 struct berval *scope);
111 static int regex_matches(
112 struct berval *pat, char *str, char *buf,
113 int nmatch, regmatch_t *matches);
114 static int string_expand(
115 struct berval *newbuf, struct berval *pattern,
116 char *match, int nmatch, regmatch_t *matches);
118 typedef struct AciSetCookie {
123 SLAP_SET_GATHER aci_set_gather;
124 SLAP_SET_GATHER aci_set_gather2;
125 static int aci_match_set ( struct berval *subj, Operation *op,
126 Entry *e, int setref );
129 * access_allowed - check whether op->o_ndn is allowed the requested access
130 * to entry e, attribute attr, value val. if val is null, access to
131 * the whole attribute is assumed (all values).
133 * This routine loops through all access controls and calls
134 * acl_mask() on each applicable access control.
135 * The loop exits when a definitive answer is reached or
136 * or no more controls remain.
143 * - can be legally called with op == NULL
144 * - can be legally called with op->o_bd == NULL
151 AttributeDescription *desc,
153 slap_access_t access,
154 AccessControlState *state,
159 AccessControl *a = NULL;
164 char accessmaskbuf[ACCESSMASK_MAXLEN];
167 slap_control_t control;
169 regmatch_t matches[MAXREMATCHES];
170 int st_same_attr = 0;
171 static AccessControlState state_init = ACL_STATE_INIT;
174 assert( desc != NULL );
175 assert( access > ACL_NONE );
176 if ( maskp ) ACL_INVALIDATE( *maskp );
178 attr = desc->ad_cname.bv_val;
180 assert( attr != NULL );
182 if( op && op->o_is_auth_check &&
183 ( access == ACL_SEARCH || access == ACL_READ ))
189 if ( state->as_vd_ad==desc) {
190 if ( state->as_recorded ) {
191 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
194 return state->as_result;
195 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
196 val != NULL && state->as_vd_acl == NULL )
198 return state->as_result;
206 state->as_vd_ad=desc;
209 Debug( LDAP_DEBUG_ACL,
210 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
211 access2str( access ), e->e_dn, attr );
224 * FIXME: experimental; use first backend rules
225 * iff there is no global_acl (ITS#3100) */
226 if ( frontendDB->be_acl == NULL )
232 assert( be != NULL );
235 if ( op->o_pb != NULL ) {
236 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
238 /* ACL plugin denied access */
242 #endif /* LDAP_SLAPI */
244 /* grant database root access */
245 if ( be != NULL && be_isroot( op ) ) {
246 Debug( LDAP_DEBUG_ACL,
247 "<= root access granted\n",
250 mask = ACL_LVL_WRITE;
257 * no-user-modification operational attributes are ignored
258 * by ACL_WRITE checking as any found here are not provided
261 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
262 && desc != slap_schema.si_ad_entry
263 && desc != slap_schema.si_ad_children )
265 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
266 " %s access granted\n",
271 /* use backend default access if no backend acls */
272 if( be != NULL && be->be_acl == NULL ) {
273 Debug( LDAP_DEBUG_ACL,
274 "=> access_allowed: backend default %s access %s to \"%s\"\n",
275 access2str( access ),
276 be->be_dfltaccess >= access ? "granted" : "denied",
277 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
278 ret = be->be_dfltaccess >= access;
283 mask = ACL_PRIV_LEVEL;
284 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
285 mask |= ACL_ACCESS2PRIV( i );
292 /* be is always non-NULL */
293 /* use global default access if no global acls */
294 } else if ( be == NULL && frontendDB->be_acl == NULL ) {
295 Debug( LDAP_DEBUG_ACL,
296 "=> access_allowed: global default %s access %s to \"%s\"\n",
297 access2str( access ),
298 frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
299 ret = frontendDB->be_dfltaccess >= access;
304 mask = ACL_PRIV_LEVEL;
305 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
306 mask |= ACL_ACCESS2PRIV( i );
318 assert( state->as_vd_acl != NULL );
320 a = state->as_vd_acl;
321 count = state->as_vd_acl_count;
322 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
323 mask = state->as_vd_acl_mask;
324 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
329 if ( state ) state->as_vi_acl = NULL;
333 memset(matches, '\0', sizeof(matches));
336 while((a = acl_get( a, &count, op, e, desc, val,
337 MAXREMATCHES, matches, state )) != NULL)
341 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
342 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
343 (int)matches[i].rm_so, (int)matches[i].rm_eo );
344 if( matches[i].rm_so <= matches[0].rm_eo ) {
346 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
347 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
350 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
354 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
355 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
356 ret = state->as_result;
359 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
364 control = acl_mask( a, &mask, op,
365 e, desc, val, MAXREMATCHES, matches, count, state );
367 if ( control != ACL_BREAK ) {
371 memset(matches, '\0', sizeof(matches));
374 if ( ACL_IS_INVALID( mask ) ) {
375 Debug( LDAP_DEBUG_ACL,
376 "=> access_allowed: \"%s\" (%s) invalid!\n",
380 } else if ( control == ACL_BREAK ) {
381 Debug( LDAP_DEBUG_ACL,
382 "=> access_allowed: no more rules\n", 0, 0, 0);
387 Debug( LDAP_DEBUG_ACL,
388 "=> access_allowed: %s access %s by %s\n",
389 access2str( access ),
390 ACL_GRANT(mask, access) ? "granted" : "denied",
391 accessmask2str( mask, accessmaskbuf ) );
393 ret = ACL_GRANT(mask, access);
396 if( state != NULL ) {
397 /* If not value-dependent, save ACL in case of more attrs */
398 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
399 state->as_vi_acl = a;
400 state->as_result = ret;
402 state->as_recorded |= ACL_STATE_RECORDED;
404 if (be_null) op->o_bd = NULL;
405 if ( maskp ) *maskp = mask;
411 * acl_get - return the acl applicable to entry e, attribute
412 * attr. the acl returned is suitable for use in subsequent calls to
413 * acl_access_allowed().
416 static AccessControl *
422 AttributeDescription *desc,
426 AccessControlState *state )
433 assert( count != NULL );
434 assert( desc != NULL );
436 attr = desc->ad_cname.bv_val;
438 assert( attr != NULL );
441 if( op->o_bd == NULL ) {
442 a = frontendDB->be_acl;
444 a = op->o_bd->be_acl;
455 dnlen = e->e_nname.bv_len;
457 for ( ; a != NULL; a = a->acl_next ) {
460 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
461 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
462 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
463 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
464 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
468 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
469 *count, a->acl_dn_pat.bv_val, 0 );
470 patlen = a->acl_dn_pat.bv_len;
471 if ( dnlen < patlen )
474 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
475 /* base dn -- entire object DN must match */
476 if ( dnlen != patlen )
479 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
480 int rdnlen = -1, sep = 0;
482 if ( dnlen <= patlen )
486 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
491 rdnlen = dn_rdnlen( NULL, &e->e_nname );
492 if ( rdnlen != dnlen - patlen - sep )
495 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
496 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
499 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
500 if ( dnlen <= patlen )
502 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
506 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
510 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
514 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
515 matches[0].rm_so = matches[0].rm_eo = -1;
519 /* Is this ACL only for a specific value? */
520 if ( a->acl_attrval.bv_len ) {
525 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
526 state->as_recorded |= ACL_STATE_RECORDED_VD;
527 state->as_vd_acl = prev;
528 state->as_vd_acl_count = *count;
529 state->as_vd_access = a->acl_access;
530 state->as_vd_access_count = 1;
531 ACL_INVALIDATE( state->as_vd_acl_mask );
534 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
535 Debug( LDAP_DEBUG_ACL,
536 "acl_get: valpat %s\n",
537 a->acl_attrval.bv_val, 0, 0 );
538 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
543 Debug( LDAP_DEBUG_ACL,
545 a->acl_attrval.bv_val, 0, 0 );
547 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
548 if (value_match( &match, desc,
549 desc->ad_type->sat_equality, 0,
550 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
557 patlen = a->acl_attrval.bv_len;
558 vdnlen = val->bv_len;
560 if ( vdnlen < patlen )
563 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
564 if ( vdnlen > patlen )
567 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
570 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
573 rdnlen = dn_rdnlen( NULL, val );
574 if ( rdnlen != vdnlen - patlen - 1 )
577 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
578 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
581 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
582 if ( vdnlen <= patlen )
585 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
589 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
595 if ( a->acl_filter != NULL ) {
596 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
597 if ( rc != LDAP_COMPARE_TRUE ) {
602 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
607 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
612 * Record value-dependent access control state
614 #define ACL_RECORD_VALUE_STATE do { \
615 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
616 state->as_recorded |= ACL_STATE_RECORDED_VD; \
617 state->as_vd_acl = a; \
618 AC_MEMCPY( state->as_vd_acl_matches, matches, \
619 sizeof( state->as_vd_acl_matches )) ; \
620 state->as_vd_acl_count = count; \
621 state->as_vd_access = b; \
622 state->as_vd_access_count = i; \
627 * acl_mask - modifies mask based upon the given acl and the
628 * requested access to entry e, attribute attr, value val. if val
629 * is null, access to the whole attribute is assumed (all values).
631 * returns 0 access NOT allowed
635 static slap_control_t
641 AttributeDescription *desc,
646 AccessControlState *state )
648 int i, odnlen, patlen;
651 char accessmaskbuf[ACCESSMASK_MAXLEN];
652 char accessmaskbuf1[ACCESSMASK_MAXLEN];
657 assert( mask != NULL );
658 assert( desc != NULL );
660 attr = desc->ad_cname.bv_val;
662 assert( attr != NULL );
664 Debug( LDAP_DEBUG_ACL,
665 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
668 Debug( LDAP_DEBUG_ACL,
669 "=> acl_mask: to %s by \"%s\", (%s) \n",
670 val ? "value" : "all values",
671 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
672 accessmask2str( *mask, accessmaskbuf ) );
675 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
676 && state->as_vd_acl == a )
678 b = state->as_vd_access;
679 i = state->as_vd_access_count;
686 for ( ; b != NULL; b = b->a_next, i++ ) {
687 slap_mask_t oldmask, modmask;
689 ACL_INVALIDATE( modmask );
691 /* AND <who> clauses */
692 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
693 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
694 b->a_dn_pat.bv_val, 0, 0);
696 * if access applies to the entry itself, and the
697 * user is bound as somebody in the same namespace as
698 * the entry, OR the given dn matches the dn pattern
701 * NOTE: styles "anonymous", "users", "self"
702 * and "creator" have been moved to an enumeration,
703 * whose value is set in a_dn_style; however,
704 * the string is maintaned in a_dn_pat.
706 if ( b->a_dn_style == ACL_STYLE_ANONYMOUS /* bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) */ ) {
707 if ( op->o_ndn.bv_len != 0 ) {
711 } else if ( b->a_dn_style == ACL_STYLE_USERS /* bvmatch( &b->a_dn_pat, &aci_bv_users ) */ ) {
712 if ( op->o_ndn.bv_len == 0 ) {
716 } else if ( b->a_dn_style == ACL_STYLE_SELF /* bvmatch( &b->a_dn_pat, &aci_bv_self ) */ ) {
717 if ( op->o_ndn.bv_len == 0 ) {
721 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
725 } else if ( b->a_dn_style == ACL_STYLE_CREATOR /* bvmatch ( &b->a_dn_pat, &aci_bv_creator ) */ ) {
729 for ( a = e->e_attrs; a; a = a->a_next ) {
730 if ( a->a_desc == slap_schema.si_ad_creatorsName ) {
735 if ( a == NULL || !dn_match( &a->a_nvals[ 0 ], &op->o_ndn ) ) {
739 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
740 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
742 regmatch_t tmp_matches[2],
743 *tmp_matchesp = tmp_matches;
747 switch ( a->acl_dn_style ) {
748 case ACL_STYLE_REGEX:
749 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
750 tmp_matchesp = matches;
754 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
757 tmp_matches[0].rm_so = 0;
758 tmp_matches[0].rm_eo = e->e_nname.bv_len;
763 case ACL_STYLE_SUBTREE:
764 case ACL_STYLE_CHILDREN:
765 tmp_matches[0].rm_so = 0;
766 tmp_matches[0].rm_eo = e->e_nname.bv_len;
767 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
768 tmp_matches[1].rm_eo = e->e_nname.bv_len;
782 if ( !regex_matches( &b->a_dn_pat,
783 op->o_ndn.bv_val, e->e_ndn,
784 tmp_nmatch, tmp_matchesp ) )
794 if ( e->e_dn == NULL )
797 if ( b->a_dn_expand ) {
799 char buf[ACL_BUF_SIZE];
802 regmatch_t tmp_matches[2],
803 *tmp_matchesp = tmp_matches;
807 bv.bv_len = sizeof( buf ) - 1;
810 switch ( a->acl_dn_style ) {
811 case ACL_STYLE_REGEX:
812 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
813 tmp_matchesp = matches;
817 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
820 tmp_matches[0].rm_so = 0;
821 tmp_matches[0].rm_eo = e->e_nname.bv_len;
826 case ACL_STYLE_SUBTREE:
827 case ACL_STYLE_CHILDREN:
828 tmp_matches[0].rm_so = 0;
829 tmp_matches[0].rm_eo = e->e_nname.bv_len;
830 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
831 tmp_matches[1].rm_eo = e->e_nname.bv_len;
845 if ( string_expand( &bv, &b->a_dn_pat,
847 tmp_nmatch, tmp_matchesp ) )
852 if ( dnNormalize(0, NULL, NULL, &bv,
853 &pat, op->o_tmpmemctx )
856 /* 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 ) {
912 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
921 if ( b->a_sockurl_pat.bv_len ) {
922 if ( ! op->o_conn->c_listener ) {
925 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
926 b->a_sockurl_pat.bv_val, 0, 0 );
928 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
929 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
930 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
931 e->e_ndn, nmatch, matches ) )
936 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
938 char buf[ACL_BUF_SIZE];
940 bv.bv_len = sizeof( buf ) - 1;
942 if ( string_expand( &bv, &b->a_sockurl_pat,
943 e->e_ndn, nmatch, matches ) )
948 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
954 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
962 if ( b->a_domain_pat.bv_len ) {
963 if ( !op->o_conn->c_peer_domain.bv_val ) {
966 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
967 b->a_domain_pat.bv_val, 0, 0 );
968 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
969 if ( b->a_domain_style == ACL_STYLE_REGEX) {
970 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
971 e->e_ndn, nmatch, matches ) )
976 char buf[ACL_BUF_SIZE];
978 struct berval cmp = op->o_conn->c_peer_domain;
979 struct berval pat = b->a_domain_pat;
981 if ( b->a_domain_expand ) {
984 bv.bv_len = sizeof(buf) - 1;
987 if ( string_expand(&bv, &b->a_domain_pat,
988 e->e_ndn, nmatch, matches) )
995 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
996 int offset = cmp.bv_len - pat.bv_len;
1001 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1005 /* trim the domain */
1006 cmp.bv_val = &cmp.bv_val[ offset ];
1007 cmp.bv_len -= offset;
1010 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1017 if ( b->a_peername_pat.bv_len ) {
1018 if ( !op->o_conn->c_peer_name.bv_val ) {
1021 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1022 b->a_peername_pat.bv_val, 0, 0 );
1023 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1024 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1025 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1026 e->e_ndn, nmatch, matches ) )
1032 /* try exact match */
1033 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1034 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1038 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1040 char buf[ACL_BUF_SIZE];
1042 bv.bv_len = sizeof( buf ) - 1;
1044 if ( string_expand( &bv, &b->a_peername_pat,
1045 e->e_ndn, nmatch, matches ) )
1050 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1054 /* extract IP and try exact match */
1055 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1057 char buf[] = "255.255.255.255";
1060 int port_number = -1;
1062 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1063 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1066 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1067 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1069 port = strrchr( ip.bv_val, ':' );
1073 ip.bv_len = port - ip.bv_val;
1075 port_number = strtol( port, &next, 10 );
1076 if ( next[0] != '\0' )
1080 /* the port check can be anticipated here */
1081 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1084 /* address longer than expected? */
1085 if ( ip.bv_len >= sizeof(buf) )
1088 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1089 buf[ ip.bv_len ] = '\0';
1091 addr = inet_addr( buf );
1093 /* unable to convert? */
1094 if ( addr == (unsigned long)(-1) )
1097 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1100 #ifdef LDAP_PF_LOCAL
1101 /* extract path and try exact match */
1102 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1105 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1106 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1109 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1110 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1112 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1115 #endif /* LDAP_PF_LOCAL */
1117 /* exact match (very unlikely...) */
1118 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1125 if ( b->a_sockname_pat.bv_len ) {
1126 if ( !op->o_conn->c_sock_name.bv_val ) {
1129 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1130 b->a_sockname_pat.bv_val, 0, 0 );
1131 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1132 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1133 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1134 e->e_ndn, nmatch, matches ) )
1139 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1141 char buf[ACL_BUF_SIZE];
1143 bv.bv_len = sizeof( buf ) - 1;
1145 if ( string_expand( &bv, &b->a_sockname_pat,
1146 e->e_ndn, nmatch, matches ) )
1151 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1156 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1163 if ( b->a_dn_at != NULL ) {
1168 const char *attr = b->a_dn_at->ad_cname.bv_val;
1170 assert( attr != NULL );
1172 if ( op->o_ndn.bv_len == 0 ) {
1176 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1180 /* see if asker is listed in dnattr */
1181 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1183 at = attrs_find( at->a_next, b->a_dn_at ) )
1185 if( value_find_ex( b->a_dn_at,
1186 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1187 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1189 &bv, op->o_tmpmemctx ) == 0 )
1198 /* have a dnattr match. if this is a self clause then
1199 * the target must also match the op dn.
1201 if ( b->a_dn_self ) {
1202 /* check if the target is an attribute. */
1203 if ( val == NULL ) continue;
1205 /* target is attribute, check if the attribute value
1208 rc = value_match( &match, b->a_dn_at,
1209 b->a_dn_at->ad_type->sat_equality, 0,
1211 /* on match error or no match, fail the ACL clause */
1212 if (rc != LDAP_SUCCESS || match != 0 )
1216 /* no dnattr match, check if this is a self clause */
1217 if ( ! b->a_dn_self )
1220 ACL_RECORD_VALUE_STATE;
1222 /* this is a self clause, check if the target is an
1228 /* target is attribute, check if the attribute value
1231 rc = value_match( &match, b->a_dn_at,
1232 b->a_dn_at->ad_type->sat_equality, 0,
1235 /* on match error or no match, fail the ACL clause */
1236 if (rc != LDAP_SUCCESS || match != 0 )
1241 if ( b->a_group_pat.bv_len ) {
1243 struct berval ndn = BER_BVNULL;
1246 if ( op->o_ndn.bv_len == 0 ) {
1250 /* b->a_group is an unexpanded entry name, expanded it should be an
1251 * entry with objectclass group* and we test to see if odn is one of
1252 * the values in the attribute group
1254 /* see if asker is listed in dnattr */
1255 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1256 char buf[ACL_BUF_SIZE];
1258 regmatch_t tmp_matches[2],
1259 *tmp_matchesp = tmp_matches;
1261 bv.bv_len = sizeof(buf) - 1;
1266 switch ( a->acl_dn_style ) {
1267 case ACL_STYLE_REGEX:
1268 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1269 tmp_matchesp = matches;
1270 tmp_nmatch = nmatch;
1274 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1275 case ACL_STYLE_BASE:
1276 tmp_matches[0].rm_so = 0;
1277 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1282 case ACL_STYLE_SUBTREE:
1283 case ACL_STYLE_CHILDREN:
1284 tmp_matches[0].rm_so = 0;
1285 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1286 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1287 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1301 if ( string_expand( &bv, &b->a_group_pat,
1303 tmp_nmatch, tmp_matchesp ) )
1308 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1309 op->o_tmpmemctx ) != LDAP_SUCCESS )
1311 /* did not expand to a valid dn */
1318 bv = b->a_group_pat;
1321 rc = backend_group( op, e, &bv, &op->o_ndn,
1322 b->a_group_oc, b->a_group_at );
1325 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1333 if ( b->a_set_pat.bv_len != 0 ) {
1335 char buf[ACL_BUF_SIZE];
1337 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1339 regmatch_t tmp_matches[2],
1340 *tmp_matchesp = tmp_matches;
1343 bv.bv_len = sizeof(buf) - 1;
1348 switch ( a->acl_dn_style ) {
1349 case ACL_STYLE_REGEX:
1350 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1351 tmp_matchesp = matches;
1352 tmp_nmatch = nmatch;
1356 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1357 case ACL_STYLE_BASE:
1358 tmp_matches[0].rm_so = 0;
1359 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1364 case ACL_STYLE_SUBTREE:
1365 case ACL_STYLE_CHILDREN:
1366 tmp_matches[0].rm_so = 0;
1367 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1368 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1369 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1383 if ( string_expand( &bv, &b->a_set_pat,
1385 tmp_nmatch, tmp_matchesp ) )
1393 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1398 if ( b->a_authz.sai_ssf ) {
1399 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1400 b->a_authz.sai_ssf, op->o_ssf, 0 );
1401 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1406 if ( b->a_authz.sai_transport_ssf ) {
1407 Debug( LDAP_DEBUG_ACL,
1408 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1409 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1410 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1415 if ( b->a_authz.sai_tls_ssf ) {
1416 Debug( LDAP_DEBUG_ACL,
1417 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1418 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1419 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1424 if ( b->a_authz.sai_sasl_ssf ) {
1425 Debug( LDAP_DEBUG_ACL,
1426 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1427 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1428 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1433 #ifdef SLAPD_ACI_ENABLED
1434 if ( b->a_aci_at != NULL ) {
1436 slap_access_t grant, deny, tgrant, tdeny;
1437 struct berval parent_ndn, old_parent_ndn;
1438 BerVarray bvals = NULL;
1441 /* this case works different from the others above.
1442 * since aci's themselves give permissions, we need
1443 * to first check b->a_access_mask, the ACL's access level.
1446 if ( e->e_nname.bv_len == 0 ) {
1447 /* no ACIs in the root DSE */
1451 /* first check if the right being requested
1452 * is allowed by the ACL clause.
1454 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1457 /* start out with nothing granted, nothing denied */
1461 /* get the aci attribute */
1462 at = attr_find( e->e_attrs, b->a_aci_at );
1465 /* FIXME: this breaks acl caching;
1466 * see also ACL_RECORD_VALUE_STATE below */
1467 ACL_RECORD_VALUE_STATE;
1469 /* the aci is an multi-valued attribute. The
1470 * rights are determined by OR'ing the individual
1471 * rights given by the acis.
1473 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1478 &grant, &deny, &aci_bv_entry ) != 0)
1484 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1485 accessmask2str(tgrant,accessmaskbuf),
1486 accessmask2str(tdeny, accessmaskbuf1), 0);
1489 /* If the entry level aci didn't contain anything valid for the
1490 * current operation, climb up the tree and evaluate the
1491 * acis with scope set to subtree
1493 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1494 dnParent(&(e->e_nname), &parent_ndn);
1495 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1496 old_parent_ndn = parent_ndn;
1497 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1498 ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1506 for( i = 0; bvals[i].bv_val != NULL; i++){
1508 /* FIXME: this breaks acl caching;
1509 * see also ACL_RECORD_VALUE_STATE above */
1510 ACL_RECORD_VALUE_STATE;
1512 if (aci_mask(op, e, desc, val, &bvals[i],
1514 &grant, &deny, &aci_bv_children) != 0)
1518 /* evaluation stops as soon as either a "deny" or a
1519 * "grant" directive matches.
1521 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1525 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1526 accessmask2str(tgrant,accessmaskbuf),
1527 accessmask2str(tdeny, accessmaskbuf1), 0);
1531 case LDAP_NO_SUCH_ATTRIBUTE:
1532 /* just go on if the aci-Attribute is not present in
1535 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1539 case LDAP_NO_SUCH_OBJECT:
1540 /* We have reached the base object */
1541 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1552 dnParent(&old_parent_ndn, &parent_ndn);
1557 /* remove anything that the ACL clause does not allow */
1558 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1559 tdeny &= ACL_PRIV_MASK;
1561 /* see if we have anything to contribute */
1562 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1566 /* this could be improved by changing acl_mask so that it can deal with
1567 * by clauses that return grant/deny pairs. Right now, it does either
1568 * additive or subtractive rights, but not both at the same time. So,
1569 * we need to combine the grant/deny pair into a single rights mask in
1570 * a smart way: if either grant or deny is "empty", then we use the
1571 * opposite as is, otherwise we remove any denied rights from the grant
1572 * rights mask and construct an additive mask.
1574 if (ACL_IS_INVALID(tdeny)) {
1575 modmask = tgrant | ACL_PRIV_ADDITIVE;
1577 } else if (ACL_IS_INVALID(tgrant)) {
1578 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1581 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1587 modmask = b->a_access_mask;
1590 Debug( LDAP_DEBUG_ACL,
1591 "<= acl_mask: [%d] applying %s (%s)\n",
1592 i, accessmask2str( modmask, accessmaskbuf ),
1593 b->a_type == ACL_CONTINUE
1595 : b->a_type == ACL_BREAK
1601 if( ACL_IS_ADDITIVE(modmask) ) {
1603 ACL_PRIV_SET( *mask, modmask );
1606 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1608 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1609 /* substract privs */
1610 ACL_PRIV_CLR( *mask, modmask );
1613 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1620 Debug( LDAP_DEBUG_ACL,
1621 "<= acl_mask: [%d] mask: %s\n",
1622 i, accessmask2str(*mask, accessmaskbuf), 0 );
1624 if( b->a_type == ACL_CONTINUE ) {
1627 } else if ( b->a_type == ACL_BREAK ) {
1635 /* implicit "by * none" clause */
1638 Debug( LDAP_DEBUG_ACL,
1639 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1640 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1645 * acl_check_modlist - check access control on the given entry to see if
1646 * it allows the given modifications by the user associated with op.
1647 * returns 1 if mods allowed ok
1648 * 0 mods not allowed
1655 Modifications *mlist
1659 AccessControlState state = ACL_STATE_INIT;
1662 int ret = 1; /* default is access allowed */
1670 assert( be != NULL );
1672 /* short circuit root database access */
1673 if ( be_isroot( op ) ) {
1674 Debug( LDAP_DEBUG_ACL,
1675 "<= acl_access_allowed: granted to database root\n",
1680 /* use backend default access if no backend acls */
1681 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1682 Debug( LDAP_DEBUG_ACL,
1683 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1684 access2str( ACL_WRITE ),
1685 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1686 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1690 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1692 * no-user-modification operational attributes are ignored
1693 * by ACL_WRITE checking as any found here are not provided
1696 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1697 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1698 " modify access granted\n",
1699 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1703 switch ( mlist->sml_op ) {
1704 case LDAP_MOD_REPLACE:
1706 * We must check both permission to delete the whole
1707 * attribute and permission to add the specific attributes.
1708 * This prevents abuse from selfwriters.
1710 if ( ! access_allowed( op, e,
1711 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1717 if ( mlist->sml_values == NULL ) break;
1719 /* fall thru to check value to add */
1722 assert( mlist->sml_values != NULL );
1724 for ( bv = mlist->sml_nvalues
1725 ? mlist->sml_nvalues : mlist->sml_values;
1726 bv->bv_val != NULL; bv++ )
1728 if ( ! access_allowed( op, e,
1729 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1737 case LDAP_MOD_DELETE:
1738 if ( mlist->sml_values == NULL ) {
1739 if ( ! access_allowed( op, e,
1740 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1747 for ( bv = mlist->sml_nvalues
1748 ? mlist->sml_nvalues : mlist->sml_values;
1749 bv->bv_val != NULL; bv++ )
1751 if ( ! access_allowed( op, e,
1752 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1760 case SLAP_MOD_SOFTADD:
1761 /* allow adding attribute via modrdn thru */
1773 if (be_null) op->o_bd = NULL;
1779 struct berval *list,
1792 while (len >= 0 && --ix >= 0) {
1793 while (--len >= 0 && *p++ != sep) ;
1795 while (len >= 0 && *p == ' ') {
1806 while (--len >= 0 && *p != sep) {
1810 while (bv->bv_len > 0 && *--p == ' ')
1815 typedef struct aci_set_gather_t {
1821 aci_set_cb_gather( Operation *op, SlapReply *rs )
1823 aci_set_gather_t *p = (aci_set_gather_t *)op->o_callback->sc_private;
1825 if ( rs->sr_type == REP_SEARCH ) {
1826 BerValue bvals[ 2 ];
1827 BerVarray bvalsp = NULL;
1830 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1831 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
1833 if ( desc == slap_schema.si_ad_entryDN ) {
1835 bvals[ 0 ] = rs->sr_entry->e_nname;
1836 BER_BVZERO( &bvals[ 1 ] );
1841 a = attr_find( rs->sr_entry->e_attrs, desc );
1845 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1848 bvalsp = a->a_nvals;
1854 p->bvals = slap_set_join( p->cookie, p->bvals,
1855 ( '|' | SLAP_SET_RREF ), bvalsp );
1859 assert( rs->sr_type == REP_RESULT );
1866 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
1868 AciSetCookie *cp = (AciSetCookie *)cookie;
1870 LDAPURLDesc *ludp = NULL;
1871 Operation op2 = { 0 };
1872 SlapReply rs = {REP_RESULT};
1873 AttributeName anlist[ 2 ], *anlistp = NULL;
1875 slap_callback cb = { NULL, aci_set_cb_gather, NULL, NULL };
1876 aci_set_gather_t p = { 0 };
1877 const char *text = NULL;
1878 static struct berval defaultFilter_bv = BER_BVC( "(objectClass=*)" );
1880 /* this routine needs to return the bervals instead of
1881 * plain strings, since syntax is not known. It should
1882 * also return the syntax or some "comparison cookie".
1884 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
1885 return aci_set_gather2( cookie, name, desc );
1888 rc = ldap_url_parse( name->bv_val, &ludp );
1889 if ( rc != LDAP_URL_SUCCESS ) {
1890 rc = LDAP_PROTOCOL_ERROR;
1894 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
1896 /* host part must be empty */
1897 /* extensions parts must be empty */
1898 rc = LDAP_PROTOCOL_ERROR;
1902 /* Grab the searchbase and see if an appropriate database can be found */
1903 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
1904 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
1905 &op2.o_req_ndn, cp->op->o_tmpmemctx );
1906 BER_BVZERO( &op2.o_req_dn );
1907 if ( rc != LDAP_SUCCESS ) {
1911 op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
1912 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
1913 rc = LDAP_NO_SUCH_OBJECT;
1917 /* Grab the filter */
1918 if ( ludp->lud_filter ) {
1919 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
1920 cp->op->o_tmpmemctx );
1923 op2.ors_filterstr = defaultFilter_bv;
1926 op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
1927 if ( op2.ors_filter == NULL ) {
1928 rc = LDAP_PROTOCOL_ERROR;
1932 /* Grab the scope */
1933 op2.ors_scope = ludp->lud_scope;
1935 /* Grap the attributes */
1936 if ( ludp->lud_attrs ) {
1937 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
1940 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
1941 cp->op->o_tmpmemctx );
1943 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
1944 ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
1945 anlistp[ nattrs ].an_desc = NULL;
1946 rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
1947 &anlistp[ nattrs ].an_desc, &text );
1948 if ( rc != LDAP_SUCCESS ) {
1957 anlistp[ nattrs ].an_name = desc->ad_cname;
1958 anlistp[ nattrs ].an_desc = desc;
1960 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
1964 op2.o_tag = LDAP_REQ_SEARCH;
1965 op2.o_protocol = LDAP_VERSION3;
1966 op2.o_ndn = op2.o_bd->be_rootndn;
1967 op2.o_callback = &cb;
1968 op2.o_time = slap_get_time();
1969 op2.o_do_not_cache = 1;
1970 op2.o_is_auth_check = 0;
1971 op2.o_threadctx = cp->op->o_threadctx;
1972 op2.o_tmpmemctx = cp->op->o_tmpmemctx;
1973 op2.o_tmpmfuncs = cp->op->o_tmpmfuncs;
1975 op2.o_pb = cp->op->o_pb;
1977 op2.o_conn = cp->op->o_conn;
1978 op2.o_connid = cp->op->o_connid;
1979 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
1980 op2.ors_slimit = SLAP_NO_LIMIT;
1981 op2.ors_tlimit = SLAP_NO_LIMIT;
1982 op2.ors_attrs = anlistp;
1983 op2.ors_attrsonly = 0;
1984 op2.o_sync_slog_size = -1;
1988 rc = op2.o_bd->be_search( &op2, &rs );
1994 if ( op2.ors_filter ) {
1995 filter_free_x( cp->op, op2.ors_filter );
1997 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
1998 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2000 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2001 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2004 ldap_free_urldesc( ludp );
2006 if ( anlistp && anlistp != anlist ) {
2007 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2014 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2016 AciSetCookie *cp = (AciSetCookie *)cookie;
2017 BerVarray bvals = NULL;
2021 /* this routine needs to return the bervals instead of
2022 * plain strings, since syntax is not known. It should
2023 * also return the syntax or some "comparison cookie".
2025 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2026 if ( rc == LDAP_SUCCESS ) {
2027 if ( desc == slap_schema.si_ad_entryDN ) {
2028 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2029 cp->op->o_tmpmemctx );
2031 BER_BVZERO( &bvals[ 1 ] );
2035 backend_attribute( cp->op,
2036 cp->e, &ndn, desc, &bvals, ACL_NONE );
2039 if ( !BER_BVISNULL( &ndn ) ) {
2040 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2049 struct berval *subj,
2055 struct berval set = BER_BVNULL;
2057 AciSetCookie cookie;
2060 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2062 struct berval subjdn, ndn = BER_BVNULL;
2063 struct berval setat;
2066 AttributeDescription *desc = NULL;
2068 /* format of string is "entry/setAttrName" */
2069 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2073 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2074 setat = aci_bv_set_attr;
2078 * NOTE: dnNormalize honors the ber_len field
2079 * as the length of the dn to be normalized
2081 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2082 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2084 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2085 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2089 BER_BVZERO( &bvals[0] );
2090 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2092 bvals[0].bv_val = bvals[i-1].bv_val;
2093 BER_BVZERO( &bvals[i-1] );
2095 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2096 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2101 if ( !BER_BVISNULL( &set ) ) {
2104 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2105 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2106 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2112 #ifdef SLAPD_ACI_ENABLED
2114 aci_list_map_rights(
2115 struct berval *list )
2122 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2125 switch (*bv.bv_val) {
2127 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2130 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2131 * the right 's' to mean "set", but in the examples states
2132 * that the right 's' means "search". The latter definition
2135 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2138 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2141 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2144 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
2145 * define any equivalent to the AUTH right, so I've just used
2148 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2160 struct berval *list,
2161 const struct berval *attr,
2162 struct berval *val )
2164 struct berval bv, left, right;
2167 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2168 if (aci_get_part(&bv, 0, '=', &left) < 0
2169 || aci_get_part(&bv, 1, '=', &right) < 0)
2171 if (ber_bvstrcasecmp(attr, &bv) == 0)
2173 } else if (val == NULL) {
2174 if (ber_bvstrcasecmp(attr, &left) == 0)
2177 if (ber_bvstrcasecmp(attr, &left) == 0) {
2178 /* this is experimental code that implements a
2179 * simple (prefix) match of the attribute value.
2180 * the ACI draft does not provide for aci's that
2181 * apply to specific values, but it would be
2182 * nice to have. If the <attr> part of an aci's
2183 * rights list is of the form <attr>=<value>,
2184 * that means the aci applies only to attrs with
2185 * the given value. Furthermore, if the attr is
2186 * of the form <attr>=<value>*, then <value> is
2187 * treated as a prefix, and the aci applies to
2188 * any value with that prefix.
2190 * Ideally, this would allow r.e. matches.
2192 if (aci_get_part(&right, 0, '*', &left) < 0
2193 || right.bv_len <= left.bv_len)
2195 if (ber_bvstrcasecmp(val, &right) == 0)
2197 } else if (val->bv_len >= left.bv_len) {
2198 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2207 static slap_access_t
2208 aci_list_get_attr_rights(
2209 struct berval *list,
2210 const struct berval *attr,
2211 struct berval *val )
2217 /* loop through each rights/attr pair, skip first part (action) */
2219 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2220 if (aci_list_has_attr(&bv, attr, val) == 0)
2222 if (aci_get_part(list, i, ';', &bv) < 0)
2224 mask |= aci_list_map_rights(&bv);
2230 aci_list_get_rights(
2231 struct berval *list,
2232 const struct berval *attr,
2234 slap_access_t *grant,
2235 slap_access_t *deny )
2237 struct berval perm, actn;
2238 slap_access_t *mask;
2241 if (attr == NULL || attr->bv_len == 0
2242 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2243 attr = &aci_bv_br_entry;
2249 /* loop through each permissions clause */
2250 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2251 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2253 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2255 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2262 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2263 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2270 struct berval *subj,
2271 struct berval *defgrpoc,
2272 struct berval *defgrpat,
2279 struct berval subjdn;
2280 struct berval grpoc;
2281 struct berval grpat;
2282 ObjectClass *grp_oc = NULL;
2283 AttributeDescription *grp_ad = NULL;
2287 /* format of string is "group/objectClassValue/groupAttrName" */
2288 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2292 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2296 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2300 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2301 if( rc != LDAP_SUCCESS ) {
2307 grp_oc = oc_bvfind( &grpoc );
2309 if (grp_oc != NULL && grp_ad != NULL ) {
2310 char buf[ACL_BUF_SIZE];
2311 struct berval bv, ndn;
2312 bv.bv_len = sizeof( buf ) - 1;
2313 bv.bv_val = (char *)&buf;
2314 if ( string_expand(&bv, &subjdn,
2315 e->e_ndn, nmatch, matches) )
2320 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2321 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2322 grp_oc, grp_ad ) == 0 );
2323 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2335 AttributeDescription *desc,
2339 regmatch_t *matches,
2340 slap_access_t *grant,
2341 slap_access_t *deny,
2342 struct berval *scope
2345 struct berval bv, perms, sdn;
2349 assert( !BER_BVISNULL( &desc->ad_cname ) );
2351 /* parse an aci of the form:
2352 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2354 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2355 a full description of the format for this attribute.
2356 Differences: "this" in the draft is "self" here, and
2357 "self" and "public" is in the position of dnType.
2359 For now, this routine only supports scope=entry.
2361 /* check that the aci has all 5 components */
2362 if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2366 /* check that the aci family is supported */
2367 if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2371 /* check that the scope matches */
2372 if ( aci_get_part( aci, 1, '#', &bv ) < 0
2373 || ber_bvstrcasecmp( scope, &bv ) != 0 )
2378 /* get the list of permissions clauses, bail if empty */
2379 if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2383 /* check if any permissions allow desired access */
2384 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2388 /* see if we have a DN match */
2389 if ( aci_get_part( aci, 3, '#', &bv ) < 0 ) {
2393 if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2397 if ( ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0 ) {
2401 if ( dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2403 if ( dn_match( &op->o_ndn, &ndn ) ) {
2406 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2410 } else if ( ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0 ) {
2413 } else if ( ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0 ) {
2414 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2418 } else if ( ber_bvstrcasecmp( &aci_bv_creator, &bv ) == 0 ) {
2421 for ( a = e->e_attrs; a; a = a->a_next ) {
2422 if ( a->a_desc == slap_schema.si_ad_creatorsName ) {
2427 if ( a != NULL && dn_match( &op->o_ndn, &a->a_nvals[ 0 ] ) ) {
2431 } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0 ) {
2433 AttributeDescription *ad = NULL;
2436 rc = slap_bv2ad( &sdn, &ad, &text );
2438 if( rc != LDAP_SUCCESS ) {
2446 for ( at = attrs_find( e->e_attrs, ad );
2448 at = attrs_find( at->a_next, ad ) )
2450 if ( value_find_ex( ad,
2451 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2452 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2454 &bv, op->o_tmpmemctx ) == 0 )
2464 } else if ( ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0 ) {
2465 if ( aci_group_member( &sdn, &aci_bv_group_class,
2466 &aci_bv_group_attr, op, e, nmatch, matches ) )
2471 } else if ( ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0 ) {
2472 if ( aci_group_member( &sdn, &aci_bv_role_class,
2473 &aci_bv_role_attr, op, e, nmatch, matches ) )
2478 } else if ( ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0 ) {
2479 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2483 } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0 ) {
2484 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2492 #endif /* SLAPD_ACI_ENABLED */
2500 regmatch_t *matches)
2508 bv->bv_val[0] = '\0';
2509 bv->bv_len--; /* leave space for lone $ */
2512 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2513 sp < pat->bv_val + pat->bv_len ; sp++ )
2515 /* did we previously see a $ */
2517 if ( flag == 1 && *sp == '$' ) {
2522 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2525 } else if ( *sp >= '0' && *sp <= '9' ) {
2533 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2534 if ( *sp >= '0' && *sp <= '9' ) {
2535 n = 10*n + ( *sp - '0' );
2539 if ( *sp != /*'{'*/ '}' ) {
2545 if ( n >= nmatch ) {
2551 i = matches[n].rm_so;
2552 l = matches[n].rm_eo;
2553 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2571 /* must have ended with a single $ */
2579 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2580 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2587 struct berval *pat, /* pattern to expand and match against */
2588 char *str, /* string to match against pattern */
2589 char *buf, /* buffer with $N expansion variables */
2590 int nmatch, /* size of the matches array */
2591 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2595 char newbuf[ACL_BUF_SIZE];
2599 bv.bv_len = sizeof( newbuf ) - 1;
2606 string_expand( &bv, pat, buf, nmatch, matches );
2607 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2609 char error[ACL_BUF_SIZE];
2610 regerror( rc, &re, error, sizeof( error ) );
2612 Debug( LDAP_DEBUG_TRACE,
2613 "compile( \"%s\", \"%s\") failed %s\n",
2614 pat->bv_val, str, error );
2618 rc = regexec( &re, str, 0, NULL, 0 );
2621 Debug( LDAP_DEBUG_TRACE,
2622 "=> regex_matches: string: %s\n", str, 0, 0 );
2623 Debug( LDAP_DEBUG_TRACE,
2624 "=> regex_matches: rc: %d %s\n",
2625 rc, !rc ? "matches" : "no matches", 0 );