1 /* acl.c - routines to parse and check acl's */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
19 static AccessControl * acl_get(
20 AccessControl *ac, int *count,
21 Backend *be, Operation *op,
23 AttributeDescription *desc,
24 int nmatches, regmatch_t *matches );
26 static slap_control_t acl_mask(
27 AccessControl *ac, slap_mask_t *mask,
28 Backend *be, Connection *conn, Operation *op,
30 AttributeDescription *desc,
32 regmatch_t *matches );
34 #ifdef SLAPD_ACI_ENABLED
40 AttributeDescription *desc,
45 slap_access_t *deny );
48 static int regex_matches(
49 char *pat, char *str, char *buf, regmatch_t *matches);
50 static void string_expand(
51 char *newbuf, int bufsiz, char *pattern,
52 char *match, regmatch_t *matches);
54 char **aci_set_gather (void *cookie, char *name, char *attr);
55 static int aci_match_set ( struct berval *subj, Backend *be,
56 Entry *e, Connection *conn, Operation *op, int setref );
59 * access_allowed - check whether op->o_ndn is allowed the requested access
60 * to entry e, attribute attr, value val. if val is null, access to
61 * the whole attribute is assumed (all values).
63 * This routine loops through all access controls and calls
64 * acl_mask() on each applicable access control.
65 * The loop exits when a definitive answer is reached or
66 * or no more controls remain.
79 AttributeDescription *desc,
81 slap_access_t access )
86 char accessmaskbuf[ACCESSMASK_MAXLEN];
89 slap_control_t control;
91 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
93 regmatch_t matches[MAXREMATCHES];
95 Debug( LDAP_DEBUG_ACL,
96 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
100 assert( be != NULL );
102 assert( attr != NULL );
103 assert( access > ACL_NONE );
105 /* grant database root access */
106 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
107 Debug( LDAP_DEBUG_ACL,
108 "<= root access granted\n",
114 * no-user-modification operational attributes are ignored
115 * by ACL_WRITE checking as any found here are not provided
118 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
119 && desc != slap_schema.si_ad_entry
120 && desc != slap_schema.si_ad_children )
122 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
123 " %s access granted\n",
128 /* use backend default access if no backend acls */
129 if( be != NULL && be->be_acl == NULL ) {
130 Debug( LDAP_DEBUG_ACL,
131 "=> access_allowed: backend default %s access %s to \"%s\"\n",
132 access2str( access ),
133 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
135 return be->be_dfltaccess >= access;
138 /* be is always non-NULL */
139 /* use global default access if no global acls */
140 } else if ( be == NULL && global_acl == NULL ) {
141 Debug( LDAP_DEBUG_ACL,
142 "=> access_allowed: global default %s access %s to \"%s\"\n",
143 access2str( access ),
144 global_default_access >= access ? "granted" : "denied", op->o_dn );
146 return global_default_access >= access;
151 memset(matches, '\0', sizeof(matches));
157 while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
161 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
162 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
163 (int)matches[i].rm_so, (int)matches[i].rm_eo );
165 if( matches[i].rm_so <= matches[0].rm_eo ) {
167 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
168 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
171 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
174 control = acl_mask( a, &mask, be, conn, op,
175 e, desc, val, matches );
177 if ( control != ACL_BREAK ) {
181 memset(matches, '\0', sizeof(matches));
184 if ( ACL_IS_INVALID( mask ) ) {
185 Debug( LDAP_DEBUG_ACL,
186 "=> access_allowed: \"%s\" (%s) invalid!\n",
190 } else if ( control == ACL_BREAK ) {
191 Debug( LDAP_DEBUG_ACL,
192 "=> access_allowed: no more rules\n", 0, 0, 0);
196 Debug( LDAP_DEBUG_ACL,
197 "=> access_allowed: %s access %s by %s\n",
198 access2str( access ),
199 ACL_GRANT(mask, access) ? "granted" : "denied",
200 accessmask2str( mask, accessmaskbuf ) );
202 return ACL_GRANT(mask, access);
206 * acl_get - return the acl applicable to entry e, attribute
207 * attr. the acl returned is suitable for use in subsequent calls to
208 * acl_access_allowed().
211 static AccessControl *
218 AttributeDescription *desc,
220 regmatch_t *matches )
226 assert( count != NULL );
228 attr = desc ? desc->ad_cname->bv_val : NULL;
243 dnlen = strlen(e->e_ndn);
245 for ( ; a != NULL; a = a->acl_next ) {
248 if (a->acl_dn_pat != NULL) {
249 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
250 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
251 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
253 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
257 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
258 *count, a->acl_dn_pat, 0 );
260 patlen = strlen( a->acl_dn_pat );
261 if ( dnlen < patlen )
264 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
265 /* base dn -- entire object DN must match */
266 if ( dnlen != patlen )
269 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
273 if ( dnlen <= patlen )
276 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
279 rdn = dn_rdn( NULL, e->e_ndn );
281 rdnlen = strlen( rdn );
284 if ( rdnlen != dnlen - patlen - 1 )
287 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
288 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
291 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
292 if ( dnlen <= patlen )
294 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
298 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
302 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
306 if ( a->acl_filter != NULL ) {
307 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
308 if ( rc != LDAP_COMPARE_TRUE ) {
313 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
316 if ( attr == NULL || a->acl_attrs == NULL ||
317 ad_inlist( desc, a->acl_attrs ) )
319 Debug( LDAP_DEBUG_ACL,
320 "<= acl_get: [%d] acl %s attr: %s\n",
321 *count, e->e_dn, attr );
324 matches[0].rm_so = matches[0].rm_eo = -1;
327 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
333 * acl_mask - modifies mask based upon the given acl and the
334 * requested access to entry e, attribute attr, value val. if val
335 * is null, access to the whole attribute is assumed (all values).
337 * returns 0 access NOT allowed
341 static slap_control_t
349 AttributeDescription *desc,
354 int i, odnlen, patlen;
357 char accessmaskbuf[ACCESSMASK_MAXLEN];
359 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
362 assert( mask != NULL );
364 Debug( LDAP_DEBUG_ACL,
365 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
368 Debug( LDAP_DEBUG_ACL,
369 "=> acl_mask: to %s by \"%s\", (%s) \n",
370 val ? "value" : "all values",
371 op->o_ndn ? op->o_ndn : "",
372 accessmask2str( *mask, accessmaskbuf ) );
374 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
375 slap_mask_t oldmask, modmask;
377 ACL_INVALIDATE( modmask );
379 /* AND <who> clauses */
380 if ( b->a_dn_pat != NULL ) {
381 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
384 * if access applies to the entry itself, and the
385 * user is bound as somebody in the same namespace as
386 * the entry, OR the given dn matches the dn pattern
388 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
389 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
393 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
394 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
398 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
399 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
403 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
407 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
408 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
409 int ret = regex_matches( b->a_dn_pat,
410 op->o_ndn, e->e_ndn, matches );
418 if ( e->e_dn == NULL )
421 patlen = strlen( b->a_dn_pat );
422 odnlen = strlen( op->o_ndn );
423 if ( odnlen < patlen )
426 if ( b->a_dn_style == ACL_STYLE_BASE ) {
427 /* base dn -- entire object DN must match */
428 if ( odnlen != patlen )
431 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
435 if ( odnlen <= patlen )
438 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
441 rdn = dn_rdn( NULL, op->o_ndn );
443 rdnlen = strlen( rdn );
446 if ( rdnlen != odnlen - patlen - 1 )
449 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
450 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
453 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
454 if ( odnlen <= patlen )
456 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
460 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
466 if ( b->a_sockurl_pat != NULL ) {
467 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
468 b->a_sockurl_pat, 0, 0 );
470 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
471 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
472 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
473 e->e_ndn, matches ) )
478 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
484 if ( b->a_domain_pat != NULL ) {
485 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
486 b->a_domain_pat, 0, 0 );
488 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
489 if ( b->a_domain_style == ACL_STYLE_REGEX) {
490 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
491 e->e_ndn, matches ) )
496 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
502 if ( b->a_peername_pat != NULL ) {
503 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
504 b->a_peername_pat, 0, 0 );
506 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
507 if ( b->a_peername_style == ACL_STYLE_REGEX) {
508 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
509 e->e_ndn, matches ) )
514 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
520 if ( b->a_sockname_pat != NULL ) {
521 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
522 b->a_sockname_pat, 0, 0 );
524 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
525 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
526 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
527 e->e_ndn, matches ) )
532 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
538 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
543 const char *desc = b->a_dn_at->ad_cname->bv_val;
545 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
548 bv.bv_val = op->o_ndn;
549 bv.bv_len = strlen( bv.bv_val );
551 /* see if asker is listed in dnattr */
552 for( at = attrs_find( e->e_attrs, b->a_dn_at );
554 at = attrs_find( at->a_next, b->a_dn_at ) )
556 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
564 /* have a dnattr match. if this is a self clause then
565 * the target must also match the op dn.
567 if ( b->a_dn_self ) {
568 /* check if the target is an attribute. */
571 /* target is attribute, check if the attribute value
574 rc = value_match( &match, b->a_dn_at,
575 b->a_dn_at->ad_type->sat_equality, 0,
577 /* on match error or no match, fail the ACL clause */
578 if (rc != LDAP_SUCCESS || match != 0 )
582 /* no dnattr match, check if this is a self clause */
583 if ( ! b->a_dn_self )
585 /* this is a self clause, check if the target is an
590 /* target is attribute, check if the attribute value
593 rc = value_match( &match, b->a_dn_at,
594 b->a_dn_at->ad_type->sat_equality, 0,
597 /* on match error or no match, fail the ACL clause */
598 if (rc != LDAP_SUCCESS || match != 0 )
603 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
606 /* b->a_group is an unexpanded entry name, expanded it should be an
607 * entry with objectclass group* and we test to see if odn is one of
608 * the values in the attribute group
610 /* see if asker is listed in dnattr */
611 if ( b->a_group_style == ACL_STYLE_REGEX ) {
612 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
613 if ( dn_normalize(buf) == NULL ) {
614 /* did not expand to a valid dn */
618 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
619 buf[sizeof(buf) - 1] = 0;
622 if (backend_group(be, e, buf, op->o_ndn,
623 b->a_group_oc, b->a_group_at) != 0)
629 if ( b->a_set_pat != NULL ) {
632 bv.bv_val = b->a_set_pat;
633 bv.bv_len = strlen(b->a_set_pat);
634 if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
639 if ( b->a_authz.sai_ssf ) {
640 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
641 b->a_authz.sai_ssf, op->o_ssf, 0 );
643 if ( b->a_authz.sai_ssf > op->o_ssf ) {
648 if ( b->a_authz.sai_transport_ssf ) {
649 Debug( LDAP_DEBUG_ACL,
650 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
651 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
653 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
658 if ( b->a_authz.sai_tls_ssf ) {
659 Debug( LDAP_DEBUG_ACL,
660 "<= check a_authz.sai_tls_ssf: ACL %u > OP %s\n",
661 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
663 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
668 if ( b->a_authz.sai_sasl_ssf ) {
669 Debug( LDAP_DEBUG_ACL,
670 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %s\n",
671 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
673 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
678 #ifdef SLAPD_ACI_ENABLED
679 if ( b->a_aci_at != NULL ) {
681 slap_access_t grant, deny, tgrant, tdeny;
683 /* this case works different from the others above.
684 * since aci's themselves give permissions, we need
685 * to first check b->a_access_mask, the ACL's access level.
688 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
692 if ( e->e_dn == NULL ) {
696 /* first check if the right being requested
697 * is allowed by the ACL clause.
699 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
703 /* get the aci attribute */
704 at = attr_find( e->e_attrs, b->a_aci_at );
709 /* start out with nothing granted, nothing denied */
713 /* the aci is an multi-valued attribute. The
714 * rights are determined by OR'ing the individual
715 * rights given by the acis.
717 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
718 if (aci_mask( be, conn, op,
719 e, desc, val, at->a_vals[i],
720 matches, &grant, &deny ) != 0)
727 /* remove anything that the ACL clause does not allow */
728 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
729 tdeny &= ACL_PRIV_MASK;
731 /* see if we have anything to contribute */
732 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
736 /* this could be improved by changing acl_mask so that it can deal with
737 * by clauses that return grant/deny pairs. Right now, it does either
738 * additive or subtractive rights, but not both at the same time. So,
739 * we need to combine the grant/deny pair into a single rights mask in
740 * a smart way: if either grant or deny is "empty", then we use the
741 * opposite as is, otherwise we remove any denied rights from the grant
742 * rights mask and construct an additive mask.
744 if (ACL_IS_INVALID(tdeny)) {
745 modmask = tgrant | ACL_PRIV_ADDITIVE;
747 } else if (ACL_IS_INVALID(tgrant)) {
748 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
751 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
757 modmask = b->a_access_mask;
761 Debug( LDAP_DEBUG_ACL,
762 "<= acl_mask: [%d] applying %s (%s)\n",
763 i, accessmask2str( modmask, accessmaskbuf ),
764 b->a_type == ACL_CONTINUE
766 : b->a_type == ACL_BREAK
773 if( ACL_IS_ADDITIVE(modmask) ) {
775 ACL_PRIV_SET( *mask, modmask );
778 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
780 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
781 /* substract privs */
782 ACL_PRIV_CLR( *mask, modmask );
785 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
792 Debug( LDAP_DEBUG_ACL,
793 "<= acl_mask: [%d] mask: %s\n",
794 i, accessmask2str(*mask, accessmaskbuf), 0 );
796 if( b->a_type == ACL_CONTINUE ) {
799 } else if ( b->a_type == ACL_BREAK ) {
807 Debug( LDAP_DEBUG_ACL,
808 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
809 accessmask2str(*mask, accessmaskbuf), 0, 0 );
814 * acl_check_modlist - check access control on the given entry to see if
815 * it allows the given modifications by the user associated with op.
816 * returns 1 if mods allowed ok
831 assert( be != NULL );
833 /* short circuit root database access */
834 if ( be_isroot( be, op->o_ndn ) ) {
835 Debug( LDAP_DEBUG_ACL,
836 "<= acl_access_allowed: granted to database root\n",
841 /* use backend default access if no backend acls */
842 if( be != NULL && be->be_acl == NULL ) {
843 Debug( LDAP_DEBUG_ACL,
844 "=> access_allowed: backend default %s access %s to \"%s\"\n",
845 access2str( ACL_WRITE ),
846 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
848 return be->be_dfltaccess >= ACL_WRITE;
851 /* be is always non-NULL */
852 /* use global default access if no global acls */
853 } else if ( be == NULL && global_acl == NULL ) {
854 Debug( LDAP_DEBUG_ACL,
855 "=> access_allowed: global default %s access %s to \"%s\"\n",
856 access2str( ACL_WRITE ),
857 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
859 return global_default_access >= ACL_WRITE;
863 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
865 * no-user-modification operational attributes are ignored
866 * by ACL_WRITE checking as any found here are not provided
869 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
870 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
871 " modify access granted\n",
872 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
876 switch ( mlist->sml_op ) {
877 case LDAP_MOD_REPLACE:
879 if ( mlist->sml_bvalues == NULL ) {
882 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
883 if ( ! access_allowed( be, conn, op, e,
884 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
891 case LDAP_MOD_DELETE:
892 if ( mlist->sml_bvalues == NULL ) {
893 if ( ! access_allowed( be, conn, op, e,
894 mlist->sml_desc, NULL, ACL_WRITE ) )
900 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
901 if ( ! access_allowed( be, conn, op, e,
902 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
915 aci_bvstrdup( struct berval *bv )
919 s = (char *)ch_malloc(bv->bv_len + 1);
921 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
934 res = strncasecmp( s, bv->bv_val, bv->bv_len );
938 if (len > (int)bv->bv_len)
940 if (len < (int)bv->bv_len)
961 while (len >= 0 && --ix >= 0) {
962 while (--len >= 0 && *p++ != sep) ;
964 while (len >= 0 && *p == ' ') {
975 while (--len >= 0 && *p != sep) {
979 while (bv->bv_len > 0 && *--p == ' ')
985 aci_set_gather (void *cookie, char *name, char *attr)
992 } *cp = (void *)cookie;
993 struct berval **bvals = NULL;
998 /* this routine needs to return the bervals instead of
999 * plain strings, since syntax is not known. It should
1000 * also return the syntax or some "comparison cookie".
1003 if ((ndn = ch_strdup(name)) != NULL) {
1004 if (dn_normalize(ndn) != NULL) {
1006 AttributeDescription *desc = NULL;
1007 if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1008 backend_attribute(cp->be, NULL /*cp->conn*/,
1009 NULL /*cp->op*/, cp->e,
1011 if (bvals != NULL) {
1012 for (i = 0; bvals[i] != NULL; i++) { }
1013 vals = ch_calloc(i + 1, sizeof(char *));
1016 vals[i] = bvals[i]->bv_val;
1017 bvals[i]->bv_val = NULL;
1020 ber_bvecfree(bvals);
1032 struct berval *subj,
1050 set = aci_bvstrdup(subj);
1055 struct berval **bvals;
1057 AttributeDescription *desc = NULL;
1059 /* format of string is "entry/setAttrName" */
1060 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1064 subjdn = aci_bvstrdup(&bv);
1065 if ( subjdn == NULL ) {
1069 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1070 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1072 setat = aci_bvstrdup(&bv);
1074 if ( setat != NULL ) {
1075 if ( dn_normalize(subjdn) != NULL
1076 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1078 backend_attribute(be, NULL, NULL, e,
1079 subjdn, desc, &bvals);
1081 if ( bvals != NULL ) {
1082 if ( bvals[0] != NULL )
1083 set = ch_strdup(bvals[0]->bv_val);
1084 ber_bvecfree(bvals);
1097 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1103 #ifdef SLAPD_ACI_ENABLED
1105 aci_list_map_rights(
1106 struct berval *list )
1113 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1116 switch (*bv.bv_val) {
1118 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1121 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1122 * the right 's' to mean "set", but in the examples states
1123 * that the right 's' means "search". The latter definition
1126 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1129 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1132 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1135 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1136 * define any equivalent to the AUTH right, so I've just used
1139 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1151 struct berval *list,
1153 struct berval *val )
1155 struct berval bv, left, right;
1158 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1159 if (aci_get_part(&bv, 0, '=', &left) < 0
1160 || aci_get_part(&bv, 1, '=', &right) < 0)
1162 if (aci_strbvcmp(attr, &bv) == 0)
1164 } else if (val == NULL) {
1165 if (aci_strbvcmp(attr, &left) == 0)
1168 if (aci_strbvcmp(attr, &left) == 0) {
1169 /* this is experimental code that implements a
1170 * simple (prefix) match of the attribute value.
1171 * the ACI draft does not provide for aci's that
1172 * apply to specific values, but it would be
1173 * nice to have. If the <attr> part of an aci's
1174 * rights list is of the form <attr>=<value>,
1175 * that means the aci applies only to attrs with
1176 * the given value. Furthermore, if the attr is
1177 * of the form <attr>=<value>*, then <value> is
1178 * treated as a prefix, and the aci applies to
1179 * any value with that prefix.
1181 * Ideally, this would allow r.e. matches.
1183 if (aci_get_part(&right, 0, '*', &left) < 0
1184 || right.bv_len <= left.bv_len)
1186 if (aci_strbvcmp(val->bv_val, &right) == 0)
1188 } else if (val->bv_len >= left.bv_len) {
1189 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1198 static slap_access_t
1199 aci_list_get_attr_rights(
1200 struct berval *list,
1202 struct berval *val )
1208 /* loop through each rights/attr pair, skip first part (action) */
1210 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1211 if (aci_list_has_attr(&bv, attr, val) == 0)
1213 if (aci_get_part(list, i, ';', &bv) < 0)
1215 mask |= aci_list_map_rights(&bv);
1221 aci_list_get_rights(
1222 struct berval *list,
1225 slap_access_t *grant,
1226 slap_access_t *deny )
1228 struct berval perm, actn;
1229 slap_access_t *mask;
1232 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1239 /* loop through each permissions clause */
1240 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1241 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1243 if (aci_strbvcmp( "grant", &actn ) == 0) {
1245 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1252 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1253 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1260 struct berval *subj,
1261 const char *defgrpoc,
1262 const char *defgrpat,
1270 char *subjdn, *grpdn = NULL;
1273 ObjectClass *grp_oc = NULL;
1274 AttributeDescription *grp_ad = NULL;
1278 /* format of string is "group/objectClassValue/groupAttrName" */
1279 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1283 subjdn = aci_bvstrdup(&bv);
1284 if (subjdn == NULL) {
1288 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1289 grpoc = ch_strdup( defgrpoc );
1291 grpoc = aci_bvstrdup(&bv);
1294 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1295 grpat = ch_strdup( defgrpat );
1297 grpat = aci_bvstrdup(&bv);
1300 rc = slap_str2ad( grpat, &grp_ad, &text );
1301 if( rc != LDAP_SUCCESS ) {
1307 grp_oc = oc_find( grpoc );
1308 grpdn = (char *)ch_malloc(1024);
1310 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1311 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1312 if ( dn_normalize(grpdn) != NULL ) {
1313 rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1318 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1332 AttributeDescription *desc,
1335 regmatch_t *matches,
1336 slap_access_t *grant,
1340 struct berval bv, perms, sdn;
1343 char *attr = desc->ad_cname->bv_val;
1345 /* parse an aci of the form:
1346 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1348 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1349 a full description of the format for this attribute.
1351 For now, this routine only supports scope=entry.
1354 /* check that the aci has all 5 components */
1355 if (aci_get_part(aci, 4, '#', NULL) < 0)
1358 /* check that the aci family is supported */
1359 if (aci_get_part(aci, 0, '#', &bv) < 0)
1362 /* check that the scope is "entry" */
1363 if (aci_get_part(aci, 1, '#', &bv) < 0
1364 || aci_strbvcmp( "entry", &bv ) != 0)
1369 /* get the list of permissions clauses, bail if empty */
1370 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1373 /* check if any permissions allow desired access */
1374 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1377 /* see if we have a DN match */
1378 if (aci_get_part(aci, 3, '#', &bv) < 0)
1381 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1384 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1385 subjdn = aci_bvstrdup(&sdn);
1389 if ( dn_normalize(subjdn) != NULL )
1390 if (strcasecmp(op->o_ndn, subjdn) != 0)
1396 if (aci_strbvcmp( "self", &bv ) == 0) {
1397 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1400 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1401 char *dnattr = aci_bvstrdup(&sdn);
1403 AttributeDescription *ad = NULL;
1406 rc = slap_str2ad( dnattr, &ad, &text );
1409 if( rc != LDAP_SUCCESS ) {
1415 bv.bv_val = op->o_ndn;
1416 bv.bv_len = strlen( bv.bv_val );
1418 for(at = attrs_find( e->e_attrs, ad );
1420 at = attrs_find( at->a_next, ad ) )
1422 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1432 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1433 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1436 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1437 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1440 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1441 if (aci_match_set(&sdn, be, e, conn, op, 0))
1444 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1445 if (aci_match_set(&sdn, be, e, conn, op, 1))
1453 #endif /* SLAPD_ACI_ENABLED */
1461 regmatch_t *matches)
1470 bufsiz--; /* leave space for lone $ */
1473 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1474 /* did we previously see a $ */
1479 } else if (*sp >= '0' && *sp <= '9' ) {
1486 i = matches[n].rm_so;
1487 l = matches[n].rm_eo;
1488 for ( ; size < 512 && i < l; size++, i++ ) {
1506 /* must have ended with a single $ */
1513 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1514 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1519 char *pat, /* pattern to expand and match against */
1520 char *str, /* string to match against pattern */
1521 char *buf, /* buffer with $N expansion variables */
1522 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1529 if(str == NULL) str = "";
1531 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1532 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1534 regerror(rc, &re, error, sizeof(error));
1536 Debug( LDAP_DEBUG_TRACE,
1537 "compile( \"%s\", \"%s\") failed %s\n",
1542 rc = regexec(&re, str, 0, NULL, 0);
1545 Debug( LDAP_DEBUG_TRACE,
1546 "=> regex_matches: string: %s\n", str, 0, 0 );
1547 Debug( LDAP_DEBUG_TRACE,
1548 "=> regex_matches: rc: %d %s\n",
1549 rc, !rc ? "matches" : "no matches", 0 );