1 /* acl.c - routines to parse and check acl's */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
40 #include "slapi/slapi.h"
41 #endif /* LDAPI_SLAPI */
43 #define ACL_BUF_SIZE 1024 /* use most appropriate size */
49 aci_bv_entry = BER_BVC("entry"),
50 aci_bv_children = BER_BVC("children"),
51 aci_bv_br_entry = BER_BVC("[entry]"),
52 aci_bv_br_all = BER_BVC("[all]"),
53 aci_bv_access_id = BER_BVC("access-id"),
54 aci_bv_anonymous = BER_BVC("anonymous"),
55 aci_bv_public = BER_BVC("public"),
56 aci_bv_users = BER_BVC("users"),
57 aci_bv_self = BER_BVC("self"),
58 aci_bv_dnattr = BER_BVC("dnattr"),
59 aci_bv_group = BER_BVC("group"),
60 aci_bv_role = BER_BVC("role"),
61 aci_bv_set = BER_BVC("set"),
62 aci_bv_set_ref = BER_BVC("set-ref"),
63 aci_bv_grant = BER_BVC("grant"),
64 aci_bv_deny = BER_BVC("deny"),
66 aci_bv_ip_eq = BER_BVC("IP="),
68 aci_bv_path_eq = BER_BVC("PATH="),
69 aci_bv_dirsep = BER_BVC(LDAP_DIRSEP),
70 #endif /* LDAP_PF_LOCAL */
72 aci_bv_group_class = BER_BVC(SLAPD_GROUP_CLASS),
73 aci_bv_group_attr = BER_BVC(SLAPD_GROUP_ATTR),
74 aci_bv_role_class = BER_BVC(SLAPD_ROLE_CLASS),
75 aci_bv_role_attr = BER_BVC(SLAPD_ROLE_ATTR);
78 static AccessControl * acl_get(
79 AccessControl *ac, int *count,
80 Operation *op, Entry *e,
81 AttributeDescription *desc,
83 int nmatches, regmatch_t *matches,
84 AccessControlState *state );
86 static slap_control_t acl_mask(
87 AccessControl *ac, slap_mask_t *mask,
88 Operation *op, Entry *e,
89 AttributeDescription *desc,
93 AccessControlState *state );
95 #ifdef SLAPD_ACI_ENABLED
97 Operation *op, Entry *e,
98 AttributeDescription *desc,
102 slap_access_t *grant,
104 struct berval *scope);
107 static int regex_matches(
108 struct berval *pat, char *str, char *buf, regmatch_t *matches);
109 static void string_expand(
110 struct berval *newbuf, struct berval *pattern,
111 char *match, regmatch_t *matches);
113 typedef struct AciSetCookie {
118 SLAP_SET_GATHER aci_set_gather;
119 static int aci_match_set ( struct berval *subj, Operation *op,
120 Entry *e, int setref );
123 * access_allowed - check whether op->o_ndn is allowed the requested access
124 * to entry e, attribute attr, value val. if val is null, access to
125 * the whole attribute is assumed (all values).
127 * This routine loops through all access controls and calls
128 * acl_mask() on each applicable access control.
129 * The loop exits when a definitive answer is reached or
130 * or no more controls remain.
141 AttributeDescription *desc,
143 slap_access_t access,
144 AccessControlState *state )
148 AccessControl *a = NULL;
153 char accessmaskbuf[ACCESSMASK_MAXLEN];
156 slap_control_t control;
158 regmatch_t matches[MAXREMATCHES];
159 int st_same_attr = 0;
160 static AccessControlState state_init = ACL_STATE_INIT;
163 assert( desc != NULL );
164 assert( access > ACL_NONE );
166 attr = desc->ad_cname.bv_val;
168 assert( attr != NULL );
170 if( op && op->o_is_auth_check &&
171 ( access == ACL_SEARCH || access == ACL_READ ))
177 if ( state->as_vd_ad==desc) {
178 if ( state->as_recorded ) {
179 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
182 return state->as_result;
183 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
184 val != NULL && state->as_vd_acl == NULL )
186 return state->as_result;
194 state->as_vd_ad=desc;
198 LDAP_LOG( ACL, ENTRY,
199 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
200 access2str( access ), e->e_dn, attr );
202 Debug( LDAP_DEBUG_ACL,
203 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
204 access2str( access ), e->e_dn, attr );
218 assert( be != NULL );
221 if ( op->o_pb != NULL ) {
222 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
224 /* ACL plugin denied access */
228 #endif /* LDAP_SLAPI */
230 /* grant database root access */
231 if ( be != NULL && be_isroot( op ) ) {
234 "access_allowed: conn %lu root access granted\n",
235 op->o_connid, 0, 0 );
237 Debug( LDAP_DEBUG_ACL,
238 "<= root access granted\n",
245 * no-user-modification operational attributes are ignored
246 * by ACL_WRITE checking as any found here are not provided
249 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
250 && desc != slap_schema.si_ad_entry
251 && desc != slap_schema.si_ad_children )
254 LDAP_LOG( ACL, DETAIL1,
255 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
256 "access granted\n", op->o_connid, attr , 0 );
258 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
259 " %s access granted\n",
265 /* use backend default access if no backend acls */
266 if( be != NULL && be->be_acl == NULL ) {
268 LDAP_LOG( ACL, DETAIL1,
269 "access_allowed: backend default %s access %s to \"%s\"\n",
270 access2str( access ),
271 be->be_dfltaccess >= access ? "granted" : "denied",
272 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
274 Debug( LDAP_DEBUG_ACL,
275 "=> access_allowed: backend default %s access %s to \"%s\"\n",
276 access2str( access ),
277 be->be_dfltaccess >= access ? "granted" : "denied",
278 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
280 ret = be->be_dfltaccess >= access;
284 /* be is always non-NULL */
285 /* use global default access if no global acls */
286 } else if ( be == NULL && global_acl == NULL ) {
288 LDAP_LOG( ACL, DETAIL1,
289 "access_allowed: global default %s access %s to \"%s\"\n",
290 access2str( access ),
291 global_default_access >= access ? "granted" : "denied",
294 Debug( LDAP_DEBUG_ACL,
295 "=> access_allowed: global default %s access %s to \"%s\"\n",
296 access2str( access ),
297 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
299 ret = global_default_access >= access;
308 assert( state->as_vd_acl != NULL );
310 a = state->as_vd_acl;
311 count = state->as_vd_acl_count;
312 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
313 mask = state->as_vd_acl_mask;
314 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
319 if ( state ) state->as_vi_acl = NULL;
323 memset(matches, '\0', sizeof(matches));
326 while((a = acl_get( a, &count, op, e, desc, val,
327 MAXREMATCHES, matches, state )) != NULL)
331 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
333 LDAP_LOG( ACL, DETAIL1,
334 "access_allowed: match[%d]: %d %d ",
335 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
337 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
338 (int)matches[i].rm_so, (int)matches[i].rm_eo );
340 if( matches[i].rm_so <= matches[0].rm_eo ) {
342 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
343 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
347 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
349 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
354 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
355 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
356 ret = state->as_result;
359 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
364 control = acl_mask( a, &mask, op,
365 e, desc, val, matches, count, state );
367 if ( control != ACL_BREAK ) {
371 memset(matches, '\0', sizeof(matches));
374 if ( ACL_IS_INVALID( mask ) ) {
376 LDAP_LOG( ACL, DETAIL1,
377 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
378 op->o_connid, e->e_dn, attr );
380 Debug( LDAP_DEBUG_ACL,
381 "=> access_allowed: \"%s\" (%s) invalid!\n",
386 } else if ( control == ACL_BREAK ) {
388 LDAP_LOG( ACL, DETAIL1,
389 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
391 Debug( LDAP_DEBUG_ACL,
392 "=> access_allowed: no more rules\n", 0, 0, 0);
399 LDAP_LOG( ACL, ENTRY,
400 "access_allowed: %s access %s by %s\n",
401 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
402 accessmask2str( mask, accessmaskbuf ) );
404 Debug( LDAP_DEBUG_ACL,
405 "=> access_allowed: %s access %s by %s\n",
406 access2str( access ),
407 ACL_GRANT(mask, access) ? "granted" : "denied",
408 accessmask2str( mask, accessmaskbuf ) );
411 ret = ACL_GRANT(mask, access);
414 if( state != NULL ) {
415 /* If not value-dependent, save ACL in case of more attrs */
416 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
417 state->as_vi_acl = a;
418 state->as_result = ret;
420 state->as_recorded |= ACL_STATE_RECORDED;
422 if (be_null) op->o_bd = NULL;
428 * acl_get - return the acl applicable to entry e, attribute
429 * attr. the acl returned is suitable for use in subsequent calls to
430 * acl_access_allowed().
433 static AccessControl *
439 AttributeDescription *desc,
443 AccessControlState *state )
450 assert( count != NULL );
451 assert( desc != NULL );
453 attr = desc->ad_cname.bv_val;
455 assert( attr != NULL );
458 if( op->o_bd == NULL ) {
461 a = op->o_bd->be_acl;
472 dnlen = e->e_nname.bv_len;
474 for ( ; a != NULL; a = a->acl_next ) {
477 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
478 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
480 LDAP_LOG( ACL, DETAIL1,
481 "acl_get: dnpat [%d] %s nsub: %d\n",
482 *count, a->acl_dn_pat.bv_val,
483 (int) a->acl_dn_re.re_nsub );
485 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
486 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
488 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
493 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
494 *count, a->acl_dn_pat.bv_val, 0 );
496 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
497 *count, a->acl_dn_pat.bv_val, 0 );
499 patlen = a->acl_dn_pat.bv_len;
500 if ( dnlen < patlen )
503 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
504 /* base dn -- entire object DN must match */
505 if ( dnlen != patlen )
508 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
509 int rdnlen = -1, sep = 0;
511 if ( dnlen <= patlen )
515 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
520 rdnlen = dn_rdnlen( NULL, &e->e_nname );
521 if ( rdnlen != dnlen - patlen - sep )
524 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
525 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
528 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
529 if ( dnlen <= patlen )
531 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
535 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
540 LDAP_LOG( ACL, DETAIL1,
541 "acl_get: [%d] matched\n", *count, 0, 0 );
543 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
548 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
549 matches[0].rm_so = matches[0].rm_eo = -1;
553 /* Is this ACL only for a specific value? */
554 if ( a->acl_attrval.bv_len ) {
559 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
560 state->as_recorded |= ACL_STATE_RECORDED_VD;
561 state->as_vd_acl = prev;
562 state->as_vd_acl_count = *count;
563 state->as_vd_access = a->acl_access;
564 state->as_vd_access_count = 1;
565 ACL_INVALIDATE( state->as_vd_acl_mask );
568 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
570 LDAP_LOG( ACL, DETAIL1,
571 "acl_get: valpat %s\n",
572 a->acl_attrval.bv_val, 0, 0 );
574 Debug( LDAP_DEBUG_ACL,
575 "acl_get: valpat %s\n",
576 a->acl_attrval.bv_val, 0, 0 );
578 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
584 LDAP_LOG( ACL, DETAIL1,
586 a->acl_attrval.bv_val, 0, 0 );
588 Debug( LDAP_DEBUG_ACL,
590 a->acl_attrval.bv_val, 0, 0 );
593 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
594 if (value_match( &match, desc,
595 desc->ad_type->sat_equality, 0,
596 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
603 patlen = a->acl_attrval.bv_len;
604 vdnlen = val->bv_len;
606 if ( vdnlen < patlen )
609 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
610 if ( vdnlen > patlen )
613 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
616 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
619 rdnlen = dn_rdnlen( NULL, val );
620 if ( rdnlen != vdnlen - patlen - 1 )
623 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
624 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
627 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
628 if ( vdnlen <= patlen )
631 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
635 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
641 if ( a->acl_filter != NULL ) {
642 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
643 if ( rc != LDAP_COMPARE_TRUE ) {
649 LDAP_LOG( ACL, DETAIL1,
650 "acl_get: [%d] attr %s\n", *count, attr ,0 );
652 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
659 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
661 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
667 * Record value-dependent access control state
669 #define ACL_RECORD_VALUE_STATE do { \
670 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
671 state->as_recorded |= ACL_STATE_RECORDED_VD; \
672 state->as_vd_acl = a; \
673 AC_MEMCPY( state->as_vd_acl_matches, matches, \
674 sizeof( state->as_vd_acl_matches )) ; \
675 state->as_vd_acl_count = count; \
676 state->as_vd_access = b; \
677 state->as_vd_access_count = i; \
682 * acl_mask - modifies mask based upon the given acl and the
683 * requested access to entry e, attribute attr, value val. if val
684 * is null, access to the whole attribute is assumed (all values).
686 * returns 0 access NOT allowed
690 static slap_control_t
696 AttributeDescription *desc,
700 AccessControlState *state )
702 int i, odnlen, patlen;
705 char accessmaskbuf[ACCESSMASK_MAXLEN];
706 char accessmaskbuf1[ACCESSMASK_MAXLEN];
711 assert( mask != NULL );
712 assert( desc != NULL );
714 attr = desc->ad_cname.bv_val;
716 assert( attr != NULL );
719 LDAP_LOG( ACL, ENTRY,
720 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
721 op->o_connid, e->e_dn, attr );
724 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
725 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
726 accessmask2str( *mask, accessmaskbuf ) );
728 Debug( LDAP_DEBUG_ACL,
729 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
732 Debug( LDAP_DEBUG_ACL,
733 "=> acl_mask: to %s by \"%s\", (%s) \n",
734 val ? "value" : "all values",
735 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
736 accessmask2str( *mask, accessmaskbuf ) );
740 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
741 && state->as_vd_acl == a )
743 b = state->as_vd_access;
744 i = state->as_vd_access_count;
751 for ( ; b != NULL; b = b->a_next, i++ ) {
752 slap_mask_t oldmask, modmask;
754 ACL_INVALIDATE( modmask );
756 /* AND <who> clauses */
757 if ( b->a_dn_pat.bv_len != 0 ) {
759 LDAP_LOG( ACL, DETAIL1,
760 "acl_mask: conn %lu check a_dn_pat: %s\n",
761 op->o_connid, b->a_dn_pat.bv_val ,0 );
763 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
764 b->a_dn_pat.bv_val, 0, 0);
767 * if access applies to the entry itself, and the
768 * user is bound as somebody in the same namespace as
769 * the entry, OR the given dn matches the dn pattern
771 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
772 if ( op->o_ndn.bv_len != 0 ) {
776 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
777 if ( op->o_ndn.bv_len == 0 ) {
781 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
782 if ( op->o_ndn.bv_len == 0 ) {
786 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
790 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
791 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
792 int ret = regex_matches( &b->a_dn_pat,
793 op->o_ndn.bv_val, e->e_ndn, matches );
804 if ( e->e_dn == NULL )
807 if ( b->a_dn_expand ) {
809 char buf[ACL_BUF_SIZE];
811 bv.bv_len = sizeof( buf ) - 1;
814 string_expand(&bv, &b->a_dn_pat,
816 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
817 /* did not expand to a valid dn */
825 odnlen = op->o_ndn.bv_len;
826 if ( odnlen < patlen ) {
827 goto dn_match_cleanup;
831 if ( b->a_dn_style == ACL_STYLE_BASE ) {
832 /* base dn -- entire object DN must match */
833 if ( odnlen != patlen ) {
834 goto dn_match_cleanup;
837 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
840 if ( odnlen <= patlen ) {
841 goto dn_match_cleanup;
844 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
845 goto dn_match_cleanup;
848 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
849 if ( rdnlen != odnlen - patlen - 1 ) {
850 goto dn_match_cleanup;
853 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
854 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
855 goto dn_match_cleanup;
858 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
859 if ( odnlen <= patlen ) {
860 goto dn_match_cleanup;
863 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
864 goto dn_match_cleanup;
868 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
871 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
881 if ( b->a_sockurl_pat.bv_len ) {
882 if ( ! op->o_conn->c_listener ) {
886 LDAP_LOG( ACL, DETAIL1,
887 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
888 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
890 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
891 b->a_sockurl_pat.bv_val, 0, 0 );
894 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
895 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
896 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
897 e->e_ndn, matches ) )
902 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
904 char buf[ACL_BUF_SIZE];
906 bv.bv_len = sizeof( buf ) - 1;
908 string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, matches );
910 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 ) {
915 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
921 if ( b->a_domain_pat.bv_len ) {
922 if ( !op->o_conn->c_peer_domain.bv_val ) {
926 LDAP_LOG( ACL, DETAIL1,
927 "acl_mask: conn %lu check a_domain_pat: %s\n",
928 op->o_connid, b->a_domain_pat.bv_val , 0 );
930 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
931 b->a_domain_pat.bv_val, 0, 0 );
933 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
934 if ( b->a_domain_style == ACL_STYLE_REGEX) {
935 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
936 e->e_ndn, matches ) )
941 char buf[ACL_BUF_SIZE];
943 struct berval cmp = op->o_conn->c_peer_domain;
944 struct berval pat = b->a_domain_pat;
946 if ( b->a_domain_expand ) {
949 bv.bv_len = sizeof(buf) - 1;
952 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
956 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
957 int offset = cmp.bv_len - pat.bv_len;
962 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
966 /* trim the domain */
967 cmp.bv_val = &cmp.bv_val[ offset ];
968 cmp.bv_len -= offset;
971 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
978 if ( b->a_peername_pat.bv_len ) {
979 if ( !op->o_conn->c_peer_name.bv_val ) {
983 LDAP_LOG( ACL, DETAIL1,
984 "acl_mask: conn %lu check a_peername_path: %s\n",
985 op->o_connid, b->a_peername_pat.bv_val , 0 );
987 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
988 b->a_peername_pat.bv_val, 0, 0 );
990 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
991 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
992 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
993 e->e_ndn, matches ) )
999 /* try exact match */
1000 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1001 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1005 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1007 char buf[ACL_BUF_SIZE];
1009 bv.bv_len = sizeof( buf ) - 1;
1011 string_expand( &bv, &b->a_peername_pat, e->e_ndn, matches );
1013 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1017 /* extract IP and try exact match */
1018 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1020 char buf[] = "255.255.255.255";
1023 int port_number = -1;
1025 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1026 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1029 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1030 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1032 port = strrchr( ip.bv_val, ':' );
1036 ip.bv_len = port - ip.bv_val;
1038 port_number = strtol( port, &next, 10 );
1039 if ( next[0] != '\0' )
1043 /* the port check can be anticipated here */
1044 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1047 /* address longer than expected? */
1048 if ( ip.bv_len >= sizeof(buf) )
1051 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1052 buf[ ip.bv_len ] = '\0';
1054 addr = inet_addr( buf );
1056 /* unable to convert? */
1057 if ( addr == (unsigned long)(-1) )
1060 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1063 #ifdef LDAP_PF_LOCAL
1064 /* extract path and try exact match */
1065 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1068 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1069 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1072 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1073 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1075 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1078 #endif /* LDAP_PF_LOCAL */
1080 /* exact match (very unlikely...) */
1081 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1088 if ( b->a_sockname_pat.bv_len ) {
1089 if ( !op->o_conn->c_sock_name.bv_val ) {
1093 LDAP_LOG( ACL, DETAIL1,
1094 "acl_mask: conn %lu check a_sockname_path: %s\n",
1095 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1097 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1098 b->a_sockname_pat.bv_val, 0, 0 );
1100 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1101 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1102 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1103 e->e_ndn, matches ) )
1108 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1110 char buf[ACL_BUF_SIZE];
1112 bv.bv_len = sizeof( buf ) - 1;
1114 string_expand( &bv, &b->a_sockname_pat, e->e_ndn, matches );
1116 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1121 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1127 if ( b->a_dn_at != NULL ) {
1132 const char *attr = b->a_dn_at->ad_cname.bv_val;
1134 assert( attr != NULL );
1136 if ( op->o_ndn.bv_len == 0 ) {
1141 LDAP_LOG( ACL, DETAIL1,
1142 "acl_mask: conn %lu check a_dn_pat: %s\n",
1143 op->o_connid, attr , 0 );
1145 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1150 /* see if asker is listed in dnattr */
1151 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1153 at = attrs_find( at->a_next, b->a_dn_at ) )
1155 if( value_find_ex( b->a_dn_at,
1156 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1157 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1159 &bv, op->o_tmpmemctx ) == 0 )
1168 /* have a dnattr match. if this is a self clause then
1169 * the target must also match the op dn.
1171 if ( b->a_dn_self ) {
1172 /* check if the target is an attribute. */
1173 if ( val == NULL ) continue;
1175 /* target is attribute, check if the attribute value
1178 rc = value_match( &match, b->a_dn_at,
1179 b->a_dn_at->ad_type->sat_equality, 0,
1181 /* on match error or no match, fail the ACL clause */
1182 if (rc != LDAP_SUCCESS || match != 0 )
1186 /* no dnattr match, check if this is a self clause */
1187 if ( ! b->a_dn_self )
1190 ACL_RECORD_VALUE_STATE;
1192 /* this is a self clause, check if the target is an
1198 /* target is attribute, check if the attribute value
1201 rc = value_match( &match, b->a_dn_at,
1202 b->a_dn_at->ad_type->sat_equality, 0,
1205 /* on match error or no match, fail the ACL clause */
1206 if (rc != LDAP_SUCCESS || match != 0 )
1211 if ( b->a_group_pat.bv_len ) {
1213 struct berval ndn = BER_BVNULL;
1216 if ( op->o_ndn.bv_len == 0 ) {
1220 /* b->a_group is an unexpanded entry name, expanded it should be an
1221 * entry with objectclass group* and we test to see if odn is one of
1222 * the values in the attribute group
1224 /* see if asker is listed in dnattr */
1225 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1226 char buf[ACL_BUF_SIZE];
1227 bv.bv_len = sizeof(buf) - 1;
1230 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1231 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1232 /* did not expand to a valid dn */
1239 bv = b->a_group_pat;
1242 rc = backend_group( op, e, &bv, &op->o_ndn,
1243 b->a_group_oc, b->a_group_at );
1245 if ( ndn.bv_val ) free( ndn.bv_val );
1252 if ( b->a_set_pat.bv_len != 0 ) {
1254 char buf[ACL_BUF_SIZE];
1255 if( b->a_set_style == ACL_STYLE_REGEX ){
1256 bv.bv_len = sizeof(buf) - 1;
1258 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1262 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1267 if ( b->a_authz.sai_ssf ) {
1269 LDAP_LOG( ACL, DETAIL1,
1270 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1271 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1273 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1274 b->a_authz.sai_ssf, op->o_ssf, 0 );
1276 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1281 if ( b->a_authz.sai_transport_ssf ) {
1283 LDAP_LOG( ACL, DETAIL1,
1284 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1286 op->o_connid, b->a_authz.sai_transport_ssf,
1287 op->o_transport_ssf );
1289 Debug( LDAP_DEBUG_ACL,
1290 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1291 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1293 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1298 if ( b->a_authz.sai_tls_ssf ) {
1300 LDAP_LOG( ACL, DETAIL1,
1301 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1303 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1305 Debug( LDAP_DEBUG_ACL,
1306 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1307 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1309 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1314 if ( b->a_authz.sai_sasl_ssf ) {
1316 LDAP_LOG( ACL, DETAIL1,
1317 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1319 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1321 Debug( LDAP_DEBUG_ACL,
1322 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1323 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1325 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1330 #ifdef SLAPD_ACI_ENABLED
1331 if ( b->a_aci_at != NULL ) {
1333 slap_access_t grant, deny, tgrant, tdeny;
1334 struct berval parent_ndn, old_parent_ndn;
1335 BerVarray bvals = NULL;
1338 /* this case works different from the others above.
1339 * since aci's themselves give permissions, we need
1340 * to first check b->a_access_mask, the ACL's access level.
1343 if ( e->e_nname.bv_len == 0 ) {
1344 /* no ACIs in the root DSE */
1348 /* first check if the right being requested
1349 * is allowed by the ACL clause.
1351 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1354 /* start out with nothing granted, nothing denied */
1358 /* get the aci attribute */
1359 at = attr_find( e->e_attrs, b->a_aci_at );
1361 ACL_RECORD_VALUE_STATE;
1362 /* the aci is an multi-valued attribute. The
1363 * rights are determined by OR'ing the individual
1364 * rights given by the acis.
1366 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1370 matches, &grant, &deny, &aci_bv_entry ) != 0)
1376 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1377 accessmask2str(tgrant,accessmaskbuf),
1378 accessmask2str(tdeny, accessmaskbuf1), 0);
1381 /* If the entry level aci didn't contain anything valid for the
1382 * current operation, climb up the tree and evaluate the
1383 * acis with scope set to subtree
1385 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1386 dnParent(&(e->e_nname), &parent_ndn);
1387 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1388 old_parent_ndn = parent_ndn;
1389 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1390 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1394 for( i = 0; bvals[i].bv_val != NULL; i++){
1395 ACL_RECORD_VALUE_STATE;
1396 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1397 &grant, &deny, &aci_bv_children) != 0) {
1400 /* evaluation stops as soon as either a "deny" or a
1401 * "grant" directive matches.
1403 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1407 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1408 accessmask2str(tgrant,accessmaskbuf),
1409 accessmask2str(tdeny, accessmaskbuf1), 0);
1414 case LDAP_NO_SUCH_ATTRIBUTE:
1415 /* just go on if the aci-Attribute is not present in
1418 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1421 case LDAP_NO_SUCH_OBJECT:
1422 /* We have reached the base object */
1423 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1433 dnParent(&old_parent_ndn, &parent_ndn);
1438 /* remove anything that the ACL clause does not allow */
1439 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1440 tdeny &= ACL_PRIV_MASK;
1442 /* see if we have anything to contribute */
1443 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1447 /* this could be improved by changing acl_mask so that it can deal with
1448 * by clauses that return grant/deny pairs. Right now, it does either
1449 * additive or subtractive rights, but not both at the same time. So,
1450 * we need to combine the grant/deny pair into a single rights mask in
1451 * a smart way: if either grant or deny is "empty", then we use the
1452 * opposite as is, otherwise we remove any denied rights from the grant
1453 * rights mask and construct an additive mask.
1455 if (ACL_IS_INVALID(tdeny)) {
1456 modmask = tgrant | ACL_PRIV_ADDITIVE;
1458 } else if (ACL_IS_INVALID(tgrant)) {
1459 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1462 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1468 modmask = b->a_access_mask;
1472 LDAP_LOG( ACL, RESULTS,
1473 "acl_mask: [%d] applying %s (%s)\n",
1474 i, accessmask2str( modmask, accessmaskbuf),
1475 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1476 ? "break" : "stop" );
1478 Debug( LDAP_DEBUG_ACL,
1479 "<= acl_mask: [%d] applying %s (%s)\n",
1480 i, accessmask2str( modmask, accessmaskbuf ),
1481 b->a_type == ACL_CONTINUE
1483 : b->a_type == ACL_BREAK
1490 if( ACL_IS_ADDITIVE(modmask) ) {
1492 ACL_PRIV_SET( *mask, modmask );
1495 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1497 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1498 /* substract privs */
1499 ACL_PRIV_CLR( *mask, modmask );
1502 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1510 LDAP_LOG( ACL, DETAIL1,
1511 "acl_mask: conn %lu [%d] mask: %s\n",
1512 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1514 Debug( LDAP_DEBUG_ACL,
1515 "<= acl_mask: [%d] mask: %s\n",
1516 i, accessmask2str(*mask, accessmaskbuf), 0 );
1519 if( b->a_type == ACL_CONTINUE ) {
1522 } else if ( b->a_type == ACL_BREAK ) {
1530 /* implicit "by * none" clause */
1534 LDAP_LOG( ACL, RESULTS,
1535 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1536 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1538 Debug( LDAP_DEBUG_ACL,
1539 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1540 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1546 * acl_check_modlist - check access control on the given entry to see if
1547 * it allows the given modifications by the user associated with op.
1548 * returns 1 if mods allowed ok
1549 * 0 mods not allowed
1556 Modifications *mlist
1560 AccessControlState state = ACL_STATE_INIT;
1563 int ret = 1; /* default is access allowed */
1571 assert( be != NULL );
1573 /* short circuit root database access */
1574 if ( be_isroot( op ) ) {
1576 LDAP_LOG( ACL, DETAIL1,
1577 "acl_check_modlist: conn %lu access granted to root user\n",
1578 op->o_connid, 0, 0 );
1580 Debug( LDAP_DEBUG_ACL,
1581 "<= acl_access_allowed: granted to database root\n",
1587 /* use backend default access if no backend acls */
1588 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1590 LDAP_LOG( ACL, DETAIL1,
1591 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1592 access2str( ACL_WRITE ),
1593 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1596 Debug( LDAP_DEBUG_ACL,
1597 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1598 access2str( ACL_WRITE ),
1599 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1601 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1605 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1607 * no-user-modification operational attributes are ignored
1608 * by ACL_WRITE checking as any found here are not provided
1611 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1613 LDAP_LOG( ACL, DETAIL1,
1614 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1615 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1617 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1618 " modify access granted\n",
1619 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1624 switch ( mlist->sml_op ) {
1625 case LDAP_MOD_REPLACE:
1627 * We must check both permission to delete the whole
1628 * attribute and permission to add the specific attributes.
1629 * This prevents abuse from selfwriters.
1631 if ( ! access_allowed( op, e,
1632 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1638 if ( mlist->sml_values == NULL ) break;
1640 /* fall thru to check value to add */
1643 assert( mlist->sml_values != NULL );
1645 for ( bv = mlist->sml_nvalues
1646 ? mlist->sml_nvalues : mlist->sml_values;
1647 bv->bv_val != NULL; bv++ )
1649 if ( ! access_allowed( op, e,
1650 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1658 case LDAP_MOD_DELETE:
1659 if ( mlist->sml_values == NULL ) {
1660 if ( ! access_allowed( op, e,
1661 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1668 for ( bv = mlist->sml_nvalues
1669 ? mlist->sml_nvalues : mlist->sml_values;
1670 bv->bv_val != NULL; bv++ )
1672 if ( ! access_allowed( op, e,
1673 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1681 case SLAP_MOD_SOFTADD:
1682 /* allow adding attribute via modrdn thru */
1694 if (be_null) op->o_bd = NULL;
1700 struct berval *list,
1714 while (len >= 0 && --ix >= 0) {
1715 while (--len >= 0 && *p++ != sep) ;
1717 while (len >= 0 && *p == ' ') {
1728 while (--len >= 0 && *p != sep) {
1732 while (bv->bv_len > 0 && *--p == ' ')
1738 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1740 AciSetCookie *cp = (AciSetCookie *)cookie;
1741 BerVarray bvals = NULL;
1744 /* this routine needs to return the bervals instead of
1745 * plain strings, since syntax is not known. It should
1746 * also return the syntax or some "comparison cookie".
1749 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1751 AttributeDescription *desc = NULL;
1752 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1753 backend_attribute(cp->op,
1754 cp->e, &ndn, desc, &bvals);
1756 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1763 struct berval *subj,
1769 struct berval set = BER_BVNULL;
1771 AciSetCookie cookie;
1774 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1776 struct berval subjdn, ndn = BER_BVNULL;
1777 struct berval setat;
1780 AttributeDescription *desc = NULL;
1782 /* format of string is "entry/setAttrName" */
1783 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1787 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1788 setat.bv_val = SLAPD_ACI_SET_ATTR;
1789 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1792 if ( setat.bv_val != NULL ) {
1794 * NOTE: dnNormalize honors the ber_len field
1795 * as the length of the dn to be normalized
1797 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1798 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1800 backend_attribute(op, e,
1801 &ndn, desc, &bvals);
1802 if ( bvals != NULL ) {
1803 if ( bvals[0].bv_val != NULL ) {
1806 bvals[0].bv_val = NULL;
1807 for (i=1;bvals[i].bv_val;i++);
1808 bvals[0].bv_val = bvals[i-1].bv_val;
1809 bvals[i-1].bv_val = NULL;
1811 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1819 if (set.bv_val != NULL) {
1822 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1823 &op->o_ndn, &e->e_nname, NULL) > 0);
1824 sl_free(set.bv_val, op->o_tmpmemctx);
1829 #ifdef SLAPD_ACI_ENABLED
1831 aci_list_map_rights(
1832 struct berval *list )
1839 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1842 switch (*bv.bv_val) {
1844 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1847 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1848 * the right 's' to mean "set", but in the examples states
1849 * that the right 's' means "search". The latter definition
1852 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1855 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1858 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1861 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1862 * define any equivalent to the AUTH right, so I've just used
1865 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1877 struct berval *list,
1878 const struct berval *attr,
1879 struct berval *val )
1881 struct berval bv, left, right;
1884 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1885 if (aci_get_part(&bv, 0, '=', &left) < 0
1886 || aci_get_part(&bv, 1, '=', &right) < 0)
1888 if (ber_bvstrcasecmp(attr, &bv) == 0)
1890 } else if (val == NULL) {
1891 if (ber_bvstrcasecmp(attr, &left) == 0)
1894 if (ber_bvstrcasecmp(attr, &left) == 0) {
1895 /* this is experimental code that implements a
1896 * simple (prefix) match of the attribute value.
1897 * the ACI draft does not provide for aci's that
1898 * apply to specific values, but it would be
1899 * nice to have. If the <attr> part of an aci's
1900 * rights list is of the form <attr>=<value>,
1901 * that means the aci applies only to attrs with
1902 * the given value. Furthermore, if the attr is
1903 * of the form <attr>=<value>*, then <value> is
1904 * treated as a prefix, and the aci applies to
1905 * any value with that prefix.
1907 * Ideally, this would allow r.e. matches.
1909 if (aci_get_part(&right, 0, '*', &left) < 0
1910 || right.bv_len <= left.bv_len)
1912 if (ber_bvstrcasecmp(val, &right) == 0)
1914 } else if (val->bv_len >= left.bv_len) {
1915 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1924 static slap_access_t
1925 aci_list_get_attr_rights(
1926 struct berval *list,
1927 const struct berval *attr,
1928 struct berval *val )
1934 /* loop through each rights/attr pair, skip first part (action) */
1936 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1937 if (aci_list_has_attr(&bv, attr, val) == 0)
1939 if (aci_get_part(list, i, ';', &bv) < 0)
1941 mask |= aci_list_map_rights(&bv);
1947 aci_list_get_rights(
1948 struct berval *list,
1949 const struct berval *attr,
1951 slap_access_t *grant,
1952 slap_access_t *deny )
1954 struct berval perm, actn;
1955 slap_access_t *mask;
1958 if (attr == NULL || attr->bv_len == 0
1959 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1960 attr = &aci_bv_br_entry;
1966 /* loop through each permissions clause */
1967 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1968 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1970 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1972 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1979 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1980 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1987 struct berval *subj,
1988 struct berval *defgrpoc,
1989 struct berval *defgrpat,
1995 struct berval subjdn;
1996 struct berval grpoc;
1997 struct berval grpat;
1998 ObjectClass *grp_oc = NULL;
1999 AttributeDescription *grp_ad = NULL;
2003 /* format of string is "group/objectClassValue/groupAttrName" */
2004 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2008 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2012 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2016 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2017 if( rc != LDAP_SUCCESS ) {
2023 grp_oc = oc_bvfind( &grpoc );
2025 if (grp_oc != NULL && grp_ad != NULL ) {
2026 char buf[ACL_BUF_SIZE];
2027 struct berval bv, ndn;
2028 bv.bv_len = sizeof( buf ) - 1;
2029 bv.bv_val = (char *)&buf;
2030 string_expand(&bv, &subjdn, e->e_ndn, matches);
2031 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2032 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2033 grp_oc, grp_ad) == 0);
2046 AttributeDescription *desc,
2049 regmatch_t *matches,
2050 slap_access_t *grant,
2051 slap_access_t *deny,
2052 struct berval *scope
2055 struct berval bv, perms, sdn;
2059 assert( desc->ad_cname.bv_val != NULL );
2061 /* parse an aci of the form:
2062 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2064 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2065 a full description of the format for this attribute.
2066 Differences: "this" in the draft is "self" here, and
2067 "self" and "public" is in the position of dnType.
2069 For now, this routine only supports scope=entry.
2071 /* check that the aci has all 5 components */
2072 if (aci_get_part(aci, 4, '#', NULL) < 0)
2075 /* check that the aci family is supported */
2076 if (aci_get_part(aci, 0, '#', &bv) < 0)
2079 /* check that the scope matches */
2080 if (aci_get_part(aci, 1, '#', &bv) < 0
2081 || ber_bvstrcasecmp( scope, &bv ) != 0)
2086 /* get the list of permissions clauses, bail if empty */
2087 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2090 /* check if any permissions allow desired access */
2091 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2094 /* see if we have a DN match */
2095 if (aci_get_part(aci, 3, '#', &bv) < 0)
2098 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2101 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2104 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2105 if (dn_match( &op->o_ndn, &ndn))
2111 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2114 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2115 if (dn_match(&op->o_ndn, &e->e_nname))
2118 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2120 AttributeDescription *ad = NULL;
2123 rc = slap_bv2ad( &sdn, &ad, &text );
2125 if( rc != LDAP_SUCCESS ) {
2133 for(at = attrs_find( e->e_attrs, ad );
2135 at = attrs_find( at->a_next, ad ) )
2137 if (value_find_ex( ad,
2138 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2139 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2141 &bv, op->o_tmpmemctx) == 0 )
2151 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2152 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2155 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2156 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2159 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2160 if (aci_match_set(&sdn, op, e, 0))
2163 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2164 if (aci_match_set(&sdn, op, e, 1))
2172 #endif /* SLAPD_ACI_ENABLED */
2179 regmatch_t *matches)
2187 bv->bv_val[0] = '\0';
2188 bv->bv_len--; /* leave space for lone $ */
2191 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2192 sp < pat->bv_val + pat->bv_len ; sp++ )
2194 /* did we previously see a $ */
2196 if ( flag == 1 && *sp == '$' ) {
2201 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2204 } else if ( *sp >= '0' && *sp <= '9' ) {
2212 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2213 if ( *sp >= '0' && *sp <= '9' ) {
2214 n = 10*n + ( *sp - '0' );
2218 if ( *sp != /*'{'*/ '}' ) {
2223 if ( n >= MAXREMATCHES ) {
2228 i = matches[n].rm_so;
2229 l = matches[n].rm_eo;
2230 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2248 /* must have ended with a single $ */
2257 LDAP_LOG( ACL, DETAIL1,
2258 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2259 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2261 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2262 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2268 struct berval *pat, /* pattern to expand and match against */
2269 char *str, /* string to match against pattern */
2270 char *buf, /* buffer with $N expansion variables */
2271 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2275 char newbuf[ACL_BUF_SIZE];
2279 bv.bv_len = sizeof(newbuf) - 1;
2282 if(str == NULL) str = "";
2284 string_expand(&bv, pat, buf, matches);
2285 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2286 char error[ACL_BUF_SIZE];
2287 regerror(rc, &re, error, sizeof(error));
2291 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2292 pat->bv_val, str, error );
2294 Debug( LDAP_DEBUG_TRACE,
2295 "compile( \"%s\", \"%s\") failed %s\n",
2296 pat->bv_val, str, error );
2301 rc = regexec(&re, str, 0, NULL, 0);
2305 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2306 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2307 rc, rc ? "matches" : "no matches", 0 );
2309 Debug( LDAP_DEBUG_TRACE,
2310 "=> regex_matches: string: %s\n", str, 0, 0 );
2311 Debug( LDAP_DEBUG_TRACE,
2312 "=> regex_matches: rc: %d %s\n",
2313 rc, !rc ? "matches" : "no matches", 0 );