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-2009 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 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
42 static const struct berval acl_bv_ip_eq = BER_BVC( "IP=" );
44 static const struct berval acl_bv_ipv6_eq = BER_BVC( "IP=[" );
45 #endif /* LDAP_PF_INET6 */
47 static const struct berval acl_bv_path_eq = BER_BVC("PATH=");
48 #endif /* LDAP_PF_LOCAL */
50 static AccessControl * slap_acl_get(
51 Operation *op, AclCheck *ak,
53 AclRegexMatches *matches,
56 static slap_control_t slap_acl_mask(
57 Operation *op, AclCheck *ak,
59 AclRegexMatches *matches,
62 static int regex_matches(
63 struct berval *pat, char *str,
64 struct berval *dn_matches, struct berval *val_matches,
65 AclRegexMatches *matches);
67 typedef struct AclSetCookie {
69 #define asc_op asc_cookie.set_op
74 SLAP_SET_GATHER acl_set_gather;
75 SLAP_SET_GATHER acl_set_gather2;
78 * access_allowed - check whether op->o_ndn is allowed the requested access
79 * to entry e, attribute attr, value val. if val is null, access to
80 * the whole attribute is assumed (all values).
82 * This routine loops through all access controls and calls
83 * slap_acl_mask() on each applicable access control.
84 * The loop exits when a definitive answer is reached or
85 * or no more controls remain.
92 * - can be legally called with op == NULL
93 * - can be legally called with op->o_bd == NULL
97 slap_access_always_allowed(
102 ACL_LVL_ASSIGN_MANAGE( ak->ak_mask );
107 #define MATCHES_DNMAXCOUNT(m) \
108 ( sizeof ( (m)->dn_data ) / sizeof( *(m)->dn_data ) )
109 #define MATCHES_VALMAXCOUNT(m) \
110 ( sizeof ( (m)->val_data ) / sizeof( *(m)->val_data ) )
111 #define MATCHES_MEMSET(m) do { \
112 memset( (m)->dn_data, '\0', sizeof( (m)->dn_data ) ); \
113 memset( (m)->val_data, '\0', sizeof( (m)->val_data ) ); \
114 (m)->dn_count = MATCHES_DNMAXCOUNT( (m) ); \
115 (m)->val_count = MATCHES_VALMAXCOUNT( (m) ); \
116 } while ( 0 /* CONSTCOND */ )
125 AccessControl *a = NULL;
128 char accessmaskbuf[ACCESSMASK_MAXLEN];
130 slap_control_t control;
131 slap_access_t access_level;
134 AclRegexMatches matches;
135 AccessControlState acl_state = ACL_STATE_INIT;
136 static AccessControlState state_init = ACL_STATE_INIT;
138 assert( op != NULL );
139 assert( ak != NULL );
140 assert( ak->ak_e != NULL );
141 assert( ak->ak_desc != NULL );
143 access_level = ACL_LEVEL( ak->ak_access );
144 attr = ak->ak_desc->ad_cname.bv_val;
146 assert( attr != NULL );
148 /* grant database root access */
149 if ( be_isroot( op ) ) {
150 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
151 ak->ak_mask = ACL_LVL_MANAGE;
155 oldmask = ak->ak_mask;
156 ACL_INIT( ak->ak_mask );
159 * no-user-modification operational attributes are ignored
160 * by ACL_WRITE checking as any found here are not provided
163 * NOTE: but they are not ignored for ACL_MANAGE, because
164 * if we get here it means a non-root user is trying to
165 * manage data, so we need to check its privileges.
167 if ( access_level == ACL_WRITE
168 && is_at_no_user_mod( ak->ak_desc->ad_type )
169 && ak->ak_desc != slap_schema.si_ad_entry
170 && ak->ak_desc != slap_schema.si_ad_children )
172 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
173 " %s access granted\n",
178 /* use backend default access if no backend acls */
179 if ( op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
182 Debug( LDAP_DEBUG_ACL,
183 "=> slap_access_allowed: backend default %s "
184 "access %s to \"%s\"\n",
185 access2str( ak->ak_access ),
186 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
187 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
188 ret = op->o_bd->be_dfltaccess >= access_level;
190 ak->ak_mask = ACL_PRIV_LEVEL;
191 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
192 ACL_PRIV_SET( ak->ak_mask, ACL_ACCESS2PRIV( i ) );
201 if ( ak->ak_state == NULL )
202 ak->ak_state = &acl_state;
203 if ( ak->ak_state->as_desc == ak->ak_desc &&
204 ak->ak_state->as_access == ak->ak_access &&
205 ak->ak_state->as_vd_acl != NULL )
207 a = ak->ak_state->as_vd_acl;
208 count = ak->ak_state->as_vd_acl_count;
209 if ( ak->ak_state->as_fe_done )
210 ak->ak_state->as_fe_done--;
211 ACL_PRIV_ASSIGN( ak->ak_mask, ak->ak_state->as_vd_mask );
213 *ak->ak_state = state_init;
219 MATCHES_MEMSET( &matches );
221 while ( ( a = slap_acl_get( op, ak, a, &matches, &count ) ) != NULL )
224 int dnmaxcount = MATCHES_DNMAXCOUNT( &matches );
225 int valmaxcount = MATCHES_VALMAXCOUNT( &matches );
226 regmatch_t *dn_data = matches.dn_data;
227 regmatch_t *val_data = matches.val_data;
230 for ( i = 0; i < dnmaxcount && dn_data[i].rm_eo > 0; i++ ) {
231 char *data = ak->ak_e->e_ndn;
233 Debug( LDAP_DEBUG_ACL, "=> match[dn%d]: %d %d ", i,
234 (int)dn_data[i].rm_so,
235 (int)dn_data[i].rm_eo );
236 if ( dn_data[i].rm_so <= dn_data[0].rm_eo ) {
238 for ( n = dn_data[i].rm_so;
239 n < dn_data[i].rm_eo; n++ ) {
240 Debug( LDAP_DEBUG_ACL, "%c",
244 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
248 for ( i = 0; i < valmaxcount && val_data[i].rm_eo > 0; i++ ) {
249 char *data = ak->ak_val->bv_val;
251 Debug( LDAP_DEBUG_ACL, "=> match[val%d]: %d %d ", i,
252 (int)val_data[i].rm_so,
253 (int)val_data[i].rm_eo );
254 if ( val_data[i].rm_so <= val_data[0].rm_eo ) {
256 for ( n = val_data[i].rm_so;
257 n < val_data[i].rm_eo; n++ ) {
258 Debug( LDAP_DEBUG_ACL, "%c",
262 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
265 control = slap_acl_mask( op, ak, a,
268 if ( control != ACL_BREAK ) {
272 MATCHES_MEMSET( &matches );
275 if ( ACL_IS_INVALID( ak->ak_mask ) ) {
276 Debug( LDAP_DEBUG_ACL,
277 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
278 ak->ak_e->e_dn, attr, 0 );
279 ACL_PRIV_ASSIGN( ak->ak_mask, oldmask );
281 } else if ( control == ACL_BREAK ) {
282 Debug( LDAP_DEBUG_ACL,
283 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
288 ret = ACL_GRANT( ak->ak_mask, ak->ak_access );
290 Debug( LDAP_DEBUG_ACL,
291 "=> slap_access_allowed: %s access %s by %s\n",
292 access2str( ak->ak_access ), ret ? "granted" : "denied",
293 accessmask2str( ak->ak_mask, accessmaskbuf, 1 ) );
296 if ( ak->ak_state == &acl_state )
310 * NOTE: control gets here if FIXME
311 * if an appropriate backend cannot be selected for the operation,
312 * we assume that the frontend should handle this
313 * FIXME: should select_backend() take care of this,
314 * and return frontendDB instead of NULL? maybe for some value
319 if ( op->o_bd == NULL ) {
320 op->o_bd = select_backend( &op->o_req_ndn, 0 );
321 if ( op->o_bd == NULL )
322 op->o_bd = frontendDB;
324 rc = slap_access_allowed( op, ak );
339 char accessmaskbuf[ACCESSMASK_MAXLEN];
341 slap_access_t access_level;
344 assert( ak != NULL );
345 assert( ak->ak_e != NULL );
346 assert( ak->ak_desc != NULL );
348 access_level = ACL_LEVEL( ak->ak_access );
350 assert( access_level > ACL_NONE );
352 ACL_INVALIDATE( ak->ak_mask );
354 attr = ak->ak_desc->ad_cname.bv_val;
356 assert( attr != NULL );
359 if ( op->o_acl_priv != ACL_NONE ) {
360 ak->ak_access = op->o_acl_priv;
362 } else if ( op->o_is_auth_check &&
363 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
365 ak->ak_access = ACL_AUTH;
367 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
368 ak->ak_desc == slap_schema.si_ad_entry )
370 ak->ak_access = ACL_MANAGE;
374 if ( ak->ak_state != NULL ) {
375 if ( ak->ak_state->as_desc == ak->ak_desc &&
376 ak->ak_state->as_access == ak->ak_access &&
377 ak->ak_state->as_result != -1 &&
378 ak->ak_state->as_vd_acl == NULL )
380 Debug( LDAP_DEBUG_ACL,
381 "=> access_allowed: result was in cache (%s)\n",
383 return ak->ak_state->as_result;
385 Debug( LDAP_DEBUG_ACL,
386 "=> access_allowed: result not in cache (%s)\n",
391 ACL_INIT( ak->ak_mask );
393 Debug( LDAP_DEBUG_ACL,
394 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
395 access2str( ak->ak_access ), ak->ak_e->e_dn, attr );
402 if ( op->o_bd == NULL ) {
403 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
406 /* FIXME: experimental; use first backend rules
407 * iff there is no global_acl (ITS#3100)
409 if ( frontendDB->be_acl != NULL ) {
410 op->o_bd = frontendDB;
413 assert( op->o_bd != NULL );
415 /* this is enforced in backend_add() */
416 if ( op->o_bd->bd_info->bi_access_allowed ) {
417 /* delegate to backend */
418 ret = op->o_bd->bd_info->bi_access_allowed( op, ak );
421 /* use default (but pass through frontend
422 * for global ACL overlays) */
423 ret = frontendDB->bd_info->bi_access_allowed( op, ak );
427 if ( ACL_IS_INVALID( ak->ak_mask ) ) {
428 Debug( LDAP_DEBUG_ACL,
429 "=> access_allowed: \"%s\" (%s) invalid!\n",
430 ak->ak_e->e_dn, attr, 0 );
431 ACL_INIT( ak->ak_mask );
434 Debug( LDAP_DEBUG_ACL,
435 "=> access_allowed: no more rules\n", 0, 0, 0 );
441 Debug( LDAP_DEBUG_ACL,
442 "=> access_allowed: %s access %s by %s\n",
443 access2str( ak->ak_access ), ret ? "granted" : "denied",
444 accessmask2str( ak->ak_mask, accessmaskbuf, 1 ) );
447 if ( ak->ak_state != NULL ) {
448 ak->ak_state->as_access = ak->ak_access;
449 ak->ak_state->as_result = ret;
450 ak->ak_state->as_desc = ak->ak_desc;
452 if ( be_null ) op->o_bd = NULL;
458 * slap_acl_get - return the acl applicable to entry e, attribute
459 * attr. the acl returned is suitable for use in subsequent calls to
460 * acl_access_allowed().
463 static AccessControl *
468 AclRegexMatches *matches,
475 assert( ak->ak_e != NULL );
476 assert( count != NULL );
477 assert( ak->ak_desc != NULL );
478 assert( ak->ak_state != NULL );
480 attr = ak->ak_desc->ad_cname.bv_val;
482 assert( attr != NULL );
485 if( op->o_bd == NULL || op->o_bd->be_acl == NULL ) {
486 a = frontendDB->be_acl;
488 a = op->o_bd->be_acl;
493 if ( a == frontendDB->be_acl )
494 ak->ak_state->as_fe_done = 1;
500 dnlen = ak->ak_e->e_nname.bv_len;
503 for ( ; a != NULL; prev = a, a = a->acl_next ) {
506 if ( a != frontendDB->be_acl && ak->ak_state->as_fe_done )
507 ak->ak_state->as_fe_done++;
509 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
510 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
511 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
512 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
513 if ( regexec ( &a->acl_dn_re,
516 matches->dn_data, 0 ) )
522 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
523 *count, a->acl_dn_pat.bv_val, 0 );
524 patlen = a->acl_dn_pat.bv_len;
525 if ( dnlen < patlen )
528 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
529 /* base dn -- entire object DN must match */
530 if ( dnlen != patlen )
533 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
534 ber_len_t rdnlen = 0;
537 if ( dnlen <= patlen )
541 if ( !DN_SEPARATOR( ak->ak_e->e_ndn[dnlen - patlen - 1] ) )
546 rdnlen = dn_rdnlen( NULL, &ak->ak_e->e_nname );
547 if ( rdnlen + patlen + sep != dnlen )
550 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
551 if ( dnlen > patlen && !DN_SEPARATOR( ak->ak_e->e_ndn[dnlen - patlen - 1] ) )
554 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
555 if ( dnlen <= patlen )
557 if ( !DN_SEPARATOR( ak->ak_e->e_ndn[dnlen - patlen - 1] ) )
561 if ( strcmp( a->acl_dn_pat.bv_val, ak->ak_e->e_ndn + dnlen - patlen ) != 0 )
565 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
569 if ( a->acl_attrs && !ad_inlist( ak->ak_desc, a->acl_attrs ) ) {
570 matches->dn_data[0].rm_so = -1;
571 matches->dn_data[0].rm_eo = -1;
572 matches->val_data[0].rm_so = -1;
573 matches->val_data[0].rm_eo = -1;
577 /* Is this ACL only for a specific value? */
578 if ( a->acl_attrval.bv_len ) {
579 if ( ak->ak_val == NULL ) {
583 if ( ak->ak_state->as_vd_acl == NULL ) {
584 ak->ak_state->as_vd_acl = prev;
585 ak->ak_state->as_vd_acl_count = *count - 1;
586 ACL_PRIV_ASSIGN ( ak->ak_state->as_vd_mask, ak->ak_mask );
589 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
590 Debug( LDAP_DEBUG_ACL,
591 "acl_get: valpat %s\n",
592 a->acl_attrval.bv_val, 0, 0 );
593 if ( regexec ( &a->acl_attrval_re,
596 matches->val_data, 0 ) )
604 Debug( LDAP_DEBUG_ACL,
606 a->acl_attrval.bv_val, 0, 0 );
608 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
609 if (value_match( &match, ak->ak_desc,
610 a->acl_attrval_mr, 0,
611 ak->ak_val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
616 ber_len_t patlen, vdnlen;
618 patlen = a->acl_attrval.bv_len;
619 vdnlen = ak->ak_val->bv_len;
621 if ( vdnlen < patlen )
624 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
625 if ( vdnlen > patlen )
628 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
629 ber_len_t rdnlen = 0;
631 if ( !DN_SEPARATOR( ak->ak_val->bv_val[vdnlen - patlen - 1] ) )
634 rdnlen = dn_rdnlen( NULL, ak->ak_val );
635 if ( rdnlen + patlen + 1 != vdnlen )
638 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
639 if ( vdnlen > patlen && !DN_SEPARATOR( ak->ak_val->bv_val[vdnlen - patlen - 1] ) )
642 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
643 if ( vdnlen <= patlen )
646 if ( !DN_SEPARATOR( ak->ak_val->bv_val[vdnlen - patlen - 1] ) )
650 if ( strcmp( a->acl_attrval.bv_val, ak->ak_val->bv_val + vdnlen - patlen ) )
656 if ( a->acl_filter != NULL ) {
657 ber_int_t rc = test_filter( NULL, ak->ak_e, a->acl_filter );
658 if ( rc != LDAP_COMPARE_TRUE ) {
663 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
668 if ( !ak->ak_state->as_fe_done ) {
669 ak->ak_state->as_fe_done = 1;
670 a = frontendDB->be_acl;
674 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
679 * Record value-dependent access control state
681 #define ACL_RECORD_VALUE_STATE do { \
682 if( ak->ak_state && ak->ak_state->as_vd_acl == NULL ) { \
683 ak->ak_state->as_vd_acl = a; \
684 ak->ak_state->as_vd_acl_count = count; \
685 ACL_PRIV_ASSIGN( ak->ak_state->as_vd_mask, ak->ak_mask ); \
694 AclRegexMatches *matches,
696 struct berval *opndn )
699 * if access applies to the entry itself, and the
700 * user is bound as somebody in the same namespace as
701 * the entry, OR the given dn matches the dn pattern
704 * NOTE: styles "anonymous", "users" and "self"
705 * have been moved to enum slap_style_t, whose
706 * value is set in a_dn_style; however, the string
707 * is maintained in a_dn_pat.
710 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
711 if ( !BER_BVISEMPTY( opndn ) ) {
715 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
716 if ( BER_BVISEMPTY( opndn ) ) {
720 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
721 struct berval ndn, selfndn;
724 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &ak->ak_e->e_nname ) ) {
728 level = bdn->a_self_level;
731 ndn = ak->ak_e->e_nname;
736 selfndn = ak->ak_e->e_nname;
739 for ( ; level > 0; level-- ) {
740 if ( BER_BVISEMPTY( &ndn ) ) {
743 dnParent( &ndn, &ndn );
746 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
751 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
752 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
753 AclRegexMatches tmp_matches,
754 *tmp_matchesp = &tmp_matches;
756 regmatch_t *tmp_data;
758 MATCHES_MEMSET( &tmp_matches );
759 tmp_data = &tmp_matches.dn_data[0];
761 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
762 tmp_matchesp = matches;
763 else switch ( a->acl_dn_style ) {
764 case ACL_STYLE_REGEX:
765 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
766 tmp_matchesp = matches;
769 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
772 tmp_data[0].rm_so = 0;
773 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
774 tmp_matches.dn_count = 1;
778 case ACL_STYLE_SUBTREE:
779 case ACL_STYLE_CHILDREN:
780 tmp_data[0].rm_so = 0;
781 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
782 tmp_data[1].rm_so = ak->ak_e->e_nname.bv_len - a->acl_dn_pat.bv_len;
783 tmp_data[1].rm_eo = ak->ak_e->e_nname.bv_len;
784 tmp_matches.dn_count = 2;
797 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
798 &ak->ak_e->e_nname, NULL, tmp_matchesp ) )
806 ber_len_t patlen, odnlen;
809 if ( ak->ak_e->e_dn == NULL )
812 if ( bdn->a_expand ) {
814 char buf[ACL_BUF_SIZE];
816 AclRegexMatches tmp_matches,
817 *tmp_matchesp = &tmp_matches;
819 regmatch_t *tmp_data;
821 MATCHES_MEMSET( &tmp_matches );
822 tmp_data = &tmp_matches.dn_data[0];
824 bv.bv_len = sizeof( buf ) - 1;
827 /* Expand value regex */
828 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
829 tmp_matchesp = matches;
830 else switch ( a->acl_dn_style ) {
831 case ACL_STYLE_REGEX:
832 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
833 tmp_matchesp = matches;
836 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
839 tmp_data[0].rm_so = 0;
840 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
841 tmp_matches.dn_count = 1;
845 case ACL_STYLE_SUBTREE:
846 case ACL_STYLE_CHILDREN:
847 tmp_data[0].rm_so = 0;
848 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
849 tmp_data[1].rm_so = ak->ak_e->e_nname.bv_len - a->acl_dn_pat.bv_len;
850 tmp_data[1].rm_eo = ak->ak_e->e_nname.bv_len;
851 tmp_matches.dn_count = 2;
864 if ( acl_string_expand( &bv, &bdn->a_pat,
866 ak->ak_val, tmp_matchesp ) )
871 if ( dnNormalize(0, NULL, NULL, &bv,
872 &pat, op->o_tmpmemctx )
875 /* did not expand to a valid dn */
884 odnlen = opndn->bv_len;
885 if ( odnlen < patlen ) {
886 goto dn_match_cleanup;
890 if ( bdn->a_style == ACL_STYLE_BASE ) {
891 /* base dn -- entire object DN must match */
892 if ( odnlen != patlen ) {
893 goto dn_match_cleanup;
896 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
897 ber_len_t rdnlen = 0;
899 if ( odnlen <= patlen ) {
900 goto dn_match_cleanup;
903 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
904 goto dn_match_cleanup;
907 rdnlen = dn_rdnlen( NULL, opndn );
908 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
909 goto dn_match_cleanup;
912 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
913 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
914 goto dn_match_cleanup;
917 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
918 if ( odnlen <= patlen ) {
919 goto dn_match_cleanup;
922 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
923 goto dn_match_cleanup;
926 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
927 int level = bdn->a_level;
930 if ( odnlen <= patlen ) {
931 goto dn_match_cleanup;
934 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
936 goto dn_match_cleanup;
940 for ( ; level > 0; level-- ) {
941 if ( BER_BVISEMPTY( &ndn ) ) {
942 goto dn_match_cleanup;
944 dnParent( &ndn, &ndn );
945 if ( ndn.bv_len < patlen ) {
946 goto dn_match_cleanup;
950 if ( ndn.bv_len != patlen ) {
951 goto dn_match_cleanup;
955 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
958 if ( pat.bv_val != bdn->a_pat.bv_val ) {
959 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
977 struct berval *opndn )
983 const char *attr = bdn->a_at->ad_cname.bv_val;
985 assert( attr != NULL );
987 if ( BER_BVISEMPTY( opndn ) ) {
991 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
994 /* see if asker is listed in dnattr */
995 for ( at = attrs_find( ak->ak_e->e_attrs, bdn->a_at );
997 at = attrs_find( at->a_next, bdn->a_at ) )
999 if ( attr_valfind( at,
1000 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1001 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1002 &bv, NULL, op->o_tmpmemctx ) == 0 )
1011 /* have a dnattr match. if this is a self clause then
1012 * the target must also match the op dn.
1014 if ( bdn->a_self ) {
1015 /* check if the target is an attribute. */
1016 if ( ak->ak_val == NULL ) return 1;
1018 /* target is attribute, check if the attribute value
1021 rc = value_match( &match, bdn->a_at,
1022 bdn->a_at->ad_type->sat_equality, 0,
1023 ak->ak_val, &bv, &text );
1024 /* on match error or no match, fail the ACL clause */
1025 if ( rc != LDAP_SUCCESS || match != 0 )
1030 /* no dnattr match, check if this is a self clause */
1031 if ( ! bdn->a_self )
1034 ACL_RECORD_VALUE_STATE;
1036 /* this is a self clause, check if the target is an
1039 if ( ak->ak_val == NULL )
1042 /* target is attribute, check if the attribute value
1045 rc = value_match( &match, bdn->a_at,
1046 bdn->a_at->ad_type->sat_equality, 0,
1047 ak->ak_val, &bv, &text );
1049 /* on match error or no match, fail the ACL clause */
1050 if ( rc != LDAP_SUCCESS || match != 0 )
1059 * slap_acl_mask - modifies mask based upon the given acl and the
1060 * requested access to entry e, attribute attr, value val. if val
1061 * is null, access to the whole attribute is assumed (all values).
1063 * returns 0 access NOT allowed
1067 static slap_control_t
1072 AclRegexMatches *matches,
1078 char accessmaskbuf[ACCESSMASK_MAXLEN];
1082 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1083 #endif /* SLAP_DYNACL */
1085 assert( a != NULL );
1086 assert( ak->ak_desc != NULL );
1088 attr = ak->ak_desc->ad_cname.bv_val;
1090 assert( attr != NULL );
1092 Debug( LDAP_DEBUG_ACL,
1093 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1094 ak->ak_e->e_dn, attr, 0 );
1096 Debug( LDAP_DEBUG_ACL,
1097 "=> acl_mask: to %s by \"%s\", (%s) \n",
1098 ak->ak_val ? "value" : "all values",
1099 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1100 accessmask2str( ak->ak_mask, accessmaskbuf, 1 ) );
1105 for ( ; b != NULL; b = b->a_next, i++ ) {
1106 slap_mask_t oldmask, modmask;
1108 ACL_INVALIDATE( modmask );
1110 /* AND <who> clauses */
1111 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1112 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1113 b->a_dn_pat.bv_val, 0, 0);
1115 * if access applies to the entry itself, and the
1116 * user is bound as somebody in the same namespace as
1117 * the entry, OR the given dn matches the dn pattern
1120 * NOTE: styles "anonymous", "users" and "self"
1121 * have been moved to enum slap_style_t, whose
1122 * value is set in a_dn_style; however, the string
1123 * is maintained in a_dn_pat.
1126 if ( acl_mask_dn( op, ak, a, matches,
1127 &b->a_dn, &op->o_ndn ) )
1133 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1136 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1137 b->a_realdn_pat.bv_val, 0, 0);
1139 * if access applies to the entry itself, and the
1140 * user is bound as somebody in the same namespace as
1141 * the entry, OR the given dn matches the dn pattern
1144 * NOTE: styles "anonymous", "users" and "self"
1145 * have been moved to enum slap_style_t, whose
1146 * value is set in a_dn_style; however, the string
1147 * is maintained in a_dn_pat.
1150 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1152 ndn = op->o_conn->c_ndn;
1157 if ( acl_mask_dn( op, ak, a, matches,
1158 &b->a_realdn, &ndn ) )
1164 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1165 if ( ! op->o_conn->c_listener ) {
1168 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1169 b->a_sockurl_pat.bv_val, 0, 0 );
1171 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1172 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1173 if ( !regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1174 &ak->ak_e->e_nname, ak->ak_val, matches ) )
1179 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1181 char buf[ACL_BUF_SIZE];
1183 bv.bv_len = sizeof( buf ) - 1;
1185 if ( acl_string_expand( &bv, &b->a_sockurl_pat, &ak->ak_e->e_nname, ak->ak_val, matches ) )
1190 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1196 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1204 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1205 if ( !op->o_conn->c_peer_domain.bv_val ) {
1208 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1209 b->a_domain_pat.bv_val, 0, 0 );
1210 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1211 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1212 if ( !regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1213 &ak->ak_e->e_nname, ak->ak_val, matches ) )
1218 char buf[ACL_BUF_SIZE];
1220 struct berval cmp = op->o_conn->c_peer_domain;
1221 struct berval pat = b->a_domain_pat;
1223 if ( b->a_domain_expand ) {
1226 bv.bv_len = sizeof(buf) - 1;
1229 if ( acl_string_expand(&bv, &b->a_domain_pat, &ak->ak_e->e_nname, ak->ak_val, matches) )
1236 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1237 int offset = cmp.bv_len - pat.bv_len;
1242 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1246 /* trim the domain */
1247 cmp.bv_val = &cmp.bv_val[ offset ];
1248 cmp.bv_len -= offset;
1251 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1258 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1259 if ( !op->o_conn->c_peer_name.bv_val ) {
1262 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1263 b->a_peername_pat.bv_val, 0, 0 );
1264 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1265 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1266 if ( !regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1267 &ak->ak_e->e_nname, ak->ak_val, matches ) )
1273 /* try exact match */
1274 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1275 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1279 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1281 char buf[ACL_BUF_SIZE];
1283 bv.bv_len = sizeof( buf ) - 1;
1285 if ( acl_string_expand( &bv, &b->a_peername_pat, &ak->ak_e->e_nname, ak->ak_val, matches ) )
1290 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1294 /* extract IP and try exact match */
1295 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1297 char buf[STRLENOF("255.255.255.255") + 1];
1300 int port_number = -1;
1302 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1303 acl_bv_ip_eq.bv_val,
1304 acl_bv_ip_eq.bv_len ) != 0 )
1307 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1308 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1310 port = strrchr( ip.bv_val, ':' );
1312 ip.bv_len = port - ip.bv_val;
1314 if ( lutil_atoi( &port_number, port ) != 0 )
1318 /* the port check can be anticipated here */
1319 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1322 /* address longer than expected? */
1323 if ( ip.bv_len >= sizeof(buf) )
1326 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1327 buf[ ip.bv_len ] = '\0';
1329 addr = inet_addr( buf );
1331 /* unable to convert? */
1332 if ( addr == (unsigned long)(-1) )
1335 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1338 #ifdef LDAP_PF_INET6
1339 /* extract IPv6 and try exact match */
1340 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1342 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1344 struct in6_addr addr;
1345 int port_number = -1;
1347 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1348 acl_bv_ipv6_eq.bv_val,
1349 acl_bv_ipv6_eq.bv_len ) != 0 )
1352 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1353 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1355 port = strrchr( ip.bv_val, ']' );
1357 ip.bv_len = port - ip.bv_val;
1359 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1363 /* the port check can be anticipated here */
1364 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1367 /* address longer than expected? */
1368 if ( ip.bv_len >= sizeof(buf) )
1371 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1372 buf[ ip.bv_len ] = '\0';
1374 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1378 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1380 #endif /* LDAP_PF_INET6 */
1382 #ifdef LDAP_PF_LOCAL
1383 /* extract path and try exact match */
1384 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1387 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1388 acl_bv_path_eq.bv_val,
1389 acl_bv_path_eq.bv_len ) != 0 )
1392 path.bv_val = op->o_conn->c_peer_name.bv_val
1393 + acl_bv_path_eq.bv_len;
1394 path.bv_len = op->o_conn->c_peer_name.bv_len
1395 - acl_bv_path_eq.bv_len;
1397 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1400 #endif /* LDAP_PF_LOCAL */
1402 /* exact match (very unlikely...) */
1403 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1410 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1411 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1414 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1415 b->a_sockname_pat.bv_val, 0, 0 );
1416 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1417 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1418 if ( !regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1419 &ak->ak_e->e_nname, ak->ak_val, matches ) )
1424 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1426 char buf[ACL_BUF_SIZE];
1428 bv.bv_len = sizeof( buf ) - 1;
1430 if ( acl_string_expand( &bv, &b->a_sockname_pat, &ak->ak_e->e_nname, ak->ak_val, matches ) )
1435 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1440 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1447 if ( b->a_dn_at != NULL ) {
1448 if ( acl_mask_dnattr( op, ak, a,
1450 &b->a_dn, &op->o_ndn ) )
1456 if ( b->a_realdn_at != NULL ) {
1459 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1461 ndn = op->o_conn->c_ndn;
1466 if ( acl_mask_dnattr( op, ak, a,
1468 &b->a_realdn, &ndn ) )
1474 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1476 struct berval ndn = BER_BVNULL;
1479 if ( op->o_ndn.bv_len == 0 ) {
1483 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1484 b->a_group_pat.bv_val, 0, 0 );
1486 /* b->a_group is an unexpanded entry name, expanded it should be an
1487 * entry with objectclass group* and we test to see if odn is one of
1488 * the values in the attribute group
1490 /* see if asker is listed in dnattr */
1491 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1492 char buf[ACL_BUF_SIZE];
1493 AclRegexMatches tmp_matches,
1494 *tmp_matchesp = &tmp_matches;
1495 regmatch_t *tmp_data;
1497 MATCHES_MEMSET( &tmp_matches );
1498 tmp_data = &tmp_matches.dn_data[0];
1500 bv.bv_len = sizeof(buf) - 1;
1505 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1506 tmp_matchesp = matches;
1507 else switch ( a->acl_dn_style ) {
1508 case ACL_STYLE_REGEX:
1509 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1510 tmp_matchesp = matches;
1514 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1515 case ACL_STYLE_BASE:
1516 tmp_data[0].rm_so = 0;
1517 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
1518 tmp_matches.dn_count = 1;
1522 case ACL_STYLE_SUBTREE:
1523 case ACL_STYLE_CHILDREN:
1524 tmp_data[0].rm_so = 0;
1525 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
1527 tmp_data[1].rm_so = ak->ak_e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1528 tmp_data[1].rm_eo = ak->ak_e->e_nname.bv_len;
1529 tmp_matches.dn_count = 2;
1542 if ( acl_string_expand( &bv, &b->a_group_pat,
1543 &ak->ak_e->e_nname, ak->ak_val,
1549 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1550 op->o_tmpmemctx ) != LDAP_SUCCESS )
1552 /* did not expand to a valid dn */
1559 bv = b->a_group_pat;
1562 rc = backend_group( op, ak->ak_e, &bv, &op->o_ndn,
1563 b->a_group_oc, b->a_group_at );
1566 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1574 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1576 char buf[ACL_BUF_SIZE];
1578 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1579 b->a_set_pat.bv_val, 0, 0 );
1581 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1582 AclRegexMatches tmp_matches,
1583 *tmp_matchesp = &tmp_matches;
1585 regmatch_t *tmp_data;
1587 MATCHES_MEMSET( &tmp_matches );
1588 tmp_data = &tmp_matches.dn_data[0];
1590 bv.bv_len = sizeof( buf ) - 1;
1595 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1596 tmp_matchesp = matches;
1597 else switch ( a->acl_dn_style ) {
1598 case ACL_STYLE_REGEX:
1599 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1600 tmp_matchesp = matches;
1604 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1605 case ACL_STYLE_BASE:
1606 tmp_data[0].rm_so = 0;
1607 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
1608 tmp_matches.dn_count = 1;
1612 case ACL_STYLE_SUBTREE:
1613 case ACL_STYLE_CHILDREN:
1614 tmp_data[0].rm_so = 0;
1615 tmp_data[0].rm_eo = ak->ak_e->e_nname.bv_len;
1616 tmp_data[1].rm_so = ak->ak_e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1617 tmp_data[1].rm_eo = ak->ak_e->e_nname.bv_len; tmp_matches.dn_count = 2;
1630 if ( acl_string_expand( &bv, &b->a_set_pat,
1631 &ak->ak_e->e_nname, ak->ak_val,
1641 if ( acl_match_set( &bv, op, ak->ak_e, NULL ) == 0 ) {
1646 if ( b->a_authz.sai_ssf ) {
1647 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1648 b->a_authz.sai_ssf, op->o_ssf, 0 );
1649 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1654 if ( b->a_authz.sai_transport_ssf ) {
1655 Debug( LDAP_DEBUG_ACL,
1656 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1657 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1658 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1663 if ( b->a_authz.sai_tls_ssf ) {
1664 Debug( LDAP_DEBUG_ACL,
1665 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1666 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1667 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1672 if ( b->a_authz.sai_sasl_ssf ) {
1673 Debug( LDAP_DEBUG_ACL,
1674 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1675 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1676 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1681 /* check for the "self" modifier in the <access> field */
1682 if ( b->a_dn.a_self ) {
1686 ACL_RECORD_VALUE_STATE;
1688 /* must have DN syntax */
1689 if ( ak->ak_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1690 !is_at_syntax( ak->ak_desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1692 /* check if the target is an attribute. */
1693 if ( ak->ak_val == NULL ) continue;
1695 /* a DN must be present */
1696 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1700 /* target is attribute, check if the attribute value
1703 rc = value_match( &match, ak->ak_desc,
1704 ak->ak_desc->ad_type->sat_equality, 0,
1705 ak->ak_val, &op->o_ndn, &dummy );
1706 /* on match error or no match, fail the ACL clause */
1707 if ( rc != LDAP_SUCCESS || match != 0 )
1712 if ( b->a_dynacl ) {
1714 slap_access_t tgrant, tdeny;
1716 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1719 /* this case works different from the others above.
1720 * since dynamic ACL's themselves give permissions, we need
1721 * to first check b->a_access_mask, the ACL's access level.
1723 /* first check if the right being requested
1724 * is allowed by the ACL clause.
1726 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1730 /* start out with nothing granted, nothing denied */
1731 ACL_INVALIDATE(tgrant);
1732 ACL_INVALIDATE(tdeny);
1734 for ( da = b->a_dynacl; da; da = da->da_next ) {
1735 slap_access_t grant,
1738 ACL_INVALIDATE(grant);
1739 ACL_INVALIDATE(deny);
1741 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1742 da->da_name, 0, 0 );
1745 * XXXmanu Only DN matches are supplied
1746 * sending attribute values matches require
1749 (void)da->da_mask( da->da_private, op, ak->ak_e, ak->ak_desc,
1750 ak->ak_val, matches->dn_count, matches->dn_data,
1757 /* remove anything that the ACL clause does not allow */
1758 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1759 tdeny &= ACL_PRIV_MASK;
1761 /* see if we have anything to contribute */
1762 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1766 /* this could be improved by changing slap_acl_mask so that it can deal with
1767 * by clauses that return grant/deny pairs. Right now, it does either
1768 * additive or subtractive rights, but not both at the same time. So,
1769 * we need to combine the grant/deny pair into a single rights mask in
1770 * a smart way: if either grant or deny is "empty", then we use the
1771 * opposite as is, otherwise we remove any denied rights from the grant
1772 * rights mask and construct an additive mask.
1774 if (ACL_IS_INVALID(tdeny)) {
1775 modmask = tgrant | ACL_PRIV_ADDITIVE;
1777 } else if (ACL_IS_INVALID(tgrant)) {
1778 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1781 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1785 #endif /* SLAP_DYNACL */
1787 modmask = b->a_access_mask;
1790 Debug( LDAP_DEBUG_ACL,
1791 "<= acl_mask: [%d] applying %s (%s)\n",
1792 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1793 b->a_type == ACL_CONTINUE
1795 : b->a_type == ACL_BREAK
1799 oldmask = ak->ak_mask;
1801 if( ACL_IS_ADDITIVE(modmask) ) {
1803 ACL_PRIV_SET( ak->ak_mask, modmask );
1806 ACL_PRIV_CLR( ak->ak_mask, ~ACL_PRIV_MASK );
1808 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1809 /* substract privs */
1810 ACL_PRIV_CLR( ak->ak_mask, modmask );
1813 ACL_PRIV_CLR( ak->ak_mask, ~ACL_PRIV_MASK );
1817 ak->ak_mask = modmask;
1820 Debug( LDAP_DEBUG_ACL,
1821 "<= acl_mask: [%d] mask: %s\n",
1822 i, accessmask2str(ak->ak_mask, accessmaskbuf, 1), 0 );
1824 if( b->a_type == ACL_CONTINUE ) {
1827 } else if ( b->a_type == ACL_BREAK ) {
1835 /* implicit "by * none" clause */
1836 ACL_INIT(ak->ak_mask);
1838 Debug( LDAP_DEBUG_ACL,
1839 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1840 accessmask2str(ak->ak_mask, accessmaskbuf, 1), 0, 0 );
1845 * acl_check_modlist - check access control on the given entry to see if
1846 * it allows the given modifications by the user associated with op.
1847 * returns 1 if mods allowed ok
1848 * 0 mods not allowed
1855 Modifications *mlist )
1857 AccessControlState state = ACL_STATE_INIT;
1861 int ret = 1; /* default is access allowed */
1865 be = LDAP_STAILQ_FIRST(&backendDB);
1869 assert( be != NULL );
1871 /* If ADD attribute checking is not enabled, just allow it */
1872 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1875 /* short circuit root database access */
1876 if ( be_isroot( op ) ) {
1877 Debug( LDAP_DEBUG_ACL,
1878 "<= acl_access_allowed: granted to database root\n",
1883 /* use backend default access if no backend acls */
1884 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1885 Debug( LDAP_DEBUG_ACL,
1886 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1887 access2str( ACL_WRITE ),
1888 op->o_bd->be_dfltaccess >= ACL_WRITE
1889 ? "granted" : "denied",
1891 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1896 ak.ak_state = &state;
1898 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1900 * Internal mods are ignored by ACL_WRITE checking
1902 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1903 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1904 " modify access granted\n",
1905 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1910 * no-user-modification operational attributes are ignored
1911 * by ACL_WRITE checking as any found here are not provided
1914 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1915 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1917 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1918 " modify access granted\n",
1919 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1923 ak.ak_desc = mlist->sml_desc;
1924 switch ( mlist->sml_op ) {
1925 case LDAP_MOD_REPLACE:
1926 case LDAP_MOD_INCREMENT:
1928 * We must check both permission to delete the whole
1929 * attribute and permission to add the specific attributes.
1930 * This prevents abuse from selfwriters.
1933 ak.ak_access = ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL;
1934 if ( ! access_allowed( op, &ak ))
1940 if ( mlist->sml_values == NULL ) break;
1942 /* fall thru to check value to add */
1945 assert( mlist->sml_values != NULL );
1946 ak.ak_access = ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD;
1948 for ( ak.ak_val = mlist->sml_nvalues
1949 ? mlist->sml_nvalues : mlist->sml_values;
1950 ak.ak_val->bv_val != NULL; ak.ak_val++ )
1952 if ( ! access_allowed( op, &ak ))
1960 case LDAP_MOD_DELETE:
1961 if ( mlist->sml_values == NULL ) {
1963 ak.ak_access = ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL;
1964 if ( ! access_allowed( op, &ak ))
1971 for ( ak.ak_val = mlist->sml_nvalues
1972 ? mlist->sml_nvalues : mlist->sml_values;
1973 ak.ak_val->bv_val != NULL; ak.ak_val++ )
1975 if ( ! access_allowed( op, &ak ))
1983 case SLAP_MOD_SOFTADD:
1984 /* allow adding attribute via modrdn thru */
1996 if (be_null) op->o_bd = NULL;
2002 struct berval *list,
2015 while ( len >= 0 && --ix >= 0 ) {
2016 while ( --len >= 0 && *p++ != sep )
2019 while ( len >= 0 && *p == ' ' ) {
2032 while ( --len >= 0 && *p != sep ) {
2036 while ( bv->bv_len > 0 && *--p == ' ' ) {
2043 typedef struct acl_set_gather_t {
2049 acl_set_cb_gather( Operation *op, SlapReply *rs )
2051 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2053 if ( rs->sr_type == REP_SEARCH ) {
2054 BerValue bvals[ 2 ];
2055 BerVarray bvalsp = NULL;
2058 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2059 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2061 if ( desc == NULL ) {
2065 if ( desc == slap_schema.si_ad_entryDN ) {
2067 bvals[ 0 ] = rs->sr_entry->e_nname;
2068 BER_BVZERO( &bvals[ 1 ] );
2073 a = attr_find( rs->sr_entry->e_attrs, desc );
2075 bvalsp = a->a_nvals;
2080 p->bvals = slap_set_join( p->cookie, p->bvals,
2081 ( '|' | SLAP_SET_RREF ), bvalsp );
2086 assert( rs->sr_type == REP_RESULT );
2093 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2095 AclSetCookie *cp = (AclSetCookie *)cookie;
2097 LDAPURLDesc *ludp = NULL;
2098 Operation op2 = { 0 };
2099 SlapReply rs = {REP_RESULT};
2100 AttributeName anlist[ 2 ], *anlistp = NULL;
2102 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2103 acl_set_gather_t p = { 0 };
2105 /* this routine needs to return the bervals instead of
2106 * plain strings, since syntax is not known. It should
2107 * also return the syntax or some "comparison cookie".
2109 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2110 return acl_set_gather2( cookie, name, desc );
2113 rc = ldap_url_parse( name->bv_val, &ludp );
2114 if ( rc != LDAP_URL_SUCCESS ) {
2115 Debug( LDAP_DEBUG_TRACE,
2116 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2117 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2119 rc = LDAP_PROTOCOL_ERROR;
2123 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2125 /* host part must be empty */
2126 /* extensions parts must be empty */
2127 Debug( LDAP_DEBUG_TRACE,
2128 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2129 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2131 rc = LDAP_PROTOCOL_ERROR;
2135 /* Grab the searchbase and see if an appropriate database can be found */
2136 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2137 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2138 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2139 BER_BVZERO( &op2.o_req_dn );
2140 if ( rc != LDAP_SUCCESS ) {
2141 Debug( LDAP_DEBUG_TRACE,
2142 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2143 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2148 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2149 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2150 Debug( LDAP_DEBUG_TRACE,
2151 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2152 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2154 rc = LDAP_NO_SUCH_OBJECT;
2158 /* Grab the filter */
2159 if ( ludp->lud_filter ) {
2160 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2161 cp->asc_op->o_tmpmemctx );
2162 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2163 if ( op2.ors_filter == NULL ) {
2164 Debug( LDAP_DEBUG_TRACE,
2165 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2166 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2168 rc = LDAP_PROTOCOL_ERROR;
2173 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2174 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2178 /* Grab the scope */
2179 op2.ors_scope = ludp->lud_scope;
2181 /* Grap the attributes */
2182 if ( ludp->lud_attrs ) {
2185 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2188 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2189 cp->asc_op->o_tmpmemctx );
2191 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2193 AttributeDescription *desc = NULL;
2194 const char *text = NULL;
2196 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2197 rc = slap_bv2ad( &name, &desc, &text );
2198 if ( rc == LDAP_SUCCESS ) {
2199 anlistp[ nattrs ].an_name = name;
2200 anlistp[ nattrs ].an_desc = desc;
2209 anlistp[ nattrs ].an_name = desc->ad_cname;
2210 anlistp[ nattrs ].an_desc = desc;
2212 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2216 op2.o_hdr = cp->asc_op->o_hdr;
2217 op2.o_tag = LDAP_REQ_SEARCH;
2218 op2.o_ndn = op2.o_bd->be_rootndn;
2219 op2.o_callback = &cb;
2220 slap_op_time( &op2.o_time, &op2.o_tincr );
2221 op2.o_do_not_cache = 1;
2222 op2.o_is_auth_check = 0;
2223 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2224 op2.ors_slimit = SLAP_NO_LIMIT;
2225 op2.ors_tlimit = SLAP_NO_LIMIT;
2226 op2.ors_attrs = anlistp;
2227 op2.ors_attrsonly = 0;
2228 op2.o_private = cp->asc_op->o_private;
2229 op2.o_extra = cp->asc_op->o_extra;
2233 rc = op2.o_bd->be_search( &op2, &rs );
2239 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2240 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2242 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2243 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2245 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2246 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2249 ldap_free_urldesc( ludp );
2251 if ( anlistp && anlistp != anlist ) {
2252 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2259 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2261 AclSetCookie *cp = (AclSetCookie *)cookie;
2262 BerVarray bvals = NULL;
2266 /* this routine needs to return the bervals instead of
2267 * plain strings, since syntax is not known. It should
2268 * also return the syntax or some "comparison cookie".
2270 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2271 if ( rc == LDAP_SUCCESS ) {
2272 if ( desc == slap_schema.si_ad_entryDN ) {
2273 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2274 cp->asc_op->o_tmpmemctx );
2276 BER_BVZERO( &bvals[ 1 ] );
2280 backend_attribute( cp->asc_op,
2281 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2284 if ( !BER_BVISNULL( &ndn ) ) {
2285 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2294 struct berval *subj,
2297 struct berval *default_set_attribute )
2299 struct berval set = BER_BVNULL;
2301 AclSetCookie cookie;
2303 if ( default_set_attribute == NULL ) {
2307 struct berval subjdn, ndn = BER_BVNULL;
2308 struct berval setat;
2309 BerVarray bvals = NULL;
2311 AttributeDescription *desc = NULL;
2313 /* format of string is "entry/setAttrName" */
2314 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2318 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2319 setat = *default_set_attribute;
2323 * NOTE: dnNormalize honors the ber_len field
2324 * as the length of the dn to be normalized
2326 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2327 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2329 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2330 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2334 BER_BVZERO( &bvals[0] );
2335 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2337 bvals[0].bv_val = bvals[i-1].bv_val;
2338 BER_BVZERO( &bvals[i-1] );
2340 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2341 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2346 if ( !BER_BVISNULL( &set ) ) {
2349 rc = ( slap_set_filter(
2351 (SetCookie *)&cookie, &set,
2352 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2353 if ( set.bv_val != subj->bv_val ) {
2354 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2364 * dynamic ACL infrastructure
2366 static slap_dynacl_t *da_list = NULL;
2369 slap_dynacl_register( slap_dynacl_t *da )
2373 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2374 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2379 if ( tmp != NULL ) {
2383 if ( da->da_mask == NULL ) {
2387 da->da_private = NULL;
2388 da->da_next = da_list;
2394 static slap_dynacl_t *
2395 slap_dynacl_next( slap_dynacl_t *da )
2404 slap_dynacl_get( const char *name )
2408 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2409 if ( strcasecmp( da->da_name, name ) == 0 ) {
2416 #endif /* SLAP_DYNACL */
2419 * statically built-in dynamic ACL initialization
2421 static int (*acl_init_func[])( void ) = {
2423 /* TODO: remove when ACI will only be dynamic */
2424 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2426 #endif /* SLAPD_ACI_ENABLED */
2427 #endif /* SLAP_DYNACL */
2437 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2438 rc = (*(acl_init_func[ i ]))();
2451 struct berval *dn_matches,
2452 struct berval *val_matches,
2453 AclRegexMatches *matches)
2459 enum { DN_FLAG, VAL_FLAG } tflag;
2462 bv->bv_val[0] = '\0';
2463 bv->bv_len--; /* leave space for lone $ */
2467 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2468 sp < pat->bv_val + pat->bv_len ; sp++ )
2470 /* did we previously see a $ */
2472 if ( flag == 1 && *sp == '$' ) {
2478 } else if ( flag == 2 && *sp == 'v' /*'}'*/) {
2481 } else if ( flag == 2 && *sp == 'd' /*'}'*/) {
2484 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2487 } else if ( *sp >= '0' && *sp <= '9' ) {
2498 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2499 if ( *sp >= '0' && *sp <= '9' ) {
2500 n = 10*n + ( *sp - '0' );
2504 if ( *sp != /*'{'*/ '}' ) {
2512 nm = matches->dn_count;
2513 m = matches->dn_data;
2514 data = dn_matches ? dn_matches->bv_val : NULL;
2517 nm = matches->val_count;
2518 m = matches->val_data;
2519 data = val_matches ? val_matches->bv_val : NULL;
2528 if ( data == NULL ) {
2537 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2556 /* must have ended with a single $ */
2564 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2565 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2572 struct berval *pat, /* pattern to expand and match against */
2573 char *str, /* string to match against pattern */
2574 struct berval *dn_matches, /* buffer with $N expansion variables from DN */
2575 struct berval *val_matches, /* buffer with $N expansion variables from val */
2576 AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
2580 char newbuf[ACL_BUF_SIZE];
2584 bv.bv_len = sizeof( newbuf ) - 1;
2591 acl_string_expand( &bv, pat, dn_matches, val_matches, matches );
2592 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2594 char error[ACL_BUF_SIZE];
2595 regerror( rc, &re, error, sizeof( error ) );
2597 Debug( LDAP_DEBUG_TRACE,
2598 "compile( \"%s\", \"%s\") failed %s\n",
2599 pat->bv_val, str, error );
2603 rc = regexec( &re, str, 0, NULL, 0 );
2606 Debug( LDAP_DEBUG_TRACE,
2607 "=> regex_matches: string: %s\n", str, 0, 0 );
2608 Debug( LDAP_DEBUG_TRACE,
2609 "=> regex_matches: rc: %d %s\n",
2610 rc, !rc ? "matches" : "no matches", 0 );