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_access_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];
88 slap_access_mask_t mask;
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
344 slap_access_mask_t *mask,
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_access_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 #ifdef SLAPD_ACI_ENABLED
640 if ( b->a_aci_at != NULL ) {
642 slap_access_t grant, deny, tgrant, tdeny;
644 /* this case works different from the others above.
645 * since aci's themselves give permissions, we need
646 * to first check b->a_mask, the ACL's access level.
649 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
653 if ( e->e_dn == NULL ) {
657 /* first check if the right being requested
658 * is allowed by the ACL clause.
660 if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
664 /* get the aci attribute */
665 at = attr_find( e->e_attrs, b->a_aci_at );
670 /* start out with nothing granted, nothing denied */
674 /* the aci is an multi-valued attribute. The
675 * rights are determined by OR'ing the individual
676 * rights given by the acis.
678 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
679 if (aci_mask( be, conn, op,
680 e, desc, val, at->a_vals[i],
681 matches, &grant, &deny ) != 0)
688 /* remove anything that the ACL clause does not allow */
689 tgrant &= b->a_mask & ACL_PRIV_MASK;
690 tdeny &= ACL_PRIV_MASK;
692 /* see if we have anything to contribute */
693 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
697 /* this could be improved by changing acl_mask so that it can deal with
698 * by clauses that return grant/deny pairs. Right now, it does either
699 * additive or subtractive rights, but not both at the same time. So,
700 * we need to combine the grant/deny pair into a single rights mask in
701 * a smart way: if either grant or deny is "empty", then we use the
702 * opposite as is, otherwise we remove any denied rights from the grant
703 * rights mask and construct an additive mask.
705 if (ACL_IS_INVALID(tdeny)) {
706 modmask = tgrant | ACL_PRIV_ADDITIVE;
708 } else if (ACL_IS_INVALID(tgrant)) {
709 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
712 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
722 Debug( LDAP_DEBUG_ACL,
723 "<= acl_mask: [%d] applying %s (%s)\n",
724 i, accessmask2str( modmask, accessmaskbuf ),
725 b->a_type == ACL_CONTINUE
727 : b->a_type == ACL_BREAK
734 if( ACL_IS_ADDITIVE(modmask) ) {
736 ACL_PRIV_SET( *mask, modmask );
739 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
741 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
742 /* substract privs */
743 ACL_PRIV_CLR( *mask, modmask );
746 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
753 Debug( LDAP_DEBUG_ACL,
754 "<= acl_mask: [%d] mask: %s\n",
755 i, accessmask2str(*mask, accessmaskbuf), 0 );
757 if( b->a_type == ACL_CONTINUE ) {
760 } else if ( b->a_type == ACL_BREAK ) {
768 Debug( LDAP_DEBUG_ACL,
769 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
770 accessmask2str(*mask, accessmaskbuf), 0, 0 );
775 * acl_check_modlist - check access control on the given entry to see if
776 * it allows the given modifications by the user associated with op.
777 * returns 1 if mods allowed ok
792 assert( be != NULL );
794 /* short circuit root database access */
795 if ( be_isroot( be, op->o_ndn ) ) {
796 Debug( LDAP_DEBUG_ACL,
797 "<= acl_access_allowed: granted to database root\n",
802 /* use backend default access if no backend acls */
803 if( be != NULL && be->be_acl == NULL ) {
804 Debug( LDAP_DEBUG_ACL,
805 "=> access_allowed: backend default %s access %s to \"%s\"\n",
806 access2str( ACL_WRITE ),
807 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
809 return be->be_dfltaccess >= ACL_WRITE;
812 /* be is always non-NULL */
813 /* use global default access if no global acls */
814 } else if ( be == NULL && global_acl == NULL ) {
815 Debug( LDAP_DEBUG_ACL,
816 "=> access_allowed: global default %s access %s to \"%s\"\n",
817 access2str( ACL_WRITE ),
818 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
820 return global_default_access >= ACL_WRITE;
824 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
826 * no-user-modification operational attributes are ignored
827 * by ACL_WRITE checking as any found here are not provided
830 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
831 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
832 " modify access granted\n",
833 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
837 switch ( mlist->sml_op ) {
838 case LDAP_MOD_REPLACE:
840 if ( mlist->sml_bvalues == NULL ) {
843 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
844 if ( ! access_allowed( be, conn, op, e,
845 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
852 case LDAP_MOD_DELETE:
853 if ( mlist->sml_bvalues == NULL ) {
854 if ( ! access_allowed( be, conn, op, e,
855 mlist->sml_desc, NULL, ACL_WRITE ) )
861 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
862 if ( ! access_allowed( be, conn, op, e,
863 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
876 aci_bvstrdup( struct berval *bv )
880 s = (char *)ch_malloc(bv->bv_len + 1);
882 memcpy(s, bv->bv_val, bv->bv_len);
895 res = strncasecmp( s, bv->bv_val, bv->bv_len );
899 if (len > (int)bv->bv_len)
901 if (len < (int)bv->bv_len)
922 while (len >= 0 && --ix >= 0) {
923 while (--len >= 0 && *p++ != sep) ;
925 while (len >= 0 && *p == ' ') {
936 while (--len >= 0 && *p != sep) {
940 while (bv->bv_len > 0 && *--p == ' ')
946 aci_set_gather (void *cookie, char *name, char *attr)
953 } *cp = (void *)cookie;
954 struct berval **bvals = NULL;
959 /* this routine needs to return the bervals instead of
960 * plain strings, since syntax is not known. It should
961 * also return the syntax or some "comparison cookie".
964 if ((ndn = ch_strdup(name)) != NULL) {
965 if (dn_normalize(ndn) != NULL) {
967 AttributeDescription *desc = NULL;
968 if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
969 backend_attribute(cp->be, NULL /*cp->conn*/,
970 NULL /*cp->op*/, cp->e,
973 for (i = 0; bvals[i] != NULL; i++) { }
974 vals = ch_calloc(i + 1, sizeof(char *));
977 vals[i] = bvals[i]->bv_val;
978 bvals[i]->bv_val = NULL;
1011 set = aci_bvstrdup(subj);
1016 struct berval **bvals;
1018 AttributeDescription *desc = NULL;
1020 /* format of string is "entry/setAttrName" */
1021 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1025 subjdn = aci_bvstrdup(&bv);
1026 if ( subjdn == NULL ) {
1030 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1031 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1033 setat = aci_bvstrdup(&bv);
1035 if ( setat != NULL ) {
1036 if ( dn_normalize(subjdn) != NULL
1037 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1039 backend_attribute(be, NULL, NULL, e,
1040 subjdn, desc, &bvals);
1042 if ( bvals != NULL ) {
1043 if ( bvals[0] != NULL )
1044 set = ch_strdup(bvals[0]->bv_val);
1045 ber_bvecfree(bvals);
1058 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1064 #ifdef SLAPD_ACI_ENABLED
1066 aci_list_map_rights(
1067 struct berval *list )
1074 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1077 switch (*bv.bv_val) {
1079 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1082 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1083 * the right 's' to mean "set", but in the examples states
1084 * that the right 's' means "search". The latter definition
1087 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1090 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1093 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1096 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1097 * define any equivalent to the AUTH right, so I've just used
1100 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1112 struct berval *list,
1114 struct berval *val )
1116 struct berval bv, left, right;
1119 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1120 if (aci_get_part(&bv, 0, '=', &left) < 0
1121 || aci_get_part(&bv, 1, '=', &right) < 0)
1123 if (aci_strbvcmp(attr, &bv) == 0)
1125 } else if (val == NULL) {
1126 if (aci_strbvcmp(attr, &left) == 0)
1129 if (aci_strbvcmp(attr, &left) == 0) {
1130 /* this is experimental code that implements a
1131 * simple (prefix) match of the attribute value.
1132 * the ACI draft does not provide for aci's that
1133 * apply to specific values, but it would be
1134 * nice to have. If the <attr> part of an aci's
1135 * rights list is of the form <attr>=<value>,
1136 * that means the aci applies only to attrs with
1137 * the given value. Furthermore, if the attr is
1138 * of the form <attr>=<value>*, then <value> is
1139 * treated as a prefix, and the aci applies to
1140 * any value with that prefix.
1142 * Ideally, this would allow r.e. matches.
1144 if (aci_get_part(&right, 0, '*', &left) < 0
1145 || right.bv_len <= left.bv_len)
1147 if (aci_strbvcmp(val->bv_val, &right) == 0)
1149 } else if (val->bv_len >= left.bv_len) {
1150 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1159 static slap_access_t
1160 aci_list_get_attr_rights(
1161 struct berval *list,
1163 struct berval *val )
1169 /* loop through each rights/attr pair, skip first part (action) */
1171 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1172 if (aci_list_has_attr(&bv, attr, val) == 0)
1174 if (aci_get_part(list, i, ';', &bv) < 0)
1176 mask |= aci_list_map_rights(&bv);
1182 aci_list_get_rights(
1183 struct berval *list,
1186 slap_access_t *grant,
1187 slap_access_t *deny )
1189 struct berval perm, actn;
1190 slap_access_t *mask;
1193 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1200 /* loop through each permissions clause */
1201 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1202 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1204 if (aci_strbvcmp( "grant", &actn ) == 0) {
1206 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1213 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1214 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1221 struct berval *subj,
1222 const char *defgrpoc,
1223 const char *defgrpat,
1231 char *subjdn, *grpdn = NULL;
1234 ObjectClass *grp_oc = NULL;
1235 AttributeDescription *grp_ad = NULL;
1239 /* format of string is "group/objectClassValue/groupAttrName" */
1240 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1244 subjdn = aci_bvstrdup(&bv);
1245 if (subjdn == NULL) {
1249 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1250 grpoc = ch_strdup( defgrpoc );
1252 grpoc = aci_bvstrdup(&bv);
1255 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1256 grpat = ch_strdup( defgrpat );
1258 grpat = aci_bvstrdup(&bv);
1261 rc = slap_str2ad( grpat, &grp_ad, &text );
1262 if( rc != LDAP_SUCCESS ) {
1268 grp_oc = oc_find( grpoc );
1269 grpdn = (char *)ch_malloc(1024);
1271 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1272 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1273 if ( dn_normalize(grpdn) != NULL ) {
1274 rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1279 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1293 AttributeDescription *desc,
1296 regmatch_t *matches,
1297 slap_access_t *grant,
1301 struct berval bv, perms, sdn;
1304 char *attr = desc->ad_cname->bv_val;
1306 /* parse an aci of the form:
1307 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1309 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1310 a full description of the format for this attribute.
1312 For now, this routine only supports scope=entry.
1315 /* check that the aci has all 5 components */
1316 if (aci_get_part(aci, 4, '#', NULL) < 0)
1319 /* check that the aci family is supported */
1320 if (aci_get_part(aci, 0, '#', &bv) < 0)
1323 /* check that the scope is "entry" */
1324 if (aci_get_part(aci, 1, '#', &bv) < 0
1325 || aci_strbvcmp( "entry", &bv ) != 0)
1330 /* get the list of permissions clauses, bail if empty */
1331 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1334 /* check if any permissions allow desired access */
1335 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1338 /* see if we have a DN match */
1339 if (aci_get_part(aci, 3, '#', &bv) < 0)
1342 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1345 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1346 subjdn = aci_bvstrdup(&sdn);
1350 if ( dn_normalize(subjdn) != NULL )
1351 if (strcasecmp(op->o_ndn, subjdn) != 0)
1357 if (aci_strbvcmp( "self", &bv ) == 0) {
1358 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1361 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1362 char *dnattr = aci_bvstrdup(&sdn);
1364 AttributeDescription *ad = NULL;
1367 rc = slap_str2ad( dnattr, &ad, &text );
1370 if( rc != LDAP_SUCCESS ) {
1376 bv.bv_val = op->o_ndn;
1377 bv.bv_len = strlen( bv.bv_val );
1379 for(at = attrs_find( e->e_attrs, ad );
1381 at = attrs_find( at->a_next, ad ) )
1383 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1393 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1394 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1397 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1398 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1401 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1402 if (aci_match_set(&sdn, be, e, conn, op, 0))
1405 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1406 if (aci_match_set(&sdn, be, e, conn, op, 1))
1414 #endif /* SLAPD_ACI_ENABLED */
1422 regmatch_t *matches)
1431 bufsiz--; /* leave space for lone $ */
1434 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1435 /* did we previously see a $ */
1440 } else if (*sp >= '0' && *sp <= '9' ) {
1447 i = matches[n].rm_so;
1448 l = matches[n].rm_eo;
1449 for ( ; size < 512 && i < l; size++, i++ ) {
1467 /* must have ended with a single $ */
1474 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1475 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1480 char *pat, /* pattern to expand and match against */
1481 char *str, /* string to match against pattern */
1482 char *buf, /* buffer with $N expansion variables */
1483 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1490 if(str == NULL) str = "";
1492 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1493 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1495 regerror(rc, &re, error, sizeof(error));
1497 Debug( LDAP_DEBUG_TRACE,
1498 "compile( \"%s\", \"%s\") failed %s\n",
1503 rc = regexec(&re, str, 0, NULL, 0);
1506 Debug( LDAP_DEBUG_TRACE,
1507 "=> regex_matches: string: %s\n", str, 0, 0 );
1508 Debug( LDAP_DEBUG_TRACE,
1509 "=> regex_matches: rc: %d %s\n",
1510 rc, !rc ? "matches" : "no matches", 0 );