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 * FIXME: experimental; use first backend rules
225 * iff there is no global_acl (ITS#3100) */
226 if ( global_acl == NULL )
232 assert( be != NULL );
235 if ( op->o_pb != NULL ) {
236 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
238 /* ACL plugin denied access */
242 #endif /* LDAP_SLAPI */
244 /* grant database root access */
245 if ( be != NULL && be_isroot( op ) ) {
248 "access_allowed: conn %lu root access granted\n",
249 op->o_connid, 0, 0 );
251 Debug( LDAP_DEBUG_ACL,
252 "<= root access granted\n",
256 mask = ACL_LVL_WRITE;
263 * no-user-modification operational attributes are ignored
264 * by ACL_WRITE checking as any found here are not provided
267 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
268 && desc != slap_schema.si_ad_entry
269 && desc != slap_schema.si_ad_children )
272 LDAP_LOG( ACL, DETAIL1,
273 "access_allowed: conn %lu NoUserMod Operational attribute: %s "
274 "access granted\n", op->o_connid, attr , 0 );
276 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
277 " %s access granted\n",
283 /* use backend default access if no backend acls */
284 if( be != NULL && be->be_acl == NULL ) {
286 LDAP_LOG( ACL, DETAIL1,
287 "access_allowed: backend default %s access %s to \"%s\"\n",
288 access2str( access ),
289 be->be_dfltaccess >= access ? "granted" : "denied",
290 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
292 Debug( LDAP_DEBUG_ACL,
293 "=> access_allowed: backend default %s access %s to \"%s\"\n",
294 access2str( access ),
295 be->be_dfltaccess >= access ? "granted" : "denied",
296 op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
298 ret = be->be_dfltaccess >= access;
303 mask = ACL_PRIV_LEVEL;
304 for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
305 mask |= ACL_ACCESS2PRIV( i );
312 /* be is always non-NULL */
313 /* use global default access if no global acls */
314 } else if ( be == NULL && global_acl == NULL ) {
316 LDAP_LOG( ACL, DETAIL1,
317 "access_allowed: global default %s access %s to \"%s\"\n",
318 access2str( access ),
319 global_default_access >= access ? "granted" : "denied",
322 Debug( LDAP_DEBUG_ACL,
323 "=> access_allowed: global default %s access %s to \"%s\"\n",
324 access2str( access ),
325 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
327 ret = global_default_access >= access;
332 mask = ACL_PRIV_LEVEL;
333 for ( i = ACL_NONE; i <= global_default_access; i++ ) {
334 mask |= ACL_ACCESS2PRIV( i );
346 assert( state->as_vd_acl != NULL );
348 a = state->as_vd_acl;
349 count = state->as_vd_acl_count;
350 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
351 mask = state->as_vd_acl_mask;
352 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
357 if ( state ) state->as_vi_acl = NULL;
361 memset(matches, '\0', sizeof(matches));
364 while((a = acl_get( a, &count, op, e, desc, val,
365 MAXREMATCHES, matches, state )) != NULL)
369 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
371 LDAP_LOG( ACL, DETAIL1,
372 "access_allowed: match[%d]: %d %d ",
373 i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
375 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
376 (int)matches[i].rm_so, (int)matches[i].rm_eo );
378 if( matches[i].rm_so <= matches[0].rm_eo ) {
380 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
381 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
385 LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
387 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
392 if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
393 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
394 ret = state->as_result;
397 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
402 control = acl_mask( a, &mask, op,
403 e, desc, val, matches, count, state );
405 if ( control != ACL_BREAK ) {
409 memset(matches, '\0', sizeof(matches));
412 if ( ACL_IS_INVALID( mask ) ) {
414 LDAP_LOG( ACL, DETAIL1,
415 "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
416 op->o_connid, e->e_dn, attr );
418 Debug( LDAP_DEBUG_ACL,
419 "=> access_allowed: \"%s\" (%s) invalid!\n",
424 } else if ( control == ACL_BREAK ) {
426 LDAP_LOG( ACL, DETAIL1,
427 "access_allowed: conn %lu no more rules\n", op->o_connid, 0,0 );
429 Debug( LDAP_DEBUG_ACL,
430 "=> access_allowed: no more rules\n", 0, 0, 0);
437 LDAP_LOG( ACL, ENTRY,
438 "access_allowed: %s access %s by %s\n",
439 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
440 accessmask2str( mask, accessmaskbuf ) );
442 Debug( LDAP_DEBUG_ACL,
443 "=> access_allowed: %s access %s by %s\n",
444 access2str( access ),
445 ACL_GRANT(mask, access) ? "granted" : "denied",
446 accessmask2str( mask, accessmaskbuf ) );
449 ret = ACL_GRANT(mask, access);
452 if( state != NULL ) {
453 /* If not value-dependent, save ACL in case of more attrs */
454 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
455 state->as_vi_acl = a;
456 state->as_result = ret;
458 state->as_recorded |= ACL_STATE_RECORDED;
460 if (be_null) op->o_bd = NULL;
461 if ( maskp ) *maskp = mask;
467 * acl_get - return the acl applicable to entry e, attribute
468 * attr. the acl returned is suitable for use in subsequent calls to
469 * acl_access_allowed().
472 static AccessControl *
478 AttributeDescription *desc,
482 AccessControlState *state )
489 assert( count != NULL );
490 assert( desc != NULL );
492 attr = desc->ad_cname.bv_val;
494 assert( attr != NULL );
497 if( op->o_bd == NULL ) {
500 a = op->o_bd->be_acl;
511 dnlen = e->e_nname.bv_len;
513 for ( ; a != NULL; a = a->acl_next ) {
516 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
517 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
519 LDAP_LOG( ACL, DETAIL1,
520 "acl_get: dnpat [%d] %s nsub: %d\n",
521 *count, a->acl_dn_pat.bv_val,
522 (int) a->acl_dn_re.re_nsub );
524 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
525 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
527 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
532 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
533 *count, a->acl_dn_pat.bv_val, 0 );
535 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
536 *count, a->acl_dn_pat.bv_val, 0 );
538 patlen = a->acl_dn_pat.bv_len;
539 if ( dnlen < patlen )
542 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
543 /* base dn -- entire object DN must match */
544 if ( dnlen != patlen )
547 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
548 int rdnlen = -1, sep = 0;
550 if ( dnlen <= patlen )
554 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
559 rdnlen = dn_rdnlen( NULL, &e->e_nname );
560 if ( rdnlen != dnlen - patlen - sep )
563 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
564 if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
567 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
568 if ( dnlen <= patlen )
570 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
574 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
579 LDAP_LOG( ACL, DETAIL1,
580 "acl_get: [%d] matched\n", *count, 0, 0 );
582 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
587 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
588 matches[0].rm_so = matches[0].rm_eo = -1;
592 /* Is this ACL only for a specific value? */
593 if ( a->acl_attrval.bv_len ) {
598 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
599 state->as_recorded |= ACL_STATE_RECORDED_VD;
600 state->as_vd_acl = prev;
601 state->as_vd_acl_count = *count;
602 state->as_vd_access = a->acl_access;
603 state->as_vd_access_count = 1;
604 ACL_INVALIDATE( state->as_vd_acl_mask );
607 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
609 LDAP_LOG( ACL, DETAIL1,
610 "acl_get: valpat %s\n",
611 a->acl_attrval.bv_val, 0, 0 );
613 Debug( LDAP_DEBUG_ACL,
614 "acl_get: valpat %s\n",
615 a->acl_attrval.bv_val, 0, 0 );
617 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
623 LDAP_LOG( ACL, DETAIL1,
625 a->acl_attrval.bv_val, 0, 0 );
627 Debug( LDAP_DEBUG_ACL,
629 a->acl_attrval.bv_val, 0, 0 );
632 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
633 if (value_match( &match, desc,
634 desc->ad_type->sat_equality, 0,
635 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
642 patlen = a->acl_attrval.bv_len;
643 vdnlen = val->bv_len;
645 if ( vdnlen < patlen )
648 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
649 if ( vdnlen > patlen )
652 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
655 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
658 rdnlen = dn_rdnlen( NULL, val );
659 if ( rdnlen != vdnlen - patlen - 1 )
662 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
663 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
666 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
667 if ( vdnlen <= patlen )
670 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
674 if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
680 if ( a->acl_filter != NULL ) {
681 ber_int_t rc = test_filter( NULL, e, a->acl_filter );
682 if ( rc != LDAP_COMPARE_TRUE ) {
688 LDAP_LOG( ACL, DETAIL1,
689 "acl_get: [%d] attr %s\n", *count, attr ,0 );
691 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
698 LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
700 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
706 * Record value-dependent access control state
708 #define ACL_RECORD_VALUE_STATE do { \
709 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
710 state->as_recorded |= ACL_STATE_RECORDED_VD; \
711 state->as_vd_acl = a; \
712 AC_MEMCPY( state->as_vd_acl_matches, matches, \
713 sizeof( state->as_vd_acl_matches )) ; \
714 state->as_vd_acl_count = count; \
715 state->as_vd_access = b; \
716 state->as_vd_access_count = i; \
721 * acl_mask - modifies mask based upon the given acl and the
722 * requested access to entry e, attribute attr, value val. if val
723 * is null, access to the whole attribute is assumed (all values).
725 * returns 0 access NOT allowed
729 static slap_control_t
735 AttributeDescription *desc,
739 AccessControlState *state )
741 int i, odnlen, patlen;
744 char accessmaskbuf[ACCESSMASK_MAXLEN];
745 char accessmaskbuf1[ACCESSMASK_MAXLEN];
750 assert( mask != NULL );
751 assert( desc != NULL );
753 attr = desc->ad_cname.bv_val;
755 assert( attr != NULL );
758 LDAP_LOG( ACL, ENTRY,
759 "acl_mask: conn %lu access to entry \"%s\", attr \"%s\" requested\n",
760 op->o_connid, e->e_dn, attr );
763 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
764 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
765 accessmask2str( *mask, accessmaskbuf ) );
767 Debug( LDAP_DEBUG_ACL,
768 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
771 Debug( LDAP_DEBUG_ACL,
772 "=> acl_mask: to %s by \"%s\", (%s) \n",
773 val ? "value" : "all values",
774 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
775 accessmask2str( *mask, accessmaskbuf ) );
779 if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
780 && state->as_vd_acl == a )
782 b = state->as_vd_access;
783 i = state->as_vd_access_count;
790 for ( ; b != NULL; b = b->a_next, i++ ) {
791 slap_mask_t oldmask, modmask;
793 ACL_INVALIDATE( modmask );
795 /* AND <who> clauses */
796 if ( b->a_dn_pat.bv_len != 0 ) {
798 LDAP_LOG( ACL, DETAIL1,
799 "acl_mask: conn %lu check a_dn_pat: %s\n",
800 op->o_connid, b->a_dn_pat.bv_val ,0 );
802 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
803 b->a_dn_pat.bv_val, 0, 0);
806 * if access applies to the entry itself, and the
807 * user is bound as somebody in the same namespace as
808 * the entry, OR the given dn matches the dn pattern
810 if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
811 if ( op->o_ndn.bv_len != 0 ) {
815 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
816 if ( op->o_ndn.bv_len == 0 ) {
820 } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
821 if ( op->o_ndn.bv_len == 0 ) {
825 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
829 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
830 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
831 int ret = regex_matches( &b->a_dn_pat,
832 op->o_ndn.bv_val, e->e_ndn, matches );
843 if ( e->e_dn == NULL )
846 if ( b->a_dn_expand ) {
848 char buf[ACL_BUF_SIZE];
850 bv.bv_len = sizeof( buf ) - 1;
853 string_expand(&bv, &b->a_dn_pat,
855 if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
856 /* did not expand to a valid dn */
864 odnlen = op->o_ndn.bv_len;
865 if ( odnlen < patlen ) {
866 goto dn_match_cleanup;
870 if ( b->a_dn_style == ACL_STYLE_BASE ) {
871 /* base dn -- entire object DN must match */
872 if ( odnlen != patlen ) {
873 goto dn_match_cleanup;
876 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
879 if ( odnlen <= patlen ) {
880 goto dn_match_cleanup;
883 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
884 goto dn_match_cleanup;
887 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
888 if ( rdnlen != odnlen - patlen - 1 ) {
889 goto dn_match_cleanup;
892 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
893 if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
894 goto dn_match_cleanup;
897 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
898 if ( odnlen <= patlen ) {
899 goto dn_match_cleanup;
902 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
903 goto dn_match_cleanup;
907 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
910 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
920 if ( b->a_sockurl_pat.bv_len ) {
921 if ( ! op->o_conn->c_listener ) {
925 LDAP_LOG( ACL, DETAIL1,
926 "acl_mask: conn %lu check a_sockurl_pat: %s\n",
927 op->o_connid, b->a_sockurl_pat.bv_val , 0 );
929 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
930 b->a_sockurl_pat.bv_val, 0, 0 );
933 if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
934 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
935 if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
936 e->e_ndn, matches ) )
941 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
943 char buf[ACL_BUF_SIZE];
945 bv.bv_len = sizeof( buf ) - 1;
947 string_expand( &bv, &b->a_sockurl_pat, e->e_ndn, matches );
949 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 ) {
954 if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
960 if ( b->a_domain_pat.bv_len ) {
961 if ( !op->o_conn->c_peer_domain.bv_val ) {
965 LDAP_LOG( ACL, DETAIL1,
966 "acl_mask: conn %lu check a_domain_pat: %s\n",
967 op->o_connid, b->a_domain_pat.bv_val , 0 );
969 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
970 b->a_domain_pat.bv_val, 0, 0 );
972 if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
973 if ( b->a_domain_style == ACL_STYLE_REGEX) {
974 if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
975 e->e_ndn, matches ) )
980 char buf[ACL_BUF_SIZE];
982 struct berval cmp = op->o_conn->c_peer_domain;
983 struct berval pat = b->a_domain_pat;
985 if ( b->a_domain_expand ) {
988 bv.bv_len = sizeof(buf) - 1;
991 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
995 if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
996 int offset = cmp.bv_len - pat.bv_len;
1001 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1005 /* trim the domain */
1006 cmp.bv_val = &cmp.bv_val[ offset ];
1007 cmp.bv_len -= offset;
1010 if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1017 if ( b->a_peername_pat.bv_len ) {
1018 if ( !op->o_conn->c_peer_name.bv_val ) {
1022 LDAP_LOG( ACL, DETAIL1,
1023 "acl_mask: conn %lu check a_peername_path: %s\n",
1024 op->o_connid, b->a_peername_pat.bv_val , 0 );
1026 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1027 b->a_peername_pat.bv_val, 0, 0 );
1029 if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1030 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1031 if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1032 e->e_ndn, matches ) )
1038 /* try exact match */
1039 if ( b->a_peername_style == ACL_STYLE_BASE ) {
1040 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1044 } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1046 char buf[ACL_BUF_SIZE];
1048 bv.bv_len = sizeof( buf ) - 1;
1050 string_expand( &bv, &b->a_peername_pat, e->e_ndn, matches );
1052 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1056 /* extract IP and try exact match */
1057 } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1059 char buf[] = "255.255.255.255";
1062 int port_number = -1;
1064 if ( strncasecmp( op->o_conn->c_peer_name.bv_val,
1065 aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 )
1068 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1069 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1071 port = strrchr( ip.bv_val, ':' );
1075 ip.bv_len = port - ip.bv_val;
1077 port_number = strtol( port, &next, 10 );
1078 if ( next[0] != '\0' )
1082 /* the port check can be anticipated here */
1083 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1086 /* address longer than expected? */
1087 if ( ip.bv_len >= sizeof(buf) )
1090 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1091 buf[ ip.bv_len ] = '\0';
1093 addr = inet_addr( buf );
1095 /* unable to convert? */
1096 if ( addr == (unsigned long)(-1) )
1099 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1102 #ifdef LDAP_PF_LOCAL
1103 /* extract path and try exact match */
1104 } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1107 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1108 aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1111 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1112 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1114 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1117 #endif /* LDAP_PF_LOCAL */
1119 /* exact match (very unlikely...) */
1120 } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1127 if ( b->a_sockname_pat.bv_len ) {
1128 if ( !op->o_conn->c_sock_name.bv_val ) {
1132 LDAP_LOG( ACL, DETAIL1,
1133 "acl_mask: conn %lu check a_sockname_path: %s\n",
1134 op->o_connid, b->a_sockname_pat.bv_val , 0 );
1136 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1137 b->a_sockname_pat.bv_val, 0, 0 );
1139 if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1140 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1141 if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1142 e->e_ndn, matches ) )
1147 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1149 char buf[ACL_BUF_SIZE];
1151 bv.bv_len = sizeof( buf ) - 1;
1153 string_expand( &bv, &b->a_sockname_pat, e->e_ndn, matches );
1155 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1160 if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
1166 if ( b->a_dn_at != NULL ) {
1171 const char *attr = b->a_dn_at->ad_cname.bv_val;
1173 assert( attr != NULL );
1175 if ( op->o_ndn.bv_len == 0 ) {
1180 LDAP_LOG( ACL, DETAIL1,
1181 "acl_mask: conn %lu check a_dn_pat: %s\n",
1182 op->o_connid, attr , 0 );
1184 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1189 /* see if asker is listed in dnattr */
1190 for( at = attrs_find( e->e_attrs, b->a_dn_at );
1192 at = attrs_find( at->a_next, b->a_dn_at ) )
1194 if( value_find_ex( b->a_dn_at,
1195 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1196 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1198 &bv, op->o_tmpmemctx ) == 0 )
1207 /* have a dnattr match. if this is a self clause then
1208 * the target must also match the op dn.
1210 if ( b->a_dn_self ) {
1211 /* check if the target is an attribute. */
1212 if ( val == NULL ) continue;
1214 /* target is attribute, check if the attribute value
1217 rc = value_match( &match, b->a_dn_at,
1218 b->a_dn_at->ad_type->sat_equality, 0,
1220 /* on match error or no match, fail the ACL clause */
1221 if (rc != LDAP_SUCCESS || match != 0 )
1225 /* no dnattr match, check if this is a self clause */
1226 if ( ! b->a_dn_self )
1229 ACL_RECORD_VALUE_STATE;
1231 /* this is a self clause, check if the target is an
1237 /* target is attribute, check if the attribute value
1240 rc = value_match( &match, b->a_dn_at,
1241 b->a_dn_at->ad_type->sat_equality, 0,
1244 /* on match error or no match, fail the ACL clause */
1245 if (rc != LDAP_SUCCESS || match != 0 )
1250 if ( b->a_group_pat.bv_len ) {
1252 struct berval ndn = BER_BVNULL;
1255 if ( op->o_ndn.bv_len == 0 ) {
1259 /* b->a_group is an unexpanded entry name, expanded it should be an
1260 * entry with objectclass group* and we test to see if odn is one of
1261 * the values in the attribute group
1263 /* see if asker is listed in dnattr */
1264 if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1265 char buf[ACL_BUF_SIZE];
1266 bv.bv_len = sizeof(buf) - 1;
1269 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1270 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1271 /* did not expand to a valid dn */
1278 bv = b->a_group_pat;
1281 rc = backend_group( op, e, &bv, &op->o_ndn,
1282 b->a_group_oc, b->a_group_at );
1284 if ( ndn.bv_val ) free( ndn.bv_val );
1291 if ( b->a_set_pat.bv_len != 0 ) {
1293 char buf[ACL_BUF_SIZE];
1294 if( b->a_set_style == ACL_STYLE_REGEX ){
1295 bv.bv_len = sizeof(buf) - 1;
1297 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1301 if (aci_match_set( &bv, op, e, 0 ) == 0) {
1306 if ( b->a_authz.sai_ssf ) {
1308 LDAP_LOG( ACL, DETAIL1,
1309 "acl_mask: conn %lu check a_authz.sai_ssf: ACL %u > OP %u\n",
1310 op->o_connid, b->a_authz.sai_ssf, op->o_ssf );
1312 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1313 b->a_authz.sai_ssf, op->o_ssf, 0 );
1315 if ( b->a_authz.sai_ssf > op->o_ssf ) {
1320 if ( b->a_authz.sai_transport_ssf ) {
1322 LDAP_LOG( ACL, DETAIL1,
1323 "acl_mask: conn %lu check a_authz.sai_transport_ssf: "
1325 op->o_connid, b->a_authz.sai_transport_ssf,
1326 op->o_transport_ssf );
1328 Debug( LDAP_DEBUG_ACL,
1329 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1330 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1332 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
1337 if ( b->a_authz.sai_tls_ssf ) {
1339 LDAP_LOG( ACL, DETAIL1,
1340 "acl_mask: conn %lu check a_authz.sai_tls_ssf: ACL %u > "
1342 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf );
1344 Debug( LDAP_DEBUG_ACL,
1345 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1346 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1348 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
1353 if ( b->a_authz.sai_sasl_ssf ) {
1355 LDAP_LOG( ACL, DETAIL1,
1356 "acl_mask: conn %lu check a_authz.sai_sasl_ssf: "
1358 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1360 Debug( LDAP_DEBUG_ACL,
1361 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1362 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1364 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
1369 #ifdef SLAPD_ACI_ENABLED
1370 if ( b->a_aci_at != NULL ) {
1372 slap_access_t grant, deny, tgrant, tdeny;
1373 struct berval parent_ndn, old_parent_ndn;
1374 BerVarray bvals = NULL;
1377 /* this case works different from the others above.
1378 * since aci's themselves give permissions, we need
1379 * to first check b->a_access_mask, the ACL's access level.
1382 if ( e->e_nname.bv_len == 0 ) {
1383 /* no ACIs in the root DSE */
1387 /* first check if the right being requested
1388 * is allowed by the ACL clause.
1390 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1393 /* start out with nothing granted, nothing denied */
1397 /* get the aci attribute */
1398 at = attr_find( e->e_attrs, b->a_aci_at );
1400 ACL_RECORD_VALUE_STATE;
1401 /* the aci is an multi-valued attribute. The
1402 * rights are determined by OR'ing the individual
1403 * rights given by the acis.
1405 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1409 matches, &grant, &deny, &aci_bv_entry ) != 0)
1415 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1416 accessmask2str(tgrant,accessmaskbuf),
1417 accessmask2str(tdeny, accessmaskbuf1), 0);
1420 /* If the entry level aci didn't contain anything valid for the
1421 * current operation, climb up the tree and evaluate the
1422 * acis with scope set to subtree
1424 if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1425 dnParent(&(e->e_nname), &parent_ndn);
1426 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1427 old_parent_ndn = parent_ndn;
1428 Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1429 ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1433 for( i = 0; bvals[i].bv_val != NULL; i++){
1434 ACL_RECORD_VALUE_STATE;
1435 if (aci_mask(op, e, desc, val, &bvals[i], matches,
1436 &grant, &deny, &aci_bv_children) != 0) {
1439 /* evaluation stops as soon as either a "deny" or a
1440 * "grant" directive matches.
1442 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1446 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1447 accessmask2str(tgrant,accessmaskbuf),
1448 accessmask2str(tdeny, accessmaskbuf1), 0);
1453 case LDAP_NO_SUCH_ATTRIBUTE:
1454 /* just go on if the aci-Attribute is not present in
1457 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1460 case LDAP_NO_SUCH_OBJECT:
1461 /* We have reached the base object */
1462 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1472 dnParent(&old_parent_ndn, &parent_ndn);
1477 /* remove anything that the ACL clause does not allow */
1478 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1479 tdeny &= ACL_PRIV_MASK;
1481 /* see if we have anything to contribute */
1482 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
1486 /* this could be improved by changing acl_mask so that it can deal with
1487 * by clauses that return grant/deny pairs. Right now, it does either
1488 * additive or subtractive rights, but not both at the same time. So,
1489 * we need to combine the grant/deny pair into a single rights mask in
1490 * a smart way: if either grant or deny is "empty", then we use the
1491 * opposite as is, otherwise we remove any denied rights from the grant
1492 * rights mask and construct an additive mask.
1494 if (ACL_IS_INVALID(tdeny)) {
1495 modmask = tgrant | ACL_PRIV_ADDITIVE;
1497 } else if (ACL_IS_INVALID(tgrant)) {
1498 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1501 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1507 modmask = b->a_access_mask;
1511 LDAP_LOG( ACL, RESULTS,
1512 "acl_mask: [%d] applying %s (%s)\n",
1513 i, accessmask2str( modmask, accessmaskbuf),
1514 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1515 ? "break" : "stop" );
1517 Debug( LDAP_DEBUG_ACL,
1518 "<= acl_mask: [%d] applying %s (%s)\n",
1519 i, accessmask2str( modmask, accessmaskbuf ),
1520 b->a_type == ACL_CONTINUE
1522 : b->a_type == ACL_BREAK
1529 if( ACL_IS_ADDITIVE(modmask) ) {
1531 ACL_PRIV_SET( *mask, modmask );
1534 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1536 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1537 /* substract privs */
1538 ACL_PRIV_CLR( *mask, modmask );
1541 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1549 LDAP_LOG( ACL, DETAIL1,
1550 "acl_mask: conn %lu [%d] mask: %s\n",
1551 op->o_connid, i, accessmask2str( *mask, accessmaskbuf) );
1553 Debug( LDAP_DEBUG_ACL,
1554 "<= acl_mask: [%d] mask: %s\n",
1555 i, accessmask2str(*mask, accessmaskbuf), 0 );
1558 if( b->a_type == ACL_CONTINUE ) {
1561 } else if ( b->a_type == ACL_BREAK ) {
1569 /* implicit "by * none" clause */
1573 LDAP_LOG( ACL, RESULTS,
1574 "acl_mask: conn %lu no more <who> clauses, returning %d (stop)\n",
1575 op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1577 Debug( LDAP_DEBUG_ACL,
1578 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1579 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1585 * acl_check_modlist - check access control on the given entry to see if
1586 * it allows the given modifications by the user associated with op.
1587 * returns 1 if mods allowed ok
1588 * 0 mods not allowed
1595 Modifications *mlist
1599 AccessControlState state = ACL_STATE_INIT;
1602 int ret = 1; /* default is access allowed */
1610 assert( be != NULL );
1612 /* short circuit root database access */
1613 if ( be_isroot( op ) ) {
1615 LDAP_LOG( ACL, DETAIL1,
1616 "acl_check_modlist: conn %lu access granted to root user\n",
1617 op->o_connid, 0, 0 );
1619 Debug( LDAP_DEBUG_ACL,
1620 "<= acl_access_allowed: granted to database root\n",
1626 /* use backend default access if no backend acls */
1627 if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1629 LDAP_LOG( ACL, DETAIL1,
1630 "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1631 access2str( ACL_WRITE ),
1632 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied",
1635 Debug( LDAP_DEBUG_ACL,
1636 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1637 access2str( ACL_WRITE ),
1638 op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1640 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1644 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1646 * no-user-modification operational attributes are ignored
1647 * by ACL_WRITE checking as any found here are not provided
1650 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1652 LDAP_LOG( ACL, DETAIL1,
1653 "acl_check_modlist: conn %lu no-user-mod %s: modify access granted\n",
1654 op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1656 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1657 " modify access granted\n",
1658 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1663 switch ( mlist->sml_op ) {
1664 case LDAP_MOD_REPLACE:
1666 * We must check both permission to delete the whole
1667 * attribute and permission to add the specific attributes.
1668 * This prevents abuse from selfwriters.
1670 if ( ! access_allowed( op, e,
1671 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1677 if ( mlist->sml_values == NULL ) break;
1679 /* fall thru to check value to add */
1682 assert( mlist->sml_values != NULL );
1684 for ( bv = mlist->sml_nvalues
1685 ? mlist->sml_nvalues : mlist->sml_values;
1686 bv->bv_val != NULL; bv++ )
1688 if ( ! access_allowed( op, e,
1689 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1697 case LDAP_MOD_DELETE:
1698 if ( mlist->sml_values == NULL ) {
1699 if ( ! access_allowed( op, e,
1700 mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1707 for ( bv = mlist->sml_nvalues
1708 ? mlist->sml_nvalues : mlist->sml_values;
1709 bv->bv_val != NULL; bv++ )
1711 if ( ! access_allowed( op, e,
1712 mlist->sml_desc, bv, ACL_WRITE, &state ) )
1720 case SLAP_MOD_SOFTADD:
1721 /* allow adding attribute via modrdn thru */
1733 if (be_null) op->o_bd = NULL;
1739 struct berval *list,
1753 while (len >= 0 && --ix >= 0) {
1754 while (--len >= 0 && *p++ != sep) ;
1756 while (len >= 0 && *p == ' ') {
1767 while (--len >= 0 && *p != sep) {
1771 while (bv->bv_len > 0 && *--p == ' ')
1777 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1779 AciSetCookie *cp = (AciSetCookie *)cookie;
1780 BerVarray bvals = NULL;
1783 /* this routine needs to return the bervals instead of
1784 * plain strings, since syntax is not known. It should
1785 * also return the syntax or some "comparison cookie".
1788 if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1790 AttributeDescription *desc = NULL;
1791 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1792 backend_attribute(cp->op,
1793 cp->e, &ndn, desc, &bvals);
1795 slap_sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1802 struct berval *subj,
1808 struct berval set = BER_BVNULL;
1810 AciSetCookie cookie;
1813 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1815 struct berval subjdn, ndn = BER_BVNULL;
1816 struct berval setat;
1819 AttributeDescription *desc = NULL;
1821 /* format of string is "entry/setAttrName" */
1822 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1826 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1827 setat.bv_val = SLAPD_ACI_SET_ATTR;
1828 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1831 if ( setat.bv_val != NULL ) {
1833 * NOTE: dnNormalize honors the ber_len field
1834 * as the length of the dn to be normalized
1836 if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1837 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1839 backend_attribute(op, e,
1840 &ndn, desc, &bvals);
1841 if ( bvals != NULL ) {
1842 if ( bvals[0].bv_val != NULL ) {
1845 bvals[0].bv_val = NULL;
1846 for (i=1;bvals[i].bv_val;i++);
1847 bvals[0].bv_val = bvals[i-1].bv_val;
1848 bvals[i-1].bv_val = NULL;
1850 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1858 if (set.bv_val != NULL) {
1861 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1862 &op->o_ndn, &e->e_nname, NULL) > 0);
1863 slap_sl_free(set.bv_val, op->o_tmpmemctx);
1868 #ifdef SLAPD_ACI_ENABLED
1870 aci_list_map_rights(
1871 struct berval *list )
1878 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1881 switch (*bv.bv_val) {
1883 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1886 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1887 * the right 's' to mean "set", but in the examples states
1888 * that the right 's' means "search". The latter definition
1891 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1894 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1897 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1900 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1901 * define any equivalent to the AUTH right, so I've just used
1904 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1916 struct berval *list,
1917 const struct berval *attr,
1918 struct berval *val )
1920 struct berval bv, left, right;
1923 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1924 if (aci_get_part(&bv, 0, '=', &left) < 0
1925 || aci_get_part(&bv, 1, '=', &right) < 0)
1927 if (ber_bvstrcasecmp(attr, &bv) == 0)
1929 } else if (val == NULL) {
1930 if (ber_bvstrcasecmp(attr, &left) == 0)
1933 if (ber_bvstrcasecmp(attr, &left) == 0) {
1934 /* this is experimental code that implements a
1935 * simple (prefix) match of the attribute value.
1936 * the ACI draft does not provide for aci's that
1937 * apply to specific values, but it would be
1938 * nice to have. If the <attr> part of an aci's
1939 * rights list is of the form <attr>=<value>,
1940 * that means the aci applies only to attrs with
1941 * the given value. Furthermore, if the attr is
1942 * of the form <attr>=<value>*, then <value> is
1943 * treated as a prefix, and the aci applies to
1944 * any value with that prefix.
1946 * Ideally, this would allow r.e. matches.
1948 if (aci_get_part(&right, 0, '*', &left) < 0
1949 || right.bv_len <= left.bv_len)
1951 if (ber_bvstrcasecmp(val, &right) == 0)
1953 } else if (val->bv_len >= left.bv_len) {
1954 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1963 static slap_access_t
1964 aci_list_get_attr_rights(
1965 struct berval *list,
1966 const struct berval *attr,
1967 struct berval *val )
1973 /* loop through each rights/attr pair, skip first part (action) */
1975 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1976 if (aci_list_has_attr(&bv, attr, val) == 0)
1978 if (aci_get_part(list, i, ';', &bv) < 0)
1980 mask |= aci_list_map_rights(&bv);
1986 aci_list_get_rights(
1987 struct berval *list,
1988 const struct berval *attr,
1990 slap_access_t *grant,
1991 slap_access_t *deny )
1993 struct berval perm, actn;
1994 slap_access_t *mask;
1997 if (attr == NULL || attr->bv_len == 0
1998 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1999 attr = &aci_bv_br_entry;
2005 /* loop through each permissions clause */
2006 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2007 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2009 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2011 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2018 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2019 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2026 struct berval *subj,
2027 struct berval *defgrpoc,
2028 struct berval *defgrpat,
2034 struct berval subjdn;
2035 struct berval grpoc;
2036 struct berval grpat;
2037 ObjectClass *grp_oc = NULL;
2038 AttributeDescription *grp_ad = NULL;
2042 /* format of string is "group/objectClassValue/groupAttrName" */
2043 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2047 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2051 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2055 rc = slap_bv2ad( &grpat, &grp_ad, &text );
2056 if( rc != LDAP_SUCCESS ) {
2062 grp_oc = oc_bvfind( &grpoc );
2064 if (grp_oc != NULL && grp_ad != NULL ) {
2065 char buf[ACL_BUF_SIZE];
2066 struct berval bv, ndn;
2067 bv.bv_len = sizeof( buf ) - 1;
2068 bv.bv_val = (char *)&buf;
2069 string_expand(&bv, &subjdn, e->e_ndn, matches);
2070 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2071 rc = (backend_group(op, e, &ndn, &op->o_ndn,
2072 grp_oc, grp_ad) == 0);
2085 AttributeDescription *desc,
2088 regmatch_t *matches,
2089 slap_access_t *grant,
2090 slap_access_t *deny,
2091 struct berval *scope
2094 struct berval bv, perms, sdn;
2098 assert( desc->ad_cname.bv_val != NULL );
2100 /* parse an aci of the form:
2101 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2103 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2104 a full description of the format for this attribute.
2105 Differences: "this" in the draft is "self" here, and
2106 "self" and "public" is in the position of dnType.
2108 For now, this routine only supports scope=entry.
2110 /* check that the aci has all 5 components */
2111 if (aci_get_part(aci, 4, '#', NULL) < 0)
2114 /* check that the aci family is supported */
2115 if (aci_get_part(aci, 0, '#', &bv) < 0)
2118 /* check that the scope matches */
2119 if (aci_get_part(aci, 1, '#', &bv) < 0
2120 || ber_bvstrcasecmp( scope, &bv ) != 0)
2125 /* get the list of permissions clauses, bail if empty */
2126 if (aci_get_part(aci, 2, '#', &perms) <= 0)
2129 /* check if any permissions allow desired access */
2130 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2133 /* see if we have a DN match */
2134 if (aci_get_part(aci, 3, '#', &bv) < 0)
2137 if (aci_get_part(aci, 4, '#', &sdn) < 0)
2140 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2143 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2144 if (dn_match( &op->o_ndn, &ndn))
2150 } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2153 } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2154 if (dn_match(&op->o_ndn, &e->e_nname))
2157 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2159 AttributeDescription *ad = NULL;
2162 rc = slap_bv2ad( &sdn, &ad, &text );
2164 if( rc != LDAP_SUCCESS ) {
2172 for(at = attrs_find( e->e_attrs, ad );
2174 at = attrs_find( at->a_next, ad ) )
2176 if (value_find_ex( ad,
2177 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2178 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2180 &bv, op->o_tmpmemctx) == 0 )
2190 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2191 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2194 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2195 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2198 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2199 if (aci_match_set(&sdn, op, e, 0))
2202 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2203 if (aci_match_set(&sdn, op, e, 1))
2211 #endif /* SLAPD_ACI_ENABLED */
2218 regmatch_t *matches)
2226 bv->bv_val[0] = '\0';
2227 bv->bv_len--; /* leave space for lone $ */
2230 for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2231 sp < pat->bv_val + pat->bv_len ; sp++ )
2233 /* did we previously see a $ */
2235 if ( flag == 1 && *sp == '$' ) {
2240 } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2243 } else if ( *sp >= '0' && *sp <= '9' ) {
2251 for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2252 if ( *sp >= '0' && *sp <= '9' ) {
2253 n = 10*n + ( *sp - '0' );
2257 if ( *sp != /*'{'*/ '}' ) {
2262 if ( n >= MAXREMATCHES ) {
2267 i = matches[n].rm_so;
2268 l = matches[n].rm_eo;
2269 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2287 /* must have ended with a single $ */
2296 LDAP_LOG( ACL, DETAIL1,
2297 "string_expand: pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2298 LDAP_LOG( ACL, DETAIL1, "string_expand: expanded = %s\n", bv->bv_val, 0, 0 );
2300 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2301 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2307 struct berval *pat, /* pattern to expand and match against */
2308 char *str, /* string to match against pattern */
2309 char *buf, /* buffer with $N expansion variables */
2310 regmatch_t *matches /* offsets in buffer for $N expansion variables */
2314 char newbuf[ACL_BUF_SIZE];
2318 bv.bv_len = sizeof(newbuf) - 1;
2321 if(str == NULL) str = "";
2323 string_expand(&bv, pat, buf, matches);
2324 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2325 char error[ACL_BUF_SIZE];
2326 regerror(rc, &re, error, sizeof(error));
2330 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2331 pat->bv_val, str, error );
2333 Debug( LDAP_DEBUG_TRACE,
2334 "compile( \"%s\", \"%s\") failed %s\n",
2335 pat->bv_val, str, error );
2340 rc = regexec(&re, str, 0, NULL, 0);
2344 LDAP_LOG( ACL, DETAIL2, "regex_matches: string: %s\n", str, 0, 0 );
2345 LDAP_LOG( ACL, DETAIL2, "regex_matches: rc: %d %s\n",
2346 rc, rc ? "matches" : "no matches", 0 );
2348 Debug( LDAP_DEBUG_TRACE,
2349 "=> regex_matches: string: %s\n", str, 0, 0 );
2350 Debug( LDAP_DEBUG_TRACE,
2351 "=> regex_matches: rc: %d %s\n",
2352 rc, !rc ? "matches" : "no matches", 0 );