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 ? conn->c_connid : -1, access2str( access ), e->e_dn, attr ));
106 Debug( LDAP_DEBUG_ACL,
107 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
108 access2str( access ), e->e_dn, attr );
116 if ( be == NULL ) be = &backends[0];
117 assert( be != NULL );
119 /* grant database root access */
120 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
122 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
123 "access_allowed: conn %d root access granted\n",
126 Debug( LDAP_DEBUG_ACL,
127 "<= root access granted\n",
134 * no-user-modification operational attributes are ignored
135 * by ACL_WRITE checking as any found here are not provided
138 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
139 && desc != slap_schema.si_ad_entry
140 && desc != slap_schema.si_ad_children )
143 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
144 "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
145 conn->c_connid, attr ));
147 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
148 " %s access granted\n",
154 /* use backend default access if no backend acls */
155 if( be != NULL && be->be_acl == NULL ) {
157 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
158 "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
159 conn->c_connid, access2str( access ),
160 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn ));
162 Debug( LDAP_DEBUG_ACL,
163 "=> access_allowed: backend default %s access %s to \"%s\"\n",
164 access2str( access ),
165 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
167 return be->be_dfltaccess >= access;
170 /* be is always non-NULL */
171 /* use global default access if no global acls */
172 } else if ( be == NULL && global_acl == NULL ) {
174 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
175 "access_allowed: conn %d global default %s access %s to \"%s\"\n",
176 conn->c_connid, access2str( access ),
177 global_default_access >= access ? "granted" : "denied", op->o_dn ));
179 Debug( LDAP_DEBUG_ACL,
180 "=> access_allowed: global default %s access %s to \"%s\"\n",
181 access2str( access ),
182 global_default_access >= access ? "granted" : "denied", op->o_dn );
184 return global_default_access >= access;
189 memset(matches, '\0', sizeof(matches));
195 while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
199 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
201 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
202 "access_allowed: conn %d match[%d]: %d %d ",
203 conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
205 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
206 (int)matches[i].rm_so, (int)matches[i].rm_eo );
208 if( matches[i].rm_so <= matches[0].rm_eo ) {
210 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
211 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
215 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
217 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
221 control = acl_mask( a, &mask, be, conn, op,
222 e, desc, val, matches );
224 if ( control != ACL_BREAK ) {
228 memset(matches, '\0', sizeof(matches));
231 if ( ACL_IS_INVALID( mask ) ) {
233 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
234 "access_allowed: conn %d \"%s\" (%s) invalid!\n",
235 conn->c_connid, e->e_dn, attr ));
237 Debug( LDAP_DEBUG_ACL,
238 "=> access_allowed: \"%s\" (%s) invalid!\n",
243 } else if ( control == ACL_BREAK ) {
245 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
246 "access_allowed: conn %d no more rules\n", conn->c_connid ));
248 Debug( LDAP_DEBUG_ACL,
249 "=> access_allowed: no more rules\n", 0, 0, 0);
255 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
256 "access_allowed: conn %d %s access %s by %s\n",
258 access2str( access ),
259 ACL_GRANT( mask, access ) ? "granted" : "denied",
260 accessmask2str( mask, accessmaskbuf ) ));
262 Debug( LDAP_DEBUG_ACL,
263 "=> access_allowed: %s access %s by %s\n",
264 access2str( access ),
265 ACL_GRANT(mask, access) ? "granted" : "denied",
266 accessmask2str( mask, accessmaskbuf ) );
268 return ACL_GRANT(mask, access);
272 * acl_get - return the acl applicable to entry e, attribute
273 * attr. the acl returned is suitable for use in subsequent calls to
274 * acl_access_allowed().
277 static AccessControl *
284 AttributeDescription *desc,
286 regmatch_t *matches )
292 assert( count != NULL );
293 assert( desc != NULL );
295 attr = desc->ad_cname.bv_val;
297 assert( attr != NULL );
312 dnlen = strlen(e->e_ndn);
314 for ( ; a != NULL; a = a->acl_next ) {
317 if (a->acl_dn_pat != NULL) {
318 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
320 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
321 "acl_get: dnpat [%d] %s nsub: %d\n",
322 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub ));
324 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
325 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
327 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
332 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
333 "acl_get: dn [%d] %s\n",
334 *count, a->acl_dn_pat ));
336 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
337 *count, a->acl_dn_pat, 0 );
339 patlen = strlen( a->acl_dn_pat );
340 if ( dnlen < patlen )
343 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
344 /* base dn -- entire object DN must match */
345 if ( dnlen != patlen )
348 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
352 if ( dnlen <= patlen )
355 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
358 rdn = dn_rdn( NULL, e->e_ndn );
360 rdnlen = strlen( rdn );
363 if ( rdnlen != dnlen - patlen - 1 )
366 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
367 if ( dnlen > patlen && ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) ) )
370 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
371 if ( dnlen <= patlen )
373 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
377 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
382 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
383 "acl_get: [%d] matched\n",
386 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
391 if ( a->acl_filter != NULL ) {
392 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
393 if ( rc != LDAP_COMPARE_TRUE ) {
399 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
400 "acl_get: [%d] check attr %s\n",
403 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
406 if ( attr == NULL || a->acl_attrs == NULL ||
407 ad_inlist( desc, a->acl_attrs ) )
410 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
411 "acl_get: [%d] acl %s attr: %s\n",
412 *count, e->e_dn, attr ));
414 Debug( LDAP_DEBUG_ACL,
415 "<= acl_get: [%d] acl %s attr: %s\n",
416 *count, e->e_dn, attr );
420 matches[0].rm_so = matches[0].rm_eo = -1;
424 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
425 "acl_get: done.\n" ));
427 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
434 * acl_mask - modifies mask based upon the given acl and the
435 * requested access to entry e, attribute attr, value val. if val
436 * is null, access to the whole attribute is assumed (all values).
438 * returns 0 access NOT allowed
442 static slap_control_t
450 AttributeDescription *desc,
455 int i, odnlen, patlen;
458 char accessmaskbuf[ACCESSMASK_MAXLEN];
463 assert( mask != NULL );
464 assert( desc != NULL );
466 attr = desc->ad_cname.bv_val;
468 assert( attr != NULL );
471 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
472 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
473 conn->c_connid, e->e_dn, attr ));
475 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
476 " to %s by \"%s\", (%s) \n",
477 val ? "value" : "all values",
478 op->o_ndn ? op->o_ndn : "",
479 accessmask2str( *mask, accessmaskbuf ) ));
481 Debug( LDAP_DEBUG_ACL,
482 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
485 Debug( LDAP_DEBUG_ACL,
486 "=> acl_mask: to %s by \"%s\", (%s) \n",
487 val ? "value" : "all values",
488 op->o_ndn ? op->o_ndn : "",
489 accessmask2str( *mask, accessmaskbuf ) );
492 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
493 slap_mask_t oldmask, modmask;
495 ACL_INVALIDATE( modmask );
497 /* AND <who> clauses */
498 if ( b->a_dn_pat != NULL ) {
500 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
501 "acl_mask: conn %d check a_dn_pat: %s\n",
502 conn->c_connid, b->a_dn_pat ));
504 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
508 * if access applies to the entry itself, and the
509 * user is bound as somebody in the same namespace as
510 * the entry, OR the given dn matches the dn pattern
512 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
513 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
517 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
518 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
522 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
523 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
527 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
531 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
532 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
533 int ret = regex_matches( b->a_dn_pat,
534 op->o_ndn, e->e_ndn, matches );
542 if ( e->e_dn == NULL )
545 patlen = strlen( b->a_dn_pat );
546 odnlen = strlen( op->o_ndn );
547 if ( odnlen < patlen )
550 if ( b->a_dn_style == ACL_STYLE_BASE ) {
551 /* base dn -- entire object DN must match */
552 if ( odnlen != patlen )
555 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
559 if ( odnlen <= patlen )
562 if ( !DN_SEPARATOR( op->o_ndn[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn[odnlen - patlen - 2] ) )
565 rdn = dn_rdn( NULL, op->o_ndn );
567 rdnlen = strlen( rdn );
570 if ( rdnlen != odnlen - patlen - 1 )
573 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
574 if ( odnlen > patlen && ( !DN_SEPARATOR( op->o_ndn[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn[odnlen - patlen - 2] ) ) )
577 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
578 if ( odnlen <= patlen )
580 if ( !DN_SEPARATOR( op->o_ndn[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn[odnlen - patlen - 2] ) )
584 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
590 if ( b->a_sockurl_pat != NULL ) {
592 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
593 "acl_mask: conn %d check a_sockurl_pat: %s\n",
594 conn->c_connid, b->a_sockurl_pat ));
596 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
597 b->a_sockurl_pat, 0, 0 );
600 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
601 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
602 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
603 e->e_ndn, matches ) )
608 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
614 if ( b->a_domain_pat != NULL ) {
616 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
617 "acl_mask: conn %d check a_domain_pat: %s\n",
618 conn->c_connid, b->a_domain_pat ));
620 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
621 b->a_domain_pat, 0, 0 );
623 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
624 if ( b->a_domain_style == ACL_STYLE_REGEX) {
625 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
626 e->e_ndn, matches ) )
631 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
637 if ( b->a_peername_pat != NULL ) {
639 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
640 "acl_mask: conn %d check a_perrname_path: %s\n",
641 conn->c_connid, b->a_peername_pat ));
643 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
644 b->a_peername_pat, 0, 0 );
646 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
647 if ( b->a_peername_style == ACL_STYLE_REGEX) {
648 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
649 e->e_ndn, matches ) )
654 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
660 if ( b->a_sockname_pat != NULL ) {
662 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
663 "acl_mask: conn %d check a_sockname_path: %s\n",
664 conn->c_connid, b->a_sockname_pat ));
666 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
667 b->a_sockname_pat, 0, 0 );
669 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
670 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
671 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
672 e->e_ndn, matches ) )
677 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
683 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
688 const char *attr = b->a_dn_at->ad_cname.bv_val;
690 assert( attr != NULL );
693 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
694 "acl_mask: conn %d check a_dn_pat: %s\n",
695 conn->c_connid, attr ));
697 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
700 bv.bv_val = op->o_ndn;
701 bv.bv_len = strlen( bv.bv_val );
703 /* see if asker is listed in dnattr */
704 for( at = attrs_find( e->e_attrs, b->a_dn_at );
706 at = attrs_find( at->a_next, b->a_dn_at ) )
708 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
716 /* have a dnattr match. if this is a self clause then
717 * the target must also match the op dn.
719 if ( b->a_dn_self ) {
720 /* check if the target is an attribute. */
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,
729 /* on match error or no match, fail the ACL clause */
730 if (rc != LDAP_SUCCESS || match != 0 )
734 /* no dnattr match, check if this is a self clause */
735 if ( ! b->a_dn_self )
737 /* this is a self clause, check if the target is an
742 /* target is attribute, check if the attribute value
745 rc = value_match( &match, b->a_dn_at,
746 b->a_dn_at->ad_type->sat_equality, 0,
749 /* on match error or no match, fail the ACL clause */
750 if (rc != LDAP_SUCCESS || match != 0 )
755 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
758 /* b->a_group is an unexpanded entry name, expanded it should be an
759 * entry with objectclass group* and we test to see if odn is one of
760 * the values in the attribute group
762 /* see if asker is listed in dnattr */
763 if ( b->a_group_style == ACL_STYLE_REGEX ) {
764 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
765 if ( dn_normalize(buf) == NULL ) {
766 /* did not expand to a valid dn */
770 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
771 buf[sizeof(buf) - 1] = 0;
774 if (backend_group(be, conn, op, e, buf, op->o_ndn,
775 b->a_group_oc, b->a_group_at) != 0)
781 if ( b->a_set_pat != NULL ) {
784 bv.bv_val = b->a_set_pat;
785 bv.bv_len = strlen(b->a_set_pat);
786 if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
791 if ( b->a_authz.sai_ssf ) {
793 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
794 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
795 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
797 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
798 b->a_authz.sai_ssf, op->o_ssf, 0 );
800 if ( b->a_authz.sai_ssf > op->o_ssf ) {
805 if ( b->a_authz.sai_transport_ssf ) {
807 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
808 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
809 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
811 Debug( LDAP_DEBUG_ACL,
812 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
813 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
815 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
820 if ( b->a_authz.sai_tls_ssf ) {
822 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
823 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
824 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
826 Debug( LDAP_DEBUG_ACL,
827 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
828 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
830 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
835 if ( b->a_authz.sai_sasl_ssf ) {
837 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
838 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
839 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
841 Debug( LDAP_DEBUG_ACL,
842 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
843 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
845 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
850 #ifdef SLAPD_ACI_ENABLED
851 if ( b->a_aci_at != NULL ) {
853 slap_access_t grant, deny, tgrant, tdeny;
855 /* this case works different from the others above.
856 * since aci's themselves give permissions, we need
857 * to first check b->a_access_mask, the ACL's access level.
860 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
864 if ( e->e_dn == NULL ) {
868 /* first check if the right being requested
869 * is allowed by the ACL clause.
871 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
875 /* get the aci attribute */
876 at = attr_find( e->e_attrs, b->a_aci_at );
881 /* start out with nothing granted, nothing denied */
885 /* the aci is an multi-valued attribute. The
886 * rights are determined by OR'ing the individual
887 * rights given by the acis.
889 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
890 if (aci_mask( be, conn, op,
891 e, desc, val, at->a_vals[i],
892 matches, &grant, &deny ) != 0)
899 /* remove anything that the ACL clause does not allow */
900 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
901 tdeny &= ACL_PRIV_MASK;
903 /* see if we have anything to contribute */
904 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
908 /* this could be improved by changing acl_mask so that it can deal with
909 * by clauses that return grant/deny pairs. Right now, it does either
910 * additive or subtractive rights, but not both at the same time. So,
911 * we need to combine the grant/deny pair into a single rights mask in
912 * a smart way: if either grant or deny is "empty", then we use the
913 * opposite as is, otherwise we remove any denied rights from the grant
914 * rights mask and construct an additive mask.
916 if (ACL_IS_INVALID(tdeny)) {
917 modmask = tgrant | ACL_PRIV_ADDITIVE;
919 } else if (ACL_IS_INVALID(tgrant)) {
920 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
923 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
929 modmask = b->a_access_mask;
933 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
934 "acl_mask: conn %d [%d] applying %s (%s)\n",
935 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
936 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
937 ? "break" : "stop" ));
939 Debug( LDAP_DEBUG_ACL,
940 "<= acl_mask: [%d] applying %s (%s)\n",
941 i, accessmask2str( modmask, accessmaskbuf ),
942 b->a_type == ACL_CONTINUE
944 : b->a_type == ACL_BREAK
951 if( ACL_IS_ADDITIVE(modmask) ) {
953 ACL_PRIV_SET( *mask, modmask );
956 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
958 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
959 /* substract privs */
960 ACL_PRIV_CLR( *mask, modmask );
963 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
971 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
972 "acl_mask: conn %d [%d] mask: %s\n",
973 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
975 Debug( LDAP_DEBUG_ACL,
976 "<= acl_mask: [%d] mask: %s\n",
977 i, accessmask2str(*mask, accessmaskbuf), 0 );
980 if( b->a_type == ACL_CONTINUE ) {
983 } else if ( b->a_type == ACL_BREAK ) {
991 /* implicit "by * none" clause */
995 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
996 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
997 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
999 Debug( LDAP_DEBUG_ACL,
1000 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1001 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1007 * acl_check_modlist - check access control on the given entry to see if
1008 * it allows the given modifications by the user associated with op.
1009 * returns 1 if mods allowed ok
1010 * 0 mods not allowed
1019 Modifications *mlist
1024 assert( be != NULL );
1026 /* short circuit root database access */
1027 if ( be_isroot( be, op->o_ndn ) ) {
1029 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1030 "acl_check_modlist: conn %d access granted to root user\n",
1033 Debug( LDAP_DEBUG_ACL,
1034 "<= acl_access_allowed: granted to database root\n",
1040 /* use backend default access if no backend acls */
1041 if( be != NULL && be->be_acl == NULL ) {
1043 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1044 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1045 conn->c_connid, access2str( ACL_WRITE ),
1046 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1048 Debug( LDAP_DEBUG_ACL,
1049 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1050 access2str( ACL_WRITE ),
1051 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1053 return be->be_dfltaccess >= ACL_WRITE;
1056 /* be is always non-NULL */
1057 /* use global default access if no global acls */
1058 } else if ( be == NULL && global_acl == NULL ) {
1060 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1061 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1062 conn->c_connid, access2str( ACL_WRITE ),
1063 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1065 Debug( LDAP_DEBUG_ACL,
1066 "=> access_allowed: global default %s access %s to \"%s\"\n",
1067 access2str( ACL_WRITE ),
1068 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1070 return global_default_access >= ACL_WRITE;
1074 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1076 * no-user-modification operational attributes are ignored
1077 * by ACL_WRITE checking as any found here are not provided
1080 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1082 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1083 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1084 conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1086 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1087 " modify access granted\n",
1088 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1093 switch ( mlist->sml_op ) {
1094 case LDAP_MOD_REPLACE:
1096 if ( mlist->sml_bvalues == NULL ) {
1099 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1100 if ( ! access_allowed( be, conn, op, e,
1101 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1108 case LDAP_MOD_DELETE:
1109 if ( mlist->sml_bvalues == NULL ) {
1110 if ( ! access_allowed( be, conn, op, e,
1111 mlist->sml_desc, NULL, ACL_WRITE ) )
1117 for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
1118 if ( ! access_allowed( be, conn, op, e,
1119 mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
1132 aci_bvstrdup( struct berval *bv )
1136 s = (char *)ch_malloc(bv->bv_len + 1);
1138 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1151 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1155 if (len > (int)bv->bv_len)
1157 if (len < (int)bv->bv_len)
1164 struct berval *list,
1178 while (len >= 0 && --ix >= 0) {
1179 while (--len >= 0 && *p++ != sep) ;
1181 while (len >= 0 && *p == ' ') {
1192 while (--len >= 0 && *p != sep) {
1196 while (bv->bv_len > 0 && *--p == ' ')
1202 aci_set_gather (void *cookie, char *name, char *attr)
1209 } *cp = (void *)cookie;
1210 struct berval **bvals = NULL;
1215 /* this routine needs to return the bervals instead of
1216 * plain strings, since syntax is not known. It should
1217 * also return the syntax or some "comparison cookie".
1220 if ((ndn = ch_strdup(name)) != NULL) {
1221 if (dn_normalize(ndn) != NULL) {
1223 AttributeDescription *desc = NULL;
1224 if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1225 backend_attribute(cp->be, NULL /*cp->conn*/,
1226 NULL /*cp->op*/, cp->e,
1228 if (bvals != NULL) {
1229 for (i = 0; bvals[i] != NULL; i++) { }
1230 vals = ch_calloc(i + 1, sizeof(char *));
1233 vals[i] = bvals[i]->bv_val;
1234 bvals[i]->bv_val = NULL;
1237 ber_bvecfree(bvals);
1248 struct berval *subj,
1266 set = aci_bvstrdup(subj);
1271 struct berval **bvals;
1273 AttributeDescription *desc = NULL;
1275 /* format of string is "entry/setAttrName" */
1276 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1280 subjdn = aci_bvstrdup(&bv);
1281 if ( subjdn == NULL ) {
1285 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1286 setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1288 setat = aci_bvstrdup(&bv);
1290 if ( setat != NULL ) {
1291 if ( dn_normalize(subjdn) != NULL
1292 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1294 backend_attribute(be, NULL, NULL, e,
1295 subjdn, desc, &bvals);
1296 if ( bvals != NULL ) {
1297 if ( bvals[0] != NULL )
1298 set = ch_strdup(bvals[0]->bv_val);
1299 ber_bvecfree(bvals);
1312 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1318 #ifdef SLAPD_ACI_ENABLED
1320 aci_list_map_rights(
1321 struct berval *list )
1328 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1331 switch (*bv.bv_val) {
1333 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1336 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1337 * the right 's' to mean "set", but in the examples states
1338 * that the right 's' means "search". The latter definition
1341 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1344 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1347 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1350 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1351 * define any equivalent to the AUTH right, so I've just used
1354 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1366 struct berval *list,
1368 struct berval *val )
1370 struct berval bv, left, right;
1373 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1374 if (aci_get_part(&bv, 0, '=', &left) < 0
1375 || aci_get_part(&bv, 1, '=', &right) < 0)
1377 if (aci_strbvcmp(attr, &bv) == 0)
1379 } else if (val == NULL) {
1380 if (aci_strbvcmp(attr, &left) == 0)
1383 if (aci_strbvcmp(attr, &left) == 0) {
1384 /* this is experimental code that implements a
1385 * simple (prefix) match of the attribute value.
1386 * the ACI draft does not provide for aci's that
1387 * apply to specific values, but it would be
1388 * nice to have. If the <attr> part of an aci's
1389 * rights list is of the form <attr>=<value>,
1390 * that means the aci applies only to attrs with
1391 * the given value. Furthermore, if the attr is
1392 * of the form <attr>=<value>*, then <value> is
1393 * treated as a prefix, and the aci applies to
1394 * any value with that prefix.
1396 * Ideally, this would allow r.e. matches.
1398 if (aci_get_part(&right, 0, '*', &left) < 0
1399 || right.bv_len <= left.bv_len)
1401 if (aci_strbvcmp(val->bv_val, &right) == 0)
1403 } else if (val->bv_len >= left.bv_len) {
1404 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1413 static slap_access_t
1414 aci_list_get_attr_rights(
1415 struct berval *list,
1417 struct berval *val )
1423 /* loop through each rights/attr pair, skip first part (action) */
1425 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1426 if (aci_list_has_attr(&bv, attr, val) == 0)
1428 if (aci_get_part(list, i, ';', &bv) < 0)
1430 mask |= aci_list_map_rights(&bv);
1436 aci_list_get_rights(
1437 struct berval *list,
1440 slap_access_t *grant,
1441 slap_access_t *deny )
1443 struct berval perm, actn;
1444 slap_access_t *mask;
1447 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1454 /* loop through each permissions clause */
1455 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1456 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1458 if (aci_strbvcmp( "grant", &actn ) == 0) {
1460 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1467 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1468 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1475 struct berval *subj,
1476 const char *defgrpoc,
1477 const char *defgrpat,
1486 char *subjdn, *grpdn = NULL;
1489 ObjectClass *grp_oc = NULL;
1490 AttributeDescription *grp_ad = NULL;
1494 /* format of string is "group/objectClassValue/groupAttrName" */
1495 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1499 subjdn = aci_bvstrdup(&bv);
1500 if (subjdn == NULL) {
1504 if (aci_get_part(subj, 1, '/', &bv) < 0) {
1505 grpoc = ch_strdup( defgrpoc );
1507 grpoc = aci_bvstrdup(&bv);
1510 if (aci_get_part(subj, 2, '/', &bv) < 0) {
1511 grpat = ch_strdup( defgrpat );
1513 grpat = aci_bvstrdup(&bv);
1516 rc = slap_str2ad( grpat, &grp_ad, &text );
1517 if( rc != LDAP_SUCCESS ) {
1523 grp_oc = oc_find( grpoc );
1524 grpdn = (char *)ch_malloc(1024);
1526 if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1527 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1528 if ( dn_normalize(grpdn) != NULL ) {
1529 rc = (backend_group(be, conn, op, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1547 AttributeDescription *desc,
1550 regmatch_t *matches,
1551 slap_access_t *grant,
1555 struct berval bv, perms, sdn;
1558 char *attr = desc->ad_cname.bv_val;
1560 assert( attr != NULL );
1562 /* parse an aci of the form:
1563 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1565 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1566 a full description of the format for this attribute.
1568 For now, this routine only supports scope=entry.
1571 /* check that the aci has all 5 components */
1572 if (aci_get_part(aci, 4, '#', NULL) < 0)
1575 /* check that the aci family is supported */
1576 if (aci_get_part(aci, 0, '#', &bv) < 0)
1579 /* check that the scope is "entry" */
1580 if (aci_get_part(aci, 1, '#', &bv) < 0
1581 || aci_strbvcmp( "entry", &bv ) != 0)
1586 /* get the list of permissions clauses, bail if empty */
1587 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1590 /* check if any permissions allow desired access */
1591 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1594 /* see if we have a DN match */
1595 if (aci_get_part(aci, 3, '#', &bv) < 0)
1598 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1601 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1602 subjdn = aci_bvstrdup(&sdn);
1606 if ( dn_normalize(subjdn) != NULL )
1607 if (strcasecmp(op->o_ndn, subjdn) != 0)
1613 if (aci_strbvcmp( "self", &bv ) == 0) {
1614 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1617 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1618 char *dnattr = aci_bvstrdup(&sdn);
1620 AttributeDescription *ad = NULL;
1623 rc = slap_str2ad( dnattr, &ad, &text );
1626 if( rc != LDAP_SUCCESS ) {
1632 bv.bv_val = op->o_ndn;
1633 bv.bv_len = strlen( bv.bv_val );
1635 for(at = attrs_find( e->e_attrs, ad );
1637 at = attrs_find( at->a_next, ad ) )
1639 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1648 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1649 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, conn, op, matches))
1652 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1653 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, conn, op, matches))
1656 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1657 if (aci_match_set(&sdn, be, e, conn, op, 0))
1660 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1661 if (aci_match_set(&sdn, be, e, conn, op, 1))
1669 #endif /* SLAPD_ACI_ENABLED */
1677 regmatch_t *matches)
1686 bufsiz--; /* leave space for lone $ */
1689 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1690 /* did we previously see a $ */
1695 } else if (*sp >= '0' && *sp <= '9' ) {
1702 i = matches[n].rm_so;
1703 l = matches[n].rm_eo;
1704 for ( ; size < 512 && i < l; size++, i++ ) {
1722 /* must have ended with a single $ */
1730 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1731 "string_expand: pattern = %s\n", pat ));
1732 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1733 "string_expand: expanded = %s\n", newbuf ));
1735 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1736 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1742 char *pat, /* pattern to expand and match against */
1743 char *str, /* string to match against pattern */
1744 char *buf, /* buffer with $N expansion variables */
1745 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1752 if(str == NULL) str = "";
1754 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1755 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1757 regerror(rc, &re, error, sizeof(error));
1760 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1761 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1764 Debug( LDAP_DEBUG_TRACE,
1765 "compile( \"%s\", \"%s\") failed %s\n",
1771 rc = regexec(&re, str, 0, NULL, 0);
1775 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1776 "regex_matches: string: %s\n", str ));
1777 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1778 "regex_matches: rc: %d %s\n",
1779 rc, rc ? "matches" : "no matches" ));
1781 Debug( LDAP_DEBUG_TRACE,
1782 "=> regex_matches: string: %s\n", str, 0, 0 );
1783 Debug( LDAP_DEBUG_TRACE,
1784 "=> regex_matches: rc: %d %s\n",
1785 rc, !rc ? "matches" : "no matches", 0 );