1 /* acl.c - routines to parse and check acl's */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
19 static AccessControl * acl_get(
20 AccessControl *ac, int *count,
21 Backend *be, Operation *op,
23 AttributeDescription *desc,
24 int nmatches, regmatch_t *matches );
26 static slap_control_t acl_mask(
27 AccessControl *ac, slap_mask_t *mask,
28 Backend *be, Connection *conn, Operation *op,
30 AttributeDescription *desc,
32 regmatch_t *matches );
34 #ifdef SLAPD_ACI_ENABLED
40 AttributeDescription *desc,
45 slap_access_t *deny );
48 static int regex_matches(
49 char *pat, char *str, char *buf, regmatch_t *matches);
50 static void string_expand(
51 char *newbuf, int bufsiz, char *pattern,
52 char *match, regmatch_t *matches);
54 char **aci_set_gather (void *cookie, char *name, char *attr);
55 static int aci_match_set ( struct berval *subj, Backend *be,
56 Entry *e, Connection *conn, Operation *op, int setref );
59 * access_allowed - check whether op->o_ndn is allowed the requested access
60 * to entry e, attribute attr, value val. if val is null, access to
61 * the whole attribute is assumed (all values).
63 * This routine loops through all access controls and calls
64 * acl_mask() on each applicable access control.
65 * The loop exits when a definitive answer is reached or
66 * or no more controls remain.
79 AttributeDescription *desc,
81 slap_access_t access )
86 char accessmaskbuf[ACCESSMASK_MAXLEN];
89 slap_control_t control;
91 regmatch_t matches[MAXREMATCHES];
94 assert( desc != NULL );
95 assert( access > ACL_NONE );
97 attr = desc->ad_cname->bv_val;
99 assert( attr != NULL );
102 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
103 "access_allowed: conn %d %s access to \"%s\" \"%s\" requested\n",
104 conn->c_connid, access2str( access ), e->e_dn, attr ));
106 Debug( LDAP_DEBUG_ACL,
107 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
108 access2str( access ),
117 if ( be == NULL ) be = &backends[0];
118 assert( be != NULL );
120 /* grant database root access */
121 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
123 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
124 "access_allowed: conn %d root access granted\n",
127 Debug( LDAP_DEBUG_ACL,
128 "<= root access granted\n",
135 * no-user-modification operational attributes are ignored
136 * by ACL_WRITE checking as any found here are not provided
139 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
140 && desc != slap_schema.si_ad_entry
141 && desc != slap_schema.si_ad_children )
144 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
145 "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
146 conn->c_connid, attr ));
148 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
149 " %s access granted\n",
155 /* use backend default access if no backend acls */
156 if( be != NULL && be->be_acl == NULL ) {
158 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
159 "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
160 conn->c_connid, access2str( access ),
161 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn ));
163 Debug( LDAP_DEBUG_ACL,
164 "=> access_allowed: backend default %s access %s to \"%s\"\n",
165 access2str( access ),
166 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
168 return be->be_dfltaccess >= access;
171 /* be is always non-NULL */
172 /* use global default access if no global acls */
173 } else if ( be == NULL && global_acl == NULL ) {
175 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
176 "access_allowed: conn %d global default %s access %s to \"%s\"\n",
177 conn->c_connid, access2str( access ),
178 global_default_access >= access ? "granted" : "denied", op->o_dn ));
180 Debug( LDAP_DEBUG_ACL,
181 "=> access_allowed: global default %s access %s to \"%s\"\n",
182 access2str( access ),
183 global_default_access >= access ? "granted" : "denied", op->o_dn );
185 return global_default_access >= access;
190 memset(matches, '\0', sizeof(matches));
196 while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
200 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
202 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
203 "access_allowed: conn %d match[%d]: %d %d ",
204 conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
206 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
207 (int)matches[i].rm_so, (int)matches[i].rm_eo );
209 if( matches[i].rm_so <= matches[0].rm_eo ) {
211 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
212 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
216 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
218 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
222 control = acl_mask( a, &mask, be, conn, op,
223 e, desc, val, matches );
225 if ( control != ACL_BREAK ) {
229 memset(matches, '\0', sizeof(matches));
232 if ( ACL_IS_INVALID( mask ) ) {
234 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
235 "access_allowed: conn %d \"%s\" (%s) invalid!\n",
236 conn->c_connid, e->e_dn, attr ));
238 Debug( LDAP_DEBUG_ACL,
239 "=> access_allowed: \"%s\" (%s) invalid!\n",
244 } else if ( control == ACL_BREAK ) {
246 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
247 "access_allowed: conn %d no more rules\n", conn->c_connid ));
249 Debug( LDAP_DEBUG_ACL,
250 "=> access_allowed: no more rules\n", 0, 0, 0);
256 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
257 "access_allowed: conn %d %s access %s by %s\n",
259 access2str( access ),
260 ACL_GRANT( mask, access ) ? "granted" : "denied",
261 accessmask2str( mask, accessmaskbuf ) ));
263 Debug( LDAP_DEBUG_ACL,
264 "=> access_allowed: %s access %s by %s\n",
265 access2str( access ),
266 ACL_GRANT(mask, access) ? "granted" : "denied",
267 accessmask2str( mask, accessmaskbuf ) );
269 return ACL_GRANT(mask, access);
273 * acl_get - return the acl applicable to entry e, attribute
274 * attr. the acl returned is suitable for use in subsequent calls to
275 * acl_access_allowed().
278 static AccessControl *
285 AttributeDescription *desc,
287 regmatch_t *matches )
293 assert( count != NULL );
294 assert( desc != NULL );
296 attr = desc->ad_cname->bv_val;
298 assert( attr != NULL );
313 dnlen = strlen(e->e_ndn);
315 for ( ; a != NULL; a = a->acl_next ) {
318 if (a->acl_dn_pat != NULL) {
319 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
321 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
322 "acl_get: dnpat [%d] %s nsub: %d\n",
323 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub ));
325 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
326 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
328 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
333 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
334 "acl_get: dn [%d] %s\n",
335 *count, a->acl_dn_pat ));
337 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
338 *count, a->acl_dn_pat, 0 );
340 patlen = strlen( a->acl_dn_pat );
341 if ( dnlen < patlen )
344 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
345 /* base dn -- entire object DN must match */
346 if ( dnlen != patlen )
349 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
353 if ( dnlen <= patlen )
356 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
359 rdn = dn_rdn( NULL, e->e_ndn );
361 rdnlen = strlen( rdn );
364 if ( rdnlen != dnlen - patlen - 1 )
367 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
368 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
371 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
372 if ( dnlen <= patlen )
374 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
378 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
383 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
384 "acl_get: [%d] matched\n",
387 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
392 if ( a->acl_filter != NULL ) {
393 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
394 if ( rc != LDAP_COMPARE_TRUE ) {
400 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
401 "acl_get: [%d] check attr %s\n",
404 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
407 if ( attr == NULL || a->acl_attrs == NULL ||
408 ad_inlist( desc, a->acl_attrs ) )
411 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
412 "acl_get: [%d] acl %s attr: %s\n",
413 *count, e->e_dn, attr ));
415 Debug( LDAP_DEBUG_ACL,
416 "<= acl_get: [%d] acl %s attr: %s\n",
417 *count, e->e_dn, attr );
421 matches[0].rm_so = matches[0].rm_eo = -1;
425 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
426 "acl_get: done.\n" ));
428 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
435 * acl_mask - modifies mask based upon the given acl and the
436 * requested access to entry e, attribute attr, value val. if val
437 * is null, access to the whole attribute is assumed (all values).
439 * returns 0 access NOT allowed
443 static slap_control_t
451 AttributeDescription *desc,
456 int i, odnlen, patlen;
459 char accessmaskbuf[ACCESSMASK_MAXLEN];
464 assert( mask != NULL );
465 assert( desc != NULL );
467 attr = desc->ad_cname->bv_val;
469 assert( attr != NULL );
472 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
473 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
474 conn->c_connid, e->e_dn, attr ));
476 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
477 " to %s by \"%s\", (%s) \n",
478 val ? "value" : "all values",
479 op->o_ndn ? op->o_ndn : "",
480 accessmask2str( *mask, accessmaskbuf ) ));
482 Debug( LDAP_DEBUG_ACL,
483 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
486 Debug( LDAP_DEBUG_ACL,
487 "=> acl_mask: to %s by \"%s\", (%s) \n",
488 val ? "value" : "all values",
489 op->o_ndn ? op->o_ndn : "",
490 accessmask2str( *mask, accessmaskbuf ) );
493 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
494 slap_mask_t oldmask, modmask;
496 ACL_INVALIDATE( modmask );
498 /* AND <who> clauses */
499 if ( b->a_dn_pat != NULL ) {
501 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
502 "acl_mask: conn %d check a_dn_pat: %s\n",
503 conn->c_connid, b->a_dn_pat ));
505 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
509 * if access applies to the entry itself, and the
510 * user is bound as somebody in the same namespace as
511 * the entry, OR the given dn matches the dn pattern
513 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
514 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
518 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
519 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
523 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
524 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
528 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
532 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
533 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
534 int ret = regex_matches( b->a_dn_pat,
535 op->o_ndn, e->e_ndn, matches );
543 if ( e->e_dn == NULL )
546 patlen = strlen( b->a_dn_pat );
547 odnlen = strlen( op->o_ndn );
548 if ( odnlen < patlen )
551 if ( b->a_dn_style == ACL_STYLE_BASE ) {
552 /* base dn -- entire object DN must match */
553 if ( odnlen != patlen )
556 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
560 if ( odnlen <= patlen )
563 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
566 rdn = dn_rdn( NULL, op->o_ndn );
568 rdnlen = strlen( rdn );
571 if ( rdnlen != odnlen - patlen - 1 )
574 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
575 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
578 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
579 if ( odnlen <= patlen )
581 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
585 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
591 if ( b->a_sockurl_pat != NULL ) {
593 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
594 "acl_mask: conn %d check a_sockurl_pat: %s\n",
595 conn->c_connid, b->a_sockurl_pat ));
597 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
598 b->a_sockurl_pat, 0, 0 );
601 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
602 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
603 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
604 e->e_ndn, matches ) )
609 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
615 if ( b->a_domain_pat != NULL ) {
617 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
618 "acl_mask: conn %d check a_domain_pat: %s\n",
619 conn->c_connid, b->a_domain_pat ));
621 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
622 b->a_domain_pat, 0, 0 );
624 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
625 if ( b->a_domain_style == ACL_STYLE_REGEX) {
626 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
627 e->e_ndn, matches ) )
632 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
638 if ( b->a_peername_pat != NULL ) {
640 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
641 "acl_mask: conn %d check a_perrname_path: %s\n",
642 conn->c_connid, b->a_peername_pat ));
644 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
645 b->a_peername_pat, 0, 0 );
647 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
648 if ( b->a_peername_style == ACL_STYLE_REGEX) {
649 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
650 e->e_ndn, matches ) )
655 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
661 if ( b->a_sockname_pat != NULL ) {
663 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
664 "acl_mask: conn %d check a_sockname_path: %s\n",
665 conn->c_connid, b->a_sockname_pat ));
667 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
668 b->a_sockname_pat, 0, 0 );
670 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
671 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
672 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
673 e->e_ndn, matches ) )
678 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
684 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
689 const char *attr = b->a_dn_at->ad_cname->bv_val;
691 assert( attr != NULL );
694 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
695 "acl_mask: conn %d check a_dn_pat: %s\n",
696 conn->c_connid, attr ));
698 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
701 bv.bv_val = op->o_ndn;
702 bv.bv_len = strlen( bv.bv_val );
704 /* see if asker is listed in dnattr */
705 for( at = attrs_find( e->e_attrs, b->a_dn_at );
707 at = attrs_find( at->a_next, b->a_dn_at ) )
709 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
717 /* have a dnattr match. if this is a self clause then
718 * the target must also match the op dn.
720 if ( b->a_dn_self ) {
721 /* check if the target is an attribute. */
724 /* target is attribute, check if the attribute value
727 rc = value_match( &match, b->a_dn_at,
728 b->a_dn_at->ad_type->sat_equality, 0,
730 /* on match error or no match, fail the ACL clause */
731 if (rc != LDAP_SUCCESS || match != 0 )
735 /* no dnattr match, check if this is a self clause */
736 if ( ! b->a_dn_self )
738 /* this is a self clause, check if the target is an
743 /* target is attribute, check if the attribute value
746 rc = value_match( &match, b->a_dn_at,
747 b->a_dn_at->ad_type->sat_equality, 0,
750 /* on match error or no match, fail the ACL clause */
751 if (rc != LDAP_SUCCESS || match != 0 )
756 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
759 /* b->a_group is an unexpanded entry name, expanded it should be an
760 * entry with objectclass group* and we test to see if odn is one of
761 * the values in the attribute group
763 /* see if asker is listed in dnattr */
764 if ( b->a_group_style == ACL_STYLE_REGEX ) {
765 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
766 if ( dn_normalize(buf) == NULL ) {
767 /* did not expand to a valid dn */
771 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
772 buf[sizeof(buf) - 1] = 0;
775 if (backend_group(be, conn, op, e, buf, op->o_ndn,
776 b->a_group_oc, b->a_group_at) != 0)
782 if ( b->a_set_pat != NULL ) {
785 bv.bv_val = b->a_set_pat;
786 bv.bv_len = strlen(b->a_set_pat);
787 if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
792 if ( b->a_authz.sai_ssf ) {
794 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
795 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
796 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
798 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
799 b->a_authz.sai_ssf, op->o_ssf, 0 );
801 if ( b->a_authz.sai_ssf > op->o_ssf ) {
806 if ( b->a_authz.sai_transport_ssf ) {
808 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
809 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
810 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
812 Debug( LDAP_DEBUG_ACL,
813 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
814 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
816 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
821 if ( b->a_authz.sai_tls_ssf ) {
823 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
824 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
825 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
827 Debug( LDAP_DEBUG_ACL,
828 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
829 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
831 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
836 if ( b->a_authz.sai_sasl_ssf ) {
838 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
839 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
840 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
842 Debug( LDAP_DEBUG_ACL,
843 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
844 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
846 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
851 #ifdef SLAPD_ACI_ENABLED
852 if ( b->a_aci_at != NULL ) {
854 slap_access_t grant, deny, tgrant, tdeny;
856 /* this case works different from the others above.
857 * since aci's themselves give permissions, we need
858 * to first check b->a_access_mask, the ACL's access level.
861 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
865 if ( e->e_dn == NULL ) {
869 /* first check if the right being requested
870 * is allowed by the ACL clause.
872 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
876 /* get the aci attribute */
877 at = attr_find( e->e_attrs, b->a_aci_at );
882 /* start out with nothing granted, nothing denied */
886 /* the aci is an multi-valued attribute. The
887 * rights are determined by OR'ing the individual
888 * rights given by the acis.
890 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
891 if (aci_mask( be, conn, op,
892 e, desc, val, at->a_vals[i],
893 matches, &grant, &deny ) != 0)
900 /* remove anything that the ACL clause does not allow */
901 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
902 tdeny &= ACL_PRIV_MASK;
904 /* see if we have anything to contribute */
905 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
909 /* this could be improved by changing acl_mask so that it can deal with
910 * by clauses that return grant/deny pairs. Right now, it does either
911 * additive or subtractive rights, but not both at the same time. So,
912 * we need to combine the grant/deny pair into a single rights mask in
913 * a smart way: if either grant or deny is "empty", then we use the
914 * opposite as is, otherwise we remove any denied rights from the grant
915 * rights mask and construct an additive mask.
917 if (ACL_IS_INVALID(tdeny)) {
918 modmask = tgrant | ACL_PRIV_ADDITIVE;
920 } else if (ACL_IS_INVALID(tgrant)) {
921 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
924 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
930 modmask = b->a_access_mask;
934 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
935 "acl_mask: conn %d [%d] applying %s (%s)\n",
936 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
937 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
938 ? "break" : "stop" ));
940 Debug( LDAP_DEBUG_ACL,
941 "<= acl_mask: [%d] applying %s (%s)\n",
942 i, accessmask2str( modmask, accessmaskbuf ),
943 b->a_type == ACL_CONTINUE
945 : b->a_type == ACL_BREAK
952 if( ACL_IS_ADDITIVE(modmask) ) {
954 ACL_PRIV_SET( *mask, modmask );
957 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
959 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
960 /* substract privs */
961 ACL_PRIV_CLR( *mask, modmask );
964 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
972 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
973 "acl_mask: conn %d [%d] mask: %s\n",
974 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
976 Debug( LDAP_DEBUG_ACL,
977 "<= acl_mask: [%d] mask: %s\n",
978 i, accessmask2str(*mask, accessmaskbuf), 0 );
981 if( b->a_type == ACL_CONTINUE ) {
984 } else if ( b->a_type == ACL_BREAK ) {
992 /* implicit "by * none" clause */
996 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
997 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
998 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1000 Debug( LDAP_DEBUG_ACL,
1001 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1002 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1008 * acl_check_modlist - check access control on the given entry to see if
1009 * it allows the given modifications by the user associated with op.
1010 * returns 1 if mods allowed ok
1011 * 0 mods not allowed
1020 Modifications *mlist
1025 assert( be != NULL );
1027 /* short circuit root database access */
1028 if ( be_isroot( be, op->o_ndn ) ) {
1030 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1031 "acl_check_modlist: conn %d access granted to root user\n",
1034 Debug( LDAP_DEBUG_ACL,
1035 "<= acl_access_allowed: granted to database root\n",
1041 /* use backend default access if no backend acls */
1042 if( be != NULL && be->be_acl == NULL ) {
1044 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1045 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1046 conn->c_connid, access2str( ACL_WRITE ),
1047 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1049 Debug( LDAP_DEBUG_ACL,
1050 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1051 access2str( ACL_WRITE ),
1052 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1054 return be->be_dfltaccess >= ACL_WRITE;
1057 /* be is always non-NULL */
1058 /* use global default access if no global acls */
1059 } else if ( be == NULL && global_acl == NULL ) {
1061 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1062 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1063 conn->c_connid, access2str( ACL_WRITE ),
1064 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1066 Debug( LDAP_DEBUG_ACL,
1067 "=> access_allowed: global default %s access %s to \"%s\"\n",
1068 access2str( ACL_WRITE ),
1069 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1071 return global_default_access >= ACL_WRITE;
1075 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1077 * no-user-modification operational attributes are ignored
1078 * by ACL_WRITE checking as any found here are not provided
1081 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1083 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1084 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1085 conn->c_connid, mlist->sml_desc->ad_cname->bv_val ));
1087 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1088 " modify access granted\n",
1089 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
1094 switch ( mlist->sml_op ) {
1095 case LDAP_MOD_REPLACE:
1097 if ( mlist->sml_bvalues == NULL ) {
1100 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1101 if ( ! access_allowed( be, conn, op, e,
1102 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1109 case LDAP_MOD_DELETE:
1110 if ( mlist->sml_bvalues == NULL ) {
1111 if ( ! access_allowed( be, conn, op, e,
1112 mlist->sml_desc, NULL, ACL_WRITE ) )
1118 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1119 if ( ! access_allowed( be, conn, op, e,
1120 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1133 aci_bvstrdup( struct berval *bv )
1137 s = (char *)ch_malloc(bv->bv_len + 1);
1139 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1152 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1156 if (len > (int)bv->bv_len)
1158 if (len < (int)bv->bv_len)
1165 struct berval *list,
1179 while (len >= 0 && --ix >= 0) {
1180 while (--len >= 0 && *p++ != sep) ;
1182 while (len >= 0 && *p == ' ') {
1193 while (--len >= 0 && *p != sep) {
1197 while (bv->bv_len > 0 && *--p == ' ')
1203 aci_set_gather (void *cookie, char *name, char *attr)
1210 } *cp = (void *)cookie;
1211 struct berval **bvals = NULL;
1216 /* this routine needs to return the bervals instead of
1217 * plain strings, since syntax is not known. It should
1218 * also return the syntax or some "comparison cookie".
1221 if ((ndn = ch_strdup(name)) != NULL) {
1222 if (dn_normalize(ndn) != NULL) {
1224 AttributeDescription *desc = NULL;
1225 if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1226 backend_attribute(cp->be, NULL /*cp->conn*/,
1227 NULL /*cp->op*/, cp->e,
1229 if (bvals != NULL) {
1230 for (i = 0; bvals[i] != NULL; i++) { }
1231 vals = ch_calloc(i + 1, sizeof(char *));
1234 vals[i] = bvals[i]->bv_val;
1235 bvals[i]->bv_val = NULL;
1238 ber_bvecfree(bvals);
1250 struct berval *subj,
1268 set = aci_bvstrdup(subj);
1273 struct berval **bvals;
1275 AttributeDescription *desc = NULL;
1277 /* format of string is "entry/setAttrName" */
1278 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1282 subjdn = aci_bvstrdup(&bv);
1283 if ( subjdn == NULL ) {
1287 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1288 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1290 setat = aci_bvstrdup(&bv);
1292 if ( setat != NULL ) {
1293 if ( dn_normalize(subjdn) != NULL
1294 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1296 backend_attribute(be, NULL, NULL, e,
1297 subjdn, desc, &bvals);
1299 if ( bvals != NULL ) {
1300 if ( bvals[0] != NULL )
1301 set = ch_strdup(bvals[0]->bv_val);
1302 ber_bvecfree(bvals);
1315 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1321 #ifdef SLAPD_ACI_ENABLED
1323 aci_list_map_rights(
1324 struct berval *list )
1331 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1334 switch (*bv.bv_val) {
1336 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1339 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1340 * the right 's' to mean "set", but in the examples states
1341 * that the right 's' means "search". The latter definition
1344 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1347 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1350 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1353 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1354 * define any equivalent to the AUTH right, so I've just used
1357 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1369 struct berval *list,
1371 struct berval *val )
1373 struct berval bv, left, right;
1376 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1377 if (aci_get_part(&bv, 0, '=', &left) < 0
1378 || aci_get_part(&bv, 1, '=', &right) < 0)
1380 if (aci_strbvcmp(attr, &bv) == 0)
1382 } else if (val == NULL) {
1383 if (aci_strbvcmp(attr, &left) == 0)
1386 if (aci_strbvcmp(attr, &left) == 0) {
1387 /* this is experimental code that implements a
1388 * simple (prefix) match of the attribute value.
1389 * the ACI draft does not provide for aci's that
1390 * apply to specific values, but it would be
1391 * nice to have. If the <attr> part of an aci's
1392 * rights list is of the form <attr>=<value>,
1393 * that means the aci applies only to attrs with
1394 * the given value. Furthermore, if the attr is
1395 * of the form <attr>=<value>*, then <value> is
1396 * treated as a prefix, and the aci applies to
1397 * any value with that prefix.
1399 * Ideally, this would allow r.e. matches.
1401 if (aci_get_part(&right, 0, '*', &left) < 0
1402 || right.bv_len <= left.bv_len)
1404 if (aci_strbvcmp(val->bv_val, &right) == 0)
1406 } else if (val->bv_len >= left.bv_len) {
1407 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1416 static slap_access_t
1417 aci_list_get_attr_rights(
1418 struct berval *list,
1420 struct berval *val )
1426 /* loop through each rights/attr pair, skip first part (action) */
1428 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1429 if (aci_list_has_attr(&bv, attr, val) == 0)
1431 if (aci_get_part(list, i, ';', &bv) < 0)
1433 mask |= aci_list_map_rights(&bv);
1439 aci_list_get_rights(
1440 struct berval *list,
1443 slap_access_t *grant,
1444 slap_access_t *deny )
1446 struct berval perm, actn;
1447 slap_access_t *mask;
1450 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1457 /* loop through each permissions clause */
1458 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1459 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1461 if (aci_strbvcmp( "grant", &actn ) == 0) {
1463 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1470 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1471 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1478 struct berval *subj,
1479 const char *defgrpoc,
1480 const char *defgrpat,
1489 char *subjdn, *grpdn = NULL;
1492 ObjectClass *grp_oc = NULL;
1493 AttributeDescription *grp_ad = NULL;
1497 /* format of string is "group/objectClassValue/groupAttrName" */
1498 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1502 subjdn = aci_bvstrdup(&bv);
1503 if (subjdn == NULL) {
1507 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1508 grpoc = ch_strdup( defgrpoc );
1510 grpoc = aci_bvstrdup(&bv);
1513 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1514 grpat = ch_strdup( defgrpat );
1516 grpat = aci_bvstrdup(&bv);
1519 rc = slap_str2ad( grpat, &grp_ad, &text );
1520 if( rc != LDAP_SUCCESS ) {
1526 grp_oc = oc_find( grpoc );
1527 grpdn = (char *)ch_malloc(1024);
1529 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1530 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1531 if ( dn_normalize(grpdn) != NULL ) {
1532 rc = (backend_group(be, conn, op, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1537 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1551 AttributeDescription *desc,
1554 regmatch_t *matches,
1555 slap_access_t *grant,
1559 struct berval bv, perms, sdn;
1562 char *attr = desc->ad_cname->bv_val;
1564 assert( attr != NULL );
1566 /* parse an aci of the form:
1567 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1569 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1570 a full description of the format for this attribute.
1572 For now, this routine only supports scope=entry.
1575 /* check that the aci has all 5 components */
1576 if (aci_get_part(aci, 4, '#', NULL) < 0)
1579 /* check that the aci family is supported */
1580 if (aci_get_part(aci, 0, '#', &bv) < 0)
1583 /* check that the scope is "entry" */
1584 if (aci_get_part(aci, 1, '#', &bv) < 0
1585 || aci_strbvcmp( "entry", &bv ) != 0)
1590 /* get the list of permissions clauses, bail if empty */
1591 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1594 /* check if any permissions allow desired access */
1595 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1598 /* see if we have a DN match */
1599 if (aci_get_part(aci, 3, '#', &bv) < 0)
1602 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1605 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1606 subjdn = aci_bvstrdup(&sdn);
1610 if ( dn_normalize(subjdn) != NULL )
1611 if (strcasecmp(op->o_ndn, subjdn) != 0)
1617 if (aci_strbvcmp( "self", &bv ) == 0) {
1618 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1621 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1622 char *dnattr = aci_bvstrdup(&sdn);
1624 AttributeDescription *ad = NULL;
1627 rc = slap_str2ad( dnattr, &ad, &text );
1630 if( rc != LDAP_SUCCESS ) {
1636 bv.bv_val = op->o_ndn;
1637 bv.bv_len = strlen( bv.bv_val );
1639 for(at = attrs_find( e->e_attrs, ad );
1641 at = attrs_find( at->a_next, ad ) )
1643 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1653 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1654 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, conn, op, matches))
1657 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1658 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, conn, op, matches))
1661 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1662 if (aci_match_set(&sdn, be, e, conn, op, 0))
1665 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1666 if (aci_match_set(&sdn, be, e, conn, op, 1))
1674 #endif /* SLAPD_ACI_ENABLED */
1682 regmatch_t *matches)
1691 bufsiz--; /* leave space for lone $ */
1694 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1695 /* did we previously see a $ */
1700 } else if (*sp >= '0' && *sp <= '9' ) {
1707 i = matches[n].rm_so;
1708 l = matches[n].rm_eo;
1709 for ( ; size < 512 && i < l; size++, i++ ) {
1727 /* must have ended with a single $ */
1735 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1736 "string_expand: pattern = %s\n", pat ));
1737 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1738 "string_expand: expanded = %s\n", newbuf ));
1740 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1741 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1747 char *pat, /* pattern to expand and match against */
1748 char *str, /* string to match against pattern */
1749 char *buf, /* buffer with $N expansion variables */
1750 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1757 if(str == NULL) str = "";
1759 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1760 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1762 regerror(rc, &re, error, sizeof(error));
1765 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1766 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1769 Debug( LDAP_DEBUG_TRACE,
1770 "compile( \"%s\", \"%s\") failed %s\n",
1776 rc = regexec(&re, str, 0, NULL, 0);
1780 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1781 "regex_matches: string: %s\n", str ));
1782 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1783 "regex_matches: rc: %d %s\n",
1784 rc, rc ? "matches" : "no matches" ));
1786 Debug( LDAP_DEBUG_TRACE,
1787 "=> regex_matches: string: %s\n", str, 0, 0 );
1788 Debug( LDAP_DEBUG_TRACE,
1789 "=> regex_matches: rc: %d %s\n",
1790 rc, !rc ? "matches" : "no matches", 0 );