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 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
93 regmatch_t matches[MAXREMATCHES];
96 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
97 "access_allowed: conn %d %s access to \"%s\" \"%s\" requested\n",
98 conn->c_connid, access2str( access ), e->e_dn, attr ));
100 Debug( LDAP_DEBUG_ACL,
101 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
102 access2str( access ),
106 assert( be != NULL );
108 assert( attr != NULL );
109 assert( access > ACL_NONE );
111 /* grant database root access */
112 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
114 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
115 "access_allowed: conn %d root access granted\n",
118 Debug( LDAP_DEBUG_ACL,
119 "<= root access granted\n",
126 * no-user-modification operational attributes are ignored
127 * by ACL_WRITE checking as any found here are not provided
130 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
131 && desc != slap_schema.si_ad_entry
132 && desc != slap_schema.si_ad_children )
135 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
136 "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
137 conn->c_connid, attr ));
139 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
140 " %s access granted\n",
146 /* use backend default access if no backend acls */
147 if( be != NULL && be->be_acl == NULL ) {
149 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
150 "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
151 conn->c_connid, access2str( access ),
152 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn ));
154 Debug( LDAP_DEBUG_ACL,
155 "=> access_allowed: backend default %s access %s to \"%s\"\n",
156 access2str( access ),
157 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
159 return be->be_dfltaccess >= access;
162 /* be is always non-NULL */
163 /* use global default access if no global acls */
164 } else if ( be == NULL && global_acl == NULL ) {
166 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
167 "access_allowed: conn %d global default %s access %s to \"%s\"\n",
168 conn->c_connid, access2str( access ),
169 global_default_access >= access ? "granted" : "denied", op->o_dn ));
171 Debug( LDAP_DEBUG_ACL,
172 "=> access_allowed: global default %s access %s to \"%s\"\n",
173 access2str( access ),
174 global_default_access >= access ? "granted" : "denied", op->o_dn );
176 return global_default_access >= access;
181 memset(matches, '\0', sizeof(matches));
187 while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
191 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
193 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
194 "access_allowed: conn %d match[%d]: %d %d ",
195 conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
197 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
198 (int)matches[i].rm_so, (int)matches[i].rm_eo );
200 if( matches[i].rm_so <= matches[0].rm_eo ) {
202 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
203 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
207 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
209 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
213 control = acl_mask( a, &mask, be, conn, op,
214 e, desc, val, matches );
216 if ( control != ACL_BREAK ) {
220 memset(matches, '\0', sizeof(matches));
223 if ( ACL_IS_INVALID( mask ) ) {
225 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
226 "access_allowed: conn %d \"%s\" (%s) invalid!\n",
227 conn->c_connid, e->e_dn, attr ));
229 Debug( LDAP_DEBUG_ACL,
230 "=> access_allowed: \"%s\" (%s) invalid!\n",
235 } else if ( control == ACL_BREAK ) {
237 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
238 "access_allowed: conn %d no more rules\n", conn->c_connid ));
240 Debug( LDAP_DEBUG_ACL,
241 "=> access_allowed: no more rules\n", 0, 0, 0);
247 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
248 "access_allowed: conn %d %s access %s by %s\n",
250 access2str( access ),
251 ACL_GRANT( mask, access ) ? "granted" : "denied",
252 accessmask2str( mask, accessmaskbuf ) ));
254 Debug( LDAP_DEBUG_ACL,
255 "=> access_allowed: %s access %s by %s\n",
256 access2str( access ),
257 ACL_GRANT(mask, access) ? "granted" : "denied",
258 accessmask2str( mask, accessmaskbuf ) );
260 return ACL_GRANT(mask, access);
264 * acl_get - return the acl applicable to entry e, attribute
265 * attr. the acl returned is suitable for use in subsequent calls to
266 * acl_access_allowed().
269 static AccessControl *
276 AttributeDescription *desc,
278 regmatch_t *matches )
284 assert( count != NULL );
286 attr = desc ? desc->ad_cname->bv_val : NULL;
301 dnlen = strlen(e->e_ndn);
303 for ( ; a != NULL; a = a->acl_next ) {
306 if (a->acl_dn_pat != NULL) {
307 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
309 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
310 "acl_get: dnpat [%d] %s nsub: %d\n",
311 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub ));
313 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
314 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
316 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
321 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
322 "acl_get: dn [%d] %s\n",
323 *count, a->acl_dn_pat ));
325 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
326 *count, a->acl_dn_pat, 0 );
328 patlen = strlen( a->acl_dn_pat );
329 if ( dnlen < patlen )
332 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
333 /* base dn -- entire object DN must match */
334 if ( dnlen != patlen )
337 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
341 if ( dnlen <= patlen )
344 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
347 rdn = dn_rdn( NULL, e->e_ndn );
349 rdnlen = strlen( rdn );
352 if ( rdnlen != dnlen - patlen - 1 )
355 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
356 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
359 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
360 if ( dnlen <= patlen )
362 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
366 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
371 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
372 "acl_get: [%d] matched\n",
375 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
380 if ( a->acl_filter != NULL ) {
381 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
382 if ( rc != LDAP_COMPARE_TRUE ) {
388 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
389 "acl_get: [%d] check attr %s\n",
392 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
395 if ( attr == NULL || a->acl_attrs == NULL ||
396 ad_inlist( desc, a->acl_attrs ) )
399 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
400 "acl_get: [%d] acl %s attr: %s\n",
401 *count, e->e_dn, attr ));
403 Debug( LDAP_DEBUG_ACL,
404 "<= acl_get: [%d] acl %s attr: %s\n",
405 *count, e->e_dn, attr );
409 matches[0].rm_so = matches[0].rm_eo = -1;
413 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
414 "acl_get: done.\n" ));
416 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
423 * acl_mask - modifies mask based upon the given acl and the
424 * requested access to entry e, attribute attr, value val. if val
425 * is null, access to the whole attribute is assumed (all values).
427 * returns 0 access NOT allowed
431 static slap_control_t
439 AttributeDescription *desc,
444 int i, odnlen, patlen;
447 char accessmaskbuf[ACCESSMASK_MAXLEN];
449 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
452 assert( mask != NULL );
455 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
456 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
457 conn->c_connid, e->e_dn, attr ));
459 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
460 " to %s by \"%s\", (%s) \n",
461 val ? "value" : "all values",
462 op->o_ndn ? op->o_ndn : "",
463 accessmask2str( *mask, accessmaskbuf ) ));
465 Debug( LDAP_DEBUG_ACL,
466 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
469 Debug( LDAP_DEBUG_ACL,
470 "=> acl_mask: to %s by \"%s\", (%s) \n",
471 val ? "value" : "all values",
472 op->o_ndn ? op->o_ndn : "",
473 accessmask2str( *mask, accessmaskbuf ) );
476 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
477 slap_mask_t oldmask, modmask;
479 ACL_INVALIDATE( modmask );
481 /* AND <who> clauses */
482 if ( b->a_dn_pat != NULL ) {
484 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
485 "acl_mask: conn %d check a_dn_pat: %s\n",
486 conn->c_connid, b->a_dn_pat ));
488 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
492 * if access applies to the entry itself, and the
493 * user is bound as somebody in the same namespace as
494 * the entry, OR the given dn matches the dn pattern
496 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
497 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
501 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
502 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
506 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
507 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
511 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
515 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
516 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
517 int ret = regex_matches( b->a_dn_pat,
518 op->o_ndn, e->e_ndn, matches );
526 if ( e->e_dn == NULL )
529 patlen = strlen( b->a_dn_pat );
530 odnlen = strlen( op->o_ndn );
531 if ( odnlen < patlen )
534 if ( b->a_dn_style == ACL_STYLE_BASE ) {
535 /* base dn -- entire object DN must match */
536 if ( odnlen != patlen )
539 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
543 if ( odnlen <= patlen )
546 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
549 rdn = dn_rdn( NULL, op->o_ndn );
551 rdnlen = strlen( rdn );
554 if ( rdnlen != odnlen - patlen - 1 )
557 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
558 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
561 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
562 if ( odnlen <= patlen )
564 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
568 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
574 if ( b->a_sockurl_pat != NULL ) {
576 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
577 "acl_mask: conn %d check a_sockurl_pat: %s\n",
578 conn->c_connid, b->a_sockurl_pat ));
580 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
581 b->a_sockurl_pat, 0, 0 );
584 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
585 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
586 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
587 e->e_ndn, matches ) )
592 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
598 if ( b->a_domain_pat != NULL ) {
600 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
601 "acl_mask: conn %d check a_domain_pat: %s\n",
602 conn->c_connid, b->a_domain_pat ));
604 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
605 b->a_domain_pat, 0, 0 );
607 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
608 if ( b->a_domain_style == ACL_STYLE_REGEX) {
609 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
610 e->e_ndn, matches ) )
615 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
621 if ( b->a_peername_pat != NULL ) {
623 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
624 "acl_mask: conn %d check a_perrname_path: %s\n",
625 conn->c_connid, b->a_peername_pat ));
627 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
628 b->a_peername_pat, 0, 0 );
630 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
631 if ( b->a_peername_style == ACL_STYLE_REGEX) {
632 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
633 e->e_ndn, matches ) )
638 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
644 if ( b->a_sockname_pat != NULL ) {
646 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
647 "acl_mask: conn %d check a_sockname_path: %s\n",
648 conn->c_connid, b->a_sockname_pat ));
650 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
651 b->a_sockname_pat, 0, 0 );
653 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
654 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
655 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
656 e->e_ndn, matches ) )
661 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
667 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
672 const char *desc = b->a_dn_at->ad_cname->bv_val;
675 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
676 "acl_mask: conn %d check a_dn_pat: %s\n",
677 conn->c_connid, desc ));
679 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
682 bv.bv_val = op->o_ndn;
683 bv.bv_len = strlen( bv.bv_val );
685 /* see if asker is listed in dnattr */
686 for( at = attrs_find( e->e_attrs, b->a_dn_at );
688 at = attrs_find( at->a_next, b->a_dn_at ) )
690 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
698 /* have a dnattr match. if this is a self clause then
699 * the target must also match the op dn.
701 if ( b->a_dn_self ) {
702 /* check if the target is an attribute. */
705 /* target is attribute, check if the attribute value
708 rc = value_match( &match, b->a_dn_at,
709 b->a_dn_at->ad_type->sat_equality, 0,
711 /* on match error or no match, fail the ACL clause */
712 if (rc != LDAP_SUCCESS || match != 0 )
716 /* no dnattr match, check if this is a self clause */
717 if ( ! b->a_dn_self )
719 /* this is a self clause, check if the target is an
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,
731 /* on match error or no match, fail the ACL clause */
732 if (rc != LDAP_SUCCESS || match != 0 )
737 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
740 /* b->a_group is an unexpanded entry name, expanded it should be an
741 * entry with objectclass group* and we test to see if odn is one of
742 * the values in the attribute group
744 /* see if asker is listed in dnattr */
745 if ( b->a_group_style == ACL_STYLE_REGEX ) {
746 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
747 if ( dn_normalize(buf) == NULL ) {
748 /* did not expand to a valid dn */
752 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
753 buf[sizeof(buf) - 1] = 0;
756 if (backend_group(be, conn, op, e, buf, op->o_ndn,
757 b->a_group_oc, b->a_group_at) != 0)
763 if ( b->a_set_pat != NULL ) {
766 bv.bv_val = b->a_set_pat;
767 bv.bv_len = strlen(b->a_set_pat);
768 if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
773 if ( b->a_authz.sai_ssf ) {
775 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
776 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
777 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
779 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
780 b->a_authz.sai_ssf, op->o_ssf, 0 );
782 if ( b->a_authz.sai_ssf > op->o_ssf ) {
787 if ( b->a_authz.sai_transport_ssf ) {
789 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
790 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
791 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
793 Debug( LDAP_DEBUG_ACL,
794 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
795 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
797 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
802 if ( b->a_authz.sai_tls_ssf ) {
804 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
805 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
806 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
808 Debug( LDAP_DEBUG_ACL,
809 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
810 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
812 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
817 if ( b->a_authz.sai_sasl_ssf ) {
819 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
820 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
821 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
823 Debug( LDAP_DEBUG_ACL,
824 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
825 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
827 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
832 #ifdef SLAPD_ACI_ENABLED
833 if ( b->a_aci_at != NULL ) {
835 slap_access_t grant, deny, tgrant, tdeny;
837 /* this case works different from the others above.
838 * since aci's themselves give permissions, we need
839 * to first check b->a_access_mask, the ACL's access level.
842 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
846 if ( e->e_dn == NULL ) {
850 /* first check if the right being requested
851 * is allowed by the ACL clause.
853 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
857 /* get the aci attribute */
858 at = attr_find( e->e_attrs, b->a_aci_at );
863 /* start out with nothing granted, nothing denied */
867 /* the aci is an multi-valued attribute. The
868 * rights are determined by OR'ing the individual
869 * rights given by the acis.
871 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
872 if (aci_mask( be, conn, op,
873 e, desc, val, at->a_vals[i],
874 matches, &grant, &deny ) != 0)
881 /* remove anything that the ACL clause does not allow */
882 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
883 tdeny &= ACL_PRIV_MASK;
885 /* see if we have anything to contribute */
886 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
890 /* this could be improved by changing acl_mask so that it can deal with
891 * by clauses that return grant/deny pairs. Right now, it does either
892 * additive or subtractive rights, but not both at the same time. So,
893 * we need to combine the grant/deny pair into a single rights mask in
894 * a smart way: if either grant or deny is "empty", then we use the
895 * opposite as is, otherwise we remove any denied rights from the grant
896 * rights mask and construct an additive mask.
898 if (ACL_IS_INVALID(tdeny)) {
899 modmask = tgrant | ACL_PRIV_ADDITIVE;
901 } else if (ACL_IS_INVALID(tgrant)) {
902 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
905 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
911 modmask = b->a_access_mask;
915 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
916 "acl_mask: conn %d [%d] applying %s (%s)\n",
917 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
918 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
919 ? "break" : "stop" ));
921 Debug( LDAP_DEBUG_ACL,
922 "<= acl_mask: [%d] applying %s (%s)\n",
923 i, accessmask2str( modmask, accessmaskbuf ),
924 b->a_type == ACL_CONTINUE
926 : b->a_type == ACL_BREAK
933 if( ACL_IS_ADDITIVE(modmask) ) {
935 ACL_PRIV_SET( *mask, modmask );
938 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
940 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
941 /* substract privs */
942 ACL_PRIV_CLR( *mask, modmask );
945 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
953 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
954 "acl_mask: conn %d [%d] mask: %s\n",
955 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
957 Debug( LDAP_DEBUG_ACL,
958 "<= acl_mask: [%d] mask: %s\n",
959 i, accessmask2str(*mask, accessmaskbuf), 0 );
962 if( b->a_type == ACL_CONTINUE ) {
965 } else if ( b->a_type == ACL_BREAK ) {
974 LDAP_LOG(( "aci", LDAP_LEVEL_RESULTS,
975 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
976 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
978 Debug( LDAP_DEBUG_ACL,
979 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
980 accessmask2str(*mask, accessmaskbuf), 0, 0 );
986 * acl_check_modlist - check access control on the given entry to see if
987 * it allows the given modifications by the user associated with op.
988 * returns 1 if mods allowed ok
1003 assert( be != NULL );
1005 /* short circuit root database access */
1006 if ( be_isroot( be, op->o_ndn ) ) {
1008 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1009 "acl_check_modlist: conn %d access granted to root user\n",
1012 Debug( LDAP_DEBUG_ACL,
1013 "<= acl_access_allowed: granted to database root\n",
1019 /* use backend default access if no backend acls */
1020 if( be != NULL && be->be_acl == NULL ) {
1022 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1023 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1024 conn->c_connid, access2str( ACL_WRITE ),
1025 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1027 Debug( LDAP_DEBUG_ACL,
1028 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1029 access2str( ACL_WRITE ),
1030 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1032 return be->be_dfltaccess >= ACL_WRITE;
1035 /* be is always non-NULL */
1036 /* use global default access if no global acls */
1037 } else if ( be == NULL && global_acl == NULL ) {
1039 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1040 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1041 conn->c_connid, access2str( ACL_WRITE ),
1042 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1044 Debug( LDAP_DEBUG_ACL,
1045 "=> access_allowed: global default %s access %s to \"%s\"\n",
1046 access2str( ACL_WRITE ),
1047 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1049 return global_default_access >= ACL_WRITE;
1053 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1055 * no-user-modification operational attributes are ignored
1056 * by ACL_WRITE checking as any found here are not provided
1059 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1061 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1062 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1063 conn->c_connid, mlist->sml_desc->ad_cname->bv_val ));
1065 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1066 " modify access granted\n",
1067 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
1072 switch ( mlist->sml_op ) {
1073 case LDAP_MOD_REPLACE:
1075 if ( mlist->sml_bvalues == NULL ) {
1078 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1079 if ( ! access_allowed( be, conn, op, e,
1080 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1087 case LDAP_MOD_DELETE:
1088 if ( mlist->sml_bvalues == NULL ) {
1089 if ( ! access_allowed( be, conn, op, e,
1090 mlist->sml_desc, NULL, ACL_WRITE ) )
1096 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1097 if ( ! access_allowed( be, conn, op, e,
1098 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1111 aci_bvstrdup( struct berval *bv )
1115 s = (char *)ch_malloc(bv->bv_len + 1);
1117 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1130 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1134 if (len > (int)bv->bv_len)
1136 if (len < (int)bv->bv_len)
1143 struct berval *list,
1157 while (len >= 0 && --ix >= 0) {
1158 while (--len >= 0 && *p++ != sep) ;
1160 while (len >= 0 && *p == ' ') {
1171 while (--len >= 0 && *p != sep) {
1175 while (bv->bv_len > 0 && *--p == ' ')
1181 aci_set_gather (void *cookie, char *name, char *attr)
1188 } *cp = (void *)cookie;
1189 struct berval **bvals = NULL;
1194 /* this routine needs to return the bervals instead of
1195 * plain strings, since syntax is not known. It should
1196 * also return the syntax or some "comparison cookie".
1199 if ((ndn = ch_strdup(name)) != NULL) {
1200 if (dn_normalize(ndn) != NULL) {
1202 AttributeDescription *desc = NULL;
1203 if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1204 backend_attribute(cp->be, NULL /*cp->conn*/,
1205 NULL /*cp->op*/, cp->e,
1207 if (bvals != NULL) {
1208 for (i = 0; bvals[i] != NULL; i++) { }
1209 vals = ch_calloc(i + 1, sizeof(char *));
1212 vals[i] = bvals[i]->bv_val;
1213 bvals[i]->bv_val = NULL;
1216 ber_bvecfree(bvals);
1228 struct berval *subj,
1246 set = aci_bvstrdup(subj);
1251 struct berval **bvals;
1253 AttributeDescription *desc = NULL;
1255 /* format of string is "entry/setAttrName" */
1256 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1260 subjdn = aci_bvstrdup(&bv);
1261 if ( subjdn == NULL ) {
1265 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1266 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1268 setat = aci_bvstrdup(&bv);
1270 if ( setat != NULL ) {
1271 if ( dn_normalize(subjdn) != NULL
1272 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1274 backend_attribute(be, NULL, NULL, e,
1275 subjdn, desc, &bvals);
1277 if ( bvals != NULL ) {
1278 if ( bvals[0] != NULL )
1279 set = ch_strdup(bvals[0]->bv_val);
1280 ber_bvecfree(bvals);
1293 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1299 #ifdef SLAPD_ACI_ENABLED
1301 aci_list_map_rights(
1302 struct berval *list )
1309 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1312 switch (*bv.bv_val) {
1314 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1317 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1318 * the right 's' to mean "set", but in the examples states
1319 * that the right 's' means "search". The latter definition
1322 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1325 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1328 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1331 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1332 * define any equivalent to the AUTH right, so I've just used
1335 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1347 struct berval *list,
1349 struct berval *val )
1351 struct berval bv, left, right;
1354 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1355 if (aci_get_part(&bv, 0, '=', &left) < 0
1356 || aci_get_part(&bv, 1, '=', &right) < 0)
1358 if (aci_strbvcmp(attr, &bv) == 0)
1360 } else if (val == NULL) {
1361 if (aci_strbvcmp(attr, &left) == 0)
1364 if (aci_strbvcmp(attr, &left) == 0) {
1365 /* this is experimental code that implements a
1366 * simple (prefix) match of the attribute value.
1367 * the ACI draft does not provide for aci's that
1368 * apply to specific values, but it would be
1369 * nice to have. If the <attr> part of an aci's
1370 * rights list is of the form <attr>=<value>,
1371 * that means the aci applies only to attrs with
1372 * the given value. Furthermore, if the attr is
1373 * of the form <attr>=<value>*, then <value> is
1374 * treated as a prefix, and the aci applies to
1375 * any value with that prefix.
1377 * Ideally, this would allow r.e. matches.
1379 if (aci_get_part(&right, 0, '*', &left) < 0
1380 || right.bv_len <= left.bv_len)
1382 if (aci_strbvcmp(val->bv_val, &right) == 0)
1384 } else if (val->bv_len >= left.bv_len) {
1385 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1394 static slap_access_t
1395 aci_list_get_attr_rights(
1396 struct berval *list,
1398 struct berval *val )
1404 /* loop through each rights/attr pair, skip first part (action) */
1406 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1407 if (aci_list_has_attr(&bv, attr, val) == 0)
1409 if (aci_get_part(list, i, ';', &bv) < 0)
1411 mask |= aci_list_map_rights(&bv);
1417 aci_list_get_rights(
1418 struct berval *list,
1421 slap_access_t *grant,
1422 slap_access_t *deny )
1424 struct berval perm, actn;
1425 slap_access_t *mask;
1428 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1435 /* loop through each permissions clause */
1436 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1437 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1439 if (aci_strbvcmp( "grant", &actn ) == 0) {
1441 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1448 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1449 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1456 struct berval *subj,
1457 const char *defgrpoc,
1458 const char *defgrpat,
1467 char *subjdn, *grpdn = NULL;
1470 ObjectClass *grp_oc = NULL;
1471 AttributeDescription *grp_ad = NULL;
1475 /* format of string is "group/objectClassValue/groupAttrName" */
1476 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1480 subjdn = aci_bvstrdup(&bv);
1481 if (subjdn == NULL) {
1485 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1486 grpoc = ch_strdup( defgrpoc );
1488 grpoc = aci_bvstrdup(&bv);
1491 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1492 grpat = ch_strdup( defgrpat );
1494 grpat = aci_bvstrdup(&bv);
1497 rc = slap_str2ad( grpat, &grp_ad, &text );
1498 if( rc != LDAP_SUCCESS ) {
1504 grp_oc = oc_find( grpoc );
1505 grpdn = (char *)ch_malloc(1024);
1507 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1508 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1509 if ( dn_normalize(grpdn) != NULL ) {
1510 rc = (backend_group(be, conn, op, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1515 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1529 AttributeDescription *desc,
1532 regmatch_t *matches,
1533 slap_access_t *grant,
1537 struct berval bv, perms, sdn;
1540 char *attr = desc->ad_cname->bv_val;
1542 /* parse an aci of the form:
1543 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1545 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1546 a full description of the format for this attribute.
1548 For now, this routine only supports scope=entry.
1551 /* check that the aci has all 5 components */
1552 if (aci_get_part(aci, 4, '#', NULL) < 0)
1555 /* check that the aci family is supported */
1556 if (aci_get_part(aci, 0, '#', &bv) < 0)
1559 /* check that the scope is "entry" */
1560 if (aci_get_part(aci, 1, '#', &bv) < 0
1561 || aci_strbvcmp( "entry", &bv ) != 0)
1566 /* get the list of permissions clauses, bail if empty */
1567 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1570 /* check if any permissions allow desired access */
1571 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1574 /* see if we have a DN match */
1575 if (aci_get_part(aci, 3, '#', &bv) < 0)
1578 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1581 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1582 subjdn = aci_bvstrdup(&sdn);
1586 if ( dn_normalize(subjdn) != NULL )
1587 if (strcasecmp(op->o_ndn, subjdn) != 0)
1593 if (aci_strbvcmp( "self", &bv ) == 0) {
1594 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1597 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1598 char *dnattr = aci_bvstrdup(&sdn);
1600 AttributeDescription *ad = NULL;
1603 rc = slap_str2ad( dnattr, &ad, &text );
1606 if( rc != LDAP_SUCCESS ) {
1612 bv.bv_val = op->o_ndn;
1613 bv.bv_len = strlen( bv.bv_val );
1615 for(at = attrs_find( e->e_attrs, ad );
1617 at = attrs_find( at->a_next, ad ) )
1619 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1629 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1630 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, conn, op, matches))
1633 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1634 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, conn, op, matches))
1637 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1638 if (aci_match_set(&sdn, be, e, conn, op, 0))
1641 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1642 if (aci_match_set(&sdn, be, e, conn, op, 1))
1650 #endif /* SLAPD_ACI_ENABLED */
1658 regmatch_t *matches)
1667 bufsiz--; /* leave space for lone $ */
1670 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1671 /* did we previously see a $ */
1676 } else if (*sp >= '0' && *sp <= '9' ) {
1683 i = matches[n].rm_so;
1684 l = matches[n].rm_eo;
1685 for ( ; size < 512 && i < l; size++, i++ ) {
1703 /* must have ended with a single $ */
1711 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1712 "string_expand: pattern = %s\n", pat ));
1713 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1714 "string_expand: expanded = %s\n", newbuf ));
1716 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1717 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1723 char *pat, /* pattern to expand and match against */
1724 char *str, /* string to match against pattern */
1725 char *buf, /* buffer with $N expansion variables */
1726 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1733 if(str == NULL) str = "";
1735 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1736 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1738 regerror(rc, &re, error, sizeof(error));
1741 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1742 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1745 Debug( LDAP_DEBUG_TRACE,
1746 "compile( \"%s\", \"%s\") failed %s\n",
1752 rc = regexec(&re, str, 0, NULL, 0);
1756 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1757 "regex_matches: string: %s\n", str ));
1758 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1759 "regex_matches: rc: %d %s\n",
1760 rc, rc ? "matches" : "no matches" ));
1762 Debug( LDAP_DEBUG_TRACE,
1763 "=> regex_matches: string: %s\n", str, 0, 0 );
1764 Debug( LDAP_DEBUG_TRACE,
1765 "=> regex_matches: rc: %d %s\n",
1766 rc, !rc ? "matches" : "no matches", 0 );