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-2008 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 AccessControl *ac, int *count,
52 Operation *op, Entry *e,
53 AttributeDescription *desc,
55 AclRegexMatches *matches,
56 AccessControlState *state );
58 static slap_control_t slap_acl_mask(
59 AccessControl *ac, slap_mask_t *mask,
60 Operation *op, Entry *e,
61 AttributeDescription *desc,
63 AclRegexMatches *matches,
65 AccessControlState *state,
66 slap_access_t access );
68 static int regex_matches(
69 struct berval *pat, char *str, char *buf,
70 AclRegexMatches *matches);
72 typedef struct AclSetCookie {
74 #define asc_op asc_cookie.set_op
79 SLAP_SET_GATHER acl_set_gather;
80 SLAP_SET_GATHER acl_set_gather2;
83 * access_allowed - check whether op->o_ndn is allowed the requested access
84 * to entry e, attribute attr, value val. if val is null, access to
85 * the whole attribute is assumed (all values).
87 * This routine loops through all access controls and calls
88 * slap_acl_mask() on each applicable access control.
89 * The loop exits when a definitive answer is reached or
90 * or no more controls remain.
97 * - can be legally called with op == NULL
98 * - can be legally called with op->o_bd == NULL
102 slap_access_always_allowed(
105 AttributeDescription *desc,
107 slap_access_t access,
108 AccessControlState *state,
111 assert( maskp != NULL );
114 ACL_LVL_ASSIGN_MANAGE( *maskp );
119 #define MATCHES_DNMAXCOUNT(m) \
120 ( sizeof ( (m)->dn_data ) / sizeof( *(m)->dn_data ) )
121 #define MATCHES_VALMAXCOUNT(m) \
122 ( sizeof ( (m)->val_data ) / sizeof( *(m)->val_data ) )
123 #define MATCHES_MEMSET(m) do { \
124 memset( (m)->dn_data, '\0', sizeof( (m)->dn_data ) ); \
125 memset( (m)->val_data, '\0', sizeof( (m)->val_data ) ); \
126 (m)->dn_count = MATCHES_DNMAXCOUNT( (m) ); \
127 (m)->val_count = MATCHES_VALMAXCOUNT( (m) ); \
128 } while ( 0 /* CONSTCOND */ )
134 AttributeDescription *desc,
136 slap_access_t access,
137 AccessControlState *state,
142 AccessControl *a = NULL;
145 char accessmaskbuf[ACCESSMASK_MAXLEN];
148 slap_control_t control;
149 slap_access_t access_level;
151 AclRegexMatches matches;
152 AccessControlState acl_state = ACL_STATE_INIT;
154 assert( op != NULL );
156 assert( desc != NULL );
157 assert( maskp != NULL );
159 access_level = ACL_LEVEL( access );
160 attr = desc->ad_cname.bv_val;
162 assert( attr != NULL );
166 /* grant database root access */
167 if ( be_isroot( op ) ) {
168 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
169 mask = ACL_LVL_MANAGE;
174 * no-user-modification operational attributes are ignored
175 * by ACL_WRITE checking as any found here are not provided
178 * NOTE: but they are not ignored for ACL_MANAGE, because
179 * if we get here it means a non-root user is trying to
180 * manage data, so we need to check its privileges.
182 if ( access_level == ACL_WRITE
183 && is_at_no_user_mod( desc->ad_type )
184 && desc != slap_schema.si_ad_entry
185 && desc != slap_schema.si_ad_children )
187 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
188 " %s access granted\n",
193 /* use backend default access if no backend acls */
194 if ( op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
197 Debug( LDAP_DEBUG_ACL,
198 "=> slap_access_allowed: backend default %s "
199 "access %s to \"%s\"\n",
200 access2str( access ),
201 op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
202 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
203 ret = op->o_bd->be_dfltaccess >= access_level;
205 mask = ACL_PRIV_LEVEL;
206 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
207 ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
218 if ( state->as_vd_ad == desc ) {
219 a = state->as_vd_acl;
220 count = state->as_vd_acl_count;
221 if ( state->as_fe_done )
224 state->as_vi_acl = NULL;
230 state->as_fe_done = 0;
232 ACL_PRIV_ASSIGN( mask, *maskp );
233 MATCHES_MEMSET( &matches );
235 while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
236 &matches, state ) ) != NULL )
239 int dnmaxcount = MATCHES_DNMAXCOUNT( &matches );
240 int valmaxcount = MATCHES_VALMAXCOUNT( &matches );
241 regmatch_t *dn_data = matches.dn_data;
242 regmatch_t *val_data = matches.val_data;
245 for ( i = 0; i < dnmaxcount && dn_data[i].rm_eo > 0; i++ ) {
246 char *debugmsg = "=> match[dn%d]: %d %d ";
247 char *data = e->e_ndn;
249 Debug( LDAP_DEBUG_ACL, debugmsg, i,
250 (int)dn_data[i].rm_so,
251 (int)dn_data[i].rm_eo );
252 if ( dn_data[i].rm_so <= dn_data[0].rm_eo ) {
254 for ( n = dn_data[i].rm_so;
255 n < dn_data[i].rm_eo; n++ ) {
256 Debug( LDAP_DEBUG_ACL, "%c",
260 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
264 for ( i = 0; i < valmaxcount && val_data[i].rm_eo > 0; i++ ) {
265 char *debugmsg = "=> match[val%d]: %d %d ";
266 char *data = val->bv_val;
268 Debug( LDAP_DEBUG_ACL, debugmsg, i,
269 (int)val_data[i].rm_so,
270 (int)val_data[i].rm_eo );
271 if ( val_data[i].rm_so <= val_data[0].rm_eo ) {
273 for ( n = val_data[i].rm_so;
274 n < val_data[i].rm_eo; n++ ) {
275 Debug( LDAP_DEBUG_ACL, "%c",
279 Debug( LDAP_DEBUG_ACL, "\n", 0, 0, 0 );
283 if ( state->as_vi_acl == a &&
284 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
286 Debug( LDAP_DEBUG_ACL,
287 "=> slap_access_allowed: result was in cache (%s)\n",
289 ret = state->as_result;
292 Debug( LDAP_DEBUG_ACL,
293 "=> slap_access_allowed: result not in cache (%s)\n",
298 control = slap_acl_mask( a, &mask, op,
299 e, desc, val, &matches, count, state, access );
301 if ( control != ACL_BREAK ) {
305 MATCHES_MEMSET( &matches );
308 if ( ACL_IS_INVALID( mask ) ) {
309 Debug( LDAP_DEBUG_ACL,
310 "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
312 ACL_PRIV_ASSIGN( mask, *maskp );
314 } else if ( control == ACL_BREAK ) {
315 Debug( LDAP_DEBUG_ACL,
316 "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
321 ret = ACL_GRANT( mask, access );
323 Debug( LDAP_DEBUG_ACL,
324 "=> slap_access_allowed: %s access %s by %s\n",
325 access2str( access ), ret ? "granted" : "denied",
326 accessmask2str( mask, accessmaskbuf, 1 ) );
329 ACL_PRIV_ASSIGN( *maskp, mask );
337 AttributeDescription *desc,
339 slap_access_t access,
340 AccessControlState *state,
347 * NOTE: control gets here if FIXME
348 * if an appropriate backend cannot be selected for the operation,
349 * we assume that the frontend should handle this
350 * FIXME: should select_backend() take care of this,
351 * and return frontendDB instead of NULL? maybe for some value
356 if ( op->o_bd == NULL ) {
357 op->o_bd = select_backend( &op->o_req_ndn, 0 );
358 if ( op->o_bd == NULL )
359 op->o_bd = frontendDB;
361 rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
371 AttributeDescription *desc,
373 slap_access_t access,
374 AccessControlState *state,
378 AccessControl *a = NULL;
382 char accessmaskbuf[ACCESSMASK_MAXLEN];
385 slap_access_t access_level;
387 static AccessControlState state_init = ACL_STATE_INIT;
390 assert( desc != NULL );
392 access_level = ACL_LEVEL( access );
394 assert( access_level > ACL_NONE );
397 if ( maskp ) ACL_INVALIDATE( *maskp );
399 attr = desc->ad_cname.bv_val;
401 assert( attr != NULL );
404 if ( op->o_acl_priv != ACL_NONE ) {
405 access = op->o_acl_priv;
407 } else if ( op->o_is_auth_check &&
408 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
412 } else if ( get_relax( op ) && access_level == ACL_WRITE &&
413 desc == slap_schema.si_ad_entry )
420 if ( state->as_vd_ad == desc ) {
421 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
424 return state->as_result;
432 Debug( LDAP_DEBUG_ACL,
433 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
434 access2str( access ), e->e_dn, attr );
441 if ( op->o_bd == NULL ) {
442 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
445 /* FIXME: experimental; use first backend rules
446 * iff there is no global_acl (ITS#3100)
448 if ( frontendDB->be_acl != NULL ) {
449 op->o_bd = frontendDB;
452 assert( op->o_bd != NULL );
454 /* this is enforced in backend_add() */
455 if ( op->o_bd->bd_info->bi_access_allowed ) {
456 /* delegate to backend */
457 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
458 desc, val, access, state, &mask );
461 /* use default (but pass through frontend
462 * for global ACL overlays) */
463 ret = frontendDB->bd_info->bi_access_allowed( op, e,
464 desc, val, access, state, &mask );
468 if ( ACL_IS_INVALID( mask ) ) {
469 Debug( LDAP_DEBUG_ACL,
470 "=> access_allowed: \"%s\" (%s) invalid!\n",
475 Debug( LDAP_DEBUG_ACL,
476 "=> access_allowed: no more rules\n", 0, 0, 0 );
482 Debug( LDAP_DEBUG_ACL,
483 "=> access_allowed: %s access %s by %s\n",
484 access2str( access ), ret ? "granted" : "denied",
485 accessmask2str( mask, accessmaskbuf, 1 ) );
488 if ( state != NULL ) {
489 /* If not value-dependent, save ACL in case of more attrs */
490 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
491 state->as_vi_acl = a;
492 state->as_result = ret;
494 state->as_recorded |= ACL_STATE_RECORDED;
495 state->as_vd_ad = desc;
497 if ( be_null ) op->o_bd = NULL;
498 if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
504 * slap_acl_get - return the acl applicable to entry e, attribute
505 * attr. the acl returned is suitable for use in subsequent calls to
506 * acl_access_allowed().
509 static AccessControl *
515 AttributeDescription *desc,
517 AclRegexMatches *matches,
518 AccessControlState *state )
525 assert( count != NULL );
526 assert( desc != NULL );
527 assert( state != NULL );
529 attr = desc->ad_cname.bv_val;
531 assert( attr != NULL );
534 if( op->o_bd == NULL || op->o_bd->be_acl == NULL ) {
535 a = frontendDB->be_acl;
537 a = op->o_bd->be_acl;
542 if ( a == frontendDB->be_acl )
543 state->as_fe_done = 1;
549 dnlen = e->e_nname.bv_len;
552 for ( ; a != NULL; prev = a, a = a->acl_next ) {
555 if ( a != frontendDB->be_acl && state->as_fe_done )
558 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
559 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
560 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
561 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
562 if ( regexec ( &a->acl_dn_re,
565 matches->dn_data, 0 ) )
569 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
570 *count, a->acl_dn_pat.bv_val, 0 );
571 patlen = a->acl_dn_pat.bv_len;
572 if ( dnlen < patlen )
575 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
576 /* base dn -- entire object DN must match */
577 if ( dnlen != patlen )
580 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
581 ber_len_t rdnlen = 0;
584 if ( dnlen <= patlen )
588 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
593 rdnlen = dn_rdnlen( NULL, &e->e_nname );
594 if ( rdnlen != dnlen - patlen - sep )
597 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
598 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
601 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
602 if ( dnlen <= patlen )
604 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
608 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
612 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
616 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
617 matches->dn_data[0].rm_so = -1;
618 matches->dn_data[0].rm_eo = -1;
619 matches->val_data[0].rm_so = -1;
620 matches->val_data[0].rm_eo = -1;
624 /* Is this ACL only for a specific value? */
625 if ( a->acl_attrval.bv_len ) {
630 if( !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
631 state->as_recorded |= ACL_STATE_RECORDED_VD;
632 state->as_vd_acl = prev;
633 state->as_vd_acl_count = *count - 1;
636 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
639 Debug( LDAP_DEBUG_ACL,
640 "acl_get: valpat %s\n",
641 a->acl_attrval.bv_val, 0, 0 );
642 if ( rc = regexec ( &a->acl_attrval_re,
645 matches->val_data, 0 ) )
653 Debug( LDAP_DEBUG_ACL,
655 a->acl_attrval.bv_val, 0, 0 );
657 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
658 if (value_match( &match, desc,
659 a->acl_attrval_mr, 0,
660 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
667 patlen = a->acl_attrval.bv_len;
668 vdnlen = val->bv_len;
670 if ( vdnlen < patlen )
673 if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
674 if ( vdnlen > patlen )
677 } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
678 ber_len_t rdnlen = 0;
680 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
683 rdnlen = dn_rdnlen( NULL, val );
684 if ( rdnlen != vdnlen - patlen - 1 )
687 } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
688 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
691 } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
692 if ( vdnlen <= patlen )
695 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
699 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ) )
705 if ( a->acl_filter != NULL ) {
706 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
707 if ( rc != LDAP_COMPARE_TRUE ) {
712 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
717 if ( !state->as_fe_done ) {
718 state->as_fe_done = 1;
719 a = frontendDB->be_acl;
723 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
728 * Record value-dependent access control state
730 #define ACL_RECORD_VALUE_STATE do { \
731 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
732 state->as_recorded |= ACL_STATE_RECORDED_VD; \
733 state->as_vd_acl = a; \
734 state->as_vd_acl_count = count; \
742 AttributeDescription *desc,
745 AclRegexMatches *matches,
747 struct berval *opndn )
750 * if access applies to the entry itself, and the
751 * user is bound as somebody in the same namespace as
752 * the entry, OR the given dn matches the dn pattern
755 * NOTE: styles "anonymous", "users" and "self"
756 * have been moved to enum slap_style_t, whose
757 * value is set in a_dn_style; however, the string
758 * is maintained in a_dn_pat.
761 if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
762 if ( !BER_BVISEMPTY( opndn ) ) {
766 } else if ( bdn->a_style == ACL_STYLE_USERS ) {
767 if ( BER_BVISEMPTY( opndn ) ) {
771 } else if ( bdn->a_style == ACL_STYLE_SELF ) {
772 struct berval ndn, selfndn;
775 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
779 level = bdn->a_self_level;
787 selfndn = e->e_nname;
790 for ( ; level > 0; level-- ) {
791 if ( BER_BVISEMPTY( &ndn ) ) {
794 dnParent( &ndn, &ndn );
797 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
802 } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
803 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
804 AclRegexMatches tmp_matches,
805 *tmp_matchesp = &tmp_matches;
808 regmatch_t *tmp_data;
810 MATCHES_MEMSET( &tmp_matches );
811 tmp_data = &tmp_matches.dn_data[0];
813 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
814 tmp_matchesp = matches;
815 else switch ( a->acl_dn_style ) {
816 case ACL_STYLE_REGEX:
817 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
818 tmp_matchesp = matches;
821 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
824 tmp_data[0].rm_so = 0;
825 tmp_data[0].rm_eo = e->e_nname.bv_len;
826 tmp_matches.dn_count = 1;
830 case ACL_STYLE_SUBTREE:
831 case ACL_STYLE_CHILDREN:
832 tmp_data[0].rm_so = 0;
833 tmp_data[0].rm_eo = e->e_nname.bv_len;
834 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
835 tmp_data[1].rm_eo = e->e_nname.bv_len;
836 tmp_matches.dn_count = 2;
849 if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
850 e->e_ndn, tmp_matchesp ) )
858 ber_len_t patlen, odnlen;
861 if ( e->e_dn == NULL )
864 if ( bdn->a_expand ) {
866 char buf[ACL_BUF_SIZE];
868 AclRegexMatches tmp_matches,
869 *tmp_matchesp = &tmp_matches;
871 regmatch_t *tmp_data;
873 MATCHES_MEMSET( &tmp_matches );
874 tmp_data = &tmp_matches.dn_data[0];
876 bv.bv_len = sizeof( buf ) - 1;
879 /* Expand value regex */
880 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
881 tmp_matchesp = matches;
882 else switch ( a->acl_dn_style ) {
883 case ACL_STYLE_REGEX:
884 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
885 tmp_matchesp = matches;
888 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
891 tmp_data[0].rm_so = 0;
892 tmp_data[0].rm_eo = e->e_nname.bv_len;
893 tmp_matches.dn_count = 1;
897 case ACL_STYLE_SUBTREE:
898 case ACL_STYLE_CHILDREN:
899 tmp_data[0].rm_so = 0;
900 tmp_data[0].rm_eo = e->e_nname.bv_len;
901 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
902 tmp_data[1].rm_eo = e->e_nname.bv_len;
903 tmp_matches.dn_count = 2;
916 if ( acl_string_expand( &bv, &bdn->a_pat,
918 val->bv_val, tmp_matchesp ) )
923 if ( dnNormalize(0, NULL, NULL, &bv,
924 &pat, op->o_tmpmemctx )
927 /* did not expand to a valid dn */
936 odnlen = opndn->bv_len;
937 if ( odnlen < patlen ) {
938 goto dn_match_cleanup;
942 if ( bdn->a_style == ACL_STYLE_BASE ) {
943 /* base dn -- entire object DN must match */
944 if ( odnlen != patlen ) {
945 goto dn_match_cleanup;
948 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
949 ber_len_t rdnlen = 0;
951 if ( odnlen <= patlen ) {
952 goto dn_match_cleanup;
955 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
956 goto dn_match_cleanup;
959 rdnlen = dn_rdnlen( NULL, opndn );
960 if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
961 goto dn_match_cleanup;
964 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
965 if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
966 goto dn_match_cleanup;
969 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
970 if ( odnlen <= patlen ) {
971 goto dn_match_cleanup;
974 if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
975 goto dn_match_cleanup;
978 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
979 int level = bdn->a_level;
982 if ( odnlen <= patlen ) {
983 goto dn_match_cleanup;
986 if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
988 goto dn_match_cleanup;
992 for ( ; level > 0; level-- ) {
993 if ( BER_BVISEMPTY( &ndn ) ) {
994 goto dn_match_cleanup;
996 dnParent( &ndn, &ndn );
997 if ( ndn.bv_len < patlen ) {
998 goto dn_match_cleanup;
1002 if ( ndn.bv_len != patlen ) {
1003 goto dn_match_cleanup;
1007 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1010 if ( pat.bv_val != bdn->a_pat.bv_val ) {
1011 slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1030 AclRegexMatches *matches,
1032 AccessControlState *state,
1033 slap_dn_access *bdn,
1034 struct berval *opndn )
1040 const char *attr = bdn->a_at->ad_cname.bv_val;
1042 assert( attr != NULL );
1044 if ( BER_BVISEMPTY( opndn ) ) {
1048 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1051 /* see if asker is listed in dnattr */
1052 for ( at = attrs_find( e->e_attrs, bdn->a_at );
1054 at = attrs_find( at->a_next, bdn->a_at ) )
1056 if ( attr_valfind( at,
1057 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1058 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1059 &bv, NULL, op->o_tmpmemctx ) == 0 )
1068 /* have a dnattr match. if this is a self clause then
1069 * the target must also match the op dn.
1071 if ( bdn->a_self ) {
1072 /* check if the target is an attribute. */
1073 if ( val == NULL ) return 1;
1075 /* target is attribute, check if the attribute value
1078 rc = value_match( &match, bdn->a_at,
1079 bdn->a_at->ad_type->sat_equality, 0,
1081 /* on match error or no match, fail the ACL clause */
1082 if ( rc != LDAP_SUCCESS || match != 0 )
1087 /* no dnattr match, check if this is a self clause */
1088 if ( ! bdn->a_self )
1091 ACL_RECORD_VALUE_STATE;
1093 /* this is a self clause, check if the target is an
1099 /* target is attribute, check if the attribute value
1102 rc = value_match( &match, bdn->a_at,
1103 bdn->a_at->ad_type->sat_equality, 0,
1106 /* on match error or no match, fail the ACL clause */
1107 if ( rc != LDAP_SUCCESS || match != 0 )
1116 * slap_acl_mask - modifies mask based upon the given acl and the
1117 * requested access to entry e, attribute attr, value val. if val
1118 * is null, access to the whole attribute is assumed (all values).
1120 * returns 0 access NOT allowed
1124 static slap_control_t
1130 AttributeDescription *desc,
1132 AclRegexMatches *matches,
1134 AccessControlState *state,
1135 slap_access_t access )
1140 char accessmaskbuf[ACCESSMASK_MAXLEN];
1143 slap_mask_t a2pmask = ACL_ACCESS2PRIV( access );
1145 assert( a != NULL );
1146 assert( mask != NULL );
1147 assert( desc != NULL );
1149 attr = desc->ad_cname.bv_val;
1151 assert( attr != NULL );
1153 Debug( LDAP_DEBUG_ACL,
1154 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1157 Debug( LDAP_DEBUG_ACL,
1158 "=> acl_mask: to %s by \"%s\", (%s) \n",
1159 val ? "value" : "all values",
1160 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
1161 accessmask2str( *mask, accessmaskbuf, 1 ) );
1167 for ( ; b != NULL; b = b->a_next, i++ ) {
1168 slap_mask_t oldmask, modmask;
1170 ACL_INVALIDATE( modmask );
1172 /* AND <who> clauses */
1173 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1174 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1175 b->a_dn_pat.bv_val, 0, 0);
1177 * if access applies to the entry itself, and the
1178 * user is bound as somebody in the same namespace as
1179 * the entry, OR the given dn matches the dn pattern
1182 * NOTE: styles "anonymous", "users" and "self"
1183 * have been moved to enum slap_style_t, whose
1184 * value is set in a_dn_style; however, the string
1185 * is maintained in a_dn_pat.
1188 if ( acl_mask_dn( op, e, desc, val, a, matches,
1189 &b->a_dn, &op->o_ndn ) )
1195 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1198 Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1199 b->a_realdn_pat.bv_val, 0, 0);
1201 * if access applies to the entry itself, and the
1202 * user is bound as somebody in the same namespace as
1203 * the entry, OR the given dn matches the dn pattern
1206 * NOTE: styles "anonymous", "users" and "self"
1207 * have been moved to enum slap_style_t, whose
1208 * value is set in a_dn_style; however, the string
1209 * is maintained in a_dn_pat.
1212 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1214 ndn = op->o_conn->c_ndn;
1219 if ( acl_mask_dn( op, e, desc, val, a, matches,
1220 &b->a_realdn, &ndn ) )
1226 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1227 if ( ! op->o_conn->c_listener ) {
1230 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1231 b->a_sockurl_pat.bv_val, 0, 0 );
1233 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1234 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1235 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1236 e->e_ndn, matches ) )
1241 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1243 char buf[ACL_BUF_SIZE];
1245 bv.bv_len = sizeof( buf ) - 1;
1247 if ( acl_string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, val->bv_val, matches ) )
1252 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1258 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1266 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1267 if ( !op->o_conn->c_peer_domain.bv_val ) {
1270 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1271 b->a_domain_pat.bv_val, 0, 0 );
1272 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1273 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1274 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1275 e->e_ndn, matches ) )
1280 char buf[ACL_BUF_SIZE];
1282 struct berval cmp = op->o_conn->c_peer_domain;
1283 struct berval pat = b->a_domain_pat;
1285 if ( b->a_domain_expand ) {
1288 bv.bv_len = sizeof(buf) - 1;
1291 if ( acl_string_expand(&bv, &b->a_domain_pat, e->e_ndn, val->bv_val, matches) )
1298 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1299 int offset = cmp.bv_len - pat.bv_len;
1304 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1308 /* trim the domain */
1309 cmp.bv_val = &cmp.bv_val[ offset ];
1310 cmp.bv_len -= offset;
1313 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1320 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1321 if ( !op->o_conn->c_peer_name.bv_val ) {
1324 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1325 b->a_peername_pat.bv_val, 0, 0 );
1326 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1327 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1328 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1329 e->e_ndn, matches ) )
1335 /* try exact match */
1336 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1337 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1341 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1343 char buf[ACL_BUF_SIZE];
1345 bv.bv_len = sizeof( buf ) - 1;
1347 if ( acl_string_expand( &bv, &b->a_peername_pat, e->e_ndn, val->bv_val, matches ) )
1352 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1356 /* extract IP and try exact match */
1357 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1359 char buf[STRLENOF("255.255.255.255") + 1];
1362 int port_number = -1;
1364 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1365 acl_bv_ip_eq.bv_val,
1366 acl_bv_ip_eq.bv_len ) != 0 )
1369 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1370 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1372 port = strrchr( ip.bv_val, ':' );
1374 ip.bv_len = port - ip.bv_val;
1376 if ( lutil_atoi( &port_number, port ) != 0 )
1380 /* the port check can be anticipated here */
1381 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1384 /* address longer than expected? */
1385 if ( ip.bv_len >= sizeof(buf) )
1388 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1389 buf[ ip.bv_len ] = '\0';
1391 addr = inet_addr( buf );
1393 /* unable to convert? */
1394 if ( addr == (unsigned long)(-1) )
1397 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1400 #ifdef LDAP_PF_INET6
1401 /* extract IPv6 and try exact match */
1402 } else if ( b->a_peername_style == ACL_STYLE_IPV6 ) {
1404 char buf[STRLENOF("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") + 1];
1406 struct in6_addr addr;
1407 int port_number = -1;
1409 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1410 acl_bv_ipv6_eq.bv_val,
1411 acl_bv_ipv6_eq.bv_len ) != 0 )
1414 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ipv6_eq.bv_len;
1415 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ipv6_eq.bv_len;
1417 port = strrchr( ip.bv_val, ']' );
1419 ip.bv_len = port - ip.bv_val;
1421 if ( port[0] == ':' && lutil_atoi( &port_number, ++port ) != 0 )
1425 /* the port check can be anticipated here */
1426 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1429 /* address longer than expected? */
1430 if ( ip.bv_len >= sizeof(buf) )
1433 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1434 buf[ ip.bv_len ] = '\0';
1436 if ( inet_pton( AF_INET6, buf, &addr ) != 1 )
1440 if ( !slap_addr6_mask( &addr, &b->a_peername_mask6, &b->a_peername_addr6 ) )
1442 #endif /* LDAP_PF_INET6 */
1444 #ifdef LDAP_PF_LOCAL
1445 /* extract path and try exact match */
1446 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1449 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1450 acl_bv_path_eq.bv_val,
1451 acl_bv_path_eq.bv_len ) != 0 )
1454 path.bv_val = op->o_conn->c_peer_name.bv_val
1455 + acl_bv_path_eq.bv_len;
1456 path.bv_len = op->o_conn->c_peer_name.bv_len
1457 - acl_bv_path_eq.bv_len;
1459 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1462 #endif /* LDAP_PF_LOCAL */
1464 /* exact match (very unlikely...) */
1465 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1472 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1473 if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1476 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1477 b->a_sockname_pat.bv_val, 0, 0 );
1478 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1479 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1480 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1481 e->e_ndn, matches ) )
1486 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1488 char buf[ACL_BUF_SIZE];
1490 bv.bv_len = sizeof( buf ) - 1;
1492 if ( acl_string_expand( &bv, &b->a_sockname_pat, e->e_ndn, val->bv_val, matches ) )
1497 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1502 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1509 if ( b->a_dn_at != NULL ) {
1510 if ( acl_mask_dnattr( op, e, val, a, b, i,
1511 matches, count, state,
1512 &b->a_dn, &op->o_ndn ) )
1518 if ( b->a_realdn_at != NULL ) {
1521 if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1523 ndn = op->o_conn->c_ndn;
1528 if ( acl_mask_dnattr( op, e, val, a, b, i,
1529 matches, count, state,
1530 &b->a_realdn, &ndn ) )
1536 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1538 struct berval ndn = BER_BVNULL;
1541 if ( op->o_ndn.bv_len == 0 ) {
1545 Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1546 b->a_group_pat.bv_val, 0, 0 );
1548 /* b->a_group is an unexpanded entry name, expanded it should be an
1549 * entry with objectclass group* and we test to see if odn is one of
1550 * the values in the attribute group
1552 /* see if asker is listed in dnattr */
1553 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1554 char buf[ACL_BUF_SIZE];
1555 AclRegexMatches tmp_matches,
1556 *tmp_matchesp = &tmp_matches;
1557 regmatch_t *tmp_data;
1559 MATCHES_MEMSET( &tmp_matches );
1560 tmp_data = &tmp_matches.dn_data[0];
1562 bv.bv_len = sizeof(buf) - 1;
1567 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1568 tmp_matchesp = matches;
1569 else switch ( a->acl_dn_style ) {
1570 case ACL_STYLE_REGEX:
1571 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1572 tmp_matchesp = matches;
1576 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1577 case ACL_STYLE_BASE:
1578 tmp_data[0].rm_so = 0;
1579 tmp_data[0].rm_eo = e->e_nname.bv_len;
1580 tmp_matches.dn_count = 1;
1584 case ACL_STYLE_SUBTREE:
1585 case ACL_STYLE_CHILDREN:
1586 tmp_data[0].rm_so = 0;
1587 tmp_data[0].rm_eo = e->e_nname.bv_len;
1589 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1590 tmp_data[1].rm_eo = e->e_nname.bv_len;
1591 tmp_matches.dn_count = 2;
1604 if ( acl_string_expand( &bv, &b->a_group_pat,
1605 e->e_nname.bv_val, val->bv_val,
1611 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1612 op->o_tmpmemctx ) != LDAP_SUCCESS )
1614 /* did not expand to a valid dn */
1621 bv = b->a_group_pat;
1624 rc = backend_group( op, e, &bv, &op->o_ndn,
1625 b->a_group_oc, b->a_group_at );
1628 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1636 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1638 char buf[ACL_BUF_SIZE];
1640 Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1641 b->a_set_pat.bv_val, 0, 0 );
1643 if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1644 AclRegexMatches tmp_matches,
1645 *tmp_matchesp = &tmp_matches;
1647 regmatch_t *tmp_data;
1649 MATCHES_MEMSET( &tmp_matches );
1650 tmp_data = &tmp_matches.dn_data[0];
1652 bv.bv_len = sizeof( buf ) - 1;
1657 if ( a->acl_attrval_style == ACL_STYLE_REGEX )
1658 tmp_matchesp = matches;
1659 else switch ( a->acl_dn_style ) {
1660 case ACL_STYLE_REGEX:
1661 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1662 tmp_matchesp = matches;
1666 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1667 case ACL_STYLE_BASE:
1668 tmp_data[0].rm_so = 0;
1669 tmp_data[0].rm_eo = e->e_nname.bv_len;
1670 tmp_matches.dn_count = 1;
1674 case ACL_STYLE_SUBTREE:
1675 case ACL_STYLE_CHILDREN:
1676 tmp_data[0].rm_so = 0;
1677 tmp_data[0].rm_eo = e->e_nname.bv_len;
1678 tmp_data[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1679 tmp_data[1].rm_eo = e->e_nname.bv_len; tmp_matches.dn_count = 2;
1692 if ( acl_string_expand( &bv, &b->a_set_pat,
1693 e->e_nname.bv_val, val->bv_val,
1703 if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1708 if ( b->a_authz.sai_ssf ) {
1709 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1710 b->a_authz.sai_ssf, op->o_ssf, 0 );
1711 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1716 if ( b->a_authz.sai_transport_ssf ) {
1717 Debug( LDAP_DEBUG_ACL,
1718 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1719 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1720 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1725 if ( b->a_authz.sai_tls_ssf ) {
1726 Debug( LDAP_DEBUG_ACL,
1727 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1728 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1729 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1734 if ( b->a_authz.sai_sasl_ssf ) {
1735 Debug( LDAP_DEBUG_ACL,
1736 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1737 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1738 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1743 /* check for the "self" modifier in the <access> field */
1744 if ( b->a_dn.a_self ) {
1748 ACL_RECORD_VALUE_STATE;
1750 /* must have DN syntax */
1751 if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1752 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1754 /* check if the target is an attribute. */
1755 if ( val == NULL ) continue;
1757 /* a DN must be present */
1758 if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1762 /* target is attribute, check if the attribute value
1765 rc = value_match( &match, desc,
1766 desc->ad_type->sat_equality, 0,
1767 val, &op->o_ndn, &dummy );
1768 /* on match error or no match, fail the ACL clause */
1769 if ( rc != LDAP_SUCCESS || match != 0 )
1774 if ( b->a_dynacl ) {
1776 slap_access_t tgrant, tdeny;
1778 Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1781 /* this case works different from the others above.
1782 * since dynamic ACL's themselves give permissions, we need
1783 * to first check b->a_access_mask, the ACL's access level.
1785 /* first check if the right being requested
1786 * is allowed by the ACL clause.
1788 if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1792 /* start out with nothing granted, nothing denied */
1793 ACL_INVALIDATE(tgrant);
1794 ACL_INVALIDATE(tdeny);
1796 for ( da = b->a_dynacl; da; da = da->da_next ) {
1797 slap_access_t grant,
1800 ACL_INVALIDATE(grant);
1801 ACL_INVALIDATE(deny);
1803 Debug( LDAP_DEBUG_ACL, " <= check a_dynacl: %s\n",
1804 da->da_name, 0, 0 );
1807 * XXXmanu Only DN matches are supplied
1808 * sending attribute values matches require
1811 (void)da->da_mask( da->da_private, op, e, desc,
1812 val, matches->dn_count, matches->dn_data,
1819 /* remove anything that the ACL clause does not allow */
1820 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1821 tdeny &= ACL_PRIV_MASK;
1823 /* see if we have anything to contribute */
1824 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1828 /* this could be improved by changing slap_acl_mask so that it can deal with
1829 * by clauses that return grant/deny pairs. Right now, it does either
1830 * additive or subtractive rights, but not both at the same time. So,
1831 * we need to combine the grant/deny pair into a single rights mask in
1832 * a smart way: if either grant or deny is "empty", then we use the
1833 * opposite as is, otherwise we remove any denied rights from the grant
1834 * rights mask and construct an additive mask.
1836 if (ACL_IS_INVALID(tdeny)) {
1837 modmask = tgrant | ACL_PRIV_ADDITIVE;
1839 } else if (ACL_IS_INVALID(tgrant)) {
1840 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1843 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1847 #endif /* SLAP_DYNACL */
1849 modmask = b->a_access_mask;
1852 Debug( LDAP_DEBUG_ACL,
1853 "<= acl_mask: [%d] applying %s (%s)\n",
1854 i, accessmask2str( modmask, accessmaskbuf, 1 ),
1855 b->a_type == ACL_CONTINUE
1857 : b->a_type == ACL_BREAK
1863 if( ACL_IS_ADDITIVE(modmask) ) {
1865 ACL_PRIV_SET( *mask, modmask );
1868 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1870 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1871 /* substract privs */
1872 ACL_PRIV_CLR( *mask, modmask );
1875 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1882 Debug( LDAP_DEBUG_ACL,
1883 "<= acl_mask: [%d] mask: %s\n",
1884 i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1886 if( b->a_type == ACL_CONTINUE ) {
1889 } else if ( b->a_type == ACL_BREAK ) {
1897 /* implicit "by * none" clause */
1900 Debug( LDAP_DEBUG_ACL,
1901 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1902 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1907 * acl_check_modlist - check access control on the given entry to see if
1908 * it allows the given modifications by the user associated with op.
1909 * returns 1 if mods allowed ok
1910 * 0 mods not allowed
1917 Modifications *mlist )
1920 AccessControlState state = ACL_STATE_INIT;
1923 int ret = 1; /* default is access allowed */
1927 be = LDAP_STAILQ_FIRST(&backendDB);
1931 assert( be != NULL );
1933 /* If ADD attribute checking is not enabled, just allow it */
1934 if ( op->o_tag == LDAP_REQ_ADD && !SLAP_DBACL_ADD( be ))
1937 /* short circuit root database access */
1938 if ( be_isroot( op ) ) {
1939 Debug( LDAP_DEBUG_ACL,
1940 "<= acl_access_allowed: granted to database root\n",
1945 /* use backend default access if no backend acls */
1946 if( op->o_bd != NULL && op->o_bd->be_acl == NULL && frontendDB->be_acl == NULL ) {
1947 Debug( LDAP_DEBUG_ACL,
1948 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1949 access2str( ACL_WRITE ),
1950 op->o_bd->be_dfltaccess >= ACL_WRITE
1951 ? "granted" : "denied",
1953 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1957 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1959 * Internal mods are ignored by ACL_WRITE checking
1961 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1962 Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1963 " modify access granted\n",
1964 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1969 * no-user-modification operational attributes are ignored
1970 * by ACL_WRITE checking as any found here are not provided
1973 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1974 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1976 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1977 " modify access granted\n",
1978 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1982 switch ( mlist->sml_op ) {
1983 case LDAP_MOD_REPLACE:
1984 case LDAP_MOD_INCREMENT:
1986 * We must check both permission to delete the whole
1987 * attribute and permission to add the specific attributes.
1988 * This prevents abuse from selfwriters.
1990 if ( ! access_allowed( op, e,
1991 mlist->sml_desc, NULL,
1992 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1999 if ( mlist->sml_values == NULL ) break;
2001 /* fall thru to check value to add */
2004 assert( mlist->sml_values != NULL );
2006 for ( bv = mlist->sml_nvalues
2007 ? mlist->sml_nvalues : mlist->sml_values;
2008 bv->bv_val != NULL; bv++ )
2010 if ( ! access_allowed( op, e,
2011 mlist->sml_desc, bv,
2012 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2021 case LDAP_MOD_DELETE:
2022 if ( mlist->sml_values == NULL ) {
2023 if ( ! access_allowed( op, e,
2024 mlist->sml_desc, NULL,
2025 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2033 for ( bv = mlist->sml_nvalues
2034 ? mlist->sml_nvalues : mlist->sml_values;
2035 bv->bv_val != NULL; bv++ )
2037 if ( ! access_allowed( op, e,
2038 mlist->sml_desc, bv,
2039 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2048 case SLAP_MOD_SOFTADD:
2049 /* allow adding attribute via modrdn thru */
2061 if (be_null) op->o_bd = NULL;
2067 struct berval *list,
2080 while ( len >= 0 && --ix >= 0 ) {
2081 while ( --len >= 0 && *p++ != sep )
2084 while ( len >= 0 && *p == ' ' ) {
2097 while ( --len >= 0 && *p != sep ) {
2101 while ( bv->bv_len > 0 && *--p == ' ' ) {
2108 typedef struct acl_set_gather_t {
2114 acl_set_cb_gather( Operation *op, SlapReply *rs )
2116 acl_set_gather_t *p = (acl_set_gather_t *)op->o_callback->sc_private;
2118 if ( rs->sr_type == REP_SEARCH ) {
2119 BerValue bvals[ 2 ];
2120 BerVarray bvalsp = NULL;
2123 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2124 AttributeDescription *desc = rs->sr_attrs[ j ].an_desc;
2126 if ( desc == NULL ) {
2130 if ( desc == slap_schema.si_ad_entryDN ) {
2132 bvals[ 0 ] = rs->sr_entry->e_nname;
2133 BER_BVZERO( &bvals[ 1 ] );
2138 a = attr_find( rs->sr_entry->e_attrs, desc );
2140 bvalsp = a->a_nvals;
2145 p->bvals = slap_set_join( p->cookie, p->bvals,
2146 ( '|' | SLAP_SET_RREF ), bvalsp );
2151 assert( rs->sr_type == REP_RESULT );
2158 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2160 AclSetCookie *cp = (AclSetCookie *)cookie;
2162 LDAPURLDesc *ludp = NULL;
2163 Operation op2 = { 0 };
2164 SlapReply rs = {REP_RESULT};
2165 AttributeName anlist[ 2 ], *anlistp = NULL;
2167 slap_callback cb = { NULL, acl_set_cb_gather, NULL, NULL };
2168 acl_set_gather_t p = { 0 };
2170 /* this routine needs to return the bervals instead of
2171 * plain strings, since syntax is not known. It should
2172 * also return the syntax or some "comparison cookie".
2174 if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2175 return acl_set_gather2( cookie, name, desc );
2178 rc = ldap_url_parse( name->bv_val, &ludp );
2179 if ( rc != LDAP_URL_SUCCESS ) {
2180 Debug( LDAP_DEBUG_TRACE,
2181 "%s acl_set_gather: unable to parse URL=\"%s\"\n",
2182 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2184 rc = LDAP_PROTOCOL_ERROR;
2188 if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2190 /* host part must be empty */
2191 /* extensions parts must be empty */
2192 Debug( LDAP_DEBUG_TRACE,
2193 "%s acl_set_gather: host/exts must be absent in URL=\"%s\"\n",
2194 cp->asc_op->o_log_prefix, name->bv_val, 0 );
2196 rc = LDAP_PROTOCOL_ERROR;
2200 /* Grab the searchbase and see if an appropriate database can be found */
2201 ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2202 rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2203 &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2204 BER_BVZERO( &op2.o_req_dn );
2205 if ( rc != LDAP_SUCCESS ) {
2206 Debug( LDAP_DEBUG_TRACE,
2207 "%s acl_set_gather: DN=\"%s\" normalize failed\n",
2208 cp->asc_op->o_log_prefix, ludp->lud_dn, 0 );
2213 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2214 if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2215 Debug( LDAP_DEBUG_TRACE,
2216 "%s acl_set_gather: no database could be selected for DN=\"%s\"\n",
2217 cp->asc_op->o_log_prefix, op2.o_req_ndn.bv_val, 0 );
2219 rc = LDAP_NO_SUCH_OBJECT;
2223 /* Grab the filter */
2224 if ( ludp->lud_filter ) {
2225 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2226 cp->asc_op->o_tmpmemctx );
2227 op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2228 if ( op2.ors_filter == NULL ) {
2229 Debug( LDAP_DEBUG_TRACE,
2230 "%s acl_set_gather: unable to parse filter=\"%s\"\n",
2231 cp->asc_op->o_log_prefix, op2.ors_filterstr.bv_val, 0 );
2233 rc = LDAP_PROTOCOL_ERROR;
2238 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
2239 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
2243 /* Grab the scope */
2244 op2.ors_scope = ludp->lud_scope;
2246 /* Grap the attributes */
2247 if ( ludp->lud_attrs ) {
2250 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2253 anlistp = slap_sl_calloc( sizeof( AttributeName ), nattrs + 2,
2254 cp->asc_op->o_tmpmemctx );
2256 for ( i = 0, nattrs = 0; ludp->lud_attrs[ i ]; i++ ) {
2258 AttributeDescription *desc = NULL;
2259 const char *text = NULL;
2261 ber_str2bv( ludp->lud_attrs[ i ], 0, 0, &name );
2262 rc = slap_bv2ad( &name, &desc, &text );
2263 if ( rc == LDAP_SUCCESS ) {
2264 anlistp[ nattrs ].an_name = name;
2265 anlistp[ nattrs ].an_desc = desc;
2274 anlistp[ nattrs ].an_name = desc->ad_cname;
2275 anlistp[ nattrs ].an_desc = desc;
2277 BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2281 op2.o_hdr = cp->asc_op->o_hdr;
2282 op2.o_tag = LDAP_REQ_SEARCH;
2283 op2.o_ndn = op2.o_bd->be_rootndn;
2284 op2.o_callback = &cb;
2285 slap_op_time( &op2.o_time, &op2.o_tincr );
2286 op2.o_do_not_cache = 1;
2287 op2.o_is_auth_check = 0;
2288 ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2289 op2.ors_slimit = SLAP_NO_LIMIT;
2290 op2.ors_tlimit = SLAP_NO_LIMIT;
2291 op2.ors_attrs = anlistp;
2292 op2.ors_attrsonly = 0;
2293 op2.o_private = cp->asc_op->o_private;
2294 op2.o_extra = cp->asc_op->o_extra;
2298 rc = op2.o_bd->be_search( &op2, &rs );
2304 if ( op2.ors_filter && op2.ors_filter != slap_filter_objectClass_pres ) {
2305 filter_free_x( cp->asc_op, op2.ors_filter, 1 );
2307 if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2308 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2310 if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2311 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2314 ldap_free_urldesc( ludp );
2316 if ( anlistp && anlistp != anlist ) {
2317 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2324 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2326 AclSetCookie *cp = (AclSetCookie *)cookie;
2327 BerVarray bvals = NULL;
2331 /* this routine needs to return the bervals instead of
2332 * plain strings, since syntax is not known. It should
2333 * also return the syntax or some "comparison cookie".
2335 rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2336 if ( rc == LDAP_SUCCESS ) {
2337 if ( desc == slap_schema.si_ad_entryDN ) {
2338 bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2339 cp->asc_op->o_tmpmemctx );
2341 BER_BVZERO( &bvals[ 1 ] );
2345 backend_attribute( cp->asc_op,
2346 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2349 if ( !BER_BVISNULL( &ndn ) ) {
2350 slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2359 struct berval *subj,
2362 struct berval *default_set_attribute )
2364 struct berval set = BER_BVNULL;
2366 AclSetCookie cookie;
2368 if ( default_set_attribute == NULL ) {
2372 struct berval subjdn, ndn = BER_BVNULL;
2373 struct berval setat;
2374 BerVarray bvals = NULL;
2376 AttributeDescription *desc = NULL;
2378 /* format of string is "entry/setAttrName" */
2379 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2383 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2384 setat = *default_set_attribute;
2388 * NOTE: dnNormalize honors the ber_len field
2389 * as the length of the dn to be normalized
2391 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2392 if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2394 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2395 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2399 BER_BVZERO( &bvals[0] );
2400 for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2402 bvals[0].bv_val = bvals[i-1].bv_val;
2403 BER_BVZERO( &bvals[i-1] );
2405 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2406 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2411 if ( !BER_BVISNULL( &set ) ) {
2414 rc = ( slap_set_filter(
2416 (SetCookie *)&cookie, &set,
2417 &op->o_ndn, &e->e_nname, NULL ) > 0 );
2418 if ( set.bv_val != subj->bv_val ) {
2419 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2429 * dynamic ACL infrastructure
2431 static slap_dynacl_t *da_list = NULL;
2434 slap_dynacl_register( slap_dynacl_t *da )
2438 for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2439 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2444 if ( tmp != NULL ) {
2448 if ( da->da_mask == NULL ) {
2452 da->da_private = NULL;
2453 da->da_next = da_list;
2459 static slap_dynacl_t *
2460 slap_dynacl_next( slap_dynacl_t *da )
2469 slap_dynacl_get( const char *name )
2473 for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2474 if ( strcasecmp( da->da_name, name ) == 0 ) {
2481 #endif /* SLAP_DYNACL */
2484 * statically built-in dynamic ACL initialization
2486 static int (*acl_init_func[])( void ) = {
2488 /* TODO: remove when ACI will only be dynamic */
2489 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2491 #endif /* SLAPD_ACI_ENABLED */
2492 #endif /* SLAP_DYNACL */
2502 for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2503 rc = (*(acl_init_func[ i ]))();
2518 AclRegexMatches *matches)
2524 enum { DN_FLAG, VAL_FLAG } tflag;
2527 bv->bv_val[0] = '\0';
2528 bv->bv_len--; /* leave space for lone $ */
2532 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2533 sp < pat->bv_val + pat->bv_len ; sp++ )
2535 /* did we previously see a $ */
2537 if ( flag == 1 && *sp == '$' ) {
2543 } else if ( flag == 2 && *sp == 'v' /*'}'*/) {
2546 } else if ( flag == 2 && *sp == 'd' /*'}'*/) {
2549 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2552 } else if ( *sp >= '0' && *sp <= '9' ) {
2563 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2564 if ( *sp >= '0' && *sp <= '9' ) {
2565 n = 10*n + ( *sp - '0' );
2569 if ( *sp != /*'{'*/ '}' ) {
2577 nm = matches->dn_count;
2578 m = matches->dn_data;
2582 nm = matches->val_count;
2583 m = matches->val_data;
2591 if ( data == NULL ) {
2600 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2619 /* must have ended with a single $ */
2627 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2628 Debug( LDAP_DEBUG_ACL, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2635 struct berval *pat, /* pattern to expand and match against */
2636 char *str, /* string to match against pattern */
2637 char *buf, /* buffer with $N expansion variables */
2638 AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
2642 char newbuf[ACL_BUF_SIZE];
2646 bv.bv_len = sizeof( newbuf ) - 1;
2653 acl_string_expand( &bv, pat, buf, NULL, matches );
2654 rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2656 char error[ACL_BUF_SIZE];
2657 regerror( rc, &re, error, sizeof( error ) );
2659 Debug( LDAP_DEBUG_TRACE,
2660 "compile( \"%s\", \"%s\") failed %s\n",
2661 pat->bv_val, str, error );
2665 rc = regexec( &re, str, 0, NULL, 0 );
2668 Debug( LDAP_DEBUG_TRACE,
2669 "=> regex_matches: string: %s\n", str, 0, 0 );
2670 Debug( LDAP_DEBUG_TRACE,
2671 "=> regex_matches: rc: %d %s\n",
2672 rc, !rc ? "matches" : "no matches", 0 );