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 ) {
511 if ( dnlen <= patlen )
514 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
517 rdnlen = dn_rdnlen( NULL, &e->e_nname );
518 if ( rdnlen != dnlen - patlen - 1 )
521 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
522 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
525 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
526 if ( dnlen <= patlen )
528 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
532 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
537 LDAP_LOG( ACL, DETAIL1,
538 "acl_get: [%d] matched\n", *count, 0, 0 );
540 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
545 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
546 matches[0].rm_so = matches[0].rm_eo = -1;
550 /* Is this ACL only for a specific value? */
551 if ( a->acl_attrval.bv_len ) {
556 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
557 state->as_recorded |= ACL_STATE_RECORDED_VD;
558 state->as_vd_acl = prev;
559 state->as_vd_acl_count = *count;
560 state->as_vd_access = a->acl_access;
561 state->as_vd_access_count = 1;
562 ACL_INVALIDATE( state->as_vd_acl_mask );
565 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
567 LDAP_LOG( ACL, DETAIL1,
568 "acl_get: valpat %s\n",
569 a->acl_attrval.bv_val, 0, 0 );
571 Debug( LDAP_DEBUG_ACL,
572 "acl_get: valpat %s\n",
573 a->acl_attrval.bv_val, 0, 0 );
575 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
581 LDAP_LOG( ACL, DETAIL1,
583 a->acl_attrval.bv_val, 0, 0 );
585 Debug( LDAP_DEBUG_ACL,
587 a->acl_attrval.bv_val, 0, 0 );
590 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
591 if (value_match( &match, desc,
592 desc->ad_type->sat_equality, 0,
593 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
600 patlen = a->acl_attrval.bv_len;
601 vdnlen = val->bv_len;
603 if ( vdnlen < patlen )
606 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
607 if ( vdnlen > patlen )
610 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
613 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
616 rdnlen = dn_rdnlen( NULL, val );
617 if ( rdnlen != vdnlen - patlen - 1 )
620 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
621 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
624 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
625 if ( vdnlen <= patlen )
628 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
632 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
638 if ( a->acl_filter != NULL ) {
639 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
640 if ( rc != LDAP_COMPARE_TRUE ) {
646 LDAP_LOG( ACL, DETAIL1,
647 "acl_get: [%d] attr %s\n", *count, attr ,0 );
649 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
656 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
658 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
664 * Record value-dependent access control state
666 #define ACL_RECORD_VALUE_STATE do { \
667 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
668 state->as_recorded |= ACL_STATE_RECORDED_VD; \
669 state->as_vd_acl = a; \
670 AC_MEMCPY( state->as_vd_acl_matches, matches, \
671 sizeof( state->as_vd_acl_matches )) ; \
672 state->as_vd_acl_count = count; \
673 state->as_vd_access = b; \
674 state->as_vd_access_count = i; \
679 * acl_mask - modifies mask based upon the given acl and the
680 * requested access to entry e, attribute attr, value val. if val
681 * is null, access to the whole attribute is assumed (all values).
683 * returns 0 access NOT allowed
687 static slap_control_t
693 AttributeDescription *desc,
697 AccessControlState *state )
699 int i, odnlen, patlen;
702 char accessmaskbuf[ACCESSMASK_MAXLEN];
703 char accessmaskbuf1[ACCESSMASK_MAXLEN];
708 assert( mask != NULL );
709 assert( desc != NULL );
711 attr = desc->ad_cname.bv_val;
713 assert( attr != NULL );
716 LDAP_LOG( ACL, ENTRY,
717 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
718 op->o_connid, e->e_dn, attr );
721 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
722 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
723 accessmask2str( *mask, accessmaskbuf ) );
725 Debug( LDAP_DEBUG_ACL,
726 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
729 Debug( LDAP_DEBUG_ACL,
730 "=> acl_mask: to %s by \"%s\", (%s) \n",
731 val ? "value" : "all values",
732 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
733 accessmask2str( *mask, accessmaskbuf ) );
737 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
738 && state->as_vd_acl == a )
740 b = state->as_vd_access;
741 i = state->as_vd_access_count;
748 for ( ; b != NULL; b = b->a_next, i++ ) {
749 slap_mask_t oldmask, modmask;
751 ACL_INVALIDATE( modmask );
753 /* AND <who> clauses */
754 if ( b->a_dn_pat.bv_len != 0 ) {
756 LDAP_LOG( ACL, DETAIL1,
757 "acl_mask: conn %lu check a_dn_pat: %s\n",
758 op->o_connid, b->a_dn_pat.bv_val ,0 );
760 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
761 b->a_dn_pat.bv_val, 0, 0);
764 * if access applies to the entry itself, and the
765 * user is bound as somebody in the same namespace as
766 * the entry, OR the given dn matches the dn pattern
768 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
769 if ( op->o_ndn.bv_len != 0 ) {
773 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
774 if ( op->o_ndn.bv_len == 0 ) {
778 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
779 if ( op->o_ndn.bv_len == 0 ) {
783 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
787 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
788 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
789 int ret = regex_matches( &b->a_dn_pat,
790 op->o_ndn.bv_val, e->e_ndn, matches );
801 if ( e->e_dn == NULL )
804 if ( b->a_dn_expand ) {
806 char buf[ACL_BUF_SIZE];
808 bv.bv_len = sizeof( buf ) - 1;
811 string_expand(&bv, &b->a_dn_pat,
813 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
814 /* did not expand to a valid dn */
822 odnlen = op->o_ndn.bv_len;
823 if ( odnlen < patlen ) {
824 goto dn_match_cleanup;
828 if ( b->a_dn_style == ACL_STYLE_BASE ) {
829 /* base dn -- entire object DN must match */
830 if ( odnlen != patlen ) {
831 goto dn_match_cleanup;
834 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
837 if ( odnlen <= patlen ) {
838 goto dn_match_cleanup;
841 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
842 goto dn_match_cleanup;
845 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
846 if ( rdnlen != odnlen - patlen - 1 ) {
847 goto dn_match_cleanup;
850 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
851 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
852 goto dn_match_cleanup;
855 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
856 if ( odnlen <= patlen ) {
857 goto dn_match_cleanup;
860 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
861 goto dn_match_cleanup;
865 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
868 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
878 if ( b->a_sockurl_pat.bv_len ) {
879 if ( ! op->o_conn->c_listener ) {
883 LDAP_LOG( ACL, DETAIL1,
884 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
885 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
887 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
888 b->a_sockurl_pat.bv_val, 0, 0 );
891 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
892 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
893 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
894 e->e_ndn, matches ) )
899 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
901 char buf[ACL_BUF_SIZE];
903 bv.bv_len = sizeof( buf ) - 1;
905 string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, matches );
907 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 ) {
912 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
918 if ( b->a_domain_pat.bv_len ) {
919 if ( !op->o_conn->c_peer_domain.bv_val ) {
923 LDAP_LOG( ACL, DETAIL1,
924 "acl_mask: conn %lu check a_domain_pat: %s\n",
925 op->o_connid, b->a_domain_pat.bv_val , 0 );
927 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
928 b->a_domain_pat.bv_val, 0, 0 );
930 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
931 if ( b->a_domain_style == ACL_STYLE_REGEX) {
932 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
933 e->e_ndn, matches ) )
938 char buf[ACL_BUF_SIZE];
940 struct berval cmp = op->o_conn->c_peer_domain;
941 struct berval pat = b->a_domain_pat;
943 if ( b->a_domain_expand ) {
946 bv.bv_len = sizeof(buf) - 1;
949 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
953 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
954 int offset = cmp.bv_len - pat.bv_len;
959 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
963 /* trim the domain */
964 cmp.bv_val = &cmp.bv_val[ offset ];
965 cmp.bv_len -= offset;
968 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
975 if ( b->a_peername_pat.bv_len ) {
976 if ( !op->o_conn->c_peer_name.bv_val ) {
980 LDAP_LOG( ACL, DETAIL1,
981 "acl_mask: conn %lu check a_peername_path: %s\n",
982 op->o_connid, b->a_peername_pat.bv_val , 0 );
984 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
985 b->a_peername_pat.bv_val, 0, 0 );
987 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
988 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
989 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
990 e->e_ndn, matches ) )
996 /* try exact match */
997 if ( b->a_peername_style == ACL_STYLE_BASE ) {
998 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1002 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1004 char buf[ACL_BUF_SIZE];
1006 bv.bv_len = sizeof( buf ) - 1;
1008 string_expand( &bv, &b->a_peername_pat, e->e_ndn, matches );
1010 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1014 /* extract IP and try exact match */
1015 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1017 char buf[] = "255.255.255.255";
1020 int port_number = -1;
1022 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1023 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1026 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1027 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1029 port = strrchr( ip.bv_val, ':' );
1033 ip.bv_len = port - ip.bv_val;
1035 port_number = strtol( port, &next, 10 );
1036 if ( next[0] != '\0' )
1040 /* the port check can be anticipated here */
1041 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1044 /* address longer than expected? */
1045 if ( ip.bv_len >= sizeof(buf) )
1048 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1049 buf[ ip.bv_len ] = '\0';
1051 addr = inet_addr( buf );
1053 /* unable to convert? */
1054 if ( addr == (unsigned long)(-1) )
1057 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1060 #ifdef LDAP_PF_LOCAL
1061 /* extract path and try exact match */
1062 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1065 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1066 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1069 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1070 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1072 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1075 #endif /* LDAP_PF_LOCAL */
1077 /* exact match (very unlikely...) */
1078 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1085 if ( b->a_sockname_pat.bv_len ) {
1086 if ( !op->o_conn->c_sock_name.bv_val ) {
1090 LDAP_LOG( ACL, DETAIL1,
1091 "acl_mask: conn %lu check a_sockname_path: %s\n",
1092 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1094 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1095 b->a_sockname_pat.bv_val, 0, 0 );
1097 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1098 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1099 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1100 e->e_ndn, matches ) )
1105 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1107 char buf[ACL_BUF_SIZE];
1109 bv.bv_len = sizeof( buf ) - 1;
1111 string_expand( &bv, &b->a_sockname_pat, e->e_ndn, matches );
1113 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1118 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1124 if ( b->a_dn_at != NULL ) {
1129 const char *attr = b->a_dn_at->ad_cname.bv_val;
1131 assert( attr != NULL );
1133 if ( op->o_ndn.bv_len == 0 ) {
1138 LDAP_LOG( ACL, DETAIL1,
1139 "acl_mask: conn %lu check a_dn_pat: %s\n",
1140 op->o_connid, attr , 0 );
1142 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1147 /* see if asker is listed in dnattr */
1148 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1150 at = attrs_find( at->a_next, b->a_dn_at ) )
1152 if( value_find_ex( b->a_dn_at,
1153 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1154 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1156 &bv, op->o_tmpmemctx ) == 0 )
1165 /* have a dnattr match. if this is a self clause then
1166 * the target must also match the op dn.
1168 if ( b->a_dn_self ) {
1169 /* check if the target is an attribute. */
1170 if ( val == NULL ) continue;
1172 /* target is attribute, check if the attribute value
1175 rc = value_match( &match, b->a_dn_at,
1176 b->a_dn_at->ad_type->sat_equality, 0,
1178 /* on match error or no match, fail the ACL clause */
1179 if (rc != LDAP_SUCCESS || match != 0 )
1183 /* no dnattr match, check if this is a self clause */
1184 if ( ! b->a_dn_self )
1187 ACL_RECORD_VALUE_STATE;
1189 /* this is a self clause, check if the target is an
1195 /* target is attribute, check if the attribute value
1198 rc = value_match( &match, b->a_dn_at,
1199 b->a_dn_at->ad_type->sat_equality, 0,
1202 /* on match error or no match, fail the ACL clause */
1203 if (rc != LDAP_SUCCESS || match != 0 )
1208 if ( b->a_group_pat.bv_len ) {
1210 struct berval ndn = BER_BVNULL;
1213 if ( op->o_ndn.bv_len == 0 ) {
1217 /* b->a_group is an unexpanded entry name, expanded it should be an
1218 * entry with objectclass group* and we test to see if odn is one of
1219 * the values in the attribute group
1221 /* see if asker is listed in dnattr */
1222 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1223 char buf[ACL_BUF_SIZE];
1224 bv.bv_len = sizeof(buf) - 1;
1227 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1228 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1229 /* did not expand to a valid dn */
1236 bv = b->a_group_pat;
1239 rc = backend_group( op, e, &bv, &op->o_ndn,
1240 b->a_group_oc, b->a_group_at );
1242 if ( ndn.bv_val ) free( ndn.bv_val );
1249 if ( b->a_set_pat.bv_len != 0 ) {
1251 char buf[ACL_BUF_SIZE];
1252 if( b->a_set_style == ACL_STYLE_REGEX ){
1253 bv.bv_len = sizeof(buf) - 1;
1255 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1259 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1264 if ( b->a_authz.sai_ssf ) {
1266 LDAP_LOG( ACL, DETAIL1,
1267 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1268 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1270 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1271 b->a_authz.sai_ssf, op->o_ssf, 0 );
1273 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1278 if ( b->a_authz.sai_transport_ssf ) {
1280 LDAP_LOG( ACL, DETAIL1,
1281 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1283 op->o_connid, b->a_authz.sai_transport_ssf,
1284 op->o_transport_ssf );
1286 Debug( LDAP_DEBUG_ACL,
1287 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1288 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1290 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1295 if ( b->a_authz.sai_tls_ssf ) {
1297 LDAP_LOG( ACL, DETAIL1,
1298 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1300 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1302 Debug( LDAP_DEBUG_ACL,
1303 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1304 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1306 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1311 if ( b->a_authz.sai_sasl_ssf ) {
1313 LDAP_LOG( ACL, DETAIL1,
1314 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1316 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1318 Debug( LDAP_DEBUG_ACL,
1319 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1320 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1322 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1327 #ifdef SLAPD_ACI_ENABLED
1328 if ( b->a_aci_at != NULL ) {
1330 slap_access_t grant, deny, tgrant, tdeny;
1331 struct berval parent_ndn, old_parent_ndn;
1332 BerVarray bvals = NULL;
1335 /* this case works different from the others above.
1336 * since aci's themselves give permissions, we need
1337 * to first check b->a_access_mask, the ACL's access level.
1340 if ( e->e_nname.bv_len == 0 ) {
1341 /* no ACIs in the root DSE */
1345 /* first check if the right being requested
1346 * is allowed by the ACL clause.
1348 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1351 /* start out with nothing granted, nothing denied */
1355 /* get the aci attribute */
1356 at = attr_find( e->e_attrs, b->a_aci_at );
1358 ACL_RECORD_VALUE_STATE;
1359 /* the aci is an multi-valued attribute. The
1360 * rights are determined by OR'ing the individual
1361 * rights given by the acis.
1363 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1367 matches, &grant, &deny, &aci_bv_entry ) != 0)
1373 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1374 accessmask2str(tgrant,accessmaskbuf),
1375 accessmask2str(tdeny, accessmaskbuf1), 0);
1378 /* If the entry level aci didn't contain anything valid for the
1379 * current operation, climb up the tree and evaluate the
1380 * acis with scope set to subtree
1382 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1383 dnParent(&(e->e_nname), &parent_ndn);
1384 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1385 old_parent_ndn = parent_ndn;
1386 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1387 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1391 for( i = 0; bvals[i].bv_val != NULL; i++){
1392 ACL_RECORD_VALUE_STATE;
1393 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1394 &grant, &deny, &aci_bv_children) != 0) {
1397 /* evaluation stops as soon as either a "deny" or a
1398 * "grant" directive matches.
1400 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1404 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1405 accessmask2str(tgrant,accessmaskbuf),
1406 accessmask2str(tdeny, accessmaskbuf1), 0);
1411 case LDAP_NO_SUCH_ATTRIBUTE:
1412 /* just go on if the aci-Attribute is not present in
1415 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1418 case LDAP_NO_SUCH_OBJECT:
1419 /* We have reached the base object */
1420 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1430 dnParent(&old_parent_ndn, &parent_ndn);
1435 /* remove anything that the ACL clause does not allow */
1436 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1437 tdeny &= ACL_PRIV_MASK;
1439 /* see if we have anything to contribute */
1440 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1444 /* this could be improved by changing acl_mask so that it can deal with
1445 * by clauses that return grant/deny pairs. Right now, it does either
1446 * additive or subtractive rights, but not both at the same time. So,
1447 * we need to combine the grant/deny pair into a single rights mask in
1448 * a smart way: if either grant or deny is "empty", then we use the
1449 * opposite as is, otherwise we remove any denied rights from the grant
1450 * rights mask and construct an additive mask.
1452 if (ACL_IS_INVALID(tdeny)) {
1453 modmask = tgrant | ACL_PRIV_ADDITIVE;
1455 } else if (ACL_IS_INVALID(tgrant)) {
1456 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1459 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1465 modmask = b->a_access_mask;
1469 LDAP_LOG( ACL, RESULTS,
1470 "acl_mask: [%d] applying %s (%s)\n",
1471 i, accessmask2str( modmask, accessmaskbuf),
1472 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1473 ? "break" : "stop" );
1475 Debug( LDAP_DEBUG_ACL,
1476 "<= acl_mask: [%d] applying %s (%s)\n",
1477 i, accessmask2str( modmask, accessmaskbuf ),
1478 b->a_type == ACL_CONTINUE
1480 : b->a_type == ACL_BREAK
1487 if( ACL_IS_ADDITIVE(modmask) ) {
1489 ACL_PRIV_SET( *mask, modmask );
1492 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1494 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1495 /* substract privs */
1496 ACL_PRIV_CLR( *mask, modmask );
1499 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1507 LDAP_LOG( ACL, DETAIL1,
1508 "acl_mask: conn %lu [%d] mask: %s\n",
1509 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1511 Debug( LDAP_DEBUG_ACL,
1512 "<= acl_mask: [%d] mask: %s\n",
1513 i, accessmask2str(*mask, accessmaskbuf), 0 );
1516 if( b->a_type == ACL_CONTINUE ) {
1519 } else if ( b->a_type == ACL_BREAK ) {
1527 /* implicit "by * none" clause */
1531 LDAP_LOG( ACL, RESULTS,
1532 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1533 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1535 Debug( LDAP_DEBUG_ACL,
1536 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1537 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1543 * acl_check_modlist - check access control on the given entry to see if
1544 * it allows the given modifications by the user associated with op.
1545 * returns 1 if mods allowed ok
1546 * 0 mods not allowed
1553 Modifications *mlist
1557 AccessControlState state = ACL_STATE_INIT;
1560 int ret = 1; /* default is access allowed */
1568 assert( be != NULL );
1570 /* short circuit root database access */
1571 if ( be_isroot( op ) ) {
1573 LDAP_LOG( ACL, DETAIL1,
1574 "acl_check_modlist: conn %lu access granted to root user\n",
1575 op->o_connid, 0, 0 );
1577 Debug( LDAP_DEBUG_ACL,
1578 "<= acl_access_allowed: granted to database root\n",
1584 /* use backend default access if no backend acls */
1585 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1587 LDAP_LOG( ACL, DETAIL1,
1588 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1589 access2str( ACL_WRITE ),
1590 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1593 Debug( LDAP_DEBUG_ACL,
1594 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1595 access2str( ACL_WRITE ),
1596 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1598 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1602 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1604 * no-user-modification operational attributes are ignored
1605 * by ACL_WRITE checking as any found here are not provided
1608 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1610 LDAP_LOG( ACL, DETAIL1,
1611 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1612 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1614 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1615 " modify access granted\n",
1616 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1621 switch ( mlist->sml_op ) {
1622 case LDAP_MOD_REPLACE:
1624 * We must check both permission to delete the whole
1625 * attribute and permission to add the specific attributes.
1626 * This prevents abuse from selfwriters.
1628 if ( ! access_allowed( op, e,
1629 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1635 if ( mlist->sml_values == NULL ) break;
1637 /* fall thru to check value to add */
1640 assert( mlist->sml_values != NULL );
1642 for ( bv = mlist->sml_nvalues
1643 ? mlist->sml_nvalues : mlist->sml_values;
1644 bv->bv_val != NULL; bv++ )
1646 if ( ! access_allowed( op, e,
1647 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1655 case LDAP_MOD_DELETE:
1656 if ( mlist->sml_values == NULL ) {
1657 if ( ! access_allowed( op, e,
1658 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1665 for ( bv = mlist->sml_nvalues
1666 ? mlist->sml_nvalues : mlist->sml_values;
1667 bv->bv_val != NULL; bv++ )
1669 if ( ! access_allowed( op, e,
1670 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1678 case SLAP_MOD_SOFTADD:
1679 /* allow adding attribute via modrdn thru */
1691 if (be_null) op->o_bd = NULL;
1697 struct berval *list,
1711 while (len >= 0 && --ix >= 0) {
1712 while (--len >= 0 && *p++ != sep) ;
1714 while (len >= 0 && *p == ' ') {
1725 while (--len >= 0 && *p != sep) {
1729 while (bv->bv_len > 0 && *--p == ' ')
1735 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1737 AciSetCookie *cp = (AciSetCookie *)cookie;
1738 BerVarray bvals = NULL;
1741 /* this routine needs to return the bervals instead of
1742 * plain strings, since syntax is not known. It should
1743 * also return the syntax or some "comparison cookie".
1746 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1748 AttributeDescription *desc = NULL;
1749 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1750 backend_attribute(cp->op,
1751 cp->e, &ndn, desc, &bvals);
1753 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1760 struct berval *subj,
1766 struct berval set = BER_BVNULL;
1768 AciSetCookie cookie;
1771 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1773 struct berval subjdn, ndn = BER_BVNULL;
1774 struct berval setat;
1777 AttributeDescription *desc = NULL;
1779 /* format of string is "entry/setAttrName" */
1780 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1784 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1785 setat.bv_val = SLAPD_ACI_SET_ATTR;
1786 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1789 if ( setat.bv_val != NULL ) {
1791 * NOTE: dnNormalize honors the ber_len field
1792 * as the length of the dn to be normalized
1794 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1795 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1797 backend_attribute(op, e,
1798 &ndn, desc, &bvals);
1799 if ( bvals != NULL ) {
1800 if ( bvals[0].bv_val != NULL ) {
1803 bvals[0].bv_val = NULL;
1804 for (i=1;bvals[i].bv_val;i++);
1805 bvals[0].bv_val = bvals[i-1].bv_val;
1806 bvals[i-1].bv_val = NULL;
1808 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1816 if (set.bv_val != NULL) {
1819 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1820 &op->o_ndn, &e->e_nname, NULL) > 0);
1821 sl_free(set.bv_val, op->o_tmpmemctx);
1826 #ifdef SLAPD_ACI_ENABLED
1828 aci_list_map_rights(
1829 struct berval *list )
1836 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1839 switch (*bv.bv_val) {
1841 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1844 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1845 * the right 's' to mean "set", but in the examples states
1846 * that the right 's' means "search". The latter definition
1849 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1852 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1855 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1858 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1859 * define any equivalent to the AUTH right, so I've just used
1862 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1874 struct berval *list,
1875 const struct berval *attr,
1876 struct berval *val )
1878 struct berval bv, left, right;
1881 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1882 if (aci_get_part(&bv, 0, '=', &left) < 0
1883 || aci_get_part(&bv, 1, '=', &right) < 0)
1885 if (ber_bvstrcasecmp(attr, &bv) == 0)
1887 } else if (val == NULL) {
1888 if (ber_bvstrcasecmp(attr, &left) == 0)
1891 if (ber_bvstrcasecmp(attr, &left) == 0) {
1892 /* this is experimental code that implements a
1893 * simple (prefix) match of the attribute value.
1894 * the ACI draft does not provide for aci's that
1895 * apply to specific values, but it would be
1896 * nice to have. If the <attr> part of an aci's
1897 * rights list is of the form <attr>=<value>,
1898 * that means the aci applies only to attrs with
1899 * the given value. Furthermore, if the attr is
1900 * of the form <attr>=<value>*, then <value> is
1901 * treated as a prefix, and the aci applies to
1902 * any value with that prefix.
1904 * Ideally, this would allow r.e. matches.
1906 if (aci_get_part(&right, 0, '*', &left) < 0
1907 || right.bv_len <= left.bv_len)
1909 if (ber_bvstrcasecmp(val, &right) == 0)
1911 } else if (val->bv_len >= left.bv_len) {
1912 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1921 static slap_access_t
1922 aci_list_get_attr_rights(
1923 struct berval *list,
1924 const struct berval *attr,
1925 struct berval *val )
1931 /* loop through each rights/attr pair, skip first part (action) */
1933 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1934 if (aci_list_has_attr(&bv, attr, val) == 0)
1936 if (aci_get_part(list, i, ';', &bv) < 0)
1938 mask |= aci_list_map_rights(&bv);
1944 aci_list_get_rights(
1945 struct berval *list,
1946 const struct berval *attr,
1948 slap_access_t *grant,
1949 slap_access_t *deny )
1951 struct berval perm, actn;
1952 slap_access_t *mask;
1955 if (attr == NULL || attr->bv_len == 0
1956 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1957 attr = &aci_bv_br_entry;
1963 /* loop through each permissions clause */
1964 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1965 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1967 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1969 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1976 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1977 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1984 struct berval *subj,
1985 struct berval *defgrpoc,
1986 struct berval *defgrpat,
1992 struct berval subjdn;
1993 struct berval grpoc;
1994 struct berval grpat;
1995 ObjectClass *grp_oc = NULL;
1996 AttributeDescription *grp_ad = NULL;
2000 /* format of string is "group/objectClassValue/groupAttrName" */
2001 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2005 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2009 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2013 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2014 if( rc != LDAP_SUCCESS ) {
2020 grp_oc = oc_bvfind( &grpoc );
2022 if (grp_oc != NULL && grp_ad != NULL ) {
2023 char buf[ACL_BUF_SIZE];
2024 struct berval bv, ndn;
2025 bv.bv_len = sizeof( buf ) - 1;
2026 bv.bv_val = (char *)&buf;
2027 string_expand(&bv, &subjdn, e->e_ndn, matches);
2028 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2029 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2030 grp_oc, grp_ad) == 0);
2043 AttributeDescription *desc,
2046 regmatch_t *matches,
2047 slap_access_t *grant,
2048 slap_access_t *deny,
2049 struct berval *scope
2052 struct berval bv, perms, sdn;
2056 assert( desc->ad_cname.bv_val != NULL );
2058 /* parse an aci of the form:
2059 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2061 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2062 a full description of the format for this attribute.
2063 Differences: "this" in the draft is "self" here, and
2064 "self" and "public" is in the position of dnType.
2066 For now, this routine only supports scope=entry.
2068 /* check that the aci has all 5 components */
2069 if (aci_get_part(aci, 4, '#', NULL) < 0)
2072 /* check that the aci family is supported */
2073 if (aci_get_part(aci, 0, '#', &bv) < 0)
2076 /* check that the scope matches */
2077 if (aci_get_part(aci, 1, '#', &bv) < 0
2078 || ber_bvstrcasecmp( scope, &bv ) != 0)
2083 /* get the list of permissions clauses, bail if empty */
2084 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2087 /* check if any permissions allow desired access */
2088 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2091 /* see if we have a DN match */
2092 if (aci_get_part(aci, 3, '#', &bv) < 0)
2095 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2098 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2101 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2102 if (dn_match( &op->o_ndn, &ndn))
2108 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2111 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2112 if (dn_match(&op->o_ndn, &e->e_nname))
2115 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2117 AttributeDescription *ad = NULL;
2120 rc = slap_bv2ad( &sdn, &ad, &text );
2122 if( rc != LDAP_SUCCESS ) {
2130 for(at = attrs_find( e->e_attrs, ad );
2132 at = attrs_find( at->a_next, ad ) )
2134 if (value_find_ex( ad,
2135 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2136 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2138 &bv, op->o_tmpmemctx) == 0 )
2148 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2149 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2152 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2153 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2156 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2157 if (aci_match_set(&sdn, op, e, 0))
2160 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2161 if (aci_match_set(&sdn, op, e, 1))
2169 #endif /* SLAPD_ACI_ENABLED */
2176 regmatch_t *matches)
2184 bv->bv_val[0] = '\0';
2185 bv->bv_len--; /* leave space for lone $ */
2188 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2189 sp < pat->bv_val + pat->bv_len ; sp++ )
2191 /* did we previously see a $ */
2193 if ( flag == 1 && *sp == '$' ) {
2198 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2201 } else if ( *sp >= '0' && *sp <= '9' ) {
2209 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2210 if ( *sp >= '0' && *sp <= '9' ) {
2211 n = 10*n + ( *sp - '0' );
2215 if ( *sp != /*'{'*/ '}' ) {
2220 if ( n >= MAXREMATCHES ) {
2225 i = matches[n].rm_so;
2226 l = matches[n].rm_eo;
2227 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2245 /* must have ended with a single $ */
2254 LDAP_LOG( ACL, DETAIL1,
2255 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2256 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2258 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2259 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2265 struct berval *pat, /* pattern to expand and match against */
2266 char *str, /* string to match against pattern */
2267 char *buf, /* buffer with $N expansion variables */
2268 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2272 char newbuf[ACL_BUF_SIZE];
2276 bv.bv_len = sizeof(newbuf) - 1;
2279 if(str == NULL) str = "";
2281 string_expand(&bv, pat, buf, matches);
2282 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2283 char error[ACL_BUF_SIZE];
2284 regerror(rc, &re, error, sizeof(error));
2288 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2289 pat->bv_val, str, error );
2291 Debug( LDAP_DEBUG_TRACE,
2292 "compile( \"%s\", \"%s\") failed %s\n",
2293 pat->bv_val, str, error );
2298 rc = regexec(&re, str, 0, NULL, 0);
2302 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2303 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2304 rc, rc ? "matches" : "no matches", 0 );
2306 Debug( LDAP_DEBUG_TRACE,
2307 "=> regex_matches: string: %s\n", str, 0, 0 );
2308 Debug( LDAP_DEBUG_TRACE,
2309 "=> regex_matches: rc: %d %s\n",
2310 rc, !rc ? "matches" : "no matches", 0 );