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>
18 static AccessControl * acl_get(
19 AccessControl *ac, int *count,
20 Backend *be, Operation *op,
22 AttributeDescription *desc,
23 int nmatches, regmatch_t *matches );
25 static slap_control_t acl_mask(
26 AccessControl *ac, slap_access_mask_t *mask,
27 Backend *be, Connection *conn, Operation *op,
29 AttributeDescription *desc,
31 regmatch_t *matches );
33 #ifdef SLAPD_ACI_ENABLED
39 AttributeDescription *desc,
44 slap_access_t *deny );
47 static int regex_matches(
48 char *pat, char *str, char *buf, regmatch_t *matches);
49 static void string_expand(
50 char *newbuf, int bufsiz, char *pattern,
51 char *match, regmatch_t *matches);
55 * access_allowed - check whether op->o_ndn is allowed the requested access
56 * to entry e, attribute attr, value val. if val is null, access to
57 * the whole attribute is assumed (all values).
59 * This routine loops through all access controls and calls
60 * acl_mask() on each applicable access control.
61 * The loop exits when a definitive answer is reached or
62 * or no more controls remain.
75 AttributeDescription *desc,
77 slap_access_t access )
82 char accessmaskbuf[ACCESSMASK_MAXLEN];
84 slap_access_mask_t mask;
85 slap_control_t control;
87 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
89 regmatch_t matches[MAXREMATCHES];
91 Debug( LDAP_DEBUG_ACL,
92 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
98 assert( attr != NULL );
99 assert( access > ACL_NONE );
101 /* grant database root access */
102 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
103 Debug( LDAP_DEBUG_ACL,
104 "<= root access granted\n",
110 * no-user-modification operational attributes are ignored
111 * by ACL_WRITE checking as any found here are not provided
114 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
116 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
117 " %s access granted\n",
122 /* use backend default access if no backend acls */
123 if( be != NULL && be->be_acl == NULL ) {
124 Debug( LDAP_DEBUG_ACL,
125 "=> access_allowed: backend default %s access %s to \"%s\"\n",
126 access2str( access ),
127 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
129 return be->be_dfltaccess >= access;
132 /* be is always non-NULL */
133 /* use global default access if no global acls */
134 } else if ( be == NULL && global_acl == NULL ) {
135 Debug( LDAP_DEBUG_ACL,
136 "=> access_allowed: global default %s access %s to \"%s\"\n",
137 access2str( access ),
138 global_default_access >= access ? "granted" : "denied", op->o_dn );
140 return global_default_access >= access;
145 memset(matches, '\0', sizeof(matches));
151 while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
155 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
156 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
157 (int)matches[i].rm_so, (int)matches[i].rm_eo );
159 if( matches[i].rm_so <= matches[0].rm_eo ) {
161 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
162 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
165 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
168 control = acl_mask( a, &mask, be, conn, op,
169 e, desc, val, matches );
171 if ( control != ACL_BREAK ) {
175 memset(matches, '\0', sizeof(matches));
178 if ( ACL_IS_INVALID( mask ) ) {
179 Debug( LDAP_DEBUG_ACL,
180 "=> access_allowed: \"%s\" (%s) invalid!\n",
184 } else if ( control == ACL_BREAK ) {
185 Debug( LDAP_DEBUG_ACL,
186 "=> access_allowed: no more rules\n", 0, 0, 0);
190 Debug( LDAP_DEBUG_ACL,
191 "=> access_allowed: %s access %s by %s\n",
192 access2str( access ),
193 ACL_GRANT(mask, access) ? "granted" : "denied",
194 accessmask2str( mask, accessmaskbuf ) );
196 return ACL_GRANT(mask, access);
200 * acl_get - return the acl applicable to entry e, attribute
201 * attr. the acl returned is suitable for use in subsequent calls to
202 * acl_access_allowed().
205 static AccessControl *
212 AttributeDescription *desc,
214 regmatch_t *matches )
220 assert( count != NULL );
222 attr = desc ? desc->ad_cname->bv_val : NULL;
237 dnlen = strlen(e->e_ndn);
239 for ( ; a != NULL; a = a->acl_next ) {
242 if (a->acl_dn_pat != NULL) {
243 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
244 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
245 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
247 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
251 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
252 *count, a->acl_dn_pat, 0 );
254 patlen = strlen( a->acl_dn_pat );
255 if ( dnlen < patlen )
258 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
259 /* base dn -- entire object DN must match */
260 if ( dnlen != patlen )
263 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
267 if ( dnlen <= patlen )
270 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
273 rdn = dn_rdn( NULL, e->e_ndn );
275 rdnlen = strlen( rdn );
278 if ( rdnlen != dnlen - patlen - 1 )
281 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
282 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
285 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
286 if ( dnlen <= patlen )
288 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
292 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
296 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
300 if ( a->acl_filter != NULL ) {
301 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
302 if ( rc != LDAP_COMPARE_TRUE ) {
307 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
310 if ( attr == NULL || a->acl_attrs == NULL ||
311 ad_inlist( desc, a->acl_attrs ) )
313 Debug( LDAP_DEBUG_ACL,
314 "<= acl_get: [%d] acl %s attr: %s\n",
315 *count, e->e_dn, attr );
318 matches[0].rm_so = matches[0].rm_eo = -1;
321 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
327 * acl_mask - modifies mask based upon the given acl and the
328 * requested access to entry e, attribute attr, value val. if val
329 * is null, access to the whole attribute is assumed (all values).
331 * returns 0 access NOT allowed
335 static slap_control_t
338 slap_access_mask_t *mask,
343 AttributeDescription *desc,
348 int i, odnlen, patlen;
351 char accessmaskbuf[ACCESSMASK_MAXLEN];
353 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
356 assert( mask != NULL );
358 Debug( LDAP_DEBUG_ACL,
359 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
362 Debug( LDAP_DEBUG_ACL,
363 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
364 val ? val->bv_val : "*",
365 op->o_ndn ? op->o_ndn : "",
366 accessmask2str( *mask, accessmaskbuf ) );
368 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
369 slap_access_mask_t oldmask, modmask;
371 ACL_INVALIDATE( modmask );
373 /* AND <who> clauses */
374 if ( b->a_dn_pat != NULL ) {
375 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
378 * if access applies to the entry itself, and the
379 * user is bound as somebody in the same namespace as
380 * the entry, OR the given dn matches the dn pattern
382 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
383 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
387 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
388 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
392 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
393 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
397 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
401 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
402 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
403 int ret = regex_matches( b->a_dn_pat,
404 op->o_ndn, e->e_ndn, matches );
412 if ( e->e_dn == NULL )
415 patlen = strlen( b->a_dn_pat );
416 odnlen = strlen( op->o_ndn );
417 if ( odnlen < patlen )
420 if ( b->a_dn_style == ACL_STYLE_BASE ) {
421 /* base dn -- entire object DN must match */
422 if ( odnlen != patlen )
425 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
429 if ( odnlen <= patlen )
432 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
435 rdn = dn_rdn( NULL, op->o_ndn );
437 rdnlen = strlen( rdn );
440 if ( rdnlen != odnlen - patlen - 1 )
443 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
444 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
447 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
448 if ( odnlen <= patlen )
450 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
454 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
460 if ( b->a_sockurl_pat != NULL ) {
461 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
462 b->a_sockurl_pat, 0, 0 );
464 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
465 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
466 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
467 e->e_ndn, matches ) )
472 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
478 if ( b->a_domain_pat != NULL ) {
479 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
480 b->a_domain_pat, 0, 0 );
482 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
483 if ( b->a_domain_style == ACL_STYLE_REGEX) {
484 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
485 e->e_ndn, matches ) )
490 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
496 if ( b->a_peername_pat != NULL ) {
497 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
498 b->a_peername_pat, 0, 0 );
500 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
501 if ( b->a_peername_style == ACL_STYLE_REGEX) {
502 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
503 e->e_ndn, matches ) )
508 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
514 if ( b->a_sockname_pat != NULL ) {
515 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
516 b->a_sockname_pat, 0, 0 );
518 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
519 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
520 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
521 e->e_ndn, matches ) )
526 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
532 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
537 const char *desc = b->a_dn_at->ad_cname->bv_val;
539 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
542 bv.bv_val = op->o_ndn;
543 bv.bv_len = strlen( bv.bv_val );
545 /* see if asker is listed in dnattr */
546 for( at = attrs_find( e->e_attrs, b->a_dn_at );
548 at = attrs_find( at->a_next, b->a_dn_at ) )
550 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
558 /* have a dnattr match. if this is a self clause then
559 * the target must also match the op dn.
561 if ( b->a_dn_self ) {
562 /* check if the target is an attribute. */
565 /* target is attribute, check if the attribute value
568 rc = value_match( &match, b->a_dn_at,
569 b->a_dn_at->ad_type->sat_equality,
571 /* on match error or no match, fail the ACL clause */
572 if (rc != LDAP_SUCCESS || match != 0 )
576 /* no dnattr match, check if this is a self clause */
577 if ( ! b->a_dn_self )
579 /* this is a self clause, check if the target is an
584 /* target is attribute, check if the attribute value
587 rc = value_match( &match, b->a_dn_at,
588 b->a_dn_at->ad_type->sat_equality,
590 /* on match error or no match, fail the ACL clause */
591 if (rc != LDAP_SUCCESS || match != 0 )
596 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
599 /* b->a_group is an unexpanded entry name, expanded it should be an
600 * entry with objectclass group* and we test to see if odn is one of
601 * the values in the attribute group
603 /* see if asker is listed in dnattr */
604 if ( b->a_group_style == ACL_STYLE_REGEX ) {
605 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
606 if ( dn_normalize(buf) == NULL ) {
607 /* did not expand to a valid dn */
611 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
612 buf[sizeof(buf) - 1] = 0;
615 if (backend_group(be, e, buf, op->o_ndn,
616 b->a_group_oc, b->a_group_at) != 0)
622 #ifdef SLAPD_ACI_ENABLED
623 if ( b->a_aci_at != NULL ) {
625 slap_access_t grant, deny, tgrant, tdeny;
627 /* this case works different from the others above.
628 * since aci's themselves give permissions, we need
629 * to first check b->a_mask, the ACL's access level.
632 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
636 if ( e->e_dn == NULL ) {
640 /* first check if the right being requested
641 * is allowed by the ACL clause.
643 if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
647 /* get the aci attribute */
648 at = attr_find( e->e_attrs, b->a_aci_at );
653 /* start out with nothing granted, nothing denied */
657 /* the aci is an multi-valued attribute. The
658 * rights are determined by OR'ing the individual
659 * rights given by the acis.
661 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
662 if (aci_mask( be, conn, op,
663 e, desc, val, at->a_vals[i],
664 matches, &grant, &deny ) != 0)
671 /* remove anything that the ACL clause does not allow */
672 tgrant &= b->a_mask & ACL_PRIV_MASK;
673 tdeny &= ACL_PRIV_MASK;
675 /* see if we have anything to contribute */
676 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
680 /* this could be improved by changing acl_mask so that it can deal with
681 * by clauses that return grant/deny pairs. Right now, it does either
682 * additive or subtractive rights, but not both at the same time. So,
683 * we need to combine the grant/deny pair into a single rights mask in
684 * a smart way: if either grant or deny is "empty", then we use the
685 * opposite as is, otherwise we remove any denied rights from the grant
686 * rights mask and construct an additive mask.
688 if (ACL_IS_INVALID(tdeny)) {
689 modmask = tgrant | ACL_PRIV_ADDITIVE;
691 } else if (ACL_IS_INVALID(tgrant)) {
692 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
695 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
705 Debug( LDAP_DEBUG_ACL,
706 "<= acl_mask: [%d] applying %s (%s)\n",
707 i, accessmask2str( modmask, accessmaskbuf ),
708 b->a_type == ACL_CONTINUE
710 : b->a_type == ACL_BREAK
717 if( ACL_IS_ADDITIVE(modmask) ) {
719 ACL_PRIV_SET( *mask, modmask );
722 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
724 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
725 /* substract privs */
726 ACL_PRIV_CLR( *mask, modmask );
729 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
736 Debug( LDAP_DEBUG_ACL,
737 "<= acl_mask: [%d] mask: %s\n",
738 i, accessmask2str(*mask, accessmaskbuf), 0 );
740 if( b->a_type == ACL_CONTINUE ) {
743 } else if ( b->a_type == ACL_BREAK ) {
751 Debug( LDAP_DEBUG_ACL,
752 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
753 accessmask2str(*mask, accessmaskbuf), 0, 0 );
758 * acl_check_modlist - check access control on the given entry to see if
759 * it allows the given modifications by the user associated with op.
760 * returns 1 if mods allowed ok
775 assert( be != NULL );
777 /* short circuit root database access */
778 if ( be_isroot( be, op->o_ndn ) ) {
779 Debug( LDAP_DEBUG_ACL,
780 "<= acl_access_allowed: granted to database root\n",
785 /* use backend default access if no backend acls */
786 if( be != NULL && be->be_acl == NULL ) {
787 Debug( LDAP_DEBUG_ACL,
788 "=> access_allowed: backend default %s access %s to \"%s\"\n",
789 access2str( ACL_WRITE ),
790 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
792 return be->be_dfltaccess >= ACL_WRITE;
795 /* be is always non-NULL */
796 /* use global default access if no global acls */
797 } else if ( be == NULL && global_acl == NULL ) {
798 Debug( LDAP_DEBUG_ACL,
799 "=> access_allowed: global default %s access %s to \"%s\"\n",
800 access2str( ACL_WRITE ),
801 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
803 return global_default_access >= ACL_WRITE;
807 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
809 * no-user-modification operational attributes are ignored
810 * by ACL_WRITE checking as any found here are not provided
813 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
814 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
815 " modify access granted\n",
816 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
820 switch ( mlist->sml_op ) {
821 case LDAP_MOD_REPLACE:
823 if ( mlist->sml_bvalues == NULL ) {
826 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
827 if ( ! access_allowed( be, conn, op, e,
828 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
835 case LDAP_MOD_DELETE:
836 if ( mlist->sml_bvalues == NULL ) {
837 if ( ! access_allowed( be, conn, op, e,
838 mlist->sml_desc, NULL, ACL_WRITE ) )
844 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
845 if ( ! access_allowed( be, conn, op, e,
846 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
858 #ifdef SLAPD_ACI_ENABLED
860 aci_bvstrdup( struct berval *bv )
864 s = (char *)ch_malloc(bv->bv_len + 1);
866 memcpy(s, bv->bv_val, bv->bv_len);
879 res = strncasecmp( s, bv->bv_val, bv->bv_len );
883 if (len > (int)bv->bv_len)
885 if (len < (int)bv->bv_len)
906 while (len >= 0 && --ix >= 0) {
907 while (--len >= 0 && *p++ != sep) ;
909 while (len >= 0 && *p == ' ') {
920 while (--len >= 0 && *p != sep) {
924 while (bv->bv_len > 0 && *--p == ' ')
931 struct berval *list )
938 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
941 switch (*bv.bv_val) {
943 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
946 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
947 * the right 's' to mean "set", but in the examples states
948 * that the right 's' means "search". The latter definition
951 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
954 ACL_PRIV_SET(mask, ACL_PRIV_READ);
957 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
960 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
961 * define any equivalent to the AUTH right, so I've just used
964 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
980 struct berval bv, left, right;
983 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
984 if (aci_get_part(&bv, 0, '=', &left) < 0
985 || aci_get_part(&bv, 1, '=', &right) < 0)
987 if (aci_strbvcmp(attr, &bv) == 0)
989 } else if (val == NULL) {
990 if (aci_strbvcmp(attr, &left) == 0)
993 if (aci_strbvcmp(attr, &left) == 0) {
994 /* this is experimental code that implements a
995 * simple (prefix) match of the attribute value.
996 * the ACI draft does not provide for aci's that
997 * apply to specific values, but it would be
998 * nice to have. If the <attr> part of an aci's
999 * rights list is of the form <attr>=<value>,
1000 * that means the aci applies only to attrs with
1001 * the given value. Furthermore, if the attr is
1002 * of the form <attr>=<value>*, then <value> is
1003 * treated as a prefix, and the aci applies to
1004 * any value with that prefix.
1006 * Ideally, this would allow r.e. matches.
1008 if (aci_get_part(&right, 0, '*', &left) < 0
1009 || right.bv_len <= left.bv_len)
1011 if (aci_strbvcmp(val->bv_val, &right) == 0)
1013 } else if (val->bv_len >= left.bv_len) {
1014 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1023 static slap_access_t
1024 aci_list_get_attr_rights(
1025 struct berval *list,
1027 struct berval *val )
1033 /* loop through each rights/attr pair, skip first part (action) */
1035 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1036 if (aci_list_has_attr(&bv, attr, val) == 0)
1038 if (aci_get_part(list, i, ';', &bv) < 0)
1040 mask |= aci_list_map_rights(&bv);
1046 aci_list_get_rights(
1047 struct berval *list,
1050 slap_access_t *grant,
1051 slap_access_t *deny )
1053 struct berval perm, actn;
1054 slap_access_t *mask;
1057 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1064 /* loop through each permissions clause */
1065 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1066 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1068 if (aci_strbvcmp( "grant", &actn ) == 0) {
1070 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1077 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1078 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1085 struct berval *subj,
1086 const char *defgrpoc,
1087 const char *defgrpat,
1095 char *subjdn, *grpdn = NULL;
1098 ObjectClass *grp_oc = NULL;
1099 AttributeDescription *grp_ad = NULL;
1103 /* format of string is "group/objectClassValue/groupAttrName" */
1104 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1108 subjdn = aci_bvstrdup(&bv);
1109 if (subjdn == NULL) {
1113 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1114 grpoc = ch_strdup( defgrpoc );
1116 grpoc = aci_bvstrdup(&bv);
1119 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1120 grpat = ch_strdup( defgrpat );
1122 grpat = aci_bvstrdup(&bv);
1125 rc = slap_str2ad( grpat, &grp_ad, &text );
1126 if( rc != LDAP_SUCCESS ) {
1132 grp_oc = oc_find( grpoc );
1133 grpdn = (char *)ch_malloc(1024);
1135 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1136 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1137 if ( dn_normalize(grpdn) != NULL ) {
1138 rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1143 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1157 AttributeDescription *desc,
1160 regmatch_t *matches,
1161 slap_access_t *grant,
1165 struct berval bv, perms, sdn;
1168 char *attr = desc->ad_cname->bv_val;
1170 /* parse an aci of the form:
1171 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1173 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1174 a full description of the format for this attribute.
1176 For now, this routine only supports scope=entry.
1179 /* check that the aci has all 5 components */
1180 if (aci_get_part(aci, 4, '#', NULL) < 0)
1183 /* check that the aci family is supported */
1184 if (aci_get_part(aci, 0, '#', &bv) < 0)
1187 /* check that the scope is "entry" */
1188 if (aci_get_part(aci, 1, '#', &bv) < 0
1189 || aci_strbvcmp( "entry", &bv ) != 0)
1194 /* get the list of permissions clauses, bail if empty */
1195 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1198 /* check if any permissions allow desired access */
1199 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1202 /* see if we have a DN match */
1203 if (aci_get_part(aci, 3, '#', &bv) < 0)
1206 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1209 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1210 subjdn = aci_bvstrdup(&sdn);
1214 if ( dn_normalize(subjdn) != NULL )
1215 if (strcasecmp(op->o_ndn, subjdn) != 0)
1221 if (aci_strbvcmp( "self", &bv ) == 0) {
1222 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1225 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1226 char *dnattr = aci_bvstrdup(&sdn);
1228 AttributeDescription *ad = NULL;
1231 rc = slap_str2ad( dnattr, &ad, &text );
1234 if( rc != LDAP_SUCCESS ) {
1240 bv.bv_val = op->o_ndn;
1241 bv.bv_len = strlen( bv.bv_val );
1243 for(at = attrs_find( e->e_attrs, ad );
1245 at = attrs_find( at->a_next, ad ) )
1247 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1257 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1258 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1261 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1262 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1270 #endif /* SLAPD_ACI_ENABLED */
1278 regmatch_t *matches)
1287 bufsiz--; /* leave space for lone $ */
1290 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1291 /* did we previously see a $ */
1296 } else if (*sp >= '0' && *sp <= '9' ) {
1303 i = matches[n].rm_so;
1304 l = matches[n].rm_eo;
1305 for ( ; size < 512 && i < l; size++, i++ ) {
1323 /* must have ended with a single $ */
1330 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1331 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1336 char *pat, /* pattern to expand and match against */
1337 char *str, /* string to match against pattern */
1338 char *buf, /* buffer with $N expansion variables */
1339 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1346 if(str == NULL) str = "";
1348 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1349 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1351 regerror(rc, &re, error, sizeof(error));
1353 Debug( LDAP_DEBUG_TRACE,
1354 "compile( \"%s\", \"%s\") failed %s\n",
1359 rc = regexec(&re, str, 0, NULL, 0);
1362 Debug( LDAP_DEBUG_TRACE,
1363 "=> regex_matches: string: %s\n", str, 0, 0 );
1364 Debug( LDAP_DEBUG_TRACE,
1365 "=> regex_matches: rc: %d %s\n",
1366 rc, !rc ? "matches" : "no matches", 0 );