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 ) )
120 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
121 " %s access granted\n",
126 /* use backend default access if no backend acls */
127 if( be != NULL && be->be_acl == NULL ) {
128 Debug( LDAP_DEBUG_ACL,
129 "=> access_allowed: backend default %s access %s to \"%s\"\n",
130 access2str( access ),
131 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
133 return be->be_dfltaccess >= access;
136 /* be is always non-NULL */
137 /* use global default access if no global acls */
138 } else if ( be == NULL && global_acl == NULL ) {
139 Debug( LDAP_DEBUG_ACL,
140 "=> access_allowed: global default %s access %s to \"%s\"\n",
141 access2str( access ),
142 global_default_access >= access ? "granted" : "denied", op->o_dn );
144 return global_default_access >= access;
149 memset(matches, '\0', sizeof(matches));
155 while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
159 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
160 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
161 (int)matches[i].rm_so, (int)matches[i].rm_eo );
163 if( matches[i].rm_so <= matches[0].rm_eo ) {
165 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
166 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
169 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
172 control = acl_mask( a, &mask, be, conn, op,
173 e, desc, val, matches );
175 if ( control != ACL_BREAK ) {
179 memset(matches, '\0', sizeof(matches));
182 if ( ACL_IS_INVALID( mask ) ) {
183 Debug( LDAP_DEBUG_ACL,
184 "=> access_allowed: \"%s\" (%s) invalid!\n",
188 } else if ( control == ACL_BREAK ) {
189 Debug( LDAP_DEBUG_ACL,
190 "=> access_allowed: no more rules\n", 0, 0, 0);
194 Debug( LDAP_DEBUG_ACL,
195 "=> access_allowed: %s access %s by %s\n",
196 access2str( access ),
197 ACL_GRANT(mask, access) ? "granted" : "denied",
198 accessmask2str( mask, accessmaskbuf ) );
200 return ACL_GRANT(mask, access);
204 * acl_get - return the acl applicable to entry e, attribute
205 * attr. the acl returned is suitable for use in subsequent calls to
206 * acl_access_allowed().
209 static AccessControl *
216 AttributeDescription *desc,
218 regmatch_t *matches )
224 assert( count != NULL );
226 attr = desc ? desc->ad_cname->bv_val : NULL;
241 dnlen = strlen(e->e_ndn);
243 for ( ; a != NULL; a = a->acl_next ) {
246 if (a->acl_dn_pat != NULL) {
247 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
248 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
249 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
251 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
255 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
256 *count, a->acl_dn_pat, 0 );
258 patlen = strlen( a->acl_dn_pat );
259 if ( dnlen < patlen )
262 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
263 /* base dn -- entire object DN must match */
264 if ( dnlen != patlen )
267 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
271 if ( dnlen <= patlen )
274 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
277 rdn = dn_rdn( NULL, e->e_ndn );
279 rdnlen = strlen( rdn );
282 if ( rdnlen != dnlen - patlen - 1 )
285 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
286 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
289 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
290 if ( dnlen <= patlen )
292 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
296 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
300 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
304 if ( a->acl_filter != NULL ) {
305 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
306 if ( rc != LDAP_COMPARE_TRUE ) {
311 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
314 if ( attr == NULL || a->acl_attrs == NULL ||
315 ad_inlist( desc, a->acl_attrs ) )
317 Debug( LDAP_DEBUG_ACL,
318 "<= acl_get: [%d] acl %s attr: %s\n",
319 *count, e->e_dn, attr );
322 matches[0].rm_so = matches[0].rm_eo = -1;
325 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
331 * acl_mask - modifies mask based upon the given acl and the
332 * requested access to entry e, attribute attr, value val. if val
333 * is null, access to the whole attribute is assumed (all values).
335 * returns 0 access NOT allowed
339 static slap_control_t
342 slap_access_mask_t *mask,
347 AttributeDescription *desc,
352 int i, odnlen, patlen;
355 char accessmaskbuf[ACCESSMASK_MAXLEN];
357 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
360 assert( mask != NULL );
362 Debug( LDAP_DEBUG_ACL,
363 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
366 Debug( LDAP_DEBUG_ACL,
367 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
368 val ? val->bv_val : "*",
369 op->o_ndn ? op->o_ndn : "",
370 accessmask2str( *mask, accessmaskbuf ) );
372 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
373 slap_access_mask_t oldmask, modmask;
375 ACL_INVALIDATE( modmask );
377 /* AND <who> clauses */
378 if ( b->a_dn_pat != NULL ) {
379 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
382 * if access applies to the entry itself, and the
383 * user is bound as somebody in the same namespace as
384 * the entry, OR the given dn matches the dn pattern
386 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
387 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
391 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
392 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
396 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
397 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
401 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
405 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
406 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
407 int ret = regex_matches( b->a_dn_pat,
408 op->o_ndn, e->e_ndn, matches );
416 if ( e->e_dn == NULL )
419 patlen = strlen( b->a_dn_pat );
420 odnlen = strlen( op->o_ndn );
421 if ( odnlen < patlen )
424 if ( b->a_dn_style == ACL_STYLE_BASE ) {
425 /* base dn -- entire object DN must match */
426 if ( odnlen != patlen )
429 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
433 if ( odnlen <= patlen )
436 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
439 rdn = dn_rdn( NULL, op->o_ndn );
441 rdnlen = strlen( rdn );
444 if ( rdnlen != odnlen - patlen - 1 )
447 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
448 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
451 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
452 if ( odnlen <= patlen )
454 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
458 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
464 if ( b->a_sockurl_pat != NULL ) {
465 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
466 b->a_sockurl_pat, 0, 0 );
468 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
469 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
470 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
471 e->e_ndn, matches ) )
476 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
482 if ( b->a_domain_pat != NULL ) {
483 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
484 b->a_domain_pat, 0, 0 );
486 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
487 if ( b->a_domain_style == ACL_STYLE_REGEX) {
488 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
489 e->e_ndn, matches ) )
494 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
500 if ( b->a_peername_pat != NULL ) {
501 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
502 b->a_peername_pat, 0, 0 );
504 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
505 if ( b->a_peername_style == ACL_STYLE_REGEX) {
506 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
507 e->e_ndn, matches ) )
512 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
518 if ( b->a_sockname_pat != NULL ) {
519 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
520 b->a_sockname_pat, 0, 0 );
522 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
523 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
524 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
525 e->e_ndn, matches ) )
530 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
536 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
541 const char *desc = b->a_dn_at->ad_cname->bv_val;
543 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
546 bv.bv_val = op->o_ndn;
547 bv.bv_len = strlen( bv.bv_val );
549 /* see if asker is listed in dnattr */
550 for( at = attrs_find( e->e_attrs, b->a_dn_at );
552 at = attrs_find( at->a_next, b->a_dn_at ) )
554 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
562 /* have a dnattr match. if this is a self clause then
563 * the target must also match the op dn.
565 if ( b->a_dn_self ) {
566 /* check if the target is an attribute. */
569 /* target is attribute, check if the attribute value
572 rc = value_match( &match, b->a_dn_at,
573 b->a_dn_at->ad_type->sat_equality, 0,
575 /* on match error or no match, fail the ACL clause */
576 if (rc != LDAP_SUCCESS || match != 0 )
580 /* no dnattr match, check if this is a self clause */
581 if ( ! b->a_dn_self )
583 /* this is a self clause, check if the target is an
588 /* target is attribute, check if the attribute value
591 rc = value_match( &match, b->a_dn_at,
592 b->a_dn_at->ad_type->sat_equality, 0,
595 /* on match error or no match, fail the ACL clause */
596 if (rc != LDAP_SUCCESS || match != 0 )
601 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
604 /* b->a_group is an unexpanded entry name, expanded it should be an
605 * entry with objectclass group* and we test to see if odn is one of
606 * the values in the attribute group
608 /* see if asker is listed in dnattr */
609 if ( b->a_group_style == ACL_STYLE_REGEX ) {
610 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
611 if ( dn_normalize(buf) == NULL ) {
612 /* did not expand to a valid dn */
616 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
617 buf[sizeof(buf) - 1] = 0;
620 if (backend_group(be, e, buf, op->o_ndn,
621 b->a_group_oc, b->a_group_at) != 0)
627 #ifdef SLAPD_ACI_ENABLED
628 if ( b->a_set_pat != NULL ) {
631 bv.bv_val = b->a_set_pat;
632 bv.bv_len = strlen(b->a_set_pat);
633 if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
638 if ( b->a_aci_at != NULL ) {
640 slap_access_t grant, deny, tgrant, tdeny;
642 /* this case works different from the others above.
643 * since aci's themselves give permissions, we need
644 * to first check b->a_mask, the ACL's access level.
647 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
651 if ( e->e_dn == NULL ) {
655 /* first check if the right being requested
656 * is allowed by the ACL clause.
658 if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
662 /* get the aci attribute */
663 at = attr_find( e->e_attrs, b->a_aci_at );
668 /* start out with nothing granted, nothing denied */
672 /* the aci is an multi-valued attribute. The
673 * rights are determined by OR'ing the individual
674 * rights given by the acis.
676 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
677 if (aci_mask( be, conn, op,
678 e, desc, val, at->a_vals[i],
679 matches, &grant, &deny ) != 0)
686 /* remove anything that the ACL clause does not allow */
687 tgrant &= b->a_mask & ACL_PRIV_MASK;
688 tdeny &= ACL_PRIV_MASK;
690 /* see if we have anything to contribute */
691 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
695 /* this could be improved by changing acl_mask so that it can deal with
696 * by clauses that return grant/deny pairs. Right now, it does either
697 * additive or subtractive rights, but not both at the same time. So,
698 * we need to combine the grant/deny pair into a single rights mask in
699 * a smart way: if either grant or deny is "empty", then we use the
700 * opposite as is, otherwise we remove any denied rights from the grant
701 * rights mask and construct an additive mask.
703 if (ACL_IS_INVALID(tdeny)) {
704 modmask = tgrant | ACL_PRIV_ADDITIVE;
706 } else if (ACL_IS_INVALID(tgrant)) {
707 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
710 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
720 Debug( LDAP_DEBUG_ACL,
721 "<= acl_mask: [%d] applying %s (%s)\n",
722 i, accessmask2str( modmask, accessmaskbuf ),
723 b->a_type == ACL_CONTINUE
725 : b->a_type == ACL_BREAK
732 if( ACL_IS_ADDITIVE(modmask) ) {
734 ACL_PRIV_SET( *mask, modmask );
737 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
739 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
740 /* substract privs */
741 ACL_PRIV_CLR( *mask, modmask );
744 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
751 Debug( LDAP_DEBUG_ACL,
752 "<= acl_mask: [%d] mask: %s\n",
753 i, accessmask2str(*mask, accessmaskbuf), 0 );
755 if( b->a_type == ACL_CONTINUE ) {
758 } else if ( b->a_type == ACL_BREAK ) {
766 Debug( LDAP_DEBUG_ACL,
767 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
768 accessmask2str(*mask, accessmaskbuf), 0, 0 );
773 * acl_check_modlist - check access control on the given entry to see if
774 * it allows the given modifications by the user associated with op.
775 * returns 1 if mods allowed ok
790 assert( be != NULL );
792 /* short circuit root database access */
793 if ( be_isroot( be, op->o_ndn ) ) {
794 Debug( LDAP_DEBUG_ACL,
795 "<= acl_access_allowed: granted to database root\n",
800 /* use backend default access if no backend acls */
801 if( be != NULL && be->be_acl == NULL ) {
802 Debug( LDAP_DEBUG_ACL,
803 "=> access_allowed: backend default %s access %s to \"%s\"\n",
804 access2str( ACL_WRITE ),
805 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
807 return be->be_dfltaccess >= ACL_WRITE;
810 /* be is always non-NULL */
811 /* use global default access if no global acls */
812 } else if ( be == NULL && global_acl == NULL ) {
813 Debug( LDAP_DEBUG_ACL,
814 "=> access_allowed: global default %s access %s to \"%s\"\n",
815 access2str( ACL_WRITE ),
816 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
818 return global_default_access >= ACL_WRITE;
822 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
824 * no-user-modification operational attributes are ignored
825 * by ACL_WRITE checking as any found here are not provided
828 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
829 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
830 " modify access granted\n",
831 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
835 switch ( mlist->sml_op ) {
836 case LDAP_MOD_REPLACE:
838 if ( mlist->sml_bvalues == NULL ) {
841 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
842 if ( ! access_allowed( be, conn, op, e,
843 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
850 case LDAP_MOD_DELETE:
851 if ( mlist->sml_bvalues == NULL ) {
852 if ( ! access_allowed( be, conn, op, e,
853 mlist->sml_desc, NULL, ACL_WRITE ) )
859 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
860 if ( ! access_allowed( be, conn, op, e,
861 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
873 #ifdef SLAPD_ACI_ENABLED
875 aci_bvstrdup( struct berval *bv )
879 s = (char *)ch_malloc(bv->bv_len + 1);
881 memcpy(s, bv->bv_val, bv->bv_len);
894 res = strncasecmp( s, bv->bv_val, bv->bv_len );
898 if (len > (int)bv->bv_len)
900 if (len < (int)bv->bv_len)
921 while (len >= 0 && --ix >= 0) {
922 while (--len >= 0 && *p++ != sep) ;
924 while (len >= 0 && *p == ' ') {
935 while (--len >= 0 && *p != sep) {
939 while (bv->bv_len > 0 && *--p == ' ')
946 struct berval *list )
953 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
956 switch (*bv.bv_val) {
958 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
961 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
962 * the right 's' to mean "set", but in the examples states
963 * that the right 's' means "search". The latter definition
966 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
969 ACL_PRIV_SET(mask, ACL_PRIV_READ);
972 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
975 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
976 * define any equivalent to the AUTH right, so I've just used
979 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
995 struct berval bv, left, right;
998 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
999 if (aci_get_part(&bv, 0, '=', &left) < 0
1000 || aci_get_part(&bv, 1, '=', &right) < 0)
1002 if (aci_strbvcmp(attr, &bv) == 0)
1004 } else if (val == NULL) {
1005 if (aci_strbvcmp(attr, &left) == 0)
1008 if (aci_strbvcmp(attr, &left) == 0) {
1009 /* this is experimental code that implements a
1010 * simple (prefix) match of the attribute value.
1011 * the ACI draft does not provide for aci's that
1012 * apply to specific values, but it would be
1013 * nice to have. If the <attr> part of an aci's
1014 * rights list is of the form <attr>=<value>,
1015 * that means the aci applies only to attrs with
1016 * the given value. Furthermore, if the attr is
1017 * of the form <attr>=<value>*, then <value> is
1018 * treated as a prefix, and the aci applies to
1019 * any value with that prefix.
1021 * Ideally, this would allow r.e. matches.
1023 if (aci_get_part(&right, 0, '*', &left) < 0
1024 || right.bv_len <= left.bv_len)
1026 if (aci_strbvcmp(val->bv_val, &right) == 0)
1028 } else if (val->bv_len >= left.bv_len) {
1029 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1038 static slap_access_t
1039 aci_list_get_attr_rights(
1040 struct berval *list,
1042 struct berval *val )
1048 /* loop through each rights/attr pair, skip first part (action) */
1050 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1051 if (aci_list_has_attr(&bv, attr, val) == 0)
1053 if (aci_get_part(list, i, ';', &bv) < 0)
1055 mask |= aci_list_map_rights(&bv);
1061 aci_list_get_rights(
1062 struct berval *list,
1065 slap_access_t *grant,
1066 slap_access_t *deny )
1068 struct berval perm, actn;
1069 slap_access_t *mask;
1072 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1079 /* loop through each permissions clause */
1080 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1081 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1083 if (aci_strbvcmp( "grant", &actn ) == 0) {
1085 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1092 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1093 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1100 struct berval *subj,
1101 const char *defgrpoc,
1102 const char *defgrpat,
1110 char *subjdn, *grpdn = NULL;
1113 ObjectClass *grp_oc = NULL;
1114 AttributeDescription *grp_ad = NULL;
1118 /* format of string is "group/objectClassValue/groupAttrName" */
1119 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1123 subjdn = aci_bvstrdup(&bv);
1124 if (subjdn == NULL) {
1128 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1129 grpoc = ch_strdup( defgrpoc );
1131 grpoc = aci_bvstrdup(&bv);
1134 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1135 grpat = ch_strdup( defgrpat );
1137 grpat = aci_bvstrdup(&bv);
1140 rc = slap_str2ad( grpat, &grp_ad, &text );
1141 if( rc != LDAP_SUCCESS ) {
1147 grp_oc = oc_find( grpoc );
1148 grpdn = (char *)ch_malloc(1024);
1150 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1151 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1152 if ( dn_normalize(grpdn) != NULL ) {
1153 rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1158 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1167 aci_set_gather (void *cookie, char *name, char *attr)
1174 } *cp = (void *)cookie;
1175 struct berval **bvals = NULL;
1180 /* this routine needs to return the bervals instead of
1181 * plain strings, since syntax is not known. It should
1182 * also return the syntax or some "comparison cookie".
1185 if ((ndn = ch_strdup(name)) != NULL) {
1186 if (dn_normalize(ndn) != NULL) {
1188 AttributeDescription *desc = NULL;
1189 if (slap_str2ad(attr, &desc, &text) == 0) {
1190 backend_attribute(cp->be, NULL /*cp->conn*/,
1191 NULL /*cp->op*/, cp->e,
1193 if (bvals != NULL) {
1194 for (i = 0; bvals[i] != NULL; i++) { }
1195 vals = ch_calloc(i + 1, sizeof(char *));
1198 vals[i] = bvals[i]->bv_val;
1199 bvals[i]->bv_val = NULL;
1202 ber_bvecfree(bvals);
1214 struct berval *subj,
1232 set = aci_bvstrdup(subj);
1237 struct berval **bvals;
1239 AttributeDescription *desc = NULL;
1241 /* format of string is "entry/setAttrName" */
1242 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1246 subjdn = aci_bvstrdup(&bv);
1247 if ( subjdn == NULL ) {
1251 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1252 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1254 setat = aci_bvstrdup(&bv);
1256 if ( setat != NULL ) {
1257 if ( dn_normalize(subjdn) != NULL
1258 && slap_str2ad(setat, &desc, &text) == 0 )
1260 backend_attribute(be, NULL, NULL, e,
1261 subjdn, desc, &bvals);
1263 if ( bvals != NULL ) {
1264 if ( bvals[0] != NULL )
1265 set = ch_strdup(bvals[0]->bv_val);
1266 ber_bvecfree(bvals);
1279 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1291 AttributeDescription *desc,
1294 regmatch_t *matches,
1295 slap_access_t *grant,
1299 struct berval bv, perms, sdn;
1302 char *attr = desc->ad_cname->bv_val;
1304 /* parse an aci of the form:
1305 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1307 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1308 a full description of the format for this attribute.
1310 For now, this routine only supports scope=entry.
1313 /* check that the aci has all 5 components */
1314 if (aci_get_part(aci, 4, '#', NULL) < 0)
1317 /* check that the aci family is supported */
1318 if (aci_get_part(aci, 0, '#', &bv) < 0)
1321 /* check that the scope is "entry" */
1322 if (aci_get_part(aci, 1, '#', &bv) < 0
1323 || aci_strbvcmp( "entry", &bv ) != 0)
1328 /* get the list of permissions clauses, bail if empty */
1329 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1332 /* check if any permissions allow desired access */
1333 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1336 /* see if we have a DN match */
1337 if (aci_get_part(aci, 3, '#', &bv) < 0)
1340 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1343 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1344 subjdn = aci_bvstrdup(&sdn);
1348 if ( dn_normalize(subjdn) != NULL )
1349 if (strcasecmp(op->o_ndn, subjdn) != 0)
1355 if (aci_strbvcmp( "self", &bv ) == 0) {
1356 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1359 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1360 char *dnattr = aci_bvstrdup(&sdn);
1362 AttributeDescription *ad = NULL;
1365 rc = slap_str2ad( dnattr, &ad, &text );
1368 if( rc != LDAP_SUCCESS ) {
1374 bv.bv_val = op->o_ndn;
1375 bv.bv_len = strlen( bv.bv_val );
1377 for(at = attrs_find( e->e_attrs, ad );
1379 at = attrs_find( at->a_next, ad ) )
1381 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1391 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1392 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1395 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1396 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1399 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1400 if (aci_match_set(&sdn, be, e, conn, op, 0))
1403 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1404 if (aci_match_set(&sdn, be, e, conn, op, 1))
1412 #endif /* SLAPD_ACI_ENABLED */
1420 regmatch_t *matches)
1429 bufsiz--; /* leave space for lone $ */
1432 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1433 /* did we previously see a $ */
1438 } else if (*sp >= '0' && *sp <= '9' ) {
1445 i = matches[n].rm_so;
1446 l = matches[n].rm_eo;
1447 for ( ; size < 512 && i < l; size++, i++ ) {
1465 /* must have ended with a single $ */
1472 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1473 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1478 char *pat, /* pattern to expand and match against */
1479 char *str, /* string to match against pattern */
1480 char *buf, /* buffer with $N expansion variables */
1481 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1488 if(str == NULL) str = "";
1490 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1491 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1493 regerror(rc, &re, error, sizeof(error));
1495 Debug( LDAP_DEBUG_TRACE,
1496 "compile( \"%s\", \"%s\") failed %s\n",
1501 rc = regexec(&re, str, 0, NULL, 0);
1504 Debug( LDAP_DEBUG_TRACE,
1505 "=> regex_matches: string: %s\n", str, 0, 0 );
1506 Debug( LDAP_DEBUG_TRACE,
1507 "=> regex_matches: rc: %d %s\n",
1508 rc, !rc ? "matches" : "no matches", 0 );