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",
249 access2str( access ),
250 ACL_GRANT( mask, access ) ? "granted" : "denied",
251 accessmask2str( mask, accessmaskbuf ) ));
253 Debug( LDAP_DEBUG_ACL,
254 "=> access_allowed: %s access %s by %s\n",
255 access2str( access ),
256 ACL_GRANT(mask, access) ? "granted" : "denied",
257 accessmask2str( mask, accessmaskbuf ) );
259 return ACL_GRANT(mask, access);
263 * acl_get - return the acl applicable to entry e, attribute
264 * attr. the acl returned is suitable for use in subsequent calls to
265 * acl_access_allowed().
268 static AccessControl *
275 AttributeDescription *desc,
277 regmatch_t *matches )
283 assert( count != NULL );
285 attr = desc ? desc->ad_cname->bv_val : NULL;
300 dnlen = strlen(e->e_ndn);
302 for ( ; a != NULL; a = a->acl_next ) {
305 if (a->acl_dn_pat != NULL) {
306 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
308 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
309 "acl_get: dnpat [%d] %s nsub: %d\n",
310 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub ));
312 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
313 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
315 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
320 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
321 "acl_get: dn [%d] %s\n",
322 *count, a->acl_dn_pat ));
324 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
325 *count, a->acl_dn_pat, 0 );
327 patlen = strlen( a->acl_dn_pat );
328 if ( dnlen < patlen )
331 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
332 /* base dn -- entire object DN must match */
333 if ( dnlen != patlen )
336 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
340 if ( dnlen <= patlen )
343 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
346 rdn = dn_rdn( NULL, e->e_ndn );
348 rdnlen = strlen( rdn );
351 if ( rdnlen != dnlen - patlen - 1 )
354 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
355 if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
358 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
359 if ( dnlen <= patlen )
361 if ( e->e_ndn[dnlen - patlen - 1] != ',' )
365 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
370 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
371 "acl_get: [%d] matched\n",
374 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
379 if ( a->acl_filter != NULL ) {
380 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
381 if ( rc != LDAP_COMPARE_TRUE ) {
387 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
388 "acl_get: [%d] check attr %s\n",
391 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
394 if ( attr == NULL || a->acl_attrs == NULL ||
395 ad_inlist( desc, a->acl_attrs ) )
398 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
399 "acl_get: [%d] acl %s attr: %s\n",
400 *count, e->e_dn, attr ));
402 Debug( LDAP_DEBUG_ACL,
403 "<= acl_get: [%d] acl %s attr: %s\n",
404 *count, e->e_dn, attr );
408 matches[0].rm_so = matches[0].rm_eo = -1;
412 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
413 "acl_get: done.\n" ));
415 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
422 * acl_mask - modifies mask based upon the given acl and the
423 * requested access to entry e, attribute attr, value val. if val
424 * is null, access to the whole attribute is assumed (all values).
426 * returns 0 access NOT allowed
430 static slap_control_t
438 AttributeDescription *desc,
443 int i, odnlen, patlen;
446 char accessmaskbuf[ACCESSMASK_MAXLEN];
448 const char *attr = desc ? desc->ad_cname->bv_val : NULL;
451 assert( mask != NULL );
454 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
455 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
456 conn->c_connid, e->e_dn, attr ));
458 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
459 " to %s by \"%s\", (%s) \n",
460 val ? "value" : "all values",
461 op->o_ndn ? op->o_ndn : "",
462 accessmask2str( *mask, accessmaskbuf ) ));
464 Debug( LDAP_DEBUG_ACL,
465 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
468 Debug( LDAP_DEBUG_ACL,
469 "=> acl_mask: to %s by \"%s\", (%s) \n",
470 val ? "value" : "all values",
471 op->o_ndn ? op->o_ndn : "",
472 accessmask2str( *mask, accessmaskbuf ) );
475 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
476 slap_mask_t oldmask, modmask;
478 ACL_INVALIDATE( modmask );
480 /* AND <who> clauses */
481 if ( b->a_dn_pat != NULL ) {
483 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
484 "acl_mask: conn %d check a_dn_pat: %s\n",
485 conn->c_connid, b->a_dn_pat ));
487 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
491 * if access applies to the entry itself, and the
492 * user is bound as somebody in the same namespace as
493 * the entry, OR the given dn matches the dn pattern
495 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
496 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
500 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
501 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
505 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
506 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
510 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
514 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
515 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
516 int ret = regex_matches( b->a_dn_pat,
517 op->o_ndn, e->e_ndn, matches );
525 if ( e->e_dn == NULL )
528 patlen = strlen( b->a_dn_pat );
529 odnlen = strlen( op->o_ndn );
530 if ( odnlen < patlen )
533 if ( b->a_dn_style == ACL_STYLE_BASE ) {
534 /* base dn -- entire object DN must match */
535 if ( odnlen != patlen )
538 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
542 if ( odnlen <= patlen )
545 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
548 rdn = dn_rdn( NULL, op->o_ndn );
550 rdnlen = strlen( rdn );
553 if ( rdnlen != odnlen - patlen - 1 )
556 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
557 if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
560 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
561 if ( odnlen <= patlen )
563 if ( op->o_ndn[odnlen - patlen - 1] != ',' )
567 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
573 if ( b->a_sockurl_pat != NULL ) {
575 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
576 "acl_mask: conn %d check a_sockurl_pat: %s\n",
577 conn->c_connid, b->a_sockurl_pat ));
579 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
580 b->a_sockurl_pat, 0, 0 );
583 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
584 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
585 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
586 e->e_ndn, matches ) )
591 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
597 if ( b->a_domain_pat != NULL ) {
599 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
600 "acl_mask: conn %d check a_domain_pat: %s\n",
601 conn->c_connid, b->a_domain_pat ));
603 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
604 b->a_domain_pat, 0, 0 );
606 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
607 if ( b->a_domain_style == ACL_STYLE_REGEX) {
608 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
609 e->e_ndn, matches ) )
614 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
620 if ( b->a_peername_pat != NULL ) {
622 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
623 "acl_mask: conn %d check a_perrname_path: %s\n",
624 conn->c_connid, b->a_peername_pat ));
626 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
627 b->a_peername_pat, 0, 0 );
629 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
630 if ( b->a_peername_style == ACL_STYLE_REGEX) {
631 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
632 e->e_ndn, matches ) )
637 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
643 if ( b->a_sockname_pat != NULL ) {
645 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
646 "acl_mask: conn %d check a_sockname_path: %s\n",
647 conn->c_connid, b->a_sockname_pat ));
649 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
650 b->a_sockname_pat, 0, 0 );
652 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
653 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
654 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
655 e->e_ndn, matches ) )
660 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
666 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
671 const char *desc = b->a_dn_at->ad_cname->bv_val;
674 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
675 "acl_mask: conn %d check a_dn_pat: %s\n",
676 conn->c_connid, desc ));
678 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
681 bv.bv_val = op->o_ndn;
682 bv.bv_len = strlen( bv.bv_val );
684 /* see if asker is listed in dnattr */
685 for( at = attrs_find( e->e_attrs, b->a_dn_at );
687 at = attrs_find( at->a_next, b->a_dn_at ) )
689 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
697 /* have a dnattr match. if this is a self clause then
698 * the target must also match the op dn.
700 if ( b->a_dn_self ) {
701 /* check if the target is an attribute. */
704 /* target is attribute, check if the attribute value
707 rc = value_match( &match, b->a_dn_at,
708 b->a_dn_at->ad_type->sat_equality, 0,
710 /* on match error or no match, fail the ACL clause */
711 if (rc != LDAP_SUCCESS || match != 0 )
715 /* no dnattr match, check if this is a self clause */
716 if ( ! b->a_dn_self )
718 /* this is a self clause, check if the target is an
723 /* target is attribute, check if the attribute value
726 rc = value_match( &match, b->a_dn_at,
727 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 )
736 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
739 /* b->a_group is an unexpanded entry name, expanded it should be an
740 * entry with objectclass group* and we test to see if odn is one of
741 * the values in the attribute group
743 /* see if asker is listed in dnattr */
744 if ( b->a_group_style == ACL_STYLE_REGEX ) {
745 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
746 if ( dn_normalize(buf) == NULL ) {
747 /* did not expand to a valid dn */
751 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
752 buf[sizeof(buf) - 1] = 0;
755 if (backend_group(be, conn, op, e, buf, op->o_ndn,
756 b->a_group_oc, b->a_group_at) != 0)
762 if ( b->a_set_pat != NULL ) {
765 bv.bv_val = b->a_set_pat;
766 bv.bv_len = strlen(b->a_set_pat);
767 if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
772 if ( b->a_authz.sai_ssf ) {
774 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
775 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
776 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
778 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
779 b->a_authz.sai_ssf, op->o_ssf, 0 );
781 if ( b->a_authz.sai_ssf > op->o_ssf ) {
786 if ( b->a_authz.sai_transport_ssf ) {
788 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
789 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
790 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
792 Debug( LDAP_DEBUG_ACL,
793 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
794 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
796 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
801 if ( b->a_authz.sai_tls_ssf ) {
803 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
804 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
805 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
807 Debug( LDAP_DEBUG_ACL,
808 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
809 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
811 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
816 if ( b->a_authz.sai_sasl_ssf ) {
818 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
819 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
820 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
822 Debug( LDAP_DEBUG_ACL,
823 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
824 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
826 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
831 #ifdef SLAPD_ACI_ENABLED
832 if ( b->a_aci_at != NULL ) {
834 slap_access_t grant, deny, tgrant, tdeny;
836 /* this case works different from the others above.
837 * since aci's themselves give permissions, we need
838 * to first check b->a_access_mask, the ACL's access level.
841 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
845 if ( e->e_dn == NULL ) {
849 /* first check if the right being requested
850 * is allowed by the ACL clause.
852 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
856 /* get the aci attribute */
857 at = attr_find( e->e_attrs, b->a_aci_at );
862 /* start out with nothing granted, nothing denied */
866 /* the aci is an multi-valued attribute. The
867 * rights are determined by OR'ing the individual
868 * rights given by the acis.
870 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
871 if (aci_mask( be, conn, op,
872 e, desc, val, at->a_vals[i],
873 matches, &grant, &deny ) != 0)
880 /* remove anything that the ACL clause does not allow */
881 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
882 tdeny &= ACL_PRIV_MASK;
884 /* see if we have anything to contribute */
885 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
889 /* this could be improved by changing acl_mask so that it can deal with
890 * by clauses that return grant/deny pairs. Right now, it does either
891 * additive or subtractive rights, but not both at the same time. So,
892 * we need to combine the grant/deny pair into a single rights mask in
893 * a smart way: if either grant or deny is "empty", then we use the
894 * opposite as is, otherwise we remove any denied rights from the grant
895 * rights mask and construct an additive mask.
897 if (ACL_IS_INVALID(tdeny)) {
898 modmask = tgrant | ACL_PRIV_ADDITIVE;
900 } else if (ACL_IS_INVALID(tgrant)) {
901 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
904 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
910 modmask = b->a_access_mask;
914 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
915 "acl_mask: conn %d [%d] applying %s (%s)\n",
916 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
917 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
918 ? "break" : "stop" ));
920 Debug( LDAP_DEBUG_ACL,
921 "<= acl_mask: [%d] applying %s (%s)\n",
922 i, accessmask2str( modmask, accessmaskbuf ),
923 b->a_type == ACL_CONTINUE
925 : b->a_type == ACL_BREAK
932 if( ACL_IS_ADDITIVE(modmask) ) {
934 ACL_PRIV_SET( *mask, modmask );
937 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
939 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
940 /* substract privs */
941 ACL_PRIV_CLR( *mask, modmask );
944 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
952 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
953 "acl_mask: conn %d [%d] mask: %s\n",
954 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
956 Debug( LDAP_DEBUG_ACL,
957 "<= acl_mask: [%d] mask: %s\n",
958 i, accessmask2str(*mask, accessmaskbuf), 0 );
961 if( b->a_type == ACL_CONTINUE ) {
964 } else if ( b->a_type == ACL_BREAK ) {
973 LDAP_LOG(( "aci", LDAP_LEVEL_RESULTS,
974 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
975 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
977 Debug( LDAP_DEBUG_ACL,
978 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
979 accessmask2str(*mask, accessmaskbuf), 0, 0 );
985 * acl_check_modlist - check access control on the given entry to see if
986 * it allows the given modifications by the user associated with op.
987 * returns 1 if mods allowed ok
1002 assert( be != NULL );
1004 /* short circuit root database access */
1005 if ( be_isroot( be, op->o_ndn ) ) {
1007 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1008 "acl_check_modlist: conn %d access granted to root user\n",
1011 Debug( LDAP_DEBUG_ACL,
1012 "<= acl_access_allowed: granted to database root\n",
1018 /* use backend default access if no backend acls */
1019 if( be != NULL && be->be_acl == NULL ) {
1021 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1022 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1023 conn->c_connid, access2str( ACL_WRITE ),
1024 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1026 Debug( LDAP_DEBUG_ACL,
1027 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1028 access2str( ACL_WRITE ),
1029 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1031 return be->be_dfltaccess >= ACL_WRITE;
1034 /* be is always non-NULL */
1035 /* use global default access if no global acls */
1036 } else if ( be == NULL && global_acl == NULL ) {
1038 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1039 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1040 conn->c_connid, access2str( ACL_WRITE ),
1041 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1043 Debug( LDAP_DEBUG_ACL,
1044 "=> access_allowed: global default %s access %s to \"%s\"\n",
1045 access2str( ACL_WRITE ),
1046 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1048 return global_default_access >= ACL_WRITE;
1052 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1054 * no-user-modification operational attributes are ignored
1055 * by ACL_WRITE checking as any found here are not provided
1058 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1060 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1061 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1062 conn->c_connid, mlist->sml_desc->ad_cname->bv_val ));
1064 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1065 " modify access granted\n",
1066 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
1071 switch ( mlist->sml_op ) {
1072 case LDAP_MOD_REPLACE:
1074 if ( mlist->sml_bvalues == NULL ) {
1077 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1078 if ( ! access_allowed( be, conn, op, e,
1079 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1086 case LDAP_MOD_DELETE:
1087 if ( mlist->sml_bvalues == NULL ) {
1088 if ( ! access_allowed( be, conn, op, e,
1089 mlist->sml_desc, NULL, ACL_WRITE ) )
1095 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1096 if ( ! access_allowed( be, conn, op, e,
1097 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1110 aci_bvstrdup( struct berval *bv )
1114 s = (char *)ch_malloc(bv->bv_len + 1);
1116 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1129 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1133 if (len > (int)bv->bv_len)
1135 if (len < (int)bv->bv_len)
1142 struct berval *list,
1156 while (len >= 0 && --ix >= 0) {
1157 while (--len >= 0 && *p++ != sep) ;
1159 while (len >= 0 && *p == ' ') {
1170 while (--len >= 0 && *p != sep) {
1174 while (bv->bv_len > 0 && *--p == ' ')
1180 aci_set_gather (void *cookie, char *name, char *attr)
1187 } *cp = (void *)cookie;
1188 struct berval **bvals = NULL;
1193 /* this routine needs to return the bervals instead of
1194 * plain strings, since syntax is not known. It should
1195 * also return the syntax or some "comparison cookie".
1198 if ((ndn = ch_strdup(name)) != NULL) {
1199 if (dn_normalize(ndn) != NULL) {
1201 AttributeDescription *desc = NULL;
1202 if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1203 backend_attribute(cp->be, NULL /*cp->conn*/,
1204 NULL /*cp->op*/, cp->e,
1206 if (bvals != NULL) {
1207 for (i = 0; bvals[i] != NULL; i++) { }
1208 vals = ch_calloc(i + 1, sizeof(char *));
1211 vals[i] = bvals[i]->bv_val;
1212 bvals[i]->bv_val = NULL;
1215 ber_bvecfree(bvals);
1227 struct berval *subj,
1245 set = aci_bvstrdup(subj);
1250 struct berval **bvals;
1252 AttributeDescription *desc = NULL;
1254 /* format of string is "entry/setAttrName" */
1255 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1259 subjdn = aci_bvstrdup(&bv);
1260 if ( subjdn == NULL ) {
1264 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1265 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1267 setat = aci_bvstrdup(&bv);
1269 if ( setat != NULL ) {
1270 if ( dn_normalize(subjdn) != NULL
1271 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1273 backend_attribute(be, NULL, NULL, e,
1274 subjdn, desc, &bvals);
1276 if ( bvals != NULL ) {
1277 if ( bvals[0] != NULL )
1278 set = ch_strdup(bvals[0]->bv_val);
1279 ber_bvecfree(bvals);
1292 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1298 #ifdef SLAPD_ACI_ENABLED
1300 aci_list_map_rights(
1301 struct berval *list )
1308 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1311 switch (*bv.bv_val) {
1313 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1316 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1317 * the right 's' to mean "set", but in the examples states
1318 * that the right 's' means "search". The latter definition
1321 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1324 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1327 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1330 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1331 * define any equivalent to the AUTH right, so I've just used
1334 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1346 struct berval *list,
1348 struct berval *val )
1350 struct berval bv, left, right;
1353 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1354 if (aci_get_part(&bv, 0, '=', &left) < 0
1355 || aci_get_part(&bv, 1, '=', &right) < 0)
1357 if (aci_strbvcmp(attr, &bv) == 0)
1359 } else if (val == NULL) {
1360 if (aci_strbvcmp(attr, &left) == 0)
1363 if (aci_strbvcmp(attr, &left) == 0) {
1364 /* this is experimental code that implements a
1365 * simple (prefix) match of the attribute value.
1366 * the ACI draft does not provide for aci's that
1367 * apply to specific values, but it would be
1368 * nice to have. If the <attr> part of an aci's
1369 * rights list is of the form <attr>=<value>,
1370 * that means the aci applies only to attrs with
1371 * the given value. Furthermore, if the attr is
1372 * of the form <attr>=<value>*, then <value> is
1373 * treated as a prefix, and the aci applies to
1374 * any value with that prefix.
1376 * Ideally, this would allow r.e. matches.
1378 if (aci_get_part(&right, 0, '*', &left) < 0
1379 || right.bv_len <= left.bv_len)
1381 if (aci_strbvcmp(val->bv_val, &right) == 0)
1383 } else if (val->bv_len >= left.bv_len) {
1384 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1393 static slap_access_t
1394 aci_list_get_attr_rights(
1395 struct berval *list,
1397 struct berval *val )
1403 /* loop through each rights/attr pair, skip first part (action) */
1405 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1406 if (aci_list_has_attr(&bv, attr, val) == 0)
1408 if (aci_get_part(list, i, ';', &bv) < 0)
1410 mask |= aci_list_map_rights(&bv);
1416 aci_list_get_rights(
1417 struct berval *list,
1420 slap_access_t *grant,
1421 slap_access_t *deny )
1423 struct berval perm, actn;
1424 slap_access_t *mask;
1427 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1434 /* loop through each permissions clause */
1435 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1436 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1438 if (aci_strbvcmp( "grant", &actn ) == 0) {
1440 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1447 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1448 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1455 struct berval *subj,
1456 const char *defgrpoc,
1457 const char *defgrpat,
1466 char *subjdn, *grpdn = NULL;
1469 ObjectClass *grp_oc = NULL;
1470 AttributeDescription *grp_ad = NULL;
1474 /* format of string is "group/objectClassValue/groupAttrName" */
1475 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1479 subjdn = aci_bvstrdup(&bv);
1480 if (subjdn == NULL) {
1484 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1485 grpoc = ch_strdup( defgrpoc );
1487 grpoc = aci_bvstrdup(&bv);
1490 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1491 grpat = ch_strdup( defgrpat );
1493 grpat = aci_bvstrdup(&bv);
1496 rc = slap_str2ad( grpat, &grp_ad, &text );
1497 if( rc != LDAP_SUCCESS ) {
1503 grp_oc = oc_find( grpoc );
1504 grpdn = (char *)ch_malloc(1024);
1506 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1507 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1508 if ( dn_normalize(grpdn) != NULL ) {
1509 rc = (backend_group(be, conn, op, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1514 if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1528 AttributeDescription *desc,
1531 regmatch_t *matches,
1532 slap_access_t *grant,
1536 struct berval bv, perms, sdn;
1539 char *attr = desc->ad_cname->bv_val;
1541 /* parse an aci of the form:
1542 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1544 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1545 a full description of the format for this attribute.
1547 For now, this routine only supports scope=entry.
1550 /* check that the aci has all 5 components */
1551 if (aci_get_part(aci, 4, '#', NULL) < 0)
1554 /* check that the aci family is supported */
1555 if (aci_get_part(aci, 0, '#', &bv) < 0)
1558 /* check that the scope is "entry" */
1559 if (aci_get_part(aci, 1, '#', &bv) < 0
1560 || aci_strbvcmp( "entry", &bv ) != 0)
1565 /* get the list of permissions clauses, bail if empty */
1566 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1569 /* check if any permissions allow desired access */
1570 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1573 /* see if we have a DN match */
1574 if (aci_get_part(aci, 3, '#', &bv) < 0)
1577 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1580 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1581 subjdn = aci_bvstrdup(&sdn);
1585 if ( dn_normalize(subjdn) != NULL )
1586 if (strcasecmp(op->o_ndn, subjdn) != 0)
1592 if (aci_strbvcmp( "self", &bv ) == 0) {
1593 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1596 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1597 char *dnattr = aci_bvstrdup(&sdn);
1599 AttributeDescription *ad = NULL;
1602 rc = slap_str2ad( dnattr, &ad, &text );
1605 if( rc != LDAP_SUCCESS ) {
1611 bv.bv_val = op->o_ndn;
1612 bv.bv_len = strlen( bv.bv_val );
1614 for(at = attrs_find( e->e_attrs, ad );
1616 at = attrs_find( at->a_next, ad ) )
1618 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1628 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1629 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, conn, op, matches))
1632 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1633 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, conn, op, matches))
1636 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1637 if (aci_match_set(&sdn, be, e, conn, op, 0))
1640 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1641 if (aci_match_set(&sdn, be, e, conn, op, 1))
1649 #endif /* SLAPD_ACI_ENABLED */
1657 regmatch_t *matches)
1666 bufsiz--; /* leave space for lone $ */
1669 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1670 /* did we previously see a $ */
1675 } else if (*sp >= '0' && *sp <= '9' ) {
1682 i = matches[n].rm_so;
1683 l = matches[n].rm_eo;
1684 for ( ; size < 512 && i < l; size++, i++ ) {
1702 /* must have ended with a single $ */
1710 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1711 "string_expand: pattern = %s\n", pat ));
1712 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1713 "string_expand: expanded = %s\n", newbuf ));
1715 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1716 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1722 char *pat, /* pattern to expand and match against */
1723 char *str, /* string to match against pattern */
1724 char *buf, /* buffer with $N expansion variables */
1725 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1732 if(str == NULL) str = "";
1734 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1735 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1737 regerror(rc, &re, error, sizeof(error));
1740 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1741 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1744 Debug( LDAP_DEBUG_TRACE,
1745 "compile( \"%s\", \"%s\") failed %s\n",
1751 rc = regexec(&re, str, 0, NULL, 0);
1755 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1756 "regex_matches: string: %s\n", str ));
1757 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1758 "regex_matches: rc: %d %s\n",
1759 rc, rc ? "matches" : "no matches" ));
1761 Debug( LDAP_DEBUG_TRACE,
1762 "=> regex_matches: string: %s\n", str, 0, 0 );
1763 Debug( LDAP_DEBUG_TRACE,
1764 "=> regex_matches: rc: %d %s\n",
1765 rc, !rc ? "matches" : "no matches", 0 );