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.
137 * - can be legally called with op == NULL
138 * - can be legally called with op->o_bd == NULL
145 AttributeDescription *desc,
147 slap_access_t access,
148 AccessControlState *state,
153 AccessControl *a = NULL;
158 char accessmaskbuf[ACCESSMASK_MAXLEN];
161 slap_control_t control;
163 regmatch_t matches[MAXREMATCHES];
164 int st_same_attr = 0;
165 static AccessControlState state_init = ACL_STATE_INIT;
168 assert( desc != NULL );
169 assert( access > ACL_NONE );
170 if ( maskp ) ACL_INVALIDATE( *maskp );
172 attr = desc->ad_cname.bv_val;
174 assert( attr != NULL );
176 if( op && op->o_is_auth_check &&
177 ( access == ACL_SEARCH || access == ACL_READ ))
183 if ( state->as_vd_ad==desc) {
184 if ( state->as_recorded ) {
185 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
188 return state->as_result;
189 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
190 val != NULL && state->as_vd_acl == NULL )
192 return state->as_result;
200 state->as_vd_ad=desc;
204 LDAP_LOG( ACL, ENTRY,
205 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
206 access2str( access ), e->e_dn, attr );
208 Debug( LDAP_DEBUG_ACL,
209 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
210 access2str( access ), e->e_dn, attr );
224 assert( be != NULL );
227 if ( op->o_pb != NULL ) {
228 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
230 /* ACL plugin denied access */
234 #endif /* LDAP_SLAPI */
236 /* grant database root access */
237 if ( be != NULL && be_isroot( op ) ) {
240 "access_allowed: conn %lu root access granted\n",
241 op->o_connid, 0, 0 );
243 Debug( LDAP_DEBUG_ACL,
244 "<= root access granted\n",
248 mask = ACL_LVL_WRITE;
255 * no-user-modification operational attributes are ignored
256 * by ACL_WRITE checking as any found here are not provided
259 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
260 && desc != slap_schema.si_ad_entry
261 && desc != slap_schema.si_ad_children )
264 LDAP_LOG( ACL, DETAIL1,
265 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
266 "access granted\n", op->o_connid, attr , 0 );
268 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
269 " %s access granted\n",
275 /* use backend default access if no backend acls */
276 if( be != NULL && be->be_acl == NULL ) {
278 LDAP_LOG( ACL, DETAIL1,
279 "access_allowed: backend default %s access %s to \"%s\"\n",
280 access2str( access ),
281 be->be_dfltaccess >= access ? "granted" : "denied",
282 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
284 Debug( LDAP_DEBUG_ACL,
285 "=> access_allowed: backend default %s access %s to \"%s\"\n",
286 access2str( access ),
287 be->be_dfltaccess >= access ? "granted" : "denied",
288 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
290 ret = be->be_dfltaccess >= access;
295 mask = ACL_PRIV_LEVEL;
296 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
297 mask |= ACL_ACCESS2PRIV( i );
304 /* be is always non-NULL */
305 /* use global default access if no global acls */
306 } else if ( be == NULL && global_acl == NULL ) {
308 LDAP_LOG( ACL, DETAIL1,
309 "access_allowed: global default %s access %s to \"%s\"\n",
310 access2str( access ),
311 global_default_access >= access ? "granted" : "denied",
314 Debug( LDAP_DEBUG_ACL,
315 "=> access_allowed: global default %s access %s to \"%s\"\n",
316 access2str( access ),
317 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
319 ret = global_default_access >= access;
324 mask = ACL_PRIV_LEVEL;
325 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
326 mask |= ACL_ACCESS2PRIV( i );
338 assert( state->as_vd_acl != NULL );
340 a = state->as_vd_acl;
341 count = state->as_vd_acl_count;
342 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
343 mask = state->as_vd_acl_mask;
344 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
349 if ( state ) state->as_vi_acl = NULL;
353 memset(matches, '\0', sizeof(matches));
356 while((a = acl_get( a, &count, op, e, desc, val,
357 MAXREMATCHES, matches, state )) != NULL)
361 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
363 LDAP_LOG( ACL, DETAIL1,
364 "access_allowed: match[%d]: %d %d ",
365 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
367 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
368 (int)matches[i].rm_so, (int)matches[i].rm_eo );
370 if( matches[i].rm_so <= matches[0].rm_eo ) {
372 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
373 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
377 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
379 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
384 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
385 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
386 ret = state->as_result;
389 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
394 control = acl_mask( a, &mask, op,
395 e, desc, val, matches, count, state );
397 if ( control != ACL_BREAK ) {
401 memset(matches, '\0', sizeof(matches));
404 if ( ACL_IS_INVALID( mask ) ) {
406 LDAP_LOG( ACL, DETAIL1,
407 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
408 op->o_connid, e->e_dn, attr );
410 Debug( LDAP_DEBUG_ACL,
411 "=> access_allowed: \"%s\" (%s) invalid!\n",
416 } else if ( control == ACL_BREAK ) {
418 LDAP_LOG( ACL, DETAIL1,
419 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
421 Debug( LDAP_DEBUG_ACL,
422 "=> access_allowed: no more rules\n", 0, 0, 0);
429 LDAP_LOG( ACL, ENTRY,
430 "access_allowed: %s access %s by %s\n",
431 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
432 accessmask2str( mask, accessmaskbuf ) );
434 Debug( LDAP_DEBUG_ACL,
435 "=> access_allowed: %s access %s by %s\n",
436 access2str( access ),
437 ACL_GRANT(mask, access) ? "granted" : "denied",
438 accessmask2str( mask, accessmaskbuf ) );
441 ret = ACL_GRANT(mask, access);
444 if( state != NULL ) {
445 /* If not value-dependent, save ACL in case of more attrs */
446 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
447 state->as_vi_acl = a;
448 state->as_result = ret;
450 state->as_recorded |= ACL_STATE_RECORDED;
452 if (be_null) op->o_bd = NULL;
453 if ( maskp ) *maskp = mask;
459 * acl_get - return the acl applicable to entry e, attribute
460 * attr. the acl returned is suitable for use in subsequent calls to
461 * acl_access_allowed().
464 static AccessControl *
470 AttributeDescription *desc,
474 AccessControlState *state )
481 assert( count != NULL );
482 assert( desc != NULL );
484 attr = desc->ad_cname.bv_val;
486 assert( attr != NULL );
489 if( op->o_bd == NULL ) {
492 a = op->o_bd->be_acl;
503 dnlen = e->e_nname.bv_len;
505 for ( ; a != NULL; a = a->acl_next ) {
508 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
509 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
511 LDAP_LOG( ACL, DETAIL1,
512 "acl_get: dnpat [%d] %s nsub: %d\n",
513 *count, a->acl_dn_pat.bv_val,
514 (int) a->acl_dn_re.re_nsub );
516 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
517 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
519 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
524 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
525 *count, a->acl_dn_pat.bv_val, 0 );
527 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
528 *count, a->acl_dn_pat.bv_val, 0 );
530 patlen = a->acl_dn_pat.bv_len;
531 if ( dnlen < patlen )
534 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
535 /* base dn -- entire object DN must match */
536 if ( dnlen != patlen )
539 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
540 int rdnlen = -1, sep = 0;
542 if ( dnlen <= patlen )
546 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
551 rdnlen = dn_rdnlen( NULL, &e->e_nname );
552 if ( rdnlen != dnlen - patlen - sep )
555 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
556 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
559 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
560 if ( dnlen <= patlen )
562 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
566 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
571 LDAP_LOG( ACL, DETAIL1,
572 "acl_get: [%d] matched\n", *count, 0, 0 );
574 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
579 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
580 matches[0].rm_so = matches[0].rm_eo = -1;
584 /* Is this ACL only for a specific value? */
585 if ( a->acl_attrval.bv_len ) {
590 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
591 state->as_recorded |= ACL_STATE_RECORDED_VD;
592 state->as_vd_acl = prev;
593 state->as_vd_acl_count = *count;
594 state->as_vd_access = a->acl_access;
595 state->as_vd_access_count = 1;
596 ACL_INVALIDATE( state->as_vd_acl_mask );
599 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
601 LDAP_LOG( ACL, DETAIL1,
602 "acl_get: valpat %s\n",
603 a->acl_attrval.bv_val, 0, 0 );
605 Debug( LDAP_DEBUG_ACL,
606 "acl_get: valpat %s\n",
607 a->acl_attrval.bv_val, 0, 0 );
609 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
615 LDAP_LOG( ACL, DETAIL1,
617 a->acl_attrval.bv_val, 0, 0 );
619 Debug( LDAP_DEBUG_ACL,
621 a->acl_attrval.bv_val, 0, 0 );
624 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
625 if (value_match( &match, desc,
626 desc->ad_type->sat_equality, 0,
627 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
634 patlen = a->acl_attrval.bv_len;
635 vdnlen = val->bv_len;
637 if ( vdnlen < patlen )
640 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
641 if ( vdnlen > patlen )
644 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
647 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
650 rdnlen = dn_rdnlen( NULL, val );
651 if ( rdnlen != vdnlen - patlen - 1 )
654 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
655 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
658 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
659 if ( vdnlen <= patlen )
662 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
666 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
672 if ( a->acl_filter != NULL ) {
673 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
674 if ( rc != LDAP_COMPARE_TRUE ) {
680 LDAP_LOG( ACL, DETAIL1,
681 "acl_get: [%d] attr %s\n", *count, attr ,0 );
683 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
690 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
692 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
698 * Record value-dependent access control state
700 #define ACL_RECORD_VALUE_STATE do { \
701 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
702 state->as_recorded |= ACL_STATE_RECORDED_VD; \
703 state->as_vd_acl = a; \
704 AC_MEMCPY( state->as_vd_acl_matches, matches, \
705 sizeof( state->as_vd_acl_matches )) ; \
706 state->as_vd_acl_count = count; \
707 state->as_vd_access = b; \
708 state->as_vd_access_count = i; \
713 * acl_mask - modifies mask based upon the given acl and the
714 * requested access to entry e, attribute attr, value val. if val
715 * is null, access to the whole attribute is assumed (all values).
717 * returns 0 access NOT allowed
721 static slap_control_t
727 AttributeDescription *desc,
731 AccessControlState *state )
733 int i, odnlen, patlen;
736 char accessmaskbuf[ACCESSMASK_MAXLEN];
737 char accessmaskbuf1[ACCESSMASK_MAXLEN];
742 assert( mask != NULL );
743 assert( desc != NULL );
745 attr = desc->ad_cname.bv_val;
747 assert( attr != NULL );
750 LDAP_LOG( ACL, ENTRY,
751 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
752 op->o_connid, e->e_dn, attr );
755 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
756 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
757 accessmask2str( *mask, accessmaskbuf ) );
759 Debug( LDAP_DEBUG_ACL,
760 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
763 Debug( LDAP_DEBUG_ACL,
764 "=> acl_mask: to %s by \"%s\", (%s) \n",
765 val ? "value" : "all values",
766 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
767 accessmask2str( *mask, accessmaskbuf ) );
771 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
772 && state->as_vd_acl == a )
774 b = state->as_vd_access;
775 i = state->as_vd_access_count;
782 for ( ; b != NULL; b = b->a_next, i++ ) {
783 slap_mask_t oldmask, modmask;
785 ACL_INVALIDATE( modmask );
787 /* AND <who> clauses */
788 if ( b->a_dn_pat.bv_len != 0 ) {
790 LDAP_LOG( ACL, DETAIL1,
791 "acl_mask: conn %lu check a_dn_pat: %s\n",
792 op->o_connid, b->a_dn_pat.bv_val ,0 );
794 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
795 b->a_dn_pat.bv_val, 0, 0);
798 * if access applies to the entry itself, and the
799 * user is bound as somebody in the same namespace as
800 * the entry, OR the given dn matches the dn pattern
802 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
803 if ( op->o_ndn.bv_len != 0 ) {
807 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
808 if ( op->o_ndn.bv_len == 0 ) {
812 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
813 if ( op->o_ndn.bv_len == 0 ) {
817 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
821 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
822 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
823 int ret = regex_matches( &b->a_dn_pat,
824 op->o_ndn.bv_val, e->e_ndn, matches );
835 if ( e->e_dn == NULL )
838 if ( b->a_dn_expand ) {
840 char buf[ACL_BUF_SIZE];
842 bv.bv_len = sizeof( buf ) - 1;
845 string_expand(&bv, &b->a_dn_pat,
847 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
848 /* did not expand to a valid dn */
856 odnlen = op->o_ndn.bv_len;
857 if ( odnlen < patlen ) {
858 goto dn_match_cleanup;
862 if ( b->a_dn_style == ACL_STYLE_BASE ) {
863 /* base dn -- entire object DN must match */
864 if ( odnlen != patlen ) {
865 goto dn_match_cleanup;
868 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
871 if ( odnlen <= patlen ) {
872 goto dn_match_cleanup;
875 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
876 goto dn_match_cleanup;
879 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
880 if ( rdnlen != odnlen - patlen - 1 ) {
881 goto dn_match_cleanup;
884 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
885 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
886 goto dn_match_cleanup;
889 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
890 if ( odnlen <= patlen ) {
891 goto dn_match_cleanup;
894 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
895 goto dn_match_cleanup;
899 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
902 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
912 if ( b->a_sockurl_pat.bv_len ) {
913 if ( ! op->o_conn->c_listener ) {
917 LDAP_LOG( ACL, DETAIL1,
918 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
919 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
921 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
922 b->a_sockurl_pat.bv_val, 0, 0 );
925 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
926 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
927 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
928 e->e_ndn, matches ) )
933 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
935 char buf[ACL_BUF_SIZE];
937 bv.bv_len = sizeof( buf ) - 1;
939 string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, matches );
941 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 ) {
946 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
952 if ( b->a_domain_pat.bv_len ) {
953 if ( !op->o_conn->c_peer_domain.bv_val ) {
957 LDAP_LOG( ACL, DETAIL1,
958 "acl_mask: conn %lu check a_domain_pat: %s\n",
959 op->o_connid, b->a_domain_pat.bv_val , 0 );
961 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
962 b->a_domain_pat.bv_val, 0, 0 );
964 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
965 if ( b->a_domain_style == ACL_STYLE_REGEX) {
966 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
967 e->e_ndn, matches ) )
972 char buf[ACL_BUF_SIZE];
974 struct berval cmp = op->o_conn->c_peer_domain;
975 struct berval pat = b->a_domain_pat;
977 if ( b->a_domain_expand ) {
980 bv.bv_len = sizeof(buf) - 1;
983 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
987 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
988 int offset = cmp.bv_len - pat.bv_len;
993 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
997 /* trim the domain */
998 cmp.bv_val = &cmp.bv_val[ offset ];
999 cmp.bv_len -= offset;
1002 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1009 if ( b->a_peername_pat.bv_len ) {
1010 if ( !op->o_conn->c_peer_name.bv_val ) {
1014 LDAP_LOG( ACL, DETAIL1,
1015 "acl_mask: conn %lu check a_peername_path: %s\n",
1016 op->o_connid, b->a_peername_pat.bv_val , 0 );
1018 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1019 b->a_peername_pat.bv_val, 0, 0 );
1021 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1022 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1023 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1024 e->e_ndn, matches ) )
1030 /* try exact match */
1031 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1032 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1036 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1038 char buf[ACL_BUF_SIZE];
1040 bv.bv_len = sizeof( buf ) - 1;
1042 string_expand( &bv, &b->a_peername_pat, e->e_ndn, matches );
1044 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1048 /* extract IP and try exact match */
1049 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1051 char buf[] = "255.255.255.255";
1054 int port_number = -1;
1056 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1057 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1060 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1061 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1063 port = strrchr( ip.bv_val, ':' );
1067 ip.bv_len = port - ip.bv_val;
1069 port_number = strtol( port, &next, 10 );
1070 if ( next[0] != '\0' )
1074 /* the port check can be anticipated here */
1075 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1078 /* address longer than expected? */
1079 if ( ip.bv_len >= sizeof(buf) )
1082 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1083 buf[ ip.bv_len ] = '\0';
1085 addr = inet_addr( buf );
1087 /* unable to convert? */
1088 if ( addr == (unsigned long)(-1) )
1091 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1094 #ifdef LDAP_PF_LOCAL
1095 /* extract path and try exact match */
1096 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1099 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1100 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1103 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1104 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1106 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1109 #endif /* LDAP_PF_LOCAL */
1111 /* exact match (very unlikely...) */
1112 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1119 if ( b->a_sockname_pat.bv_len ) {
1120 if ( !op->o_conn->c_sock_name.bv_val ) {
1124 LDAP_LOG( ACL, DETAIL1,
1125 "acl_mask: conn %lu check a_sockname_path: %s\n",
1126 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1128 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1129 b->a_sockname_pat.bv_val, 0, 0 );
1131 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1132 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1133 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1134 e->e_ndn, matches ) )
1139 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1141 char buf[ACL_BUF_SIZE];
1143 bv.bv_len = sizeof( buf ) - 1;
1145 string_expand( &bv, &b->a_sockname_pat, e->e_ndn, matches );
1147 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1152 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1158 if ( b->a_dn_at != NULL ) {
1163 const char *attr = b->a_dn_at->ad_cname.bv_val;
1165 assert( attr != NULL );
1167 if ( op->o_ndn.bv_len == 0 ) {
1172 LDAP_LOG( ACL, DETAIL1,
1173 "acl_mask: conn %lu check a_dn_pat: %s\n",
1174 op->o_connid, attr , 0 );
1176 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1181 /* see if asker is listed in dnattr */
1182 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1184 at = attrs_find( at->a_next, b->a_dn_at ) )
1186 if( value_find_ex( b->a_dn_at,
1187 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1188 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1190 &bv, op->o_tmpmemctx ) == 0 )
1199 /* have a dnattr match. if this is a self clause then
1200 * the target must also match the op dn.
1202 if ( b->a_dn_self ) {
1203 /* check if the target is an attribute. */
1204 if ( val == NULL ) continue;
1206 /* target is attribute, check if the attribute value
1209 rc = value_match( &match, b->a_dn_at,
1210 b->a_dn_at->ad_type->sat_equality, 0,
1212 /* on match error or no match, fail the ACL clause */
1213 if (rc != LDAP_SUCCESS || match != 0 )
1217 /* no dnattr match, check if this is a self clause */
1218 if ( ! b->a_dn_self )
1221 ACL_RECORD_VALUE_STATE;
1223 /* this is a self clause, check if the target is an
1229 /* target is attribute, check if the attribute value
1232 rc = value_match( &match, b->a_dn_at,
1233 b->a_dn_at->ad_type->sat_equality, 0,
1236 /* on match error or no match, fail the ACL clause */
1237 if (rc != LDAP_SUCCESS || match != 0 )
1242 if ( b->a_group_pat.bv_len ) {
1244 struct berval ndn = BER_BVNULL;
1247 if ( op->o_ndn.bv_len == 0 ) {
1251 /* b->a_group is an unexpanded entry name, expanded it should be an
1252 * entry with objectclass group* and we test to see if odn is one of
1253 * the values in the attribute group
1255 /* see if asker is listed in dnattr */
1256 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1257 char buf[ACL_BUF_SIZE];
1258 bv.bv_len = sizeof(buf) - 1;
1261 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1262 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1263 /* did not expand to a valid dn */
1270 bv = b->a_group_pat;
1273 rc = backend_group( op, e, &bv, &op->o_ndn,
1274 b->a_group_oc, b->a_group_at );
1276 if ( ndn.bv_val ) free( ndn.bv_val );
1283 if ( b->a_set_pat.bv_len != 0 ) {
1285 char buf[ACL_BUF_SIZE];
1286 if( b->a_set_style == ACL_STYLE_REGEX ){
1287 bv.bv_len = sizeof(buf) - 1;
1289 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1293 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1298 if ( b->a_authz.sai_ssf ) {
1300 LDAP_LOG( ACL, DETAIL1,
1301 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1302 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1304 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1305 b->a_authz.sai_ssf, op->o_ssf, 0 );
1307 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1312 if ( b->a_authz.sai_transport_ssf ) {
1314 LDAP_LOG( ACL, DETAIL1,
1315 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1317 op->o_connid, b->a_authz.sai_transport_ssf,
1318 op->o_transport_ssf );
1320 Debug( LDAP_DEBUG_ACL,
1321 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1322 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1324 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1329 if ( b->a_authz.sai_tls_ssf ) {
1331 LDAP_LOG( ACL, DETAIL1,
1332 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1334 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1336 Debug( LDAP_DEBUG_ACL,
1337 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1338 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1340 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1345 if ( b->a_authz.sai_sasl_ssf ) {
1347 LDAP_LOG( ACL, DETAIL1,
1348 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1350 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1352 Debug( LDAP_DEBUG_ACL,
1353 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1354 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1356 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1361 #ifdef SLAPD_ACI_ENABLED
1362 if ( b->a_aci_at != NULL ) {
1364 slap_access_t grant, deny, tgrant, tdeny;
1365 struct berval parent_ndn, old_parent_ndn;
1366 BerVarray bvals = NULL;
1369 /* this case works different from the others above.
1370 * since aci's themselves give permissions, we need
1371 * to first check b->a_access_mask, the ACL's access level.
1374 if ( e->e_nname.bv_len == 0 ) {
1375 /* no ACIs in the root DSE */
1379 /* first check if the right being requested
1380 * is allowed by the ACL clause.
1382 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1385 /* start out with nothing granted, nothing denied */
1389 /* get the aci attribute */
1390 at = attr_find( e->e_attrs, b->a_aci_at );
1392 ACL_RECORD_VALUE_STATE;
1393 /* the aci is an multi-valued attribute. The
1394 * rights are determined by OR'ing the individual
1395 * rights given by the acis.
1397 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1401 matches, &grant, &deny, &aci_bv_entry ) != 0)
1407 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1408 accessmask2str(tgrant,accessmaskbuf),
1409 accessmask2str(tdeny, accessmaskbuf1), 0);
1412 /* If the entry level aci didn't contain anything valid for the
1413 * current operation, climb up the tree and evaluate the
1414 * acis with scope set to subtree
1416 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1417 dnParent(&(e->e_nname), &parent_ndn);
1418 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1419 old_parent_ndn = parent_ndn;
1420 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1421 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1425 for( i = 0; bvals[i].bv_val != NULL; i++){
1426 ACL_RECORD_VALUE_STATE;
1427 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1428 &grant, &deny, &aci_bv_children) != 0) {
1431 /* evaluation stops as soon as either a "deny" or a
1432 * "grant" directive matches.
1434 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1438 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1439 accessmask2str(tgrant,accessmaskbuf),
1440 accessmask2str(tdeny, accessmaskbuf1), 0);
1445 case LDAP_NO_SUCH_ATTRIBUTE:
1446 /* just go on if the aci-Attribute is not present in
1449 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1452 case LDAP_NO_SUCH_OBJECT:
1453 /* We have reached the base object */
1454 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1464 dnParent(&old_parent_ndn, &parent_ndn);
1469 /* remove anything that the ACL clause does not allow */
1470 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1471 tdeny &= ACL_PRIV_MASK;
1473 /* see if we have anything to contribute */
1474 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1478 /* this could be improved by changing acl_mask so that it can deal with
1479 * by clauses that return grant/deny pairs. Right now, it does either
1480 * additive or subtractive rights, but not both at the same time. So,
1481 * we need to combine the grant/deny pair into a single rights mask in
1482 * a smart way: if either grant or deny is "empty", then we use the
1483 * opposite as is, otherwise we remove any denied rights from the grant
1484 * rights mask and construct an additive mask.
1486 if (ACL_IS_INVALID(tdeny)) {
1487 modmask = tgrant | ACL_PRIV_ADDITIVE;
1489 } else if (ACL_IS_INVALID(tgrant)) {
1490 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1493 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1499 modmask = b->a_access_mask;
1503 LDAP_LOG( ACL, RESULTS,
1504 "acl_mask: [%d] applying %s (%s)\n",
1505 i, accessmask2str( modmask, accessmaskbuf),
1506 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1507 ? "break" : "stop" );
1509 Debug( LDAP_DEBUG_ACL,
1510 "<= acl_mask: [%d] applying %s (%s)\n",
1511 i, accessmask2str( modmask, accessmaskbuf ),
1512 b->a_type == ACL_CONTINUE
1514 : b->a_type == ACL_BREAK
1521 if( ACL_IS_ADDITIVE(modmask) ) {
1523 ACL_PRIV_SET( *mask, modmask );
1526 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1528 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1529 /* substract privs */
1530 ACL_PRIV_CLR( *mask, modmask );
1533 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1541 LDAP_LOG( ACL, DETAIL1,
1542 "acl_mask: conn %lu [%d] mask: %s\n",
1543 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1545 Debug( LDAP_DEBUG_ACL,
1546 "<= acl_mask: [%d] mask: %s\n",
1547 i, accessmask2str(*mask, accessmaskbuf), 0 );
1550 if( b->a_type == ACL_CONTINUE ) {
1553 } else if ( b->a_type == ACL_BREAK ) {
1561 /* implicit "by * none" clause */
1565 LDAP_LOG( ACL, RESULTS,
1566 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1567 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1569 Debug( LDAP_DEBUG_ACL,
1570 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1571 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1577 * acl_check_modlist - check access control on the given entry to see if
1578 * it allows the given modifications by the user associated with op.
1579 * returns 1 if mods allowed ok
1580 * 0 mods not allowed
1587 Modifications *mlist
1591 AccessControlState state = ACL_STATE_INIT;
1594 int ret = 1; /* default is access allowed */
1602 assert( be != NULL );
1604 /* short circuit root database access */
1605 if ( be_isroot( op ) ) {
1607 LDAP_LOG( ACL, DETAIL1,
1608 "acl_check_modlist: conn %lu access granted to root user\n",
1609 op->o_connid, 0, 0 );
1611 Debug( LDAP_DEBUG_ACL,
1612 "<= acl_access_allowed: granted to database root\n",
1618 /* use backend default access if no backend acls */
1619 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1621 LDAP_LOG( ACL, DETAIL1,
1622 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1623 access2str( ACL_WRITE ),
1624 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1627 Debug( LDAP_DEBUG_ACL,
1628 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1629 access2str( ACL_WRITE ),
1630 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1632 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1636 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1638 * no-user-modification operational attributes are ignored
1639 * by ACL_WRITE checking as any found here are not provided
1642 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1644 LDAP_LOG( ACL, DETAIL1,
1645 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1646 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1648 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1649 " modify access granted\n",
1650 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1655 switch ( mlist->sml_op ) {
1656 case LDAP_MOD_REPLACE:
1658 * We must check both permission to delete the whole
1659 * attribute and permission to add the specific attributes.
1660 * This prevents abuse from selfwriters.
1662 if ( ! access_allowed( op, e,
1663 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1669 if ( mlist->sml_values == NULL ) break;
1671 /* fall thru to check value to add */
1674 assert( mlist->sml_values != NULL );
1676 for ( bv = mlist->sml_nvalues
1677 ? mlist->sml_nvalues : mlist->sml_values;
1678 bv->bv_val != NULL; bv++ )
1680 if ( ! access_allowed( op, e,
1681 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1689 case LDAP_MOD_DELETE:
1690 if ( mlist->sml_values == NULL ) {
1691 if ( ! access_allowed( op, e,
1692 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1699 for ( bv = mlist->sml_nvalues
1700 ? mlist->sml_nvalues : mlist->sml_values;
1701 bv->bv_val != NULL; bv++ )
1703 if ( ! access_allowed( op, e,
1704 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1712 case SLAP_MOD_SOFTADD:
1713 /* allow adding attribute via modrdn thru */
1725 if (be_null) op->o_bd = NULL;
1731 struct berval *list,
1745 while (len >= 0 && --ix >= 0) {
1746 while (--len >= 0 && *p++ != sep) ;
1748 while (len >= 0 && *p == ' ') {
1759 while (--len >= 0 && *p != sep) {
1763 while (bv->bv_len > 0 && *--p == ' ')
1769 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1771 AciSetCookie *cp = (AciSetCookie *)cookie;
1772 BerVarray bvals = NULL;
1775 /* this routine needs to return the bervals instead of
1776 * plain strings, since syntax is not known. It should
1777 * also return the syntax or some "comparison cookie".
1780 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1782 AttributeDescription *desc = NULL;
1783 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1784 backend_attribute(cp->op,
1785 cp->e, &ndn, desc, &bvals);
1787 slap_sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1794 struct berval *subj,
1800 struct berval set = BER_BVNULL;
1802 AciSetCookie cookie;
1805 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1807 struct berval subjdn, ndn = BER_BVNULL;
1808 struct berval setat;
1811 AttributeDescription *desc = NULL;
1813 /* format of string is "entry/setAttrName" */
1814 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1818 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1819 setat.bv_val = SLAPD_ACI_SET_ATTR;
1820 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1823 if ( setat.bv_val != NULL ) {
1825 * NOTE: dnNormalize honors the ber_len field
1826 * as the length of the dn to be normalized
1828 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1829 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1831 backend_attribute(op, e,
1832 &ndn, desc, &bvals);
1833 if ( bvals != NULL ) {
1834 if ( bvals[0].bv_val != NULL ) {
1837 bvals[0].bv_val = NULL;
1838 for (i=1;bvals[i].bv_val;i++);
1839 bvals[0].bv_val = bvals[i-1].bv_val;
1840 bvals[i-1].bv_val = NULL;
1842 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1850 if (set.bv_val != NULL) {
1853 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1854 &op->o_ndn, &e->e_nname, NULL) > 0);
1855 slap_sl_free(set.bv_val, op->o_tmpmemctx);
1860 #ifdef SLAPD_ACI_ENABLED
1862 aci_list_map_rights(
1863 struct berval *list )
1870 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1873 switch (*bv.bv_val) {
1875 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1878 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1879 * the right 's' to mean "set", but in the examples states
1880 * that the right 's' means "search". The latter definition
1883 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1886 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1889 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1892 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1893 * define any equivalent to the AUTH right, so I've just used
1896 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1908 struct berval *list,
1909 const struct berval *attr,
1910 struct berval *val )
1912 struct berval bv, left, right;
1915 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1916 if (aci_get_part(&bv, 0, '=', &left) < 0
1917 || aci_get_part(&bv, 1, '=', &right) < 0)
1919 if (ber_bvstrcasecmp(attr, &bv) == 0)
1921 } else if (val == NULL) {
1922 if (ber_bvstrcasecmp(attr, &left) == 0)
1925 if (ber_bvstrcasecmp(attr, &left) == 0) {
1926 /* this is experimental code that implements a
1927 * simple (prefix) match of the attribute value.
1928 * the ACI draft does not provide for aci's that
1929 * apply to specific values, but it would be
1930 * nice to have. If the <attr> part of an aci's
1931 * rights list is of the form <attr>=<value>,
1932 * that means the aci applies only to attrs with
1933 * the given value. Furthermore, if the attr is
1934 * of the form <attr>=<value>*, then <value> is
1935 * treated as a prefix, and the aci applies to
1936 * any value with that prefix.
1938 * Ideally, this would allow r.e. matches.
1940 if (aci_get_part(&right, 0, '*', &left) < 0
1941 || right.bv_len <= left.bv_len)
1943 if (ber_bvstrcasecmp(val, &right) == 0)
1945 } else if (val->bv_len >= left.bv_len) {
1946 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1955 static slap_access_t
1956 aci_list_get_attr_rights(
1957 struct berval *list,
1958 const struct berval *attr,
1959 struct berval *val )
1965 /* loop through each rights/attr pair, skip first part (action) */
1967 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1968 if (aci_list_has_attr(&bv, attr, val) == 0)
1970 if (aci_get_part(list, i, ';', &bv) < 0)
1972 mask |= aci_list_map_rights(&bv);
1978 aci_list_get_rights(
1979 struct berval *list,
1980 const struct berval *attr,
1982 slap_access_t *grant,
1983 slap_access_t *deny )
1985 struct berval perm, actn;
1986 slap_access_t *mask;
1989 if (attr == NULL || attr->bv_len == 0
1990 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1991 attr = &aci_bv_br_entry;
1997 /* loop through each permissions clause */
1998 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1999 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2001 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2003 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2010 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2011 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2018 struct berval *subj,
2019 struct berval *defgrpoc,
2020 struct berval *defgrpat,
2026 struct berval subjdn;
2027 struct berval grpoc;
2028 struct berval grpat;
2029 ObjectClass *grp_oc = NULL;
2030 AttributeDescription *grp_ad = NULL;
2034 /* format of string is "group/objectClassValue/groupAttrName" */
2035 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2039 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2043 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2047 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2048 if( rc != LDAP_SUCCESS ) {
2054 grp_oc = oc_bvfind( &grpoc );
2056 if (grp_oc != NULL && grp_ad != NULL ) {
2057 char buf[ACL_BUF_SIZE];
2058 struct berval bv, ndn;
2059 bv.bv_len = sizeof( buf ) - 1;
2060 bv.bv_val = (char *)&buf;
2061 string_expand(&bv, &subjdn, e->e_ndn, matches);
2062 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2063 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2064 grp_oc, grp_ad) == 0);
2077 AttributeDescription *desc,
2080 regmatch_t *matches,
2081 slap_access_t *grant,
2082 slap_access_t *deny,
2083 struct berval *scope
2086 struct berval bv, perms, sdn;
2090 assert( desc->ad_cname.bv_val != NULL );
2092 /* parse an aci of the form:
2093 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2095 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2096 a full description of the format for this attribute.
2097 Differences: "this" in the draft is "self" here, and
2098 "self" and "public" is in the position of dnType.
2100 For now, this routine only supports scope=entry.
2102 /* check that the aci has all 5 components */
2103 if (aci_get_part(aci, 4, '#', NULL) < 0)
2106 /* check that the aci family is supported */
2107 if (aci_get_part(aci, 0, '#', &bv) < 0)
2110 /* check that the scope matches */
2111 if (aci_get_part(aci, 1, '#', &bv) < 0
2112 || ber_bvstrcasecmp( scope, &bv ) != 0)
2117 /* get the list of permissions clauses, bail if empty */
2118 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2121 /* check if any permissions allow desired access */
2122 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2125 /* see if we have a DN match */
2126 if (aci_get_part(aci, 3, '#', &bv) < 0)
2129 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2132 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2135 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2136 if (dn_match( &op->o_ndn, &ndn))
2142 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2145 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2146 if (dn_match(&op->o_ndn, &e->e_nname))
2149 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2151 AttributeDescription *ad = NULL;
2154 rc = slap_bv2ad( &sdn, &ad, &text );
2156 if( rc != LDAP_SUCCESS ) {
2164 for(at = attrs_find( e->e_attrs, ad );
2166 at = attrs_find( at->a_next, ad ) )
2168 if (value_find_ex( ad,
2169 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2170 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2172 &bv, op->o_tmpmemctx) == 0 )
2182 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2183 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2186 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2187 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2190 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2191 if (aci_match_set(&sdn, op, e, 0))
2194 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2195 if (aci_match_set(&sdn, op, e, 1))
2203 #endif /* SLAPD_ACI_ENABLED */
2210 regmatch_t *matches)
2218 bv->bv_val[0] = '\0';
2219 bv->bv_len--; /* leave space for lone $ */
2222 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2223 sp < pat->bv_val + pat->bv_len ; sp++ )
2225 /* did we previously see a $ */
2227 if ( flag == 1 && *sp == '$' ) {
2232 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2235 } else if ( *sp >= '0' && *sp <= '9' ) {
2243 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2244 if ( *sp >= '0' && *sp <= '9' ) {
2245 n = 10*n + ( *sp - '0' );
2249 if ( *sp != /*'{'*/ '}' ) {
2254 if ( n >= MAXREMATCHES ) {
2259 i = matches[n].rm_so;
2260 l = matches[n].rm_eo;
2261 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2279 /* must have ended with a single $ */
2288 LDAP_LOG( ACL, DETAIL1,
2289 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2290 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2292 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2293 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2299 struct berval *pat, /* pattern to expand and match against */
2300 char *str, /* string to match against pattern */
2301 char *buf, /* buffer with $N expansion variables */
2302 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2306 char newbuf[ACL_BUF_SIZE];
2310 bv.bv_len = sizeof(newbuf) - 1;
2313 if(str == NULL) str = "";
2315 string_expand(&bv, pat, buf, matches);
2316 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2317 char error[ACL_BUF_SIZE];
2318 regerror(rc, &re, error, sizeof(error));
2322 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2323 pat->bv_val, str, error );
2325 Debug( LDAP_DEBUG_TRACE,
2326 "compile( \"%s\", \"%s\") failed %s\n",
2327 pat->bv_val, str, error );
2332 rc = regexec(&re, str, 0, NULL, 0);
2336 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2337 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2338 rc, rc ? "matches" : "no matches", 0 );
2340 Debug( LDAP_DEBUG_TRACE,
2341 "=> regex_matches: string: %s\n", str, 0, 0 );
2342 Debug( LDAP_DEBUG_TRACE,
2343 "=> regex_matches: rc: %d %s\n",
2344 rc, !rc ? "matches" : "no matches", 0 );