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);
57 * access_allowed - check whether op->o_ndn is allowed the requested access
58 * to entry e, attribute attr, value val. if val is null, access to
59 * the whole attribute is assumed (all values).
61 * This routine loops through all access controls and calls
62 * acl_mask() on each applicable access control.
63 * The loop exits when a definitive answer is reached or
64 * or no more controls remain.
77 AttributeDescription *desc,
79 slap_access_t access )
84 char accessmaskbuf[ACCESSMASK_MAXLEN];
86 slap_access_mask_t mask;
87 slap_control_t control;
89 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
91 regmatch_t matches[MAXREMATCHES];
93 Debug( LDAP_DEBUG_ACL,
94 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
100 assert( attr != NULL );
101 assert( access > ACL_NONE );
103 /* grant database root access */
104 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
105 Debug( LDAP_DEBUG_ACL,
106 "<= root access granted\n",
112 * no-user-modification operational attributes are ignored
113 * by ACL_WRITE checking as any found here are not provided
116 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
118 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
119 " %s access granted\n",
124 /* use backend default access if no backend acls */
125 if( be != NULL && be->be_acl == NULL ) {
126 Debug( LDAP_DEBUG_ACL,
127 "=> access_allowed: backend default %s access %s to \"%s\"\n",
128 access2str( access ),
129 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
131 return be->be_dfltaccess >= access;
134 /* be is always non-NULL */
135 /* use global default access if no global acls */
136 } else if ( be == NULL && global_acl == NULL ) {
137 Debug( LDAP_DEBUG_ACL,
138 "=> access_allowed: global default %s access %s to \"%s\"\n",
139 access2str( access ),
140 global_default_access >= access ? "granted" : "denied", op->o_dn );
142 return global_default_access >= access;
147 memset(matches, '\0', sizeof(matches));
153 while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
157 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
158 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
159 (int)matches[i].rm_so, (int)matches[i].rm_eo );
161 if( matches[i].rm_so <= matches[0].rm_eo ) {
163 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
164 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
167 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
170 control = acl_mask( a, &mask, be, conn, op,
171 e, desc, val, matches );
173 if ( control != ACL_BREAK ) {
177 memset(matches, '\0', sizeof(matches));
180 if ( ACL_IS_INVALID( mask ) ) {
181 Debug( LDAP_DEBUG_ACL,
182 "=> access_allowed: \"%s\" (%s) invalid!\n",
186 } else if ( control == ACL_BREAK ) {
187 Debug( LDAP_DEBUG_ACL,
188 "=> access_allowed: no more rules\n", 0, 0, 0);
192 Debug( LDAP_DEBUG_ACL,
193 "=> access_allowed: %s access %s by %s\n",
194 access2str( access ),
195 ACL_GRANT(mask, access) ? "granted" : "denied",
196 ""/*accessmask2str( mask, accessmaskbuf )*/ );
198 return ACL_GRANT(mask, access);
202 * acl_get - return the acl applicable to entry e, attribute
203 * attr. the acl returned is suitable for use in subsequent calls to
204 * acl_access_allowed().
207 static AccessControl *
214 AttributeDescription *desc,
216 regmatch_t *matches )
222 assert( count != NULL );
224 attr = desc ? desc->ad_cname->bv_val : NULL;
239 dnlen = strlen(e->e_ndn);
241 for ( ; a != NULL; a = a->acl_next ) {
244 if (a->acl_dn_pat != NULL) {
245 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
246 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
247 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
249 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
253 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
254 *count, a->acl_dn_pat, 0 );
256 patlen = strlen( a->acl_dn_pat );
257 if ( dnlen < patlen )
260 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
261 /* base dn -- entire object DN must match */
262 if ( dnlen != patlen )
265 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
269 if ( dnlen <= patlen )
272 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
275 rdn = dn_rdn( NULL, e->e_ndn );
277 rdnlen = strlen( rdn );
280 if ( rdnlen != dnlen - patlen - 1 )
283 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
284 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
287 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
288 if ( dnlen <= patlen )
290 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
294 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
298 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
302 if ( a->acl_filter != NULL ) {
303 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
304 if ( rc != LDAP_COMPARE_TRUE ) {
309 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
312 if ( attr == NULL || a->acl_attrs == NULL ||
313 ad_inlist( desc, a->acl_attrs ) )
315 Debug( LDAP_DEBUG_ACL,
316 "<= acl_get: [%d] acl %s attr: %s\n",
317 *count, e->e_dn, attr );
320 matches[0].rm_so = matches[0].rm_eo = -1;
323 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
329 * acl_mask - modifies mask based upon the given acl and the
330 * requested access to entry e, attribute attr, value val. if val
331 * is null, access to the whole attribute is assumed (all values).
333 * returns 0 access NOT allowed
337 static slap_control_t
340 slap_access_mask_t *mask,
345 AttributeDescription *desc,
350 int i, odnlen, patlen;
353 char accessmaskbuf[ACCESSMASK_MAXLEN];
355 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
358 assert( mask != NULL );
360 Debug( LDAP_DEBUG_ACL,
361 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
364 Debug( LDAP_DEBUG_ACL,
365 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
366 val ? val->bv_val : "*",
367 op->o_ndn ? op->o_ndn : "",
368 ""/*accessmask2str( *mask, accessmaskbuf )*/ );
370 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
371 slap_access_mask_t oldmask, modmask;
373 ACL_INVALIDATE( modmask );
375 /* AND <who> clauses */
376 if ( b->a_dn_pat != NULL ) {
377 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
380 * if access applies to the entry itself, and the
381 * user is bound as somebody in the same namespace as
382 * the entry, OR the given dn matches the dn pattern
384 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
385 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
389 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
390 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
394 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
395 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
399 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
403 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
404 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
405 int ret = regex_matches( b->a_dn_pat,
406 op->o_ndn, e->e_ndn, matches );
414 if ( e->e_dn == NULL )
417 patlen = strlen( b->a_dn_pat );
418 odnlen = strlen( op->o_ndn );
419 if ( odnlen < patlen )
422 if ( b->a_dn_style == ACL_STYLE_BASE ) {
423 /* base dn -- entire object DN must match */
424 if ( odnlen != patlen )
427 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
431 if ( odnlen <= patlen )
434 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
437 rdn = dn_rdn( NULL, op->o_ndn );
439 rdnlen = strlen( rdn );
442 if ( rdnlen != odnlen - patlen - 1 )
445 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
446 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
449 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
450 if ( odnlen <= patlen )
452 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
456 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
462 if ( b->a_sockurl_pat != NULL ) {
463 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
464 b->a_sockurl_pat, 0, 0 );
466 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
467 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
468 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
469 e->e_ndn, matches ) )
474 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
480 if ( b->a_domain_pat != NULL ) {
481 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
482 b->a_domain_pat, 0, 0 );
484 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
485 if ( b->a_domain_style == ACL_STYLE_REGEX) {
486 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
487 e->e_ndn, matches ) )
492 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
498 if ( b->a_peername_pat != NULL ) {
499 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
500 b->a_peername_pat, 0, 0 );
502 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
503 if ( b->a_peername_style == ACL_STYLE_REGEX) {
504 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
505 e->e_ndn, matches ) )
510 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
516 if ( b->a_sockname_pat != NULL ) {
517 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
518 b->a_sockname_pat, 0, 0 );
520 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
521 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
522 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
523 e->e_ndn, matches ) )
528 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
534 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
539 const char *desc = b->a_dn_at->ad_cname->bv_val;
541 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
544 bv.bv_val = op->o_ndn;
545 bv.bv_len = strlen( bv.bv_val );
547 /* see if asker is listed in dnattr */
548 for( at = attrs_find( e->e_attrs, b->a_dn_at );
550 at = attrs_find( at->a_next, b->a_dn_at ) )
552 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
560 if ( b->a_dn_self && (val == NULL
561 || value_match( &match, b->a_dn_at,
562 b->a_dn_at->ad_type->sat_equality, val, &bv, &text ) )
568 } else if ( ! b->a_dn_self || val == NULL
569 || value_match( &match, b->a_dn_at,
570 b->a_dn_at->ad_type->sat_equality, val, &bv, &text )
578 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
581 /* b->a_group is an unexpanded entry name, expanded it should be an
582 * entry with objectclass group* and we test to see if odn is one of
583 * the values in the attribute group
585 /* see if asker is listed in dnattr */
586 if ( b->a_group_style != ACL_STYLE_REGEX ) {
587 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
588 if ( dn_normalize(buf) == NULL ) {
589 /* did not expand to a valid dn */
593 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
594 buf[sizeof(buf) - 1] = 0;
597 if (backend_group(be, e, buf, op->o_ndn,
598 b->a_group_oc, b->a_group_at) != 0)
604 #ifdef SLAPD_ACI_ENABLED
605 if ( b->a_aci_at != NULL ) {
607 slap_access_t grant, deny, tgrant, tdeny;
609 /* this case works different from the others above.
610 * since aci's themselves give permissions, we need
611 * to first check b->a_mask, the ACL's access level.
614 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
618 if ( e->e_dn == NULL ) {
622 /* first check if the right being requested
623 * is allowed by the ACL clause.
625 if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
629 /* get the aci attribute */
630 at = attr_find( e->e_attrs, b->a_aci_at );
635 /* start out with nothing granted, nothing denied */
639 /* the aci is an multi-valued attribute. The
640 * rights are determined by OR'ing the individual
641 * rights given by the acis.
643 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
644 if (aci_mask( be, conn, op,
645 e, desc, val, at->a_vals[i],
646 matches, &grant, &deny ) != 0)
653 /* remove anything that the ACL clause does not allow */
654 tgrant &= b->a_mask & ACL_PRIV_MASK;
655 tdeny &= ACL_PRIV_MASK;
657 /* see if we have anything to contribute */
658 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
662 /* this could be improved by changing acl_mask so that it can deal with
663 * by clauses that return grant/deny pairs. Right now, it does either
664 * additive or subtractive rights, but not both at the same time. So,
665 * we need to combine the grant/deny pair into a single rights mask in
666 * a smart way: if either grant or deny is "empty", then we use the
667 * opposite as is, otherwise we remove any denied rights from the grant
668 * rights mask and construct an additive mask.
670 if (ACL_IS_INVALID(tdeny)) {
671 modmask = tgrant | ACL_PRIV_ADDITIVE;
673 } else if (ACL_IS_INVALID(tgrant)) {
674 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
677 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
687 Debug( LDAP_DEBUG_ACL,
688 "<= acl_mask: [%d] applying %s (%s)\n",
689 i, ""/*accessmask2str( modmask, accessmaskbuf )*/,
690 b->a_type == ACL_CONTINUE
692 : b->a_type == ACL_BREAK
699 if( ACL_IS_ADDITIVE(modmask) ) {
701 ACL_PRIV_SET( *mask, modmask );
704 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
706 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
707 /* substract privs */
708 ACL_PRIV_CLR( *mask, modmask );
711 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
718 Debug( LDAP_DEBUG_ACL,
719 "<= acl_mask: [%d] mask: %s\n",
720 i, ""/*accessmask2str(*mask, accessmaskbuf)*/, 0 );
722 if( b->a_type == ACL_CONTINUE ) {
725 } else if ( b->a_type == ACL_BREAK ) {
733 Debug( LDAP_DEBUG_ACL,
734 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
735 ""/*accessmask2str(*mask, accessmaskbuf)*/, 0, 0 );
740 * acl_check_modlist - check access control on the given entry to see if
741 * it allows the given modifications by the user associated with op.
742 * returns 1 if mods allowed ok
757 assert( be != NULL );
759 /* short circuit root database access */
760 if ( be_isroot( be, op->o_ndn ) ) {
761 Debug( LDAP_DEBUG_ACL,
762 "<= acl_access_allowed: granted to database root\n",
767 /* use backend default access if no backend acls */
768 if( be != NULL && be->be_acl == NULL ) {
769 Debug( LDAP_DEBUG_ACL,
770 "=> access_allowed: backend default %s access %s to \"%s\"\n",
771 access2str( ACL_WRITE ),
772 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
774 return be->be_dfltaccess >= ACL_WRITE;
777 /* be is always non-NULL */
778 /* use global default access if no global acls */
779 } else if ( be == NULL && global_acl == NULL ) {
780 Debug( LDAP_DEBUG_ACL,
781 "=> access_allowed: global default %s access %s to \"%s\"\n",
782 access2str( ACL_WRITE ),
783 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
785 return global_default_access >= ACL_WRITE;
789 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
791 * no-user-modification operational attributes are ignored
792 * by ACL_WRITE checking as any found here are not provided
795 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
796 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
797 " modify access granted\n",
798 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
802 switch ( mlist->sml_op ) {
803 case LDAP_MOD_REPLACE:
805 if ( mlist->sml_bvalues == NULL ) {
808 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
809 if ( ! access_allowed( be, conn, op, e,
810 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
817 case LDAP_MOD_DELETE:
818 if ( mlist->sml_bvalues == NULL ) {
819 if ( ! access_allowed( be, conn, op, e,
820 mlist->sml_desc, NULL, ACL_WRITE ) )
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 ) )
840 #ifdef SLAPD_ACI_ENABLED
842 aci_bvstrdup( struct berval *bv )
846 s = (char *)ch_malloc(bv->bv_len + 1);
848 memcpy(s, bv->bv_val, bv->bv_len);
861 res = strncasecmp( s, bv->bv_val, bv->bv_len );
865 if (len > (int)bv->bv_len)
867 if (len < (int)bv->bv_len)
888 while (len >= 0 && --ix >= 0) {
889 while (--len >= 0 && *p++ != sep) ;
891 while (len >= 0 && *p == ' ') {
902 while (--len >= 0 && *p != sep) {
906 while (bv->bv_len > 0 && *--p == ' ')
913 struct berval *list )
920 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
923 switch (*bv.bv_val) {
925 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
928 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
929 * the right 's' to mean "set", but in the examples states
930 * that the right 's' means "search". The latter definition
933 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
936 ACL_PRIV_SET(mask, ACL_PRIV_READ);
939 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
942 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
943 * define any equivalent to the AUTH right, so I've just used
946 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
962 struct berval bv, left, right;
965 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
966 if (aci_get_part(&bv, 0, '=', &left) < 0
967 || aci_get_part(&bv, 1, '=', &right) < 0)
969 if (aci_strbvcmp(attr, &bv) == 0)
971 } else if (val == NULL) {
972 if (aci_strbvcmp(attr, &left) == 0)
975 if (aci_strbvcmp(attr, &left) == 0) {
976 /* this is experimental code that implements a
977 * simple (prefix) match of the attribute value.
978 * the ACI draft does not provide for aci's that
979 * apply to specific values, but it would be
980 * nice to have. If the <attr> part of an aci's
981 * rights list is of the form <attr>=<value>,
982 * that means the aci applies only to attrs with
983 * the given value. Furthermore, if the attr is
984 * of the form <attr>=<value>*, then <value> is
985 * treated as a prefix, and the aci applies to
986 * any value with that prefix.
988 * Ideally, this would allow r.e. matches.
990 if (aci_get_part(&right, 0, '*', &left) < 0
991 || right.bv_len <= left.bv_len)
993 if (aci_strbvcmp(val->bv_val, &right) == 0)
995 } else if (val->bv_len >= left.bv_len) {
996 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1005 static slap_access_t
1006 aci_list_get_attr_rights(
1007 struct berval *list,
1009 struct berval *val )
1015 /* loop through each rights/attr pair, skip first part (action) */
1017 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1018 if (aci_list_has_attr(&bv, attr, val) == 0)
1020 if (aci_get_part(list, i, ';', &bv) < 0)
1022 mask |= aci_list_map_rights(&bv);
1028 aci_list_get_rights(
1029 struct berval *list,
1032 slap_access_t *grant,
1033 slap_access_t *deny )
1035 struct berval perm, actn;
1036 slap_access_t *mask;
1039 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1046 /* loop through each permissions clause */
1047 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1048 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1050 if (aci_strbvcmp( "grant", &actn ) == 0) {
1052 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1059 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1060 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1067 struct berval *subj,
1068 const char *defgrpoc,
1069 const char *defgrpat,
1077 char *subjdn, *grpdn = NULL;
1080 ObjectClass *grp_oc = NULL;
1081 AttributeDescription *grp_ad = NULL;
1085 /* format of string is "group/objectClassValue/groupAttrName" */
1086 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1090 subjdn = aci_bvstrdup(&bv);
1091 if (subjdn == NULL) {
1095 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1096 grpoc = ch_strdup( defgrpoc );
1098 grpoc = aci_bvstrdup(&bv);
1101 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1102 grpat = ch_strdup( defgrpat );
1104 grpat = aci_bvstrdup(&bv);
1107 rc = slap_str2ad( grpat, &grp_ad, &text );
1108 if( rc != LDAP_SUCCESS ) {
1114 grp_oc = oc_find( grpoc );
1115 grpdn = (char *)ch_malloc(1024);
1117 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1118 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1119 if ( dn_normalize(grpdn) != NULL ) {
1120 rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1125 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1134 aci_set_gather (void *cookie, char *name, char *attr)
1141 } *cp = (void *)cookie;
1150 strcpy(hack.tag, "DKSDoc_Template");
1154 if ((ndn = ch_strdup(name)) != NULL) {
1155 if (dn_normalize(ndn) != NULL) {
1158 AttributeDescription *desc = NULL;
1159 if (slap_str2ad(attr, &desc, &text) == 0) {
1160 memset(&oc, 0, sizeof(oc));
1161 oc.soc_oid = (char *)&hack;
1162 backend_group(cp->be, cp->e, ndn, cp->op->o_ndn, &oc, desc);
1172 aci_match_template (
1173 struct berval *subj,
1189 if ((template = aci_bvstrdup(subj))) {
1194 rc = (set_filter(aci_set_gather, &cookie, template, op->o_ndn, e->e_ndn, NULL) > 0);
1206 AttributeDescription *desc,
1209 regmatch_t *matches,
1210 slap_access_t *grant,
1214 struct berval bv, perms, sdn;
1217 char *attr = desc->ad_cname->bv_val;
1219 /* parse an aci of the form:
1220 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1222 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1223 a full description of the format for this attribute.
1225 For now, this routine only supports scope=entry.
1228 /* check that the aci has all 5 components */
1229 if (aci_get_part(aci, 4, '#', NULL) < 0)
1232 /* check that the aci family is supported */
1233 if (aci_get_part(aci, 0, '#', &bv) < 0)
1236 /* check that the scope is "entry" */
1237 if (aci_get_part(aci, 1, '#', &bv) < 0
1238 || aci_strbvcmp( "entry", &bv ) != 0)
1243 /* get the list of permissions clauses, bail if empty */
1244 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1247 /* check if any permissions allow desired access */
1248 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1251 /* see if we have a DN match */
1252 if (aci_get_part(aci, 3, '#', &bv) < 0)
1255 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1258 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1259 subjdn = aci_bvstrdup(&sdn);
1263 if ( dn_normalize(subjdn) != NULL )
1264 if (strcasecmp(op->o_ndn, subjdn) != 0)
1270 if (aci_strbvcmp( "self", &bv ) == 0) {
1271 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1274 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1275 char *dnattr = aci_bvstrdup(&sdn);
1277 AttributeDescription *ad = NULL;
1280 rc = slap_str2ad( dnattr, &ad, &text );
1283 if( rc != LDAP_SUCCESS ) {
1289 bv.bv_val = op->o_ndn;
1290 bv.bv_len = strlen( bv.bv_val );
1292 for(at = attrs_find( e->e_attrs, ad );
1294 at = attrs_find( at->a_next, ad ) )
1296 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1306 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1307 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1310 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1311 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1314 } else if (aci_strbvcmp( "template", &bv ) == 0) {
1315 if (aci_match_template(&sdn, be, e, conn, op))
1323 #endif /* SLAPD_ACI_ENABLED */
1331 regmatch_t *matches)
1340 bufsiz--; /* leave space for lone $ */
1343 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1344 /* did we previously see a $ */
1349 } else if (*sp >= '0' && *sp <= '9' ) {
1356 i = matches[n].rm_so;
1357 l = matches[n].rm_eo;
1358 for ( ; size < 512 && i < l; size++, i++ ) {
1376 /* must have ended with a single $ */
1383 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1384 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1389 char *pat, /* pattern to expand and match against */
1390 char *str, /* string to match against pattern */
1391 char *buf, /* buffer with $N expansion variables */
1392 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1399 if(str == NULL) str = "";
1401 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1402 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1404 regerror(rc, &re, error, sizeof(error));
1406 Debug( LDAP_DEBUG_TRACE,
1407 "compile( \"%s\", \"%s\") failed %s\n",
1412 rc = regexec(&re, str, 0, NULL, 0);
1415 Debug( LDAP_DEBUG_TRACE,
1416 "=> regex_matches: string: %s\n", str, 0, 0 );
1417 Debug( LDAP_DEBUG_TRACE,
1418 "=> regex_matches: rc: %d %s\n",
1419 rc, !rc ? "matches" : "no matches", 0 );